feat(xep): Add joinRoom, leaveRoom routines.

Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
This commit is contained in:
Ikjot Singh Dhody 2023-05-31 13:31:02 +05:30
parent 762cf1c77a
commit 68809469f6
5 changed files with 61 additions and 5 deletions

View File

@ -48,6 +48,7 @@ export 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
export 'package:moxxmpp/src/xeps/xep_0030/helpers.dart';
export 'package:moxxmpp/src/xeps/xep_0030/types.dart';
export 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart';
export 'package:moxxmpp/src/xeps/xep_0045/xep_0045.dart';
export 'package:moxxmpp/src/xeps/xep_0054.dart';
export 'package:moxxmpp/src/xeps/xep_0060/errors.dart';
export 'package:moxxmpp/src/xeps/xep_0060/helpers.dart';

View File

@ -20,6 +20,9 @@ const discoItemsXmlns = 'http://jabber.org/protocol/disco#items';
// XEP-0033
const extendedAddressingXmlns = 'http://jabber.org/protocol/address';
// XEP-0045
const mucXmlns = 'http://jabber.org/protocol/muc';
// XEP-0054
const vCardTempXmlns = 'vcard-temp';
const vCardTempUpdate = 'vcard-temp:x:update';

View File

@ -1,3 +1,5 @@
abstract class MUCError {}
class InvalidStanzaFormat extends MUCError {}
class NoNicknameSpecified extends MUCError {}

View File

@ -9,7 +9,7 @@ class RoomInformation {
});
factory RoomInformation.fromStanza({
required String roomJID,
required JID roomJID,
required XMLNode stanza,
}) {
final featureNodes = stanza.children[0].findTags('feature');
@ -31,7 +31,7 @@ class RoomInformation {
throw InvalidStanzaFormat();
}
}
final String jid;
final JID jid;
final List<String> features;
final String name;
}

View File

@ -8,15 +8,16 @@ class MUCManager extends XmppManagerBase {
@override
Future<bool> isSupported() async => true;
Future<Result<RoomInformation, MUCError>> queryRoomInformation(
String roomJID) async {
Future<Result<RoomInformation, MUCError>> queryRoomInformation({
required JID roomJID,
}) async {
final attrs = getAttributes();
try {
final result = await attrs.sendStanza(
StanzaDetails(
Stanza.iq(
type: 'get',
to: roomJID,
to: roomJID.toString(),
children: [
XMLNode.xmlns(
tag: 'query',
@ -33,4 +34,53 @@ class MUCManager extends XmppManagerBase {
return Result(InvalidStanzaFormat());
}
}
Future<Result<bool, MUCError>> joinRoom({
required JID roomJIDWithNickname,
}) async {
if (roomJIDWithNickname.resource.isEmpty) {
return Result(NoNicknameSpecified());
}
final attrs = getAttributes();
try {
await attrs.sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJIDWithNickname.toString(),
children: [
XMLNode.xmlns(
tag: 'x',
xmlns: mucXmlns,
)
],
),
),
);
return const Result(true);
} catch (e) {
return Result(InvalidStanzaFormat());
}
}
Future<Result<bool, MUCError>> leaveRoom({
required JID roomJIDWithNickname,
}) async {
if (roomJIDWithNickname.resource.isEmpty) {
return Result(NoNicknameSpecified());
}
final attrs = getAttributes();
try {
await attrs.sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJIDWithNickname.toString(),
type: 'unavailable',
),
),
);
return const Result(true);
} catch (e) {
return Result(InvalidStanzaFormat());
}
}
}