converse-mam: Don't fetch MAM messages on each page load.

This commit is contained in:
JC Brand 2016-12-04 14:44:43 +00:00
parent 3b3720c32d
commit 2a81d2e6f3
3 changed files with 11 additions and 6 deletions

View File

@ -3,6 +3,7 @@
## 2.0.4 (Unreleased) ## 2.0.4 (Unreleased)
- Bugfix. Switching from bookmarks form to config form shows only the spinner. [jcbrand] - Bugfix. Switching from bookmarks form to config form shows only the spinner. [jcbrand]
- Bugfix. Other room occupants sometimes not shown when reloading the page. [jcbrand] - Bugfix. Other room occupants sometimes not shown when reloading the page. [jcbrand]
- Optimize fetching of MAM messages (in some cases happened on each page load). [jcbrand]
## 2.0.3 (2016-11-30) ## 2.0.3 (2016-11-30)
- #735 Room configuration button not visible. [jcbrand] - #735 Room configuration button not visible. [jcbrand]

View File

@ -69,12 +69,15 @@
if (this.disable_mam || !converse.features.findWhere({'var': Strophe.NS.MAM})) { if (this.disable_mam || !converse.features.findWhere({'var': Strophe.NS.MAM})) {
return this.__super__.afterMessagesFetched.apply(this, arguments); return this.__super__.afterMessagesFetched.apply(this, arguments);
} }
if (this.model.messages.length < converse.archived_messages_page_size) { if (!this.model.get('mam_initialized') &&
this.model.messages.length < converse.archived_messages_page_size) {
this.fetchArchivedMessages({ this.fetchArchivedMessages({
'before': '', // Page backwards from the most recent message 'before': '', // Page backwards from the most recent message
'with': this.model.get('jid'), 'with': this.model.get('jid'),
'max': converse.archived_messages_page_size 'max': converse.archived_messages_page_size
}); });
this.model.save({'mam_initialized': true});
} }
return this.__super__.afterMessagesFetched.apply(this, arguments); return this.__super__.afterMessagesFetched.apply(this, arguments);
}, },

View File

@ -1583,6 +1583,7 @@
* Then, upon receiving them, call onChatRoomMessage * Then, upon receiving them, call onChatRoomMessage
* so that they are displayed inside it. * so that they are displayed inside it.
*/ */
var that = this;
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.log("Attempted to fetch archived messages but this user's server doesn't support XEP-0313"); converse.log("Attempted to fetch archived messages but this user's server doesn't support XEP-0313");
return; return;
@ -1590,15 +1591,15 @@
this.addSpinner(); this.addSpinner();
converse_api.archive.query(_.extend(options, {'groupchat': true}), converse_api.archive.query(_.extend(options, {'groupchat': true}),
function (messages) { function (messages) {
this.clearSpinner(); that.clearSpinner();
if (messages.length) { if (messages.length) {
_.map(messages, this.onChatRoomMessage.bind(this)); _.map(messages, that.onChatRoomMessage.bind(that));
} }
}.bind(this), },
function () { function () {
this.clearSpinner(); that.clearSpinner();
converse.log("Error while trying to fetch archived messages", "error"); converse.log("Error while trying to fetch archived messages", "error");
}.bind(this) }
); );
} }
}); });