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
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)
}
}

View file

@ -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
}
}
}