From d1a2d5841137035aead462acbcc06cfab04916d7 Mon Sep 17 00:00:00 2001 From: Rohan Sircar Date: Wed, 13 Nov 2019 20:34:31 +0530 Subject: [PATCH] proper formatting for last active --- .../org/ros/chatto/dto/ActiveUserDTO.java | 2 +- .../ros/chatto/service/UserServiceImpl.java | 66 ++++++++++++++----- chatto/src/main/resources/templates/chat.html | 2 +- 3 files changed, 53 insertions(+), 17 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/dto/ActiveUserDTO.java b/chatto/src/main/java/org/ros/chatto/dto/ActiveUserDTO.java index d15ad8e..72e16d9 100644 --- a/chatto/src/main/java/org/ros/chatto/dto/ActiveUserDTO.java +++ b/chatto/src/main/java/org/ros/chatto/dto/ActiveUserDTO.java @@ -8,5 +8,5 @@ import lombok.Data; public class ActiveUserDTO { private String userName; private Boolean online; - private Instant lastActive; + private String lastActive; } diff --git a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java index edfba7f..e24ea39 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java @@ -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 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 getOtherActiveUsers(String userName) { List userList = findAllOtherUsers(userName); List 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 convertToMap(List userSessionList) - { + + private Map convertToMap(List userSessionList) { Map 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 + + } } diff --git a/chatto/src/main/resources/templates/chat.html b/chatto/src/main/resources/templates/chat.html index c7bf5bb..0258840 100644 --- a/chatto/src/main/resources/templates/chat.html +++ b/chatto/src/main/resources/templates/chat.html @@ -89,7 +89,7 @@

-

Last active 3 hours ago

+

Last active 3 hours ago