Switched to persistent storage of active users. This solves the issue of tracking active user with multiple sessions
This commit is contained in:
parent
d1a2d58411
commit
2564fdca35
@ -1,7 +1,5 @@
|
||||
package org.ros.chatto.dto;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
|
@ -7,5 +7,6 @@ import lombok.Data;
|
||||
@Data
|
||||
public class ActiveUser {
|
||||
private String userName;
|
||||
private Instant lastActive;
|
||||
// private Instant lastActive;
|
||||
private int numSessions = 0;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package org.ros.chatto.logged;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.http.HttpSessionBindingEvent;
|
||||
import javax.servlet.http.HttpSessionBindingListener;
|
||||
@ -11,6 +12,7 @@ import org.ros.chatto.model.ChatUser;
|
||||
import org.ros.chatto.model.UserSession;
|
||||
import org.ros.chatto.repository.UserSessionRepository;
|
||||
import org.ros.chatto.service.UserService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
@ -47,25 +49,31 @@ public class LoggedUser implements HttpSessionBindingListener {
|
||||
}
|
||||
|
||||
Instant instant = Instant.now();
|
||||
boolean found = activeUsers.stream().anyMatch(au -> au.getUserName().equals(user.getUsername()));
|
||||
if (!found) {
|
||||
System.out.println("Test found ");
|
||||
ActiveUser activeUser = new ActiveUser();
|
||||
activeUser.setUserName(user.getUsername());
|
||||
activeUser.setLastActive(instant);
|
||||
activeUsers.add(activeUser);
|
||||
}
|
||||
// Optional<ActiveUser> activeUserOptional = activeUsers.stream()
|
||||
// .filter(au -> au.getUserName().equals(user.getUsername()))
|
||||
// .findFirst();
|
||||
// ActiveUser activeUser2 = activeUserOptional.get();
|
||||
// activeUser2.setNumSessions(activeUser2.getNumSessions() + 1);
|
||||
// boolean found = activeUsers.stream().anyMatch(au -> au.getUserName().equals(user.getUsername()));
|
||||
// if (!found) {
|
||||
// System.out.println("Test found ");
|
||||
// ActiveUser activeUser = new ActiveUser();
|
||||
// activeUser.setUserName(user.getUsername());
|
||||
//// activeUser.setLastActive(instant);
|
||||
// activeUsers.add(activeUser);
|
||||
// }
|
||||
ChatUser chatUser = userService.findByUserName(user.getUsername());
|
||||
|
||||
UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
|
||||
|
||||
if(userSession == null)
|
||||
{
|
||||
|
||||
if (userSession == null) {
|
||||
userSession = new UserSession();
|
||||
}
|
||||
|
||||
|
||||
userSession.setUser(chatUser);
|
||||
userSession.setTimeStamp(instant);
|
||||
userSession.setOnline(true);
|
||||
userSession.setNumSessions(userSession.getNumSessions() + 1);
|
||||
userSessionRepository.save(userSession);
|
||||
}
|
||||
|
||||
@ -74,12 +82,46 @@ public class LoggedUser implements HttpSessionBindingListener {
|
||||
List<String> users = activeUserStore.getUsers();
|
||||
LoggedUser user = (LoggedUser) event.getValue();
|
||||
List<ActiveUser> activeUsers = activeUserStore.getActiveUsers();
|
||||
|
||||
UserService userService = getUserService(event);
|
||||
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
|
||||
Instant instant = Instant.now();
|
||||
|
||||
if (users.contains(user.getUsername())) {
|
||||
users.remove(user.getUsername());
|
||||
}
|
||||
|
||||
activeUsers.removeIf(au -> au.getUserName().equals(user.getUsername()));
|
||||
activeUsers.removeIf(au -> {
|
||||
int numSessions = au.getNumSessions();
|
||||
if (au.getUserName().equals(user.getUsername()) && --numSessions == 0) {
|
||||
au.setNumSessions(numSessions);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
ChatUser chatUser = userService.findByUserName(user.getUsername());
|
||||
|
||||
UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
|
||||
|
||||
if (userSession == null) {
|
||||
// userSession = new UserSession();
|
||||
throw new UsernameNotFoundException("User session not found");
|
||||
}
|
||||
|
||||
int numSessions = userSession.getNumSessions();
|
||||
|
||||
if(--numSessions == 0)
|
||||
{
|
||||
userSession.setOnline(false);
|
||||
}
|
||||
|
||||
userSession.setUser(chatUser);
|
||||
userSession.setTimeStamp(instant);
|
||||
userSession.setNumSessions(numSessions);
|
||||
userSessionRepository.save(userSession);
|
||||
}
|
||||
|
||||
private UserService getUserService(HttpSessionEvent se) {
|
||||
|
@ -25,6 +25,10 @@ public class UserSession {
|
||||
@OneToOne
|
||||
@JoinColumn(name = "user_id")
|
||||
private ChatUser user;
|
||||
|
||||
private boolean online;
|
||||
|
||||
private int numSessions;
|
||||
// private Boolean loggedIn;
|
||||
private Instant timeStamp;
|
||||
}
|
||||
|
@ -90,19 +90,38 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
public List<ActiveUserDTO> getOtherActiveUsers(String userName) {
|
||||
List<String> userList = findAllOtherUsers(userName);
|
||||
List<String> onlineUsers = activeUserStore.getUsers();
|
||||
// List<String> onlineUsers = activeUserStore.getUsers();
|
||||
|
||||
List<UserSession> userSessionsList = userSessionRepository.findAll();
|
||||
Map<String, UserSession> lastActiveMap = convertToMap(userSessionsList);
|
||||
// userList.forEach(u -> {
|
||||
// ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
||||
// activeUserDTO.setUserName(u);
|
||||
// activeUserDTO.setOnline(false);
|
||||
// activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u)));
|
||||
// if (onlineUsers.contains(u)) {
|
||||
// activeUserDTO.setOnline(true);
|
||||
// }
|
||||
// activeUserDTOs.add(activeUserDTO);
|
||||
// });
|
||||
|
||||
List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
|
||||
Map<String, Instant> lastActiveMap = convertToMap(userSessionRepository.findAll());
|
||||
userList.forEach(u -> {
|
||||
ActiveUserDTO activeUserDTO = new ActiveUserDTO();
|
||||
UserSession us = lastActiveMap.get(u);
|
||||
|
||||
activeUserDTO.setUserName(u);
|
||||
activeUserDTO.setOnline(false);
|
||||
activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u)));
|
||||
if (onlineUsers.contains(u)) {
|
||||
activeUserDTO.setOnline(true);
|
||||
activeUserDTO.setLastActive(null);
|
||||
if(us != null)
|
||||
{
|
||||
activeUserDTO.setOnline(us.isOnline());
|
||||
activeUserDTO.setLastActive(toLastActiveString(lastActiveMap.get(u).getTimeStamp()));
|
||||
}
|
||||
|
||||
activeUserDTOs.add(activeUserDTO);
|
||||
});
|
||||
|
||||
return activeUserDTOs;
|
||||
}
|
||||
|
||||
@ -112,10 +131,18 @@ public class UserServiceImpl implements UserService {
|
||||
return userRepository.findByUserName(userName);
|
||||
}
|
||||
|
||||
private Map<String, Instant> convertToMap(List<UserSession> userSessionList) {
|
||||
Map<String, Instant> userMap = new HashMap<>();
|
||||
// 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 Map<String, UserSession> convertToMap(List<UserSession> userSessionList) {
|
||||
Map<String, UserSession> userMap = new HashMap<>();
|
||||
userSessionList.forEach(us -> {
|
||||
userMap.put(us.getUser().getUserName(), us.getTimeStamp());
|
||||
userMap.put(us.getUser().getUserName(), us);
|
||||
});
|
||||
return userMap;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user