feat(xep): Small fixes - review cycle 1.

Signed-off-by: Ikjot Singh Dhody <ikjotsd@gmail.com>
This commit is contained in:
Ikjot Singh Dhody 2023-06-18 20:09:06 +05:30
parent 66195f66fa
commit 1f1321b269
6 changed files with 99 additions and 49 deletions

View File

@ -83,6 +83,8 @@ class _MyHomePageState extends State<MyHomePage> {
),
MessageManager(),
PresenceManager(),
OccupantIdManager(),
MUCManager()
])
..registerFeatureNegotiators([
ResourceBindingNegotiator(),
@ -158,6 +160,47 @@ class _MyHomePageState extends State<MyHomePage> {
),
obscureText: true,
),
TextButton(
onPressed: () async {
// final muc = connection.getManagerById<MUCManager>(mucManager);
// final roomInformationResult = await muc!.queryRoomInformation(
// JID.fromString('moxxmpp-muc-test@muc.moxxy.org'));
// if (roomInformationResult.isType<RoomInformation>()) {
// print('Room information received');
// print(roomInformationResult.get<RoomInformation>().jid);
// print(roomInformationResult.get<RoomInformation>().name);
// print(roomInformationResult.get<RoomInformation>().features);
// }
// final muc = connection.getManagerById<MUCManager>(mucManager);
// print('joining room');
// final roomInformationResult = await muc!.joinRoom(
// JID.fromString('moxxmpp-muc-test@muc.moxxy.org/test_1'));
// if (roomInformationResult.isType<MUCError>()) {
// print(roomInformationResult.get());
// } else {
// print(roomInformationResult.get());
// }
print('HERE IS YOUR JID');
print(connection.resource);
final sid = connection.generateId();
final originId = connection.generateId();
final message =
connection.getManagerById<MessageManager>(messageManager);
message!.sendMessage(
JID.fromString('moxxmpp-muc-test@muc.moxxy.org/ISD'),
TypedMap<StanzaHandlerExtension>.fromList([
const MessageBodyData('Testing'),
const MarkableData(true),
MessageIdData(sid),
StableIdData(originId, null),
ConversationTypeData(ConversationType.groupchatprivate)
]),
);
},
child: const Text('Test'),
),
],
),
),

View File

@ -33,3 +33,4 @@ const stickersManager = 'org.moxxmpp.stickersmanager';
const entityCapabilitiesManager = 'org.moxxmpp.entitycapabilities';
const messageProcessingHintManager = 'org.moxxmpp.messageprocessinghint';
const occupantIdManager = 'org.moxxmpp.occupantidmanager';
const mucManager = 'org.moxxmpp.mucmanager';

View File

