mv-experiment #1
|
@ -145,31 +145,124 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol {
|
||||||
// MARK: - PreKey
|
// MARK: - PreKey
|
||||||
extension ClientMartinOMEMO: SignalPreKeyStoreProtocol {
|
extension ClientMartinOMEMO: SignalPreKeyStoreProtocol {
|
||||||
func currentPreKeyId() -> UInt32 {
|
func currentPreKeyId() -> UInt32 {
|
||||||
0
|
do {
|
||||||
|
let data = try Database.shared.dbQueue.read { db in
|
||||||
|
try Row.fetchOne(
|
||||||
|
db,
|
||||||
|
sql: "SELECT max(id) FROM omemo_pre_keys WHERE account = :account",
|
||||||
|
arguments: ["account": credentials.bareJid]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return data?["id"] ?? 0
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPreKey(withId: UInt32) -> Data? {
|
func loadPreKey(withId: UInt32) -> Data? {
|
||||||
print(withId)
|
do {
|
||||||
|
let data = try Database.shared.dbQueue.read { db in
|
||||||
|
try Row.fetchOne(
|
||||||
|
db,
|
||||||
|
sql: "SELECT key FROM omemo_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
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func storePreKey(_ data: Data, withId: UInt32) -> Bool {
|
func storePreKey(_ data: Data, withId: UInt32) -> Bool {
|
||||||
print(data, withId)
|
do {
|
||||||
|
try Database.shared.dbQueue.write { db in
|
||||||
|
try db.execute(
|
||||||
|
sql: "INSERT INTO omemo_pre_keys (account, id, key) VALUES (:account, :id, :key)",
|
||||||
|
arguments: ["account": credentials.bareJid, "id": withId, "key": data]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func containsPreKey(withId: UInt32) -> Bool {
|
func containsPreKey(withId: UInt32) -> Bool {
|
||||||
print(withId)
|
do {
|
||||||
|
let rec = try Database.shared.dbQueue.read { db in
|
||||||
|
try Row.fetchOne(
|
||||||
|
db,
|
||||||
|
sql: "SELECT key FROM omemo_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
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func deletePreKey(withId: UInt32) -> Bool {
|
func deletePreKey(withId: UInt32) -> Bool {
|
||||||
print(withId)
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Check logic of this function carefully!!!
|
||||||
func flushDeletedPreKeys() -> Bool {
|
func flushDeletedPreKeys() -> Bool {
|
||||||
false
|
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() {
|
||||||
|
do {
|
||||||
|
try Database.shared.dbQueue.write { db in
|
||||||
|
try db.execute(
|
||||||
|
sql: "DELETE FROM omemo_pre_keys WHERE account = :account",
|
||||||
|
arguments: ["account": credentials.bareJid]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue