瀏覽代碼

Refactor room auto-configuration

JC Brand 6 年之前
父節點
當前提交
346baa686f
共有 3 個文件被更改,包括 141 次插入115 次删除
  1. 48 39
      dist/converse.js
  2. 45 37
      src/headless/converse-muc.js
  3. 48 39
      src/headless/dist/converse-headless.js

+ 48 - 39
dist/converse.js

@@ -67514,50 +67514,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc
         });
       },
 
-      autoConfigureChatRoom() {
-        /* Automatically configure groupchat based on this model's
-         * 'roomconfig' data.
-         *
-         * Returns a promise which resolves once a response IQ has
-         * been received.
-         */
-        return new Promise((resolve, reject) => {
-          this.fetchRoomConfiguration().then(stanza => {
-            const configArray = [],
-                  fields = stanza.querySelectorAll('field'),
-                  config = this.get('roomconfig');
-            let count = fields.length;
-
-            _.each(fields, field => {
-              const fieldname = field.getAttribute('var').replace('muc#roomconfig_', ''),
-                    type = field.getAttribute('type');
-              let value;
-
-              if (fieldname in config) {
-                switch (type) {
-                  case 'boolean':
-                    value = config[fieldname] ? 1 : 0;
-                    break;
+      /**
+       * Given a <field> element, return a copy with a <value> child if
+       * we can find a value for it in this rooms config.
+       * @private
+       * @method _converse.ChatRoom#autoConfigureChatRoom
+       * @returns { Element }
+       */
+      addFieldValue(field) {
+        const type = field.getAttribute('type');
+        const fieldname = field.getAttribute('var').replace('muc#roomconfig_', '');
+        const config = this.get('roomconfig');
 
-                  case 'list-multi':
-                    // TODO: we don't yet handle "list-multi" types
-                    value = field.innerHTML;
-                    break;
+        if (fieldname in config) {
+          let value;
 
-                  default:
-                    value = config[fieldname];
-                }
+          switch (type) {
+            case 'boolean':
+              value = config[fieldname] ? 1 : 0;
+              break;
 
-                field.innerHTML = $build('value').t(value);
-              }
+            case 'list-multi':
+              // TODO: we don't yet handle "list-multi" types
+              value = field.innerHTML;
+              break;
 
-              configArray.push(field);
+            default:
+              value = config[fieldname];
+          }
 
-              if (! --count) {
-                this.sendConfiguration(configArray, resolve, reject);
-              }
-            });
-          });
+          field.innerHTML = $build('value').t(value);
+        }
+
+        return field;
+      },
+
+      /**
+       * Automatically configure the groupchat based on this model's
+       * 'roomconfig' data.
+       * @private
+       * @method _converse.ChatRoom#autoConfigureChatRoom
+       * @returns { promise }
+       * Returns a promise which resolves once a response IQ has
+       * been received.
+       */
+      autoConfigureChatRoom() {
+        return new Promise(async (resolve, reject) => {
+          const stanza = await this.fetchRoomConfiguration();
+          const fields = sizzle('field', stanza);
+          const configArray = fields.map(f => this.addFieldValue(f));
+
+          if (configArray.length) {
+            this.sendConfiguration(configArray, resolve, reject);
+          }
         });
       },
 

+ 45 - 37
src/headless/converse-muc.js

@@ -644,44 +644,52 @@ converse.plugins.add('converse-muc', {
                 });
             },
 
+            /**
+             * Given a <field> element, return a copy with a <value> child if
+             * we can find a value for it in this rooms config.
+             * @private
+             * @method _converse.ChatRoom#autoConfigureChatRoom
+             * @returns { Element }
+             */
+            addFieldValue (field) {
+                const type = field.getAttribute('type');
+                const fieldname = field.getAttribute('var').replace('muc#roomconfig_', '');
+                const config = this.get('roomconfig');
+                if (fieldname in config) {
+                    let value;
+                    switch (type) {
+                        case 'boolean':
+                            value = config[fieldname] ? 1 : 0;
+                            break;
+                        case 'list-multi':
+                            // TODO: we don't yet handle "list-multi" types
+                            value = field.innerHTML;
+                            break;
+                        default:
+                            value = config[fieldname];
+                    }
+                    field.innerHTML = $build('value').t(value);
+                }
+                return field;
+            },
+
+            /**
+             * Automatically configure the groupchat based on this model's
+             * 'roomconfig' data.
+             * @private
+             * @method _converse.ChatRoom#autoConfigureChatRoom
+             * @returns { promise }
+             * Returns a promise which resolves once a response IQ has
+             * been received.
+             */
             autoConfigureChatRoom () {
-                /* Automatically configure groupchat based on this model's
-                 * 'roomconfig' data.
-                 *
-                 * Returns a promise which resolves once a response IQ has
-                 * been received.
-                 */
-                return new Promise((resolve, reject) => {
-                    this.fetchRoomConfiguration().then((stanza) => {
-                        const configArray = [],
-                            fields = stanza.querySelectorAll('field'),
-                            config = this.get('roomconfig');
-                        let count = fields.length;
-
-                        _.each(fields, (field) => {
-                            const fieldname = field.getAttribute('var').replace('muc#roomconfig_', ''),
-                                type = field.getAttribute('type');
-                            let value;
-                            if (fieldname in config) {
-                                switch (type) {
-                                    case 'boolean':
-                                        value = config[fieldname] ? 1 : 0;
-                                        break;
-                                    case 'list-multi':
-                                        // TODO: we don't yet handle "list-multi" types
-                                        value = field.innerHTML;
-                                        break;
-                                    default:
-                                        value = config[fieldname];
-                                }
-                                field.innerHTML = $build('value').t(value);
-                            }
-                            configArray.push(field);
-                            if (!--count) {
-                                this.sendConfiguration(configArray, resolve, reject);
-                            }
-                        });
-                    });
+                return new Promise(async (resolve, reject) => {
+                    const stanza = await this.fetchRoomConfiguration();
+                    const fields = sizzle('field', stanza);
+                    const configArray = fields.map(f => this.addFieldValue(f))
+                    if (configArray.length) {
+                        this.sendConfiguration(configArray, resolve, reject);
+                    }
                 });
             },
 

+ 48 - 39
src/headless/dist/converse-headless.js

@@ -45762,50 +45762,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc
         });
       },
 
-      autoConfigureChatRoom() {
-        /* Automatically configure groupchat based on this model's
-         * 'roomconfig' data.
-         *
-         * Returns a promise which resolves once a response IQ has
-         * been received.
-         */
-        return new Promise((resolve, reject) => {
-          this.fetchRoomConfiguration().then(stanza => {
-            const configArray = [],
-                  fields = stanza.querySelectorAll('field'),
-                  config = this.get('roomconfig');
-            let count = fields.length;
-
-            _.each(fields, field => {
-              const fieldname = field.getAttribute('var').replace('muc#roomconfig_', ''),
-                    type = field.getAttribute('type');
-              let value;
-
-              if (fieldname in config) {
-                switch (type) {
-                  case 'boolean':
-                    value = config[fieldname] ? 1 : 0;
-                    break;
+      /**
+       * Given a <field> element, return a copy with a <value> child if
+       * we can find a value for it in this rooms config.
+       * @private
+       * @method _converse.ChatRoom#autoConfigureChatRoom
+       * @returns { Element }
+       */
+      addFieldValue(field) {
+        const type = field.getAttribute('type');
+        const fieldname = field.getAttribute('var').replace('muc#roomconfig_', '');
+        const config = this.get('roomconfig');
 
-                  case 'list-multi':
-                    // TODO: we don't yet handle "list-multi" types
-                    value = field.innerHTML;
-                    break;
+        if (fieldname in config) {
+          let value;
 
-                  default:
-                    value = config[fieldname];
-                }
+          switch (type) {
+            case 'boolean':
+              value = config[fieldname] ? 1 : 0;
+              break;
 
-                field.innerHTML = $build('value').t(value);
-              }
+            case 'list-multi':
+              // TODO: we don't yet handle "list-multi" types
+              value = field.innerHTML;
+              break;
 
-              configArray.push(field);
+            default:
+              value = config[fieldname];
+          }
 
-              if (! --count) {
-                this.sendConfiguration(configArray, resolve, reject);
-              }
-            });
-          });
+          field.innerHTML = $build('value').t(value);
+        }
+
+        return field;
+      },
+
+      /**
+       * Automatically configure the groupchat based on this model's
+       * 'roomconfig' data.
+       * @private
+       * @method _converse.ChatRoom#autoConfigureChatRoom
+       * @returns { promise }
+       * Returns a promise which resolves once a response IQ has
+       * been received.
+       */
+      autoConfigureChatRoom() {
+        return new Promise(async (resolve, reject) => {
+          const stanza = await this.fetchRoomConfiguration();
+          const fields = sizzle('field', stanza);
+          const configArray = fields.map(f => this.addFieldValue(f));
+
+          if (configArray.length) {
+            this.sendConfiguration(configArray, resolve, reject);
+          }
         });
       },