From ed411c226d249a237278dd62df0531754796eff0 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 13 Feb 2020 15:12:03 +0100 Subject: [PATCH] Avoid creating Message objects for empty messages --- src/headless/converse-chat.js | 14 +++++++++----- src/headless/converse-muc.js | 18 ++++++------------ src/headless/utils/core.js | 11 +++++++++++ 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/headless/converse-chat.js b/src/headless/converse-chat.js index 84ba3db2c..7c5a349b6 100644 --- a/src/headless/converse-chat.js +++ b/src/headless/converse-chat.js @@ -87,7 +87,10 @@ converse.plugins.add('converse-chat', { async initialize () { this.initialized = u.getResolveablePromise(); - + this.on('invalid', () => { + log.error("Message not created due to validation error!"); + log.error(this.toJSON()); + }); if (this.get('type') === 'chat') { ModelWithContact.prototype.initialize.apply(this, arguments); this.setRosterContact(Strophe.getBareJidFromJid(this.get('from'))); @@ -106,6 +109,10 @@ converse.plugins.add('converse-chat', { this.initialized.resolve(); }, + validate (attrs) { + return !u.shouldCreateMessage(attrs); + }, + /** * Sets an auto-destruct timer for this message, if it's is_ephemeral. * @private @@ -382,10 +389,7 @@ converse.plugins.add('converse-chat', { return; } this.setEditable(attrs, attrs.time, stanza); - if (attrs['chat_state'] || - attrs['retracted'] || // Retraction received *before* the message - !u.isEmptyMessage(attrs) - ) { + if (u.shouldCreateMessage(attrs)) { const msg = this.handleCorrection(attrs) || this.messages.create(attrs); this.incrementUnreadMsgCounter(msg); } diff --git a/src/headless/converse-muc.js b/src/headless/converse-muc.js index 1e7135b02..2fe54a05c 100644 --- a/src/headless/converse-muc.js +++ b/src/headless/converse-muc.js @@ -256,6 +256,10 @@ converse.plugins.add('converse-muc', { _converse.api.trigger('chatRoomMessageInitialized', this); }, + validate (attrs) { + return !u.shouldCreateGroupchatMessage(attrs); + }, + onOccupantRemoved () { this.stopListening(this.occupant); delete this.occupant; @@ -1710,15 +1714,6 @@ converse.plugins.add('converse-muc', { return false; }, - createMessageObject (attrs) { - return new Promise((success, reject) => { - this.messages.create( - attrs, - { success, 'error': (m, e) => reject(e) } - ) - }); - }, - /** * Handler for all MUC messages sent to this groupchat. * @private @@ -1764,14 +1759,13 @@ converse.plugins.add('converse-muc', { } this.setEditable(attrs, attrs.time); - if (attrs.nick && (attrs.is_tombstone || u.isNewMessage(attrs) || !u.isEmptyMessage(attrs))) { - const msg = this.handleCorrection(attrs) || await this.createMessageObject(attrs); + if (u.shouldCreateGroupchatMessage(attrs)) { + const msg = this.handleCorrection(attrs) || await this.messages.create(attrs, {promise: true}); this.incrementUnreadMsgCounter(msg); } _converse.api.trigger('message', {'stanza': original_stanza, 'chatbox': this}); }, - handleModifyError(pres) { const text = get(pres.querySelector('error text'), 'textContent'); if (text) { diff --git a/src/headless/utils/core.js b/src/headless/utils/core.js index 7812934fc..ce2697ed4 100644 --- a/src/headless/utils/core.js +++ b/src/headless/utils/core.js @@ -123,6 +123,17 @@ u.isNewMessage = function (message) { return !(message['is_delayed'] && message['is_archived']); }; +u.shouldCreateMessage = function (attrs) { + return attrs['chat_state'] || + attrs['retracted'] || // Retraction received *before* the message + !u.isEmptyMessage(attrs); +} + +u.shouldCreateGroupchatMessage = function (attrs) { + return attrs.nick && (u.shouldCreateMessage(attrs) || attrs.is_tombstone); +}, + + u.isEmptyMessage = function (attrs) { if (attrs instanceof Model) { attrs = attrs.attributes;