[LLC] Deprecate setUser with connectUser

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-02-03 15:47:29 +05:30
parent ceb7cc0bdb
commit 3e43ba6dc7
6 changed files with 48 additions and 25 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ Future<void> main() async {
/// a backend to generate a user token using our server SDK. /// a backend to generate a user token using our server SDK.
/// Please see the following for more information: /// Please see the following for more information:
/// https://getstream.io/chat/docs/ios_user_setup_and_tokens/ /// https://getstream.io/chat/docs/ios_user_setup_and_tokens/
await client.setUser( await client.connectUser(
User( User(
id: 'cool-shadow-7', id: 'cool-shadow-7',
extraData: { extraData: {
@@ -141,9 +141,9 @@ class SendReactionResponse extends _BaseResponse {
_$SendReactionResponseFromJson(json); _$SendReactionResponseFromJson(json);
} }
/// Model response for [StreamChatClient.setGuestUser] api call /// Model response for [StreamChatClient.connectGuestUser] api call
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class SetGuestUserResponse extends _BaseResponse { class ConnectGuestUserResponse extends _BaseResponse {
/// Guest user access token /// Guest user access token
String accessToken; String accessToken;
@@ -151,8 +151,8 @@ class SetGuestUserResponse extends _BaseResponse {
User user; User user;
/// Create a new instance from a json /// Create a new instance from a json
static SetGuestUserResponse fromJson(Map<String, dynamic> json) => static ConnectGuestUserResponse fromJson(Map<String, dynamic> json) =>
_$SetGuestUserResponseFromJson(json); _$ConnectGuestUserResponseFromJson(json);
} }
/// Model response for [StreamChatClient.updateUser] api call /// Model response for [StreamChatClient.updateUser] api call
@@ -123,8 +123,8 @@ SendReactionResponse _$SendReactionResponseFromJson(Map json) {
)); ));
} }
SetGuestUserResponse _$SetGuestUserResponseFromJson(Map json) { ConnectGuestUserResponse _$ConnectGuestUserResponseFromJson(Map json) {
return SetGuestUserResponse() return ConnectGuestUserResponse()
..duration = json['duration'] as String ..duration = json['duration'] as String
..accessToken = json['access_token'] as String ..accessToken = json['access_token'] as String
..user = json['user'] == null ..user = json['user'] == null
+37 -14
View File
@@ -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. /// 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. /// 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; final TokenProvider tokenProvider;
/// [Dio] httpClient /// [Dio] httpClient
@@ -271,7 +271,7 @@ class StreamChatClient {
httpClient.unlock(); httpClient.unlock();
await setUser(User(id: userId), newToken); await connectUser(User(id: userId), newToken);
try { try {
return await httpClient.request( return await httpClient.request(
@@ -344,7 +344,12 @@ class StreamChatClient {
/// Set the current user, this triggers a connection to the API. /// Set the current user, this triggers a connection to the API.
/// It returns a [Future] that resolves when the connection is setup. /// It returns a [Future] that resolves when the connection is setup.
Future<Event> setUser(User user, String token) async { @Deprecated('Use `connectUser` instead. Will be removed in Future releases')
Future<Event> 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<Event> connectUser(User user, String token) async {
if (_connectCompleter != null && !_connectCompleter.isCompleted) { if (_connectCompleter != null && !_connectCompleter.isCompleted) {
logger.warning('Already connecting'); logger.warning('Already connecting');
throw Exception('Already connecting'); throw Exception('Already connecting');
@@ -352,7 +357,7 @@ class StreamChatClient {
_connectCompleter = Completer(); _connectCompleter = Completer();
logger.info('set user'); logger.info('connect user');
state.user = OwnUser.fromJson(user.toJson()); state.user = OwnUser.fromJson(user.toJson());
this.token = token; this.token = token;
_anonymous = false; _anonymous = false;
@@ -368,15 +373,21 @@ class StreamChatClient {
/// Set the current user using the [tokenProvider] to fetch the token. /// Set the current user using the [tokenProvider] to fetch the token.
/// It returns a [Future] that resolves when the connection is setup. /// It returns a [Future] that resolves when the connection is setup.
Future<void> setUserWithProvider(User user) async { @Deprecated(
'Use `connectUserWithProvider` instead. Will be removed in Future releases')
Future<Event> 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<Event> connectUserWithProvider(User user) async {
if (tokenProvider == null) { if (tokenProvider == null) {
throw Exception(''' throw Exception('''
TokenProvider must be provided in the constructor in order to use `setUserWithProvider` method. TokenProvider must be provided in the constructor in order to use `connectUserWithProvider` method.
Use `setUser` providing a token. Use `connectUser` providing a token.
'''); ''');
} }
final token = await tokenProvider(user.id); final token = await tokenProvider(user.id);
return setUser(user, token); return connectUser(user, token);
} }
/// Stream of [Event] coming from websocket connection /// Stream of [Event] coming from websocket connection
@@ -573,7 +584,7 @@ class StreamChatClient {
} }
if (wsConnectionStatus != ConnectionStatus.connected) { if (wsConnectionStatus != ConnectionStatus.connected) {
final errorMessage = 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) { if (persistenceEnabled) {
logger.warning( logger.warning(
'$errorMessage\nTrying to retrieve channels from the offline storage.'); '$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. /// 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. /// It returns a [Future] that resolves when the connection is setup.
Future<Event> setAnonymousUser() async { @Deprecated(
'Use `connectAnonymousUser` instead. Will be removed in Future releases')
Future<Event> 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<Event> connectAnonymousUser() async {
if (_connectCompleter != null && !_connectCompleter.isCompleted) { if (_connectCompleter != null && !_connectCompleter.isCompleted) {
logger.warning('Already connecting'); logger.warning('Already connecting');
throw Exception('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. /// Set the current user as guest, this triggers a connection to the API.
/// It returns a [Future] that resolves when the connection is setup. /// It returns a [Future] that resolves when the connection is setup.
Future<void> setGuestUser(User user) async { @Deprecated(
'Use `connectGuestUser` instead. Will be removed in Future releases')
Future<Event> 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<Event> connectGuestUser(User user) async {
_anonymous = true; _anonymous = true;
final response = await post('/guest', data: {'user': user.toJson()}) final response = await post('/guest', data: {'user': user.toJson()})
.then((res) => decode<SetGuestUserResponse>( .then((res) => decode<ConnectGuestUserResponse>(
res.data, SetGuestUserResponse.fromJson)) res.data, ConnectGuestUserResponse.fromJson))
.whenComplete(() => _anonymous = false); .whenComplete(() => _anonymous = false);
return setUser( return connectUser(
response.user, response.user,
response.accessToken, response.accessToken,
); );
@@ -3496,10 +3496,10 @@ void main() {
expect(response.users, isA<Map<String, User>>()); expect(response.users, isA<Map<String, User>>());
}); });
test('SetGuestUserResponse', () { test('ConnectGuestUserResponse', () {
const jsonExample = 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"}'; 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<User>()); expect(response.user, isA<User>());
expect(response.accessToken, isA<String>()); expect(response.accessToken, isA<String>());
}); });
@@ -390,7 +390,7 @@ void main() {
}); });
group('user', () { group('user', () {
test('setUser should throw exception', () async { test('connectUser should throw exception', () async {
final mockDio = MockDio(); final mockDio = MockDio();
when(mockDio.options).thenReturn(BaseOptions()); when(mockDio.options).thenReturn(BaseOptions());
@@ -422,7 +422,7 @@ void main() {
httpClient: mockDio, httpClient: mockDio,
); );
expect(() => client.setUserWithProvider(User(id: 'test-id')), expect(() => client.connectUserWithProvider(User(id: 'test-id')),
throwsA(isA<Exception>())); throwsA(isA<Exception>()));
}); });