2020-08-22 18:14:26 +02:00
|
|
|
package envoy.server.processors;
|
|
|
|
|
|
|
|
import static envoy.data.Message.MessageStatus.*;
|
|
|
|
import static envoy.server.Startup.config;
|
|
|
|
|
|
|
|
import java.time.Instant;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import javax.persistence.EntityExistsException;
|
|
|
|
|
|
|
|
import envoy.data.GroupMessage;
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.event.*;
|
2020-10-18 15:16:46 +02:00
|
|
|
import envoy.util.EnvoyLog;
|
|
|
|
|
2020-08-22 18:14:26 +02:00
|
|
|
import envoy.server.data.PersistenceManager;
|
2020-09-25 14:29:23 +02:00
|
|
|
import envoy.server.net.*;
|
2020-08-22 18:14:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Maximilian Käfer
|
|
|
|
* @since Envoy Server Standalone v0.1-beta
|
|
|
|
*/
|
|
|
|
public final class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
|
|
|
|
|
|
|
|
private static final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
|
|
|
private static final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
2020-10-18 15:16:46 +02:00
|
|
|
private static final Logger logger =
|
|
|
|
EnvoyLog.getLogger(GroupCreationProcessor.class);
|
2020-08-22 18:14:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void process(GroupMessage groupMessage, long socketID, ObjectWriteProxy writeProxy) {
|
|
|
|
groupMessage.nextStatus();
|
|
|
|
|
|
|
|
// Update statuses to SENT / RECEIVED depending on online status
|
2020-10-18 15:16:46 +02:00
|
|
|
groupMessage.getMemberStatuses()
|
|
|
|
.replaceAll((memberID, status) -> connectionManager.isOnline(memberID)
|
|
|
|
? RECEIVED
|
|
|
|
: SENT);
|
2020-08-22 18:14:26 +02:00
|
|
|
|
|
|
|
// Set status for sender to READ
|
|
|
|
groupMessage.getMemberStatuses().replace(groupMessage.getSenderID(), READ);
|
|
|
|
|
|
|
|
// Increment the overall status to RECEIVED if necessary
|
|
|
|
if (Collections.min(groupMessage.getMemberStatuses().values()) == RECEIVED) {
|
|
|
|
groupMessage.nextStatus();
|
|
|
|
|
|
|
|
// Notify the sender of the status change
|
|
|
|
writeProxy.write(socketID, new MessageStatusChange(groupMessage));
|
|
|
|
}
|
|
|
|
|
|
|
|
// message attachment will be automatically removed if disabled in config
|
2020-10-18 15:16:46 +02:00
|
|
|
final var groupMessageServer =
|
|
|
|
new envoy.server.data.GroupMessage(groupMessage, Instant.now());
|
2020-08-22 18:14:26 +02:00
|
|
|
// Telling the server to reload the message without the attachment and telling
|
|
|
|
// the client not to send anymore attachments
|
|
|
|
if (!config.isAttachmentSupportEnabled() && groupMessage.hasAttachment()) {
|
|
|
|
groupMessage = groupMessageServer.toCommon();
|
|
|
|
writeProxy.write(socketID, new NoAttachments());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is needed unfortunately because of f***ing lambda restrictions ("must be
|
|
|
|
// fINaL oR EFfEcTivELy FiNAl")
|
|
|
|
final var groupMessageCopy = groupMessage;
|
|
|
|
// Deliver the message to the recipients that are online
|
|
|
|
writeProxy.writeToOnlineContacts(
|
2020-10-18 15:16:46 +02:00
|
|
|
persistenceManager.getGroupByID(groupMessageCopy.getRecipientID())
|
|
|
|
.getContacts()
|
|
|
|
.stream()
|
|
|
|
.filter(c -> c.getID() != groupMessageCopy.getSenderID()),
|
|
|
|
groupMessageCopy);
|
2020-08-22 18:14:26 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
PersistenceManager.getInstance().addMessage(groupMessageServer);
|
|
|
|
} catch (final EntityExistsException e) {
|
|
|
|
logger.warning("Received a groupMessage with an ID that already exists");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|