Commit 79bfb456 authored by JC Brand's avatar JC Brand

Use async/await in MAM code and tests

parent 6e05f3b6
This diff is collapsed.
...@@ -331,10 +331,10 @@ converse.plugins.add('converse-omemo', { ...@@ -331,10 +331,10 @@ converse.plugins.add('converse-omemo', {
} }
}, },
getMessageAttributesFromStanza (stanza, original_stanza) { async getMessageAttributesFromStanza (stanza, original_stanza) {
const { _converse } = this.__super__, const { _converse } = this.__super__,
encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(), encrypted = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, original_stanza).pop(),
attrs = this.__super__.getMessageAttributesFromStanza.apply(this, arguments); attrs = await this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
if (!encrypted || !_converse.config.get('trusted')) { if (!encrypted || !_converse.config.get('trusted')) {
return attrs; return attrs;
......
...@@ -557,31 +557,22 @@ converse.plugins.add('converse-chatboxes', { ...@@ -557,31 +557,22 @@ converse.plugins.add('converse-chatboxes', {
return attrs; return attrs;
}, },
createMessage (message, original_stanza) { async createMessage (message, original_stanza) {
/* Create a Backbone.Message object inside this chat box /* Create a Backbone.Message object inside this chat box
* based on the identified message stanza. * based on the identified message stanza.
*/ */
const that = this; const attrs = await this.getMessageAttributesFromStanza(message, original_stanza),
function _create (attrs) { is_csn = u.isOnlyChatStateNotification(attrs);
const is_csn = u.isOnlyChatStateNotification(attrs);
if (is_csn && (attrs.is_delayed || if (is_csn && (attrs.is_delayed || (attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == this.get('nick')))) {
(attrs.type === 'groupchat' && Strophe.getResourceFromJid(attrs.from) == that.get('nick')))) { // XXX: MUC leakage
// XXX: MUC leakage // No need showing delayed or our own CSN messages
// No need showing delayed or our own CSN messages return;
return; } else if (!is_csn && !attrs.file && !attrs.plaintext && !attrs.message && !attrs.oob_url && attrs.type !== 'error') {
} else if (!is_csn && !attrs.file && !attrs.plaintext && !attrs.message && !attrs.oob_url && attrs.type !== 'error') { // TODO: handle <subject> messages (currently being done by ChatRoom)
// TODO: handle <subject> messages (currently being done by ChatRoom) return;
return;
} else {
return that.messages.create(attrs);
}
}
const result = this.getMessageAttributesFromStanza(message, original_stanza)
if (typeof result.then === "function") {
return new Promise((resolve, reject) => result.then(attrs => resolve(_create(attrs))));
} else { } else {
const message = _create(result) return this.messages.create(attrs);
return Promise.resolve(message);
} }
}, },
......
...@@ -133,20 +133,10 @@ converse.plugins.add('converse-mam', { ...@@ -133,20 +133,10 @@ converse.plugins.add('converse-mam', {
// New functions which don't exist yet can also be added. // New functions which don't exist yet can also be added.
ChatBox: { ChatBox: {
getMessageAttributesFromStanza (message, original_stanza) { async getMessageAttributesFromStanza (message, original_stanza) {
function _process (attrs) { const attrs = await this.__super__.getMessageAttributesFromStanza.apply(this, arguments);
const archive_id = getMessageArchiveID(original_stanza); attrs.archive_id = getMessageArchiveID(original_stanza);
if (archive_id) { return attrs;
attrs.archive_id = archive_id;
}
return attrs;
}
const result = this.__super__.getMessageAttributesFromStanza.apply(this, arguments)
if (result instanceof Promise) {
return new Promise((resolve, reject) => result.then((attrs) => resolve(_process(attrs))).catch(reject));
} else {
return _process(result);
}
} }
}, },
......
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