Browse Source

Prevent empty messages from being created

JC Brand 5 years ago
parent
commit
34c4b2aa5e
2 changed files with 16 additions and 0 deletions
  1. 15 0
      src/headless/converse-chat.js
  2. 1 0
      src/headless/converse-muc.js

+ 15 - 0
src/headless/converse-chat.js

@@ -86,6 +86,7 @@ converse.plugins.add('converse-chat', {
             },
 
             async initialize () {
+                if (!this.checkValidity()) { return; }
                 this.initialized = u.getResolveablePromise();
                 if (this.get('type') === 'chat') {
                     ModelWithContact.prototype.initialize.apply(this, arguments);
@@ -127,6 +128,20 @@ converse.plugins.add('converse-chat', {
                 }
             },
 
+            checkValidity () {
+                if (Object.keys(this.attributes).length === 3) {
+                    // XXX: This is an empty message with only the 3 default values.
+                    // This seems to happen when saving a newly created message
+                    // fails for some reason.
+                    // TODO: This is likely fixable by setting `wait` when
+                    // creating messages. See the wait-for-messages branch.
+                    this.validationError = "Empty message";
+                    this.safeDestroy();
+                    return false;
+                }
+                return true;
+            },
+
             safeDestroy () {
                 try {
                     this.destroy()

+ 1 - 0
src/headless/converse-muc.js

@@ -234,6 +234,7 @@ converse.plugins.add('converse-muc', {
         _converse.ChatRoomMessage = _converse.Message.extend({
 
             initialize () {
+                if (!this.checkValidity()) { return; }
                 if (this.get('file')) {
                     this.on('change:put', this.uploadFile, this);
                 }