Refactor code that increments unread messages counter

Removed the "fetching_messages" hack.
This commit is contained in:
JC Brand 2017-05-16 09:21:51 +02:00
parent 023a68458c
commit 4f94a1f193
3 changed files with 40 additions and 46 deletions

View File

@ -151,7 +151,6 @@
},
afterMessagesFetched: function () {
this.model.set('fetching_messages', false);
this.insertIntoDOM();
this.scrollDown();
// We only start listening for the scroll event after
@ -160,7 +159,6 @@
},
fetchMessages: function () {
this.model.set('fetching_messages', true);
this.model.messages.fetch({
'add': true,
'success': this.afterMessagesFetched.bind(this),
@ -420,38 +418,12 @@
return !this.$el.is(':visible');
},
updateNewMessageIndicators: function (message) {
/* We have two indicators of new messages. The unread messages
* counter, which shows the number of unread messages in
* the document.title, and the "new messages" indicator in
* a chat area, if it's scrolled up so that new messages
* aren't visible.
*
* In both cases we ignore MAM messages.
*/
if (!message.get('archive_id')) {
if (this.model.get('scrolled', true)) {
this.$el.find('.new-msgs-indicator').removeClass('hidden');
}
if (this.isNewMessageHidden()) {
this.model.incrementUnreadMsgCounter();
}
}
},
isNewMessageHidden: function () {
if (this.model.get('fetching_messages')) {
// We seem to be busy fetching sessionStorage archived
// messages, so the message is not considered new.
return false;
}
return _converse.windowState === 'hidden' || this.model.isScrolledUp();
},
handleTextMessage: function (message) {
this.showMessage(_.clone(message.attributes));
if (message.get('sender') !== 'me') {
this.updateNewMessageIndicators(message);
if (!message.get('archive_id') && this.model.get('scrolled', true)) {
this.$el.find('.new-msgs-indicator').removeClass('hidden');
}
} else {
// We remove the "scrolled" flag so that the chat area
// gets scrolled down. We always want to scroll down
@ -908,7 +880,7 @@
},
onWindowStateChanged: function (state) {
if (this.model.get('num_unread', 0) && !this.isNewMessageHidden()) {
if (this.model.get('num_unread', 0) && !this.model.newMessageWillBeHidden()) {
this.model.clearUnreadMsgCounter();
}
}

View File

@ -1423,9 +1423,34 @@
return this.messages.create(this.getMessageAttributes.apply(this, arguments));
},
incrementUnreadMsgCounter: function() {
isNewMessage: function (stanza) {
/* Given a message stanza, determine whether it's a new
* message, i.e. not an archived one.
*/
return !(sizzle('result[xmlns="'+Strophe.NS.MAM+'"]', stanza).length);
},
newMessageWillBeHidden: function () {
/* Returns a boolean to indicate whether a newly received
* message will be visible to the user or not.
*/
return this.get('hidden') ||
this.get('minimized') ||
this.isScrolledUp() ||
_converse.windowState === 'hidden';
},
incrementUnreadMsgCounter: function (stanza) {
/* Given a newly received message, update the unread counter if
* necessary.
*/
if (_.isNull(stanza.querySelector('body'))) {
return; // The message has no text
}
if (this.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
this.save({'num_unread': this.get('num_unread') + 1});
_converse.incrementMsgCounter();
}
},
clearUnreadMsgCounter: function() {
@ -1555,6 +1580,7 @@
if (_.isEmpty(messages)) {
// Only create the message when we're sure it's not a
// duplicate
chatbox.incrementUnreadMsgCounter(original_stanza);
chatbox.createMessage(message, delay, original_stanza);
}
}

View File

@ -947,7 +947,7 @@
var onMessageReceived = function (data) {
/* Given a newly received message, update the unread counter on
* the relevant roster contact (TODO: or chat room).
* the relevant roster contact.
*/
var chatbox = data.chatbox;
if (_.isUndefined(chatbox)) {
@ -956,19 +956,15 @@
if (_.isNull(data.stanza.querySelector('body'))) {
return; // The message has no text
}
var new_message = !(sizzle('result[xmlns="'+Strophe.NS.MAM+'"]', data.stanza).length);
var is_new_message_hidden = chatbox.get('hidden') || chatbox.get('minimized') || chatbox.isScrolledUp();
if (chatbox.get('type') !== 'chatroom' &&
chatbox.isNewMessage(data.stanza) &&
chatbox.newMessageWillBeHidden()) {
if (is_new_message_hidden && new_message) {
if (chatbox.get('type') === 'chatroom') {
// TODO
} else {
var contact = _.head(_converse.roster.where({'jid': chatbox.get('jid')}));
if (!_.isUndefined(contact)) {
contact.save({'num_unread': contact.get('num_unread') + 1});
}
}
}
};
var onChatBoxScrolledDown = function (data) {