feat(xep): Track our own affiliation and role

This commit is contained in:
PapaTutuWawa 2023-09-29 22:45:56 +02:00
parent 882d20dc7a
commit e97d6e6517
3 changed files with 43 additions and 13 deletions

View File

@ -3,14 +3,25 @@ import 'package:moxxmpp/src/jid.dart';
import 'package:moxxmpp/src/xeps/xep_0045/types.dart';
/// Triggered when the MUC changes our nickname.
class NickChangedByMUCEvent extends XmppEvent {
NickChangedByMUCEvent(this.roomJid, this.nick);
class OwnDataChangedEvent extends XmppEvent {
OwnDataChangedEvent(
this.roomJid,
this.nick,
this.affiliation,
this.role,
);
/// The JID of the room.
final JID roomJid;
/// The new nickname.
/// Our nickname.
final String nick;
/// Our affiliation.
final Affiliation affiliation;
/// Our role.
final Role role;
}
/// Triggered when an entity joins the MUC.

View File

@ -149,6 +149,13 @@ class RoomState {
/// Flag whether we're joined and can process messages
bool joined;
/// Our own affiliation inside the MUC.
Affiliation? affiliation;
/// Our own role inside the MUC.
Role? role;
/// The list of messages that we sent and are waiting for their echo.
late final List<PendingMessage> pendingMessages;
/// "List" of entities inside the MUC.

View File

@ -300,20 +300,32 @@ class MUCManager extends XmppManagerBase {
final role = Role.fromString(
item.attributes['role']! as String,
);
final affiliation = Affiliation.fromString(
item.attributes['affiliation']! as String,
);
if (statuses.contains('110')) {
if (room.nick != from.resource) {
// Notify us of the changed nick.
getAttributes().sendEvent(
NickChangedByMUCEvent(
bareFrom,
from.resource,
),
);
if (room.joined) {
if (room.nick != from.resource ||
room.affiliation != affiliation ||
room.role != role) {
// Notify us of the changed data.
getAttributes().sendEvent(
OwnDataChangedEvent(
bareFrom,
from.resource,
affiliation,
role,
),
);
}
}
// Set the nick to make sure we're in sync with the MUC.
room.nick = from.resource;
// Set the data to make sure we're in sync with the MUC.
room
..nick = from.resource
..affiliation = affiliation
..role = role;
logger.finest('Self-presence handled');
return StanzaHandlerData(
true,