Improve and simplify unread message notifications in the roster

Use one event instead of two, by listening to `num_unread` on chatboxes.
This commit is contained in:
JC Brand 2018-06-07 13:40:20 +02:00
parent 083f191ead
commit 37b622251e
6 changed files with 27 additions and 129 deletions

79
dist/converse.js vendored
View File

@ -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 /* Returns a boolean to indicate whether a newly received
* message will be visible to the user or not. * 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 return; // The message has no text
} }
if (utils.isNewMessage(stanza) && this.newMessageWillBeHidden()) { if (utils.isNewMessage(stanza) && this.isHidden()) {
this.save({ this.save({
'num_unread': this.get('num_unread') + 1 'num_unread': this.get('num_unread') + 1
}); });
@ -64141,11 +64141,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
}, },
afterShown() { afterShown() {
if (u.isPersistableModel(this.model)) {
this.model.clearUnreadMsgCounter(); this.model.clearUnreadMsgCounter();
this.model.save();
}
this.setChatState(_converse.ACTIVE); this.setChatState(_converse.ACTIVE);
this.renderEmojiPicker(); this.renderEmojiPicker();
this.scrollDown(); this.scrollDown();
@ -64231,7 +64227,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
}, },
onWindowStateChanged(state) { 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(); this.model.clearUnreadMsgCounter();
} }
} }
@ -72205,7 +72201,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
return; // The message has no text return; // The message has no text
} }
if (u.isNewMessage(stanza) && this.newMessageWillBeHidden()) { if (u.isNewMessage(stanza) && this.isHidden()) {
const settings = { const settings = {
'num_unread_general': this.get('num_unread_general') + 1 '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 ----------- */ /* -------- Event Handlers ----------- */
const onChatBoxMaximized = function onChatBoxMaximized(chatboxview) { function updateUnreadCounter(chatbox) {
/* 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({ const contact = _.head(_converse.roster.where({
'jid': chatbox.get('jid') 'jid': chatbox.get('jid')
})); }));
if (!_.isUndefined(contact)) { if (!_.isUndefined(contact)) {
contact.save({ contact.save({
'num_unread': contact.get('num_unread') + 1 'num_unread': chatbox.get('num_unread')
}); });
} }
} }
};
const onChatBoxScrolledDown = function onChatBoxScrolledDown(data) { _converse.api.listen.on('chatBoxesInitialized', () => {
const chatbox = data.chatbox; _converse.chatboxes.on('change:num_unread', updateUnreadCounter);
if (_.isUndefined(chatbox)) {
return;
}
const contact = _.head(_converse.roster.where({
'jid': chatbox.get('jid')
}));
if (!_.isUndefined(contact)) {
contact.save({
'num_unread': 0
}); });
}
};
const initRoster = function initRoster() { function initRoster() {
/* Create an instance of RosterView once the RosterGroups /* Create an instance of RosterView once the RosterGroups
* collection has been created (in converse-core.js) * 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.rosterview.render();
_converse.emit('rosterViewInitialized'); _converse.emit('rosterViewInitialized');
}; }
_converse.api.listen.on('rosterInitialized', initRoster); _converse.api.listen.on('rosterInitialized', initRoster);
_converse.api.listen.on('rosterReadyAfterReconnection', 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: { ChatBoxView: {
shouldShowOnTextMessage() { shouldShowOnTextMessage() {
if (_.includes(['mobile', 'fullscreen', 'embedded'], this.__super__._converse.view_mode)) { if (_.includes(['mobile', 'fullscreen', 'embedded'], this.__super__._converse.view_mode)) {
this.model.set('hidden', true);
return false; return false;
} else { } else {
return this.__super__.shouldShowOnTextMessage.apply(this, arguments); return this.__super__.shouldShowOnTextMessage.apply(this, arguments);

View File

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

View File

@ -1084,10 +1084,7 @@
}, },
afterShown () { afterShown () {
if (u.isPersistableModel(this.model)) {
this.model.clearUnreadMsgCounter(); this.model.clearUnreadMsgCounter();
this.model.save();
}
this.setChatState(_converse.ACTIVE); this.setChatState(_converse.ACTIVE);
this.renderEmojiPicker(); this.renderEmojiPicker();
this.scrollDown(); this.scrollDown();
@ -1164,7 +1161,7 @@
}, },
onWindowStateChanged (state) { 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(); this.model.clearUnreadMsgCounter();
} }
} }

View File

@ -953,7 +953,7 @@
if (_.isNull(body)) { if (_.isNull(body)) {
return; // The message has no text 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}; const settings = {'num_unread_general': this.get('num_unread_general') + 1};
if (this.isUserMentioned(body.textContent)) { if (this.isUserMentioned(body.textContent)) {
settings.num_unread = this.get('num_unread') + 1; settings.num_unread = this.get('num_unread') + 1;

View File

@ -938,53 +938,17 @@
/* -------- Event Handlers ----------- */ /* -------- Event Handlers ----------- */
const onChatBoxMaximized = function (chatboxview) { function updateUnreadCounter (chatbox) {
/* 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')})); const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact)) { if (!_.isUndefined(contact)) {
contact.save({'num_unread': contact.get('num_unread') + 1}); contact.save({'num_unread': chatbox.get('num_unread')});
} }
} }
}; _converse.api.listen.on('chatBoxesInitialized', () => {
_converse.chatboxes.on('change:num_unread', updateUnreadCounter)
});
const onChatBoxScrolledDown = function (data) { function initRoster () {
const { chatbox } = data;
if (_.isUndefined(chatbox)) {
return;
}
const contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact)) {
contact.save({'num_unread': 0});
}
};
const initRoster = function () {
/* Create an instance of RosterView once the RosterGroups /* Create an instance of RosterView once the RosterGroups
* collection has been created (in converse-core.js) * collection has been created (in converse-core.js)
*/ */
@ -993,12 +957,9 @@
}); });
_converse.rosterview.render(); _converse.rosterview.render();
_converse.emit('rosterViewInitialized'); _converse.emit('rosterViewInitialized');
}; }
_converse.api.listen.on('rosterInitialized', initRoster); _converse.api.listen.on('rosterInitialized', initRoster);
_converse.api.listen.on('rosterReadyAfterReconnection', 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);
} }
}); });
})); }));

View File

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