Update attachment backfill proto.

This commit is contained in:
Greyson Parrelli
2025-03-21 15:09:45 -04:00
committed by Cody Henthorne
parent e2961a3f6f
commit 7c9cd8964f
5 changed files with 32 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
package org.thoughtcrime.securesms
import org.thoughtcrime.securesms.util.RemoteConfig
import org.whispersystems.signalservice.api.account.AccountAttributes
object AppCapabilities {
@@ -13,7 +14,8 @@ object AppCapabilities {
storage = storageCapable,
deleteSync = true,
versionedExpirationTimer = true,
storageServiceEncryptionV2 = true
storageServiceEncryptionV2 = true,
attachmentBackfill = RemoteConfig.attachmentBackfillSync
)
}
}

View File

@@ -13,6 +13,7 @@ import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.jobmanager.Job
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
import org.thoughtcrime.securesms.jobs.protos.MultiDeviceAttachmentBackfillUpdateJobData
import org.thoughtcrime.securesms.util.MediaUtil
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage
import org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException
@@ -72,33 +73,25 @@ class MultiDeviceAttachmentBackfillUpdateJob(
}
override fun run(): Result {
val attachments = SignalDatabase.attachments.getAttachmentsForMessage(messageId).filterNot { it.quote }.sortedBy { it.displayOrder }
if (attachments.isEmpty()) {
val allAttachments = SignalDatabase.attachments.getAttachmentsForMessage(messageId)
val syncAttachments: List<DatabaseAttachment> = allAttachments
.filterNot { it.quote || it.contentType == MediaUtil.LONG_TEXT }
.sortedBy { it.displayOrder }
val longTextAttachment: DatabaseAttachment? = allAttachments.firstOrNull { it.contentType == MediaUtil.LONG_TEXT }
if (syncAttachments.isEmpty() && longTextAttachment == null) {
Log.w(TAG, "Failed to find any attachments for the message! Sending a missing response.")
MultiDeviceAttachmentBackfillMissingJob.enqueue(targetMessage, targetConversation)
return Result.failure()
}
val attachmentDatas = attachments.map { attachment ->
when {
attachment.hasData && !attachment.isInProgress && attachment.withinUploadThreshold() -> {
AttachmentData(attachment = attachment.toAttachmentPointer(context))
}
!attachment.hasData || attachment.isPermanentlyFailed -> {
AttachmentData(status = AttachmentData.Status.TERMINAL_ERROR)
}
else -> {
AttachmentData(status = AttachmentData.Status.PENDING)
}
}
}
val syncMessage = SignalServiceSyncMessage.forAttachmentBackfillResponse(
SyncMessage.AttachmentBackfillResponse(
targetMessage = targetMessage,
targetConversation = targetConversation,
attachments = SyncMessage.AttachmentBackfillResponse.AttachmentDataList(
attachments = attachmentDatas
attachments = syncAttachments.map { it.toAttachmentData() },
longText = longTextAttachment?.toAttachmentData()
)
)
)
@@ -131,6 +124,20 @@ class MultiDeviceAttachmentBackfillUpdateJob(
}
}
private fun DatabaseAttachment.toAttachmentData(): AttachmentData {
return when {
this.hasData && !this.isInProgress && this.withinUploadThreshold() -> {
AttachmentData(attachment = this.toAttachmentPointer(context))
}
!this.hasData || this.isPermanentlyFailed -> {
AttachmentData(status = AttachmentData.Status.TERMINAL_ERROR)
}
else -> {
AttachmentData(status = AttachmentData.Status.PENDING)
}
}
}
private fun DatabaseAttachment.withinUploadThreshold(): Boolean {
return this.uploadTimestamp > 0 && System.currentTimeMillis() - this.uploadTimestamp < UPLOAD_THRESHOLD
}

View File

@@ -1711,10 +1711,8 @@ object SyncMessageProcessor {
.enqueue()
}
if (needsUpload.size != attachments.size) {
log(timestamp, "[AttachmentBackfillRequest] At least one attachment didn't need to be uploaded. Enqueuing update job immediately.")
MultiDeviceAttachmentBackfillUpdateJob.enqueue(request.targetMessage!!, request.targetConversation!!, messageId)
}
// Enqueueing an update immediately to tell the requesting device that the primary is online.
MultiDeviceAttachmentBackfillUpdateJob.enqueue(request.targetMessage!!, request.targetConversation!!, messageId)
}
private fun ConversationIdentifier.toRecipientId(): RecipientId? {