mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Add single attachment delete sync.
This commit is contained in:
committed by
Greyson Parrelli
parent
ea87108def
commit
09003d85b1
@@ -69,6 +69,7 @@ import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
||||
import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream
|
||||
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream
|
||||
import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream
|
||||
import org.thoughtcrime.securesms.database.MessageTable.SyncMessageId
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase.Companion.messages
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase.Companion.stickers
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase.Companion.threads
|
||||
@@ -655,6 +656,47 @@ class AttachmentTable(
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAttachments(toDelete: List<SyncAttachmentId>): List<SyncMessageId> {
|
||||
val unhandled = mutableListOf<SyncMessageId>()
|
||||
for (syncAttachmentId in toDelete) {
|
||||
val messageId = SignalDatabase.messages.getMessageIdOrNull(syncAttachmentId.syncMessageId)
|
||||
if (messageId != null) {
|
||||
val attachments = readableDatabase
|
||||
.select(ID, ATTACHMENT_UUID, REMOTE_DIGEST, DATA_HASH_END)
|
||||
.from(TABLE_NAME)
|
||||
.where("$MESSAGE_ID = ?", messageId)
|
||||
.run()
|
||||
.readToList {
|
||||
SyncAttachment(
|
||||
id = AttachmentId(it.requireLong(ID)),
|
||||
uuid = UuidUtil.parseOrNull(it.requireString(ATTACHMENT_UUID)),
|
||||
digest = it.requireBlob(REMOTE_DIGEST),
|
||||
plaintextHash = it.requireString(DATA_HASH_END)
|
||||
)
|
||||
}
|
||||
|
||||
val byUuid: SyncAttachment? by lazy { attachments.firstOrNull { it.uuid != null && it.uuid == syncAttachmentId.uuid } }
|
||||
val byDigest: SyncAttachment? by lazy { attachments.firstOrNull { it.digest != null && it.digest.contentEquals(syncAttachmentId.digest) } }
|
||||
val byPlaintext: SyncAttachment? by lazy { attachments.firstOrNull { it.plaintextHash != null && it.plaintextHash == syncAttachmentId.plaintextHash } }
|
||||
|
||||
val attachmentToDelete = (byUuid ?: byDigest ?: byPlaintext)?.id
|
||||
if (attachmentToDelete != null) {
|
||||
if (attachments.size == 1) {
|
||||
SignalDatabase.messages.deleteMessage(messageId)
|
||||
} else {
|
||||
deleteAttachment(attachmentToDelete)
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, "Unable to locate sync attachment to delete for message:$messageId")
|
||||
}
|
||||
} else {
|
||||
unhandled += syncAttachmentId.syncMessageId
|
||||
}
|
||||
}
|
||||
|
||||
return unhandled
|
||||
}
|
||||
|
||||
fun trimAllAbandonedAttachments() {
|
||||
val deleteCount = writableDatabase
|
||||
.delete(TABLE_NAME)
|
||||
@@ -2295,4 +2337,8 @@ class AttachmentTable(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SyncAttachmentId(val syncMessageId: SyncMessageId, val uuid: UUID?, val digest: ByteArray?, val plaintextHash: String?)
|
||||
|
||||
class SyncAttachment(val id: AttachmentId, val uuid: UUID?, val digest: ByteArray?, val plaintextHash: String?)
|
||||
}
|
||||
|
||||
@@ -3465,6 +3465,15 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
|
||||
}
|
||||
}
|
||||
|
||||
fun getMessageIdOrNull(message: SyncMessageId): Long? {
|
||||
return readableDatabase
|
||||
.select(ID)
|
||||
.from(TABLE_NAME)
|
||||
.where("$DATE_SENT = ? AND $FROM_RECIPIENT_ID = ?", message.timetamp, message.recipientId)
|
||||
.run()
|
||||
.readToSingleLongOrNull()
|
||||
}
|
||||
|
||||
fun deleteMessages(messagesToDelete: List<MessageTable.SyncMessageId>): List<SyncMessageId> {
|
||||
val threads = mutableSetOf<Long>()
|
||||
val unhandled = mutableListOf<SyncMessageId>()
|
||||
|
||||
Reference in New Issue
Block a user