Fix various multi device issues

This commit is contained in:
Niels Andriesse 2020-03-11 14:35:55 +11:00
parent 84ec8dffcd
commit af7ac45b82
5 changed files with 17 additions and 46 deletions

View File

@ -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()

View File

@ -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) {

View File

@ -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)
}
}
}

View File

@ -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)
}
}

View File

@ -6,15 +6,7 @@ public extension OWSPrimaryStorage {
}
public func setDeviceLinks(_ deviceLinks: Set<DeviceLink>, 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<DeviceLink> {
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<DeviceLink> = []
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
guard let deviceLink = object as? DeviceLink else { return }