diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 7c36f15..442a841 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -27,14 +27,15 @@ final class ClientMartinOMEMO { OMEMOPreKey.wipe(account: credentials.bareJid) OMEMOSignedPreKey.wipe(account: credentials.bareJid) OMEMOIdentity.wipe(account: credentials.bareJid) - UserSettings.omemoDeviceId = 0 + UserSettings.set(omemoDeviceId: 0, for: credentials.bareJid) } let hasKeyPair = keyPair() != nil - if wipe || UserSettings.omemoDeviceId == 0 || !hasKeyPair { + let deviceId = UserSettings.get(omemoDeviceIdFor: credentials.bareJid) + if wipe || deviceId == 0 || !hasKeyPair { let regId: UInt32 = context.generateRegistrationId() let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId)) - UserSettings.omemoDeviceId = regId + UserSettings.set(omemoDeviceId: regId, for: credentials.bareJid) guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else { return false @@ -45,7 +46,6 @@ final class ClientMartinOMEMO { return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized()) } - print("omemoDeviceId \(UserSettings.omemoDeviceId)") return true } @@ -160,7 +160,7 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol { // MARK: - Identity extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? { - let deviceId = UserSettings.omemoDeviceId + let deviceId = UserSettings.get(omemoDeviceIdFor: credentials.bareJid) guard deviceId != 0 else { return nil } @@ -183,7 +183,7 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { } func localRegistrationId() -> UInt32 { - UserSettings.omemoDeviceId + UserSettings.get(omemoDeviceIdFor: credentials.bareJid) } func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 0794b3b..1724cf8 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -121,7 +121,7 @@ extension Client { } func disconnect() { - _ = connection.disconnect() + _ = connection.disconnect(true) } } diff --git a/ConversationsClassic/AppData/Services/Database+Migrations.swift b/ConversationsClassic/AppData/Services/Database+Migrations.swift index c7bdba5..94d1b0b 100644 --- a/ConversationsClassic/AppData/Services/Database+Migrations.swift +++ b/ConversationsClassic/AppData/Services/Database+Migrations.swift @@ -69,14 +69,14 @@ extension Database { } try db.create(table: "omemo_identities", options: [.ifNotExists]) { table in - table.column("account", .text).notNull() + table.column("account", .text).notNull().collate(.nocase) table.column("name", .text).notNull() table.column("deviceId", .integer).notNull() table.column("fingerprint", .text).notNull() table.column("key", .blob).notNull() table.column("own", .integer).notNull() table.column("status", .integer).notNull() - table.primaryKey(["account", "name", "fingerprint"], onConflict: .ignore) + table.uniqueKey(["account", "name", "fingerprint"], onConflict: .ignore) } try db.create(table: "omemo_pre_keys", options: [.ifNotExists]) { table in diff --git a/ConversationsClassic/AppData/Services/PersistUserSettings.swift b/ConversationsClassic/AppData/Services/PersistUserSettings.swift new file mode 100644 index 0000000..69c38f1 --- /dev/null +++ b/ConversationsClassic/AppData/Services/PersistUserSettings.swift @@ -0,0 +1,61 @@ +import Foundation + +// Wrapper +@propertyWrapper +private struct Storage { + private let key: String + private let defaultValue: T + + init(key: String, defaultValue: T) { + self.key = key + self.defaultValue = defaultValue + } + + var wrappedValue: T { + get { + // Read value from UserDefaults + UserDefaults.standard.object(forKey: key) as? T ?? defaultValue + } + set { + // Set value to UserDefaults + UserDefaults.standard.set(newValue, forKey: key) + UserDefaults.standard.synchronize() + } + } +} + +// Storage +private let kBase = "conversations.classic.user.defaults" +private let kOmemoDevicesIds = "\(kBase).omemoDevicesIds" +private let kSecureChatsByDefault = "\(kBase).secureChatsByDefault" + +enum UserSettings { + @Storage(key: kOmemoDevicesIds, defaultValue: [:]) + private static var omemoDevicesIds: [String: UInt32] + + @Storage(key: kSecureChatsByDefault, defaultValue: false) + private static var vSecureChatsByDefault: Bool +} + +// Public +extension UserSettings { + static func reset() { + omemoDevicesIds = [:] + vSecureChatsByDefault = false + } + + static func set(omemoDeviceId: UInt32, for account: String) { + var dict = UserSettings.omemoDevicesIds + dict[account] = omemoDeviceId + UserSettings.omemoDevicesIds = dict + } + + static func get(omemoDeviceIdFor account: String) -> UInt32 { + UserSettings.omemoDevicesIds[account] ?? 0 + } + + static var secureChatsByDefault: Bool { + get { UserSettings.vSecureChatsByDefault } + set { UserSettings.vSecureChatsByDefault = newValue } + } +} diff --git a/ConversationsClassic/Helpers/UserDefaultsWrapper.swift b/ConversationsClassic/Helpers/UserDefaultsWrapper.swift deleted file mode 100644 index 5898983..0000000 --- a/ConversationsClassic/Helpers/UserDefaultsWrapper.swift +++ /dev/null @@ -1,41 +0,0 @@ -import Foundation - -// Wrapper -@propertyWrapper -struct Storage { - private let key: String - private let defaultValue: T - - init(key: String, defaultValue: T) { - self.key = key - self.defaultValue = defaultValue - } - - var wrappedValue: T { - get { - // Read value from UserDefaults - UserDefaults.standard.object(forKey: key) as? T ?? defaultValue - } - set { - // Set value to UserDefaults - UserDefaults.standard.set(newValue, forKey: key) - } - } -} - -// Storage -private let kOmemoDeviceId = "conversations.classic.user.defaults.omemoDeviceId" -private let kSecureChatsByDefault = "conversations.classic.user.defaults.secureChatsByDefault" - -enum UserSettings { - @Storage(key: kOmemoDeviceId, defaultValue: 0) - static var omemoDeviceId: UInt32 - - @Storage(key: kSecureChatsByDefault, defaultValue: false) - static var secureChatsByDefault: Bool - - static func reset() { - omemoDeviceId = 0 - secureChatsByDefault = false - } -}