瀏覽代碼

Improve and simplify unread message notifications in the roster

Use one event instead of two, by listening to `num_unread` on chatboxes.
JC Brand 7 年之前
父節點
當前提交
37b622251e
共有 6 個文件被更改,包括 27 次插入129 次删除
  1. 14 73
      dist/converse.js
  2. 2 2
      src/converse-chatboxes.js
  3. 2 5
      src/converse-chatview.js
  4. 1 1
      src/converse-muc.js
  5. 8 47
      src/converse-rosterview.js
  6. 0 1
      src/converse-singleton.js

+ 14 - 73
dist/converse.js

@@ -62602,7 +62602,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
           }
         },
 
-        newMessageWillBeHidden() {
+        isHidden() {
           /* Returns a boolean to indicate whether a newly received
            * message will be visible to the user or not.
            */
@@ -62617,7 +62617,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
             return; // The message has no text
           }
 
-          if (utils.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
+          if (utils.isNewMessage(stanza) && this.isHidden()) {
             this.save({
               'num_unread': this.get('num_unread') + 1
             });
@@ -64141,11 +64141,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
         },
 
         afterShown() {
-          if (u.isPersistableModel(this.model)) {
-            this.model.clearUnreadMsgCounter();
-            this.model.save();
-          }
-
+          this.model.clearUnreadMsgCounter();
           this.setChatState(_converse.ACTIVE);
           this.renderEmojiPicker();
           this.scrollDown();
@@ -64231,7 +64227,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
         },
 
         onWindowStateChanged(state) {
-          if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
+          if (this.model.get('num_unread', 0) && !this.model.isHidden()) {
             this.model.clearUnreadMsgCounter();
           }
         }
@@ -72205,7 +72201,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
             return; // The message has no text
           }
 
-          if (u.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
+          if (u.isNewMessage(stanza) && this.isHidden()) {
             const settings = {
               'num_unread_general': this.get('num_unread_general') + 1
             };
@@ -76510,71 +76506,23 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
       });
       /* -------- Event Handlers ----------- */
 
-      const onChatBoxMaximized = function onChatBoxMaximized(chatboxview) {
-        /* When a chat box gets maximized, the num_unread counter needs
-         * to be cleared, but if chatbox is scrolled up, then num_unread should not be cleared.
-         */
-        const chatbox = chatboxview.model;
-
-        if (chatbox.get('type') !== 'chatroom') {
-          const contact = _.head(_converse.roster.where({
-            'jid': chatbox.get('jid')
-          }));
-
-          if (!_.isUndefined(contact) && !chatbox.isScrolledUp()) {
-            contact.save({
-              'num_unread': 0
-            });
-          }
-        }
-      };
-
-      const onMessageReceived = function onMessageReceived(data) {
-        /* Given a newly received message, update the unread counter on
-         * the relevant roster contact.
-         */
-        const chatbox = data.chatbox;
-
-        if (_.isUndefined(chatbox)) {
-          return;
-        }
-
-        if (_.isNull(data.stanza.querySelector('body'))) {
-          return; // The message has no text
-        }
-
-        if (chatbox.get('type') !== 'chatroom' && u.isNewMessage(data.stanza) && chatbox.newMessageWillBeHidden()) {
-          const contact = _.head(_converse.roster.where({
-            'jid': chatbox.get('jid')
-          }));
-
-          if (!_.isUndefined(contact)) {
-            contact.save({
-              'num_unread': contact.get('num_unread') + 1
-            });
-          }
-        }
-      };
-
-      const onChatBoxScrolledDown = function onChatBoxScrolledDown(data) {
-        const chatbox = data.chatbox;
-
-        if (_.isUndefined(chatbox)) {
-          return;
-        }
-
+      function updateUnreadCounter(chatbox) {
         const contact = _.head(_converse.roster.where({
           'jid': chatbox.get('jid')
         }));
 
         if (!_.isUndefined(contact)) {
           contact.save({
-            'num_unread': 0
+            'num_unread': chatbox.get('num_unread')
           });
         }
-      };
+      }
+
+      _converse.api.listen.on('chatBoxesInitialized', () => {
+        _converse.chatboxes.on('change:num_unread', updateUnreadCounter);
+      });
 
-      const initRoster = function initRoster() {
+      function initRoster() {
         /* Create an instance of RosterView once the RosterGroups
          * collection has been created (in converse-core.js)
          */
@@ -76585,17 +76533,11 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
         _converse.rosterview.render();
 
         _converse.emit('rosterViewInitialized');
-      };
+      }
 
       _converse.api.listen.on('rosterInitialized', initRoster);
 
       _converse.api.listen.on('rosterReadyAfterReconnection', initRoster);
-
-      _converse.api.listen.on('message', onMessageReceived);
-
-      _converse.api.listen.on('chatBoxMaximized', onChatBoxMaximized);
-
-      _converse.api.listen.on('chatBoxScrolledDown', onChatBoxScrolledDown);
     }
 
   });
