diff --git a/SignalServiceKit/src/Loki/API/LokiAPI+MultiDeviceAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI+MultiDeviceAPI.swift new file mode 100644 index 000000000..4027c3cc6 --- /dev/null +++ b/SignalServiceKit/src/Loki/API/LokiAPI+MultiDeviceAPI.swift @@ -0,0 +1,19 @@ +import PromiseKit + +public extension LokiAPI { + + public static func addSlaveAccount(with hexEncodedPublicKey: String) -> Promise { + // Adds the given slave account 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 + notImplemented() + } + + public static func getOtherAccounts(for hexEncodedPublicKey: String) -> Promise<[String]> { + // Gets the accounts associated with the given hex encoded public key from the server + notImplemented() + } +} diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index f7efab501..0c8d2673b 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -119,9 +119,10 @@ 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 - LokiDeviceLinkingAPI.getOtherAccounts(for: signalMessage.recipientID).done { hexEncodedPublicKeyList in + getOtherAccounts(for: signalMessage.recipientID).done { hexEncodedPublicKeyList in hexEncodedPublicKeyList.forEach { hexEncodedPublicKey in - internalSendSignalMessage(signalMessage.copy(with: hexEncodedPublicKey)) { } + let signalMessageCopy = signalMessage.copy(with: hexEncodedPublicKey) + internalSendSignalMessage(signalMessageCopy) { } } } return result diff --git a/SignalServiceKit/src/Loki/API/LokiDeviceLinkingAPI.swift b/SignalServiceKit/src/Loki/API/LokiDeviceLinkingAPI.swift deleted file mode 100644 index a46aa7c95..000000000 --- a/SignalServiceKit/src/Loki/API/LokiDeviceLinkingAPI.swift +++ /dev/null @@ -1,54 +0,0 @@ -import PromiseKit - -@objc(LKDeviceLinkingAPI) -final class LokiDeviceLinkingAPI : NSObject { - - private static var timerStartDate: Date? - public static var isListeningForLinkingRequests = false - - // MARK: Settings - private static let listeningTimeout: TimeInterval = 60 - - // MARK: Lifecycle - override private init() { } - - // MARK: Public API - @objc public static func startListeningForLinkingRequests(onLinkingRequestReceived: (String) -> Void, onTimeout: @escaping () -> Void) { - isListeningForLinkingRequests = true - timerStartDate = Date() - Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in - if Date().timeIntervalSince1970 - timerStartDate!.timeIntervalSince1970 >= listeningTimeout { - isListeningForLinkingRequests = false - onTimeout() - } - } - } - - @objc public static func authorizeLinkingRequest(with signature: String) { - // Authorize the linking request with the given signature - } - - public static func addSlaveAccount(with hexEncodedPublicKey: String) -> Promise { - // Adds the given slave account 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 - notImplemented() - } - - public static func getOtherAccounts(for hexEncodedPublicKey: String) -> Promise<[String]> { - // Gets the accounts associated with the given hex encoded public key from the server - notImplemented() - } -} - -//LokiDeviceLinkingAPI.startListeningForLinkingRequests(onLinkingRequestReceived: { signature in -// // 1. Validate the signature -// // 2. Ask the user to accept -// // 2.1. If the user declined, we're done -// // 2.2, If the user accepted: LokiDeviceLinkingAPI.authorizeLinkingRequest(with: signature) -//}, onTimeout: { -// // Notify the user -//}) diff --git a/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSession.swift b/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSession.swift new file mode 100644 index 000000000..de87b709c --- /dev/null +++ b/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSession.swift @@ -0,0 +1,42 @@ +import PromiseKit + +@objc(LKDeviceLinkingAPI) +final class LokiDeviceLinkingSession : NSObject { + private let delegate: LokiDeviceLinkingSessionDelegate + private var timer: Timer? + public var isListeningForLinkingRequests = false + + // MARK: Lifecycle + public init(delegate: LokiDeviceLinkingSessionDelegate) { + self.delegate = delegate + } + + // MARK: Settings + private let listeningTimeout: TimeInterval = 60 + + // MARK: Public API + public func startListeningForLinkingRequests() { + isListeningForLinkingRequests = true + timer = Timer.scheduledTimer(withTimeInterval: listeningTimeout, repeats: false) { [weak self] timer in + guard let self = self else { return } + self.stopListeningForLinkingRequests() + self.delegate.handleDeviceLinkingSessionTimeout() + } + } + + public func stopListeningForLinkingRequests() { + timer?.invalidate() + timer = nil + isListeningForLinkingRequests = false + } + + public func processLinkingRequest(with signature: String) { + guard isListeningForLinkingRequests else { return } + stopListeningForLinkingRequests() + delegate.handleDeviceLinkingRequestReceived(with: signature) + } + + public func authorizeLinkingRequest(with signature: String) { + // TODO: Authorize the linking request with the given signature + } +} diff --git a/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSessionDelegate.swift b/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSessionDelegate.swift new file mode 100644 index 000000000..4d5b229a4 --- /dev/null +++ b/SignalServiceKit/src/Loki/API/LokiDeviceLinkingSessionDelegate.swift @@ -0,0 +1,6 @@ + +public protocol LokiDeviceLinkingSessionDelegate { + + func handleDeviceLinkingRequestReceived(with signature: String) + func handleDeviceLinkingSessionTimeout() +}