From d4b9a10e54c5faa3132a81900ea4e11ba731c74e Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 18 Feb 2022 17:12:27 +0530 Subject: [PATCH 1/3] feat(llc): add support for `partialUserUpdate` endpoint. Signed-off-by: xsahil03x --- .../stream_chat/lib/src/client/client.dart | 22 ++++++++++ .../lib/src/core/api/requests.dart | 26 ++++++++++++ .../lib/src/core/api/requests.g.dart | 11 +++++ .../lib/src/core/api/user_api.dart | 13 ++++++ .../lib/src/core/models/channel_state.g.dart | 2 +- .../test/src/client/client_test.dart | 40 +++++++++++++++++++ 6 files changed, 113 insertions(+), 1 deletion(-) diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index 9c593694..c3605ac5 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -1073,6 +1073,28 @@ class StreamChatClient { Future updateUsers(List users) => _chatApi.user.updateUsers(users); + /// Partially update the given user with [id]. + /// Use [set] to define values to be set. + /// Use [unset] to define values to be unset. + Future partialUpdateUser( + String id, { + Map? set, + List? unset, + }) { + final user = PartialUpdateUserRequest( + id: id, + set: set, + unset: unset, + ); + return partialUpdateUsers([user]); + } + + /// Batch partial updates the [users]. + Future partialUpdateUsers( + List users, + ) => + _chatApi.user.partialUpdateUsers(users); + /// Bans a user from all channels Future banUser( String targetUserId, [ diff --git a/packages/stream_chat/lib/src/core/api/requests.dart b/packages/stream_chat/lib/src/core/api/requests.dart index 6f00b374..3f4e79de 100644 --- a/packages/stream_chat/lib/src/core/api/requests.dart +++ b/packages/stream_chat/lib/src/core/api/requests.dart @@ -156,3 +156,29 @@ class PaginationParams extends Equatable { lessThanOrEqual, ]; } + +/// Request model for the [client.partialUpdateUser] api call. +@JsonSerializable(createFactory: false) +class PartialUpdateUserRequest extends Equatable { + /// Creates a new PartialUpdateUserRequest instance. + const PartialUpdateUserRequest({ + required this.id, + this.set, + this.unset, + }); + + /// User ID. + final String id; + + /// Fields to set. + final Map? set; + + /// Fields to unset. + final List? unset; + + /// Serialize model to json + Map toJson() => _$PartialUpdateUserRequestToJson(this); + + @override + List get props => [id, set, unset]; +} diff --git a/packages/stream_chat/lib/src/core/api/requests.g.dart b/packages/stream_chat/lib/src/core/api/requests.g.dart index 7d45ee86..62ec12b3 100644 --- a/packages/stream_chat/lib/src/core/api/requests.g.dart +++ b/packages/stream_chat/lib/src/core/api/requests.g.dart @@ -54,3 +54,14 @@ Map _$PaginationParamsToJson(PaginationParams instance) { writeNotNull('id_lte', instance.lessThanOrEqual); return val; } + +Map _$PartialUpdateUserRequestToJson( + PartialUpdateUserRequest instance) => + { + 'stringify': instance.stringify, + 'hash_code': instance.hashCode, + 'id': instance.id, + 'set': instance.set, + 'unset': instance.unset, + 'props': instance.props, + }; diff --git a/packages/stream_chat/lib/src/core/api/user_api.dart b/packages/stream_chat/lib/src/core/api/user_api.dart index 61159731..916976c8 100644 --- a/packages/stream_chat/lib/src/core/api/user_api.dart +++ b/packages/stream_chat/lib/src/core/api/user_api.dart @@ -46,4 +46,17 @@ class UserApi { ); return UpdateUsersResponse.fromJson(response.data); } + + /// Batch partial update of [users]. + Future partialUpdateUsers( + List users, + ) async { + final response = await _client.patch( + '/users', + data: { + 'users': users, + }, + ); + return UpdateUsersResponse.fromJson(response.data); + } } diff --git a/packages/stream_chat/lib/src/core/models/channel_state.g.dart b/packages/stream_chat/lib/src/core/models/channel_state.g.dart index 0da51768..5100932c 100644 --- a/packages/stream_chat/lib/src/core/models/channel_state.g.dart +++ b/packages/stream_chat/lib/src/core/models/channel_state.g.dart @@ -21,7 +21,7 @@ ChannelState _$ChannelStateFromJson(Map json) => ChannelState( pinnedMessages: (json['pinned_messages'] as List?) ?.map((e) => Message.fromJson(e as Map)) .toList() ?? - const [], + _emptyPinnedMessages, watcherCount: json['watcher_count'] as int?, watchers: (json['watchers'] as List?) ?.map((e) => User.fromJson(e as Map)) diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index c9aa8968..b59937bb 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -1772,6 +1772,46 @@ void main() { verifyNoMoreInteractions(api.user); }); + test('`.partialUpdateUser`', () async { + const userId = 'test-user-id'; + + final set = {'color': 'yellow'}; + final unset = []; + + final partialUpdateRequest = PartialUpdateUserRequest( + id: userId, + set: set, + unset: unset, + ); + + final updatedUser = User( + id: userId, + extraData: {'color': set['color']}, + ); + + when(() => api.user.partialUpdateUsers([partialUpdateRequest])) + .thenAnswer( + (_) async => UpdateUsersResponse() + ..users = { + updatedUser.id: updatedUser, + }, + ); + + final res = await client.partialUpdateUser( + userId, + set: set, + unset: unset, + ); + + expect(res, isNotNull); + expect(res.users, {updatedUser.id: updatedUser}); + + verify( + () => api.user.partialUpdateUsers([partialUpdateRequest]), + ).called(1); + verifyNoMoreInteractions(api.user); + }); + test('`.banUser`', () async { const userId = 'test-user-id'; From 3886f10387e436bd1a8775f3b10cfe5e49de6a78 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 18 Feb 2022 17:13:36 +0530 Subject: [PATCH 2/3] chore(llc): update CHANGELOG.md Signed-off-by: xsahil03x --- packages/stream_chat/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index 0c35a7a3..82225b27 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -2,7 +2,8 @@ ✅ Added -- You can now pass `score` to `client.sendReaction` and `channel.sendReaction` functions +- You can now pass `score` to `client.sendReaction` and `channel.sendReaction` functions. +- Added new `client.partialUpdateUsers` function in order to partially update users. 🐞 Fixed From adf7befd6894392356f1ba9dcd90e495849d2aed Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Fri, 18 Feb 2022 17:54:49 +0530 Subject: [PATCH 3/3] test(llc): add `userApi.partialUpdateUsers` test. Signed-off-by: xsahil03x --- .../test/src/core/api/user_api_test.dart | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/stream_chat/test/src/core/api/user_api_test.dart b/packages/stream_chat/test/src/core/api/user_api_test.dart index dbf83f2c..764386f8 100644 --- a/packages/stream_chat/test/src/core/api/user_api_test.dart +++ b/packages/stream_chat/test/src/core/api/user_api_test.dart @@ -82,4 +82,37 @@ void main() { verify(() => client.post(path, data: any(named: 'data'))).called(1); verifyNoMoreInteractions(client); }); + + test('partialUpdateUsers', () async { + const user = PartialUpdateUserRequest( + id: 'test-user-id', + set: {'color': 'yellow'}, + ); + + const path = '/users'; + + final updatedUser = {user.id: User(id: user.id, extraData: user.set!)}; + + when(() => client.patch(path, data: { + 'users': [user], + })).thenAnswer( + (_) async => successResponse( + path, + data: { + 'users': + updatedUser.map((key, value) => MapEntry(key, value.toJson())) + }, + ), + ); + + final res = await userApi.partialUpdateUsers([user]); + + expect(res, isNotNull); + expect(res.users.length, updatedUser.length); + + verify(() => client.patch(path, data: { + 'users': [user] + })).called(1); + verifyNoMoreInteractions(client); + }); }