Skip to content

Commit

Permalink
Merge pull request #1072 from GetStream/hotfix/userReconnection
Browse files Browse the repository at this point in the history
fix(llc): send only user_id while reconnecting
  • Loading branch information
imtoori authored Apr 13, 2022
2 parents bf7d7fb + 8a4f80c commit 0b2330e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/stream_chat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
channel update.
- [[#1054]](https://github.com/GetStream/stream-chat-flutter/issues/1054) Fix `Unsupported operation: Cannot remove from an unmodifiable list`.
- [[#1033]](https://github.com/GetStream/stream-chat-flutter/issues/1033) Hard delete from dashboard does not delete message from client.
- Send only `user_id` while reconnecting.

✅ Added

Expand Down
15 changes: 12 additions & 3 deletions packages/stream_chat/lib/src/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ class StreamChatClient {
_chatPersistenceClient = _originalChatPersistenceClient;
await _chatPersistenceClient!.connect(ownUser.id);
}
final connectedUser = await openConnection();
final connectedUser = await openConnection(
includeUserDetailsInConnectCall: true,
);
return state.currentUser = connectedUser;
} catch (e, stk) {
if (e is StreamWebSocketError && e.isRetriable) {
Expand All @@ -341,7 +343,11 @@ class StreamChatClient {
}

/// Creates a new WebSocket connection with the current user.
Future<OwnUser> openConnection() async {
/// If [includeUserDetailsInConnectCall] is true it will include the current
/// user details in the connect call.
Future<OwnUser> openConnection({
bool includeUserDetailsInConnectCall = false,
}) async {
assert(
state.currentUser != null,
'User is not set on client, '
Expand Down Expand Up @@ -371,7 +377,10 @@ class StreamChatClient {
_ws.connectionStatusStream.skip(1).listen(_connectionStatusHandler);

try {
final event = await _ws.connect(user);
final event = await _ws.connect(
user,
includeUserDetails: includeUserDetailsInConnectCall,
);
return user.merge(event.me);
} catch (e, stk) {
logger.severe('error connecting ws', e, stk);
Expand Down
21 changes: 16 additions & 5 deletions packages/stream_chat/lib/src/ws/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ class WebSocket with TimerHelper {
}
}

Future<Uri> _buildUri({bool refreshToken = false}) async {
Future<Uri> _buildUri({
bool refreshToken = false,
bool includeUserDetails = true,
}) async {
final user = _user!;
final token = await tokenManager.loadToken(refresh: refreshToken);
final params = {
'user_id': user.id,
'user_details': user,
if (includeUserDetails) 'user_details': user,
'user_token': token.rawValue,
'server_determines_connection_id': true,
};
Expand All @@ -176,7 +179,10 @@ class WebSocket with TimerHelper {
bool _connectRequestInProgress = false;

/// Connect the WS using the parameters passed in the constructor
Future<Event> connect(User user) async {
Future<Event> connect(
User user, {
bool includeUserDetails = false,
}) async {
if (_connectRequestInProgress) {
throw const StreamWebSocketError('''
You've called connect twice,
Expand All @@ -191,7 +197,9 @@ class WebSocket with TimerHelper {
connectionCompleter = Completer<Event>();

try {
final uri = await _buildUri();
final uri = await _buildUri(
includeUserDetails: includeUserDetails,
);
_initWebSocketChannel(uri);
} catch (e, stk) {
_onConnectionError(e, stk);
Expand Down Expand Up @@ -219,7 +227,10 @@ class WebSocket with TimerHelper {
setTimer(
Duration(milliseconds: delay),
() async {
final uri = await _buildUri(refreshToken: refreshToken);
final uri = await _buildUri(
refreshToken: refreshToken,
includeUserDetails: false,
);
try {
_initWebSocketChannel(uri);
} catch (e, stk) {
Expand Down
10 changes: 8 additions & 2 deletions packages/stream_chat/test/src/fakes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ class FakeWebSocket extends Fake implements WebSocket {
Completer<Event>? connectionCompleter;

@override
Future<Event> connect(User user) async {
Future<Event> connect(
User user, {
bool? includeUserDetails = true,
}) async {
connectionStatus = ConnectionStatus.connecting;
final event = Event(
type: EventType.healthCheck,
Expand Down Expand Up @@ -167,7 +170,10 @@ class FakeWebSocketWithConnectionError extends Fake implements WebSocket {
Completer<Event>? connectionCompleter;

@override
Future<Event> connect(User user) async {
Future<Event> connect(
User user, {
bool? includeUserDetails = true,
}) async {
connectionStatus = ConnectionStatus.connecting;
const error = StreamWebSocketError('Error Connecting');
connectionCompleter = Completer()..completeError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.example"
minSdkVersion 21
minSdkVersion 22
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down

0 comments on commit 0b2330e

Please sign in to comment.