This commit is contained in:
fmodf 2024-08-27 16:57:30 +02:00
parent a6dc50b07e
commit 9d9edb848b

View file

@ -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)
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")
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)
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)
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)
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