From 9d9edb848b439301366c0a95dd677c2103cd75c8 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 27 Aug 2024 16:57:30 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 145 ++++++++++++++---- 1 file changed, 118 insertions(+), 27 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 5341c9e..b5749f5 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -19,49 +19,131 @@ final class ClientMartinOMEMO { } } -extension ClientMartinOMEMO: SignalSessionStoreProtocol, SignalPreKeyStoreProtocol, SignalSignedPreKeyStoreProtocol, SignalIdentityKeyStoreProtocol, SignalSenderKeyStoreProtocol { +// MARK: - Session +extension ClientMartinOMEMO: SignalSessionStoreProtocol { func sessionRecord(forAddress address: MartinOMEMO.SignalAddress) -> Data? { - // static let omemoSessionRecordLoad = Query("SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId") - - // do { - // let data = try Database.shared.dbQueue.read { db in - // - // try Row.fetchOne(db, sql: "SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId", arguments: ["account": address.account, "name": address.name, "deviceId": address.deviceId]) - // } - // return data?["key"] - // } catch { - // logIt(.error, "Error fetching chats: \(error.localizedDescription)") - // return nil - // } - print(address) - return nil + do { + let data = try Database.shared.dbQueue.read { db in + try Row.fetchOne( + db, + sql: "SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId", + arguments: ["account": credentials.bareJid, "name": address.name, "deviceId": address.deviceId] + ) + } + return data?["key"] + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return nil + } } - func allDevices(for _: String, activeAndTrusted: Bool) -> [Int32] { - print(activeAndTrusted, "allDevices") - return [] + func allDevices(for name: String, activeAndTrusted: Bool) -> [Int32] { + do { + let sql = activeAndTrusted ? + """ + SELECT s.device_id + FROM omemo_sessions s + LEFT JOIN omemo_identities i + ON s.account = i.account + AND s.name = i.name + AND s.device_id = i.device_id + WHERE s.account = :account + AND s.name = :name + AND ((i.status >= 0 AND i.status % 2 = 0) OR i.status IS NULL) + """ + : + "SELECT device_id FROM omemo_sessions WHERE account = :account AND name = :name" + let data = try Database.shared.dbQueue.read { db in + try Row.fetchAll( + db, + sql: sql, + arguments: ["account": credentials.bareJid, "name": name] + ) + } + return data.map { $0["device_id"] } + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return [] + } } func storeSessionRecord(_ data: Data, forAddress: MartinOMEMO.SignalAddress) -> Bool { - print(data, forAddress) - return false + do { + try Database.shared.dbQueue.write { db in + try db.execute( + sql: "INSERT INTO omemo_sessions (account, name, device_id, key) VALUES (:account, :name, :deviceId, :key)", + arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId, "key": data] + ) + } + return true + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return false + } } func containsSessionRecord(forAddress: MartinOMEMO.SignalAddress) -> Bool { - print(forAddress) - return false + do { + let rec = try Database.shared.dbQueue.read { db in + try Row.fetchOne( + db, + sql: "SELECT key FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId", + arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId] + ) + } + return rec != nil + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return false + } } func deleteSessionRecord(forAddress: MartinOMEMO.SignalAddress) -> Bool { - print(forAddress) - return false + do { + try Database.shared.dbQueue.write { db in + try db.execute( + sql: "DELETE FROM omemo_sessions WHERE account = :account AND name = :name AND device_id = :deviceId", + arguments: ["account": credentials.bareJid, "name": forAddress.name, "deviceId": forAddress.deviceId] + ) + } + return true + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return false + } } - func deleteAllSessions(for _: String) -> Bool { - print("deleteAllSessions") - return false + func deleteAllSessions(for name: String) -> Bool { + do { + try Database.shared.dbQueue.write { db in + try db.execute( + sql: "DELETE FROM omemo_sessions WHERE account = :account AND name = :name", + arguments: ["account": credentials.bareJid, "name": name] + ) + } + return true + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return false + } } + func sessionsWipe() { + do { + try Database.shared.dbQueue.write { db in + try db.execute( + sql: "DELETE FROM omemo_sessions WHERE account = :account", + arguments: ["account": credentials.bareJid] + ) + } + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + } + } +} + +// MARK: - PreKey +extension ClientMartinOMEMO: SignalPreKeyStoreProtocol { func currentPreKeyId() -> UInt32 { 0 } @@ -89,7 +171,10 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol, SignalPreKeyStoreProtoc func flushDeletedPreKeys() -> Bool { false } +} +// MARK: - SignedPreKey +extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol { func countSignedPreKeys() -> Int { 0 } @@ -113,7 +198,10 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol, SignalPreKeyStoreProtoc print(withId) return false } +} +// MARK: - Identity +extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? { nil } @@ -161,7 +249,10 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol, SignalPreKeyStoreProtoc print(address) return nil } +} +// MARK: - SenderKey +extension ClientMartinOMEMO: SignalSenderKeyStoreProtocol { func storeSenderKey(_ key: Data, address: MartinOMEMO.SignalAddress?, groupId: String?) -> Bool { print(key, address, groupId) return false