Commit 346baa68 authored by JC Brand's avatar JC Brand

Refactor room auto-configuration

parent 273b9584
...@@ -67514,50 +67514,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc ...@@ -67514,50 +67514,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc
}); });
}, },
autoConfigureChatRoom() { /**
/* Automatically configure groupchat based on this model's * Given a <field> element, return a copy with a <value> child if
* 'roomconfig' data. * we can find a value for it in this rooms config.
* * @private
* Returns a promise which resolves once a response IQ has * @method _converse.ChatRoom#autoConfigureChatRoom
* been received. * @returns { Element }
*/ */
return new Promise((resolve, reject) => { addFieldValue(field) {
this.fetchRoomConfiguration().then(stanza => { const type = field.getAttribute('type');
const configArray = [], const fieldname = field.getAttribute('var').replace('muc#roomconfig_', '');
fields = stanza.querySelectorAll('field'), const config = this.get('roomconfig');
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': if (fieldname in config) {
// TODO: we don't yet handle "list-multi" types let value;
value = field.innerHTML;
break;
default: switch (type) {
value = config[fieldname]; 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) { field.innerHTML = $build('value').t(value);
this.sendConfiguration(configArray, resolve, reject); }
}
}); 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);
}
}); });
}, },
...@@ -644,44 +644,52 @@ converse.plugins.add('converse-muc', { ...@@ -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 () { autoConfigureChatRoom () {
/* Automatically configure groupchat based on this model's return new Promise(async (resolve, reject) => {
* 'roomconfig' data. const stanza = await this.fetchRoomConfiguration();
* const fields = sizzle('field', stanza);
* Returns a promise which resolves once a response IQ has const configArray = fields.map(f => this.addFieldValue(f))
* been received. if (configArray.length) {
*/ this.sendConfiguration(configArray, resolve, reject);
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);
}
});
});
}); });
}, },
......
...@@ -45762,50 +45762,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc ...@@ -45762,50 +45762,59 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc
}); });
}, },
autoConfigureChatRoom() { /**
/* Automatically configure groupchat based on this model's * Given a <field> element, return a copy with a <value> child if
* 'roomconfig' data. * we can find a value for it in this rooms config.
* * @private
* Returns a promise which resolves once a response IQ has * @method _converse.ChatRoom#autoConfigureChatRoom
* been received. * @returns { Element }
*/ */
return new Promise((resolve, reject) => { addFieldValue(field) {
this.fetchRoomConfiguration().then(stanza => { const type = field.getAttribute('type');
const configArray = [], const fieldname = field.getAttribute('var').replace('muc#roomconfig_', '');
fields = stanza.querySelectorAll('field'), const config = this.get('roomconfig');
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': if (fieldname in config) {
// TODO: we don't yet handle "list-multi" types let value;
value = field.innerHTML;
break;
default: switch (type) {
value = config[fieldname]; 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) { field.innerHTML = $build('value').t(value);
this.sendConfiguration(configArray, resolve, reject); }
}
}); 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);
}
}); });
}, },
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment