diff --git a/ConversationsClassic/AppData/Client/Client.swift b/ConversationsClassic/AppData/Client/Client.swift index 3f2b6a6..48c5a1c 100644 --- a/ConversationsClassic/AppData/Client/Client.swift +++ b/ConversationsClassic/AppData/Client/Client.swift @@ -77,6 +77,18 @@ extension Client { ) } + func addRosterLocally(_ jid: String, name: String?, groups: [String]) async throws { + try await Roster.addRosterLocally(.init( + bareJid: credentials.bareJid, + contactBareJid: jid, + name: name, + subscription: "to", + ask: true, + data: .init(groups: groups, annotations: []), + locallyDeleted: false + )) + } + func deleteRoster(_ roster: Roster) async throws { _ = try await connection.module(.roster).removeItem(jid: JID(roster.contactBareJid)) } diff --git a/ConversationsClassic/AppData/Model/Roster.swift b/ConversationsClassic/AppData/Model/Roster.swift index 9909704..f7ec646 100644 --- a/ConversationsClassic/AppData/Model/Roster.swift +++ b/ConversationsClassic/AppData/Model/Roster.swift @@ -101,4 +101,10 @@ extension Roster { } } } + + static func addRosterLocally(_ roster: Roster) async throws { + try await Database.shared.dbQueue.write { db in + try roster.save(db) + } + } } diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 58dc65c..6e1f159 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -113,6 +113,9 @@ extension ClientsStore { throw AppError.clientNotFound } try await addRoster(client.credentials, contactJID: chat.participant, name: nil, groups: []) + // Hack here. Because we want to show chat immediately after adding roster (without waiting for server + // response and update rosters list) we need to write it to db manually + try await client.addRosterLocally(chat.participant, name: nil, groups: []) } } }