73 lines
2.8 KiB
Java
Executable File
73 lines
2.8 KiB
Java
Executable File
package envoy.server.processors;
|
|
|
|
import java.time.Instant;
|
|
import java.util.logging.*;
|
|
|
|
import envoy.event.ElementOperation;
|
|
import envoy.event.contact.ContactOperation;
|
|
import envoy.server.data.*;
|
|
import envoy.server.net.*;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public final class ContactOperationProcessor implements ObjectProcessor<ContactOperation> {
|
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class);
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
|
|
|
@Override
|
|
public void process(ContactOperation 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 ContactOperation(sender.toCommon(), ElementOperation.ADD));
|
|
break;
|
|
case REMOVE:
|
|
|
|
// Remove the relationships and notify sender if he logs in using another
|
|
// LocalDB
|
|
persistenceManager.removeContactBidirectional(userID, contactID);
|
|
final var contact = persistenceManager.getContactByID(contactID);
|
|
sender.setLatestContactDeletion(Instant.now());
|
|
|
|
// The sender wants to remove a normal contact
|
|
if (contact instanceof User) {
|
|
|
|
// 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 ContactOperation(sender.toCommon(), ElementOperation.REMOVE));
|
|
} else {
|
|
|
|
// The sender wants to be removed from a Group
|
|
final var group = persistenceManager.getGroupByID(contactID);
|
|
|
|
// The group has no more members and hence will be deleted
|
|
if (group.getContacts().isEmpty()) persistenceManager.deleteContact(group);
|
|
else {
|
|
|
|
// Informing the other members
|
|
writeProxy.writeToOnlineContacts(group.getContacts(), new ContactOperation(sender.toCommon(), ElementOperation.REMOVE));
|
|
group.getContacts().forEach(c -> ((User) c).setLatestContactDeletion(Instant.now()));
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
logger.warning(String.format("Received %s with an unsupported operation.", evt));
|
|
}
|
|
}
|
|
}
|