From 63d854986562c8d2619b04069db370d780c0d46c Mon Sep 17 00:00:00 2001 From: Clark Date: Wed, 31 May 2023 11:43:38 -0400 Subject: [PATCH] Light refactor to thread update. --- .../securesms/database/ThreadTable.kt | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadTable.kt index 2e6125f81c..6f86db5b99 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadTable.kt @@ -1405,48 +1405,43 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa } } - val record: MessageRecord = try { + val record: MessageRecord? = try { messages.getConversationSnippet(threadId) } catch (e: NoSuchMessageException) { val scheduledMessage: MessageRecord? = messages.getScheduledMessagesInThread(threadId).lastOrNull() - if (scheduledMessage == null) { - Log.w(TAG, "Failed to get a conversation snippet for thread $threadId") - if (shouldDelete) { - deleteConversation(threadId) - } else if (isPinned) { - updateThread( - threadId = threadId, - meaningfulMessages = meaningfulMessages, - body = null, - attachment = null, - contentType = null, - extra = null, - date = 0, - status = 0, - deliveryReceiptCount = 0, - type = 0, - unarchive = unarchive, - expiresIn = 0, - readReceiptCount = 0 - ) - } - return@withinTransaction true - } else { + if (scheduledMessage != null) { Log.i(TAG, "Using scheduled message for conversation snippet") - scheduledMessage } + scheduledMessage } - val drafts: DraftTable.Drafts = SignalDatabase.drafts.getDrafts(threadId) - if (drafts.isNotEmpty()) { - val threadRecord: ThreadRecord? = getThreadRecord(threadId) - if (threadRecord != null && - threadRecord.type == MessageTypes.BASE_DRAFT_TYPE && - threadRecord.date > record.timestamp - ) { - return@withinTransaction false + if (record == null) { + Log.w(TAG, "Failed to get a conversation snippet for thread $threadId") + if (shouldDelete) { + deleteConversation(threadId) + } else if (isPinned) { + updateThread( + threadId = threadId, + meaningfulMessages = meaningfulMessages, + body = null, + attachment = null, + contentType = null, + extra = null, + date = 0, + status = 0, + deliveryReceiptCount = 0, + type = 0, + unarchive = unarchive, + expiresIn = 0, + readReceiptCount = 0 + ) } + return@withinTransaction true + } + + if (hasMoreRecentDraft(threadId, record.timestamp)) { + return@withinTransaction false } val threadBody: ThreadBody = ThreadBodyUtil.getFormattedBodyFor(context, record) @@ -1474,6 +1469,20 @@ class ThreadTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTa } } + private fun hasMoreRecentDraft(threadId: Long, timestamp: Long): Boolean { + val drafts: DraftTable.Drafts = SignalDatabase.drafts.getDrafts(threadId) + if (drafts.isNotEmpty()) { + val threadRecord: ThreadRecord? = getThreadRecord(threadId) + if (threadRecord != null && + threadRecord.type == MessageTypes.BASE_DRAFT_TYPE && + threadRecord.date > timestamp + ) { + return true + } + } + return false + } + fun updateSnippetTypeSilently(threadId: Long) { if (threadId == -1L) { return