2024-08-31 10:16:00 +00:00
|
|
|
import Foundation
|
|
|
|
import GRDB
|
|
|
|
import Martin
|
|
|
|
|
|
|
|
// MARK: - Session
|
2024-08-31 10:59:18 +00:00
|
|
|
struct OMEMOSession: DBStorable {
|
2024-08-31 10:16:00 +00:00
|
|
|
static let databaseTableName = "omemo_sessions"
|
|
|
|
|
|
|
|
let account: String
|
|
|
|
let name: String
|
|
|
|
let deviceId: Int
|
|
|
|
let key: String
|
2024-08-31 10:59:18 +00:00
|
|
|
|
|
|
|
var id: String {
|
|
|
|
"\(account)_\(name)_\(deviceId)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension OMEMOSession {
|
|
|
|
static func keyFor(account: String, name: String, deviceId: Int32) -> String? {
|
|
|
|
do {
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try OMEMOSession
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.filter(Column("deviceId") == deviceId)
|
|
|
|
.fetchOne(db)
|
|
|
|
}?.key
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func devicesIdsFor(account: String, name: String) -> [Int32] {
|
|
|
|
do {
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try OMEMOSession
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.fetchAll(db)
|
|
|
|
.map(\.deviceId)
|
|
|
|
}.map { Int32($0) }
|
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func trustedDevicesIdsFor(account: String, name: String) -> [Int32] {
|
|
|
|
do {
|
|
|
|
let sql =
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
"""
|
|
|
|
|
|
|
|
let arguments: StatementArguments = ["account": account, "name": name]
|
|
|
|
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try Int32.fetchAll(db, sql: sql, arguments: arguments)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 12:06:14 +00:00
|
|
|
|
|
|
|
static func wipe(account: String) {
|
|
|
|
do {
|
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
|
|
|
try OMEMOSession
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.deleteAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Failed to wipe OMEMO session: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 10:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Identity
|
2024-08-31 12:06:14 +00:00
|
|
|
struct OMEMOIdentity: DBStorable {
|
2024-08-31 10:16:00 +00:00
|
|
|
static let databaseTableName = "omemo_identities"
|
|
|
|
|
|
|
|
let account: String
|
|
|
|
let name: String
|
|
|
|
let deviceId: Int
|
|
|
|
let fingerprint: String
|
|
|
|
let key: Data
|
|
|
|
let own: Bool
|
|
|
|
let status: Int
|
2024-08-31 12:06:14 +00:00
|
|
|
|
|
|
|
var id: String {
|
|
|
|
"\(account)_\(name)_\(deviceId)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension OMEMOIdentity {
|
|
|
|
static func wipe(account: String) {
|
|
|
|
do {
|
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
|
|
|
try OMEMOIdentity
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.deleteAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Failed to wipe OMEMO identity: \(error)")
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 12:56:36 +00:00
|
|
|
|
2024-08-31 13:49:54 +00:00
|
|
|
static func getFor(account: String, name: String, deviceId: Int32) -> OMEMOIdentity? {
|
|
|
|
do {
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try OMEMOIdentity
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.filter(Column("deviceId") == deviceId)
|
|
|
|
.fetchOne(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:56:36 +00:00
|
|
|
static func existsFor(account: String, name: String, fingerprint: String) -> Bool {
|
|
|
|
do {
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try OMEMOIdentity
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.filter(Column("fingerprint") == fingerprint)
|
|
|
|
.fetchOne(db) != nil
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 13:49:54 +00:00
|
|
|
|
|
|
|
func updateStatus(_ status: Int) -> Bool {
|
|
|
|
do {
|
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
|
|
|
try OMEMOIdentity
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.filter(Column("deviceId") == deviceId)
|
|
|
|
.updateAll(db, Column("status").set(to: status))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Failed to update OMEMO identity status: \(error)")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static func getAllFor(account: String, name: String) -> [OMEMOIdentity] {
|
|
|
|
do {
|
|
|
|
return try Database.shared.dbQueue.read { db in
|
|
|
|
try OMEMOIdentity
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.filter(Column("name") == name)
|
|
|
|
.fetchAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
2024-08-31 10:16:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - PreKey
|
2024-08-31 12:06:14 +00:00
|
|
|
struct OMEMOPreKey: DBStorable {
|
2024-08-31 10:16:00 +00:00
|
|
|
static let databaseTableName = "omemo_pre_keys"
|
|
|
|
|
|
|
|
let account: String
|
|
|
|
let id: Int
|
|
|
|
let key: Data
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:06:14 +00:00
|
|
|
extension OMEMOPreKey {
|
|
|
|
static func wipe(account: String) {
|
|
|
|
do {
|
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
|
|
|
try OMEMOPreKey
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.deleteAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Failed to wipe OMEMO pre key: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-31 10:16:00 +00:00
|
|
|
// MARK: - SignedPreKey
|
2024-08-31 12:06:14 +00:00
|
|
|
struct OMEMOSignedPreKey: DBStorable {
|
2024-08-31 10:16:00 +00:00
|
|
|
static let databaseTableName = "omemo_signed_pre_keys"
|
|
|
|
|
|
|
|
let account: String
|
|
|
|
let id: Int
|
|
|
|
let key: Data
|
|
|
|
}
|
2024-08-31 12:06:14 +00:00
|
|
|
|
|
|
|
extension OMEMOSignedPreKey {
|
|
|
|
static func wipe(account: String) {
|
|
|
|
do {
|
|
|
|
_ = try Database.shared.dbQueue.write { db in
|
|
|
|
try OMEMOSignedPreKey
|
|
|
|
.filter(Column("account") == account)
|
|
|
|
.deleteAll(db)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logIt(.error, "Failed to wipe OMEMO signed pre key: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|