Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- Fixed `Channel.name`/`image`/`extraData` setters throwing after a *failed* initialization; they now only throw once the channel is successfully initialized.
- Fixed `Channel.initialized` staying errored after a failed init; it now reflects a subsequent successful (re)initialization.
- Fixed a `StateError` (`Cannot add new events after calling close`) thrown when the client is disposed while a reconnect recovery is still in flight.
- Fixed `Message.deleteMyReaction` dropping an entire reaction group when its summed scores reached zero even though other users' reactions kept the count positive; the group is now retained as long as its count stays above zero.
- Fixed reaction groups synthesized from legacy `reaction_counts`/`reaction_scores` payloads being discarded at parse time when their score total was zero or negative despite a positive count.

## 10.2.0

Expand Down
7 changes: 4 additions & 3 deletions packages/stream_chat/lib/src/core/models/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class Message extends Equatable implements ComparableFieldProvider {
final count = reactionCounts?[type] ?? 0;
final sumScores = reactionScores?[type] ?? 0;

if (count == 0 || sumScores == 0) continue;
// Keep the group while count is positive; score may be zero or negative.
if (count <= 0) continue;
final now = DateTime.timestamp();
groups[type] = {
'count': count,
Expand Down Expand Up @@ -1025,11 +1026,11 @@ extension MessageReactionHelper on Message {
final group = reactionGroups.remove(type);
if (group == null) continue;

// Update the reaction group.
// Keep the group while count is positive; score may be zero or negative.
final updatedCount = group.count - 1;
final updatedSumScores = group.sumScores - reaction.score;

if (updatedCount > 0 && updatedSumScores > 0) {
if (updatedCount > 0) {
reactionGroups[type] = group.copyWith(
count: updatedCount,
sumScores: updatedSumScores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,51 @@ void main() {
// Should have updated the reaction group counts
expect(updatedMessage.reactionGroups, isEmpty);
});

test('should keep reaction group with non-positive score sum while '
'count remains positive', () {
// A positively-scored own reaction and a negatively-scored reaction
// from another user net the group score to zero while count is 2.
final ownReaction = Reaction(
type: 'like',
score: 1,
user: testUser,
userId: testUser.id,
messageId: emptyMessage.id,
);

final otherUser = User(id: 'other-user-id');
final otherReaction = Reaction(
type: 'like',
score: -1,
user: otherUser,
userId: otherUser.id,
messageId: emptyMessage.id,
);
Comment thread
xsahil03x marked this conversation as resolved.

final messageWithReactions = emptyMessage.copyWith(
ownReactions: [ownReaction],
latestReactions: [ownReaction, otherReaction],
reactionGroups: {
'like': ReactionGroup(
count: 2,
sumScores: 0,
firstReactionAt: ownReaction.createdAt,
lastReactionAt: otherReaction.createdAt,
),
},
);

final updatedMessage = messageWithReactions.deleteMyReaction();

// The group must survive because another user's reaction remains, even
// though the remaining summed score is negative.
expect(updatedMessage.reactionGroups!.length, 1);
expect(updatedMessage.reactionGroups!['like']!.count, 1);
expect(updatedMessage.reactionGroups!['like']!.sumScores, -1);
expect(updatedMessage.latestReactions!.length, 1);
expect(updatedMessage.latestReactions!.first.userId, otherUser.id);
});
});
});
}
19 changes: 19 additions & 0 deletions packages/stream_chat/test/src/core/models/message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,25 @@ void main() {
expect(message.reactionGroups!['love']!.sumScores, 5);
},
);

test(
'synthesizes groups from legacy reaction_counts/reaction_scores '
'even when the score total is zero or negative',
() {
final message = Message.fromJson(const {
'reaction_counts': {'like': 2, 'dislike': 3},
'reaction_scores': {'like': 0, 'dislike': -3},
});

// Both groups must be retained because their count is positive, even
// though the summed scores are zero and negative respectively.
expect(message.reactionGroups, isNotNull);
expect(message.reactionGroups!['like']!.count, 2);
expect(message.reactionGroups!['like']!.sumScores, 0);
expect(message.reactionGroups!['dislike']!.count, 3);
expect(message.reactionGroups!['dislike']!.sumScores, -3);
},
);
});
});

Expand Down
Loading