proper formatting for last active
This commit is contained in:
parent
74396ccd68
commit
d1a2d58411
@ -8,5 +8,5 @@ import lombok.Data;
|
||||
public class ActiveUserDTO {
|
||||
private String userName;
|
||||
private Boolean online;
|
||||
private Instant lastActive;
|
||||
private String lastActive;
|
||||
}
|
||||
|
@ -23,26 +23,28 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService{
|
||||
public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
UserRoleRepository userRoleRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
PasswordEncoder passwordEncoder;
|
||||
|
||||
|
||||
@Autowired
|
||||
RoleService roleService;
|
||||
|
||||
|
||||
@Autowired
|
||||
UserRepositoryCustom userRepositoryCustom;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserSessionRepository userSessionRepository;
|
||||
|
||||
|
||||
@Autowired
|
||||
private ActiveUserStore activeUserStore;
|
||||
|
||||
@ -77,7 +79,7 @@ public class UserServiceImpl implements UserService{
|
||||
@Override
|
||||
public List<String> findAllOtherUsers(String userName) {
|
||||
// TODO Auto-generated method stub
|
||||
return userRepositoryCustom.getAllUserNames(userName);
|
||||
return userRepositoryCustom.getAllUserNames(userName);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -86,7 +88,6 @@ public class UserServiceImpl implements UserService{
|
||||
return userRoleRepository.getAllRegularUser();
|
||||
}
|
||||
|
||||
|
||||
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
|
||||
List<String> userList = findAllOtherUsers(userName);
|
||||
List<String> onlineUsers = activeUserStore.getUsers();
|
||||
@ -96,9 +97,8 @@ public class UserServiceImpl implements UserService{
|
||||
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
||||
activeUserDTO.setUserName(u);
|
||||
activeUserDTO.setOnline(false);
|
||||
activeUserDTO.setLastActive(lastActiveMap.get(u));
|
||||
if(onlineUsers.contains(u))
|
||||
{
|
||||
activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u)));
|
||||
if (onlineUsers.contains(u)) {
|
||||
activeUserDTO.setOnline(true);
|
||||
}
|
||||
activeUserDTOs.add(activeUserDTO);
|
||||
@ -111,13 +111,49 @@ public class UserServiceImpl implements UserService{
|
||||
// TODO Auto-generated method stub
|
||||
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<>();
|
||||
userSessionList.forEach(us -> {
|
||||
userMap.put(us.getUser().getUserName(), us.getTimeStamp());
|
||||
});
|
||||
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>
|
||||
</th:block>
|
||||
<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>
|
||||
<!-- <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. -->
|
||||
|
Loading…
Reference in New Issue
Block a user