feat(xep): Add cache and roomstate to MUC implementation.

Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
This commit is contained in:
Ikjot Singh Dhody 2023-06-18 20:49:13 +05:30
parent 1f1321b269
commit 8728166a4d
2 changed files with 45 additions and 3 deletions

View File

@ -32,3 +32,14 @@ class RoomInformation {
/// The name or title of the Multi-User Chat (MUC) room. /// The name or title of the Multi-User Chat (MUC) room.
final String name; final String name;
} }
class RoomState {
RoomState({
required this.roomJid,
required this.roomInformation,
this.nick,
});
final JID roomJid;
final RoomInformation roomInformation;
String? nick;
}

View File

@ -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_0030/xep_0030.dart';
import 'package:moxxmpp/src/xeps/xep_0045/errors.dart'; import 'package:moxxmpp/src/xeps/xep_0045/errors.dart';
import 'package:moxxmpp/src/xeps/xep_0045/types.dart'; import 'package:moxxmpp/src/xeps/xep_0045/types.dart';
import 'package:synchronized/synchronized.dart';
enum ConversationType { chat, groupchat, groupchatprivate } enum ConversationType { chat, groupchat, groupchatprivate }
@ -25,6 +26,12 @@ class MUCManager extends XmppManagerBase {
@override @override
Future<bool> isSupported() async => true; Future<bool> isSupported() async => true;
/// Map full JID to RoomState
final Map<JID, RoomState> _mucRoomCache = {};
/// Cache lock
final Lock _cacheLock = Lock();
Future<Result<RoomInformation, MUCError>> queryRoomInformation( Future<Result<RoomInformation, MUCError>> queryRoomInformation(
JID roomJID, JID roomJID,
) async { ) async {
@ -38,6 +45,14 @@ class MUCManager extends XmppManagerBase {
final roomInformation = RoomInformation.fromDiscoInfo( final roomInformation = RoomInformation.fromDiscoInfo(
discoInfo: result.get<DiscoInfo>(), discoInfo: result.get<DiscoInfo>(),
); );
await _cacheLock.synchronized(
() async {
_mucRoomCache[roomJID] = RoomState(
roomJid: roomJID,
roomInformation: roomInformation,
);
},
);
return Result(roomInformation); return Result(roomInformation);
} catch (e) { } catch (e) {
return Result(InvalidDiscoInfoResponse); return Result(InvalidDiscoInfoResponse);
@ -64,23 +79,39 @@ class MUCManager extends XmppManagerBase {
), ),
), ),
); );
await _cacheLock.synchronized(
() async {
_mucRoomCache[roomJID]!.nick = nick;
},
);
return const Result(true); return const Result(true);
} }
Future<Result<bool, MUCError>> leaveRoom( Future<Result<bool, MUCError>> leaveRoom(
JID roomJIDWithNickname, JID roomJID,
) async { ) async {
if (roomJIDWithNickname.resource.isEmpty) { String? nick;
await _cacheLock.synchronized(
() async {
nick = _mucRoomCache[roomJID]!.nick;
},
);
if (nick!.isEmpty) {
return Result(NoNicknameSpecified()); return Result(NoNicknameSpecified());
} }
await getAttributes().sendStanza( await getAttributes().sendStanza(
StanzaDetails( StanzaDetails(
Stanza.presence( Stanza.presence(
to: roomJIDWithNickname.toString(), to: roomJID.withResource(nick!).toString(),
type: 'unavailable', type: 'unavailable',
), ),
), ),
); );
await _cacheLock.synchronized(
() async {
_mucRoomCache.remove(roomJID);
},
);
return const Result(true); return const Result(true);
} }
} }