diff --git a/src/headless/converse-chatboxes.js b/src/headless/converse-chatboxes.js index 51e3d3352..828938677 100644 --- a/src/headless/converse-chatboxes.js +++ b/src/headless/converse-chatboxes.js @@ -395,6 +395,15 @@ converse.plugins.add('converse-chatboxes', { } }, + getMostRecentMessage () { + for (var i=this.messages.length-1; i>=0; i--) { + const message = this.messages.at(i); + if (message.get('type') === this.get('message_type')) { + return message; + } + } + }, + getUpdatedMessageAttributes (message, stanza) { // Overridden in converse-muc and converse-mam return {}; diff --git a/src/headless/converse-mam.js b/src/headless/converse-mam.js index 54beb0738..f8d9f923e 100644 --- a/src/headless/converse-mam.js +++ b/src/headless/converse-mam.js @@ -73,17 +73,16 @@ converse.plugins.add('converse-mam', { if (this.disable_mam) { return; } - const most_recent_msg = u.getMostRecentMessage(this); - - if (!most_recent_msg) { - this.fetchArchivedMessages({'before': ''}); - } else { + const most_recent_msg = this.getMostRecentMessage(); + if (most_recent_msg) { const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`); if (stanza_id) { this.fetchArchivedMessages({'after': stanza_id}, 'forwards'); } else { this.fetchArchivedMessages({'start': most_recent_msg.get('time')}, 'forwards'); } + } else { + this.fetchArchivedMessages({'before': ''}); } }, diff --git a/src/headless/utils/core.js b/src/headless/utils/core.js index d1aebeb9a..4c7bfc3b7 100644 --- a/src/headless/utils/core.js +++ b/src/headless/utils/core.js @@ -98,11 +98,6 @@ u.isSameBareJID = function (jid1, jid2) { Strophe.getBareJidFromJid(jid2).toLowerCase(); }; -u.getMostRecentMessage = function (model) { - const messages = model.messages.filter('message'); - return messages[messages.length-1]; -} - u.isNewMessage = function (message) { /* Given a stanza, determine whether it's a new * message, i.e. not a MAM archived one.