From 5e79dd8b68445dfc2081daaf730dcefa08076c7c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 1 Mar 2023 07:55:28 +0100 Subject: [PATCH] left join trust into MessageWithContentReactions --- .../1.json | 28 +++++++++++-------- .../xmpp/MessageTransformationTest.java | 2 ++ .../database/AxolotlDatabaseStore.java | 8 +----- .../android/database/dao/AxolotlDao.java | 10 +++---- .../android/database/dao/MessageDao.java | 10 ++++--- .../entity/AxolotlIdentityEntity.java | 15 ++++++---- .../model/MessageWithContentReactions.java | 1 + 7 files changed, 40 insertions(+), 34 deletions(-) diff --git a/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json b/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json index 0c37ff688..dc9e8b7a0 100644 --- a/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json +++ b/app/schemas/im.conversations.android.database.ConversationsDatabase/1.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "8f66e4197a1676d0164aa77eea8f2a20", + "identityHash": "9d2672ebdefc9df52add6eebf635031f", "entities": [ { "tableName": "account", @@ -432,7 +432,7 @@ }, { "tableName": "axolotl_identity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `accountId` INTEGER NOT NULL, `address` TEXT NOT NULL, `deviceId` INTEGER NOT NULL, `identityKey` BLOB NOT NULL, `trust` TEXT NOT NULL, FOREIGN KEY(`accountId`) REFERENCES `account`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `accountId` INTEGER NOT NULL, `address` TEXT NOT NULL, `identityKey` BLOB NOT NULL, `trust` TEXT NOT NULL, FOREIGN KEY(`accountId`) REFERENCES `account`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", "fields": [ { "fieldPath": "id", @@ -452,12 +452,6 @@ "affinity": "TEXT", "notNull": true }, - { - "fieldPath": "deviceId", - "columnName": "deviceId", - "affinity": "INTEGER", - "notNull": true - }, { "fieldPath": "identityKey", "columnName": "identityKey", @@ -479,15 +473,25 @@ }, "indices": [ { - "name": "index_axolotl_identity_accountId_address_deviceId", + "name": "index_axolotl_identity_accountId_address_identityKey", "unique": true, "columnNames": [ "accountId", "address", - "deviceId" + "identityKey" ], "orders": [], - "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_axolotl_identity_accountId_address_deviceId` ON `${TABLE_NAME}` (`accountId`, `address`, `deviceId`)" + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_axolotl_identity_accountId_address_identityKey` ON `${TABLE_NAME}` (`accountId`, `address`, `identityKey`)" + }, + { + "name": "index_axolotl_identity_accountId_identityKey", + "unique": false, + "columnNames": [ + "accountId", + "identityKey" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_axolotl_identity_accountId_identityKey` ON `${TABLE_NAME}` (`accountId`, `identityKey`)" } ], "foreignKeys": [ @@ -2376,7 +2380,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8f66e4197a1676d0164aa77eea8f2a20')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '9d2672ebdefc9df52add6eebf635031f')" ] } } \ No newline at end of file diff --git a/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java b/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java index e506350a9..a4321f035 100644 --- a/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java +++ b/app/src/androidTest/java/im/conversations/android/xmpp/MessageTransformationTest.java @@ -427,6 +427,8 @@ public class MessageTransformationTest { Assert.assertEquals(1L, embeddedMessage.contents.size()); Assert.assertEquals( "Hi. How are you?", Iterables.getOnlyElement(embeddedMessage.contents).body); + Assert.assertNull(response.identityKey); + Assert.assertNull(response.trust); } @Test diff --git a/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java b/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java index b31bffc00..d5eb80a25 100644 --- a/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java +++ b/app/src/main/java/im/conversations/android/database/AxolotlDatabaseStore.java @@ -50,13 +50,7 @@ public class AxolotlDatabaseStore extends AbstractAccountService implements Sign } else { trust = Trust.TRUSTED; } - return axolotlDao() - .setIdentity( - account, - address.getJid(), - address.getDeviceId(), - identityKey, - trust); + return axolotlDao().setIdentity(account, address.getJid(), identityKey, trust); }); } diff --git a/app/src/main/java/im/conversations/android/database/dao/AxolotlDao.java b/app/src/main/java/im/conversations/android/database/dao/AxolotlDao.java index 07c4e3909..d33910d6d 100644 --- a/app/src/main/java/im/conversations/android/database/dao/AxolotlDao.java +++ b/app/src/main/java/im/conversations/android/database/dao/AxolotlDao.java @@ -127,12 +127,11 @@ public abstract class AxolotlDao { public boolean setIdentity( final Account account, final BareJid address, - final int deviceId, final IdentityKey identityKey, final Trust trust) { - final var existing = getIdentityKey(account.id, address, deviceId); + final var existing = getIdentityKey(account.id, address, identityKey); if (existing == null || !existing.equals(identityKey)) { - insert(AxolotlIdentityEntity.of(account, address, deviceId, identityKey, trust)); + insert(AxolotlIdentityEntity.of(account, address, identityKey, trust)); return true; } else { return false; @@ -155,8 +154,9 @@ public abstract class AxolotlDao { @Query( "SELECT identityKey FROM AXOLOTL_IDENTITY WHERE accountId=:account AND" - + " address=:address AND deviceId=:deviceId") - protected abstract IdentityKey getIdentityKey(long account, BareJid address, int deviceId); + + " address=:address AND identityKey=:identityKey") + protected abstract IdentityKey getIdentityKey( + long account, BareJid address, IdentityKey identityKey); @Query( "SELECT preKeyRecord FROM axolotl_pre_key WHERE accountId=:account AND" diff --git a/app/src/main/java/im/conversations/android/database/dao/MessageDao.java b/app/src/main/java/im/conversations/android/database/dao/MessageDao.java index 4dd0943ab..2934ee7c4 100644 --- a/app/src/main/java/im/conversations/android/database/dao/MessageDao.java +++ b/app/src/main/java/im/conversations/android/database/dao/MessageDao.java @@ -422,10 +422,12 @@ public abstract class MessageDao { @Query( "SELECT message.id as" + " id,sentAt,outgoing,toBare,toResource,fromBare,fromResource,modification,latestVersion" - + " as version,inReplyToMessageEntityId,encryption,identityKey FROM message JOIN" - + " message_version ON message.latestVersion=message_version.id WHERE" - + " message.chatId=:chatId AND latestVersion IS NOT NULL ORDER BY" - + " message.receivedAt") + + " as version,inReplyToMessageEntityId,encryption,message_version.identityKey,trust" + + " FROM chat JOIN message on message.chatId=chat.id JOIN message_version ON" + + " message.latestVersion=message_version.id LEFT JOIN axolotl_identity ON" + + " chat.accountId=axolotl_identity.accountId AND" + + " message_version.identityKey=axolotl_identity.identityKey WHERE chat.id=:chatId" + + " AND latestVersion IS NOT NULL ORDER BY message.receivedAt") public abstract List getMessages(long chatId); public void setInReplyTo( diff --git a/app/src/main/java/im/conversations/android/database/entity/AxolotlIdentityEntity.java b/app/src/main/java/im/conversations/android/database/entity/AxolotlIdentityEntity.java index 5c9a4055a..8595e30c1 100644 --- a/app/src/main/java/im/conversations/android/database/entity/AxolotlIdentityEntity.java +++ b/app/src/main/java/im/conversations/android/database/entity/AxolotlIdentityEntity.java @@ -20,8 +20,11 @@ import org.whispersystems.libsignal.IdentityKey; onDelete = ForeignKey.CASCADE), indices = { @Index( - value = {"accountId", "address", "deviceId"}, - unique = true) + value = {"accountId", "address", "identityKey"}, + unique = true), + @Index( + value = {"accountId", "identityKey"}, + unique = false) }) public class AxolotlIdentityEntity { @@ -32,18 +35,18 @@ public class AxolotlIdentityEntity { @NonNull public BareJid address; - @NonNull public Integer deviceId; - @NonNull public IdentityKey identityKey; @NonNull public Trust trust; public static AxolotlIdentityEntity of( - Account account, BareJid address, int deviceId, IdentityKey identityKey, Trust trust) { + final Account account, + final BareJid address, + final IdentityKey identityKey, + final Trust trust) { final var entity = new AxolotlIdentityEntity(); entity.accountId = account.id; entity.address = address; - entity.deviceId = deviceId; entity.identityKey = identityKey; entity.trust = trust; return entity; diff --git a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java index 6305ded9f..7b00c307d 100644 --- a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java +++ b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java @@ -34,6 +34,7 @@ public class MessageWithContentReactions { public Long inReplyToMessageEntityId; public Encryption encryption; public IdentityKey identityKey; + public Trust trust; @Relation( entity = MessageEntity.class,