This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/server/src/main/java/envoy/server/processors/UserOperationProcessor.java

64 lines
2.2 KiB
Java
Raw Normal View History

package envoy.server.processors;
2020-10-10 22:25:39 +02:00
import java.time.Instant;
import java.util.logging.*;
import envoy.event.ElementOperation;
2020-10-24 12:19:11 +02:00
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> {
2020-10-10 22:25:39 +02:00
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
private static final Logger logger =
EnvoyLog.getLogger(UserOperationProcessor.class);
2020-10-10 22:25:39 +02:00
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
@Override
2020-10-19 22:16:18 +02:00
public void process(UserOperation evt, long socketID, ObjectWriteProxy writeProxy) {
final long userID = ConnectionManager.getInstance().getUserIDBySocketID(socketID);
2020-10-10 22:25:39 +02:00
final long contactID = evt.get().getID();
2020-10-19 22:16:18 +02:00
final var recipient = persistenceManager.getUserByID(contactID);
2020-10-24 12:19:11 +02:00
// TODO: Inform the sender if the requested contact has already been deleted
if (recipient == null)
2020-10-19 22:16:18 +02:00
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));
2020-10-10 22:25:39 +02:00
persistenceManager.addContactBidirectional(userID, contactID);
2020-02-11 18:15:19 +01:00
// Notify the contact if online
2020-10-10 22:25:39 +02:00
if (connectionManager.isOnline(contactID))
writeProxy.write(connectionManager.getSocketID(contactID),
new UserOperation(sender.toCommon(), ElementOperation.ADD));
2020-10-10 22:25:39 +02:00
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
2020-10-19 22:16:18 +02:00
recipient.setLatestContactDeletion(Instant.now());
2020-10-10 22:25:39 +02:00
// Notify the removed contact if online
if (connectionManager.isOnline(contactID))
writeProxy.write(connectionManager.getSocketID(contactID),
new UserOperation(sender.toCommon(), ElementOperation.REMOVE));
break;
}
}
}