Split into LokiDeviceLinkingSession & LokiAPI+MultiDeviceAPI

This commit is contained in:
Niels Andriesse 2019-09-19 16:05:27 +10:00
parent 143755ae8e
commit 34eca6c820
5 changed files with 70 additions and 56 deletions

View File

@ -0,0 +1,19 @@
import PromiseKit
public extension LokiAPI {
public static func addSlaveAccount(with hexEncodedPublicKey: String) -> Promise<Void> {
// Adds the given slave account to the user's device mapping on the server
notImplemented()
}
public static func removeSlaveAccount(with hexEncodedPublicKey: String) -> Promise<Void> {
// 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()
}
}

View File

@ -119,9 +119,10 @@ public final class LokiAPI : NSObject {
public static func sendSignalMessage(_ signalMessage: SignalMessage, onP2PSuccess: @escaping () -> Void) -> Promise<Set<RawResponsePromise>> {
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

View File

@ -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<String> {
// Adds the given slave account to the user's device mapping on the server
notImplemented()
}
public static func removeSlaveAccount(with hexEncodedPublicKey: String) -> Promise<String> {
// 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
//})

View File

@ -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
}
}

View File

@ -0,0 +1,6 @@
public protocol LokiDeviceLinkingSessionDelegate {
func handleDeviceLinkingRequestReceived(with signature: String)
func handleDeviceLinkingSessionTimeout()
}