@@ -76689,7 +76631,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
       ChatBoxView: {
         shouldShowOnTextMessage() {
           if (_.includes(['mobile', 'fullscreen', 'embedded'], this.__super__._converse.view_mode)) {
-            this.model.set('hidden', true);
             return false;
           } else {
             return this.__super__.shouldShowOnTextMessage.apply(this, arguments);

+ 2 - 2
src/converse-chatboxes.js

@@ -506,7 +506,7 @@
                     }
                 },
 
-                newMessageWillBeHidden () {
+                isHidden () {
                     /* Returns a boolean to indicate whether a newly received
                      * message will be visible to the user or not.
                      */
@@ -523,7 +523,7 @@
                     if (_.isNull(stanza.querySelector('body'))) {
                         return; // The message has no text
                     }
-                    if (utils.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
+                    if (utils.isNewMessage(stanza) && this.isHidden()) {
                         this.save({'num_unread': this.get('num_unread') + 1});
                         _converse.incrementMsgCounter();
                     }

+ 2 - 5
src/converse-chatview.js

@@ -1084,10 +1084,7 @@
                 },
 
                 afterShown () {
-                    if (u.isPersistableModel(this.model)) {
-                        this.model.clearUnreadMsgCounter();
-                        this.model.save();
-                    }
+                    this.model.clearUnreadMsgCounter();
                     this.setChatState(_converse.ACTIVE);
                     this.renderEmojiPicker();
                     this.scrollDown();
@@ -1164,7 +1161,7 @@
                 },
 
                 onWindowStateChanged (state) {
-                    if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
+                    if (this.model.get('num_unread', 0) && !this.model.isHidden()) {
                         this.model.clearUnreadMsgCounter();
                     }
                 }

+ 1 - 1
src/converse-muc.js

@@ -953,7 +953,7 @@
                     if (_.isNull(body)) {
                         return; // The message has no text
                     }
-                    if (u.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
+                    if (u.isNewMessage(stanza) && this.isHidden()) {
                         const settings = {'num_unread_general': this.get('num_unread_general') + 1};
                         if (this.isUserMentioned(body.textContent)) {
                             settings.num_unread = this.get('num_unread') + 1;

+ 8 - 47
src/converse-rosterview.js

@@ -938,53 +938,17 @@
 
             /* -------- Event Handlers ----------- */
 
-            const onChatBoxMaximized = function (chatboxview) {
-                /* When a chat box gets maximized, the num_unread counter needs
-                 * to be cleared, but if chatbox is scrolled up, then num_unread should not be cleared.
-                 */
-                const chatbox = chatboxview.model;
-                if (chatbox.get('type') !== 'chatroom') {
-                    const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
-                    if (!_.isUndefined(contact) && !chatbox.isScrolledUp()) {
-                        contact.save({'num_unread': 0});
-                    }
-                }
-            };
-
-            const onMessageReceived = function (data) {
-                /* Given a newly received message, update the unread counter on
-                 * the relevant roster contact.
-                 */
-                const { chatbox } = data;
-                if (_.isUndefined(chatbox)) {
-                    return;
-                }
-                if (_.isNull(data.stanza.querySelector('body'))) {
-                    return; // The message has no text
-                }
-                if (chatbox.get('type') !== 'chatroom' &&
-                    u.isNewMessage(data.stanza) &&
-                    chatbox.newMessageWillBeHidden()) {
-
-                    const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
-                    if (!_.isUndefined(contact)) {
-                        contact.save({'num_unread': contact.get('num_unread') + 1});
-                    }
-                }
-            };
-
-            const onChatBoxScrolledDown = function (data) {
-                const { chatbox } = data;
-                if (_.isUndefined(chatbox)) {
-                    return;
-                }
+            function updateUnreadCounter (chatbox) {
                 const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
                 if (!_.isUndefined(contact)) {
-                    contact.save({'num_unread': 0});
+                    contact.save({'num_unread': chatbox.get('num_unread')});
                 }
-            };
+            }
+            _converse.api.listen.on('chatBoxesInitialized', () => {
+                _converse.chatboxes.on('change:num_unread', updateUnreadCounter)
+            });
 
-            const initRoster = function () {
+            function initRoster () {
                 /* Create an instance of RosterView once the RosterGroups
                  * collection has been created (in converse-core.js)
                  */
@@ -993,12 +957,9 @@
                 });
                 _converse.rosterview.render();
                 _converse.emit('rosterViewInitialized');
-            };
+            }
             _converse.api.listen.on('rosterInitialized', initRoster);
             _converse.api.listen.on('rosterReadyAfterReconnection', initRoster);
-            _converse.api.listen.on('message', onMessageReceived);
-            _converse.api.listen.on('chatBoxMaximized', onChatBoxMaximized);
-            _converse.api.listen.on('chatBoxScrolledDown', onChatBoxScrolledDown);
         }
     });
 }));

+ 0 - 1
src/converse-singleton.js

@@ -66,7 +66,6 @@
             ChatBoxView: {
                 shouldShowOnTextMessage () {
                     if (_.includes(['mobile', 'fullscreen', 'embedded'], this.__super__._converse.view_mode)) {
-                        this.model.set('hidden', true);
                         return false;
                     } else { 
                         return this.__super__.shouldShowOnTextMessage.apply(this, arguments);