session-ios/Signal/src/Models/AccountManager.swift

146 lines
5.6 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
import SignalServiceKit
/**
* 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 networkManager: TSNetworkManager {
return SSKEnvironment.shared.networkManager
}
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-01-08 21:10:32 +01: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
}
}
2019-01-08 21:10:32 +01:00
}.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> {
2019-03-30 14:22:31 +01:00
return Promise<Any> { 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,
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
func updatePushTokens(pushToken: String, voipToken: String) -> Promise<Void> {
2019-03-30 14:22:31 +01:00
return Promise<Any> { resolver in
2018-10-02 21:29:18 +02:00
tsAccountManager.registerForPushNotifications(pushToken: pushToken,
2018-10-03 15:09:24 +02:00
voipToken: voipToken,
2018-10-13 21:21:46 +02:00
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()
}
// MARK: Turn Server
func getTurnServerInfo() -> Promise<TurnServerInfo> {
2018-10-13 21:21:46 +02:00
return Promise { resolver in
2018-03-01 23:47:31 +01:00
self.networkManager.makeRequest(OWSRequestFactory.turnServerInfoRequest(),
success: { (_: URLSessionDataTask, responseObject: Any?) in
guard responseObject != nil else {
2018-10-13 21:21:46 +02:00
return resolver.reject(OWSErrorMakeUnableToProcessServerResponseError())
}
if let responseDictionary = responseObject as? [String: AnyObject] {
2018-03-01 20:42:54 +01:00
if let turnServerInfo = TurnServerInfo(attributes: responseDictionary) {
2018-10-13 21:21:46 +02:00
return resolver.fulfill(turnServerInfo)
}
2018-08-23 16:37:34 +02:00
Logger.error("unexpected server response:\(responseDictionary)")
}
2018-10-13 21:21:46 +02:00
return resolver.reject(OWSErrorMakeUnableToProcessServerResponseError())
},
failure: { (_: URLSessionDataTask, error: Error) in
2018-10-13 21:21:46 +02:00
return resolver.reject(error)
})
}
}
}