From 0bad6d9b0654945f5b6707fb4bf51843129d025f Mon Sep 17 00:00:00 2001
From: Xavi Ferrer <xavi@delape.net>
Date: Fri, 30 Oct 2020 08:26:14 +0100
Subject: [PATCH] clear room messages when leaving a room if there are more
 than 30

---
 src/solid-xmpp-chat.js | 51 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js
index c74103d..d593486 100644
--- a/src/solid-xmpp-chat.js
+++ b/src/solid-xmpp-chat.js
@@ -116,6 +116,31 @@ export const SolidXMPPChat = {
     // Change chat plugin
     converse_sib.service.plugins.sibChat = new(class {
       changeChat(jid, is_groupchat, root) {
+        function isEmptyMessage (attrs) {
+          if (attrs && attrs.attributes) {
+              attrs = attrs.attributes;
+          }
+          return !attrs['oob_url'] &&
+              !attrs['file'] &&
+              !(attrs['is_encrypted'] && attrs['plaintext']) &&
+              !attrs['message'];
+        };
+
+        function removeUnnecessaryDayIndicators(view) {
+          const pred = (el) =>
+            el.matches('.date-separator') && el.nextElementSibling.matches('.date-separator');
+          const container = view.el.querySelector('.chat-content__messages');
+          const to_remove = Array.from(container.children).filter(pred);
+          to_remove.forEach((el) => el.parentElement.removeChild(el));
+        };
+        function isHidden(classList){
+          for(let i = 0; i < classList.length; i++){
+            if(classList[i] === 'hidden'){
+              return true;
+            }
+          }
+          return false;
+        }
         if (!jid) {
           return;
         }
@@ -130,6 +155,29 @@ export const SolidXMPPChat = {
 
         if (converse_el) {
           root.appendChild(converse_el);
+          if (is_groupchat) {
+            const jid_to_clear = converse_el.getElementsByClassName('converse-chatboxes');
+            let room_to_clear_view = '';
+            if (jid_to_clear.length && jid_to_clear[0].children.length){
+              for (let i = 0; i < jid_to_clear[0].children.length; i++) {
+                  if(!isHidden(jid_to_clear[0].children[i].classList)){
+                    room_to_clear_view = this._converse.chatboxviews.views[jid_to_clear[0].children[i].id.split('-')[1]];
+                    break;
+                  }
+              }
+            }
+            if (room_to_clear_view) {
+              if (room_to_clear_view.model.messages.length > 30) {
+                const non_empty_messages = room_to_clear_view.model.messages.filter((m) => !isEmptyMessage(m));
+                if (non_empty_messages.length > 30) {
+                while (non_empty_messages.length > 30) {
+                    non_empty_messages.shift().destroy();
+                }
+                removeUnnecessaryDayIndicators(room_to_clear_view);
+                }
+              }
+            }
+          }
         }
         if (is_groupchat) {
           this._converse.api.rooms.open(jid, {}, true);
@@ -138,6 +186,7 @@ export const SolidXMPPChat = {
         }
       }
     });
+    // Initialize deferred resolution plugin
     setTimeout(async () => {
       // Initialize change change plugin
       converse.plugins.add('conversejs-changechat', converse_sib.service.plugins.sibChat);
@@ -292,7 +341,7 @@ export const SolidXMPPChat = {
         'root': this.element.shadowRoot,
         'show_client_info': false,
         'show_desktop_notifications': false,
-        'persistent_store': 'IndexedDB',
+        'persistent_store': 'localStorage',
         'sounds_path': ComponentPath + '/dist/conversejs/',
         'show_send_button': false,
         'view_mode': 'fullscreen',
-- 
GitLab