From 546581d072c3e8fd376b2d322782ff3540e8b2ba Mon Sep 17 00:00:00 2001
From: ubermanu <e.vodor@gmail.com>
Date: Mon, 15 Mar 2021 16:20:16 +0100
Subject: [PATCH] bugfix: rename plugin and add force history loading if
 content is not scrollable

---
 ...mam-history.js => sib-history-improved.js} | 37 +++++++++++++++++--
 src/solid-xmpp-chat.js                        |  4 +-
 2 files changed, 36 insertions(+), 5 deletions(-)
 rename src/plugins/{sib-mam-history.js => sib-history-improved.js} (57%)

diff --git a/src/plugins/sib-mam-history.js b/src/plugins/sib-history-improved.js
similarity index 57%
rename from src/plugins/sib-mam-history.js
rename to src/plugins/sib-history-improved.js
index f0704e7..34eed4f 100644
--- a/src/plugins/sib-mam-history.js
+++ b/src/plugins/sib-history-improved.js
@@ -1,15 +1,46 @@
 /**
- * Forces the loading of the real number of messages (or more)
- * from the setting `archived_messages_page_size`.
+ * Updates the history behavior of chat boxes.
  */
-converse.plugins.add('sib-mam-history', {
+converse.plugins.add('sib-history-improved', {
   dependencies: [
     'converse-mam',
   ],
+  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 { api } = this.__super__._converse;
+        const { utils } = converse.env;
+        const scrollable = this.msgs_container.clientHeight < this.msgs_container.scrollHeight;
+
+        if (ev.deltaY < 0 && !scrollable) {
+          api.trigger('chatBoxScrolledUp', this);
+          utils.safeSave(this.model, {
+            scrolled: true,
+            scrollTop: 0,
+          });
+        }
+      },
+    },
+  },
   initialize() {
     const { api } = this._converse;
     let counter = 0;
 
+    // Forces the loading of the real number of messages (or more)
+    // from the setting `archived_messages_page_size`.
     api.listen.on('MAMResult', async data => {
 
       const max = +api.settings.get('archived_messages_page_size');
diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js
index 1a27333..b738cb0 100644
--- a/src/solid-xmpp-chat.js
+++ b/src/solid-xmpp-chat.js
@@ -9,7 +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-history-improved.js';
 import './plugins/sib-mention-mobile.js';
 import './plugins/sib-remove-notifications.js';
 import './plugins/sib-scroll-down-on-focus.js';
@@ -225,7 +225,7 @@ export const SolidXMPPChat = {
           'sib-connected',
           'sib-custom-hats',
           'sib-disconnected',
-          'sib-mam-history',
+          'sib-history-improved',
           'sib-mention-mobile',
           'sib-remove-notifications',
           'sib-scroll-down-on-focus',
-- 
GitLab