From 8a0efb9eaf873f44b293808b9c79ac7b2bc15d23 Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 31 Aug 2024 12:10:25 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 93 +++++++++++-------- 1 file changed, 56 insertions(+), 37 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 9b96b22..9e4e432 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -6,6 +6,9 @@ import MartinOMEMO final class ClientMartinOMEMO { let credentials: Credentials + private let queue = DispatchQueue(label: "SignalPreKeyRemovalQueue") + private var preKeysMarkedForRemoval: [UInt32] = [] + init(_ credentials: Credentials) { self.credentials = credentials } @@ -227,48 +230,64 @@ extension ClientMartinOMEMO: SignalPreKeyStoreProtocol { } func deletePreKey(withId: UInt32) -> Bool { - do { - try Database.shared.dbQueue.write { db in - try db.execute( - 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 + queue.async { + print("queueing prekey with id \(withId) for removal..") + self.preKeysMarkedForRemoval.append(withId) } + return true } // TODO: Check logic of this function carefully!!! func flushDeletedPreKeys() -> Bool { - do { - 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 - } + false + // !queue.sync { () -> [UInt32] in + // defer { + // preKeysMarkedForRemoval.removeAll() + // } + // print("removing queued prekeys: \(preKeysMarkedForRemoval)") + // do { + // Database.shared.dbQueue.write { db in + // try db.execute( + // sql: "DETLETE FROM omemo_pre_keys WHERE account = :account AND id IN (:ids)", + // arguments: ["account": credentials.bareJid, "ids": preKeysMarkedForRemoval] + // ) + // } + // } catch { + // logIt(.error, "Error fetching chats: \(error.localizedDescription)") + // return [0] + // } + // + // // return preKeysMarkedForRemoval.filter { id in DBOMEMOStore.instance.deletePreKey(forAccount: context!.sessionObject.userBareJid!, withId: id) } + // }.isEmpty + // + // + // + // do { + // 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() {