mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.9 KiB
Swift
34 lines
1.9 KiB
Swift
6 years ago
|
|
||
6 years ago
|
extension OWSPrimaryStorage {
|
||
6 years ago
|
|
||
6 years ago
|
private func getCollection(for primaryDevice: String) -> String {
|
||
|
return "LokiMultiDevice-\(primaryDevice)"
|
||
|
}
|
||
|
|
||
|
public func getAuthorisation(forSecondaryDevice secondaryDevice: String, with transaction: YapDatabaseReadTransaction) -> LokiPairingAuthorisation? {
|
||
|
let query = YapDatabaseQuery(string: "WHERE \(PairingAuthorisationsIndex.secondaryDevicePubKey) = ?", parameters: [secondaryDevice])
|
||
|
let authorisations = PairingAuthorisationsIndex.getPairingAuthorisations(with: query, transaction: transaction)
|
||
|
|
||
|
// This should never be the case
|
||
|
if (authorisations.count > 1) { owsFailDebug("[Loki][Multidevice] Found multiple authorisations for secondary device: \(secondaryDevice)") }
|
||
|
|
||
|
return authorisations.first
|
||
|
}
|
||
|
|
||
|
public func createOrUpdatePairingAuthorisation(_ authorisation: LokiPairingAuthorisation, with transaction: YapDatabaseReadWriteTransaction) {
|
||
|
// iOS makes this easy, we can group all authorizations into the primary device collection
|
||
|
// Then we associate an authorisation with the secondary device key
|
||
|
transaction.setObject(authorisation, forKey: authorisation.secondaryDevicePubKey, inCollection: getCollection(for: authorisation.primaryDevicePubKey))
|
||
|
}
|
||
|
|
||
|
public func getSecondaryDevices(forPrimaryDevice primaryDevice: String, with transaction: YapDatabaseReadTransaction) -> [String] {
|
||
|
// primary device collection should have secondary devices as its keys
|
||
|
return transaction.allKeys(inCollection: getCollection(for: primaryDevice))
|
||
|
}
|
||
|
|
||
|
public func getPrimaryDevice(forSecondaryDevice secondaryDevice: String, with transaction: YapDatabaseReadTransaction) -> String? {
|
||
|
let authorisation = getAuthorisation(forSecondaryDevice: secondaryDevice, with: transaction)
|
||
|
return authorisation?.primaryDevicePubKey
|
||
|
}
|
||
|
}
|