Procházet zdrojové kódy

Avoid creating Message objects for empty messages

JC Brand před 5 roky
rodič
revize
ed411c226d

+ 9 - 5
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);
                     }

+ 6 - 12
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) {

+ 11 - 0
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;