From f61cd9914068512293eabad2075b9defa316bd4b Mon Sep 17 00:00:00 2001
From: ubermanu <e.vodor@gmail.com>
Date: Wed, 10 Mar 2021 15:01:11 +0100
Subject: [PATCH] move sib rai plugin into its own file

---
 src/plugins/converse-rai.js         |   2 +-
 src/plugins/sib-subscribe-to-rai.js |  68 ++++++++++++++++
 src/solid-xmpp-chat.js              | 116 ++--------------------------
 3 files changed, 76 insertions(+), 110 deletions(-)
 create mode 100644 src/plugins/sib-subscribe-to-rai.js

diff --git a/src/plugins/converse-rai.js b/src/plugins/converse-rai.js
index d52f067..24ed1ad 100644
--- a/src/plugins/converse-rai.js
+++ b/src/plugins/converse-rai.js
@@ -3,7 +3,7 @@
     let interestingServers = new Set();
     let subscribedServers = new Set();
 
-    converse.plugins.add('rai', {
+    converse.plugins.add('converse-rai', {
         'dependencies': [],
 
         'initialize': function() {
diff --git a/src/plugins/sib-subscribe-to-rai.js b/src/plugins/sib-subscribe-to-rai.js
new file mode 100644
index 0000000..fee52f6
--- /dev/null
+++ b/src/plugins/sib-subscribe-to-rai.js
@@ -0,0 +1,68 @@
+/**
+ * Initialize rai plugin.
+ */
+converse.plugins.add('sib-subscribe-to-rai', {
+    dependencies: [
+        'converse-rai',
+    ],
+    async initialize() {
+        const _converse = this._converse;
+        const { api } = _converse;
+
+        const getCircles = new Promise((resolve, reject) => {
+            const circleInterval = setInterval(async () => {
+                let retry = false;
+                let circles = [];
+                const user = await document.querySelector('sib-auth').getUser();
+                const userProfile = await store.getData(user['@id'], this.context);
+                for (let circleMembership of await userProfile['circles.ldp:contains']) {
+                    if (circleMembership) {
+                        circles.push(await circleMembership['circle.jabberID']);
+                    } else {
+                        retry = true;
+                    }
+                }
+                if (!retry) {
+                    clearInterval(circleInterval);
+                    resolve(circles);
+                }
+            }, 250);
+        });
+
+        const getProjects = new Promise((resolve, reject) => {
+            const projectInterval = setInterval(async () => {
+                let retry = false;
+                let projects = [];
+                const user = await document.querySelector('sib-auth').getUser();
+                const userProfile = await store.getData(user['@id'], this.context);
+                for (let projectMembership of await userProfile['projects.ldp:contains']) {
+                    if (projectMembership) {
+                        projects.push(await projectMembership['project.jabberID']);
+                    } else {
+                        retry = true;
+                    }
+                }
+                if (!retry) {
+                    clearInterval(projectInterval);
+                    resolve(projects);
+                }
+            }, 250);
+        });
+
+        let userRooms = (await Promise.all([
+            getCircles,
+            getProjects,
+        ])).flat();
+
+        // @MattJ Here userRooms is an array of each jabberID the user is on.
+        api.trigger('raiRoomsUpdated', userRooms);
+
+        api.listen.on('chatRoomActivityIndicators', jid => {
+            window.dispatchEvent(new CustomEvent('newMessage', {
+                detail: {
+                    jid,
+                },
+            }));
+        });
+    },
+});
diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js
index accf5e8..897a368 100644
--- a/src/solid-xmpp-chat.js
+++ b/src/solid-xmpp-chat.js
@@ -8,6 +8,7 @@ import './conversejs/emojis.js';
 import './plugins/converse-rai.js';
 import './plugins/sib-custom-hats.js';
 import './plugins/sib-remove-notifications.js';
+import './plugins/sib-subscribe-to-rai.js';
 
 export const SolidXMPPChat = {
     name: 'solid-xmpp-chat',
@@ -151,34 +152,10 @@ 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;
                 }
+
                 jid = jid.toLowerCase();
                 this._converse.root = root;
 
@@ -190,30 +167,8 @@ 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);
                 } else {
@@ -221,6 +176,7 @@ export const SolidXMPPChat = {
                 }
             }
         });
+
         // Initialize deferred resolution plugin
         setTimeout(async () => {
             // Initialize change change plugin
@@ -250,64 +206,6 @@ export const SolidXMPPChat = {
                 },
             });
 
-            // Initialize rai plugin
-            converse.plugins.add('conversejs-rai', {
-                async initialize() {
-
-                    let userRooms = (await Promise.all([
-                        new Promise((resolve, reject) => {
-                            const circleInterval = setInterval(async () => {
-                                let retry = false;
-                                let circles = [];
-                                const user = await document.querySelector('sib-auth').getUser();
-                                const userProfile = await store.getData(user['@id'], this.context);
-                                for (let circleMembership of await userProfile['circles.ldp:contains']) {
-                                    if (circleMembership) {
-                                        circles.push(await circleMembership['circle.jabberID']);
-                                    } else {
-                                        retry = true;
-                                    }
-                                }
-                                if (!retry) {
-                                    clearInterval(circleInterval);
-                                    resolve(circles);
-                                }
-                            }, 250);
-                        }),
-                        new Promise((resolve, reject) => {
-                            const projectInterval = setInterval(async () => {
-                                let retry = false;
-                                let projects = [];
-                                const user = await document.querySelector('sib-auth').getUser();
-                                const userProfile = await store.getData(user['@id'], this.context);
-                                for (let projectMembership of await userProfile['projects.ldp:contains']) {
-                                    if (projectMembership) {
-                                        projects.push(await projectMembership['project.jabberID']);
-                                    } else {
-                                        retry = true;
-                                    }
-                                }
-                                if (!retry) {
-                                    clearInterval(projectInterval);
-                                    resolve(projects);
-                                }
-                            }, 250);
-                        }),
-                    ])).flat();
-
-                    // @MattJ Here userRooms is an array of each jabberID the user is on.
-                    this._converse.api.trigger('raiRoomsUpdated', userRooms);
-
-                    this._converse.api.listen.on('chatRoomActivityIndicators', function(jid) {
-                        window.dispatchEvent(new CustomEvent('newMessage', {
-                            detail: {
-                                jid: jid,
-                            },
-                        }));
-                    });
-                },
-            });
-
             // Initialize deferred resolution plugin
             converse.plugins.add('conversejs-sib-focused', {
                 initialize() {
@@ -391,13 +289,13 @@ export const SolidXMPPChat = {
                     toggle_occupants: false,
                 },
                 'whitelisted_plugins': [
-                    'rai',
+                    'converse-rai',
                     'conversejs-sib-disconnected',
                     'conversejs-sib-connected',
                     'conversejs-sib-focused',
                     'conversejs-changechat',
-                    'conversejs-rai',
-                    'custom-hats',
+                    'sib-subscribe-to-rai',
+                    'sib-custom-hats',
                     'sib-remove-notifications',
                 ],
             });
-- 
GitLab