Skip to content

Commit

Permalink
Use already parsed MUC presence attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Dec 28, 2024
1 parent d827e7c commit f020650
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
52 changes: 25 additions & 27 deletions src/headless/plugins/muc/muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1801,52 +1801,51 @@ class MUC extends ModelWithMessages(ColorAwareModel(ChatBoxBase)) {

/**
* Given a presence stanza, update the occupant model based on its contents.
* @param {Element} pres - The presence stanza
* @param {MUCPresenceAttributes} attrs - The presence stanza
*/
updateOccupantsOnPresence(pres) {
const data = parseMUCPresence(pres, this);
if (data.type === 'error' || (!data.jid && !data.nick && !data.occupant_id)) {
updateOccupantsOnPresence(attrs) {
if (attrs.type === 'error' || (!attrs.jid && !attrs.nick && !attrs.occupant_id)) {
return true;
}

const occupant = this.occupants.findOccupant(data);
const occupant = this.occupants.findOccupant(attrs);
// Destroy an unavailable occupant if this isn't a nick change operation and if they're not affiliated
if (
data.type === 'unavailable' &&
attrs.type === 'unavailable' &&
occupant &&
!data.codes.includes(converse.MUC_NICK_CHANGED_CODE) &&
!['admin', 'owner', 'member'].includes(data['affiliation'])
!attrs.codes.includes(converse.MUC_NICK_CHANGED_CODE) &&
!['admin', 'owner', 'member'].includes(attrs['affiliation'])
) {
// Before destroying we set the new data, so that we can show the disconnection message
occupant.set(data);
// Before destroying we set the new attrs, so that we can show the disconnection message
occupant.set(attrs);
occupant.destroy();
return;
}

const jid = data.jid || '';
const attributes = {
...data,
const jid = attrs.jid || '';
const occupant_attrs = {
...attrs,
jid: Strophe.getBareJidFromJid(jid) || occupant?.attributes?.jid,
resource: Strophe.getResourceFromJid(jid) || occupant?.attributes?.resource,
};

if (data.is_self) {
if (attrs.is_self) {
let modified = false;
if (data.codes.includes(converse.MUC_NICK_CHANGED_CODE)) {
if (attrs.codes.includes(converse.MUC_NICK_CHANGED_CODE)) {
modified = true;
this.set('nick', data.nick);
this.set('nick', attrs.nick);
}
if (this.features.get(Strophe.NS.OCCUPANTID) && this.get('occupant-id') !== data.occupant_id) {
if (this.features.get(Strophe.NS.OCCUPANTID) && this.get('occupant-id') !== attrs.occupant_id) {
modified = true;
this.set('occupant_id', data.occupant_id);
this.set('occupant_id', attrs.occupant_id);
}
modified && this.save();
}

if (occupant) {
occupant.save(attributes);
occupant.save(occupant_attrs);
} else {
this.occupants.create(attributes);
this.occupants.create(occupant_attrs);
}
}

Expand Down Expand Up @@ -2740,12 +2739,12 @@ class MUC extends ModelWithMessages(ColorAwareModel(ChatBoxBase)) {
});

if (attrs.is_self) {
this.onOwnPresence(attrs, stanza);
this.onOwnPresence(attrs);
if (this.getOwnRole() !== 'none' && this.session.get('connection_status') === ROOMSTATUS.CONNECTING) {
this.session.save('connection_status', ROOMSTATUS.CONNECTED);
}
} else {
this.updateOccupantsOnPresence(stanza);
this.updateOccupantsOnPresence(attrs);
}
}

Expand All @@ -2761,9 +2760,8 @@ class MUC extends ModelWithMessages(ColorAwareModel(ChatBoxBase)) {
* auto-configured only if applicable and if the current
* user is the groupchat's owner.
* @param {MUCPresenceAttributes} attrs - The stanza
* @param {Element} stanza - The stanza
*/
async onOwnPresence(attrs, stanza) {
async onOwnPresence(attrs) {
await this.occupants.fetched;

if (attrs['type'] === 'unavailable') {
Expand All @@ -2777,13 +2775,13 @@ class MUC extends ModelWithMessages(ColorAwareModel(ChatBoxBase)) {
// only trigger afterwards, so that plugins can access the
// occupant in their event handlers.
this.session.save('connection_status', ROOMSTATUS.ENTERED, { 'silent': true });
this.updateOccupantsOnPresence(stanza);
this.updateOccupantsOnPresence(attrs);
this.session.trigger('change:connection_status', this.session, old_status);
} else {
this.updateOccupantsOnPresence(stanza);
this.updateOccupantsOnPresence(attrs);
}

const locked_room = stanza.querySelector("status[code='201']");
const locked_room = attrs.codes.includes('201');
if (locked_room) {
if (this.get('auto_configure')) {
await this.autoConfigureChatRoom().then(() => this.refreshDiscoInfo());
Expand Down
7 changes: 3 additions & 4 deletions src/headless/types/plugins/muc/muc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,9 @@ declare class MUC extends MUC_base {
sendUnregistrationIQ(): any;
/**
* Given a presence stanza, update the occupant model based on its contents.
* @param {Element} pres - The presence stanza
* @param {MUCPresenceAttributes} attrs - The presence stanza
*/
updateOccupantsOnPresence(pres: Element): boolean;
updateOccupantsOnPresence(attrs: import("./types").MUCPresenceAttributes): boolean;
/**
* @param {MUCMessageAttributes} attrs
*/
Expand Down Expand Up @@ -833,9 +833,8 @@ declare class MUC extends MUC_base {
* auto-configured only if applicable and if the current
* user is the groupchat's owner.
* @param {MUCPresenceAttributes} attrs - The stanza
* @param {Element} stanza - The stanza
*/
onOwnPresence(attrs: import("./types").MUCPresenceAttributes, stanza: Element): Promise<void>;
onOwnPresence(attrs: import("./types").MUCPresenceAttributes): Promise<void>;
/**
* Returns a boolean to indicate whether the current user
* was mentioned in a message.
Expand Down

0 comments on commit f020650

Please sign in to comment.