From e4c6d8ccda3a5de4dcdf0b53ca48771ffa4fd146 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 24 Mar 2022 20:46:05 +0530 Subject: [PATCH 1/3] fix(llc): also save reactions while saving threads data in persistence Signed-off-by: xsahil03x --- .../stream_chat/lib/src/client/channel.dart | 8 ++-- .../lib/src/db/chat_persistence_client.dart | 38 +++++++++++++------ 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 25aaa2498..b2887168b 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -2122,12 +2122,12 @@ class ChannelClientState { final BehaviorSubject>> _threadsController = BehaviorSubject.seeded({}); - set _threads(Map> v) { - _channel.client.chatPersistenceClient?.updateMessages( + set _threads(Map> threads) { + _threadsController.add(threads); + _channel.client.chatPersistenceClient?.updateChannelThreads( _channel.cid!, - v.values.expand((v) => v).toList(), + threads, ); - _threadsController.add(v); } /// Channel related typing users last value. diff --git a/packages/stream_chat/lib/src/db/chat_persistence_client.dart b/packages/stream_chat/lib/src/db/chat_persistence_client.dart index 82ba181a2..4e10dae3f 100644 --- a/packages/stream_chat/lib/src/db/chat_persistence_client.dart +++ b/packages/stream_chat/lib/src/db/chat_persistence_client.dart @@ -197,6 +197,22 @@ abstract class ChatPersistenceClient { /// Deletes all the members by channel [cids] Future deleteMembersByCids(List cids); + /// Updates the channel [cid] threads data along with reactions and users. + Future updateChannelThreads( + String cid, + Map> threads, + ) async { + final messages = threads.values.expand((it) => it).toList(); + final reactions = messages.expand(_expandReactions).toList(); + final users = messages.map((it) => it.user).withNullifyer.toList(); + + await Future.wait([ + updateMessages(cid, messages), + updateReactions(reactions), + updateUsers(users), + ]); + } + /// Update the channel state data using [channelState] Future updateChannelState(ChannelState channelState) => updateChannelStates([channelState]); @@ -239,17 +255,8 @@ abstract class ChatPersistenceClient { channelWithMessages[cid] = messages; channelWithPinnedMessages[cid] = pinnedMessages; - List expandReactions(Message message) { - final own = message.ownReactions; - final latest = message.latestReactions; - return [ - if (own != null) ...own.where((r) => r.userId != null), - if (latest != null) ...latest.where((r) => r.userId != null), - ]; - } - - reactions.addAll(messages.expand(expandReactions)); - pinnedReactions.addAll(pinnedMessages.expand(expandReactions)); + reactions.addAll(messages.expand(_expandReactions)); + pinnedReactions.addAll(pinnedMessages.expand(_expandReactions)); users.addAll([ channel.createdBy, @@ -292,4 +299,13 @@ abstract class ChatPersistenceClient { ), ]); } + + List _expandReactions(Message message) { + final own = message.ownReactions; + final latest = message.latestReactions; + return [ + if (own != null) ...own.where((r) => r.userId != null), + if (latest != null) ...latest.where((r) => r.userId != null), + ]; + } } From d924c18a69e3367d30fc7c0c187ae11c5dbdc095 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 24 Mar 2022 20:47:09 +0530 Subject: [PATCH 2/3] chore(llc): update CHANGELOG.md Signed-off-by: xsahil03x --- packages/stream_chat/CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index e054c4201..de19b33fc 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,5 +1,9 @@ ## Upcoming +🐞 Fixed + +- Fixed reactions not working for threads in offline mode. + ✅ Added - Handle `event.message` in `channel.truncate` events @@ -7,6 +11,7 @@ ## 3.5.1 🐞 Fixed + - `channel.unreadCount` was being set as using global unread count on a very specific case. - The reconnection logic for the WebSocket connection is now more robust. @@ -22,7 +27,7 @@ - [[#890]](https://github.com/GetStream/stream-chat-flutter/pull/890) Fixed Reactions not updating on thread messages. Thanks [bstolinski](https://github.com/bstolinski). - [[#897]](https://github.com/GetStream/stream-chat-flutter/issues/897) Fixed error type mis-match in `AuthInterceptor`. -- [[#891]](https://github.com/GetStream/stream-chat-flutter/pull/891) Fixed reply counter for parent message not +- [[#891]](https://github.com/GetStream/stream-chat-flutter/pull/891) Fixed reply counter for parent message not updating correctly after deleting thread message. - Fix `channelState.copyWith` with respect to pinnedMessages. From dc007653d498c62864ca1e003ee1133fc3147b30 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Thu, 24 Mar 2022 22:55:38 +0530 Subject: [PATCH 3/3] test(llc): fix queryChannels test Signed-off-by: xsahil03x --- .../stream_chat/test/src/client/client_test.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index b59937bb4..5ec2bfebb 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -645,8 +645,8 @@ void main() { when(() => persistence.getChannelThreads(any())) .thenAnswer((_) async => {}); - when(() => persistence.updateMessages(any(), any())) - .thenAnswer((_) => Future.value()); + when(() => persistence.updateChannelThreads(any(), any())) + .thenAnswer((_) async => {}); when(() => persistence.getChannelStateByCid(any(), messagePagination: any(named: 'messagePagination'), pinnedMessagePagination: @@ -692,7 +692,7 @@ void main() { verify(() => persistence.getChannelThreads(any())) .called((persistentChannelStates + channelStates).length); - verify(() => persistence.updateMessages(any(), any())) + verify(() => persistence.updateChannelThreads(any(), any())) .called((persistentChannelStates + channelStates).length); verify( () => persistence.getChannelStateByCid(any(), @@ -733,8 +733,8 @@ void main() { when(() => persistence.getChannelThreads(any())) .thenAnswer((_) async => {}); - when(() => persistence.updateMessages(any(), any())) - .thenAnswer((_) => Future.value()); + when(() => persistence.updateChannelThreads(any(), any())) + .thenAnswer((_) async => {}); when(() => persistence.getChannelStateByCid(any(), messagePagination: any(named: 'messagePagination'), pinnedMessagePagination: @@ -775,7 +775,7 @@ void main() { verify(() => persistence.getChannelThreads(any())) .called(persistentChannelStates.length); - verify(() => persistence.updateMessages(any(), any())) + verify(() => persistence.updateChannelThreads(any(), any())) .called(persistentChannelStates.length); verify( () => persistence.getChannelStateByCid(any(),