Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatMarkersManager improved - part 2 #272

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
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;
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;
Expand Down Expand Up @@ -73,14 +75,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
Expand Down Expand Up @@ -144,7 +146,6 @@ 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();

final Chat chat = chatManager.chatWith(bareFrom);

asyncButOrdered.performAsyncButOrdered(chat, new Runnable() {
Expand Down Expand Up @@ -218,4 +219,45 @@ public synchronized boolean removeIncomingChatMarkerMessageListener(ChatMarkersL
}
return res;
}

/**
* Send a message stanza to the recipient defined in the <tt>To</tt> getter from the <tt>Message</tt>.
*
* @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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that the user gives as a message with a markable extension to such a method. Then you could remove the ChatMarkersState and String (messageId) parameters, which would make the method much more user friendly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the user would need to deal with message.addExtension(new ChatMarkersElements.ReceivedExtension(messageId)); by himself, in other words he would doing some of the job that this class has to do.

What I want is class that can be used almost out of the box but of course knowing how the XEP works.

throws
NotConnectedException,
InterruptedException,
IllegalStateException {
Objects.requireNonNull(message, "Message must not be null");

if (message.getTo() == null) {
throw new IllegalStateException("To attribute must not be null");
}

if (!message.hasStanzaIdSet()) {
message.setStanzaId();
}

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);
}
}