From 76a9f7be7aa4af29bd37d963dfa1ce6b4a7d371a Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Fri, 22 Sep 2023 21:43:56 +0200 Subject: [PATCH] feat(xep): Allow adding MUCs to join later --- .../lib/src/xeps/xep_0045/xep_0045.dart | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart b/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart index 48d4e40..bbb9156 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart @@ -16,6 +16,9 @@ import 'package:moxxmpp/src/xeps/xep_0359.dart'; import 'package:synchronized/extension.dart'; import 'package:synchronized/synchronized.dart'; +/// (Room JID, nickname) +typedef MUCRoomJoin = (JID, String); + class MUCManager extends XmppManagerBase { MUCManager() : super(mucManager); @@ -28,6 +31,10 @@ class MUCManager extends XmppManagerBase { /// Cache lock final Lock _cacheLock = Lock(); + /// Flag indicating whether we joined the rooms added to the room list with + /// [prepareRoomList]. + bool _joinedPreparedRooms = true; + @override List getIncomingStanzaHandlers() => [ StanzaHandler( @@ -52,8 +59,9 @@ class MUCManager extends XmppManagerBase { return; } - // Only attempt rejoining if we did not resume the stream. - if (event.resumed) { + // Only attempt rejoining if we did not resume the stream and all + // prepared rooms are already joined. + if (event.resumed && _joinedPreparedRooms) { return; } @@ -70,6 +78,28 @@ class MUCManager extends XmppManagerBase { 0, ); } + _joinedPreparedRooms = true; + }); + } + + /// Prepares the internal room list to ensure that the rooms + /// [rooms] are joined once we are connected. + Future prepareRoomList(List rooms) async { + assert( + rooms.isNotEmpty, + 'The room list should not be empty', + ); + + await _cacheLock.synchronized(() { + _joinedPreparedRooms = false; + for (final room in rooms) { + final (roomJid, nick) = room; + _mucRoomCache[roomJid] = RoomState( + roomJid: roomJid, + nick: nick, + joined: false, + ); + } }); }