From 8728166a4de3ad71c2fa43ca8c9804ef4bdb6b99 Mon Sep 17 00:00:00 2001 From: Ikjot Singh Dhody Date: Sun, 18 Jun 2023 20:49:13 +0530 Subject: [PATCH] feat(xep): Add cache and roomstate to MUC implementation. Signed-off-by: Ikjot Singh Dhody --- .../moxxmpp/lib/src/xeps/xep_0045/types.dart | 11 ++++++ .../lib/src/xeps/xep_0045/xep_0045.dart | 37 +++++++++++++++++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/packages/moxxmpp/lib/src/xeps/xep_0045/types.dart b/packages/moxxmpp/lib/src/xeps/xep_0045/types.dart index b8dc73e..8dcceed 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0045/types.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0045/types.dart @@ -32,3 +32,14 @@ class RoomInformation { /// The name or title of the Multi-User Chat (MUC) room. final String name; } + +class RoomState { + RoomState({ + required this.roomJid, + required this.roomInformation, + this.nick, + }); + final JID roomJid; + final RoomInformation roomInformation; + String? nick; +} 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 54194c0..39a3e4d 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0045/xep_0045.dart @@ -11,6 +11,7 @@ import 'package:moxxmpp/src/xeps/xep_0030/types.dart'; import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart'; import 'package:moxxmpp/src/xeps/xep_0045/errors.dart'; import 'package:moxxmpp/src/xeps/xep_0045/types.dart'; +import 'package:synchronized/synchronized.dart'; enum ConversationType { chat, groupchat, groupchatprivate } @@ -25,6 +26,12 @@ class MUCManager extends XmppManagerBase { @override Future isSupported() async => true; + /// Map full JID to RoomState + final Map _mucRoomCache = {}; + + /// Cache lock + final Lock _cacheLock = Lock(); + Future> queryRoomInformation( JID roomJID, ) async { @@ -38,6 +45,14 @@ class MUCManager extends XmppManagerBase { final roomInformation = RoomInformation.fromDiscoInfo( discoInfo: result.get(), ); + await _cacheLock.synchronized( + () async { + _mucRoomCache[roomJID] = RoomState( + roomJid: roomJID, + roomInformation: roomInformation, + ); + }, + ); return Result(roomInformation); } catch (e) { return Result(InvalidDiscoInfoResponse); @@ -64,23 +79,39 @@ class MUCManager extends XmppManagerBase { ), ), ); + await _cacheLock.synchronized( + () async { + _mucRoomCache[roomJID]!.nick = nick; + }, + ); return const Result(true); } Future> leaveRoom( - JID roomJIDWithNickname, + JID roomJID, ) async { - if (roomJIDWithNickname.resource.isEmpty) { + String? nick; + await _cacheLock.synchronized( + () async { + nick = _mucRoomCache[roomJID]!.nick; + }, + ); + if (nick!.isEmpty) { return Result(NoNicknameSpecified()); } await getAttributes().sendStanza( StanzaDetails( Stanza.presence( - to: roomJIDWithNickname.toString(), + to: roomJID.withResource(nick!).toString(), type: 'unavailable', ), ), ); + await _cacheLock.synchronized( + () async { + _mucRoomCache.remove(roomJID); + }, + ); return const Result(true); } }