diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 1309126..5e4dc22 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -208,6 +208,10 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { String(format: "%02x", byte) }.joined() + defer { + _ = self.setStatus(.verifiedActive, forIdentity: identity) + } + 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 { - print(status, forIdentity) - return false + if let identity = OMEMOIdentity.getFor(account: credentials.bareJid, name: forIdentity.name, deviceId: forIdentity.deviceId) { + return identity.updateStatus(status.rawValue) + } else { + return false + } } func setStatus(active: Bool, forIdentity: MartinOMEMO.SignalAddress) -> Bool { - print(active, forIdentity) - return false + if let identity = OMEMOIdentity.getFor(account: credentials.bareJid, name: forIdentity.name, deviceId: forIdentity.deviceId) { + 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] { - do { - return try Database.shared.dbQueue.read { db in - try Row.fetchAll( - 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 { + OMEMOIdentity.getAllFor(account: credentials.bareJid, name: name) + .compactMap { identity in + guard let status = IdentityStatus(rawValue: identity.status) else { 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? { - do { - 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 - } + OMEMOIdentity.getFor(account: credentials.bareJid, name: address.name, deviceId: address.deviceId)?.fingerprint } } diff --git a/ConversationsClassic/AppData/Model/OMEMO.swift b/ConversationsClassic/AppData/Model/OMEMO.swift index 75a5450..8a2e3c1 100644 --- a/ConversationsClassic/AppData/Model/OMEMO.swift +++ b/ConversationsClassic/AppData/Model/OMEMO.swift @@ -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 { do { return try Database.shared.dbQueue.read { db in @@ -126,6 +140,35 @@ extension OMEMOIdentity { 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