浏览代码

muc: Differentiate between generally unread messages and unread mentions

JC Brand 8 年之前
父节点
当前提交
c83026e5a8
共有 4 个文件被更改,包括 66 次插入38 次删除
  1. 40 30
      spec/roomslist.js
  2. 24 7
      src/converse-muc.js
  3. 1 0
      src/converse-roomslist.js
  4. 1 1
      src/templates/rooms_list_item.html

+ 40 - 30
spec/roomslist.js

@@ -51,36 +51,6 @@
 
 
     describe("An room shown in the rooms list", function () {
     describe("An room shown in the rooms list", function () {
 
 
-        it("shows unread messages directed at the user", mock.initConverse(
-            { whitelisted_plugins: ['converse-roomslist'],
-              allow_bookmarks: false // Makes testing easier, otherwise we
-                                     // have to mock stanza traffic.
-            }, function (_converse) {
-
-            var room_jid = 'kitchen@conference.shakespeare.lit';
-            test_utils.openAndEnterChatRoom(
-                _converse, 'kitchen', 'conference.shakespeare.lit', 'romeo');
-            var view = _converse.chatboxviews.get(room_jid);
-            view.model.set({'minimized': true});
-
-            var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
-            var message = 'romeo: Your attention is required';
-            var nick = mock.chatroom_names[0],
-                msg = $msg({
-                    from: room_jid+'/'+nick,
-                    id: (new Date()).getTime(),
-                    to: 'dummy@localhost',
-                    type: 'groupchat'
-                }).c('body').t(message).tree();
-            view.handleMUCMessage(msg);
-
-            expect(_converse.minimized_chats.toggleview.$('.unread-message-count').is(':visible')).toBeTruthy();
-            expect(_converse.minimized_chats.toggleview.$('.unread-message-count').text()).toBe('1');
-
-            var room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
-            expect(_.includes(room_el.classList, 'unread-msgs'));
-        }));
-
         it("can be closed", mock.initConverse(
         it("can be closed", mock.initConverse(
             { whitelisted_plugins: ['converse-roomslist'],
             { whitelisted_plugins: ['converse-roomslist'],
               allow_bookmarks: false // Makes testing easier, otherwise we
               allow_bookmarks: false // Makes testing easier, otherwise we
@@ -106,5 +76,45 @@
             expect(room_els.length).toBe(0);
             expect(room_els.length).toBe(0);
             expect(_converse.chatboxes.length).toBe(1);
             expect(_converse.chatboxes.length).toBe(1);
         }));
         }));
+
+        it("shows unread messages directed at the user", mock.initConverse(
+            { whitelisted_plugins: ['converse-roomslist'],
+              allow_bookmarks: false // Makes testing easier, otherwise we
+                                     // have to mock stanza traffic.
+            }, function (_converse) {
+
+            var room_jid = 'kitchen@conference.shakespeare.lit';
+            test_utils.openAndEnterChatRoom(
+                _converse, 'kitchen', 'conference.shakespeare.lit', 'romeo');
+            var view = _converse.chatboxviews.get(room_jid);
+            view.model.set({'minimized': true});
+
+
+            var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
+            var nick = mock.chatroom_names[0];
+            view.handleMUCMessage(
+                $msg({
+                    from: room_jid+'/'+nick,
+                    id: (new Date()).getTime(),
+                    to: 'dummy@localhost',
+                    type: 'groupchat'
+                }).c('body').t('foo').tree());
+
+            // If the user isn't mentioned, the counter doesn't get incremented, but the text of the room is bold
+            var room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
+            expect(_.includes(room_el.classList, 'unread-msgs'));
+
+            // If the user is mentioned, the counter also gets updated
+            view.handleMUCMessage(
+                $msg({
+                    from: room_jid+'/'+nick,
+                    id: (new Date()).getTime(),
+                    to: 'dummy@localhost',
+                    type: 'groupchat'
+                }).c('body').t('romeo: Your attention is required').tree()
+            );
+            var indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicactor");
+            expect(indicator_el.textContent).toBe('1');
+        }));
     });
     });
 }));
 }));

+ 24 - 7
src/converse-muc.js

