Improve incoming group receipt processing.

This commit is contained in:
Cody Henthorne
2026-03-06 10:37:01 -05:00
committed by jeffrey-signal
parent da2eb02cde
commit bb730c137f
3 changed files with 60 additions and 43 deletions

View File

@@ -5002,8 +5002,8 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
.run()
}
fun incrementDeliveryReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long, stopwatch: Stopwatch? = null): Set<Long> {
return incrementReceiptCounts(targetTimestamps, receiptAuthor, receiptSentTimestamp, ReceiptType.DELIVERY, stopwatch = stopwatch)
fun incrementDeliveryReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long, stopwatch: Stopwatch? = null, receiptDataCache: MutableMap<Long, ReceiptData>? = null): Set<Long> {
return incrementReceiptCounts(targetTimestamps, receiptAuthor, receiptSentTimestamp, ReceiptType.DELIVERY, stopwatch = stopwatch, receiptDataCache = receiptDataCache)
}
fun incrementDeliveryReceiptCount(targetTimestamps: Long, receiptAuthor: RecipientId, receiptSentTimestamp: Long): Boolean {
@@ -5013,8 +5013,8 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
/**
* @return A list of ID's that were not updated.
*/
fun incrementReadReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long): Set<Long> {
return incrementReceiptCounts(targetTimestamps, receiptAuthor, receiptSentTimestamp, ReceiptType.READ)
fun incrementReadReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long, receiptDataCache: MutableMap<Long, ReceiptData>? = null): Set<Long> {
return incrementReceiptCounts(targetTimestamps, receiptAuthor, receiptSentTimestamp, ReceiptType.READ, receiptDataCache = receiptDataCache)
}
fun incrementReadReceiptCount(targetTimestamps: Long, receiptAuthor: RecipientId, receiptSentTimestamp: Long): Boolean {
@@ -5092,13 +5092,13 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
*
* @return All of the target timestamps that couldn't be found in the table.
*/
private fun incrementReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long, receiptType: ReceiptType, messageQualifier: MessageQualifier = MessageQualifier.ALL, stopwatch: Stopwatch? = null): Set<Long> {
private fun incrementReceiptCounts(targetTimestamps: List<Long>, receiptAuthor: RecipientId, receiptSentTimestamp: Long, receiptType: ReceiptType, messageQualifier: MessageQualifier = MessageQualifier.ALL, stopwatch: Stopwatch? = null, receiptDataCache: MutableMap<Long, ReceiptData>? = null): Set<Long> {
val messageUpdates: MutableSet<MessageReceiptUpdate> = HashSet()
val missingTargetTimestamps: MutableSet<Long> = HashSet()
writableDatabase.withinTransaction {
for (targetTimestamp in targetTimestamps) {
val updates: Set<MessageReceiptUpdate> = incrementReceiptCountInternal(targetTimestamp, receiptAuthor, receiptSentTimestamp, receiptType, messageQualifier, stopwatch)
val updates: Set<MessageReceiptUpdate> = incrementReceiptCountInternal(targetTimestamp, receiptAuthor, receiptSentTimestamp, receiptType, messageQualifier, stopwatch, receiptDataCache)
if (updates.isNotEmpty()) {
messageUpdates += updates
} else {
@@ -5131,7 +5131,7 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
return missingTargetTimestamps
}
private fun incrementReceiptCountInternal(targetTimestamp: Long, receiptAuthor: RecipientId, receiptSentTimestamp: Long, receiptType: ReceiptType, messageQualifier: MessageQualifier, stopwatch: Stopwatch? = null): Set<MessageReceiptUpdate> {
private fun incrementReceiptCountInternal(targetTimestamp: Long, receiptAuthor: RecipientId, receiptSentTimestamp: Long, receiptType: ReceiptType, messageQualifier: MessageQualifier, stopwatch: Stopwatch? = null, receiptDataCache: MutableMap<Long, ReceiptData>? = null): Set<MessageReceiptUpdate> {
val qualifierWhere: String = when (messageQualifier) {
MessageQualifier.NORMAL -> " AND NOT ($IS_STORY_CLAUSE)"
MessageQualifier.STORY -> " AND $IS_STORY_CLAUSE"
@@ -5139,39 +5139,45 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
}
// Note: While it is true that multiple messages can have the same (sent, author) pair, this should only happen for stories, which are handled below.
val receiptData: ReceiptData? = readableDatabase
.select(ID, THREAD_ID, STORY_TYPE, receiptType.columnName, TO_RECIPIENT_ID)
.from(TABLE_NAME)
.where(
"""
$DATE_SENT = $targetTimestamp AND
$FROM_RECIPIENT_ID = ? AND
(
$TO_RECIPIENT_ID = ? OR
EXISTS (
SELECT 1
FROM ${RecipientTable.TABLE_NAME}
WHERE
${RecipientTable.TABLE_NAME}.${RecipientTable.ID} = $TO_RECIPIENT_ID AND
${RecipientTable.TABLE_NAME}.${RecipientTable.TYPE} != ${RecipientTable.RecipientType.INDIVIDUAL.id}
val receiptData: ReceiptData? = receiptDataCache?.get(targetTimestamp)
?: readableDatabase
.select(ID, THREAD_ID, STORY_TYPE, receiptType.columnName, TO_RECIPIENT_ID)
.from(TABLE_NAME)
.where(
"""
$DATE_SENT = $targetTimestamp AND
$FROM_RECIPIENT_ID = ? AND
(
$TO_RECIPIENT_ID = ? OR
EXISTS (
SELECT 1
FROM ${RecipientTable.TABLE_NAME}
WHERE
${RecipientTable.TABLE_NAME}.${RecipientTable.ID} = $TO_RECIPIENT_ID AND
${RecipientTable.TABLE_NAME}.${RecipientTable.TYPE} != ${RecipientTable.RecipientType.INDIVIDUAL.id}
)
)
$qualifierWhere
""",
Recipient.self().id,
receiptAuthor
)
$qualifierWhere
""",
Recipient.self().id,
receiptAuthor
)
.limit(1)
.run()
.readToSingleObject { cursor ->
ReceiptData(
messageId = cursor.requireLong(ID),
threadId = cursor.requireLong(THREAD_ID),
storyType = StoryType.fromCode(cursor.requireInt(STORY_TYPE)),
marked = cursor.requireBoolean(receiptType.columnName),
forIndividualChat = cursor.requireLong(TO_RECIPIENT_ID) == receiptAuthor.toLong()
)
}
.limit(1)
.run()
.readToSingleObject { cursor ->
ReceiptData(
messageId = cursor.requireLong(ID),
threadId = cursor.requireLong(THREAD_ID),
storyType = StoryType.fromCode(cursor.requireInt(STORY_TYPE)),
marked = cursor.requireBoolean(receiptType.columnName),
forIndividualChat = cursor.requireLong(TO_RECIPIENT_ID) == receiptAuthor.toLong()
)
}
.also { result ->
if (result != null && !result.forIndividualChat) {
receiptDataCache?.put(targetTimestamp, result)
}
}
stopwatch?.split("receipt-query")
@@ -5191,11 +5197,15 @@ open class MessageTable(context: Context?, databaseHelper: SignalDatabase) : Dat
UPDATE $TABLE_NAME
SET
${receiptType.columnName} = 1,
$RECEIPT_TIMESTAMP = MAX($RECEIPT_TIMESTAMP, $receiptSentTimestamp)
$RECEIPT_TIMESTAMP = MAX($RECEIPT_TIMESTAMP, $receiptSentTimestamp)
WHERE
$ID = ${receiptData.messageId}
"""
)
if (receiptDataCache?.containsKey(targetTimestamp) == true) {
receiptDataCache[targetTimestamp] = receiptData.copy(marked = true)
}
}
stopwatch?.split("receipt-update")