浏览代码

Reuse XEP-359 unique and stable stanza ids for Message `id`.

This helps us to avoid duplicates by relying on the fact that Backbone
models have unique ids.
JC Brand 6 年之前
父节点
当前提交
be6a5d9c37
共有 3 个文件被更改,包括 47 次插入11 次删除
  1. 25 6
      dist/converse.js
  2. 2 1
      src/converse-chatview.js
  3. 20 4
      src/headless/converse-chatboxes.js

+ 25 - 6
dist/converse.js

@@ -49782,9 +49782,12 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_5__["default"].plugins
       },
 
       onDrop(evt) {
-        if (evt.dataTransfer.files.length == 0) // There are no files to be dropped, so this isn’t a file
+        if (evt.dataTransfer.files.length == 0) {
+          // There are no files to be dropped, so this isn’t a file
           // transfer operation.
           return;
+        }
+
         evt.preventDefault();
         this.model.sendFiles(evt.dataTransfer.files);
       },
@@ -62018,7 +62021,7 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
         if (markers.length === 0) {
           return false;
         } else if (markers.length > 1) {
-          _converse.log('onMessage: Ignoring incoming stanza with multiple message markers', Strophe.LogLevel.ERROR);
+          _converse.log('handleChatMarker: Ignoring incoming stanza with multiple message markers', Strophe.LogLevel.ERROR);
 
           _converse.log(stanza, Strophe.LogLevel.ERROR);
 
@@ -62187,10 +62190,12 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
       },
 
       getOutgoingMessageAttributes(text, spoiler_hint) {
-        const is_spoiler = this.get('composing_spoiler');
+        const is_spoiler = this.get('composing_spoiler'),
+              origin_id = _converse.connection.getUniqueId();
+
         return _.extend(this.toJSON(), {
-          'id': _converse.connection.getUniqueId(),
-          'origin_id': _converse.connection.getUniqueId(),
+          'id': origin_id,
+          'origin_id': origin_id,
           'fullname': _converse.xmppstatus.get('fullname'),
           'from': _converse.bare_jid,
           'sender': 'me',
@@ -62315,6 +62320,12 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
       },
 
       getStanzaIDs(stanza) {
+        /* Extract the XEP-0359 stanza IDs from the passed in stanza
+         * and return a map containing them.
+         *
+         * Parameters:
+         *    (XMLElement) stanza - The message stanza
+         */
         const attrs = {};
         const stanza_ids = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, stanza);
 
@@ -62329,6 +62340,12 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
           attrs[`stanza_id ${by_jid}`] = result.getAttribute('id');
         }
 
+        const origin_id = sizzle(`origin-id[xmlns="${Strophe.NS.SID}"]`, stanza).pop();
+
+        if (origin_id) {
+          attrs['origin_id'] = origin_id.getAttribute('id');
+        }
+
         return attrs;
       },
 
@@ -62388,8 +62405,10 @@ _converse_core__WEBPACK_IMPORTED_MODULE_2__["default"].plugins.add('converse-cha
 
         if (spoiler) {
           attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
-        }
+        } // We prefer to use one of the XEP-0359 unique and stable stanza IDs as the Model id, to avoid duplicates.
+
 
+        attrs['id'] = attrs['origin_id'] || attrs[`stanza_id ${attrs.from}`] || _converse.connection.getUniqueId();
         return attrs;
       },
 

+ 2 - 1
src/converse-chatview.js

@@ -405,10 +405,11 @@ converse.plugins.add('converse-chatview', {
             },
 
             onDrop (evt) {
-                if (evt.dataTransfer.files.length == 0)
+                if (evt.dataTransfer.files.length == 0) {
                     // There are no files to be dropped, so this isn’t a file
                     // transfer operation.
                     return;
+                }
                 evt.preventDefault();
                 this.model.sendFiles(evt.dataTransfer.files);
             },

+ 20 - 4
src/headless/converse-chatboxes.js

@@ -380,7 +380,7 @@ converse.plugins.add('converse-chatboxes', {
                     return false;
                 } else if (markers.length > 1) {
                     _converse.log(
-                        'onMessage: Ignoring incoming stanza with multiple message markers',
+                        'handleChatMarker: Ignoring incoming stanza with multiple message markers',
                         Strophe.LogLevel.ERROR
                     );
                     _converse.log(stanza, Strophe.LogLevel.ERROR);
@@ -508,10 +508,12 @@ converse.plugins.add('converse-chatboxes', {
             },
 
             getOutgoingMessageAttributes (text, spoiler_hint) {
-                const is_spoiler = this.get('composing_spoiler');
+                const is_spoiler = this.get('composing_spoiler'),
+                      origin_id = _converse.connection.getUniqueId();
+
                 return _.extend(this.toJSON(), {
-                    'id': _converse.connection.getUniqueId(),
-                    'origin_id': _converse.connection.getUniqueId(),
+                    'id': origin_id,
+                    'origin_id': origin_id,
                     'fullname': _converse.xmppstatus.get('fullname'),
                     'from': _converse.bare_jid,
                     'sender': 'me',
@@ -628,6 +630,12 @@ converse.plugins.add('converse-chatboxes', {
             },
 
             getStanzaIDs (stanza) {
+                /* Extract the XEP-0359 stanza IDs from the passed in stanza
+                 * and return a map containing them.
+                 *
+                 * Parameters:
+                 *    (XMLElement) stanza - The message stanza
+                 */
                 const attrs = {};
                 const stanza_ids = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, stanza);
                 if (stanza_ids.length) {
@@ -638,6 +646,11 @@ converse.plugins.add('converse-chatboxes', {
                     const by_jid = stanza.getAttribute('from');
                     attrs[`stanza_id ${by_jid}`] = result.getAttribute('id');
                 }
+
+                const origin_id = sizzle(`origin-id[xmlns="${Strophe.NS.SID}"]`, stanza).pop();
+                if (origin_id) {
+                    attrs['origin_id'] = origin_id.getAttribute('id');
+                }
                 return attrs;
             },
 
@@ -678,6 +691,7 @@ converse.plugins.add('converse-chatboxes', {
                     'type': stanza.getAttribute('type')
                 }, this.getStanzaIDs(original_stanza));
 
+
                 if (attrs.type === 'groupchat') {
                     attrs.from = stanza.getAttribute('from');
                     attrs.nick = Strophe.unescapeNode(Strophe.getResourceFromJid(attrs.from));
@@ -699,6 +713,8 @@ converse.plugins.add('converse-chatboxes', {
                 if (spoiler) {
                     attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
                 }
+                // We prefer to use one of the XEP-0359 unique and stable stanza IDs as the Model id, to avoid duplicates.
+                attrs['id'] = attrs['origin_id'] || attrs[`stanza_id ${attrs.from}`] || _converse.connection.getUniqueId();
                 return attrs;
             },