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
|
|
|
//
|
2016-10-10 22:02:09 +02:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import PromiseKit
|
2017-11-28 00:17:46 +01:00
|
|
|
import SignalServiceKit
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
|
2019-01-08 21:10:32 +01:00
|
|
|
// MARK: - Dependencies
|
|
|
|
|
|
|
|
var profileManager: OWSProfileManager {
|
|
|
|
return OWSProfileManager.shared()
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:08:11 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:18:00 +01:00
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public override init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
SwiftSingletons.register(self)
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
// 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))
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:42:54 +01:00
|
|
|
func register(verificationCode: String,
|
|
|
|
pin: String?) -> Promise<Void> {
|
2017-12-04 16:35:47 +01:00
|
|
|
guard verificationCode.count > 0 else {
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
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")
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
let registrationPromise: Promise<Void> = firstly {
|
2018-06-01 20:20:48 +02:00
|
|
|
return self.registerForTextSecure(verificationCode: verificationCode, pin: pin)
|
2019-05-06 02:30:24 +02:00
|
|
|
}
|
|
|
|
// Loki: Original code
|
|
|
|
// ========
|
|
|
|
// .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
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
self.completeRegistration()
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
|
2017-08-11 22:29:05 +02:00
|
|
|
registrationPromise.retainUntilComplete()
|
|
|
|
|
|
|
|
return registrationPromise
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:42:54 +01:00
|
|
|
private func registerForTextSecure(verificationCode: String,
|
|
|
|
pin: String?) -> Promise<Void> {
|
2019-03-30 14:44:47 +01:00
|
|
|
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,
|
2019-03-30 14:44:47 +01:00
|
|
|
success: { resolver.fulfill(()) },
|
2019-01-08 21:10:32 +01:00
|
|
|
failure: resolver.reject)
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
private func syncPushTokens() -> Promise<Void> {
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.info("")
|
2017-11-07 20:28:08 +01:00
|
|
|
let job = SyncPushTokensJob(accountManager: self, preferences: self.preferences)
|
|
|
|
job.uploadOnlyIfStale = false
|
|
|
|
return job.run()
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func completeRegistration() {
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.info("")
|
2018-10-02 21:29:18 +02:00
|
|
|
tsAccountManager.didRegister()
|
Rework push registration
== Account Registration ==
Not complete until push tokens are uploaded
== Remote Notifications Registration ==
Extracted from PushManager
- wait for notification-settings registration to complete before
requesting push tokens, otherwise it's possible token requests will
be ignored.
- Less state required for push notification callbacks, specifically, we
no longer need to ensure we've created a promise before the
registration delegate methods get called.
- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
cases of inexplicable behavior - one a recently, push notification
premature free, in redphone, and more popular use, and I've seen two
futures inexplicably being nil. Instead, let's consolidate around
PromiseKit for popularly used, maintained, strongly-typed futures.
- separate logic for registering for vanilla push/voip notifications
(few dependencies) from responding to UILocalNotifications (lots of
dependencies). Ultimately I'd like to consolidate the remaining
UILocalNotifications logic with the existing NotificationsManager
== Misc ==
more debug logging
more uniform logging
remove stale logic around newly registered user
// FREEBIE
2017-09-20 23:47:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:23:24 +02:00
|
|
|
// MARK: Message Delivery
|
2016-11-12 18:22:29 +01:00
|
|
|
|
2016-10-10 22:02:09 +02:00
|
|
|
func updatePushTokens(pushToken: String, voipToken: String) -> Promise<Void> {
|
2019-03-30 14:44:47 +01:00
|
|
|
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,
|
2019-03-30 14:44:47 +01:00
|
|
|
success: { resolver.fulfill(()) },
|
2018-10-13 21:21:46 +02:00
|
|
|
failure: resolver.reject)
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2017-10-11 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
// MARK: Turn Server
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
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(),
|
2017-07-08 23:43:34 +02:00
|
|
|
success: { (_: URLSessionDataTask, responseObject: Any?) in
|
2016-11-12 18:22:29 +01:00
|
|
|
guard responseObject != nil else {
|
2018-10-13 21:21:46 +02:00
|
|
|
return resolver.reject(OWSErrorMakeUnableToProcessServerResponseError())
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
2016-10-10 22:02:09 +02:00
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
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)
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.error("unexpected server response:\(responseDictionary)")
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
2018-10-13 21:21:46 +02:00
|
|
|
return resolver.reject(OWSErrorMakeUnableToProcessServerResponseError())
|
2016-11-12 18:22:29 +01:00
|
|
|
},
|
2017-07-08 23:43:34 +02:00
|
|
|
failure: { (_: URLSessionDataTask, error: Error) in
|
2018-10-13 21:21:46 +02:00
|
|
|
return resolver.reject(error)
|
2016-11-12 18:22:29 +01:00
|
|
|
})
|
2016-10-10 22:02:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|