Преглед изворни кода

XEP-0437 MUC RAI fixes

Avoid unnecessarily connecting to rooms that should instead receive room
activity indicators (RAI).

- Don't join a newly created room that qualifies for RAI.
- Upon switching back to the tab, don't reconnect to a room that qualifies for RAI.
- Don't ping a room that we didn't actually enter.
JC Brand пре 2 година
родитељ
комит
bb7ed5315f
1 измењених фајлова са 35 додато и 16 уклоњено
  1. 35 16
      src/headless/plugins/muc/muc.js

+ 35 - 16
src/headless/plugins/muc/muc.js

@@ -130,6 +130,15 @@ const ChatRoomMixin = {
         return this.session.get('connection_status') === ROOMSTATUS.ENTERED;
     },
 
+    /**
+     * Checks whether this MUC qualifies for subscribing to XEP-0437 Room Activity Indicators (RAI)
+     * @method _converse.ChatRoom#isRAICandidate
+     * @returns { Boolean }
+     */
+    isRAICandidate () {
+        return this.get('hidden') && api.settings.get('muc_subscribe_to_rai') && this.getOwnAffiliation() !== 'none';
+    },
+
     /**
      * Checks whether we're still joined and if so, restores the MUC state from cache.
      * @private
@@ -137,18 +146,23 @@ const ChatRoomMixin = {
      * @returns { Boolean } Returns `true` if we're still joined, otherwise returns `false`.
      */
     async restoreFromCache () {
-        if (this.isEntered() && (await this.isJoined())) {
-            // We've restored the room from cache and we're still joined.
-            await new Promise(r => this.features.fetch({ 'success': r, 'error': r }));
-            await new Promise(r => this.config.fetch({ 'success': r, 'error': r }));
+        if (this.isEntered()) {
             await this.fetchOccupants().catch(e => log.error(e));
-            await this.fetchMessages().catch(e => log.error(e));
-            return true;
-        } else {
-            this.session.save('connection_status', ROOMSTATUS.DISCONNECTED);
-            this.clearOccupantsCache();
-            return false;
+
+            if (this.isRAICandidate()) {
+                this.session.save('connection_status', ROOMSTATUS.DISCONNECTED);
+                this.enableRAI();
+                return true;
+            } else if (await this.isJoined()) {
+                await new Promise(r => this.config.fetch({ 'success': r, 'error': r }));
+                await new Promise(r => this.features.fetch({ 'success': r, 'error': r }));
+                await this.fetchMessages().catch(e => log.error(e));
+                return true;
+            }
         }
+        this.session.save('connection_status', ROOMSTATUS.DISCONNECTED);
+        this.clearOccupantsCache();
+        return false;
     },
 
     /**
@@ -277,10 +291,7 @@ const ChatRoomMixin = {
         const roomstatus = ROOMSTATUS;
         const conn_status = this.session.get('connection_status');
         if (this.get('hidden')) {
-            if (conn_status === roomstatus.ENTERED &&
-                    api.settings.get('muc_subscribe_to_rai') &&
-                    this.getOwnAffiliation() !== 'none') {
-
+            if (conn_status === roomstatus.ENTERED && this.isRAICandidate()) {
                 this.sendMarkerForLastMessage('received', true);
                 await this.leave();
                 this.enableRAI();
@@ -348,7 +359,7 @@ const ChatRoomMixin = {
 
     async onConnectionStatusChanged () {
         if (this.isEntered()) {
-            if (this.get('hidden') && api.settings.get('muc_subscribe_to_rai') && this.getOwnAffiliation() !== 'none') {
+            if (this.isRAICandidate()) {
                 try {
                     await this.leave();
                 } catch (e) {
@@ -1931,6 +1942,10 @@ const ChatRoomMixin = {
      * @returns {Promise<boolean>}
      */
     async isJoined () {
+        if (!this.isEntered()) {
+            log.info(`isJoined: not pinging MUC ${this.get('jid')} since we're not entered`);
+            return false;
+        }
         if (!api.connection.connected()) {
             await new Promise(resolve => api.listen.once('reconnected', resolve));
         }
@@ -1955,10 +1970,14 @@ const ChatRoomMixin = {
 
     /**
      * Check whether we're still joined and re-join if not
-     * @async
      * @method _converse.ChatRoom#rejoinIfNecessary
      */
     async rejoinIfNecessary () {
+        if (this.isRAICandidate()) {
+            log.debug(`rejoinIfNecessary: not rejoining hidden MUC "${this.get('jid')}" since we're using RAI`);
+            return true;
+        }
+
         if (!(await this.isJoined())) {
             this.rejoin();
             return true;