Merge pull request #134 from loki-project/multi-device

Various Multi Device Fixes
pull/135/head
gmbnt 5 years ago committed by GitHub
commit d5d26cc115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -165,9 +165,9 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate {
let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink) let linkingAuthorizationMessage = DeviceLinkingUtilities.getLinkingAuthorizationMessage(for: deviceLink)
ThreadUtil.enqueue(linkingAuthorizationMessage) ThreadUtil.enqueue(linkingAuthorizationMessage)
SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: { SSKEnvironment.shared.messageSender.send(linkingAuthorizationMessage, success: {
SSKEnvironment.shared.syncManager.syncAllContacts() let _ = SSKEnvironment.shared.syncManager.syncAllContacts()
SSKEnvironment.shared.syncManager.syncAllGroups() let _ = SSKEnvironment.shared.syncManager.syncAllGroups()
SSKEnvironment.shared.syncManager.syncAllOpenGroups() let _ = SSKEnvironment.shared.syncManager.syncAllOpenGroups()
let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey) let thread = TSContactThread.getOrCreateThread(contactId: deviceLink.slave.hexEncodedPublicKey)
thread.friendRequestStatus = .friends thread.friendRequestStatus = .friends
thread.save() thread.save()

@ -156,15 +156,6 @@ final class LandingVC : UIViewController, LinkDeviceVCDelegate, DeviceLinkingMod
databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection)
TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey
TSAccountManager.sharedInstance().didRegister() 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 let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.startLongPollerIfNeeded() appDelegate.startLongPollerIfNeeded()
let deviceLinkingModal = DeviceLinkingModal(mode: .slave, delegate: self) let deviceLinkingModal = DeviceLinkingModal(mode: .slave, delegate: self)
@ -173,20 +164,6 @@ final class LandingVC : UIViewController, LinkDeviceVCDelegate, DeviceLinkingMod
self.present(deviceLinkingModal, animated: true, completion: nil) self.present(deviceLinkingModal, animated: true, completion: nil)
let linkingRequestMessage = DeviceLinkingUtilities.getLinkingRequestMessage(for: hexEncodedPublicKey) let linkingRequestMessage = DeviceLinkingUtilities.getLinkingRequestMessage(for: hexEncodedPublicKey)
ThreadUtil.enqueue(linkingRequestMessage) 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)
}
} }
func handleDeviceLinkAuthorized(_ deviceLink: DeviceLink) { func handleDeviceLinkAuthorized(_ deviceLink: DeviceLink) {

@ -140,8 +140,9 @@ final class LinkDeviceVC : UIViewController, UIPageViewControllerDataSource, UIP
} }
fileprivate func requestDeviceLink(with hexEncodedPublicKey: String) { fileprivate func requestDeviceLink(with hexEncodedPublicKey: String) {
delegate?.requestDeviceLink(with: hexEncodedPublicKey) dismiss(animated: true) {
dismiss(animated: true, completion: nil) self.delegate?.requestDeviceLink(with: hexEncodedPublicKey)
}
} }
} }

@ -167,6 +167,7 @@ public final class LokiAPI : NSObject {
lastDeviceLinkUpdate[hexEncodedPublicKey] = Date() lastDeviceLinkUpdate[hexEncodedPublicKey] = Date()
getDestinations() getDestinations()
} else { } else {
print("[Loki] Failed to get device links due to error: \(error).")
seal.reject(error) seal.reject(error)
} }
} }

@ -6,15 +6,7 @@ public extension OWSPrimaryStorage {
} }
public func setDeviceLinks(_ deviceLinks: Set<DeviceLink>, in transaction: YapDatabaseReadWriteTransaction) { public func setDeviceLinks(_ deviceLinks: Set<DeviceLink>, in transaction: YapDatabaseReadWriteTransaction) {
let masterHexEncodedPublicKeys = Set(deviceLinks.map { $0.master.hexEncodedPublicKey }) // FIXME: Clear collections first?
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)
deviceLinks.forEach { addDeviceLink($0, in: transaction) } // TODO: Check the performance impact of this 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> { public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set<DeviceLink> {
let collection = getDeviceLinkCollection(for: masterHexEncodedPublicKey) 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> = [] var result: Set<DeviceLink> = []
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
guard let deviceLink = object as? DeviceLink else { return } guard let deviceLink = object as? DeviceLink else { return }

Loading…
Cancel
Save