@ -8,7 +8,6 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/util/typed_map.dart';
import 'package:moxxmpp/src/xeps/xep_0045/xep_0045.dart';
import 'package:moxxmpp/src/xeps/xep_0066.dart';
import 'package:moxxmpp/src/xeps/xep_0447.dart';
import 'package:moxxmpp/src/xeps/xep_0449.dart';
@ -97,17 +96,15 @@ class MessageManager extends XmppManagerBase {
/// data for building the message.
Future<void> sendMessage(
JID to,
TypedMap<StanzaHandlerExtension> extensions,
) async {
TypedMap<StanzaHandlerExtension> extensions, {
String type = 'chat',
}) async {
await getAttributes().sendStanza(
StanzaDetails(
Stanza.message(
to: to.toString(),
id: extensions.get<MessageIdData>()?.id,
type: extensions.get<ConversationTypeData>()?.conversationType ==
ConversationType.groupchat
? 'groupchat'
: 'chat',
type: type,
children: _messageSendingCallbacks
.map((c) => c(extensions))
.flattened

View File

@ -1,7 +1,14 @@
/// Represents an error related to Multi-User Chat (MUC).
abstract class MUCError {}
/// Error indicating an invalid (non-supported) stanza received while going
/// through normal operation/flow of an MUC.
class InvalidStanzaFormat extends MUCError {}
/// Represents an error indicating an abnormal condition while parsing
/// the DiscoInfo response stanza in Multi-User Chat (MUC).
class InvalidDiscoInfoResponse extends MUCError {}
/// Returned when no nickname was specified from the client side while trying to
/// perform some actions on the MUC, such as joining the room.
class NoNicknameSpecified extends MUCError {}

View File

@ -2,22 +2,33 @@ import 'package:moxxmpp/src/jid.dart';
import 'package:moxxmpp/src/xeps/xep_0030/types.dart';
class RoomInformation {
/// Represents information about a Multi-User Chat (MUC) room.
RoomInformation({
required this.jid,
required this.features,
required this.name,
});
/// Constructs a [RoomInformation] object from a [DiscoInfo] object.
/// The [DiscoInfo] object contains the necessary information to populate
/// the [RoomInformation] fields.
factory RoomInformation.fromDiscoInfo({
required DiscoInfo discoInfo,
}) =>
RoomInformation(
jid: discoInfo.jid!,
features: discoInfo.features,
name: discoInfo.identities[0].name!,
name: discoInfo.identities
.firstWhere((i) => i.category == 'conference')
.name!,
);
/// The JID (Jabber ID) of the Multi-User Chat (MUC) room.
final JID jid;
/// A list of features supported by the Multi-User Chat (MUC) room.
final List<String> features;
/// The name or title of the Multi-User Chat (MUC) room.
final String name;
}

View File

@ -7,6 +7,7 @@ import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/types/result.dart';
import 'package:moxxmpp/src/xeps/xep_0030/errors.dart';
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';
@ -27,16 +28,15 @@ class MUCManager extends XmppManagerBase {
Future<Result<RoomInformation, MUCError>> queryRoomInformation(
JID roomJID,
) async {
final result = await getAttributes()
.getManagerById<DiscoManager>(discoManager)!
.discoInfoQuery(roomJID);
if (result.isType<DiscoError>()) {
return Result(InvalidStanzaFormat());
}
try {
final attrs = getAttributes();
final result = await attrs
.getManagerById<DiscoManager>(discoManager)
?.discoInfoQuery(roomJID);
if (result!.isType<DiscoError>()) {
return Result(InvalidStanzaFormat());
}
final roomInformation = RoomInformation.fromDiscoInfo(
discoInfo: result.get(),
discoInfo: result.get<DiscoInfo>(),
);
return Result(roomInformation);
} catch (e) {
@ -45,30 +45,26 @@ class MUCManager extends XmppManagerBase {
}
Future<Result<bool, MUCError>> joinRoom(
JID roomJIDWithNickname,
JID roomJID,
String nick,
) async {
if (roomJIDWithNickname.resource.isEmpty) {
if (nick.isEmpty) {
return Result(NoNicknameSpecified());
}
final attrs = getAttributes();
try {
await attrs.sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJIDWithNickname.toString(),
children: [
XMLNode.xmlns(
tag: 'x',
xmlns: mucXmlns,
)
],
),
await getAttributes().sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJID.withResource(nick).toString(),
children: [
XMLNode.xmlns(
tag: 'x',
xmlns: mucXmlns,
)
],
),
);
return const Result(true);
} catch (e) {
return Result(InvalidStanzaFormat());
}
),
);
return const Result(true);
}
Future<Result<bool, MUCError>> leaveRoom(
@ -77,19 +73,14 @@ class MUCManager extends XmppManagerBase {
if (roomJIDWithNickname.resource.isEmpty) {
return Result(NoNicknameSpecified());
}
final attrs = getAttributes();
try {
await attrs.sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJIDWithNickname.toString(),
type: 'unavailable',
),
await getAttributes().sendStanza(
StanzaDetails(
Stanza.presence(
to: roomJIDWithNickname.toString(),
type: 'unavailable',
),
);
return const Result(true);
} catch (e) {
return Result(InvalidStanzaFormat());
}
),
);
return const Result(true);
}
}