proper formatting for last active
This commit is contained in:
parent
74396ccd68
commit
d1a2d58411
@ -8,5 +8,5 @@ import lombok.Data;
|
|||||||
public class ActiveUserDTO {
|
public class ActiveUserDTO {
|
||||||
private String userName;
|
private String userName;
|
||||||
private Boolean online;
|
private Boolean online;
|
||||||
private Instant lastActive;
|
private String lastActive;
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService{
|
public class UserServiceImpl implements UserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
@ -86,7 +88,6 @@ public class UserServiceImpl implements UserService{
|
|||||||
return userRoleRepository.getAllRegularUser();
|
return userRoleRepository.getAllRegularUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
|
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
|
||||||
List<String> userList = findAllOtherUsers(userName);
|
List<String> userList = findAllOtherUsers(userName);
|
||||||
List<String> onlineUsers = activeUserStore.getUsers();
|
List<String> onlineUsers = activeUserStore.getUsers();
|
||||||
@ -96,9 +97,8 @@ public class UserServiceImpl implements UserService{
|
|||||||
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
||||||
activeUserDTO.setUserName(u);
|
activeUserDTO.setUserName(u);
|
||||||
activeUserDTO.setOnline(false);
|
activeUserDTO.setOnline(false);
|
||||||
activeUserDTO.setLastActive(lastActiveMap.get(u));
|
activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u)));
|
||||||
if(onlineUsers.contains(u))
|
if (onlineUsers.contains(u)) {
|
||||||
{
|
|
||||||
activeUserDTO.setOnline(true);
|
activeUserDTO.setOnline(true);
|
||||||
}
|
}
|
||||||
activeUserDTOs.add(activeUserDTO);
|
activeUserDTOs.add(activeUserDTO);
|
||||||
@ -112,12 +112,48 @@ public class UserServiceImpl implements UserService{
|
|||||||
return userRepository.findByUserName(userName);
|
return userRepository.findByUserName(userName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Instant> convertToMap(List<UserSession> userSessionList)
|
private Map<String, Instant> convertToMap(List<UserSession> userSessionList) {
|
||||||
{
|
|
||||||
Map<String, Instant> userMap = new HashMap<>();
|
Map<String, Instant> userMap = new HashMap<>();
|
||||||
userSessionList.forEach(us -> {
|
userSessionList.forEach(us -> {
|
||||||
userMap.put(us.getUser().getUserName(), us.getTimeStamp());
|
userMap.put(us.getUser().getUserName(), us.getTimeStamp());
|
||||||
});
|
});
|
||||||
return userMap;
|
return userMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String toLastActiveString(Instant lastActive) {
|
||||||
|
if (lastActive == null)
|
||||||
|
return null;
|
||||||
|
Duration duration = Duration.between(lastActive, Instant.now());
|
||||||
|
long hours = duration.toHours();
|
||||||
|
long minutes = duration.toMinutes();
|
||||||
|
long days = duration.toDays();
|
||||||
|
// @formatter:off
|
||||||
|
if(minutes < 60)
|
||||||
|
{
|
||||||
|
return String.format("%d minutes ago", minutes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(hours < 24)
|
||||||
|
{
|
||||||
|
return String.format("%d hours ago", hours);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(days < 30)
|
||||||
|
{
|
||||||
|
return String.format("%d days ago", days);
|
||||||
|
}
|
||||||
|
else if(days < 366)
|
||||||
|
{
|
||||||
|
return String.format("%d months ago", days/30);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return String.format("%d years ago", days/365);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// @formatter:on
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@
|
|||||||
<p th:text="'User has not logged in yet'"></p>
|
<p th:text="'User has not logged in yet'"></p>
|
||||||
</th:block>
|
</th:block>
|
||||||
<th:block th:if="${au.lastActive != null}">
|
<th:block th:if="${au.lastActive != null}">
|
||||||
<p th:text="'Last active = ' + ${au.lastActive}">Last active 3 hours ago</p>
|
<p th:text="'Last active ' + ${au.lastActive}">Last active 3 hours ago</p>
|
||||||
</th:block>
|
</th:block>
|
||||||
<!-- <p th:case="${au.online == true}" th:text="${au.userName} + ' is online'">Khalid is online</p>
|
<!-- <p th:case="${au.online == true}" th:text="${au.userName} + ' is online'">Khalid is online</p>
|
||||||
<p th:if="${au.online == false}" th:text="${au.userName} + ' is offline' + ' Last active = ' + ${au.lastActive}">Khalid is offline. -->
|
<p th:if="${au.online == false}" th:text="${au.userName} + ' is offline' + ' Last active = ' + ${au.lastActive}">Khalid is offline. -->
|
||||||
|
Loading…
Reference in New Issue
Block a user