From 3dcb36a417ebc4b0d3e5deedf35a333376ff61a9 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 31 Aug 2020 09:03:54 +0200 Subject: [PATCH] persist presence name (pep, nick in subscribe) to DB. fixes #3856 --- .../java/eu/siacs/conversations/entities/Contact.java | 10 +++++++++- .../eu/siacs/conversations/parser/MessageParser.java | 4 +++- .../eu/siacs/conversations/parser/PresenceParser.java | 1 + .../conversations/persistance/DatabaseBackend.java | 10 +++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index 7789a6686..07ffbc89c 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -33,6 +33,7 @@ public class Contact implements ListItem, Blockable { public static final String SYSTEMNAME = "systemname"; public static final String SERVERNAME = "servername"; + public static final String PRESENCE_NAME = "presence_name"; public static final String JID = "jid"; public static final String OPTIONS = "options"; public static final String SYSTEMACCOUNT = "systemaccount"; @@ -62,13 +63,14 @@ public class Contact implements ListItem, Blockable { private long mLastseen = 0; private String mLastPresence = null; - public Contact(final String account, final String systemName, final String serverName, + public Contact(final String account, final String systemName, final String serverName, final String presenceName, final Jid jid, final int subscription, final String photoUri, final Uri systemAccount, final String keys, final String avatar, final long lastseen, final String presence, final String groups) { this.accountUuid = account; this.systemName = systemName; this.serverName = serverName; + this.presenceName = presenceName; this.jid = jid; this.subscription = subscription; this.photoUri = photoUri; @@ -116,6 +118,7 @@ public class Contact implements ListItem, Blockable { return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)), cursor.getString(cursor.getColumnIndex(SYSTEMNAME)), cursor.getString(cursor.getColumnIndex(SERVERNAME)), + cursor.getString(cursor.getColumnIndex(PRESENCE_NAME)), jid, cursor.getInt(cursor.getColumnIndex(OPTIONS)), cursor.getString(cursor.getColumnIndex(PHOTOURI)), @@ -213,6 +216,7 @@ public class Contact implements ListItem, Blockable { values.put(ACCOUNT, accountUuid); values.put(SYSTEMNAME, systemName); values.put(SERVERNAME, serverName); + values.put(PRESENCE_NAME, presenceName); values.put(JID, jid.toString()); values.put(OPTIONS, subscription); values.put(SYSTEMACCOUNT, systemAccount != null ? systemAccount.toString() : null); @@ -554,6 +558,10 @@ public class Contact implements ListItem, Blockable { return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName()); } + public boolean hasAvatarOrPresenceName() { + return (avatar != null && avatar.getFilename() != null) || presenceName != null; + } + public final class Options { public static final int TO = 0; public static final int FROM = 1; diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 7622d3071..7a82b092d 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -300,6 +300,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } else { Contact contact = account.getRoster().getContact(user); if (contact.setPresenceName(nick)) { + mXmppConnectionService.syncRoster(account); mXmppConnectionService.getAvatarService().clear(contact); } } @@ -1011,8 +1012,9 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final String nick = packet.findChildContent("nick", Namespace.NICK); if (nick != null && InvalidJid.hasValidFrom(original)) { - Contact contact = account.getRoster().getContact(from); + final Contact contact = account.getRoster().getContact(from); if (contact.setPresenceName(nick)) { + mXmppConnectionService.syncRoster(account); mXmppConnectionService.getAvatarService().clear(contact); } } diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 4e862ddf6..7f57ef733 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -337,6 +337,7 @@ public class PresenceParser extends AbstractParser implements mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false); } else if (type.equals("subscribe")) { if (contact.setPresenceName(packet.findChildContent("nick", Namespace.NICK))) { + mXmppConnectionService.syncRoster(account); mXmppConnectionService.getAvatarService().clear(contact); } if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) { diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 4d8e8d22d..789b07c42 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -63,11 +63,12 @@ import eu.siacs.conversations.xmpp.Jid; public class DatabaseBackend extends SQLiteOpenHelper { private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 46; + private static final int DATABASE_VERSION = 47; private static DatabaseBackend instance = null; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT," + + Contact.PRESENCE_NAME + " TEXT," + Contact.JID + " TEXT," + Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, " @@ -555,6 +556,9 @@ public class DatabaseBackend extends SQLiteOpenHelper { final long diff = SystemClock.elapsedRealtime() - start; Log.d(Config.LOGTAG,"deleted old edit information in "+diff+"ms"); } + if (oldVersion < 47 && newVersion >= 47) { + db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.PRESENCE_NAME + " TEXT"); + } } private void canonicalizeJids(SQLiteDatabase db) { @@ -573,7 +577,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { continue; } - String updateArgs[] = { + final String[] updateArgs = { newJid, cursor.getString(cursor.getColumnIndex(Conversation.UUID)), }; @@ -1011,7 +1015,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { final SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); for (Contact contact : roster.getContacts()) { - if (contact.getOption(Contact.Options.IN_ROSTER) || contact.getAvatarFilename() != null || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) { + if (contact.getOption(Contact.Options.IN_ROSTER) || contact.hasAvatarOrPresenceName() || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) { db.insert(Contact.TABLENAME, null, contact.getContentValues()); } else { String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";