Commit 31e884f7 authored by JC Brand's avatar JC Brand

Add code to check for duplicates

parent 64149810
...@@ -13,6 +13,39 @@ ...@@ -13,6 +13,39 @@
describe("Message Archive Management", function () { describe("Message Archive Management", function () {
// Implement the protocol defined in https://xmpp.org/extensions/xep-0313.html#config // 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 () { describe("The archive.query API", function () {
it("can be used to query for all archived messages", it("can be used to query for all archived messages",
......
...@@ -25,6 +25,18 @@ ...@@ -25,6 +25,18 @@
const MAM_ATTRIBUTES = ['with', 'start', 'end']; 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', { converse.plugins.add('converse-mam', {
optional_dependencies: ['converse-chatview', 'converse-muc'], optional_dependencies: ['converse-chatview', 'converse-muc'],
...@@ -38,9 +50,9 @@ ...@@ -38,9 +50,9 @@
ChatBox: { ChatBox: {
getMessageAttributes (message, delay, original_stanza) { getMessageAttributes (message, delay, original_stanza) {
const attrs = this.__super__.getMessageAttributes.apply(this, arguments); const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
const result = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, original_stanza).pop(); const archive_id = getMessageArchiveID(original_stanza);
if (!_.isUndefined(result)) { if (archive_id) {
attrs.archive_id = result.getAttribute('id'); attrs.archive_id = archive_id;
} }
return attrs; return attrs;
} }
...@@ -191,6 +203,17 @@ ...@@ -191,6 +203,17 @@
this.model.on('change:connection_status', this.fetchArchivedMessagesIfNecessary, this); 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 () { renderChatArea () {
const result = this.__super__.renderChatArea.apply(this, arguments); const result = this.__super__.renderChatArea.apply(this, arguments);
if (!this.disable_mam) { if (!this.disable_mam) {
......
...@@ -2127,7 +2127,7 @@ ...@@ -2127,7 +2127,7 @@
return false; return false;
}, },
isDuplicate (message) { isDuplicate (message, original_stanza) {
const msgid = message.getAttribute('id'), const msgid = message.getAttribute('id'),
jid = message.getAttribute('from'), jid = message.getAttribute('from'),
resource = Strophe.getResourceFromJid(jid), resource = Strophe.getResourceFromJid(jid),
...@@ -2162,7 +2162,7 @@ ...@@ -2162,7 +2162,7 @@
sender = resource && Strophe.unescapeNode(resource) || '', sender = resource && Strophe.unescapeNode(resource) || '',
subject = _.propertyOf(message.querySelector('subject'))('textContent'); subject = _.propertyOf(message.querySelector('subject'))('textContent');
if (this.isDuplicate(message)) { if (this.isDuplicate(message, original_stanza)) {
return true; return true;
} }
if (subject) { if (subject) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment