Avoid creating Message objects for empty messages

This commit is contained in:
JC Brand 2020-02-13 15:12:03 +01:00
parent f6f7d05c8b
commit ed411c226d
3 changed files with 26 additions and 17 deletions

View File

@ -87,7 +87,10 @@ converse.plugins.add('converse-chat', {
async initialize () { async initialize () {
this.initialized = u.getResolveablePromise(); 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') { if (this.get('type') === 'chat') {
ModelWithContact.prototype.initialize.apply(this, arguments); ModelWithContact.prototype.initialize.apply(this, arguments);
this.setRosterContact(Strophe.getBareJidFromJid(this.get('from'))); this.setRosterContact(Strophe.getBareJidFromJid(this.get('from')));
@ -106,6 +109,10 @@ converse.plugins.add('converse-chat', {
this.initialized.resolve(); this.initialized.resolve();
}, },
validate (attrs) {
return !u.shouldCreateMessage(attrs);
},
/** /**
* Sets an auto-destruct timer for this message, if it's is_ephemeral. * Sets an auto-destruct timer for this message, if it's is_ephemeral.
* @private * @private
@ -382,10 +389,7 @@ converse.plugins.add('converse-chat', {
return; return;
} }
this.setEditable(attrs, attrs.time, stanza); this.setEditable(attrs, attrs.time, stanza);
if (attrs['chat_state'] || if (u.shouldCreateMessage(attrs)) {
attrs['retracted'] || // Retraction received *before* the message
!u.isEmptyMessage(attrs)
) {
const msg = this.handleCorrection(attrs) || this.messages.create(attrs); const msg = this.handleCorrection(attrs) || this.messages.create(attrs);
this.incrementUnreadMsgCounter(msg); this.incrementUnreadMsgCounter(msg);
} }

View File

@ -256,6 +256,10 @@ converse.plugins.add('converse-muc', {
_converse.api.trigger('chatRoomMessageInitialized', this); _converse.api.trigger('chatRoomMessageInitialized', this);
}, },
validate (attrs) {
return !u.shouldCreateGroupchatMessage(attrs);
},
onOccupantRemoved () { onOccupantRemoved () {
this.stopListening(this.occupant); this.stopListening(this.occupant);
delete this.occupant; delete this.occupant;
@ -1710,15 +1714,6 @@ converse.plugins.add('converse-muc', {
return false; 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. * Handler for all MUC messages sent to this groupchat.
* @private * @private
@ -1764,14 +1759,13 @@ converse.plugins.add('converse-muc', {
} }
this.setEditable(attrs, attrs.time); this.setEditable(attrs, attrs.time);
if (attrs.nick && (attrs.is_tombstone || u.isNewMessage(attrs) || !u.isEmptyMessage(attrs))) { if (u.shouldCreateGroupchatMessage(attrs)) {
const msg = this.handleCorrection(attrs) || await this.createMessageObject(attrs); const msg = this.handleCorrection(attrs) || await this.messages.create(attrs, {promise: true});
this.incrementUnreadMsgCounter(msg); this.incrementUnreadMsgCounter(msg);
} }
_converse.api.trigger('message', {'stanza': original_stanza, 'chatbox': this}); _converse.api.trigger('message', {'stanza': original_stanza, 'chatbox': this});
}, },
handleModifyError(pres) { handleModifyError(pres) {
const text = get(pres.querySelector('error text'), 'textContent'); const text = get(pres.querySelector('error text'), 'textContent');
if (text) { if (text) {

View File

@ -123,6 +123,17 @@ u.isNewMessage = function (message) {
return !(message['is_delayed'] && message['is_archived']); 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) { u.isEmptyMessage = function (attrs) {
if (attrs instanceof Model) { if (attrs instanceof Model) {
attrs = attrs.attributes; attrs = attrs.attributes;