Fix link preview backup import/export.

This commit is contained in:
Greyson Parrelli 2024-09-25 16:07:38 -04:00
parent 81d99c9d30
commit e77d9d3ad6
3 changed files with 23 additions and 14 deletions

View file

@ -156,6 +156,11 @@ class ArchiveImportExportTests {
runTests { it.startsWith("chat_item_standard_message_with_edits_") }
}
// @Test
fun chatItemStandardMessageWithLinkPreview() {
runTests { it.startsWith("chat_item_standard_message_with_link_preview_") }
}
// @Test
fun chatItemStandardMessageWithQuote() {
runTests { it.startsWith("chat_item_standard_message_with_quote_") }

View file

@ -12,6 +12,7 @@ import org.json.JSONException
import org.signal.core.util.Base64
import org.signal.core.util.Hex
import org.signal.core.util.logging.Log
import org.signal.core.util.nullIfEmpty
import org.signal.core.util.orNull
import org.signal.core.util.requireBlob
import org.signal.core.util.requireBoolean
@ -579,12 +580,12 @@ class ChatItemExportIterator(private val cursor: Cursor, private val batchSize:
return emptyList()
}
private fun LinkPreview.toBackupLinkPreview(): org.thoughtcrime.securesms.backup.v2.proto.LinkPreview {
private fun LinkPreview.toRemoteLinkPreview(): org.thoughtcrime.securesms.backup.v2.proto.LinkPreview {
return org.thoughtcrime.securesms.backup.v2.proto.LinkPreview(
url = url,
title = title,
title = title.nullIfEmpty(),
image = (thumbnail.orNull() as? DatabaseAttachment)?.toRemoteMessageAttachment()?.pointer,
description = description,
description = description.nullIfEmpty(),
date = date
)
}
@ -690,7 +691,7 @@ class ChatItemExportIterator(private val cursor: Cursor, private val batchSize:
quote = this.toQuote(quotedAttachments),
text = text,
attachments = messageAttachments.toBackupAttachments(),
linkPreview = linkPreviews.map { it.toBackupLinkPreview() },
linkPreview = linkPreviews.map { it.toRemoteLinkPreview() },
longText = longTextAttachment?.toRemoteFilePointer(mediaArchiveEnabled),
reactions = reactionRecords.toBackupReactions()
)

View file

@ -400,18 +400,21 @@ class ChatItemImportInserter(
it.toLocalAttachment()
} ?: emptyList()
if (attachments.isNotEmpty() || linkPreviewAttachments.isNotEmpty() || quoteAttachments.isNotEmpty() || longTextAttachments.isNotEmpty()) {
val hasAttachments = attachments.isNotEmpty() || linkPreviewAttachments.isNotEmpty() || quoteAttachments.isNotEmpty() || longTextAttachments.isNotEmpty()
if (hasAttachments || linkPreviews.isNotEmpty()) {
followUp = { messageRowId ->
val attachmentMap = SignalDatabase.attachments.insertAttachmentsForMessage(messageRowId, attachments + linkPreviewAttachments + longTextAttachments, quoteAttachments)
val attachmentMap = if (hasAttachments) {
SignalDatabase.attachments.insertAttachmentsForMessage(messageRowId, attachments + linkPreviewAttachments + longTextAttachments, quoteAttachments)
} else {
emptyMap()
}
if (linkPreviews.isNotEmpty()) {
db.update(
MessageTable.TABLE_NAME,
contentValuesOf(
MessageTable.LINK_PREVIEWS to SignalDatabase.messages.getSerializedLinkPreviews(attachmentMap, linkPreviews)
),
"${MessageTable.ID} = ?",
SqlUtil.buildArgs(messageRowId)
)
db.update(MessageTable.TABLE_NAME)
.values(MessageTable.LINK_PREVIEWS to SignalDatabase.messages.getSerializedLinkPreviews(attachmentMap, linkPreviews))
.where("${MessageTable.ID} = ?", messageRowId)
.run()
}
}
}