Use async/await and don't swallow errors.

Also, handle the error in `getRoomFeatures`
This commit is contained in:
JC Brand 2019-01-10 13:09:14 +01:00
parent a4d608dcdf
commit 96f38150f9
3 changed files with 38 additions and 54 deletions

43
dist/converse.js vendored
View File

@ -64630,25 +64630,18 @@ _converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins.add('converse-dis
this.items.fetch();
},
getIdentity(category, type) {
async getIdentity(category, type) {
/* Returns a Promise which resolves with a map indicating
* whether a given identity is provided.
* whether a given identity is provided by this entity.
*
* Parameters:
* (String) category - The identity category
* (String) type - The identity type
*/
const entity = this;
return new Promise((resolve, reject) => {
function fulfillPromise() {
const model = entity.identities.findWhere({
'category': category,
'type': type
});
resolve(model);
}
entity.waitUntilFeaturesDiscovered.then(fulfillPromise).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
await this.waitUntilFeaturesDiscovered;
return this.identities.findWhere({
'category': category,
'type': type
});
},
@ -66590,26 +66583,26 @@ _converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].plugins.add('converse-muc
return this.getRoomFeatures();
},
async getRoomIdentity() {
const _ref = await Promise.all([_converse.api.disco.getIdentity('conference', 'text', this.get('jid')), _converse.api.disco.getFields(this.get('jid'))]),
_ref2 = _slicedToArray(_ref, 2),
identity = _ref2[0],
fields = _ref2[1];
async getRoomFeatures() {
let identity;
try {
identity = await _converse.api.disco.getIdentity('conference', 'text', this.get('jid'));
} catch (e) {
// Getting the identity probably failed because this room doesn't exist yet.
return _converse.log(e, Strophe.LogLevel.ERROR);
}
const fields = await _converse.api.disco.getFields(this.get('jid'));
this.save({
'name': identity && identity.get('name'),
'description': _.get(fields.findWhere({
'var': "muc#roominfo_description"
}), 'attributes.value')
});
},
const features = await _converse.api.disco.getFeatures(this.get('jid'));
async getRoomFeatures() {
// XXX: not sure whet the right place is to get the room identitiy
this.getRoomIdentity();
const features = await _converse.api.disco.getFeatures(this.get('jid')),
attrs = _.extend(_.zipObject(_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].ROOM_FEATURES, _.map(_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].ROOM_FEATURES, _.stubFalse)), {
const attrs = _.extend(_.zipObject(_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].ROOM_FEATURES, _.map(_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].ROOM_FEATURES, _.stubFalse)), {
'fetched': moment().format()
});

View File

@ -64,26 +64,18 @@ converse.plugins.add('converse-disco', {
this.items.fetch();
},
getIdentity (category, type) {
async getIdentity (category, type) {
/* Returns a Promise which resolves with a map indicating
* whether a given identity is provided.
* whether a given identity is provided by this entity.
*
* Parameters:
* (String) category - The identity category
* (String) type - The identity type
*/
const entity = this;
return new Promise((resolve, reject) => {
function fulfillPromise () {
const model = entity.identities.findWhere({
'category': category,
'type': type
});
resolve(model);
}
entity.waitUntilFeaturesDiscovered
.then(fulfillPromise)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
await this.waitUntilFeaturesDiscovered;
return this.identities.findWhere({
'category': category,
'type': type
});
},

View File

@ -502,26 +502,25 @@ converse.plugins.add('converse-muc', {
return this.getRoomFeatures();
},
async getRoomIdentity () {
const [identity, fields] = await Promise.all([
_converse.api.disco.getIdentity('conference', 'text', this.get('jid')),
_converse.api.disco.getFields(this.get('jid'))
]);
async getRoomFeatures () {
let identity;
try {
identity = await _converse.api.disco.getIdentity('conference', 'text', this.get('jid'));
} catch (e) {
// Getting the identity probably failed because this room doesn't exist yet.
return _converse.log(e, Strophe.LogLevel.ERROR);
}
const fields = await _converse.api.disco.getFields(this.get('jid'));
this.save({
'name': identity && identity.get('name'),
'description': _.get(fields.findWhere({'var': "muc#roominfo_description"}), 'attributes.value')
});
},
async getRoomFeatures () {
// XXX: not sure whet the right place is to get the room identitiy
this.getRoomIdentity();
const features = await _converse.api.disco.getFeatures(this.get('jid')),
attrs = _.extend(
_.zipObject(converse.ROOM_FEATURES, _.map(converse.ROOM_FEATURES, _.stubFalse)),
{'fetched': moment().format()}
);
const features = await _converse.api.disco.getFeatures(this.get('jid'));
const attrs = _.extend(
_.zipObject(converse.ROOM_FEATURES, _.map(converse.ROOM_FEATURES, _.stubFalse)),
{'fetched': moment().format()}
);
features.each(feature => {
const fieldname = feature.get('var');
if (!fieldname.startsWith('muc_')) {