diff --git a/Signal/src/Loki/View Controllers/BaseVC.swift b/Signal/src/Loki/View Controllers/BaseVC.swift index 4beca6679..19803f660 100644 --- a/Signal/src/Loki/View Controllers/BaseVC.swift +++ b/Signal/src/Loki/View Controllers/BaseVC.swift @@ -5,5 +5,19 @@ class BaseVC : UIViewController { override func viewDidLoad() { setNeedsStatusBarAppearanceUpdate() + NotificationCenter.default.addObserver(self, selector: #selector(handleUnexpectedDeviceLinkRequestReceivedNotification), name: .unexpectedDeviceLinkRequestReceived, object: nil) + } + + deinit { + NotificationCenter.default.removeObserver(self) + } + + @objc private func handleUnexpectedDeviceLinkRequestReceivedNotification() { + guard DeviceLinkingUtilities.shouldShowUnexpectedDeviceLinkRequestReceivedAlert else { return } + DispatchQueue.main.async { + let alert = UIAlertController(title: "Device Link Request Received", message: "Open the device link screen by going to \"Settings\"> \"Devices\" > \"Link a Device\" to link your devices.", preferredStyle: .alert) + alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) + self.present(alert, animated: true, completion: nil) + } } } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 6a8af49d0..bdb42f243 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -444,6 +444,11 @@ typedef enum : NSUInteger { selector:@selector(handleMessageFailedNotification:) name:NSNotification.messageFailed object:nil]; + // Device linking + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleUnexpectedDeviceLinkRequestReceivedNotification) + name:NSNotification.unexpectedDeviceLinkRequestReceived + object:nil]; } - (BOOL)isGroupConversation @@ -5479,6 +5484,16 @@ typedef enum : NSUInteger { }); } +- (void)handleUnexpectedDeviceLinkRequestReceivedNotification +{ + if (!LKDeviceLinkingUtilities.shouldShowUnexpectedDeviceLinkRequestReceivedAlert) { return; } + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Device Link Request Received" message:@"Open the device link screen by going to \"Settings\"> \"Devices\" > \"Link a Device\" to link your devices." preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [self presentViewController:alert animated:YES completion:nil]; + }); +} + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingUtilities.swift b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingUtilities.swift index a36a75e60..8af5145c2 100644 --- a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingUtilities.swift +++ b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingUtilities.swift @@ -1,6 +1,19 @@ -public enum DeviceLinkingUtilities { - +@objc(LKDeviceLinkingUtilities) +public final class DeviceLinkingUtilities : NSObject { + private static var lastUnexpectedDeviceLinkRequestDate: Date? = nil + + private override init() { } + + @objc public static var shouldShowUnexpectedDeviceLinkRequestReceivedAlert: Bool { + let now = Date() + if let lastUnexpectedDeviceLinkRequestDate = lastUnexpectedDeviceLinkRequestDate { + if now.timeIntervalSince(lastUnexpectedDeviceLinkRequestDate) < 30 { return false } + } + lastUnexpectedDeviceLinkRequestDate = now + return true + } + // When requesting a device link, the slave device signs the master device's public key. When authorizing // a device link, the master device signs the slave device's public key. diff --git a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift index f4ab9bef8..c2adfb2bf 100644 --- a/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift +++ b/SignalServiceKit/src/Loki/Utilities/Notification+Loki.swift @@ -17,6 +17,8 @@ public extension Notification.Name { public static let seedViewed = Notification.Name("seedViewed") // Interaction public static let dataNukeRequested = Notification.Name("dataNukeRequested") + // Device linking + public static let unexpectedDeviceLinkRequestReceived = Notification.Name("unexpectedDeviceLinkRequestReceived") } @objc public extension NSNotification { @@ -37,4 +39,6 @@ public extension Notification.Name { @objc public static let seedViewed = Notification.Name.seedViewed.rawValue as NSString // Interaction @objc public static let dataNukeRequested = Notification.Name.dataNukeRequested.rawValue as NSString + // Device linking + @objc public static let unexpectedDeviceLinkRequestReceived = Notification.Name.unexpectedDeviceLinkRequestReceived.rawValue as NSString } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 16ea7fd0b..bc6ce2031 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -494,6 +494,9 @@ NS_ASSUME_NONNULL_BEGIN } } else if (slaveSignature != nil) { // Request OWSLogInfo(@"[Loki] Received a device linking request from: %@", envelope.source); // Not slaveHexEncodedPublicKey + if (LKDeviceLinkingSession.current == nil) { + [NSNotificationCenter.defaultCenter postNotificationName:NSNotification.unexpectedDeviceLinkRequestReceived object:nil]; + } [LKDeviceLinkingSession.current processLinkingRequestFrom:slaveHexEncodedPublicKey to:masterHexEncodedPublicKey with:slaveSignature]; } } else if (contentProto.syncMessage) {