Commit 8db1f3e3 authored by JC Brand's avatar JC Brand

MAM: refactor to functions instead of extending chats with methods

parent 4349b016
...@@ -17,40 +17,34 @@ const { NS } = Strophe; ...@@ -17,40 +17,34 @@ const { NS } = Strophe;
const u = converse.env.utils; const u = converse.env.utils;
/** /**
* The MUC utils object. Contains utility functions related to multi-user chat.
* @mixin MAMEnabledChat
*/
const MAMEnabledChat = {
/**
* Fetches messages that might have been archived *after* * Fetches messages that might have been archived *after*
* the last archived message in our local cache. * the last archived message in our local cache.
* @private
*/ */
fetchNewestMessages () { function fetchNewestMessages (model) {
if (this.disable_mam) { if (model.disable_mam) {
return; return;
} }
const most_recent_msg = this.getMostRecentMessage(); const most_recent_msg = model.getMostRecentMessage();
// if clear_messages_on_reconnection is true, than any recent messages // if clear_messages_on_reconnection is true, than any recent messages
// must have been received *after* connection and we instead must query // must have been received *after* connection and we instead must query
// for earlier messages // for earlier messages
if (most_recent_msg && !api.settings.get('clear_messages_on_reconnection')) { if (most_recent_msg && !api.settings.get('clear_messages_on_reconnection')) {
const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`); const stanza_id = most_recent_msg.get(`stanza_id ${model.get('jid')}`);
if (stanza_id) { if (stanza_id) {
this.fetchArchivedMessages({'after': stanza_id}, 'forwards'); fetchArchivedMessages(model, {'after': stanza_id}, 'forwards');
} else { } else {
this.fetchArchivedMessages({'start': most_recent_msg.get('time')}, 'forwards'); fetchArchivedMessages(model, {'start': most_recent_msg.get('time')}, 'forwards');
} }
} else { } else {
this.fetchArchivedMessages({'before': ''}); fetchArchivedMessages(model, {'before': ''});
} }
}, }
async handleMAMResult (result, query, options, page_direction) { async function handleMAMResult (model, result, query, options, page_direction) {
await api.emojis.initialize(); await api.emojis.initialize();
const is_muc = this.get('type') === _converse.CHATROOMS_TYPE; const is_muc = model.get('type') === _converse.CHATROOMS_TYPE;
result.messages = result.messages.map( result.messages = result.messages.map(
s => (is_muc ? parseMUCMessage(s, this, _converse) : parseMessage(s, _converse)) s => (is_muc ? parseMUCMessage(s, model, _converse) : parseMessage(s, _converse))
); );
/** /**
...@@ -58,20 +52,19 @@ const MAMEnabledChat = { ...@@ -58,20 +52,19 @@ const MAMEnabledChat = {
* work based on the MAM result before calling the handlers here. * work based on the MAM result before calling the handlers here.
* @event _converse#MAMResult * @event _converse#MAMResult
*/ */
const data = { query, 'chatbox': this, 'messages': result.messages }; const data = { query, 'chatbox': model, 'messages': result.messages };
await api.trigger('MAMResult', data, {'synchronous': true}); await api.trigger('MAMResult', data, {'synchronous': true});
result.messages.forEach(m => this.queueMessage(m)); result.messages.forEach(m => model.queueMessage(m));
if (result.error) { if (result.error) {
const event_id = result.error.retry_event_id = u.getUniqueId(); const event_id = result.error.retry_event_id = u.getUniqueId();
api.listen.once(event_id, () => this.fetchArchivedMessages(options, page_direction)); api.listen.once(event_id, () => fetchArchivedMessages(model, options, page_direction));
this.createMessageFromError(result.error); model.createMessageFromError(result.error);
} }
}, }
/** /**
* Fetch XEP-0313 archived messages based on the passed in criteria. * Fetch XEP-0313 archived messages based on the passed in criteria.
* @private
* @param { Object } options * @param { Object } options
* @param { integer } [options.max] - The maximum number of items to return. * @param { integer } [options.max] - The maximum number of items to return.
* Defaults to "archived_messages_page_size" * Defaults to "archived_messages_page_size"
...@@ -89,12 +82,12 @@ const MAMEnabledChat = { ...@@ -89,12 +82,12 @@ const MAMEnabledChat = {
* @param { ('forwards'|'backwards')} [page_direction] - Determines whether this function should * @param { ('forwards'|'backwards')} [page_direction] - Determines whether this function should
* recursively page through the entire result set if a limited number of results were returned. * recursively page through the entire result set if a limited number of results were returned.
*/ */
async fetchArchivedMessages (options={}, page_direction) { async function fetchArchivedMessages (model, options={}, page_direction) {
if (this.disable_mam) { if (model.disable_mam) {
return; return;
} }
const is_muc = this.get('type') === _converse.CHATROOMS_TYPE; const is_muc = model.get('type') === _converse.CHATROOMS_TYPE;
const mam_jid = is_muc ? this.get('jid') : _converse.bare_jid; const mam_jid = is_muc ? model.get('jid') : _converse.bare_jid;
if (!(await api.disco.supports(NS.MAM, mam_jid))) { if (!(await api.disco.supports(NS.MAM, mam_jid))) {
return; return;
} }
...@@ -102,11 +95,11 @@ const MAMEnabledChat = { ...@@ -102,11 +95,11 @@ const MAMEnabledChat = {
const query = Object.assign({ const query = Object.assign({
'groupchat': is_muc, 'groupchat': is_muc,
'max': max, 'max': max,
'with': this.get('jid'), 'with': model.get('jid'),
}, options); }, options);
const result = await api.archive.query(query); const result = await api.archive.query(query);
await this.handleMAMResult(result, query, options, page_direction); await handleMAMResult(model, result, query, options, page_direction);
if (page_direction && result.rsm && !result.complete) { if (page_direction && result.rsm && !result.complete) {
if (page_direction === 'forwards') { if (page_direction === 'forwards') {
...@@ -114,13 +107,12 @@ const MAMEnabledChat = { ...@@ -114,13 +107,12 @@ const MAMEnabledChat = {
} else if (page_direction === 'backwards') { } else if (page_direction === 'backwards') {
options = result.rsm.previous(max, options.after).query; options = result.rsm.previous(max, options.after).query;
} }
return this.fetchArchivedMessages(options, page_direction); return fetchArchivedMessages(model, options, page_direction);
} else { } else {
// TODO: Add a special kind of message which will // TODO: Add a special kind of message which will
// render as a link to fetch further messages, either // render as a link to fetch further messages, either
// to fetch older messages or to fill in a gap. // to fetch older messages or to fill in a gap.
} }
}
} }
...@@ -139,9 +131,6 @@ converse.plugins.add('converse-mam', { ...@@ -139,9 +131,6 @@ converse.plugins.add('converse-mam', {
message_archiving_timeout: 20000, // Time (in milliseconds) to wait before aborting MAM request message_archiving_timeout: 20000, // Time (in milliseconds) to wait before aborting MAM request
}); });
Object.assign(_converse.ChatBox.prototype, MAMEnabledChat);
_converse.onMAMError = function (iq) { _converse.onMAMError = function (iq) {
if (iq?.querySelectorAll('feature-not-implemented').length) { if (iq?.querySelectorAll('feature-not-implemented').length) {
log.warn(`Message Archive Management (XEP-0313) not supported by ${iq.getAttribute('from')}`); log.warn(`Message Archive Management (XEP-0313) not supported by ${iq.getAttribute('from')}`);
...@@ -195,14 +184,14 @@ converse.plugins.add('converse-mam', { ...@@ -195,14 +184,14 @@ converse.plugins.add('converse-mam', {
} }
} }
function preMUCJoinMAMFetch (room) { function preMUCJoinMAMFetch (muc) {
if (!api.settings.get('muc_show_logs_before_join') || if (!api.settings.get('muc_show_logs_before_join') ||
!room.features.get('mam_enabled') || !muc.features.get('mam_enabled') ||
room.get('prejoin_mam_fetched')) { muc.get('prejoin_mam_fetched')) {
return; return;
} }
room.fetchNewestMessages(); fetchNewestMessages(muc);
room.save({'prejoin_mam_fetched': true}); muc.save({'prejoin_mam_fetched': true});
} }
/************************ BEGIN Event Handlers ************************/ /************************ BEGIN Event Handlers ************************/
...@@ -216,13 +205,13 @@ converse.plugins.add('converse-mam', { ...@@ -216,13 +205,13 @@ converse.plugins.add('converse-mam', {
view.model.features.on('change:mam_enabled', () => preMUCJoinMAMFetch(view.model)); view.model.features.on('change:mam_enabled', () => preMUCJoinMAMFetch(view.model));
} }
}); });
api.listen.on('enteredNewRoom', room => room.features.get('mam_enabled') && room.fetchNewestMessages()); api.listen.on('enteredNewRoom', muc => muc.features.get('mam_enabled') && fetchNewestMessages(muc));
api.listen.on('chatReconnected', chat => { api.listen.on('chatReconnected', chat => {
// XXX: For MUCs, we listen to enteredNewRoom instead // XXX: For MUCs, we listen to enteredNewRoom instead
if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE) { if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE) {
chat.fetchNewestMessages(); fetchNewestMessages(chat);
} }
}); });
...@@ -233,7 +222,7 @@ converse.plugins.add('converse-mam', { ...@@ -233,7 +222,7 @@ converse.plugins.add('converse-mam', {
// sent during the reload). // sent during the reload).
// With MUCs we can listen for `enteredNewRoom`. // With MUCs we can listen for `enteredNewRoom`.
if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE && !_converse.connection.restored) { if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE && !_converse.connection.restored) {
chat.fetchNewestMessages(); fetchNewestMessages(chat);
} }
}); });
/************************ END Event Handlers **************************/ /************************ END Event Handlers **************************/
......
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