diff --git a/src/plugins/sib-mam-history.js b/src/plugins/sib-mam-history.js new file mode 100644 index 0000000000000000000000000000000000000000..bffcc149dfbab10eef3c601aca1026bf6db2d490 --- /dev/null +++ b/src/plugins/sib-mam-history.js @@ -0,0 +1,48 @@ +/** + * Forces the loading of the real number of messages (or more) + * from the setting `archived_messages_page_size`. + */ +converse.plugins.add('sib-mam-history', { + dependencies: [ + 'converse-mam', + ], + initialize() { + const { api } = this._converse; + let counter = 0; + + api.listen.on('MAMResult', async data => { + + const max = +api.settings.get('archived_messages_page_size'); + const messages = await Promise.all(data.messages); + + // Increase counter with the messages that contains a body + counter += messages.filter(attrs => attrs.body).length; + + // Stop if the max value is not specified + // Stop if there are less messages available than the page size + // Stop if the counter registers more messages than the max value + if (!max || messages.length < max || counter >= max) { + counter = 0; + return; + } + + const is_groupchat = data.chatbox.get('type') === converse.CHATROOMS_TYPE; + const oldest_message = messages.pop(); + + if (oldest_message) { + const by_jid = is_groupchat ? data.chatbox.get('jid') : converse.bare_jid; + const stanza_id = oldest_message && oldest_message['stanza_id '.concat(by_jid)]; + + if (stanza_id) { + await data.chatbox.fetchArchivedMessages({ + 'before': stanza_id + }); + } else { + await data.chatbox.fetchArchivedMessages({ + 'end': oldest_message['time'] + }); + } + } + }) + } +}); diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js index 5ea8acd9e6d7991663355708e22493b9409e2589..f180e501489c72da7b0633eeff7e43bec5f2030d 100644 --- a/src/solid-xmpp-chat.js +++ b/src/solid-xmpp-chat.js @@ -9,6 +9,7 @@ import './plugins/converse-rai.js'; import './plugins/sib-chat-navigation.js'; import './plugins/sib-custom-hats.js'; import './plugins/sib-disconnected.js'; +import './plugins/sib-mam-history.js'; import './plugins/sib-remove-notifications.js'; import './plugins/sib-subscribe-to-rai.js'; @@ -222,6 +223,7 @@ export const SolidXMPPChat = { 'sib-connected', 'sib-custom-hats', 'sib-disconnected', + 'sib-mam-history', 'sib-remove-notifications', 'sib-subscribe-to-rai', ],