58 lines
2.1 KiB
Java
Executable File
58 lines
2.1 KiB
Java
Executable File
package envoy.server.processors;
|
|
|
|
import java.time.Instant;
|
|
import java.util.logging.*;
|
|
|
|
import envoy.event.ElementOperation;
|
|
import envoy.event.contact.UserOperation;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
import envoy.server.data.PersistenceManager;
|
|
import envoy.server.net.*;
|
|
|
|
/**
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public final class UserOperationProcessor implements ObjectProcessor<UserOperation> {
|
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
private static final Logger logger =
|
|
EnvoyLog.getLogger(UserOperationProcessor.class);
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
|
|
@Override
|
|
public void process(UserOperation evt, long socketId, ObjectWriteProxy writeProxy) {
|
|
final long userID = ConnectionManager.getInstance().getUserIDBySocketID(socketId);
|
|
final long contactID = evt.get().getID();
|
|
final var sender = persistenceManager.getUserByID(userID);
|
|
switch (evt.getOperationType()) {
|
|
case ADD:
|
|
logger.log(Level.FINE,
|
|
String.format("Adding %s to the contact list of user %d.", evt.get(), userID));
|
|
persistenceManager.addContactBidirectional(userID, contactID);
|
|
|
|
// Notify the contact if online
|
|
if (connectionManager.isOnline(contactID))
|
|
writeProxy.write(connectionManager.getSocketID(contactID),
|
|
new UserOperation(sender.toCommon(), ElementOperation.ADD));
|
|
break;
|
|
case REMOVE:
|
|
|
|
// Remove the relationships and notify sender if he logs in using another
|
|
// LocalDB
|
|
persistenceManager.removeContactBidirectional(userID, contactID);
|
|
sender.setLatestContactDeletion(Instant.now());
|
|
|
|
// Notify the removed contact on next startup(s) of this deletion
|
|
persistenceManager.getUserByID(contactID).setLatestContactDeletion(Instant.now());
|
|
|
|
// Notify the removed contact if online
|
|
if (connectionManager.isOnline(contactID))
|
|
writeProxy.write(connectionManager.getSocketID(contactID),
|
|
new UserOperation(sender.toCommon(), ElementOperation.REMOVE));
|
|
break;
|
|
}
|
|
}
|
|
}
|