浏览代码

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

JC Brand 5 年之前
父节点
当前提交
e2a7045e22
共有 2 个文件被更改,包括 41 次插入44 次删除
  1. 1 44
      src/headless/converse-muc.js
  2. 40 0
      src/headless/utils/stanza.js

+ 1 - 44
src/headless/converse-muc.js

@@ -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

+ 40 - 0
src/headless/utils/stanza.js

@@ -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;
     }
 }