Commit bbf33a8e authored by JC Brand's avatar JC Brand

Combine duplicate `queryForArchivedMessages` methods

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