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 { 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 recipient = persistenceManager.getUserByID(contactID); // TODO: Inform the sender if the requested contact has already been deleted if (recipient == null) return; 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 recipient.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; } } }