From a34e90adb817d72e67a3de8f140fc057d0e75cdf Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 27 Jun 2020 21:58:53 +0200 Subject: [PATCH] Implemented functionality regarding pending groupMessages (unfinished) (This was implemented some weeks ago but never pushed (made some revision)) --- .../java/envoy/server/data/GroupMessage.java | 13 ++++++++- .../envoy/server/data/PersistenceManager.java | 16 +++++++++++ .../processors/GroupMessageProcessor.java | 13 +++++---- .../processors/LoginCredentialProcessor.java | 27 ++++++++++++++++++- .../server/processors/MessageProcessor.java | 5 ++-- 5 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/main/java/envoy/server/data/GroupMessage.java b/src/main/java/envoy/server/data/GroupMessage.java index 13dcc49..13c1d08 100644 --- a/src/main/java/envoy/server/data/GroupMessage.java +++ b/src/main/java/envoy/server/data/GroupMessage.java @@ -3,7 +3,12 @@ package envoy.server.data; import java.util.Date; import java.util.Map; -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.NamedQuery; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import envoy.data.MessageBuilder; @@ -16,8 +21,14 @@ import envoy.data.MessageBuilder; * @since Envoy Server Standalone v0.1-beta */ @Entity +@NamedQuery( + name = GroupMessage.getPendingGroupMsg, + query = "SELECT m FROM GroupMessage m JOIN m.memberMessageStatus s WHERE (KEY(s) = :userId) AND (m.creationDate > :lastSeen)" +) public class GroupMessage extends Message { + public static final String getPendingGroupMsg = "GroupMessage.getPendingGroupMsg"; + @ElementCollection private Map memberMessageStatus; diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 1e99978..3432846 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -194,6 +194,22 @@ public class PersistenceManager { .getResultList(); } + /** + * Returns all groupMessages received while being offline or the ones that have + * changed. + * + * @param user the user who wants to receive his unread groupMessages + * @return all groupMessages that the client does not yet have (unread + * groupMessages) + * @since Envoy Server Standalone v0.1-alpha + */ + public List getPendingGroupMessages(User user) { + return entityManager.createNamedQuery(GroupMessage.getPendingGroupMsg) + .setParameter("userId", user.getID()) + .setParameter("lastSeen", user.getLastSeen()) + .getResultList(); + } + /** * Searches for users matching a search phrase. Contacts of the attached user * and the attached user is ignored. diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 725004e..f62054e 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -8,7 +8,6 @@ import javax.persistence.EntityExistsException; import envoy.data.GroupMessage; import envoy.data.Message.MessageStatus; -import envoy.event.MessageStatusChange; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; @@ -43,12 +42,12 @@ public class GroupMessageProcessor implements ObjectProcessor { // 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 MessageStatusChange(groupMessage)); - } catch (IOException e) { - logger.warning("Sender of the groupMessage online. Failed to send MessageStatusChange"); - e.printStackTrace(); - } +// if (connectionManager.isOnline(connectionManager.getUserIdBySocketID(socketID))) try { +// writeProxy.write(socketID, new MessageStatusChange(groupMessage)); +// } catch (IOException e) { +// logger.warning("Sender of the groupMessage online. Failed to send MessageStatusChange"); +// e.printStackTrace(); +// } } members.stream() diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 0a248c9..d0010a4 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,12 +1,16 @@ package envoy.server.processors; import static envoy.data.User.UserStatus.ONLINE; -import static envoy.event.HandshakeRejection.*; +import static envoy.event.HandshakeRejection.INTERNAL_ERROR; +import static envoy.event.HandshakeRejection.USERNAME_TAKEN; +import static envoy.event.HandshakeRejection.WRONG_PASSWORD_OR_USER; +import static envoy.event.HandshakeRejection.WRONG_VERSION; import java.io.IOException; import java.time.LocalDateTime; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.logging.Logger; import javax.persistence.NoResultException; @@ -16,6 +20,7 @@ import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; import envoy.event.HandshakeRejection; import envoy.event.MessageStatusChange; +import envoy.server.data.GroupMessage; import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; @@ -117,6 +122,9 @@ public final class LoginCredentialProcessor implements ObjectProcessor pendingGroupMessages = PersistenceManager.getInstance().getPendingGroupMessages(user); + logger.fine("Sending " + pendingGroupMessages.size() + " pending group messages to " + user + "..."); + for (var msg : pendingMessages) { final var msgCommon = msg.toCommon(); if (msg.getStatus() == MessageStatus.SENT) { @@ -133,6 +141,23 @@ public final class LoginCredentialProcessor implements ObjectProcessor { 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; - + if (message.getClass().equals(envoy.data.GroupMessage.class)) { + return; + } message.nextStatus(); // Convert to server message