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.
		
		
		
		
		
			
		
			
	
	
		
			119 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			119 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			Swift
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
| 
											9 years ago
										 | 
 | ||
|  | import Foundation | ||
|  | import PromiseKit | ||
| 
											5 years ago
										 | import SignalUtilitiesKit | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 | /**
 | ||
|  |  * Signal is actually two services - textSecure for messages and red phone (for calls).  | ||
|  |  * AccountManager delegates to both. | ||
|  |  */ | ||
| 
											8 years ago
										 | @objc | ||
|  | public class AccountManager: NSObject { | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     // MARK: - Dependencies | ||
|  | 
 | ||
|  |     var profileManager: OWSProfileManager { | ||
|  |         return OWSProfileManager.shared() | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     private var preferences: OWSPreferences { | ||
|  |         return Environment.shared.preferences | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     private var tsAccountManager: TSAccountManager { | ||
|  |         return TSAccountManager.sharedInstance() | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: - | ||
|  | 
 | ||
|  |     @objc | ||
|  |     public override init() { | ||
|  |         super.init() | ||
|  | 
 | ||
|  |         SwiftSingletons.register(self) | ||
|  |     } | ||
|  | 
 | ||
| 
											9 years ago
										 |     // MARK: registration | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc func registerObjc(verificationCode: String, | ||
|  |                             pin: String?) -> AnyPromise { | ||
|  |         return AnyPromise(register(verificationCode: verificationCode, pin: pin)) | ||
| 
											9 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     func register(verificationCode: String, | ||
|  |                   pin: String?) -> Promise<Void> { | ||
| 
											8 years ago
										 |         guard verificationCode.count > 0 else { | ||
| 
											8 years ago
										 |             let error = OWSErrorWithCodeDescription(.userError, | ||
|  |                                                     NSLocalizedString("REGISTRATION_ERROR_BLANK_VERIFICATION_CODE", | ||
|  |                                                                       comment: "alert body during registration")) | ||
|  |             return Promise(error: error) | ||
|  |         } | ||
|  | 
 | ||
| 
											7 years ago
										 |         Logger.debug("registering with signal server") | ||
| 
											8 years ago
										 |         let registrationPromise: Promise<Void> = firstly { | ||
| 
											8 years ago
										 |             return self.registerForTextSecure(verificationCode: verificationCode, pin: pin) | ||
| 
											7 years ago
										 |         }.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 | ||
| 
											8 years ago
										 |             self.completeRegistration() | ||
| 
											9 years ago
										 |         } | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |         registrationPromise.retainUntilComplete() | ||
|  | 
 | ||
|  |         return registrationPromise | ||
| 
											9 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     private func registerForTextSecure(verificationCode: String, | ||
|  |                                        pin: String?) -> Promise<Void> { | ||
| 
											7 years ago
										 |         return Promise { resolver in | ||
| 
											7 years ago
										 |             tsAccountManager.verifyAccount(withCode: verificationCode, | ||
| 
											7 years ago
										 |                                            pin: pin, | ||
| 
											7 years ago
										 |                                            success: { resolver.fulfill(()) }, | ||
| 
											7 years ago
										 |                                            failure: resolver.reject) | ||
| 
											9 years ago
										 |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     private func syncPushTokens() -> Promise<Void> { | ||
| 
											7 years ago
										 |         Logger.info("") | ||
| 
											8 years ago
										 |         let job = SyncPushTokensJob(accountManager: self, preferences: self.preferences) | ||
|  |         job.uploadOnlyIfStale = false | ||
|  |         return job.run() | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
|  |     private func completeRegistration() { | ||
| 
											7 years ago
										 |         Logger.info("") | ||
| 
											7 years ago
										 |         tsAccountManager.didRegister() | ||
| 
											8 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     // MARK: Message Delivery | ||
| 
											9 years ago
										 | 
 | ||
| 
											6 years ago
										 |     func updatePushTokens(pushToken: String, voipToken: String, isForcedUpdate: Bool) -> Promise<Void> { | ||
| 
											7 years ago
										 |         return Promise { resolver in | ||
| 
											7 years ago
										 |             tsAccountManager.registerForPushNotifications(pushToken: pushToken, | ||
| 
											7 years ago
										 |                                                           voipToken: voipToken, | ||
| 
											6 years ago
										 |                                                      isForcedUpdate: isForcedUpdate, | ||
|  |                                                             success: { resolver.fulfill(()) }, | ||
|  |                                                             failure: resolver.reject) | ||
| 
											9 years ago
										 |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     func enableManualMessageFetching() -> Promise<Void> { | ||
| 
											7 years ago
										 |         let anyPromise = tsAccountManager.setIsManualMessageFetchEnabled(true) | ||
|  |         return Promise(anyPromise).asVoid() | ||
| 
											8 years ago
										 |     } | ||
| 
											9 years ago
										 | } |