Commit 796582d9 authored by JC Brand's avatar JC Brand

Refactoring: got rid of the maybeFetchArchivedMessages method.

updates #306
parent 3b2b4ed6
...@@ -1313,7 +1313,7 @@ ...@@ -1313,7 +1313,7 @@
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
return; return;
} }
if (typeof this.model.get('archived_count') == 'undefined') { if (this.model.messages.length < converse.archived_messages_batch_size) {
// Get the amount of archived messages // Get the amount of archived messages
// Refer to: https://xmpp.org/extensions/xep-0059.html#count // Refer to: https://xmpp.org/extensions/xep-0059.html#count
API.archive.query({ API.archive.query({
...@@ -1321,25 +1321,18 @@ ...@@ -1321,25 +1321,18 @@
'max': 0 'max': 0
}, },
function (messages, attrs) { // On Success function (messages, attrs) { // On Success
// Whenever the archived_count attribute changes, if (this.model.messages.length < Number(attrs.count)) {
// fetchArchivedMessages will be called. this.fetchArchivedMessages({
this.model.save({'archived_count': Number(attrs.count)}); 'before': '', // Page backwards from the most recent message
this.maybeFetchArchivedMessages(); 'with': this.model.get('jid'),
'max': converse.archived_messages_batch_size
});
}
}.bind(this), }.bind(this),
function (iq) { // On Error function (iq) { // On Error
converse.log("Error occured while trying to fetch the archived messages count", "error"); converse.log("Error occured while trying to fetch the archived messages count", "error");
this.model.save({'archived_count': 0});
}.bind(this) }.bind(this)
); );
} else {
this.maybeFetchArchivedMessages();
}
},
maybeFetchArchivedMessages: function () {
if (this.model.messages.length < this.model.get('archived_count') &&
this.model.messages.length < converse.archived_messages_batch_size) {
this.fetchArchivedMessages();
} }
}, },
...@@ -1349,12 +1342,7 @@ ...@@ -1349,12 +1342,7 @@
* Then, upon receiving them, call onMessage on the chat box, * Then, upon receiving them, call onMessage on the chat box,
* so that they are displayed inside it. * so that they are displayed inside it.
*/ */
API.archive.query( API.archive.query(options,
options || {
'before': '', // Page backwards from the most recent message
'with': this.model.get('jid'),
'max': converse.archived_messages_batch_size
},
_.partial(_.map, _, converse.chatboxes.onMessage.bind(converse.chatboxes)), _.partial(_.map, _, converse.chatboxes.onMessage.bind(converse.chatboxes)),
_.partial(converse.log, "Error while trying to fetch archived messages", "error") _.partial(converse.log, "Error while trying to fetch archived messages", "error")
); );
......
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