From 3ac81fb3d7ec746dad9557122344820820929bf7 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 31 Aug 2024 18:08:10 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 78 ++++--------------- .../AppData/Model/OMEMO.swift | 25 ++++++ 2 files changed, 42 insertions(+), 61 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 40a4a97..1a26bec 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -321,94 +321,50 @@ extension ClientMartinOMEMO: SignalPreKeyStoreProtocol { // MARK: - SignedPreKey extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol { func countSignedPreKeys() -> Int { - do { - 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 - } + OMEMOSignedPreKey.countsFor(account: credentials.bareJid) } func loadSignedPreKey(withId: UInt32) -> Data? { - do { - 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 - } + OMEMOSignedPreKey.keyFor(account: credentials.bareJid, id: withId) } func storeSignedPreKey(_ data: Data, withId: UInt32) -> Bool { do { - try Database.shared.dbQueue.write { db in - try db.execute( - sql: "INSERT INTO omemo_signed_pre_keys (account, id, key) VALUES (:account, :id, :key)", - arguments: ["account": credentials.bareJid, "id": withId, "key": data] - ) + _ = try Database.shared.dbQueue.write { db in + try OMEMOSignedPreKey( + account: credentials.bareJid, + id: Int(withId), + key: data + ).insert(db) } return true } catch { - logIt(.error, "Error fetching chats: \(error.localizedDescription)") + logIt(.error, "Error storing signed pre key: \(error.localizedDescription)") return false } } func containsSignedPreKey(withId: UInt32) -> Bool { - do { - 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 - } + OMEMOSignedPreKey.keyFor(account: credentials.bareJid, id: withId) != nil } func deleteSignedPreKey(withId: UInt32) -> Bool { do { - try Database.shared.dbQueue.write { db in - try db.execute( - sql: "DELETE FROM omemo_signed_pre_keys WHERE account = :account AND id = :id", - arguments: ["account": credentials.bareJid, "id": withId] - ) + _ = try Database.shared.dbQueue.write { db in + try OMEMOSignedPreKey + .filter(Column("account") == credentials.bareJid) + .filter(Column("id") == withId) + .deleteAll(db) } return true } catch { - logIt(.error, "Error fetching chats: \(error.localizedDescription)") + logIt(.error, "Error deleting signed pre key: \(error.localizedDescription)") return false } } func wipeSignedPreKeys() { - do { - 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)") - } + OMEMOSignedPreKey.wipe(account: credentials.bareJid) } } diff --git a/ConversationsClassic/AppData/Model/OMEMO.swift b/ConversationsClassic/AppData/Model/OMEMO.swift index 40ead87..5a91842 100644 --- a/ConversationsClassic/AppData/Model/OMEMO.swift +++ b/ConversationsClassic/AppData/Model/OMEMO.swift @@ -286,4 +286,29 @@ extension OMEMOSignedPreKey { 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 + } + } }