From e0998b4e5f3c0cce4253b67d4b8207332c83cf2e Mon Sep 17 00:00:00 2001 From: Miguel Hincapie Date: Thu, 4 Oct 2018 17:13:55 -0500 Subject: [PATCH 1/4] ChatMarkersManager improved: * Filters modified to allow xep-0333 be used in MUC. * Validation when trying to obtain Chat instance. * Created a method to notify the recipients about new state. --- .../chat_markers/ChatMarkersManager.java | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java index e203edcc73..dea8d8d5ea 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java @@ -37,6 +37,7 @@ import org.jivesoftware.smack.filter.MessageTypeFilter; import org.jivesoftware.smack.filter.MessageWithBodiesFilter; import org.jivesoftware.smack.filter.NotFilter; +import org.jivesoftware.smack.filter.OrFilter; import org.jivesoftware.smack.filter.PossibleFromTypeFilter; import org.jivesoftware.smack.filter.StanzaExtensionFilter; import org.jivesoftware.smack.filter.StanzaFilter; @@ -73,14 +74,14 @@ public void connectionCreated(XMPPConnection connection) { // @FORMATTER:OFF private static final StanzaFilter INCOMING_MESSAGE_FILTER = new AndFilter( - MessageTypeFilter.NORMAL_OR_CHAT, + new OrFilter(MessageTypeFilter.NORMAL_OR_CHAT, MessageTypeFilter.GROUPCHAT), new StanzaExtensionFilter(ChatMarkersElements.NAMESPACE), PossibleFromTypeFilter.ENTITY_BARE_JID, EligibleForChatMarkerFilter.INSTANCE ); private static final StanzaFilter OUTGOING_MESSAGE_FILTER = new AndFilter( - MessageTypeFilter.NORMAL_OR_CHAT, + new OrFilter(MessageTypeFilter.NORMAL_OR_CHAT, MessageTypeFilter.GROUPCHAT), MessageWithBodiesFilter.INSTANCE, new NotFilter(ChatMarkersFilter.INSTANCE), EligibleForChatMarkerFilter.INSTANCE @@ -144,8 +145,13 @@ public void processStanza(Stanza packet) // Note that this listener is used together with a PossibleFromTypeFilter.ENTITY_BARE_JID filter, hence // every message is guaranteed to have a from address which is representable as bare JID. EntityBareJid bareFrom = message.getFrom().asEntityBareJidOrThrow(); + Chat tempChat = null; + try { + tempChat = chatManager.chatWith(bareFrom); + } catch (Exception e) { - final Chat chat = chatManager.chatWith(bareFrom); + } + final Chat chat = tempChat; asyncButOrdered.performAsyncButOrdered(chat, new Runnable() { @Override @@ -218,4 +224,41 @@ public synchronized boolean removeIncomingChatMarkerMessageListener(ChatMarkersL } return res; } + + /** + * Send a message stanza to the recipient defined in the To getter from the Message. + * + * @param message instance of {@link Message} with a To previous defined. + * @param chatMarkersState one of the values given in {@link ChatMarkersState}. + * @param messageId id of the message to be updated. + * @throws NotConnectedException if the connection is not connected. + * @throws InterruptedException if the connection is interrupted. + * @throws IllegalStateException if one of the params don't match the rules. + */ + public void markMessage(Message message, ChatMarkersState chatMarkersState, String messageId) + throws + NotConnectedException, + InterruptedException, + IllegalStateException { + if (message == null) { + throw new IllegalStateException("Message must not be null"); + } + + if (message.getTo() == null) { + throw new IllegalStateException("To attribute must not be null"); + } + + if (message.getStanzaId() == null) { + message.setStanzaId(); + } + + if (chatMarkersState == ChatMarkersState.received) { + message.addExtension(new ChatMarkersElements.ReceivedExtension(messageId)); + } else if (chatMarkersState == ChatMarkersState.displayed) { + message.addExtension(new ChatMarkersElements.DisplayedExtension(messageId)); + } else if (chatMarkersState == ChatMarkersState.acknowledged) { + message.addExtension(new ChatMarkersElements.AcknowledgedExtension(messageId)); + } + connection().sendStanza(message); + } } From 0e57a9e1a1a8ed6c6d306776b7fb62df1b75a6fb Mon Sep 17 00:00:00 2001 From: Miguel Hincapie Date: Fri, 5 Oct 2018 18:35:47 -0500 Subject: [PATCH 2/4] Minor changes due comments --- .../chat_markers/ChatMarkersManager.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java index dea8d8d5ea..ee080855b9 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java @@ -43,6 +43,7 @@ import org.jivesoftware.smack.filter.StanzaFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smackx.chat_markers.element.ChatMarkersElements; import org.jivesoftware.smackx.chat_markers.filter.ChatMarkersFilter; import org.jivesoftware.smackx.chat_markers.filter.EligibleForChatMarkerFilter; @@ -240,9 +241,7 @@ public void markMessage(Message message, ChatMarkersState chatMarkersState, Stri NotConnectedException, InterruptedException, IllegalStateException { - if (message == null) { - throw new IllegalStateException("Message must not be null"); - } + Objects.requireNonNull(message, "Message must not be null"); if (message.getTo() == null) { throw new IllegalStateException("To attribute must not be null"); @@ -252,12 +251,18 @@ public void markMessage(Message message, ChatMarkersState chatMarkersState, Stri message.setStanzaId(); } - if (chatMarkersState == ChatMarkersState.received) { - message.addExtension(new ChatMarkersElements.ReceivedExtension(messageId)); - } else if (chatMarkersState == ChatMarkersState.displayed) { - message.addExtension(new ChatMarkersElements.DisplayedExtension(messageId)); - } else if (chatMarkersState == ChatMarkersState.acknowledged) { - message.addExtension(new ChatMarkersElements.AcknowledgedExtension(messageId)); + switch (chatMarkersState) { + case received: + message.addExtension(new ChatMarkersElements.ReceivedExtension(messageId)); + break; + case displayed: + message.addExtension(new ChatMarkersElements.DisplayedExtension(messageId)); + break; + case acknowledged: + message.addExtension(new ChatMarkersElements.AcknowledgedExtension(messageId)); + break; + default: + throw new IllegalStateException("markable is automatically set in outgoing messages."); } connection().sendStanza(message); } From 49a97bbaa219c59a26bd9b294beb4b39c44a503b Mon Sep 17 00:00:00 2001 From: miguelhincapie Date: Sat, 30 Mar 2019 16:52:35 -0500 Subject: [PATCH 3/4] removed try-catch due PR comments --- .../smackx/chat_markers/ChatMarkersManager.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java index ee080855b9..69ce06a173 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java @@ -146,13 +146,7 @@ public void processStanza(Stanza packet) // Note that this listener is used together with a PossibleFromTypeFilter.ENTITY_BARE_JID filter, hence // every message is guaranteed to have a from address which is representable as bare JID. EntityBareJid bareFrom = message.getFrom().asEntityBareJidOrThrow(); - Chat tempChat = null; - try { - tempChat = chatManager.chatWith(bareFrom); - } catch (Exception e) { - - } - final Chat chat = tempChat; + final Chat chat = chatManager.chatWith(bareFrom); asyncButOrdered.performAsyncButOrdered(chat, new Runnable() { @Override From 1b4bc252e7230b8e29d15383cb9c4c1b71a294aa Mon Sep 17 00:00:00 2001 From: miguelhincapie Date: Sat, 30 Mar 2019 16:54:59 -0500 Subject: [PATCH 4/4] changed getStanzaId == null for hasStanzaIdSet() --- .../jivesoftware/smackx/chat_markers/ChatMarkersManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java index 69ce06a173..c26ee433a3 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/chat_markers/ChatMarkersManager.java @@ -241,7 +241,7 @@ public void markMessage(Message message, ChatMarkersState chatMarkersState, Stri throw new IllegalStateException("To attribute must not be null"); } - if (message.getStanzaId() == null) { + if (!message.hasStanzaIdSet()) { message.setStanzaId(); }