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.
		
		
		
		
		
			
		
			
	
	
		
			108 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			108 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Swift
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
| 
											9 years ago
										 | 
 | ||
|  | import PromiseKit | ||
| 
											5 years ago
										 | import SignalUtilitiesKit | ||
| 
											9 years ago
										 | 
 | ||
|  | @objc(OWSSyncPushTokensJob) | ||
| 
											9 years ago
										 | class SyncPushTokensJob: NSObject { | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc public static let PushTokensDidChange = Notification.Name("PushTokensDidChange") | ||
|  | 
 | ||
| 
											8 years ago
										 |     // MARK: Dependencies | ||
| 
											9 years ago
										 |     let accountManager: AccountManager | ||
| 
											8 years ago
										 |     let preferences: OWSPreferences | ||
| 
											8 years ago
										 |     var pushRegistrationManager: PushRegistrationManager { | ||
|  |         return PushRegistrationManager.shared | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc var uploadOnlyIfStale = true | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 |     @objc | ||
| 
											8 years ago
										 |     required init(accountManager: AccountManager, preferences: OWSPreferences) { | ||
| 
											9 years ago
										 |         self.accountManager = accountManager | ||
|  |         self.preferences = preferences | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     class func run(accountManager: AccountManager, preferences: OWSPreferences) -> Promise<Void> { | ||
|  |         let job = self.init(accountManager: accountManager, preferences: preferences) | ||
| 
											8 years ago
										 |         return job.run() | ||
| 
											9 years ago
										 |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     func run() -> Promise<Void> { | ||
| 
											9 years ago
										 | 
 | ||
| 
											7 years ago
										 |         let runPromise = firstly { | ||
| 
											8 years ago
										 |             return self.pushRegistrationManager.requestPushTokens() | ||
| 
											7 years ago
										 |         }.then { (pushToken: String, voipToken: String) -> Promise<Void> in | ||
| 
											9 years ago
										 |             var shouldUploadTokens = false | ||
|  | 
 | ||
| 
											9 years ago
										 |             if self.preferences.getPushToken() != pushToken || self.preferences.getVoipToken() != voipToken { | ||
| 
											9 years ago
										 |                 shouldUploadTokens = true | ||
|  |             } else if !self.uploadOnlyIfStale { | ||
|  |                 shouldUploadTokens = true | ||
| 
											9 years ago
										 |             } | ||
|  | 
 | ||
| 
											7 years ago
										 |             if AppVersion.sharedInstance().lastAppVersion != AppVersion.sharedInstance().currentAppVersion { | ||
| 
											9 years ago
										 |                 shouldUploadTokens = true | ||
|  |             } | ||
|  | 
 | ||
| 
											9 years ago
										 |             guard shouldUploadTokens else { | ||
| 
											7 years ago
										 |                 return Promise.value(()) | ||
| 
											9 years ago
										 |             } | ||
|  | 
 | ||
| 
											7 years ago
										 |             return firstly { | ||
| 
											6 years ago
										 |                 self.accountManager.updatePushTokens(pushToken: pushToken, voipToken: voipToken, isForcedUpdate: shouldUploadTokens) | ||
| 
											7 years ago
										 |             }.done { _ in | ||
|  |                 self.recordPushTokensLocally(pushToken: pushToken, voipToken: voipToken) | ||
| 
											9 years ago
										 |             } | ||
| 
											9 years ago
										 |         } | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 |         runPromise.retainUntilComplete() | ||
| 
											8 years ago
										 | 
 | ||
|  |         return runPromise | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK: - objc wrappers, since objc can't use swift parameterized types | ||
| 
											8 years ago
										 | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     class func run(accountManager: AccountManager, preferences: OWSPreferences) -> AnyPromise { | ||
| 
											8 years ago
										 |         let promise: Promise<Void> = self.run(accountManager: accountManager, preferences: preferences) | ||
| 
											8 years ago
										 |         return AnyPromise(promise) | ||
|  |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     @objc | ||
|  |     func run() -> AnyPromise { | ||
| 
											8 years ago
										 |         let promise: Promise<Void> = self.run() | ||
|  |         return AnyPromise(promise) | ||
| 
											9 years ago
										 |     } | ||
|  | 
 | ||
| 
											7 years ago
										 |     // MARK:  | ||
|  | 
 | ||
|  |     private func recordPushTokensLocally(pushToken: String, voipToken: String) { | ||
| 
											7 years ago
										 |         Logger.warn("Recording push tokens locally. pushToken: \(redact(pushToken)), voipToken: \(redact(voipToken))") | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 |         var didTokensChange = false | ||
|  | 
 | ||
| 
											9 years ago
										 |         if (pushToken != self.preferences.getPushToken()) { | ||
| 
											7 years ago
										 |             Logger.info("Recording new plain push token") | ||
| 
											9 years ago
										 |             self.preferences.setPushToken(pushToken) | ||
| 
											8 years ago
										 |             didTokensChange = true | ||
| 
											9 years ago
										 |         } | ||
|  | 
 | ||
|  |         if (voipToken != self.preferences.getVoipToken()) { | ||
| 
											7 years ago
										 |             Logger.info("Recording new voip token") | ||
| 
											9 years ago
										 |             self.preferences.setVoipToken(voipToken) | ||
| 
											8 years ago
										 |             didTokensChange = true | ||
|  |         } | ||
|  | 
 | ||
|  |         if (didTokensChange) { | ||
| 
											8 years ago
										 |             NotificationCenter.default.postNotificationNameAsync(SyncPushTokensJob.PushTokensDidChange, object: nil) | ||
| 
											9 years ago
										 |         } | ||
|  |     } | ||
|  | } | ||
| 
											7 years ago
										 | 
 | ||
|  | private func redact(_ string: String) -> String { | ||
|  |     return OWSIsDebugBuild() ? string : "[ READACTED \(string.prefix(2))...\(string.suffix(2)) ]" | ||
|  | } |