You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.2 KiB
148 lines
4.2 KiB
package org.ros.chatto.service;
|
|
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.ros.chatto.dto.ActiveUserDTO;
|
|
import org.ros.chatto.dto.UserRegistrationDTO;
|
|
import org.ros.chatto.model.ChatUser;
|
|
import org.ros.chatto.model.Role;
|
|
import org.ros.chatto.model.UserRole;
|
|
import org.ros.chatto.model.UserSession;
|
|
import org.ros.chatto.repository.RoleRepository;
|
|
import org.ros.chatto.repository.UserRepository;
|
|
import org.ros.chatto.repository.UserRoleRepository;
|
|
import org.ros.chatto.repository.UserSessionRepository;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
@Transactional
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class UserServiceImpl implements UserService {
|
|
private final UserRepository userRepository;
|
|
|
|
private final UserRoleRepository userRoleRepository;
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
private final RoleRepository roleRepository;
|
|
|
|
private final UserSessionRepository userSessionRepository;
|
|
|
|
@Override
|
|
public UserRole registerUser(final UserRegistrationDTO userRegistrationDTO) {
|
|
final ChatUser user = new ChatUser();
|
|
user.setUserName(userRegistrationDTO.getUserName());
|
|
user.setPassword(passwordEncoder.encode(userRegistrationDTO.getPassword()));
|
|
final ChatUser changedUser = userRepository.save(user);
|
|
final UserRole userRole = new UserRole();
|
|
final Role role = roleRepository.findByName("USER");
|
|
userRole.setRole(role);
|
|
userRole.setUser(changedUser);
|
|
userRoleRepository.save(userRole);
|
|
return userRole;
|
|
}
|
|
|
|
@Override
|
|
@Transactional(readOnly = true)
|
|
public List<String> findAllOtherUsers(final String userName) {
|
|
return userRepository.findAllOtherUserNames(userName);
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
@Override
|
|
public List<String> getAllRegularUsers() {
|
|
return userRoleRepository.getAllRegularUser();
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
public List<ActiveUserDTO> getOtherActiveUsers(final String userName) {
|
|
final List<String> userList = findAllOtherUsers(userName);
|
|
|
|
final List<UserSession> userSessionsList = userSessionRepository.findAllUserSessions();
|
|
final Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
|
|
|
|
final List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
|
|
userList.forEach(u -> {
|
|
final ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
|
final UserSession us = lastActiveMap.get(u);
|
|
|
|
activeUserDTO.setUserName(u);
|
|
activeUserDTO.setOnline(false);
|
|
activeUserDTO.setLastActive(null);
|
|
if (us != null) {
|
|
activeUserDTO.setOnline(us.isOnline());
|
|
activeUserDTO.setLastActive(toLastActiveString(us.getTimeStamp()));
|
|
}
|
|
|
|
activeUserDTOs.add(activeUserDTO);
|
|
});
|
|
|
|
return activeUserDTOs;
|
|
}
|
|
|
|
@Transactional(readOnly = true)
|
|
@Override
|
|
public ChatUser findByUserName(final String userName) {
|
|
return userRepository.findByUserName(userName);
|
|
}
|
|
|
|
private Map<String, UserSession> convertToMap(final List<UserSession> userSessionList) {
|
|
final Map<String, UserSession> userMap = new HashMap<>();
|
|
userSessionList.forEach(us -> {
|
|
userMap.put(us.getUser().getUserName(), us);
|
|
});
|
|
return userMap;
|
|
}
|
|
|
|
private String toLastActiveString(final Instant lastActive) {
|
|
if (lastActive == null)
|
|
return null;
|
|
final Duration duration = Duration.between(lastActive, Instant.now());
|
|
final long hours = duration.toHours();
|
|
final long minutes = duration.toMinutes();
|
|
final 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
|
|
|
|
}
|
|
|
|
@Override
|
|
@Transactional(readOnly = true)
|
|
public List<UserRole> getUserWithRole(final String userName) {
|
|
return userRoleRepository.findByUser(userName);
|
|
}
|
|
}
|