mirror of https://github.com/oxen-io/session-ios
Further build out API
parent
7fe7245c64
commit
10a6c5e2ea
@ -1,6 +1,6 @@
|
||||
|
||||
public protocol LokiDeviceLinkingSessionDelegate {
|
||||
|
||||
func authorizeDeviceLinkIfValid(_ deviceLink: LokiDeviceLink)
|
||||
func requestUserAuthorization(for deviceLink: LokiDeviceLink)
|
||||
func handleDeviceLinkingSessionTimeout()
|
||||
}
|
||||
|
@ -1,33 +1,22 @@
|
||||
|
||||
extension OWSPrimaryStorage {
|
||||
|
||||
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 storeDeviceLink(_ deviceLink: LokiDeviceLink, in transaction: YapDatabaseReadWriteTransaction) {
|
||||
let collection = getCollection(for: deviceLink.master.hexEncodedPublicKey)
|
||||
transaction.setObject(deviceLink, forKey: deviceLink.slave.hexEncodedPublicKey, inCollection: collection)
|
||||
}
|
||||
|
||||
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 getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> [LokiDeviceLink] {
|
||||
let collection = getCollection(for: masterHexEncodedPublicKey)
|
||||
var result: [LokiDeviceLink] = []
|
||||
transaction.enumerateRows(inCollection: collection) { _, object, _, _ in
|
||||
guard let deviceLink = object as? LokiDeviceLink else { return }
|
||||
result.append(deviceLink)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
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
|
||||
private func getCollection(for primaryDevice: String) -> String {
|
||||
return "LokiDeviceLinkCollection-\(primaryDevice)"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue