diff --git a/packages/stream_chat/example/lib/main.dart b/packages/stream_chat/example/lib/main.dart index 769e6d44..1261d3fe 100644 --- a/packages/stream_chat/example/lib/main.dart +++ b/packages/stream_chat/example/lib/main.dart @@ -10,7 +10,7 @@ Future main() async { /// a backend to generate a user token using our server SDK. /// Please see the following for more information: /// https://getstream.io/chat/docs/ios_user_setup_and_tokens/ - await client.setUser( + await client.connectUser( User( id: 'cool-shadow-7', extraData: { diff --git a/packages/stream_chat/lib/src/api/responses.dart b/packages/stream_chat/lib/src/api/responses.dart index e27ac2c9..3e5b7a7f 100644 --- a/packages/stream_chat/lib/src/api/responses.dart +++ b/packages/stream_chat/lib/src/api/responses.dart @@ -141,9 +141,9 @@ class SendReactionResponse extends _BaseResponse { _$SendReactionResponseFromJson(json); } -/// Model response for [StreamChatClient.setGuestUser] api call +/// Model response for [StreamChatClient.connectGuestUser] api call @JsonSerializable(createToJson: false) -class SetGuestUserResponse extends _BaseResponse { +class ConnectGuestUserResponse extends _BaseResponse { /// Guest user access token String accessToken; @@ -151,8 +151,8 @@ class SetGuestUserResponse extends _BaseResponse { User user; /// Create a new instance from a json - static SetGuestUserResponse fromJson(Map json) => - _$SetGuestUserResponseFromJson(json); + static ConnectGuestUserResponse fromJson(Map json) => + _$ConnectGuestUserResponseFromJson(json); } /// Model response for [StreamChatClient.updateUser] api call diff --git a/packages/stream_chat/lib/src/api/responses.g.dart b/packages/stream_chat/lib/src/api/responses.g.dart index db5fba9e..fc51dd52 100644 --- a/packages/stream_chat/lib/src/api/responses.g.dart +++ b/packages/stream_chat/lib/src/api/responses.g.dart @@ -123,8 +123,8 @@ SendReactionResponse _$SendReactionResponseFromJson(Map json) { )); } -SetGuestUserResponse _$SetGuestUserResponseFromJson(Map json) { - return SetGuestUserResponse() +ConnectGuestUserResponse _$ConnectGuestUserResponseFromJson(Map json) { + return ConnectGuestUserResponse() ..duration = json['duration'] as String ..accessToken = json['access_token'] as String ..user = json['user'] == null diff --git a/packages/stream_chat/lib/src/client.dart b/packages/stream_chat/lib/src/client.dart index 723b38b6..9b1ba4db 100644 --- a/packages/stream_chat/lib/src/client.dart +++ b/packages/stream_chat/lib/src/client.dart @@ -146,7 +146,7 @@ class StreamChatClient { /// A function in which you send a request to your own backend to get a Stream Chat API token. /// The token will be the return value of the function. - /// It's used by the client to refresh the token once expired or to set the user without a predefined token using [setUserWithProvider]. + /// It's used by the client to refresh the token once expired or to connect the user without a predefined token using [connectUserWithProvider]. final TokenProvider tokenProvider; /// [Dio] httpClient @@ -271,7 +271,7 @@ class StreamChatClient { httpClient.unlock(); - await setUser(User(id: userId), newToken); + await connectUser(User(id: userId), newToken); try { return await httpClient.request( @@ -344,7 +344,12 @@ class StreamChatClient { /// Set the current user, this triggers a connection to the API. /// It returns a [Future] that resolves when the connection is setup. - Future setUser(User user, String token) async { + @Deprecated('Use `connectUser` instead. Will be removed in Future releases') + Future setUser(User user, String token) => connectUser(user, token); + + /// Connects the current user, this triggers a connection to the API. + /// It returns a [Future] that resolves when the connection is setup. + Future connectUser(User user, String token) async { if (_connectCompleter != null && !_connectCompleter.isCompleted) { logger.warning('Already connecting'); throw Exception('Already connecting'); @@ -352,7 +357,7 @@ class StreamChatClient { _connectCompleter = Completer(); - logger.info('set user'); + logger.info('connect user'); state.user = OwnUser.fromJson(user.toJson()); this.token = token; _anonymous = false; @@ -368,15 +373,21 @@ class StreamChatClient { /// Set the current user using the [tokenProvider] to fetch the token. /// It returns a [Future] that resolves when the connection is setup. - Future setUserWithProvider(User user) async { + @Deprecated( + 'Use `connectUserWithProvider` instead. Will be removed in Future releases') + Future setUserWithProvider(User user) => connectUserWithProvider(user); + + /// Connects the current user using the [tokenProvider] to fetch the token. + /// It returns a [Future] that resolves when the connection is setup. + Future connectUserWithProvider(User user) async { if (tokenProvider == null) { throw Exception(''' - TokenProvider must be provided in the constructor in order to use `setUserWithProvider` method. - Use `setUser` providing a token. + TokenProvider must be provided in the constructor in order to use `connectUserWithProvider` method. + Use `connectUser` providing a token. '''); } final token = await tokenProvider(user.id); - return setUser(user, token); + return connectUser(user, token); } /// Stream of [Event] coming from websocket connection @@ -573,7 +584,7 @@ class StreamChatClient { } if (wsConnectionStatus != ConnectionStatus.connected) { final errorMessage = - 'You cannot use queryChannels without an active connection. Please call setUser to connect the client.'; + 'You cannot use queryChannels without an active connection. Please call `connectUser` to connect the client.'; if (persistenceEnabled) { logger.warning( '$errorMessage\nTrying to retrieve channels from the offline storage.'); @@ -869,7 +880,13 @@ class StreamChatClient { /// Set the current user with an anonymous id, this triggers a connection to the API. /// It returns a [Future] that resolves when the connection is setup. - Future setAnonymousUser() async { + @Deprecated( + 'Use `connectAnonymousUser` instead. Will be removed in Future releases') + Future setAnonymousUser() => connectAnonymousUser(); + + /// Connects the current user with an anonymous id, this triggers a connection to the API. + /// It returns a [Future] that resolves when the connection is setup. + Future connectAnonymousUser() async { if (_connectCompleter != null && !_connectCompleter.isCompleted) { logger.warning('Already connecting'); throw Exception('Already connecting'); @@ -892,13 +909,19 @@ class StreamChatClient { /// Set the current user as guest, this triggers a connection to the API. /// It returns a [Future] that resolves when the connection is setup. - Future setGuestUser(User user) async { + @Deprecated( + 'Use `connectGuestUser` instead. Will be removed in Future releases') + Future setGuestUser(User user) => connectGuestUser(user); + + /// Connects the current user as guest, this triggers a connection to the API. + /// It returns a [Future] that resolves when the connection is setup. + Future connectGuestUser(User user) async { _anonymous = true; final response = await post('/guest', data: {'user': user.toJson()}) - .then((res) => decode( - res.data, SetGuestUserResponse.fromJson)) + .then((res) => decode( + res.data, ConnectGuestUserResponse.fromJson)) .whenComplete(() => _anonymous = false); - return setUser( + return connectUser( response.user, response.accessToken, ); diff --git a/packages/stream_chat/test/src/api/responses_test.dart b/packages/stream_chat/test/src/api/responses_test.dart index bfd4e5a7..ae2fd083 100644 --- a/packages/stream_chat/test/src/api/responses_test.dart +++ b/packages/stream_chat/test/src/api/responses_test.dart @@ -3496,10 +3496,10 @@ void main() { expect(response.users, isA>()); }); - test('SetGuestUserResponse', () { + test('ConnectGuestUserResponse', () { const jsonExample = r'{"user":{"id":"guest-ac612aee-25fe-49fb-b1af-969e41f452a0-wild-breeze-7","role":"guest","created_at":"2020-02-03T10:19:01.538434Z","updated_at":"2020-02-03T10:19:01.539543Z","banned":false,"online":false},"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiZ3Vlc3QtYWM2MTJhZWUtMjVmZS00OWZiLWIxYWYtOTY5ZTQxZjQ1MmEwLXdpbGQtYnJlZXplLTcifQ.mmoFGu7oJjpFsp7nFN78UbIpO7gowbuIbyoppsuvbXA","duration":"4.66ms"}'; - final response = SetGuestUserResponse.fromJson(json.decode(jsonExample)); + final response = ConnectGuestUserResponse.fromJson(json.decode(jsonExample)); expect(response.user, isA()); expect(response.accessToken, isA()); }); diff --git a/packages/stream_chat/test/src/client_test.dart b/packages/stream_chat/test/src/client_test.dart index a1fa2175..bbc7aef4 100644 --- a/packages/stream_chat/test/src/client_test.dart +++ b/packages/stream_chat/test/src/client_test.dart @@ -390,7 +390,7 @@ void main() { }); group('user', () { - test('setUser should throw exception', () async { + test('connectUser should throw exception', () async { final mockDio = MockDio(); when(mockDio.options).thenReturn(BaseOptions()); @@ -422,7 +422,7 @@ void main() { httpClient: mockDio, ); - expect(() => client.setUserWithProvider(User(id: 'test-id')), + expect(() => client.connectUserWithProvider(User(id: 'test-id')), throwsA(isA())); });