From fbed3072ee2ce24618db9eacf2b3f0dd96650877 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 12 Jun 2020 23:53:40 +0200 Subject: [PATCH] GroupMessages get processed the right way now --- src/main/java/envoy/server/Startup.java | 11 +++++++- .../processors/GroupMessageProcessor.java | 25 ++++++++++++++----- .../server/processors/MessageProcessor.java | 4 +++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 63415aa..a8be82f 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -14,7 +14,15 @@ import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectMessageProcessor; import envoy.server.net.ObjectMessageReader; -import envoy.server.processors.*; +import envoy.server.processors.ContactOperationProcessor; +import envoy.server.processors.ContactSearchProcessor; +import envoy.server.processors.GroupCreationProcessor; +import envoy.server.processors.GroupMessageProcessor; +import envoy.server.processors.IDGeneratorRequestProcessor; +import envoy.server.processors.LoginCredentialProcessor; +import envoy.server.processors.MessageProcessor; +import envoy.server.processors.MessageStatusChangeProcessor; +import envoy.server.processors.UserStatusChangeProcessor; import envoy.util.EnvoyLog; /** @@ -61,6 +69,7 @@ public class Startup { Server server = new Server(8080, ObjectMessageReader::new, new ObjectMessageProcessor(Set.of(new LoginCredentialProcessor(), new MessageProcessor(), + new GroupMessageProcessor(), new GroupCreationProcessor(), new MessageStatusChangeProcessor(), new UserStatusChangeProcessor(), diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 087e2c0..a356c6e 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -8,6 +8,7 @@ import javax.persistence.EntityExistsException; import envoy.data.GroupMessage; import envoy.data.Message.MessageStatus; +import envoy.event.MessageStatusChangeEvent; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; @@ -31,16 +32,28 @@ public class GroupMessageProcessor implements ObjectProcessor { ConnectionManager connectionManager = ConnectionManager.getInstance(); final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); - for (long i = 0; i < groupMessage.getMemberStatuses().size(); i++) { - groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT); - } + members.forEach(user -> groupMessage.getMemberStatuses().replace(user.getID(), MessageStatus.SENT)); members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); }); + // Setting memberStatus of sender to READ + groupMessage.getMemberStatuses() + .replace(members.stream().filter(sender -> groupMessage.getSenderID() == sender.getID()).findAny().get().getID(), MessageStatus.READ); // Checks if all memberMessageStatuses are RECEIVED and if so sets the - // groupMessage Status to RECEIVED. - if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) groupMessage.setStatus(MessageStatus.RECEIVED); + // groupMessage Status to RECEIVED and sends a MessageStatusChangeEvent to the + // sender, if he is still online. + if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) { + groupMessage.setStatus(MessageStatus.RECEIVED); + if (connectionManager.isOnline(connectionManager.getUserIdBySocketId(socketID))) try { + writeProxy.write(socketID, new MessageStatusChangeEvent(groupMessage)); + } catch (IOException e) { + logger.warning("Sender of the groupMessage online. Failed to send MessageStatusChangeEvent"); + e.printStackTrace(); + } + } - members.forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); + members.stream() + .filter(m -> groupMessage.getSenderID() != m.getID()) + .forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); try { PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date())); diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index a326d8c..5a36b8f 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -31,6 +31,10 @@ public class MessageProcessor implements ObjectProcessor { @Override public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { + // Makes sure, that there are no groupMessages processed here, because + // groupMessage is a subclass of message. + if (message.getClass().equals(envoy.data.GroupMessage.class)) return; + message.nextStatus(); ConnectionManager connectionManager = ConnectionManager.getInstance();