@@ -28,15 +28,15 @@ final _logger = Logger('MessagingBloc');
2828
2929class MessagingBloc extends Bloc <MessagingEvent , MessagingState > {
3030 MessagingBloc (
31- this ._userId,
3231 this ._client,
3332 this ._messagingConfig,
3433 this ._chatsRepository,
3534 this ._chatsOutboxRepository,
3635 this ._smsRepository,
3736 this ._smsOutboxRepository,
37+ this ._sessionRepository,
3838 this ._submitNotification,
39- ) : super (MessagingState .initial (_client, _messagingConfig)) {
39+ ) : super (MessagingState .initial (_sessionRepository. getCurrent ().userId, _client, _messagingConfig)) {
4040 on < Connect > (_connect);
4141 on < Refresh > (_refresh);
4242 on < Disconnect > (_onDisconnect);
@@ -49,13 +49,13 @@ class MessagingBloc extends Bloc<MessagingEvent, MessagingState> {
4949 _subs.add (_client.errorStream.listen ((e) => add (_ClientError (e))));
5050 }
5151
52- final String _userId;
5352 final PhoenixSocket _client;
5453 final MessagingConfig _messagingConfig;
5554 final ChatsRepository _chatsRepository;
5655 final ChatsOutboxRepository _chatsOutboxRepository;
5756 final SmsRepository _smsRepository;
5857 final SmsOutboxRepository _smsOutboxRepository;
58+ final SessionRepository _sessionRepository;
5959 final Function (Notification ) _submitNotification;
6060 final List <StreamSubscription > _subs = [];
6161
@@ -70,10 +70,7 @@ class MessagingBloc extends Bloc<MessagingEvent, MessagingState> {
7070 // -
7171 // Uncomment section below to wipe messaging related data
7272 // -
73- // _chatsRepository.wipeChatsData();
74- // _chatsOutboxRepository.wipeOutboxData();
75- // _smsRepository.wipeData();
76- // _smsOutboxRepository.wipeOutboxData();
73+ // wipeData()
7774
7875 emit (state.copyWith (status: ConnectionStatus .connecting));
7976 _client.connect ();
@@ -89,8 +86,26 @@ class MessagingBloc extends Bloc<MessagingEvent, MessagingState> {
8986 try {
9087 // Join channel for user specific events
9188 if (_client.userChannel == null ) {
92- final userChannel = _client.createUserChannel (_userId);
93- await userChannel.join ().future;
89+ final userId = _sessionRepository.getCurrent ().userId;
90+ final userChannel = _client.createUserChannel (userId);
91+ final joinReq = await userChannel.join ().future;
92+ final response = joinReq.response;
93+
94+ /// Handle the workaround for core userId upgrade
95+ /// When the server detects that the client is using an old userId format, it responds with a "forbidden" message containing the new userId.
96+ /// The client then updates its session with the new userId, wipes local messaging data to prevent inconsistencies
97+ /// and rejoins the channel with the new userId.
98+ if (response is List && response[0 ] == 'forbidden' && response[1 ] is String ) {
99+ final newUserId = response[1 ] as String ;
100+ _logger.warning ('UserId upgrade required, new userId: $newUserId ' );
101+
102+ await _sessionRepository.patchSession (userId: newUserId);
103+ await wipeData ();
104+
105+ emit (state.copyWith (userId: newUserId));
106+ final userChannel = _client.createUserChannel (newUserId);
107+ await userChannel.join ().future;
108+ }
94109 }
95110
96111 // Init workers
@@ -137,6 +152,13 @@ class MessagingBloc extends Bloc<MessagingEvent, MessagingState> {
137152 }
138153 }
139154
155+ Future <void > wipeData () async {
156+ await _chatsRepository.wipeChatsData ();
157+ await _chatsOutboxRepository.wipeOutboxData ();
158+ await _smsRepository.wipeData ();
159+ await _smsOutboxRepository.wipeOutboxData ();
160+ }
161+
140162 void _disposeWorkers () {
141163 _chatsSyncWorker? .dispose ();
142164 _chatsSyncWorker = null ;
0 commit comments