mv-experiment #1

Merged
fmodf merged 88 commits from mv-experiment into develop 2024-09-03 15:13:59 +00:00
2 changed files with 69 additions and 38 deletions
Showing only changes of commit fa54db1a10 - Show all commits

View file

@ -208,6 +208,10 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
String(format: "%02x", byte) String(format: "%02x", byte)
}.joined() }.joined()
defer {
_ = self.setStatus(.verifiedActive, forIdentity: identity)
}
return save(address: identity, fingerprint: fingerprint, own: true, data: key.serialized()) return save(address: identity, fingerprint: fingerprint, own: true, data: key.serialized())
} }
@ -231,56 +235,40 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
} }
func setStatus(_ status: MartinOMEMO.IdentityStatus, forIdentity: MartinOMEMO.SignalAddress) -> Bool { func setStatus(_ status: MartinOMEMO.IdentityStatus, forIdentity: MartinOMEMO.SignalAddress) -> Bool {
print(status, forIdentity) if let identity = OMEMOIdentity.getFor(account: credentials.bareJid, name: forIdentity.name, deviceId: forIdentity.deviceId) {
return false return identity.updateStatus(status.rawValue)
} else {
return false
}
} }
func setStatus(active: Bool, forIdentity: MartinOMEMO.SignalAddress) -> Bool { func setStatus(active: Bool, forIdentity: MartinOMEMO.SignalAddress) -> Bool {
print(active, forIdentity) if let identity = OMEMOIdentity.getFor(account: credentials.bareJid, name: forIdentity.name, deviceId: forIdentity.deviceId) {
return false let status = IdentityStatus(rawValue: identity.status) ?? .undecidedActive
return identity.updateStatus(active ? status.toActive().rawValue : status.toInactive().rawValue)
} else {
return false
}
} }
func identities(forName name: String) -> [MartinOMEMO.Identity] { func identities(forName name: String) -> [MartinOMEMO.Identity] {
do { OMEMOIdentity.getAllFor(account: credentials.bareJid, name: name)
return try Database.shared.dbQueue.read { db in .compactMap { identity in
try Row.fetchAll( guard let status = IdentityStatus(rawValue: identity.status) else {
db,
sql: "SELECT * FROM omemo_identities WHERE account = :account AND name = :name",
arguments: ["account": credentials.bareJid, "name": name]
)
}.compactMap { row in
guard
let fingerprint = row["fingerprint"] as? String,
let statusInt = row["status"] as? Int,
let status = MartinOMEMO.IdentityStatus(rawValue: statusInt),
let deviceId = row["device_id"] as? Int32,
let own = row["own"] as? Int,
let key = row["key"] as? Data
else {
return nil return nil
} }
return MartinOMEMO.Identity(address: MartinOMEMO.SignalAddress(name: name, deviceId: deviceId), status: status, fingerprint: fingerprint, key: key, own: own > 0) return MartinOMEMO.Identity(
address: MartinOMEMO.SignalAddress(name: identity.name, deviceId: Int32(identity.deviceId)),
status: status,
fingerprint: identity.fingerprint,
key: identity.key,
own: identity.own
)
} }
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
return []
}
} }
func identityFingerprint(forAddress address: MartinOMEMO.SignalAddress) -> String? { func identityFingerprint(forAddress address: MartinOMEMO.SignalAddress) -> String? {
do { OMEMOIdentity.getFor(account: credentials.bareJid, name: address.name, deviceId: address.deviceId)?.fingerprint
let data = try Database.shared.dbQueue.read { db in
try Row.fetchOne(
db,
sql: "SELECT fingerprint FROM omemo_identities WHERE account = :account AND name = :name AND device_id = :deviceId",
arguments: ["account": credentials.bareJid, "name": address.name, "deviceId": address.deviceId]
)
}
return data?["fingerprint"]
} catch {
logIt(.error, "Error fetching chats: \(error.localizedDescription)")
return nil
}
} }
} }

View file

@ -113,6 +113,20 @@ extension OMEMOIdentity {
} }
} }
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
}
}
static func existsFor(account: String, name: String, fingerprint: String) -> Bool { static func existsFor(account: String, name: String, fingerprint: String) -> Bool {
do { do {
return try Database.shared.dbQueue.read { db in return try Database.shared.dbQueue.read { db in
@ -126,6 +140,35 @@ extension OMEMOIdentity {
return false return false
} }
} }
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 []
}
}
} }
// MARK: - PreKey // MARK: - PreKey