From 94e7d2fba194a03096b7ac2759108477cedf602b Mon Sep 17 00:00:00 2001 From: Max Radermacher Date: Mon, 14 Aug 2023 14:29:54 -0500 Subject: [PATCH] [ServiceId] Update InteractionFinder --- .../StoryGroupReplyLoader.swift | 7 +++--- .../src/Messages/OWSMessageManager.swift | 2 +- .../src/Messages/Stories/StoryFinder.swift | 2 +- .../src/Messages/Stories/StoryMessage.swift | 6 ++--- .../Database/Records/InteractionFinder.swift | 23 ++++++++++--------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Signal/src/ViewControllers/HomeView/Stories/Replies & Views Sheets/Group Reply Sheet/StoryGroupReplyLoader.swift b/Signal/src/ViewControllers/HomeView/Stories/Replies & Views Sheets/Group Reply Sheet/StoryGroupReplyLoader.swift index 12738305fd..422e09b146 100644 --- a/Signal/src/ViewControllers/HomeView/Stories/Replies & Views Sheets/Group Reply Sheet/StoryGroupReplyLoader.swift +++ b/Signal/src/ViewControllers/HomeView/Stories/Replies & Views Sheets/Group Reply Sheet/StoryGroupReplyLoader.swift @@ -3,6 +3,7 @@ // SPDX-License-Identifier: AGPL-3.0-only // +import LibSignalClient import SignalCoreKit import SignalMessaging import SignalUI @@ -51,7 +52,7 @@ class StoryGroupReplyLoader: Dependencies { self.storyMessage = storyMessage self.tableView = tableView self.messageBatchFetcher = StoryGroupReplyBatchFetcher( - storyAuthor: UntypedServiceId(storyMessage.authorUuid), + storyAuthor: Aci(fromUUID: storyMessage.authorUuid), storyTimestamp: storyMessage.timestamp ) self.messageLoader = MessageLoader( @@ -324,12 +325,12 @@ extension StoryGroupReplyLoader: DatabaseChangeDelegate { // MARK: - Batch Fetcher private class StoryGroupReplyBatchFetcher: MessageLoaderBatchFetcher { - private let storyAuthor: UntypedServiceId + private let storyAuthor: Aci private let storyTimestamp: UInt64 private(set) var uniqueIdsAndRowIds = [(uniqueId: String, rowId: Int64)]() - init(storyAuthor: UntypedServiceId, storyTimestamp: UInt64) { + init(storyAuthor: Aci, storyTimestamp: UInt64) { self.storyAuthor = storyAuthor self.storyTimestamp = storyTimestamp } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.swift b/SignalServiceKit/src/Messages/OWSMessageManager.swift index 937c48341a..0eff45510c 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.swift +++ b/SignalServiceKit/src/Messages/OWSMessageManager.swift @@ -981,7 +981,7 @@ class MessageManagerRequest: NSObject { private static func isDuplicate(_ decryptedEnvelope: DecryptedIncomingEnvelope, tx: SDSAnyReadTransaction) -> Bool { return InteractionFinder.existsIncomingMessage( timestamp: decryptedEnvelope.timestamp, - sourceServiceId: decryptedEnvelope.sourceAci.untypedServiceId, + sourceAci: decryptedEnvelope.sourceAci, sourceDeviceId: decryptedEnvelope.sourceDeviceId, transaction: tx ) diff --git a/SignalServiceKit/src/Messages/Stories/StoryFinder.swift b/SignalServiceKit/src/Messages/Stories/StoryFinder.swift index c007314b9c..79eace1e94 100644 --- a/SignalServiceKit/src/Messages/Stories/StoryFinder.swift +++ b/SignalServiceKit/src/Messages/Stories/StoryFinder.swift @@ -270,7 +270,7 @@ public class StoryFinder: NSObject { let sql = """ SELECT \(StoryMessage.columnName(.timestamp)) FROM \(StoryMessage.databaseTableName) - WHERE \(StoryMessage.columnName(.authorUuid)) != '\(StoryMessage.systemStoryAuthorUUID.uuidString)' + WHERE \(StoryMessage.columnName(.authorUuid)) != '\(StoryMessage.systemStoryAuthor.serviceIdUppercaseString)' ORDER BY \(StoryMessage.columnName(.timestamp)) ASC LIMIT 1 """ diff --git a/SignalServiceKit/src/Messages/Stories/StoryMessage.swift b/SignalServiceKit/src/Messages/Stories/StoryMessage.swift index 28afe43e79..628026cf98 100644 --- a/SignalServiceKit/src/Messages/Stories/StoryMessage.swift +++ b/SignalServiceKit/src/Messages/Stories/StoryMessage.swift @@ -347,7 +347,7 @@ public final class StoryMessage: NSObject, SDSCodableModel, Decodable { // The "Signal account" used for e.g. the onboarding story has a fixed UUID // we can use to prevent trying to actually reply, send a message, etc. - public static let systemStoryAuthorUUID = UUID(uuidString: "00000000-0000-0000-0000-000000000001")! + public static let systemStoryAuthor = Aci(fromUUID: UUID(uuidString: "00000000-0000-0000-0000-000000000001")!) @discardableResult public static func createFromSystemAuthor( @@ -378,7 +378,7 @@ public final class StoryMessage: NSObject, SDSCodableModel, Decodable { // sophisticated for future stories this is where we'd change it, maybe make this // a null timestamp and interpret that different when we read it back out. timestamp: timestamp, - authorUuid: Self.systemStoryAuthorUUID, + authorUuid: Self.systemStoryAuthor.temporary_rawUUID, groupId: nil, manifest: manifest, attachment: attachment, @@ -949,7 +949,7 @@ extension OWSOutgoingMessageRecipientState: Codable {} extension SignalServiceAddress { public var isSystemStoryAddress: Bool { - return self.uuid == StoryMessage.systemStoryAuthorUUID + return self.serviceId == StoryMessage.systemStoryAuthor } } diff --git a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift index 9feee1f76b..8b01bef299 100644 --- a/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift +++ b/SignalServiceKit/src/Storage/Database/Records/InteractionFinder.swift @@ -5,6 +5,7 @@ import Foundation import GRDB +import LibSignalClient import SignalCoreKit public enum EditMessageQueryMode { @@ -22,7 +23,7 @@ protocol InteractionFinderAdapter { static func fetch(rowId: Int64, transaction: ReadTransaction) throws -> TSInteraction? - static func existsIncomingMessage(timestamp: UInt64, sourceServiceId: UntypedServiceId, sourceDeviceId: UInt32, transaction: ReadTransaction) -> Bool + static func existsIncomingMessage(timestamp: UInt64, sourceAci: Aci, sourceDeviceId: UInt32, transaction: ReadTransaction) -> Bool static func interactions(withTimestamp timestamp: UInt64, filter: @escaping (TSInteraction) -> Bool, transaction: ReadTransaction) throws -> [TSInteraction] @@ -43,7 +44,7 @@ protocol InteractionFinderAdapter { static func enumerateGroupReplies(for storyMessage: StoryMessage, transaction: ReadTransaction, block: @escaping (TSMessage, UnsafeMutablePointer) -> Void) static func hasLocalUserReplied(storyTimestamp: UInt64, storyAuthorUuidString: String, transaction: ReadTransaction) -> Bool - static func groupReplyUniqueIdsAndRowIds(storyAuthor: UntypedServiceId, storyTimestamp: UInt64, transaction: ReadTransaction) -> [(String, Int64)] + static func groupReplyUniqueIdsAndRowIds(storyAuthor: Aci, storyTimestamp: UInt64, transaction: ReadTransaction) -> [(String, Int64)] // MARK: - instance methods @@ -119,10 +120,10 @@ public class InteractionFinder: NSObject, InteractionFinderAdapter { } } - public class func existsIncomingMessage(timestamp: UInt64, sourceServiceId: UntypedServiceId, sourceDeviceId: UInt32, transaction: SDSAnyReadTransaction) -> Bool { + public class func existsIncomingMessage(timestamp: UInt64, sourceAci: Aci, sourceDeviceId: UInt32, transaction: SDSAnyReadTransaction) -> Bool { switch transaction.readTransaction { case .grdbRead(let grdbRead): - return GRDBInteractionFinder.existsIncomingMessage(timestamp: timestamp, sourceServiceId: sourceServiceId, sourceDeviceId: sourceDeviceId, transaction: grdbRead) + return GRDBInteractionFinder.existsIncomingMessage(timestamp: timestamp, sourceAci: sourceAci, sourceDeviceId: sourceDeviceId, transaction: grdbRead) } } @@ -249,7 +250,7 @@ public class InteractionFinder: NSObject, InteractionFinderAdapter { } public static func groupReplyUniqueIdsAndRowIds( - storyAuthor: UntypedServiceId, + storyAuthor: Aci, storyTimestamp: UInt64, transaction: SDSAnyReadTransaction ) -> [(String, Int64)] { @@ -691,7 +692,7 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { ) } - static func existsIncomingMessage(timestamp: UInt64, sourceServiceId: UntypedServiceId, sourceDeviceId: UInt32, transaction: GRDBReadTransaction) -> Bool { + static func existsIncomingMessage(timestamp: UInt64, sourceAci: Aci, sourceDeviceId: UInt32, transaction: GRDBReadTransaction) -> Bool { let sql = """ SELECT EXISTS( SELECT 1 @@ -706,8 +707,8 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { """ let arguments: StatementArguments = [ timestamp, - sourceServiceId.uuidValue.uuidString, - SignalServiceAddress(sourceServiceId).phoneNumber, + sourceAci.serviceIdUppercaseString, + SignalServiceAddress(sourceAci).phoneNumber, sourceDeviceId ] do { @@ -1002,11 +1003,11 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { } static func groupReplyUniqueIdsAndRowIds( - storyAuthor: UntypedServiceId, + storyAuthor: Aci, storyTimestamp: UInt64, transaction: GRDBReadTransaction ) -> [(String, Int64)] { - guard storyAuthor.uuidValue != StoryMessage.systemStoryAuthorUUID else { + guard storyAuthor != StoryMessage.systemStoryAuthor else { // No replies on system stories. return [] } @@ -1022,7 +1023,7 @@ public class GRDBInteractionFinder: NSObject, InteractionFinderAdapter { return try Row.fetchAll( transaction.database, sql: sql, - arguments: [storyTimestamp, storyAuthor.uuidValue.uuidString] + arguments: [storyTimestamp, storyAuthor.serviceIdUppercaseString] ).map { ($0[0], $0[1]) } } catch { owsFail("error: \(error)")