This commit is contained in:
fmodf 2024-08-31 12:10:25 +02:00
parent 9067f3442a
commit 8a0efb9eaf

View file

@ -6,6 +6,9 @@ import MartinOMEMO
final class ClientMartinOMEMO { final class ClientMartinOMEMO {
let credentials: Credentials let credentials: Credentials
private let queue = DispatchQueue(label: "SignalPreKeyRemovalQueue")
private var preKeysMarkedForRemoval: [UInt32] = []
init(_ credentials: Credentials) { init(_ credentials: Credentials) {
self.credentials = credentials self.credentials = credentials
} }
@ -227,48 +230,64 @@ extension ClientMartinOMEMO: SignalPreKeyStoreProtocol {
} }
func deletePreKey(withId: UInt32) -> Bool { func deletePreKey(withId: UInt32) -> Bool {
do { queue.async {
try Database.shared.dbQueue.write { db in print("queueing prekey with id \(withId) for removal..")
try db.execute( self.preKeysMarkedForRemoval.append(withId)
sql: "DELETE FROM omemo_pre_keys WHERE account = :account AND id = :id",
arguments: ["account": credentials.bareJid, "id": withId]
)
}
return true
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
return false
} }
return true
} }
// TODO: Check logic of this function carefully!!! // TODO: Check logic of this function carefully!!!
func flushDeletedPreKeys() -> Bool { func flushDeletedPreKeys() -> Bool {
do { false
try Database.shared.dbQueue.write { db in // !queue.sync { () -> [UInt32] in
try db.execute( // defer {
sql: """ // preKeysMarkedForRemoval.removeAll()
DELETE FROM omemo_pre_keys // }
WHERE account = :account // print("removing queued prekeys: \(preKeysMarkedForRemoval)")
AND id IN ( // do {
SELECT id // Database.shared.dbQueue.write { db in
FROM omemo_pre_keys // try db.execute(
WHERE account = :account // sql: "DETLETE FROM omemo_pre_keys WHERE account = :account AND id IN (:ids)",
AND id NOT IN ( // arguments: ["account": credentials.bareJid, "ids": preKeysMarkedForRemoval]
SELECT id // )
FROM omemo_pre_keys // }
WHERE account = :account // } catch {
ORDER BY id DESC // logIt(.error, "Error fetching chats: \(error.localizedDescription)")
LIMIT 100) // return [0]
) // }
""", //
arguments: ["account": credentials.bareJid] // // return preKeysMarkedForRemoval.filter { id in DBOMEMOStore.instance.deletePreKey(forAccount: context!.sessionObject.userBareJid!, withId: id) }
) // }.isEmpty
} //
return true //
} catch { //
logIt(.error, "Error fetching chats: \(error.localizedDescription)") // do {
return false // try Database.shared.dbQueue.write { db in
} // try db.execute(
// sql: """
// DELETE FROM omemo_pre_keys
// WHERE account = :account
// AND id IN (
// SELECT id
// FROM omemo_pre_keys
// WHERE account = :account
// AND id NOT IN (
// SELECT id
// FROM omemo_pre_keys
// WHERE account = :account
// ORDER BY id DESC
// LIMIT 100)
// )
// """,
// arguments: ["account": credentials.bareJid]
// )
// }
// return true
// } catch {
// logIt(.error, "Error fetching chats: \(error.localizedDescription)")
// return false
// }
} }
func preKeysWipe() { func preKeysWipe() {