diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 34fe0eec2..e05c62410 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -319,11 +319,14 @@ static NSTimeInterval launchStartedAt; object:nil]; // Loki - Observe new messages received notifications - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNewMessagesReceived:) name:NSNotification.newMessagesReceived object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleNewMessagesReceived:) name:NSNotification.newMessagesReceived object:nil]; // Loki - Observe thread deleted notifications - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThreadDeleted:) name:NSNotification.threadDeleted object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleThreadDeleted:) name:NSNotification.threadDeleted object:nil]; + // Loki - Observe data nuke request notifications + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleDataNukeRequested:) name:NSNotification.dataNukeRequested object:nil]; + OWSLogInfo(@"application: didFinishLaunchingWithOptions completed."); [OWSAnalytics appLaunchDidBegin]; @@ -1609,4 +1612,16 @@ static NSTimeInterval launchStartedAt; } } +- (void)handleDataNukeRequested:(NSNotification *)notification { + [ThreadUtil deleteAllContent]; + [SSKEnvironment.shared.identityManager clearIdentityKey]; + [LKAPI clearRandomSnodePool]; + [self stopLongPollerIfNeeded]; + [SSKEnvironment.shared.tsAccountManager resetForReregistration]; + UIViewController *rootVC = [OnboardingController new].initialViewController; + OWSNavigationController *navigationVC = [[OWSNavigationController alloc] initWithRootViewController:rootVC]; + [navigationVC setNavigationBarHidden:YES]; + UIApplication.sharedApplication.keyWindow.rootViewController = navigationVC; +} + @end diff --git a/Signal/src/Loki/Settings/NukeDataModal.swift b/Signal/src/Loki/Settings/NukeDataModal.swift index 98dcfaaa1..987ef7112 100644 --- a/Signal/src/Loki/Settings/NukeDataModal.swift +++ b/Signal/src/Loki/Settings/NukeDataModal.swift @@ -48,15 +48,6 @@ final class NukeDataModal : Modal { // MARK: Interaction @objc private func nuke() { Analytics.shared.track("Data Nuked") - ThreadUtil.deleteAllContent() - SSKEnvironment.shared.identityManager.clearIdentityKey() - LokiAPI.clearRandomSnodePool() - let appDelegate = UIApplication.shared.delegate as! AppDelegate - appDelegate.stopLongPollerIfNeeded() - SSKEnvironment.shared.tsAccountManager.resetForReregistration() - let rootViewController = OnboardingController().initialViewController() - let navigationController = OWSNavigationController(rootViewController: rootViewController) - navigationController.isNavigationBarHidden = true - UIApplication.shared.keyWindow!.rootViewController = navigationController + NotificationCenter.default.post(name: .dataNukeRequested, object: nil) } } diff --git a/SignalServiceKit/src/Loki/Messaging/Notification+Loki.swift b/SignalServiceKit/src/Loki/Messaging/Notification+Loki.swift index e74beddef..061c3144f 100644 --- a/SignalServiceKit/src/Loki/Messaging/Notification+Loki.swift +++ b/SignalServiceKit/src/Loki/Messaging/Notification+Loki.swift @@ -5,6 +5,7 @@ public extension Notification.Name { public static let threadFriendRequestStatusChanged = Notification.Name("threadFriendRequestStatusChanged") public static let messageFriendRequestStatusChanged = Notification.Name("messageFriendRequestStatusChanged") public static let threadDeleted = Notification.Name("threadDeleted") + public static let dataNukeRequested = Notification.Name("dataNukeRequested") } @objc public extension NSNotification { @@ -13,4 +14,5 @@ public extension Notification.Name { @objc public static let threadFriendRequestStatusChanged = Notification.Name.threadFriendRequestStatusChanged.rawValue as NSString @objc public static let messageFriendRequestStatusChanged = Notification.Name.messageFriendRequestStatusChanged.rawValue as NSString @objc public static let threadDeleted = Notification.Name.threadDeleted.rawValue as NSString + @objc public static let dataNukeRequested = Notification.Name.dataNukeRequested.rawValue as NSString } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 56e74d24c..07ee706ae 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -591,6 +591,8 @@ NS_ASSUME_NONNULL_BEGIN [self handleExpirationTimerUpdateMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction]; } else if ((dataMessage.flags & SSKProtoDataMessageFlagsProfileKeyUpdate) != 0) { [self handleProfileKeyMessageWithEnvelope:envelope dataMessage:dataMessage]; + } else if ((dataMessage.flags & SSKProtoDataMessageFlagsUnlinkDevice) != 0) { + [self handleUnlinkDeviceMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction]; } else if (dataMessage.attachments.count > 0) { [self handleReceivedMediaWithEnvelope:envelope dataMessage:dataMessage @@ -1181,6 +1183,27 @@ NS_ASSUME_NONNULL_BEGIN [profileManager setProfileKeyData:profileKey forRecipientId:recipientId]; } +- (void)handleUnlinkDeviceMessageWithEnvelope:(SSKProtoEnvelope *)envelope dataMessage:(SSKProtoDataMessage *)dataMessage transaction:(YapDatabaseReadWriteTransaction *)transaction +{ + NSString *senderHexEncodedPublicKey = envelope.source; + NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; + NSString *masterHexEncodedPublicKey = [LKDatabaseUtilities getMasterHexEncodedPublicKeyFor:userHexEncodedPublicKey in:transaction]; + if (![masterHexEncodedPublicKey isEqual:senderHexEncodedPublicKey]) { return; } + NSSet *deviceLinks = [LKDatabaseUtilities getDeviceLinksFor:senderHexEncodedPublicKey in:transaction]; + if (![deviceLinks contains:^BOOL(LKDeviceLink *deviceLink) { + return [deviceLink.master.hexEncodedPublicKey isEqual:senderHexEncodedPublicKey] && [deviceLink.slave.hexEncodedPublicKey isEqual:userHexEncodedPublicKey]; + }]) { + return; + } + [LKStorageAPI getDeviceLinksAssociatedWith:userHexEncodedPublicKey].thenOn(dispatch_get_main_queue(), ^(NSSet *deviceLinks) { + if (![deviceLinks contains:^BOOL(LKDeviceLink *deviceLink) { + return [deviceLink.master.hexEncodedPublicKey isEqual:senderHexEncodedPublicKey] && [deviceLink.slave.hexEncodedPublicKey isEqual:userHexEncodedPublicKey]; + }]) { + [NSNotificationCenter.defaultCenter postNotification:NSNotification.dataNukeRequested]; + } + }); +} + - (void)handleReceivedTextMessageWithEnvelope:(SSKProtoEnvelope *)envelope dataMessage:(SSKProtoDataMessage *)dataMessage wasReceivedByUD:(BOOL)wasReceivedByUD