Skip to content

Commit

Permalink
Merge pull request #1043 from GetStream/fix/thread-offline-reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
imtoori authored Mar 25, 2022
2 parents 2bde45e + dc00765 commit 981389e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
7 changes: 6 additions & 1 deletion packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
## Upcoming

🐞 Fixed

- Fixed reactions not working for threads in offline mode.

✅ Added

- Handle `event.message` in `channel.truncate` events

## 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.

Expand All @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions packages/stream_chat/lib/src/client/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2122,12 +2122,12 @@ class ChannelClientState {
final BehaviorSubject<Map<String, List<Message>>> _threadsController =
BehaviorSubject.seeded({});

set _threads(Map<String, List<Message>> v) {
_channel.client.chatPersistenceClient?.updateMessages(
set _threads(Map<String, List<Message>> 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.
Expand Down
38 changes: 27 additions & 11 deletions packages/stream_chat/lib/src/db/chat_persistence_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ abstract class ChatPersistenceClient {
/// Deletes all the members by channel [cids]
Future<void> deleteMembersByCids(List<String> cids);

/// Updates the channel [cid] threads data along with reactions and users.
Future<void> updateChannelThreads(
String cid,
Map<String, List<Message>> 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<void> updateChannelState(ChannelState channelState) =>
updateChannelStates([channelState]);
Expand Down Expand Up @@ -239,17 +255,8 @@ abstract class ChatPersistenceClient {
channelWithMessages[cid] = messages;
channelWithPinnedMessages[cid] = pinnedMessages;

List<Reaction> 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,
Expand Down Expand Up @@ -292,4 +299,13 @@ abstract class ChatPersistenceClient {
),
]);
}

List<Reaction> _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),
];
}
}
12 changes: 6 additions & 6 deletions packages/stream_chat/test/src/client/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 981389e

Please sign in to comment.