|
@ -23,26 +23,28 @@ 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; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
UserRoleRepository userRoleRepository; |
|
|
UserRoleRepository userRoleRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
PasswordEncoder passwordEncoder; |
|
|
PasswordEncoder passwordEncoder; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
RoleService roleService; |
|
|
RoleService roleService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
UserRepositoryCustom userRepositoryCustom; |
|
|
UserRepositoryCustom userRepositoryCustom; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private UserSessionRepository userSessionRepository; |
|
|
private UserSessionRepository userSessionRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
@Autowired |
|
|
private ActiveUserStore activeUserStore; |
|
|
private ActiveUserStore activeUserStore; |
|
|
|
|
|
|
|
@ -77,7 +79,7 @@ public class UserServiceImpl implements UserService{ |
|
|
@Override |
|
|
@Override |
|
|
public List<String> findAllOtherUsers(String userName) { |
|
|
public List<String> findAllOtherUsers(String userName) { |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
return userRepositoryCustom.getAllUserNames(userName); |
|
|
|
|
|
|
|
|
return userRepositoryCustom.getAllUserNames(userName); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -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)); |
|
|
|
|
|
if(onlineUsers.contains(u)) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u))); |
|
|
|
|
|
if (onlineUsers.contains(u)) { |
|
|
activeUserDTO.setOnline(true); |
|
|
activeUserDTO.setOnline(true); |
|
|
} |
|
|
} |
|
|
activeUserDTOs.add(activeUserDTO); |
|
|
activeUserDTOs.add(activeUserDTO); |
|
@ -111,13 +111,49 @@ public class UserServiceImpl implements UserService{ |
|
|
// TODO Auto-generated method stub |
|
|
// TODO Auto-generated method stub |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |