From f0206500ea9278f40674a8a70d4c362c26ab3d18 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Sat, 28 Dec 2024 09:14:57 +0200 Subject: [PATCH] Use already parsed MUC presence attributes --- src/headless/plugins/muc/muc.js | 52 ++++++++++++------------- src/headless/types/plugins/muc/muc.d.ts | 7 ++-- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/headless/plugins/muc/muc.js b/src/headless/plugins/muc/muc.js index 9215e535a4..ee28b98164 100644 --- a/src/headless/plugins/muc/muc.js +++ b/src/headless/plugins/muc/muc.js @@ -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); } } @@ -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); } } @@ -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') { @@ -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()); diff --git a/src/headless/types/plugins/muc/muc.d.ts b/src/headless/types/plugins/muc/muc.d.ts index 43282fb846..a822457347 100644 --- a/src/headless/types/plugins/muc/muc.d.ts +++ b/src/headless/types/plugins/muc/muc.d.ts @@ -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 */ @@ -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; + onOwnPresence(attrs: import("./types").MUCPresenceAttributes): Promise; /** * Returns a boolean to indicate whether the current user * was mentioned in a message.