diff --git a/src/plugins/sib-history-scroll.js b/src/plugins/sib-history-scroll.js deleted file mode 100644 index f514a52dd5a28b58a2c1492d5f9fed950489cc0a..0000000000000000000000000000000000000000 --- a/src/plugins/sib-history-scroll.js +++ /dev/null @@ -1,68 +0,0 @@ -converse.plugins.add('sib-history-scroll', { - overrides: { - ChatRoomView: { - events: { - 'wheel .chat-content__messages': 'onScrolledUp', - }, - }, - ChatBoxView: { - events: { - 'wheel .chat-content__messages': 'onScrolledUp', - }, - - /** - * Fetch the old messages if there is no scroll available. - * @param {WheelEvent} ev - */ - onScrolledUp(ev) { - const scrollable = this.msgs_container.clientHeight < this.msgs_container.scrollHeight - if (ev.deltaY < 0 && !scrollable) { - const _converse = this.__super__._converse; - _converse.api.trigger('chatBoxScrolledUp', this); - converse.env.utils.safeSave(this.model, { - scrolled: true, - scrollTop: 0, - }); - } - }, - - /** - * Fix the scroll event when contained in the <solid-xmpp-chat> element. - * Replace the ev.target with the first element in composed path. - * - * @param {Event} ev - * @private - */ - _markScrolled: function (ev) { - const _converse = this.__super__._converse; - - let scrolled = true; - let scrollTop = null; - const is_at_bottom = this.msgs_container.scrollTop + this.msgs_container.clientHeight >= this.msgs_container.scrollHeight; - - // TODO: use composed path - const target = ev.target || ev.path[0]; - - if (is_at_bottom) { - scrolled = false; - this.onScrolledDown(); - } else if (this.msgs_container.scrollTop === 0) { - /** - * Triggered once the chat's message area has been scrolled to the top - * @event _converse#chatBoxScrolledUp - * @property { _converse.ChatBoxView | _converse.ChatRoomView } view - * @example _converse.api.listen.on('chatBoxScrolledUp', obj => { ... }); - */ - _converse.api.trigger('chatBoxScrolledUp', this); - } else { - scrollTop = target?.scrollTop; - } - - converse.env.utils.safeSave(this.model, { - scrolled, - scrollTop - }); - } - } - } -}); 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 b80778abd878384f918c97b2d45c04785762f91a..6be7190759de9134c45d421b9542e8b88ab7483a 100644 --- a/src/solid-xmpp-chat.js +++ b/src/solid-xmpp-chat.js @@ -1,7 +1,7 @@ import './conversejs/converse.min.js'; import './conversejs/emojis.js'; import './plugins/converse-rai.js'; -import './plugins/sib-history-scroll.js' +import './plugins/sib-mam-history.js' import { Sib, store, StoreMixin } from 'https://cdn.skypack.dev/@startinblox/core@0.15'; import ComponentPath from './path.js'; @@ -391,7 +391,7 @@ export const SolidXMPPChat = { 'allow_non_roster_messaging': true, 'allow_dragresize': false, 'allow_logout': false, - 'archived_messages_page_size': "30", + 'archived_messages_page_size': 30, 'auto_list_rooms': true, 'auto_login': this.element.dataset.autoLogin === 'true', 'auto_join_on_invite': true, @@ -429,7 +429,7 @@ export const SolidXMPPChat = { }, 'whitelisted_plugins': [ 'rai', - 'sib-history-scroll', + 'sib-mam-history', 'conversejs-sib-disconnected', 'conversejs-sib-connected', 'conversejs-sib-focused',