From 02141c5ef15dc2c818db8c633b8f52058cbcefa8 Mon Sep 17 00:00:00 2001
From: ubermanu <e.vodor@gmail.com>
Date: Tue, 16 Mar 2021 16:51:16 +0100
Subject: [PATCH 1/2] cleanup code and subscribe to rai if a room is open
 hidden

---
 src/plugins/converse-rai.js | 64 +++++++++++++++++++++++++++++--------
 1 file changed, 51 insertions(+), 13 deletions(-)

diff --git a/src/plugins/converse-rai.js b/src/plugins/converse-rai.js
index fb72df1..702165f 100644
--- a/src/plugins/converse-rai.js
+++ b/src/plugins/converse-rai.js
@@ -22,18 +22,57 @@ converse.plugins.add('converse-rai', {
         const conn_status = this.session.get('connection_status');
         const { api } = this.__super__._converse;
 
-        if (this.get('hidden') && conn_status === converse.ROOMSTATUS.ENTERED) {
-          if (api.settings.get('muc_subscribe_to_rai') && this.getOwnAffiliation() !== 'none') {
-            if (conn_status !== converse.ROOMSTATUS.DISCONNECTED) {
-              this.sendMarkerForLastMessage('received', true);
-              await this.leave();
-              await this.close();
-            }
-          }
+        if (this.get('hidden')
+          && conn_status === converse.ROOMSTATUS.ENTERED
+          && api.settings.get('muc_subscribe_to_rai')
+          && this.getOwnAffiliation() !== 'none'
+        ) {
+          this.sendMarkerForLastMessage('received', true);
+          await this.leave();
+          await this.close();
         } else if (conn_status === converse.ROOMSTATUS.DISCONNECTED) {
           await this.rejoin();
         }
       },
+
+      /**
+       * Subscribe to RAI if connected to the room, but it's hidden.
+       * @private
+       * @method _converse.ChatRoom#onConnectionStatusChanged
+       */
+      async onConnectionStatusChanged() {
+        const conn_status = this.session.get('connection_status');
+        const { api } = this.__super__._converse;
+
+        if (this.get('hidden')
+          && conn_status === converse.ROOMSTATUS.ENTERED
+          && api.settings.get('muc_subscribe_to_rai')
+          && this.getOwnAffiliation() !== 'none'
+        ) {
+          await this.leave();
+          await this.close();
+          this.enableRAI();
+        } else {
+          await this.__super__.onConnectionStatusChanged.apply(this, arguments);
+        }
+      },
+
+      /**
+       * Ensures that the user is subscribed to XEP-0437 Room Activity Indicators
+       * if `muc_subscribe_to_rai` is set to `true`.
+       * Only affiliated users can subscribe to RAI, but this method doesn't
+       * check whether the current user is affiliated because it's intended to be
+       * called after the MUC has been left and we don't have that information
+       * anymore.
+       * @private
+       * @method _converse.ChatRoom#enableRAI
+       */
+      enableRAI() {
+        const { api } = this.__super__._converse;
+        if (api.settings.get('muc_subscribe_to_rai')) {
+          api.rooms.subscribe(this.get('jid'));
+        }
+      },
     },
     ChatBox: {
       /**
@@ -55,10 +94,8 @@ converse.plugins.add('converse-rai', {
     const _converse = this._converse;
     const { api } = _converse;
 
-    // Register namespace
     Strophe.addNamespace('RAI', 'urn:xmpp:rai:0');
 
-    // Register settings
     api.settings.extend({
       muc_subscribe_to_rai: false,
     });
@@ -71,9 +108,10 @@ converse.plugins.add('converse-rai', {
        * @param {string|Array} jids
        * @returns {void}
        */
-      subscribe(jids) {
+      async subscribe(jids) {
         if (!api.settings.get('muc_subscribe_to_rai')) {
-          console.error('Can\'t subscribe to RAI, this feature is not enabled');
+          log.error(`You must enable the 'muc_subscribe_to_rai' option before subscribing to a MUC.`);
+          return;
         }
 
         if (typeof jids === 'string') {
@@ -87,7 +125,7 @@ converse.plugins.add('converse-rai', {
             'xmlns': Strophe.NS.RAI,
           });
           api.send(rai);
-          console.log('Sent RAI stanza for muc_domain', muc_domain, rai.toString());
+          log.debug(`Sent RAI stanza for domain "${muc_domain}"`);
         });
       },
     });
-- 
GitLab


From e4ace9be82e37b92281ca92f6a82cb84cdff15fe Mon Sep 17 00:00:00 2001
From: ubermanu <e.vodor@gmail.com>
Date: Tue, 16 Mar 2021 17:03:20 +0100
Subject: [PATCH 2/2] remove async

---
 src/plugins/converse-rai.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/plugins/converse-rai.js b/src/plugins/converse-rai.js
index 702165f..b7a1b25 100644
--- a/src/plugins/converse-rai.js
+++ b/src/plugins/converse-rai.js
@@ -108,7 +108,7 @@ converse.plugins.add('converse-rai', {
        * @param {string|Array} jids
        * @returns {void}
        */
-      async subscribe(jids) {
+      subscribe(jids) {
         if (!api.settings.get('muc_subscribe_to_rai')) {
           log.error(`You must enable the 'muc_subscribe_to_rai' option before subscribing to a MUC.`);
           return;
-- 
GitLab