proper formatting for last active

This commit is contained in:
Rohan Sircar 2019-11-13 20:34:31 +05:30
parent 74396ccd68
commit d1a2d58411
3 changed files with 53 additions and 17 deletions

View File

@ -8,5 +8,5 @@ import lombok.Data;
public class ActiveUserDTO {
private String userName;
private Boolean online;
private Instant lastActive;
private String lastActive;
}

View File

@ -23,8 +23,10 @@ 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;
@ -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);
@ -112,12 +112,48 @@ public class UserServiceImpl implements UserService{
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
}
}

View File

@ -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. -->