Commit bbf33a8e authored by JC Brand's avatar JC Brand

Combine duplicate `queryForArchivedMessages` methods

parent dbee62c7
...@@ -197,44 +197,53 @@ ...@@ -197,44 +197,53 @@
}, },
fetchArchivedMessages (options) { fetchArchivedMessages (options) {
/* Fetch archived chat messages from the XMPP server.
*
* Then, upon receiving them, call onMessage on the chat
* box, so that they are displayed inside it.
*/
const { _converse } = this.__super__; const { _converse } = this.__super__;
if (!_converse.disco_entities.get(_converse.bare_jid) if (this.disable_mam) { return; }
.features.findWhere({'var': Strophe.NS.MAM})) {
_converse.log( const is_groupchat = this.model.get('type') === CHATROOMS_TYPE;
"Attempted to fetch archived messages but this "+
"user's server doesn't support XEP-0313", let mam_jid, message_handler;
Strophe.LogLevel.WARN); if (is_groupchat) {
return; mam_jid = this.model.get('jid');
} message_handler = this.model.onMessage.bind(this.model);
if (this.disable_mam) { } else {
return; mam_jid = _converse.bare_jid;
message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes)
} }
this.addSpinner();
_converse.api.archive.query( _converse.api.disco.supports(Strophe.NS.MAM, mam_jid).then(
_.extend({ (results) => { // Success
'before': '', // Page backwards from the most recent message if (!results.length) { return; }
'max': _converse.archived_messages_page_size, this.addSpinner();
'with': this.model.get('jid'), _converse.api.archive.query(
}, options), _.extend({
(messages) => { // Success 'groupchat': is_groupchat,
this.clearSpinner(); 'before': '', // Page backwards from the most recent message
if (messages.length) { 'max': _converse.archived_messages_page_size,
_.each(messages, _converse.chatboxes.onMessage.bind(_converse.chatboxes)); 'with': this.model.get('jid'),
} }, options),
(messages) => { // Success
this.clearSpinner();
_.each(messages, message_handler);
},
() => { // Error
this.clearSpinner();
_converse.log(
"Error or timeout while trying to fetch "+
"archived messages", Strophe.LogLevel.ERROR);
}
);
}, },
() => { // Error () => { // Error
this.clearSpinner();
_converse.log( _converse.log(
"Error or timeout while trying to fetch "+ "Error or timeout while checking for MAM support",
"archived messages", Strophe.LogLevel.ERROR); Strophe.LogLevel.ERROR
);
} }
); ).catch((msg) => {
this.clearSpinner();
_converse.log(msg, Strophe.LogLevel.FATAL);
});
}, },
onScroll (ev) { onScroll (ev) {
...@@ -295,52 +304,7 @@ ...@@ -295,52 +304,7 @@
} }
this.fetchArchivedMessages(); this.fetchArchivedMessages();
this.model.save({'mam_initialized': true}); this.model.save({'mam_initialized': true});
}, }
fetchArchivedMessages (options) {
/* Fetch archived chat messages for this Chat Room
*
* Then, upon receiving them, call onMessage
* so that they are displayed inside it.
*/
const { _converse } = this.__super__,
mam_jid = this.model.get('type') === CHATROOMS_TYPE ? this.model.get('jid') : _converse.bare_jid;
_converse.api.disco.supports(Strophe.NS.MAM, mam_jid).then(
(results) => { // Success
if (!results.length) { return; }
this.addSpinner();
_converse.api.archive.query(
_.extend({
'groupchat': true,
'before': '', // Page backwards from the most recent message
'with': this.model.get('jid'),
'max': _converse.archived_messages_page_size
}, options),
(messages) => {
this.clearSpinner();
if (messages.length) {
_.each(messages, this.model.onMessage.bind(this.model));
}
},
() => {
this.clearSpinner();
_converse.log(
"Error while trying to fetch archived messages",
Strophe.LogLevel.WARN);
}
);
},
() => { // Error
_converse.log(
"Error or timeout while checking for MAM support",
Strophe.LogLevel.ERROR
);
}
).catch((msg) => {
this.clearSpinner();
_converse.log(msg, Strophe.LogLevel.FATAL);
});
} }
}, },
......
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