From 538fdd6bc58daf7ac4d0bfc0624424cf984e079f Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 12:15:55 +0530 Subject: [PATCH 1/6] Exposed score in the sendReaction function for channel --- packages/stream_chat/lib/src/client/channel.dart | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index 945574fd..fe25626e 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -811,6 +811,7 @@ class Channel { Future sendReaction( Message message, String type, { + int? score, Map extraData = const {}, bool enforceUnique = false, }) async { @@ -862,6 +863,13 @@ class Channel { state?.addMessage(newMessage); + if (score != null) { + extraData.putIfAbsent( + 'score', + () => score, + ); + } + try { final reactionResp = await _client.sendReaction( messageId, From 3fc9aabe6f0e5b5cfb7172c1a6a23f11e34b30a8 Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 14:05:48 +0530 Subject: [PATCH 2/6] Exposed score for send reaction in the client side as well --- .../stream_chat/lib/src/client/channel.dart | 12 +++-------- .../stream_chat/lib/src/client/client.dart | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/stream_chat/lib/src/client/channel.dart b/packages/stream_chat/lib/src/client/channel.dart index fe25626e..fdd1d1d6 100644 --- a/packages/stream_chat/lib/src/client/channel.dart +++ b/packages/stream_chat/lib/src/client/channel.dart @@ -811,7 +811,7 @@ class Channel { Future sendReaction( Message message, String type, { - int? score, + int score = 1, Map extraData = const {}, bool enforceUnique = false, }) async { @@ -830,7 +830,7 @@ class Channel { createdAt: now, type: type, user: user, - score: 1, + score: score, extraData: extraData, ); @@ -863,17 +863,11 @@ class Channel { state?.addMessage(newMessage); - if (score != null) { - extraData.putIfAbsent( - 'score', - () => score, - ); - } - try { final reactionResp = await _client.sendReaction( messageId, type, + score: score, extraData: extraData, enforceUnique: enforceUnique, ); diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index a9486c08..f44f3d52 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -1157,15 +1157,22 @@ class StreamChatClient { Future sendReaction( String messageId, String reactionType, { + int score = 1, Map extraData = const {}, bool enforceUnique = false, - }) => - _chatApi.message.sendReaction( - messageId, - reactionType, - extraData: extraData, - enforceUnique: enforceUnique, - ); + }) { + extraData.putIfAbsent( + 'score', + () => score, + ); + + return _chatApi.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + enforceUnique: enforceUnique, + ); + } /// Delete a [reactionType] from this [messageId] Future deleteReaction( From 6fa746fec350b8508111498bc92e17bf550828b4 Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 18:51:06 +0530 Subject: [PATCH 3/6] Fixed unmodifiable const map bug and added client and channel tests --- .../stream_chat/lib/src/client/client.dart | 10 +- .../test/src/client/channel_test.dart | 129 ++++++++++++++++++ .../test/src/client/client_test.dart | 114 ++++++++++++++-- 3 files changed, 234 insertions(+), 19 deletions(-) diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index f44f3d52..9c593694 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -1161,15 +1161,15 @@ class StreamChatClient { Map extraData = const {}, bool enforceUnique = false, }) { - extraData.putIfAbsent( - 'score', - () => score, - ); + final _extraData = { + 'score': score, + ...extraData, + }; return _chatApi.message.sendReaction( messageId, reactionType, - extraData: extraData, + extraData: _extraData, enforceUnique: enforceUnique, ); } diff --git a/packages/stream_chat/test/src/client/channel_test.dart b/packages/stream_chat/test/src/client/channel_test.dart index adbfb388..cc89c44b 100644 --- a/packages/stream_chat/test/src/client/channel_test.dart +++ b/packages/stream_chat/test/src/client/channel_test.dart @@ -1116,6 +1116,135 @@ void main() { verify(() => client.sendReaction(message.id, type)).called(1); }); + test('should work fine with score passed explicitly', () async { + const type = 'test-reaction-type'; + final message = Message(id: 'test-message-id'); + + const score = 5; + final reaction = Reaction( + type: type, + messageId: message.id, + score: score, + ); + + when(() => client.sendReaction( + message.id, + type, + score: score, + )).thenAnswer( + (_) async => SendReactionResponse() + ..message = message + ..reaction = reaction, + ); + + expectLater( + // skipping first seed message list -> [] messages + channel.state?.messagesStream.skip(1), + emitsInOrder([ + [ + isSameMessageAs( + message.copyWith( + status: MessageSendingStatus.sent, + reactionCounts: {type: 1}, + reactionScores: {type: score}, + latestReactions: [reaction], + ownReactions: [reaction], + ), + matchReactions: true, + matchSendingStatus: true, + ), + ], + ]), + ); + + final res = await channel.sendReaction( + message, + type, + score: score, + ); + + expect(res, isNotNull); + expect(res.reaction.type, type); + expect(res.reaction.messageId, message.id); + expect(res.reaction.score, score); + + verify(() => client.sendReaction( + message.id, + type, + score: score, + )).called(1); + }); + + test('should work fine with score passed explicitly and in extraData', + () async { + const type = 'test-reaction-type'; + final message = Message(id: 'test-message-id'); + + const score = 5; + const extraDataScore = 3; + const extraData = { + 'score': extraDataScore, + }; + final reaction = Reaction( + type: type, + messageId: message.id, + score: extraDataScore, + ); + + when(() => client.sendReaction( + message.id, + type, + score: score, + extraData: extraData, + )).thenAnswer( + (_) async => SendReactionResponse() + ..message = message + ..reaction = reaction, + ); + + expectLater( + // skipping first seed message list -> [] messages + channel.state?.messagesStream.skip(1), + emitsInOrder([ + [ + isSameMessageAs( + message.copyWith( + status: MessageSendingStatus.sent, + reactionCounts: {type: 1}, + reactionScores: {type: extraDataScore}, + latestReactions: [reaction], + ownReactions: [reaction], + ), + matchReactions: true, + matchSendingStatus: true, + ), + ], + ]), + ); + + final res = await channel.sendReaction( + message, + type, + score: score, + extraData: extraData, + ); + + expect(res, isNotNull); + expect(res.reaction.type, type); + expect(res.reaction.messageId, message.id); + expect( + res.reaction.score, + extraDataScore, + ); + + verify(() => client.sendReaction( + message.id, + type, + score: score, + extraData: extraData, + )).called(1); + }); + test( 'should restore previous message if `client.sendReaction` throws', () async { diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index b89db0dc..c9aa8968 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -1956,23 +1956,109 @@ void main() { verifyNoMoreInteractions(api.channel); }); - test('`.sendReaction`', () async { - const messageId = 'test-message-id'; - const reactionType = 'like'; + group('`.sendReaction`', () { + test('`.sendReaction with default params`', () async { + const messageId = 'test-message-id'; + const reactionType = 'like'; + const extraData = {'score': 1}; - when(() => api.message.sendReaction(messageId, reactionType)) - .thenAnswer((_) async => SendReactionResponse() - ..message = Message(id: messageId) - ..reaction = Reaction(type: reactionType, messageId: messageId)); + when(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).thenAnswer((_) async => SendReactionResponse() + ..message = Message(id: messageId) + ..reaction = Reaction(type: reactionType, messageId: messageId)); - final res = await client.sendReaction(messageId, reactionType); - expect(res, isNotNull); - expect(res.message.id, messageId); - expect(res.reaction.type, reactionType); - expect(res.reaction.messageId, messageId); + final res = await client.sendReaction(messageId, reactionType); + expect(res, isNotNull); + expect(res.message.id, messageId); + expect(res.reaction.type, reactionType); + expect(res.reaction.messageId, messageId); - verify(() => api.message.sendReaction(messageId, reactionType)).called(1); - verifyNoMoreInteractions(api.message); + verify(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).called(1); + verifyNoMoreInteractions(api.message); + }); + + test('`.sendReaction with score`', () async { + const messageId = 'test-message-id'; + const reactionType = 'like'; + const score = 3; + const extraData = {'score': score}; + + when(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).thenAnswer((_) async => SendReactionResponse() + ..message = Message(id: messageId) + ..reaction = Reaction( + type: reactionType, + messageId: messageId, + score: score, + )); + + final res = await client.sendReaction( + messageId, + reactionType, + score: score, + ); + expect(res, isNotNull); + expect(res.message.id, messageId); + expect(res.reaction.type, reactionType); + expect(res.reaction.messageId, messageId); + expect(res.reaction.score, score); + + verify(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).called(1); + verifyNoMoreInteractions(api.message); + }); + + test('`.sendReaction with score passed in extradata also`', () async { + const messageId = 'test-message-id'; + const reactionType = 'like'; + const score = 3; + const extraDataScore = 5; + const extraData = {'score': extraDataScore}; + + when(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).thenAnswer((_) async => SendReactionResponse() + ..message = Message(id: messageId) + ..reaction = Reaction( + type: reactionType, + messageId: messageId, + score: extraDataScore, + )); + + final res = await client.sendReaction( + messageId, + reactionType, + score: score, + extraData: extraData, + ); + expect(res, isNotNull); + expect(res.message.id, messageId); + expect(res.reaction.type, reactionType); + expect(res.reaction.messageId, messageId); + expect(res.reaction.score, extraDataScore); + + verify(() => api.message.sendReaction( + messageId, + reactionType, + extraData: extraData, + )).called(1); + verifyNoMoreInteractions(api.message); + }); }); test('`.deleteReaction`', () async { From 392a5ffa531bb568f249881e28e006cf84bc9bee Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 19:13:34 +0530 Subject: [PATCH 4/6] Made an entry into stream_chat changelog --- packages/stream_chat/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 787aa2af..ece8a30e 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,7 @@ +## Upcoming + +- You can now pass `score` to `client.sendReaction` and `channel.sendReaction` functions + ## 3.4.0 🐞 Fixed From 0db466b5b3614f67d48890d4aab4e612bbaa377a Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 19:21:20 +0530 Subject: [PATCH 5/6] Changelog addition --- packages/stream_chat/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 8226e977..ee554704 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,5 +1,7 @@ ## Upcoming +✅ Added + - You can now pass `score` to `client.sendReaction` and `channel.sendReaction` functions 🐞 Fixed From 6a4caff89783aeec1ec9d80ea1843c66b4e1a377 Mon Sep 17 00:00:00 2001 From: Ayush Shekhar Date: Wed, 16 Feb 2022 19:25:50 +0530 Subject: [PATCH 6/6] Removed extra - from changelog --- packages/stream_chat/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index ee554704..91077303 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -7,7 +7,6 @@ 🐞 Fixed - [[#890]](https://github.com/GetStream/stream-chat-flutter/pull/890). Fixed Reactions not updating on thread messages. Thanks [bstolinski](https://github.com/bstolinski). -- ## 3.4.0 🐞 Fixed