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 1 commit
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,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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
miguelhincapie marked this conversation as resolved.
Show resolved Hide resolved

final Chat chat = chatManager.chatWith(bareFrom);
}
final Chat chat = tempChat;

asyncButOrdered.performAsyncButOrdered(chat, new Runnable() {
@Override
Expand Down Expand Up @@ -218,4 +224,41 @@ 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 {
if (message == null) {
miguelhincapie marked this conversation as resolved.
Show resolved Hide resolved
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) {
miguelhincapie marked this conversation as resolved.
Show resolved Hide resolved
message.setStanzaId();
}

if (chatMarkersState == ChatMarkersState.received) {
miguelhincapie marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}