feat(xep): Fix cache issue with join/leaveRoom.

Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
This commit is contained in:
Ikjot Singh Dhody 2023-06-20 17:38:30 +05:30
parent 51bca6c25d
commit 217c3ac236

View File

@ -45,7 +45,7 @@ class MUCManager extends XmppManagerBase {
} }
Future<Result<bool, MUCError>> joinRoom( Future<Result<bool, MUCError>> joinRoom(
JID roomJID, JID roomJid,
String nick, String nick,
) async { ) async {
if (nick.isEmpty) { if (nick.isEmpty) {
@ -54,7 +54,7 @@ class MUCManager extends XmppManagerBase {
await getAttributes().sendStanza( await getAttributes().sendStanza(
StanzaDetails( StanzaDetails(
Stanza.presence( Stanza.presence(
to: roomJID.withResource(nick).toString(), to: roomJid.withResource(nick).toString(),
children: [ children: [
XMLNode.xmlns( XMLNode.xmlns(
tag: 'x', tag: 'x',
@ -66,28 +66,31 @@ class MUCManager extends XmppManagerBase {
); );
await _cacheLock.synchronized( await _cacheLock.synchronized(
() { () {
_mucRoomCache[roomJID]!.nick = nick; _mucRoomCache[roomJid] = RoomState(roomJid: roomJid, nick: nick);
}, },
); );
return const Result(true); return const Result(true);
} }
Future<Result<bool, MUCError>> leaveRoom( Future<Result<bool, MUCError>> leaveRoom(
JID roomJID, JID roomJid,
) async { ) async {
final nick = final nick = await _cacheLock.synchronized(() {
await _cacheLock.synchronized(() => _mucRoomCache[roomJID]!.nick); final nick = _mucRoomCache[roomJid]?.nick;
_mucRoomCache.remove(roomJid);
return nick;
});
await getAttributes().sendStanza( await getAttributes().sendStanza(
StanzaDetails( StanzaDetails(
Stanza.presence( Stanza.presence(
to: roomJID.withResource(nick!).toString(), to: roomJid.withResource(nick!).toString(),
type: 'unavailable', type: 'unavailable',
), ),
), ),
); );
await _cacheLock.synchronized( await _cacheLock.synchronized(
() { () {
_mucRoomCache.remove(roomJID); _mucRoomCache.remove(roomJid);
}, },
); );
return const Result(true); return const Result(true);