diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index ff0321d704..0252d36f63 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -394,14 +394,13 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; } DDLogInfo(@"Application opened with URL: %@", url); - [[TSAccountManager sharedInstance] ifRegistered:YES - runAsync:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - // Wait up to N seconds for database view registrations to - // complete. - [self showImportUIForAttachment:attachment remainingRetries:5]; - }); - }]; + if ([TSAccountManager isRegistered]) { + dispatch_async(dispatch_get_main_queue(), ^{ + // Wait up to N seconds for database view registrations to + // complete. + [self showImportUIForAttachment:attachment remainingRetries:5]; + }); + } return YES; } else { @@ -455,70 +454,52 @@ static NSString *const kURLHostVerifyPrefix = @"verify"; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ + RTCInitializeSSL(); + if ([TSAccountManager isRegistered]) { + // At this point, potentially lengthy DB locking migrations could be running. + // Avoid blocking app launch by putting all further possible DB access in async block + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + DDLogInfo( + @"%@ running post launch block for registered user: %@", self.tag, [TSAccountManager localNumber]); + + // Clean up any messages that expired since last launch immediately + // and continue cleaning in the background. + [[OWSDisappearingMessagesJob sharedJob] startIfNecessary]; + + // Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully + // sent before the app exited should be marked as failures. + [[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; + [[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; + + [AppStoreRating setupRatingLibrary]; + }); + } else { + DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag); + + // Unregistered user should have no unread messages. e.g. if you delete your account. + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; + + [TSSocketManager requestSocketOpen]; + + UITapGestureRecognizer *gesture = + [[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] action:@selector(submitLogs)]; + gesture.numberOfTapsRequired = 8; + [self.window addGestureRecognizer:gesture]; + } + }); // end dispatchOnce for first time we become active + + // Every time we become active... + if ([TSAccountManager isRegistered]) { // At this point, potentially lengthy DB locking migrations could be running. - // Avoid blocking app launch by putting all further possible DB access in async thread. - [[TSAccountManager sharedInstance] - ifRegistered:YES - runAsync:^{ - DDLogInfo(@"%@ running post launch block for registered user: %@", - self.tag, - [TSAccountManager localNumber]); - - RTCInitializeSSL(); - - [OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager] - accountManager:[Environment getCurrent].accountManager - preferences:[Environment preferences] - showAlerts:NO]; - - // Clean up any messages that expired since last launch immediately - // and continue cleaning in the background. - [[OWSDisappearingMessagesJob sharedJob] startIfNecessary]; - - // Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully - // sent before the app exited should be marked as failures. - [[[OWSFailedMessagesJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] run]; - [[[OWSFailedAttachmentDownloadsJob alloc] initWithStorageManager:[TSStorageManager sharedManager]] - run]; - - [AppStoreRating setupRatingLibrary]; - }]; - - [[TSAccountManager sharedInstance] - ifRegistered:NO - runAsync:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - DDLogInfo(@"%@ running post launch block for unregistered user.", self.tag); - - // Unregistered user should have no unread messages. e.g. if you delete your account. - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; - - [TSSocketManager requestSocketOpen]; - - UITapGestureRecognizer *gesture = - [[UITapGestureRecognizer alloc] initWithTarget:[Pastelog class] - action:@selector(submitLogs)]; - gesture.numberOfTapsRequired = 8; - [self.window addGestureRecognizer:gesture]; - }); - RTCInitializeSSL(); - }]; - }); - - [[TSAccountManager sharedInstance] - ifRegistered:YES - runAsync:^{ - [TSSocketManager requestSocketOpen]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized]; - }); - - // This will fetch new messages, if we're using domain - // fronting. - [[PushManager sharedManager] applicationDidBecomeActive]; - }]; + // Avoid blocking app launch by putting all further possible DB access in async block + dispatch_async(dispatch_get_main_queue(), ^{ + [TSSocketManager requestSocketOpen]; + [[Environment getCurrent].contactsManager fetchSystemContactsIfAlreadyAuthorized]; + // This will fetch new messages, if we're using domain fronting. + [[PushManager sharedManager] applicationDidBecomeActive]; + }); + } DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.tag); } diff --git a/Signal/src/environment/Migrations/OWS103EnableVideoCalling.m b/Signal/src/environment/Migrations/OWS103EnableVideoCalling.m index 6dcb54f92d..d0f5911f0c 100644 --- a/Signal/src/environment/Migrations/OWS103EnableVideoCalling.m +++ b/Signal/src/environment/Migrations/OWS103EnableVideoCalling.m @@ -21,31 +21,27 @@ static NSString *const OWS103EnableVideoCallingMigrationId = @"103"; - (void)runUp { DDLogWarn(@"%@ running migration...", self.tag); - - // TODO: It'd be nice if TSAccountManager had a - // [ifRegisteredRunAsync: ifNoRegisteredRunAsync:] method. - [[TSAccountManager sharedInstance] ifRegistered:YES - runAsync:^{ - TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] - initWithUpdatedAttributesWithVoice]; - [[TSNetworkManager sharedManager] makeRequest:request - success:^(NSURLSessionDataTask *task, id responseObject) { - DDLogInfo(@"%@ successfully ran", self.tag); - [self save]; - } - failure:^(NSURLSessionDataTask *task, NSError *error) { - if (!IsNSErrorNetworkFailure(error)) { - OWSProdError([OWSAnalyticsEvents - errorEnableVideoCallingRequestFailed]); - } - DDLogError(@"%@ failed with error: %@", self.tag, error); - }]; - }]; - [[TSAccountManager sharedInstance] ifRegistered:NO - runAsync:^{ - DDLogInfo(@"%@ skipping; not registered", self.tag); - [self save]; - }]; + if ([TSAccountManager isRegistered]) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + TSUpdateAttributesRequest *request = [[TSUpdateAttributesRequest alloc] initWithUpdatedAttributesWithVoice]; + [[TSNetworkManager sharedManager] makeRequest:request + success:^(NSURLSessionDataTask *task, id responseObject) { + DDLogInfo(@"%@ successfully ran", self.tag); + [self save]; + } + failure:^(NSURLSessionDataTask *task, NSError *error) { + if (!IsNSErrorNetworkFailure(error)) { + OWSProdError([OWSAnalyticsEvents errorEnableVideoCallingRequestFailed]); + } + DDLogError(@"%@ failed with error: %@", self.tag, error); + }]; + }); + } else { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + DDLogInfo(@"%@ skipping; not registered", self.tag); + [self save]; + }); + } } #pragma mark - Logging diff --git a/Signal/src/util/OWSContactsSyncing.m b/Signal/src/util/OWSContactsSyncing.m index 64aaabf747..16243c69cd 100644 --- a/Signal/src/util/OWSContactsSyncing.m +++ b/Signal/src/util/OWSContactsSyncing.m @@ -139,12 +139,9 @@ NSString *const kTSStorageManagerOWSContactsSyncingLastMessageKey = return; } - [[TSAccountManager sharedInstance] ifRegistered:YES - runAsync:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - [self sendSyncContactsMessageIfNecessary]; - }); - }]; + if ([TSAccountManager sharedInstance]) { + [self sendSyncContactsMessageIfNecessary]; + } } #pragma mark - Logging diff --git a/SignalServiceKit/src/Account/TSAccountManager.h b/SignalServiceKit/src/Account/TSAccountManager.h index 84a42d8c6b..2ced4049ac 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.h +++ b/SignalServiceKit/src/Account/TSAccountManager.h @@ -35,8 +35,6 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange; */ + (BOOL)isRegistered; -- (void)ifRegistered:(BOOL)isRegistered runAsync:(void (^)())block; - /** * Returns current phone number for this device, which may not yet have been registered. * diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m index caa07f63df..aafd4db9bc 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.m +++ b/SignalServiceKit/src/Account/TSAccountManager.m @@ -93,26 +93,6 @@ NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegist return _isRegistered; } -- (void)ifRegistered:(BOOL)runIfRegistered runAsync:(void (^)())block -{ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - if ([self isRegistered] == runIfRegistered) { - if (runIfRegistered) { - DDLogDebug(@"%@ Running existing-user block", self.tag); - } else { - DDLogDebug(@"%@ Running new-user block", self.tag); - } - block(); - } else { - if (runIfRegistered) { - DDLogDebug(@"%@ Skipping existing-user block for new-user", self.tag); - } else { - DDLogDebug(@"%@ Skipping new-user block for existing-user", self.tag); - } - } - }); -} - - (void)didRegister { DDLogInfo(@"%@ didRegister", self.tag); diff --git a/SignalServiceKit/src/Account/TSPreKeyManager.m b/SignalServiceKit/src/Account/TSPreKeyManager.m index 8dd9a218e1..e5851031cf 100644 --- a/SignalServiceKit/src/Account/TSPreKeyManager.m +++ b/SignalServiceKit/src/Account/TSPreKeyManager.m @@ -109,10 +109,11 @@ static const NSTimeInterval kSignedPreKeyUpdateFailureMaxFailureDuration = 10 * // condition. lastPreKeyCheckTimestamp = [NSDate date]; - [[TSAccountManager sharedInstance] ifRegistered:YES - runAsync:^{ - [TSPreKeyManager checkPreKeys]; - }]; + if ([TSAccountManager isRegistered]) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [TSPreKeyManager checkPreKeys]; + }); + } } }); }