@@ -362,6 +362,18 @@
 
 
             _converse.ChatRoom = _converse.ChatBox.extend({
             _converse.ChatRoom = _converse.ChatBox.extend({
 
 
+                defaults: _.extend(_converse.ChatBox.prototype.defaults, {
+                    // For group chats, we distinguish between generally unread
+                    // messages and those ones that specifically mention the
+                    // user.
+                    //
+                    // To keep things simple, we reuse `num_unread` from
+                    // _converse.ChatBox to indicate unread messages which
+                    // mention the user and `num_unread_general` to indicate
+                    // generally unread messages (which *includes* mentions!).
+                    'num_unread_general': 0
+                }),
+
                 isUserMentioned: function (message) {
                 isUserMentioned: function (message) {
                     /* Returns a boolean to indicate whether the current user
                     /* Returns a boolean to indicate whether the current user
                      * was mentioned in a message.
                      * was mentioned in a message.
@@ -383,14 +395,19 @@
                     if (_.isNull(body)) {
                     if (_.isNull(body)) {
                         return; // The message has no text
                         return; // The message has no text
                     }
                     }
-                    if (this.isNewMessage(stanza) &&
-                            this.newMessageWillBeHidden() &&
-                            this.isUserMentioned(body.textContent)) {
-
-                        this.save({'num_unread': this.get('num_unread') + 1});
-                        _converse.incrementMsgCounter();
+                    if (this.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
+                        this.save({'num_unread_general': this.get('num_unread_general') + 1});
+                        if (this.isUserMentioned(body.textContent)) {
+                            this.save({'num_unread': this.get('num_unread') + 1});
+                            _converse.incrementMsgCounter();
+                        }
                     }
                     }
                 },
                 },
+
+                clearUnreadMsgCounter: function() {
+                    this.save({'num_unread': 0});
+                    this.save({'num_unread_general': 0});
+                }
             });
             });
 
 
             _converse.ChatRoomView = _converse.ChatBoxView.extend({
             _converse.ChatRoomView = _converse.ChatBoxView.extend({
@@ -1919,7 +1936,7 @@
                         }
                         }
                     } else if (!this.model.get('features_fetched')) {
                     } else if (!this.model.get('features_fetched')) {
                         // The features for this room weren't fetched.
                         // The features for this room weren't fetched.
-                        // That must mean it's a new room without locking 
+                        // That must mean it's a new room without locking
                         // (in which case Prosody doesn't send a 201 status),
                         // (in which case Prosody doesn't send a 201 status),
                         // otherwise the features would have been fetched in
                         // otherwise the features would have been fetched in
                         // the "initialize" method already.
                         // the "initialize" method already.

+ 1 - 0
src/converse-roomslist.js

@@ -51,6 +51,7 @@
                     this.model.on('change:bookmarked', this.renderRoomsListElement, this);
                     this.model.on('change:bookmarked', this.renderRoomsListElement, this);
                     this.model.on('change:name', this.renderRoomsListElement, this);
                     this.model.on('change:name', this.renderRoomsListElement, this);
                     this.model.on('change:num_unread', this.renderRoomsListElement, this);
                     this.model.on('change:num_unread', this.renderRoomsListElement, this);
+                    this.model.on('change:num_unread_general', this.renderRoomsListElement, this);
                     this.model.on('remove', this.removeRoomsListElement, this);
                     this.model.on('remove', this.removeRoomsListElement, this);
 
 
                     var cachekey = 'converse.roomslist'+_converse.bare_jid;
                     var cachekey = 'converse.roomslist'+_converse.bare_jid;

+ 1 - 1
src/templates/rooms_list_item.html

@@ -1,4 +1,4 @@
-<dd class="available-chatroom {[ if (num_unread) { ]} unread-msgs {[ } ]}" data-room-jid="{{{jid}}}"> 
+<dd class="available-chatroom {[ if (num_unread_general) { ]} unread-msgs {[ } ]}" data-room-jid="{{{jid}}}">
 {[ if (num_unread) { ]}
 {[ if (num_unread) { ]}
     <span class="msgs-indicactor">{{{ num_unread }}}</span>
     <span class="msgs-indicactor">{{{ num_unread }}}</span>
 {[ } ]}
 {[ } ]}