From baf25a6bc868dded4c1750146e3057ba7929aea9 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 12 Sep 2013 13:44:02 -0700 Subject: [PATCH] Fix MAM retrieval when there are messages with no ids --- clientapp/helpers/xmppEventHandlers.js | 7 +++++++ clientapp/libraries/stanza.io.js | 2 ++ clientapp/models/contact.js | 26 +++++++++++++++++++++----- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/clientapp/helpers/xmppEventHandlers.js b/clientapp/helpers/xmppEventHandlers.js index e99c9f0..fa13c4e 100644 --- a/clientapp/helpers/xmppEventHandlers.js +++ b/clientapp/helpers/xmppEventHandlers.js @@ -228,7 +228,14 @@ module.exports = function (client, app) { onclick: _.bind(app.navigate, app, '/chat/' + contact.jid) }); } + contact.messages.add(message); + + var newInteraction = new Date(message.created); + if (!contact.lastInteraction || contact.lastInteraction < newInteraction) { + contact.lastInteraction = newInteraction; + } + if (!contact.lockedResource) { contact.lockedResource = msg.from.full; } else if (msg.from !== contact.lockedResource) { diff --git a/clientapp/libraries/stanza.io.js b/clientapp/libraries/stanza.io.js index 2652469..0a40395 100644 --- a/clientapp/libraries/stanza.io.js +++ b/clientapp/libraries/stanza.io.js @@ -2966,12 +2966,14 @@ MAMQuery.prototype = { return new Date(stanza.getSubText(this.xml, this.NS, 'start') || Date.now()); }, set start(value) { + if (!value) return; stanza.setSubText(this.xml, this.NS, 'start', value.toISOString()); }, get end() { return new Date(stanza.getSubText(this.xml, this.NS, 'end') || Date.now()); }, set end(value) { + if (!value) return; stanza.setSubText(this.xml, this.NS, 'end', value.toISOString()); } }; diff --git a/clientapp/models/contact.js b/clientapp/models/contact.js index 1c84340..f6e64d0 100644 --- a/clientapp/models/contact.js +++ b/clientapp/models/contact.js @@ -87,7 +87,8 @@ module.exports = HumanModel.define({ lastSentMessage: 'object', timezoneOffset: ['number', false, 0], activeContact: ['bool', true, false], - unreadCount: ['number', true, 0] + unreadCount: ['number', true, 0], + lastInteraction: 'date' }, collections: { resources: Resources, @@ -167,12 +168,20 @@ module.exports = HumanModel.define({ fetchHistory: function () { var self = this; app.whenConnected(function () { + var filter = { + count: 20, + before: true, + }; + + var lastMessage = self.messages.last(); + if (lastMessage && lastMessage.archivedId) { + filter.after = lastMessage.archivedId; + } + client.getHistory({ with: self.jid, - rsm: { - count: 20, - before: true - } + start: self.lastInteraction, + rsm: filter }, function (err, res) { if (err) return; @@ -198,8 +207,15 @@ module.exports = HumanModel.define({ if (original && original.correct(msg)) return; } + var message = new Message(msg); message.archivedId = result.mam.id; + + var newInteraction = new Date(message.created); + if (!self.lastInteraction || newInteraction > self.lastInteraction) { + self.lastInteraction = newInteraction; + } + self.messages.add(message); }); });