After reconnection, fetch new messages for private chats

This commit is contained in:
JC Brand 2017-12-02 14:12:17 +01:00
parent c26ccf5e01
commit 4df61cc496
2 changed files with 53 additions and 0 deletions

View File

@ -11,6 +11,7 @@
(function (root, factory) {
define(["sizzle",
"converse-core",
"utils",
"converse-disco",
"converse-chatview", // Could be made a soft dependency
"converse-muc", // Could be made a soft dependency
@ -18,6 +19,7 @@
], factory);
}(this, function (sizzle, converse, utils) {
"use strict";
const CHATROOMS_TYPE = 'chatroom';
const { Promise, Strophe, $iq, _, moment } = converse.env;
const RSM_ATTRIBUTES = ['max', 'first', 'last', 'after', 'before', 'index', 'count'];
@ -53,6 +55,45 @@
return result;
},
fetchNewestMessages () {
/* Fetches messages that might have been archived *after*
* the last archived message in our local cache.
*/
if (this.disable_mam) { return; }
const { _converse } = this.__super__;
this.addSpinner();
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
(result) => { // Success
if (result.supported) {
const most_recent_msg = utils.getMostRecentMessage(this.model);
const archive_id = most_recent_msg.get('archive_id');
if (archive_id) {
this.fetchArchivedMessages({
'after': most_recent_msg.get('archive_id')
});
} else {
this.fetchArchivedMessages({
'start': most_recent_msg.get('time')
});
}
} else {
this.clearSpinner();
}
},
() => { // Error
this.clearSpinner();
_converse.log(
"Error or timeout while checking for MAM support",
Strophe.LogLevel.ERROR
);
}
).catch((msg) => {
this.clearSpinner();
_converse.log(msg, Strophe.LogLevel.FATAL);
});
},
fetchArchivedMessagesIfNecessary () {
/* Check if archived messages should be fetched, and if so, do so. */
if (this.disable_mam || this.model.get('mam_initialized')) {
@ -385,6 +426,13 @@
chatboxview.fetchArchivedMessagesIfNecessary();
});
_converse.on('reconnected', () => {
const private_chats = _converse.chatboxviews.filter(
(view) => _.at(view, 'model.attributes.type')[0] === 'chatbox'
);
_.each(private_chats, (view) => view.fetchNewestMessages())
});
_.extend(_converse.api, {
/* Extend default converse.js API to add methods specific to MAM
*/

View File

@ -275,6 +275,11 @@
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.