This commit is contained in:
fmodf 2024-08-31 18:08:10 +02:00
parent 28fba4e639
commit 3ac81fb3d7
2 changed files with 42 additions and 61 deletions

View file

@ -321,94 +321,50 @@ extension ClientMartinOMEMO: SignalPreKeyStoreProtocol {
// MARK: - SignedPreKey // MARK: - SignedPreKey
extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol { extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol {
func countSignedPreKeys() -> Int { func countSignedPreKeys() -> Int {
do { OMEMOSignedPreKey.countsFor(account: credentials.bareJid)
let data = try Database.shared.dbQueue.read { db in
try Row.fetchOne(
db,
sql: "SELECT count(1) FROM omemo_signed_pre_keys WHERE account = :account",
arguments: ["account": credentials.bareJid]
)
}
return data?["count(1)"] ?? 0
} catch {
logIt(.error, "Error signed pre keys counting: \(error.localizedDescription)")
return 0
}
} }
func loadSignedPreKey(withId: UInt32) -> Data? { func loadSignedPreKey(withId: UInt32) -> Data? {
do { OMEMOSignedPreKey.keyFor(account: credentials.bareJid, id: withId)
let data = try Database.shared.dbQueue.read { db in
try Row.fetchOne(
db,
sql: "SELECT key FROM omemo_signed_pre_keys WHERE account = :account AND id = :id",
arguments: ["account": credentials.bareJid, "id": withId]
)
}
return data?["key"]
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
return nil
}
} }
func storeSignedPreKey(_ data: Data, withId: UInt32) -> Bool { func storeSignedPreKey(_ data: Data, withId: UInt32) -> Bool {
do { do {
try Database.shared.dbQueue.write { db in _ = try Database.shared.dbQueue.write { db in
try db.execute( try OMEMOSignedPreKey(
sql: "INSERT INTO omemo_signed_pre_keys (account, id, key) VALUES (:account, :id, :key)", account: credentials.bareJid,
arguments: ["account": credentials.bareJid, "id": withId, "key": data] id: Int(withId),
) key: data
).insert(db)
} }
return true return true
} catch { } catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)") logIt(.error, "Error storing signed pre key: \(error.localizedDescription)")
return false return false
} }
} }
func containsSignedPreKey(withId: UInt32) -> Bool { func containsSignedPreKey(withId: UInt32) -> Bool {
do { OMEMOSignedPreKey.keyFor(account: credentials.bareJid, id: withId) != nil
let rec = try Database.shared.dbQueue.read { db in
try Row.fetchOne(
db,
sql: "SELECT key FROM omemo_signed_pre_keys WHERE account = :account AND id = :id",
arguments: ["account": credentials.bareJid, "id": withId]
)
}
return rec != nil
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
return false
}
} }
func deleteSignedPreKey(withId: UInt32) -> Bool { func deleteSignedPreKey(withId: UInt32) -> Bool {
do { do {
try Database.shared.dbQueue.write { db in _ = try Database.shared.dbQueue.write { db in
try db.execute( try OMEMOSignedPreKey
sql: "DELETE FROM omemo_signed_pre_keys WHERE account = :account AND id = :id", .filter(Column("account") == credentials.bareJid)
arguments: ["account": credentials.bareJid, "id": withId] .filter(Column("id") == withId)
) .deleteAll(db)
} }
return true return true
} catch { } catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)") logIt(.error, "Error deleting signed pre key: \(error.localizedDescription)")
return false return false
} }
} }
func wipeSignedPreKeys() { func wipeSignedPreKeys() {
do { OMEMOSignedPreKey.wipe(account: credentials.bareJid)
try Database.shared.dbQueue.write { db in
try db.execute(
sql: "DELETE FROM omemo_signed_pre_keys WHERE account = :account",
arguments: ["account": credentials.bareJid]
)
}
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
}
} }
} }

View file

@ -286,4 +286,29 @@ extension OMEMOSignedPreKey {
logIt(.error, "Failed to wipe OMEMO signed pre key: \(error)") logIt(.error, "Failed to wipe OMEMO signed pre key: \(error)")
} }
} }
static func countsFor(account: String) -> Int {
do {
return try Database.shared.dbQueue.read { db in
try OMEMOSignedPreKey
.filter(Column("account") == account)
.fetchCount(db)
}
} catch {
return 0
}
}
static func keyFor(account: String, id: UInt32) -> Data? {
do {
return try Database.shared.dbQueue.read { db in
try OMEMOSignedPreKey
.filter(Column("account") == account)
.filter(Column("id") == id)
.fetchOne(db)
}?.key
} catch {
return nil
}
}
} }