mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-23 13:07:59 +01:00
Update to track last chat state of a chat so that the user is not updated more than once, and the XEP is not violated.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6711 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
1426964334
commit
da9a0e4543
3 changed files with 37 additions and 6 deletions
|
@ -169,4 +169,12 @@ public class Chat {
|
|||
listener.processMessage(this, message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof Chat
|
||||
&& threadID.equals(((Chat)obj).getThreadID())
|
||||
&& participant.equals(((Chat)obj).getParticipant());
|
||||
}
|
||||
}
|
|
@ -177,7 +177,7 @@ public class ChatManager {
|
|||
return jidChats.get(userJID);
|
||||
}
|
||||
|
||||
private Chat getThreadChat(String thread) {
|
||||
public Chat getThreadChat(String thread) {
|
||||
return threadChats.get(thread);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
package org.jivesoftware.smackx;
|
||||
|
||||
import org.jivesoftware.smack.*;
|
||||
import org.jivesoftware.smack.util.collections.ReferenceMap;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.filter.NotFilter;
|
||||
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
||||
|
@ -46,6 +47,9 @@ public class ChatStateManager {
|
|||
private static Map<XMPPConnection, ChatStateManager> managers =
|
||||
new WeakHashMap<XMPPConnection, ChatStateManager>();
|
||||
|
||||
private static PacketFilter filter = new NotFilter(
|
||||
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
||||
|
||||
/**
|
||||
* Returns the ChatStateManager related to the XMPPConnection and it will create one if it does
|
||||
* not yet exist.
|
||||
|
@ -72,13 +76,17 @@ public class ChatStateManager {
|
|||
|
||||
private IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
||||
|
||||
/**
|
||||
* Maps chat to last chat state.
|
||||
*/
|
||||
private Map<Chat, ChatState> chatStates = new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK,
|
||||
ReferenceMap.HARD);
|
||||
|
||||
private ChatStateManager(XMPPConnection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
PacketFilter filter = new NotFilter(
|
||||
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
||||
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
||||
filter);
|
||||
connection.getChatManager().addChatListener(incomingInterceptor);
|
||||
|
@ -98,6 +106,9 @@ public class ChatStateManager {
|
|||
* packet.
|
||||
*/
|
||||
public void setCurrentState(ChatState newState, Chat chat) throws XMPPException {
|
||||
if(!updateChatState(chat, newState)) {
|
||||
return;
|
||||
}
|
||||
Message message = new Message();
|
||||
ChatStateExtension extension = new ChatStateExtension(newState);
|
||||
message.addExtension(extension);
|
||||
|
@ -105,6 +116,15 @@ public class ChatStateManager {
|
|||
chat.sendMessage(message);
|
||||
}
|
||||
|
||||
private boolean updateChatState(Chat chat, ChatState newState) {
|
||||
ChatState lastChatState = chatStates.get(chat);
|
||||
if (lastChatState == null || lastChatState != newState) {
|
||||
chatStates.put(chat, lastChatState);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void fireNewChatState(Chat chat, ChatState state) {
|
||||
for (MessageListener listener : chat.getListeners()) {
|
||||
if (listener instanceof ChatStateListener) {
|
||||
|
@ -116,11 +136,14 @@ public class ChatStateManager {
|
|||
private class OutgoingMessageInterceptor implements PacketInterceptor {
|
||||
|
||||
public void interceptPacket(Packet packet) {
|
||||
if (!(packet instanceof Message)) {
|
||||
Message message = (Message) packet;
|
||||
Chat chat = connection.getChatManager().getThreadChat(message.getThread());
|
||||
if (chat == null) {
|
||||
return;
|
||||
}
|
||||
Message message = (Message) packet;
|
||||
message.addExtension(new ChatStateExtension(ChatState.active));
|
||||
if (updateChatState(chat, ChatState.active)) {
|
||||
message.addExtension(new ChatStateExtension(ChatState.active));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue