Improve backup error handling for sticker packs.

This commit is contained in:
Greyson Parrelli 2025-01-21 10:58:45 -05:00
parent e6368982c9
commit 096eea70d1
3 changed files with 60 additions and 13 deletions

View file

@ -51,6 +51,22 @@ object ExportSkips {
return log(sentTimestamp, "Direct story reply has no body.")
}
fun invalidChatItemStickerPackId(sentTimestamp: Long): String {
return log(sentTimestamp, "Sticker message had an invalid packId.")
}
fun invalidChatItemStickerPackKey(sentTimestamp: Long): String {
return log(sentTimestamp, "Sticker message had an invalid packKey.")
}
fun invalidStickerPackId(): String {
return log(0, "Sticker pack had an invalid packId.")
}
fun invalidStickerPackKey(): String {
return log(0, "Sticker pack had an invalid packKey.")
}
private fun log(sentTimestamp: Long, message: String): String {
return "[SKIP][$sentTimestamp] $message"
}

View file

@ -306,7 +306,7 @@ class ChatItemArchiveExporter(
val sticker = attachments?.firstOrNull { dbAttachment -> dbAttachment.isSticker }
if (sticker?.stickerLocator != null) {
builder.stickerMessage = sticker.toRemoteStickerMessage(mediaArchiveEnabled = mediaArchiveEnabled, reactions = extraData.reactionsById[id])
builder.stickerMessage = sticker.toRemoteStickerMessage(sentTimestamp = record.dateSent, mediaArchiveEnabled = mediaArchiveEnabled, reactions = extraData.reactionsById[id])
} else {
val standardMessage = record.toRemoteStandardMessage(
db = db,
@ -964,12 +964,27 @@ private fun BackupMessageRecord.toRemoteGiftBadgeUpdate(): BackupGiftBadge? {
)
}
private fun DatabaseAttachment.toRemoteStickerMessage(mediaArchiveEnabled: Boolean, reactions: List<ReactionRecord>?): StickerMessage {
private fun DatabaseAttachment.toRemoteStickerMessage(sentTimestamp: Long, mediaArchiveEnabled: Boolean, reactions: List<ReactionRecord>?): StickerMessage? {
val stickerLocator = this.stickerLocator!!
val packId = try {
Hex.fromStringCondensed(stickerLocator.packId)
} catch (e: IOException) {
Log.w(TAG, ExportSkips.invalidChatItemStickerPackId(sentTimestamp))
return null
}
val packKey = try {
Hex.fromStringCondensed(stickerLocator.packKey)
} catch (e: IOException) {
Log.w(TAG, ExportSkips.invalidChatItemStickerPackKey(sentTimestamp))
return null
}
return StickerMessage(
sticker = Sticker(
packId = Hex.fromStringCondensed(stickerLocator.packId).toByteString(),
packKey = Hex.fromStringCondensed(stickerLocator.packKey).toByteString(),
packId = packId.toByteString(),
packKey = packKey.toByteString(),
stickerId = stickerLocator.stickerId,
emoji = stickerLocator.emoji,
data_ = this.toRemoteMessageAttachment(mediaArchiveEnabled).pointer

View file

@ -8,6 +8,8 @@ package org.thoughtcrime.securesms.backup.v2.processor
import okio.ByteString.Companion.toByteString
import org.signal.core.util.Hex
import org.signal.core.util.insertInto
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.backup.v2.ExportSkips
import org.thoughtcrime.securesms.backup.v2.proto.Frame
import org.thoughtcrime.securesms.backup.v2.proto.StickerPack
import org.thoughtcrime.securesms.backup.v2.stream.BackupFrameEmitter
@ -18,6 +20,9 @@ import org.thoughtcrime.securesms.database.StickerTable.StickerPackRecordReader
import org.thoughtcrime.securesms.database.model.StickerPackRecord
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.jobs.StickerPackDownloadJob
import java.io.IOException
private val TAG = Log.tag(StickerArchiveProcessor::class)
/**
* Handles importing/exporting [StickerPack] frames for an archive.
@ -25,13 +30,12 @@ import org.thoughtcrime.securesms.jobs.StickerPackDownloadJob
object StickerArchiveProcessor {
fun export(db: SignalDatabase, emitter: BackupFrameEmitter) {
StickerPackRecordReader(db.stickerTable.getAllStickerPacks()).use { reader ->
var record: StickerPackRecord? = reader.getNext()
while (record != null) {
if (record.isInstalled) {
val frame = record.toBackupFrame()
var record: StickerPackRecord? = null
while (reader.getNext()?.let { record = it } != null) {
if (record!!.isInstalled) {
val frame = record!!.toBackupFrame() ?: continue
emitter.emit(frame)
}
record = reader.getNext()
}
}
}
@ -62,12 +66,24 @@ object StickerArchiveProcessor {
}
}
private fun StickerPackRecord.toBackupFrame(): Frame {
val packIdBytes = Hex.fromStringCondensed(packId)
val packKey = Hex.fromStringCondensed(packKey)
private fun StickerPackRecord.toBackupFrame(): Frame? {
val packIdBytes = try {
Hex.fromStringCondensed(this.packId)
} catch (e: IOException) {
Log.w(TAG, ExportSkips.invalidStickerPackId())
return null
}
val packKeyBytes = try {
Hex.fromStringCondensed(this.packKey)
} catch (e: IOException) {
Log.w(TAG, ExportSkips.invalidStickerPackKey())
return null
}
val pack = StickerPack(
packId = packIdBytes.toByteString(),
packKey = packKey.toByteString()
packKey = packKeyBytes.toByteString()
)
return Frame(stickerPack = pack)
}