Move MUC presence parsing code to src/headless/utils/stanza.js

This commit is contained in:
JC Brand 2020-04-13 15:29:40 +02:00
parent a1d5563963
commit e2a7045e22
2 changed files with 41 additions and 44 deletions

View File

@ -1566,7 +1566,7 @@ converse.plugins.add('converse-muc', {
* @param { XMLElement } pres - The presence stanza
*/
updateOccupantsOnPresence (pres) {
const data = st.parseMUCPresenceStanza(pres);
const data = st.parseMUCPresence(pres);
if (data.type === 'error' || (!data.jid && !data.nick)) {
return true;
}
@ -1594,49 +1594,6 @@ converse.plugins.add('converse-muc', {
}
},
parsePresence (pres) {
const from = pres.getAttribute("from"),
type = pres.getAttribute("type"),
data = {
'from': from,
'nick': Strophe.getResourceFromJid(from),
'type': type,
'states': [],
'show': type !== 'unavailable' ? 'online' : 'offline'
};
pres.childNodes.forEach(child => {
switch (child.nodeName) {
case "status":
data.status = child.textContent || null;
break;
case "show":
data.show = child.textContent || 'online';
break;
case "x":
if (child.getAttribute("xmlns") === Strophe.NS.MUC_USER) {
child.childNodes.forEach(item => {
switch (item.nodeName) {
case "item":
data.affiliation = item.getAttribute("affiliation");
data.role = item.getAttribute("role");
data.jid = item.getAttribute("jid");
data.nick = item.getAttribute("nick") || data.nick;
break;
case "status":
if (item.getAttribute("code")) {
data.states.push(item.getAttribute("code"));
}
}
});
} else if (child.getAttribute("xmlns") === Strophe.NS.VCARDUPDATE) {
data.image_hash = child.querySelector('photo')?.textContent;
}
}
});
return data;
},
fetchFeaturesIfConfigurationChanged (stanza) {
// 104: configuration change
// 170: logging enabled

View File

@ -350,6 +350,46 @@ const stanza_utils = {
// as the Model id, to avoid duplicates.
attrs['id'] = attrs['origin_id'] || attrs[`stanza_id ${(attrs.from_muc || attrs.from)}`] || u.getUniqueId();
return attrs;
},
/**
* Parses a passed in MUC presence stanza and returns an object of attributes.
* @private
* @method stanza_utils#parseMUCPresence
* @param { XMLElement } stanza - The presence stanza
* @returns { Object }
*/
parseMUCPresence (stanza) {
const from = stanza.getAttribute("from");
const type = stanza.getAttribute("type");
const data = {
'from': from,
'nick': Strophe.getResourceFromJid(from),
'type': type,
'states': [],
'show': type !== 'unavailable' ? 'online' : 'offline'
};
Array.from(stanza.children).forEach(child => {
if (child.matches('status')) {
data.status = child.textContent || null;
} else if (child.matches('show')) {
data.show = child.textContent || 'online';
} else if (child.matches('x') && child.getAttribute('xmlns') === Strophe.NS.MUC_USER) {
Array.from(child.children).forEach(item => {
if (item.nodeName === "item") {
data.affiliation = item.getAttribute("affiliation");
data.role = item.getAttribute("role");
data.jid = item.getAttribute("jid");
data.nick = item.getAttribute("nick") || data.nick;
} else if (item.nodeName == 'status' && item.getAttribute("code")) {
data.states.push(item.getAttribute("code"));
}
});
} else if (child.matches('x') && child.getAttribute('xmlns') === Strophe.NS.VCARDUPDATE) {
data.image_hash = child.querySelector('photo')?.textContent;
}
});
return data;
}
}