mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
3.2 KiB
Swift
73 lines
3.2 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
import GRDB
|
|
import SignalCoreKit
|
|
import SessionUtilitiesKit
|
|
|
|
public enum GetSnodePoolJob: JobExecutor {
|
|
public static let maxFailureCount: Int = -1
|
|
public static let requiresThreadId: Bool = false
|
|
public static let requiresInteractionId: Bool = false
|
|
|
|
public static func run(
|
|
_ job: Job,
|
|
queue: DispatchQueue,
|
|
success: @escaping (Job, Bool, Dependencies) -> (),
|
|
failure: @escaping (Job, Error?, Bool, Dependencies) -> (),
|
|
deferred: @escaping (Job, Dependencies) -> (),
|
|
using dependencies: Dependencies
|
|
) {
|
|
// If we already have cached Snodes then we still want to trigger the 'SnodeAPI.getSnodePool'
|
|
// but we want to succeed this job immediately (since it's marked as blocking), this allows us
|
|
// to block if we have no Snode pool and prevent other jobs from failing but avoids having to
|
|
// wait if we already have a potentially valid snode pool
|
|
guard !SnodeAPI.hasCachedSnodesIncludingExpired() else {
|
|
SNLog("[GetSnodePoolJob] Has valid cached pool, running async instead")
|
|
SnodeAPI
|
|
.getSnodePool()
|
|
.subscribe(on: DispatchQueue.global(qos: .default))
|
|
.sinkUntilComplete()
|
|
return success(job, false, dependencies)
|
|
}
|
|
|
|
// If we don't have a user yet then generate a dummy secret key to populate the snode
|
|
// pool from the seed node (so we can do so before account creation on first launch)
|
|
let ed25519SecretKey: [UInt8] = (Identity.fetchUserEd25519KeyPair()?.secretKey ??
|
|
(try! Identity.generate(from: try! Randomness.generateRandomBytes(numberBytes: 16))).ed25519KeyPair.secretKey
|
|
)
|
|
|
|
// If we don't have the snode pool cached then we should also try to build the path (this will
|
|
// speed up the onboarding process for new users because it can run before the user is created)
|
|
SnodeAPI.getSnodePool(ed25519SecretKey: ed25519SecretKey, using: dependencies)
|
|
.flatMap { _ in BuildPathsJob.runIfNeeded(ed25519SecretKey: ed25519SecretKey, using: dependencies) }
|
|
.subscribe(on: queue)
|
|
.receive(on: queue)
|
|
.sinkUntilComplete(
|
|
receiveCompletion: { result in
|
|
switch result {
|
|
case .finished:
|
|
SNLog("[GetSnodePoolJob] Completed")
|
|
success(job, false, dependencies)
|
|
|
|
case .failure(let error):
|
|
SNLog("[GetSnodePoolJob] Failed due to error: \(error)")
|
|
failure(job, error, false, dependencies)
|
|
}
|
|
}
|
|
)
|
|
}
|
|
|
|
public static func run(using dependencies: Dependencies = Dependencies()) {
|
|
GetSnodePoolJob.run(
|
|
Job(variant: .getSnodePool),
|
|
queue: .global(qos: .background),
|
|
success: { _, _, _ in },
|
|
failure: { _, _, _, _ in },
|
|
deferred: { _, _ in },
|
|
using: dependencies
|
|
)
|
|
}
|
|
}
|