From 2e89b8acee75d6616f9746d0f4945c07b5bfbda2 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 28 Jan 2025 10:30:58 -0500 Subject: [PATCH] Skip messages that aren't in the right place. --- .../securesms/backup/v2/ArchiveErrorCases.kt | 8 ++++++++ .../securesms/backup/v2/BackupRepository.kt | 3 ++- .../v2/exporters/ChatItemArchiveExporter.kt | 15 +++++++++++++++ .../backup/v2/processor/ChatArchiveProcessor.kt | 1 + .../v2/processor/RecipientArchiveProcessor.kt | 2 ++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/ArchiveErrorCases.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/ArchiveErrorCases.kt index 8af617348b..9450e0cf7a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/ArchiveErrorCases.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/ArchiveErrorCases.kt @@ -79,6 +79,14 @@ object ExportSkips { return log(sentTimestamp, "Identity verified update for ourselves.") } + fun fromRecipientIsNotAnIndividual(sentTimestamp: Long): String { + return log(sentTimestamp, "The fromRecipient does not represent an individual person.") + } + + fun oneOnOneMessageInTheWrongChat(sentTimestamp: Long): String { + return log(sentTimestamp, "A 1:1 message is located in the wrong chat.") + } + private fun log(sentTimestamp: Long, message: String): String { return "[SKIP][$sentTimestamp] $message" } diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt index 7f9e1c6552..979860b96b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/BackupRepository.kt @@ -1523,7 +1523,8 @@ data class ArchivedMediaObject(val mediaId: String, val cdn: Int) class ExportState(val backupTime: Long, val mediaBackupEnabled: Boolean) { val recipientIds: MutableSet = hashSetOf() val threadIds: MutableSet = hashSetOf() - val localToRemoteCustomChatColors: MutableMap = hashMapOf() + val contactRecipientIds: MutableSet = hashSetOf() + val threadIdToRecipientId: MutableMap = hashMapOf() } class ImportState(val mediaRootBackupKey: MediaRootBackupKey) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/exporters/ChatItemArchiveExporter.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/exporters/ChatItemArchiveExporter.kt index c34555c180..7b8884efc7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/exporters/ChatItemArchiveExporter.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/exporters/ChatItemArchiveExporter.kt @@ -427,6 +427,10 @@ private fun simpleUpdate(type: SimpleChatUpdate.Type): ChatUpdateMessage { private fun BackupMessageRecord.toBasicChatItemBuilder(selfRecipientId: RecipientId, isGroupThread: Boolean, groupReceipts: List?, exportState: ExportState, backupStartTime: Long): ChatItem.Builder? { val record = this + if (this.threadId !in exportState.threadIds) { + return null + } + val direction = when { record.type.isDirectionlessType() && !record.remoteDeleted -> { Direction.DIRECTIONLESS @@ -450,6 +454,17 @@ private fun BackupMessageRecord.toBasicChatItemBuilder(selfRecipientId: Recipien else -> record.fromRecipientId } + if (!exportState.contactRecipientIds.contains(fromRecipientId)) { + Log.w(TAG, ExportSkips.fromRecipientIsNotAnIndividual(this.dateSent)) + return null + } + + val threadRecipientId = exportState.threadIdToRecipientId[record.threadId]!! + if (exportState.contactRecipientIds.contains(threadRecipientId) && fromRecipientId != threadRecipientId && fromRecipientId != selfRecipientId.toLong()) { + Log.w(TAG, ExportSkips.oneOnOneMessageInTheWrongChat(this.dateSent)) + return null + } + val builder = ChatItem.Builder().apply { chatId = record.threadId authorId = fromRecipientId diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/ChatArchiveProcessor.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/ChatArchiveProcessor.kt index 25f59033a6..9cb2f0df3b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/ChatArchiveProcessor.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/ChatArchiveProcessor.kt @@ -32,6 +32,7 @@ object ChatArchiveProcessor { for (chat in reader) { if (exportState.recipientIds.contains(chat.recipientId)) { exportState.threadIds.add(chat.id) + exportState.threadIdToRecipientId[chat.id] = chat.recipientId emitter.emit(Frame(chat = chat)) } else { Log.w(TAG, "dropping thread for deleted recipient ${chat.recipientId}") diff --git a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/RecipientArchiveProcessor.kt b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/RecipientArchiveProcessor.kt index fcac5a15ba..27c0390c90 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/RecipientArchiveProcessor.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/backup/v2/processor/RecipientArchiveProcessor.kt @@ -38,6 +38,7 @@ object RecipientArchiveProcessor { val releaseChannelId = signalStore.releaseChannelValues.releaseChannelRecipientId if (releaseChannelId != null) { exportState.recipientIds.add(releaseChannelId.toLong()) + exportState.contactRecipientIds.add(releaseChannelId.toLong()) emitter.emit( Frame( recipient = ArchiveRecipient( @@ -54,6 +55,7 @@ object RecipientArchiveProcessor { for (recipient in reader) { if (recipient != null) { exportState.recipientIds.add(recipient.id) + exportState.contactRecipientIds.add(recipient.id) emitter.emit(Frame(recipient = recipient)) } }