Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
fixes for #466 related to jid, resource handling and duplicate detection
Browse files Browse the repository at this point in the history
  • Loading branch information
iamironrabbit committed May 29, 2018
1 parent 7cea795 commit 3a73d6a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static String processResource(String userId) {

public synchronized SessionID getSessionId(String localUserId, String remoteUserId) {

SessionID sessionId = new SessionID( XmppAddress.stripResource(localUserId), XmppAddress.stripResource(remoteUserId), SESSION_TYPE_XMPP);
SessionID sessionId = new SessionID( XmppAddress.stripResource(localUserId), remoteUserId, SESSION_TYPE_XMPP);

Session session = mSessions.get(sessionId.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ private void initJid ()
{
try {
mJid = JidCreate.from(mParticipant.getAddress().getAddress());

if (mJid.hasNoResource() && mParticipant instanceof Contact)
{
Contact contact = (Contact)mParticipant;
if (contact.getPresence() != null && contact.getPresence().getResource() != null)
mJid = JidCreate.from(mParticipant.getAddress().getBareAddress() + '/' + contact.getPresence().getResource());
}

mXa = new XmppAddress(mJid.toString());

if (mJid.hasNoResource()) {
Expand Down Expand Up @@ -383,5 +391,8 @@ public void setOmemoGroupEnabled (boolean omemoGroups)
mEnableOmemoGroups = omemoGroups;
}


public void updateParticipant (ImEntity participant)
{
mParticipant = participant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,15 @@ else if (rec.getFrom().getResource().equals(rec.getTo().getUser())) {
}

}
else
{
Contact contact = (Contact)session.getParticipant();
if (contact.getPresence() == null || contact.getPresence().getResource() == null)
{
session.updateParticipant(handlePresenceChanged(mRoster.getPresence(smackMessage.getFrom().asBareJid())));

}
}

boolean good = session.onReceiveMessage(rec, notifyUser);

Expand Down Expand Up @@ -2925,7 +2934,7 @@ Collection<ChatSession> getSessions () {
@Override
public ChatSession createChatSession(ImEntity participant, boolean isNewSession) {
ChatSession session = super.createChatSession(participant,isNewSession);
mSessions.put(participant.getAddress().getAddress(),session);
mSessions.put(participant.getAddress().getBareAddress(),session);

if (participant instanceof Contact)
getLastSeen ((Contact)participant);
Expand Down Expand Up @@ -4351,30 +4360,36 @@ private void handleChatState (String from, String chatStateXml) throws RemoteExc
if (contact == null)
return;

p = contact.getPresence();

boolean isTyping = false;

//handle is-typing, probably some indication on screen
if (chatStateXml.contains("'error'")||chatStateXml.contains("'cancel'")) {
//do nothing
}
else if (chatStateXml.contains(ChatState.active.toString())) {
p = new Presence(Presence.AVAILABLE, "", null, null,
Presence.CLIENT_TYPE_MOBILE);
if (p == null)
p = new Presence(Presence.AVAILABLE, "", null, null,
Presence.CLIENT_TYPE_MOBILE);

p.setLastSeen(new Date());
}
else if (chatStateXml.contains(ChatState.inactive.toString())) {
p = new Presence(Presence.AWAY, "", null, null,
if (p == null)
p = new Presence(Presence.AWAY, "", null, null,
Presence.CLIENT_TYPE_MOBILE);

p.setLastSeen(new Date());
}
else if (chatStateXml.contains(ChatState.composing.toString())) {
p = new Presence(Presence.AVAILABLE, "", null, null,
if (p == null)
p = new Presence(Presence.AVAILABLE, "", null, null,
Presence.CLIENT_TYPE_MOBILE);

isTyping = true;
p.setLastSeen(new Date());


}
else if (chatStateXml.contains(ChatState.inactive.toString())||chatStateXml.contains(ChatState.paused.toString())) {

Expand Down Expand Up @@ -4445,8 +4460,11 @@ private void setPresence (Jid from, int presenceType) {
if (contact == null)
return;

p = new Presence(presenceType, "", null, null,
Presence.CLIENT_TYPE_MOBILE);
p = contact.getPresence();

if (p == null)
p = new Presence(presenceType, "", null, null,
Presence.CLIENT_TYPE_MOBILE);

if (from.hasResource())
p.setResource(from.getResourceOrEmpty().toString());
Expand All @@ -4472,7 +4490,10 @@ private void getLastSeen (Contact contact)
LastActivity activity = mLastActivityManager.getLastActivity(JidCreate.bareFrom(contact.getAddress().getBareAddress()));

if (activity != null) {
Presence presence = new Presence();
Presence presence = contact.getPresence();
if (presence == null)
presence = new Presence();

Date now = new Date();
presence.setLastSeen(new Date(now.getTime() - (activity.getIdleTime() * 1000)));
contact.setPresence(presence);
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/org/awesomeapp/messenger/provider/Imps.java
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,26 @@ public static boolean messageExists (ContentResolver resolver, String id)
return result;
}

public static boolean messageExists (ContentResolver resolver, String id, int messageType)
{
boolean result = false;

Uri.Builder builder = Messages.OTR_MESSAGES_CONTENT_URI_BY_PACKET_ID.buildUpon();
builder.appendPath(id);

String[] args = {messageType+""};

Cursor c = resolver.query(builder.build(),null, Imps.Messages.TYPE + "=?", args, null);
if (c != null)
{
if (c.getCount() > 0)
result = true;

c.close();
}

return result;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ public boolean onIncomingMessage(ChatSession ses, final org.awesomeapp.messenger
&& msg.getType() == Imps.MessageType.INCOMING)
return false;


if (Imps.messageExists(mContentResolver,msg.getID()))
if (Imps.messageExists(mContentResolver,msg.getID(),msg.getType()))
return false;

String body = msg.getBody();
Expand All @@ -1140,6 +1139,7 @@ public boolean onIncomingMessage(ChatSession ses, final org.awesomeapp.messenger
contact = mConnection.getContactListManager().getContactByAddress(bareUsername);
nickname = contact.getName();
} catch (Exception e) {
return false;
}

long time = msg.getDateTime().getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ private IChatSession createChatSession() {
}
else
{
remoteAddress = Address.stripResource(mRemoteAddress);
//remoteAddress = Address.stripResource(mRemoteAddress);

session = sessionMgr.createChatSession(remoteAddress,false);
}
Expand Down

0 comments on commit 3a73d6a

Please sign in to comment.