From 7fe7245c643d76fc9f5afb0fd1836bbe1c65482b Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 20 Sep 2019 11:42:10 +1000 Subject: [PATCH] Refactor --- SignalServiceKit/src/Loki/API/LokiAPI.swift | 5 +++-- .../src/Loki/API/LokiStorageAPI.swift | 13 +++++++------ .../Loki/API/Multi Device/LokiDeviceLink.swift | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index 0c8d2673b..9d725fae6 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -119,8 +119,9 @@ public final class LokiAPI : NSObject { public static func sendSignalMessage(_ signalMessage: SignalMessage, onP2PSuccess: @escaping () -> Void) -> Promise> { let result = internalSendSignalMessage(signalMessage, onP2PSuccess: onP2PSuccess) // Use a best attempt approach for multi device for now - getOtherAccounts(for: signalMessage.recipientID).done { hexEncodedPublicKeyList in - hexEncodedPublicKeyList.forEach { hexEncodedPublicKey in + LokiStorageAPI.getDeviceLinks(associatedWith: signalMessage.recipientID).done { deviceLinks in + let associatedHexEncodedPublicKeys = Set(deviceLinks.flatMap { [ $0.master.hexEncodedPublicKey, $0.slave.hexEncodedPublicKey ] }).subtracting([ signalMessage.recipientID ]) + associatedHexEncodedPublicKeys.forEach { hexEncodedPublicKey in let signalMessageCopy = signalMessage.copy(with: hexEncodedPublicKey) internalSendSignalMessage(signalMessageCopy) { } } diff --git a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift index 69d7e04e4..6018248a7 100644 --- a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift @@ -7,18 +7,19 @@ public final class LokiStorageAPI : NSObject { override private init() { } // MARK: Public API - public static func addSlaveAccount(with hexEncodedPublicKey: String) -> Promise { - // Adds the given slave account to the user's device mapping on the server + public static func addDeviceLink(_ deviceLink: LokiDeviceLink) -> Promise { + // Adds the given device link to the user's device mapping on the server notImplemented() } - public static func removeSlaveAccount(with hexEncodedPublicKey: String) -> Promise { - // Removes the given slave account from the user's device mapping on the server + public static func removeDeviceLink(_ deviceLink: LokiDeviceLink) -> Promise { + // Removes the given device link from the user's device mapping on the server notImplemented() } - public static func getAssociatedAccounts(for hexEncodedPublicKey: String) -> Promise<[String]> { - // Gets the accounts associated with the given hex encoded public key from the server + public static func getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> Promise> { + // Gets the device links associated with the given hex encoded public key from the + // server and stores and returns the valid ones notImplemented() } } diff --git a/SignalServiceKit/src/Loki/API/Multi Device/LokiDeviceLink.swift b/SignalServiceKit/src/Loki/API/Multi Device/LokiDeviceLink.swift index 3050902c9..034036f7b 100644 --- a/SignalServiceKit/src/Loki/API/Multi Device/LokiDeviceLink.swift +++ b/SignalServiceKit/src/Loki/API/Multi Device/LokiDeviceLink.swift @@ -1,12 +1,12 @@ -public struct LokiDeviceLink { +public struct LokiDeviceLink : Hashable { public let master: Device public let slave: Device public var isAuthorized: Bool { return master.signature != nil } // MARK: Types - public struct Device { + public struct Device : Hashable { public let hexEncodedPublicKey: String public let signature: Data? @@ -14,6 +14,11 @@ public struct LokiDeviceLink { self.hexEncodedPublicKey = hexEncodedPublicKey self.signature = signature } + + public func hash(into hasher: inout Hasher) { + hexEncodedPublicKey.hash(into: &hasher) + signature?.hash(into: &hasher) + } } // MARK: Lifecycle @@ -21,4 +26,10 @@ public struct LokiDeviceLink { self.master = master self.slave = slave } + + // MARK: Hashing + public func hash(into hasher: inout Hasher) { + master.hash(into: &hasher) + slave.hash(into: &hasher) + } }