GroupMessages get processed the right way now

This commit is contained in:
DieGurke 2020-06-12 23:53:40 +02:00
parent a9de00371d
commit fbed3072ee
3 changed files with 33 additions and 7 deletions

View File

@ -14,7 +14,15 @@ import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectMessageProcessor; import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader; 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; import envoy.util.EnvoyLog;
/** /**
@ -61,6 +69,7 @@ public class Startup {
Server server = new Server(8080, ObjectMessageReader::new, Server server = new Server(8080, ObjectMessageReader::new,
new ObjectMessageProcessor(Set.of(new LoginCredentialProcessor(), new ObjectMessageProcessor(Set.of(new LoginCredentialProcessor(),
new MessageProcessor(), new MessageProcessor(),
new GroupMessageProcessor(),
new GroupCreationProcessor(), new GroupCreationProcessor(),
new MessageStatusChangeProcessor(), new MessageStatusChangeProcessor(),
new UserStatusChangeProcessor(), new UserStatusChangeProcessor(),

View File

@ -8,6 +8,7 @@ import javax.persistence.EntityExistsException;
import envoy.data.GroupMessage; import envoy.data.GroupMessage;
import envoy.data.Message.MessageStatus; import envoy.data.Message.MessageStatus;
import envoy.event.MessageStatusChangeEvent;
import envoy.server.data.PersistenceManager; import envoy.server.data.PersistenceManager;
import envoy.server.net.ConnectionManager; import envoy.server.net.ConnectionManager;
import envoy.server.net.ObjectWriteProxy; import envoy.server.net.ObjectWriteProxy;
@ -31,16 +32,28 @@ public class GroupMessageProcessor implements ObjectProcessor<GroupMessage> {
ConnectionManager connectionManager = ConnectionManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance();
final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts();
for (long i = 0; i < groupMessage.getMemberStatuses().size(); i++) { members.forEach(user -> groupMessage.getMemberStatuses().replace(user.getID(), MessageStatus.SENT));
groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT);
}
members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); }); 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 // Checks if all memberMessageStatuses are RECEIVED and if so sets the
// groupMessage Status to RECEIVED. // groupMessage Status to RECEIVED and sends a MessageStatusChangeEvent to the
if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) groupMessage.setStatus(MessageStatus.RECEIVED); // 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 { try {
PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date())); PersistenceManager.getInstance().addMessage(new envoy.server.data.GroupMessage(groupMessage, new Date()));

View File

@ -31,6 +31,10 @@ public class MessageProcessor implements ObjectProcessor<Message> {
@Override @Override
public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { 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(); message.nextStatus();
ConnectionManager connectionManager = ConnectionManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance();