pull/27/head
Niels Andriesse 6 years ago
parent 4d2441829b
commit feb1408836

@ -8,8 +8,6 @@ public extension LokiAPI {
private static var usedSnodes = [LokiAPITarget]() private static var usedSnodes = [LokiAPITarget]()
private static var cancels = [Callback]() private static var cancels = [Callback]()
private static let hexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
/// Start long polling. /// Start long polling.
/// This will send a notification if new messages were received /// This will send a notification if new messages were received
@objc public static func startLongPollingIfNecessary() { @objc public static func startLongPollingIfNecessary() {
@ -17,7 +15,7 @@ public extension LokiAPI {
isLongPolling = true isLongPolling = true
shouldStopPolling = false shouldStopPolling = false
Logger.info("[Loki] Started long polling") Logger.info("[Loki] Started long polling.")
longPoll() longPoll()
} }
@ -29,7 +27,7 @@ public extension LokiAPI {
usedSnodes.removeAll() usedSnodes.removeAll()
cancelAllPromises() cancelAllPromises()
Logger.info("[Loki] Stopped long polling") Logger.info("[Loki] Stopped long polling.")
} }
/// The long polling loop /// The long polling loop
@ -37,7 +35,7 @@ public extension LokiAPI {
// This is here so we can stop the infinite loop // This is here so we can stop the infinite loop
guard !shouldStopPolling else { return } guard !shouldStopPolling else { return }
getSwarm(for: hexEncodedPublicKey).then { _ -> Guarantee<[Result<Void>]> in getSwarm(for: userPublicKey).then { _ -> Guarantee<[Result<Void>]> in
var promises = [Promise<Void>]() var promises = [Promise<Void>]()
let connections = 3 let connections = 3
for i in 0..<connections { for i in 0..<connections {
@ -61,7 +59,7 @@ public extension LokiAPI {
} }
private static func getUnusedSnodes() -> [LokiAPITarget] { private static func getUnusedSnodes() -> [LokiAPITarget] {
let snodes = LokiAPI.swarmCache[hexEncodedPublicKey] ?? [] let snodes = LokiAPI.swarmCache[userPublicKey] ?? []
return snodes.filter { !usedSnodes.contains($0) } return snodes.filter { !usedSnodes.contains($0) }
} }
@ -107,7 +105,7 @@ public extension LokiAPI {
// Connect to the next snode if we haven't cancelled // Connect to the next snode if we haven't cancelled
// We also need to remove the cached snode so we don't contact it again // We also need to remove the cached snode so we don't contact it again
dropIfNeeded(nextSnode, hexEncodedPublicKey: hexEncodedPublicKey) dropIfNeeded(nextSnode, hexEncodedPublicKey: userPublicKey)
return connectToNextSnode() return connectToNextSnode()
} }
} }

@ -4,7 +4,7 @@ import PromiseKit
public final class LokiAPI : NSObject { public final class LokiAPI : NSObject {
internal static let storage = OWSPrimaryStorage.shared() internal static let storage = OWSPrimaryStorage.shared()
private static var userPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey } internal static var userPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey }
// MARK: Settings // MARK: Settings
private static let version = "v1" private static let version = "v1"

Loading…
Cancel
Save