session-ios/Session/Signal/AccountManager.swift

119 lines
4.2 KiB
Swift
Raw Normal View History

2017-01-12 21:37:37 +01:00
//
2019-01-08 21:10:32 +01:00
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
2017-01-12 21:37:37 +01:00
//
import Foundation
import PromiseKit
2020-11-11 07:45:50 +01:00
import SignalUtilitiesKit
/**
* Signal is actually two services - textSecure for messages and red phone (for calls).
* AccountManager delegates to both.
*/
2018-05-25 21:15:19 +02:00
@objc
public class AccountManager: NSObject {
2019-01-08 21:10:32 +01:00
// MARK: - Dependencies
var profileManager: OWSProfileManager {
return OWSProfileManager.shared()
}
private var preferences: OWSPreferences {
return Environment.shared.preferences
}
2018-10-02 21:29:18 +02:00
private var tsAccountManager: TSAccountManager {
return TSAccountManager.sharedInstance()
}
// MARK: -
@objc
public override init() {
super.init()
SwiftSingletons.register(self)
}
// MARK: registration
2018-11-29 17:18:47 +01:00
@objc func registerObjc(verificationCode: String,
pin: String?) -> AnyPromise {
return AnyPromise(register(verificationCode: verificationCode, pin: pin))
}
2018-03-01 20:42:54 +01:00
func register(verificationCode: String,
pin: String?) -> Promise<Void> {
guard verificationCode.count > 0 else {
let error = OWSErrorWithCodeDescription(.userError,
NSLocalizedString("REGISTRATION_ERROR_BLANK_VERIFICATION_CODE",
comment: "alert body during registration"))
return Promise(error: error)
}
2018-08-23 16:37:34 +02:00
Logger.debug("registering with signal server")
let registrationPromise: Promise<Void> = firstly {
2018-06-01 20:20:48 +02:00
return self.registerForTextSecure(verificationCode: verificationCode, pin: pin)
2019-05-08 07:51:08 +02:00
}.then { _ -> Promise<Void> in
return self.syncPushTokens().recover { (error) -> Promise<Void> in
switch error {
case PushRegistrationError.pushNotSupported(let description):
// This can happen with:
// - simulators, none of which support receiving push notifications
// - on iOS11 devices which have disabled "Allow Notifications" and disabled "Enable Background Refresh" in the system settings.
Logger.info("Recovered push registration error. Registering for manual message fetcher because push not supported: \(description)")
return self.enableManualMessageFetching()
default:
throw error
}
}
}.done { (_) -> Void in
self.completeRegistration()
}
registrationPromise.retainUntilComplete()
return registrationPromise
}
2018-03-01 20:42:54 +01:00
private func registerForTextSecure(verificationCode: String,
pin: String?) -> Promise<Void> {
return Promise { resolver in
2018-10-02 21:29:18 +02:00
tsAccountManager.verifyAccount(withCode: verificationCode,
2019-01-08 21:10:32 +01:00
pin: pin,
success: { resolver.fulfill(()) },
2019-01-08 21:10:32 +01:00
failure: resolver.reject)
}
}
private func syncPushTokens() -> Promise<Void> {
2018-08-23 16:37:34 +02:00
Logger.info("")
let job = SyncPushTokensJob(accountManager: self, preferences: self.preferences)
job.uploadOnlyIfStale = false
return job.run()
}
private func completeRegistration() {
2018-08-23 16:37:34 +02:00
Logger.info("")
2018-10-02 21:29:18 +02:00
tsAccountManager.didRegister()
}
// MARK: Message Delivery
2020-04-17 05:55:24 +02:00
func updatePushTokens(pushToken: String, voipToken: String, isForcedUpdate: Bool) -> Promise<Void> {
return Promise { resolver in
2018-10-02 21:29:18 +02:00
tsAccountManager.registerForPushNotifications(pushToken: pushToken,
2018-10-03 15:09:24 +02:00
voipToken: voipToken,
2020-04-17 05:55:24 +02:00
isForcedUpdate: isForcedUpdate,
success: { resolver.fulfill(()) },
failure: resolver.reject)
}
}
2018-10-01 22:29:39 +02:00
func enableManualMessageFetching() -> Promise<Void> {
2018-10-13 21:21:46 +02:00
let anyPromise = tsAccountManager.setIsManualMessageFetchEnabled(true)
return Promise(anyPromise).asVoid()
}
}