From da04f536f71aabd5621279c0935ef4a5a6fea65a Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Fri, 9 Aug 2024 20:01:32 +0200 Subject: [PATCH] remove dao methods to delete data when user is removed ...will be done by foreign keys cascading. Therefore, also added foreign key to ChatBlockEntity Signed-off-by: Marcel Hibbe --- .../10.json | 36 +++++++++++++++---- .../network/OfflineFirstChatRepository.kt | 4 +++ .../database/mappers/ConversationMapUtils.kt | 2 ++ .../data/database/model/ChatBlockEntity.kt | 20 +++++++---- .../data/database/model/ConversationEntity.kt | 2 +- .../talk/jobs/AccountRemovalWorker.java | 9 ----- .../talk/models/domain/ConversationModel.kt | 3 +- 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/10.json b/app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/10.json index 5d5e9dc49..cdff3dfae 100644 --- a/app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/10.json +++ b/app/schemas/com.nextcloud.talk.data.source.local.TalkDatabase/10.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 10, - "identityHash": "1b97b7e937102e4087f8534f1204fe94", + "identityHash": "c07a2543aa583e08e7b3208f44fcc7ac", "entities": [ { "tableName": "User", @@ -138,7 +138,7 @@ }, { "tableName": "Conversations", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`internalId` TEXT NOT NULL, `accountId` INTEGER, `token` TEXT, `name` TEXT, `displayName` TEXT, `description` TEXT, `type` TEXT, `lastPing` INTEGER NOT NULL, `participantType` TEXT, `hasPassword` INTEGER NOT NULL, `sessionId` TEXT, `actorId` TEXT, `actorType` TEXT, `isFavorite` INTEGER NOT NULL, `lastActivity` INTEGER NOT NULL, `unreadMessages` INTEGER NOT NULL, `unreadMention` INTEGER NOT NULL, `lastMessageJson` TEXT, `objectType` TEXT, `notificationLevel` TEXT, `readOnly` TEXT, `lobbyState` TEXT, `lobbyTimer` INTEGER, `lastReadMessage` INTEGER NOT NULL, `lastCommonReadMessage` INTEGER NOT NULL, `hasCall` INTEGER NOT NULL, `callFlag` INTEGER NOT NULL, `canStartCall` INTEGER NOT NULL, `canLeaveConversation` INTEGER, `canDeleteConversation` INTEGER, `unreadMentionDirect` INTEGER, `notificationCalls` INTEGER, `permissions` INTEGER NOT NULL, `messageExpiration` INTEGER NOT NULL, `status` TEXT, `statusIcon` TEXT, `statusMessage` TEXT, `statusClearAt` INTEGER, `callRecording` INTEGER NOT NULL, `avatarVersion` TEXT, `isCustomAvatar` INTEGER, `callStartTime` INTEGER, `recordingConsent` INTEGER NOT NULL, `remoteServer` TEXT, `remoteToken` TEXT, PRIMARY KEY(`internalId`), FOREIGN KEY(`accountId`) REFERENCES `User`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`internalId` TEXT NOT NULL, `accountId` INTEGER NOT NULL, `token` TEXT, `name` TEXT, `displayName` TEXT, `description` TEXT, `type` TEXT, `lastPing` INTEGER NOT NULL, `participantType` TEXT, `hasPassword` INTEGER NOT NULL, `sessionId` TEXT, `actorId` TEXT, `actorType` TEXT, `isFavorite` INTEGER NOT NULL, `lastActivity` INTEGER NOT NULL, `unreadMessages` INTEGER NOT NULL, `unreadMention` INTEGER NOT NULL, `lastMessageJson` TEXT, `objectType` TEXT, `notificationLevel` TEXT, `readOnly` TEXT, `lobbyState` TEXT, `lobbyTimer` INTEGER, `lastReadMessage` INTEGER NOT NULL, `lastCommonReadMessage` INTEGER NOT NULL, `hasCall` INTEGER NOT NULL, `callFlag` INTEGER NOT NULL, `canStartCall` INTEGER NOT NULL, `canLeaveConversation` INTEGER, `canDeleteConversation` INTEGER, `unreadMentionDirect` INTEGER, `notificationCalls` INTEGER, `permissions` INTEGER NOT NULL, `messageExpiration` INTEGER NOT NULL, `status` TEXT, `statusIcon` TEXT, `statusMessage` TEXT, `statusClearAt` INTEGER, `callRecording` INTEGER NOT NULL, `avatarVersion` TEXT, `isCustomAvatar` INTEGER, `callStartTime` INTEGER, `recordingConsent` INTEGER NOT NULL, `remoteServer` TEXT, `remoteToken` TEXT, PRIMARY KEY(`internalId`), FOREIGN KEY(`accountId`) REFERENCES `User`(`id`) ON UPDATE CASCADE ON DELETE CASCADE )", "fields": [ { "fieldPath": "internalId", @@ -150,7 +150,7 @@ "fieldPath": "accountId", "columnName": "accountId", "affinity": "INTEGER", - "notNull": false + "notNull": true }, { "fieldPath": "token", @@ -633,7 +633,7 @@ }, { "tableName": "ChatBlocks", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `internalConversationId` TEXT NOT NULL, `oldestMessageId` INTEGER NOT NULL, `newestMessageId` INTEGER NOT NULL, `hasHistory` INTEGER NOT NULL)", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `internalConversationId` TEXT NOT NULL, `accountId` INTEGER, `token` TEXT, `oldestMessageId` INTEGER NOT NULL, `newestMessageId` INTEGER NOT NULL, `hasHistory` INTEGER NOT NULL, FOREIGN KEY(`internalConversationId`) REFERENCES `Conversations`(`internalId`) ON UPDATE CASCADE ON DELETE CASCADE )", "fields": [ { "fieldPath": "id", @@ -647,6 +647,18 @@ "affinity": "TEXT", "notNull": true }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "token", + "columnName": "token", + "affinity": "TEXT", + "notNull": false + }, { "fieldPath": "oldestMessageId", "columnName": "oldestMessageId", @@ -673,13 +685,25 @@ ] }, "indices": [], - "foreignKeys": [] + "foreignKeys": [ + { + "table": "Conversations", + "onDelete": "CASCADE", + "onUpdate": "CASCADE", + "columns": [ + "internalConversationId" + ], + "referencedColumns": [ + "internalId" + ] + } + ] } ], "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, '1b97b7e937102e4087f8534f1204fe94')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'c07a2543aa583e08e7b3208f44fcc7ac')" ] } } \ No newline at end of file diff --git a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt index 541c7a7be..fd3a4f4ab 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/data/network/OfflineFirstChatRepository.kt @@ -421,6 +421,8 @@ class OfflineFirstChatRepository @Inject constructor( val newChatBlock = ChatBlockEntity( internalConversationId = internalConversationId, + accountId = conversationModel.accountId, + token = conversationModel.token, oldestMessageId = oldestMessageIdForNewChatBlock, newestMessageId = newestMessageIdForNewChatBlock, hasHistory = hasHistory @@ -531,6 +533,8 @@ class OfflineFirstChatRepository @Inject constructor( val newChatBlock = ChatBlockEntity( internalConversationId = internalConversationId, + accountId = conversationModel.accountId, + token = conversationModel.token, oldestMessageId = oldestIdFromDbChatBlocks, newestMessageId = newestIdFromDbChatBlocks, hasHistory = hasHistory diff --git a/app/src/main/java/com/nextcloud/talk/data/database/mappers/ConversationMapUtils.kt b/app/src/main/java/com/nextcloud/talk/data/database/mappers/ConversationMapUtils.kt index bd3424a3c..f6893735b 100644 --- a/app/src/main/java/com/nextcloud/talk/data/database/mappers/ConversationMapUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/data/database/mappers/ConversationMapUtils.kt @@ -16,6 +16,7 @@ import com.nextcloud.talk.models.json.conversations.Conversation fun ConversationModel.asEntity() = ConversationEntity( internalId = internalId, + accountId = accountId, token = token, name = name, displayName = displayName, @@ -64,6 +65,7 @@ fun ConversationModel.asEntity() = fun ConversationEntity.asModel() = ConversationModel( internalId = internalId, + accountId = accountId, token = token, name = name, displayName = displayName, diff --git a/app/src/main/java/com/nextcloud/talk/data/database/model/ChatBlockEntity.kt b/app/src/main/java/com/nextcloud/talk/data/database/model/ChatBlockEntity.kt index f1f8cb6a2..7559edabc 100644 --- a/app/src/main/java/com/nextcloud/talk/data/database/model/ChatBlockEntity.kt +++ b/app/src/main/java/com/nextcloud/talk/data/database/model/ChatBlockEntity.kt @@ -12,18 +12,24 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity( - tableName = "ChatBlocks" - // indices = [ - // androidx.room.Index(value = ["accountId"]) - // ] + tableName = "ChatBlocks", + foreignKeys = [ + androidx.room.ForeignKey( + entity = ConversationEntity::class, + parentColumns = arrayOf("internalId"), + childColumns = arrayOf("internalConversationId"), + onDelete = androidx.room.ForeignKey.CASCADE, + onUpdate = androidx.room.ForeignKey.CASCADE + ) + ], ) data class ChatBlockEntity( @PrimaryKey(autoGenerate = true) - @ColumnInfo(name = "id") var id: Int = 0, + @ColumnInfo(name = "id") var id: Long = 0, // accountId@token @ColumnInfo(name = "internalConversationId") var internalConversationId: String, - // @ColumnInfo(name = "accountId") var accountId: Long? = null, - // @ColumnInfo(name = "token") var token: String?, + @ColumnInfo(name = "accountId") var accountId: Long? = null, + @ColumnInfo(name = "token") var token: String?, @ColumnInfo(name = "oldestMessageId") var oldestMessageId: Long, @ColumnInfo(name = "newestMessageId") var newestMessageId: Long, @ColumnInfo(name = "hasHistory") var hasHistory: Boolean diff --git a/app/src/main/java/com/nextcloud/talk/data/database/model/ConversationEntity.kt b/app/src/main/java/com/nextcloud/talk/data/database/model/ConversationEntity.kt index d8d99a00d..7d637aa16 100644 --- a/app/src/main/java/com/nextcloud/talk/data/database/model/ConversationEntity.kt +++ b/app/src/main/java/com/nextcloud/talk/data/database/model/ConversationEntity.kt @@ -36,7 +36,7 @@ data class ConversationEntity( var internalId: String, // Defines to which talk app account this conversation belongs to - @ColumnInfo(name = "accountId") var accountId: Long? = null, + @ColumnInfo(name = "accountId") var accountId: Long, // We don't use token as primary key as we have to manage multiple talk app accounts on // the phone, thus multiple accounts can have the same conversation in their list. That's why the servers diff --git a/app/src/main/java/com/nextcloud/talk/jobs/AccountRemovalWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/AccountRemovalWorker.java index 507c340ae..18782a5be 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/AccountRemovalWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/AccountRemovalWorker.java @@ -182,7 +182,6 @@ public class AccountRemovalWorker extends Worker { try { arbitraryStorageManager.deleteAllEntriesForAccountIdentifier(user.getId()); - deleteAllUserInfo(user); deleteUser(user); } catch (Throwable e) { Log.e(TAG, "error while trying to delete All Entries For Account Identifier", e); @@ -190,14 +189,6 @@ public class AccountRemovalWorker extends Worker { } } - private void deleteAllUserInfo(User user) { - String accountId = Objects.requireNonNull(user.getId()).toString(); - String pattern = accountId + "@%"; // ... LIKE "@%" - chatMessagesDao.clearAllMessagesForUser(pattern); - conversationsDao.clearAllConversationsForUser(user.getId()); - chatBlocksDao.clearChatBlocksForUser(pattern); - } - private void deleteUser(User user) { if (user.getId() != null) { String username = user.getUsername(); diff --git a/app/src/main/java/com/nextcloud/talk/models/domain/ConversationModel.kt b/app/src/main/java/com/nextcloud/talk/models/domain/ConversationModel.kt index 3fef24bdc..480e78d03 100644 --- a/app/src/main/java/com/nextcloud/talk/models/domain/ConversationModel.kt +++ b/app/src/main/java/com/nextcloud/talk/models/domain/ConversationModel.kt @@ -15,6 +15,7 @@ import com.nextcloud.talk.models.json.participants.Participant class ConversationModel( var internalId: String, + var accountId: Long, var roomId: String? = null, var token: String? = null, var name: String? = null, @@ -32,7 +33,6 @@ class ConversationModel( var lastActivity: Long = 0, var unreadMessages: Int = 0, var unreadMention: Boolean = false, - // var lastMessageViaConversationList: LastMessageJson? = null, var lastMessageViaConversationList: ChatMessageJson? = null, var objectType: ConversationEnums.ObjectType? = null, var notificationLevel: ConversationEnums.NotificationLevel? = null, @@ -67,6 +67,7 @@ class ConversationModel( fun mapToConversationModel(conversation: Conversation, user: User): ConversationModel { return ConversationModel( internalId = user.id!!.toString() + "@" + conversation.token, + accountId = user.id!!, roomId = conversation.roomId, token = conversation.token, name = conversation.name,