Jelajahi Sumber

Add code to check for duplicates

JC Brand 7 tahun lalu
induk
melakukan
31e884f79a
3 mengubah file dengan 61 tambahan dan 5 penghapusan
  1. 33 0
      spec/mam.js
  2. 26 3
      src/converse-mam.js
  3. 2 2
      src/converse-muc.js

+ 33 - 0
spec/mam.js

@@ -13,6 +13,39 @@
     describe("Message Archive Management", function () {
         // Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config
 
+        describe("Archived Messages", function () {
+
+           it("aren't shown as duplicates", 
+                mock.initConverseWithPromises(
+                    null, ['discoInitialized'], {},
+                    function (done, _converse) {
+
+                test_utils.openAndEnterChatRoom(_converse, 'trek-radio', 'conference.lightwitch.org', 'jcbrand').then(function () {
+                    var chatroomview = _converse.chatboxviews.get('trek-radio@conference.lightwitch.org');
+                    var stanza = Strophe.xmlHtmlNode(
+                        `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452" type="groupchat" from="trek-radio@conference.lightwitch.org/comndrdukath#0805 (STO)">
+                            <body>negan</body>
+                            <stanza-id xmlns="urn:xmpp:sid:0" id="45fbbf2a-1059-479d-9283-c8effaf05621" by="trek-radio@conference.lightwitch.org"/>
+                         </message>`).firstElementChild;
+                    _converse.connection._dataRecv(test_utils.createRequest(stanza));
+
+                    stanza = Strophe.xmlHtmlNode(
+                        `<message xmlns="jabber:client" to="jcbrand@lightwitch.org/converse.js-73057452">
+                            <result xmlns="urn:xmpp:mam:2" queryid="82d9db27-6cf8-4787-8c2c-5a560263d823" id="45fbbf2a-1059-479d-9283-c8effaf05621">
+                                <forwarded xmlns="urn:xmpp:forward:0"><delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:17:23Z"/>
+                                    <message from="trek-radio@conference.lightwitch.org/comndrdukath#0805 (STO)" type="groupchat">
+                                        <body>negan</body>
+                                    </message>
+                                </forwarded>
+                            </result>
+                        </message>`).firstElementChild;
+                    chatroomview.onChatRoomMessage(stanza);
+                    expect(chatroomview.content.querySelectorAll('.chat-message').length).toBe(1);
+                    done();
+                });
+            }))
+        });
+
         describe("The archive.query API", function () {
 
            it("can be used to query for all archived messages",

+ 26 - 3
src/converse-mam.js

@@ -25,6 +25,18 @@
     const MAM_ATTRIBUTES = ['with', 'start', 'end'];
 
 
+    function getMessageArchiveID (stanza) {
+        const result = sizzle(`result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop();
+        if (!_.isUndefined(result)) {
+            return result.getAttribute('id');
+        }
+        const stanza_id = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, stanza).pop();
+        if (!_.isUndefined(stanza_id)) {
+            return stanza_id.getAttribute('id');
+        }
+    }
+
+
     converse.plugins.add('converse-mam', {
 
         optional_dependencies: ['converse-chatview', 'converse-muc'],
@@ -38,9 +50,9 @@
             ChatBox: {
                 getMessageAttributes (message, delay, original_stanza) {
                     const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
-                    const result = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, original_stanza).pop();
-                    if (!_.isUndefined(result)) {
-                        attrs.archive_id =  result.getAttribute('id');
+                    const archive_id = getMessageArchiveID(original_stanza);
+                    if (archive_id) {
+                        attrs.archive_id = archive_id;
                     }
                     return attrs;
                 }
@@ -191,6 +203,17 @@
                     this.model.on('change:connection_status', this.fetchArchivedMessagesIfNecessary, this);
                 },
 
+                isDuplicate (message, original_stanza) {
+                    const result = this.__super__.isDuplicate.apply(this, arguments);
+                    if (result) {
+                        return result;
+                    }
+                    const archive_id = getMessageArchiveID(original_stanza);
+                    if (archive_id) {
+                        return this.model.messages.filter({'archive_id': archive_id}).length > 0;
+                    }
+                },
+
                 renderChatArea () {
                     const result = this.__super__.renderChatArea.apply(this, arguments);
                     if (!this.disable_mam) {

+ 2 - 2
src/converse-muc.js

@@ -2127,7 +2127,7 @@
                     return false;
                 },
 
-                isDuplicate (message) {
+                isDuplicate (message, original_stanza) {
                     const msgid = message.getAttribute('id'),
                           jid = message.getAttribute('from'),
                           resource = Strophe.getResourceFromJid(jid),
@@ -2162,7 +2162,7 @@
                         sender = resource && Strophe.unescapeNode(resource) || '',
                         subject = _.propertyOf(message.querySelector('subject'))('textContent');
 
-                    if (this.isDuplicate(message)) {
+                    if (this.isDuplicate(message, original_stanza)) {
                         return true;
                     }
                     if (subject) {