diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 7afc4000..518ed95b 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -23,6 +23,8 @@ - [[#871]](https://github.com/GetStream/stream-chat-flutter/issues/871) Fixed thread message deletion. +- [[#846]](https://github.com/GetStream/stream-chat-flutter/issues/846) Fixed `message.ownReactions` getting truncated when receiving a reaction event. + ## 3.3.1 🐞 Fixed diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 352ce4a7..41197572 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -819,7 +819,7 @@ class Channel { final now = DateTime.now(); final user = _client.state.currentUser; - final latestReactions = [...message.latestReactions ?? []]; + var latestReactions = [...message.latestReactions ?? []]; if (enforceUnique) { latestReactions.removeWhere((it) => it.userId == user!.id); } @@ -833,10 +833,17 @@ class Channel { extraData: extraData, ); - // Inserting at the 0th index as it's the latest reaction - latestReactions.insert(0, newReaction); - final ownReactions = [...latestReactions] - ..removeWhere((it) => it.userId != user!.id); + latestReactions = (latestReactions + // Inserting at the 0th index as it's the latest reaction + ..insert(0, newReaction)) + .take(10) + .toList(); + final ownReactions = enforceUnique + ? [newReaction] + : [ + ...message.ownReactions ?? [], + newReaction, + ]; final newMessage = message.copyWith( reactionCounts: {...message.reactionCounts ?? {}} @@ -876,7 +883,6 @@ class Channel { Reaction reaction, ) async { final type = reaction.type; - final user = _client.state.currentUser; final reactionCounts = {...message.reactionCounts ?? {}}; if (reactionCounts.containsKey(type)) { @@ -893,8 +899,11 @@ class Channel { r.type == reaction.type && r.messageId == reaction.messageId); - final ownReactions = [...latestReactions] - ..removeWhere((it) => it.userId != user!.id); + final ownReactions = message.ownReactions + ?..removeWhere((r) => + r.userId == reaction.userId && + r.type == reaction.type && + r.messageId == reaction.messageId); final newMessage = message.copyWith( reactionCounts: reactionCounts..removeWhere((_, value) => value == 0), @@ -1678,10 +1687,19 @@ class ChannelClientState { void _listenReactionDeleted() { _subscriptions.add(_channel.on(EventType.reactionDeleted).listen((event) { - final userId = _channel.client.state.currentUser!.id; + final oldMessage = + messages.firstWhereOrNull((it) => it.id == event.message?.id); + final reaction = event.reaction; + final ownReactions = oldMessage?.ownReactions + ?.whereNot((it) => + it.type == reaction?.type && + it.score == reaction?.score && + it.messageId == reaction?.messageId && + it.userId == reaction?.userId && + it.extraData == reaction?.extraData) + .toList(growable: false); final message = event.message!.copyWith( - ownReactions: [...event.message!.latestReactions!] - ..removeWhere((it) => it.userId != userId), + ownReactions: ownReactions, ); addMessage(message); })); @@ -1689,10 +1707,10 @@ class ChannelClientState { void _listenReactions() { _subscriptions.add(_channel.on(EventType.reactionNew).listen((event) { - final userId = _channel.client.state.currentUser!.id; + final oldMessage = + messages.firstWhereOrNull((it) => it.id == event.message?.id); final message = event.message!.copyWith( - ownReactions: [...event.message!.latestReactions!] - ..removeWhere((it) => it.userId != userId), + ownReactions: oldMessage?.ownReactions, ); addMessage(message); })); @@ -1705,10 +1723,11 @@ class ChannelClientState { EventType.reactionUpdated, ) .listen((event) { - final userId = _channel.client.state.currentUser!.id; + final oldMessage = + messages.firstWhereOrNull((it) => it.id == event.message?.id); + final message = event.message!.copyWith( - ownReactions: [...event.message!.latestReactions!] - ..removeWhere((it) => it.userId != userId), + ownReactions: oldMessage?.ownReactions, ); addMessage(message);