diff --git a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift index 31a33d0ab..79bdfa64f 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift @@ -165,9 +165,9 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink) ThreadUtil.enqueue(linkingAuthorizationMessage) SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: { - SSKEnvironment.shared.syncManager.syncAllContacts() - SSKEnvironment.shared.syncManager.syncAllGroups() - SSKEnvironment.shared.syncManager.syncAllOpenGroups() + let _ = SSKEnvironment.shared.syncManager.syncAllContacts() + let _ = SSKEnvironment.shared.syncManager.syncAllGroups() + let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups() let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey) thread.friendRequestStatus = .friends thread.save() diff --git a/Signal/src/Loki/View Controllers/LandingVC.swift b/Signal/src/Loki/View Controllers/LandingVC.swift index a27babeda..cd2a08681 100644 --- a/Signal/src/Loki/View Controllers/LandingVC.swift +++ b/Signal/src/Loki/View Controllers/LandingVC.swift @@ -156,37 +156,14 @@ final class LandingVC : UIViewController, LinkDeviceVCDelegate, DeviceLinkingMod databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey TSAccountManager.sharedInstance().didRegister() - setUserInteractionEnabled(false) - let _ = LokiFileServerAPI.getDeviceLinks(associatedWith: hexEncodedPublicKey).done(on: DispatchQueue.main) { [weak self] deviceLinks in - guard let self = self else { return } - defer { self.setUserInteractionEnabled(true) } - guard deviceLinks.count < 2 else { - let alert = UIAlertController(title: "Multi Device Limit Reached", message: "It's currently not allowed to link more than one device.", preferredStyle: .alert) - alert.addAction(UIAlertAction(title: "OK", accessibilityIdentifier: nil, style: .default, handler: nil)) - return self.present(alert, animated: true, completion: nil) - } - let appDelegate = UIApplication.shared.delegate as! AppDelegate - appDelegate.startLongPollerIfNeeded() - let deviceLinkingModal = DeviceLinkingModal(mode: .slave, delegate: self) - deviceLinkingModal.modalPresentationStyle = .overFullScreen - deviceLinkingModal.modalTransitionStyle = .crossDissolve - self.present(deviceLinkingModal, animated: true, completion: nil) - let linkingRequestMessage = DeviceLinkingUtilities.getLinkingRequestMessage(for: hexEncodedPublicKey) - ThreadUtil.enqueue(linkingRequestMessage) - }.catch(on: DispatchQueue.main) { [weak self] _ in - let appDelegate = UIApplication.shared.delegate as! AppDelegate - appDelegate.stopLongPollerIfNeeded() - DispatchQueue.main.async { - // FIXME: For some reason resetForRegistration() complains about not being on the main queue - // without this (even though the catch closure should be executed on the main queue) - TSAccountManager.sharedInstance().resetForReregistration() - } - guard let self = self else { return } - let alert = UIAlertController(title: NSLocalizedString("Couldn't Link Device", comment: ""), message: NSLocalizedString("Please check your internet connection and try again", comment: ""), preferredStyle: .alert) - alert.addAction(UIAlertAction(title: "OK", accessibilityIdentifier: nil, style: .default, handler: nil)) - self.present(alert, animated: true, completion: nil) - self.setUserInteractionEnabled(true) - } + let appDelegate = UIApplication.shared.delegate as! AppDelegate + appDelegate.startLongPollerIfNeeded() + let deviceLinkingModal = DeviceLinkingModal(mode: .slave, delegate: self) + deviceLinkingModal.modalPresentationStyle = .overFullScreen + deviceLinkingModal.modalTransitionStyle = .crossDissolve + self.present(deviceLinkingModal, animated: true, completion: nil) + let linkingRequestMessage = DeviceLinkingUtilities.getLinkingRequestMessage(for: hexEncodedPublicKey) + ThreadUtil.enqueue(linkingRequestMessage) } func handleDeviceLinkAuthorized(_ deviceLink: DeviceLink) { diff --git a/Signal/src/Loki/View Controllers/LinkDeviceVC.swift b/Signal/src/Loki/View Controllers/LinkDeviceVC.swift index 888866bb8..42134cf06 100644 --- a/Signal/src/Loki/View Controllers/LinkDeviceVC.swift +++ b/Signal/src/Loki/View Controllers/LinkDeviceVC.swift @@ -140,8 +140,9 @@ final class LinkDeviceVC : UIViewController, UIPageViewControllerDataSource, UIP } fileprivate func requestDeviceLink(with hexEncodedPublicKey: String) { - delegate?.requestDeviceLink(with: hexEncodedPublicKey) - dismiss(animated: true, completion: nil) + dismiss(animated: true) { + self.delegate?.requestDeviceLink(with: hexEncodedPublicKey) + } } } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index fabf5e6df..020979087 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -167,6 +167,7 @@ public final class LokiAPI : NSObject { lastDeviceLinkUpdate[hexEncodedPublicKey] = Date() getDestinations() } else { + print("[Loki] Failed to get device links due to error: \(error).") seal.reject(error) } } diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift index 7867390b1..c768de746 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift @@ -6,15 +6,7 @@ public extension OWSPrimaryStorage { } public func setDeviceLinks(_ deviceLinks: Set, in transaction: YapDatabaseReadWriteTransaction) { - let masterHexEncodedPublicKeys = Set(deviceLinks.map { $0.master.hexEncodedPublicKey }) - guard !masterHexEncodedPublicKeys.isEmpty else { return } - guard masterHexEncodedPublicKeys.count == 1 else { - print("[Loki] Found inconsistent set of device links.") - return - } - let masterHexEncodedPublicKey = masterHexEncodedPublicKeys.first! - let collection = getDeviceLinkCollection(for: masterHexEncodedPublicKey) - transaction.removeAllObjects(inCollection: collection) + // FIXME: Clear collections first? deviceLinks.forEach { addDeviceLink($0, in: transaction) } // TODO: Check the performance impact of this } @@ -30,7 +22,7 @@ public extension OWSPrimaryStorage { public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set { let collection = getDeviceLinkCollection(for: masterHexEncodedPublicKey) - guard Set(transaction.allKeys(inCollection: collection)).contains(masterHexEncodedPublicKey) else { return [] } // Fixes a crash that used to occur on Josh's device + guard !transaction.allKeys(inCollection: collection).isEmpty else { return [] } // Fixes a crash that used to occur on Josh's device var result: Set = [] transaction.enumerateRows(inCollection: collection) { _, object, _, _ in guard let deviceLink = object as? DeviceLink else { return }