muc: Differentiate between generally unread messages and unread mentions

This commit is contained in:
JC Brand 2017-05-24 08:40:09 +02:00
parent 8fa06e7d52
commit c83026e5a8
4 changed files with 66 additions and 38 deletions

View File

@ -51,36 +51,6 @@
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(
{ whitelisted_plugins: ['converse-roomslist'],
allow_bookmarks: false // Makes testing easier, otherwise we
@ -106,5 +76,45 @@
expect(room_els.length).toBe(0);
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');
}));
});
}));

View File

@ -362,6 +362,18 @@
_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) {
/* Returns a boolean to indicate whether the current user
* was mentioned in a message.
@ -383,14 +395,19 @@
if (_.isNull(body)) {
return; // The message has no text
}
if (this.isNewMessage(stanza) &&
this.newMessageWillBeHidden() &&
this.isUserMentioned(body.textContent)) {
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({

View File

@ -51,6 +51,7 @@
this.model.on('change:bookmarked', 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_general', this.renderRoomsListElement, this);
this.model.on('remove', this.removeRoomsListElement, this);
var cachekey = 'converse.roomslist'+_converse.bare_jid;

View File

@ -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) { ]}
<span class="msgs-indicactor">{{{ num_unread }}}</span>
{[ } ]}