From 83ee5636b4031bfcbf37c890bfe47cc4db625b03 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 5 Jan 2021 17:50:15 -0300 Subject: [PATCH] Surface group updates exception group migrations in the inbox snippet. --- .../Database/Records/InteractionFinder.swift | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift index 93c5d21b86..a654b334ff 100644 --- a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift +++ b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -1074,23 +1074,56 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { // MARK: - instance methods func mostRecentInteractionForInbox(transaction: GRDBReadTransaction) -> TSInteraction? { - let sql = """ + let interactionsSql = """ SELECT * FROM \(InteractionRecord.databaseTableName) WHERE \(interactionColumn: .threadUniqueId) = ? AND \(interactionColumn: .errorType) IS NOT ? AND \(interactionColumn: .messageType) IS NOT ? AND \(interactionColumn: .messageType) IS NOT ? - AND \(interactionColumn: .messageType) IS NOT ? ORDER BY \(interactionColumn: .id) DESC + """ + let firstInteractionSql = interactionsSql + """ LIMIT 1 """ let arguments: StatementArguments = [threadUniqueId, TSErrorMessageType.nonBlockingIdentityChange.rawValue, TSInfoMessageType.verificationStateChange.rawValue, - TSInfoMessageType.profileUpdate.rawValue, - TSInfoMessageType.typeGroupUpdate.rawValue] - return TSInteraction.grdbFetchOne(sql: sql, arguments: arguments, transaction: transaction) + TSInfoMessageType.profileUpdate.rawValue] + guard let firstInteraction = TSInteraction.grdbFetchOne(sql: firstInteractionSql, + arguments: arguments, + transaction: transaction) else { + return nil + } + func filterForInbox(_ interaction: TSInteraction) -> Bool { + guard let message = interaction as? TSMessage else { + return true + } + return !message.isGroupMigrationMessage + } + // We can't exclude group migration messages in the query. + // In the rare case that the most recent message is a + // group migration message, we iterate backward until we + // find an interaction that isn't a group migration. + // We should never have to iterate more than twice, + // since groups can only be migrated once. + if filterForInbox(firstInteraction) { + return firstInteraction + } + do { + let cursor = TSInteraction.grdbFetchCursor(sql: interactionsSql, + arguments: arguments, + transaction: transaction) + while let interaction = try cursor.next() { + if filterForInbox(interaction) { + return interaction + } + } + return nil + } catch { + owsFailDebug("Error: \(error)") + return nil + } } func earliestKnownInteractionRowId(transaction: GRDBReadTransaction) -> Int? {