Reuses xmpp chat thread ids when sending messages.

fix-message-formatting 5166
Damian Minkov 12 years ago
parent 8a0c43ff78
commit 6aaa0bade2

@ -70,13 +70,16 @@ public class OperationSetBasicInstantMessagingJabberImpl
* Contains the complete jid of a specific user and the time that it was
* last used so that we could remove it after a certain point.
*/
private class TargetAddress
public static class TargetAddress
{
/** The last complete JID (including resource) that we got a msg from*/
String jid;
/** The time that we last sent or received a message from this jid */
long lastUpdatedTime;
/** The last chat used, this way we will reuse the thread-id */
Chat chat;
}
/**
@ -263,7 +266,11 @@ public boolean isContentTypeSupported(String contentType, Contact contact)
return true;
else if(contentType.equals(HTML_MIME_TYPE))
{
String toJID = getJidForAddress(contact.getAddress());
String toJID = null;
TargetAddress ta = getJidForAddress(contact.getAddress());
if(ta != null)
toJID = ta.jid;
if (toJID == null)
toJID = contact.getAddress();
@ -280,20 +287,27 @@ else if(contentType.equals(HTML_MIME_TYPE))
* Returns a reference to an open chat with the specified
* <tt>jid</tt> if one exists or creates a new one otherwise.
*
* @param toAddress the address without the resource, we used to put
* a reference in jids table.
* @param jid the Jabber ID that we'd like to obtain a chat instance for.
*
* @return a reference to an open chat with the specified
* <tt>jid</tt> if one exists or creates a new one otherwise.
*/
public Chat obtainChatInstance(String jid)
public Chat obtainChatInstance(String toAddress,
String jid)
{
XMPPConnection jabberConnection
= jabberProvider.getConnection();
Chat chat = jabberConnection.getChatManager().getThreadChat(jid);
TargetAddress ta = getJidForAddress(toAddress);
if (chat != null)
return chat;
if (ta != null
&& ta.chat != null
&& ta.jid.equals(jid))
{
return ta.chat;
}
org.jivesoftware.smack.MessageListener msgListener
= new org.jivesoftware.smack.MessageListener()
@ -312,7 +326,7 @@ public void processMessage(
//we don't have a thread for this chat, so let's create one.
chat = jabberConnection.getChatManager()
Chat chat = jabberConnection.getChatManager()
.createChat(jid, msgListener);
return chat;
@ -323,7 +337,7 @@ public void processMessage(
* activity (i.e. neither outgoing nor incoming messags) for more than
* JID_INACTIVITY_TIMEOUT. Note that this method is not synchronous and that
* it is only meant for use by the {@link #getJidForAddress(String)} and
* {@link #putJidForAddress(String, String)}
* {@link #putJidForAddress(String, String, Chat)}
*/
private void purgeOldJids()
{
@ -357,7 +371,7 @@ private void purgeOldJids()
* contacted us from or <tt>null</tt> if we don't have a jid for the
* specified <tt>address</tt> yet.
*/
String getJidForAddress(String address)
TargetAddress getJidForAddress(String address)
{
synchronized(jids)
{
@ -369,7 +383,7 @@ String getJidForAddress(String address)
ta.lastUpdatedTime = System.currentTimeMillis();
return ta.jid;
return ta;
}
}
@ -384,7 +398,7 @@ String getJidForAddress(String address)
* @param jid the jid (i.e. address/resource) that the contact with the
* specified <tt>address</tt> last contacted us from.
*/
private void putJidForAddress(String address, String jid)
private void putJidForAddress(String address, String jid, Chat chat)
{
synchronized(jids)
{
@ -400,6 +414,7 @@ private void putJidForAddress(String address, String jid)
ta.jid = jid;
ta.lastUpdatedTime = System.currentTimeMillis();
ta.chat = chat;
}
}
@ -449,7 +464,11 @@ private MessageDeliveredEvent sendMessage( Contact to,
}
if (toJID == null)
toJID = getJidForAddress(to.getAddress());
{
TargetAddress ta = getJidForAddress(to.getAddress());
if(ta != null)
toJID = ta.jid;
}
if (toJID == null)
{
@ -457,7 +476,7 @@ private MessageDeliveredEvent sendMessage( Contact to,
toJID = to.getAddress();
}
Chat chat = obtainChatInstance(toJID);
Chat chat = obtainChatInstance(to.getAddress(), toJID);
msg.setPacketID(message.getMessageUID());
msg.setTo(toJID);
@ -517,8 +536,7 @@ private MessageDeliveredEvent sendMessage( Contact to,
chat.sendMessage(msg);
if(sendToBaseResource)
putJidForAddress(to.getAddress(), to.getAddress());
putJidForAddress(to.getAddress(), toJID, chat);
MessageDeliveredEvent msgDeliveredEvt
= new MessageDeliveredEvent(message, to, toResource);
@ -1004,7 +1022,10 @@ public void processPacket(Packet packet)
userBareID} );
}
putJidForAddress(address, userFullId);
Chat chat =
jabberProvider.getConnection().getChatManager()
.getThreadChat(msg.getThread());
putJidForAddress(address, userFullId, chat);
if (logger.isTraceEnabled())
logger.trace("just mapped: " + userBareID

@ -144,13 +144,19 @@ else if(typingState == STATE_STOPPED)
*/
private void sendXep85ChatState(Contact contact, int state)
{
if(opSetBasicIM == null
|| parentProvider.getConnection() == null)
return;
String toJID = null;
// find the currently contacted jid to send typing info to him
// or if we do not have a jid and we have already sent message to the
// bare jid we will also send typing info there
if (toJID == null)
toJID = opSetBasicIM.getJidForAddress(contact.getAddress());
OperationSetBasicInstantMessagingJabberImpl.TargetAddress ta
= opSetBasicIM.getJidForAddress(contact.getAddress());
if (ta != null)
toJID = ta.jid;
// if we haven't sent a message yet, do not send typing notifications
if(toJID == null)
@ -160,8 +166,13 @@ private void sendXep85ChatState(Contact contact, int state)
logger.trace("Sending XEP-0085 chat state=" + state
+ " to " + toJID);
Chat chat = parentProvider.getConnection()
.getChatManager().createChat(toJID, null);
Chat chat;
if(ta != null && ta.chat != null)
chat = ta.chat;
else
chat = parentProvider.getConnection()
.getChatManager().createChat(toJID, null);
ChatState chatState = null;

Loading…
Cancel
Save