diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 28dbe2332a7..b7a5b73145c 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -169,6 +169,7 @@ class MailThread(models.AbstractModel): @api.multi def _get_message_unread(self): res = dict((res_id, 0) for res_id in self.ids) + partner_id = self.env.user.partner_id.id # search for unread messages, directly in SQL to improve performances self._cr.execute(""" SELECT msg.res_id FROM mail_message msg @@ -176,8 +177,8 @@ class MailThread(models.AbstractModel): ON rel.mail_message_id = msg.id RIGHT JOIN mail_channel_partner cp ON (cp.channel_id = rel.mail_channel_id AND cp.partner_id = %s AND (cp.seen_message_id < msg.id)) - WHERE msg.model = %s AND msg.res_id in %s""", - (self.env.user.partner_id.id, self._name, tuple(self.ids),)) + WHERE msg.model = %s AND msg.res_id in %s AND msg.author_id != %s""", + (partner_id, self._name, tuple(self.ids), partner_id,)) for result in self._cr.fetchall(): res[result[0]] += 1 diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index ec66cdde362..34559eeaad8 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -133,7 +133,7 @@ function add_message (data, options) { if (!msg) { msg = make_message(data); - // Keep the array ordered by date when inserting the new message + // Keep the array ordered by id when inserting the new message messages.splice(_.sortedIndex(messages, msg, 'id'), 0, msg); _.each(msg.channel_ids, function (channel_id) { var channel = chat_manager.get_channel(channel_id); @@ -807,6 +807,21 @@ var chat_manager = { return chat_unread_counter; }, + get_last_seen_message: function (channel) { + if (channel.last_seen_message_id) { + var messages = channel.cache['[]'].messages; + var msg = _.findWhere(messages, {id: channel.last_seen_message_id}); + if (msg) { + var i = _.sortedIndex(messages, msg, 'id') + 1; + while (i < messages.length && messages[i].author_id && messages[i].author_id[0] === session.partner_id) { + msg = messages[i]; + i++; + } + return msg; + } + } + }, + get_discuss_ids: function () { return discuss_ids; }, diff --git a/addons/mail/static/src/js/client_action.js b/addons/mail/static/src/js/client_action.js index 38cc26e8997..e091590faa1 100644 --- a/addons/mail/static/src/js/client_action.js +++ b/addons/mail/static/src/js/client_action.js @@ -423,13 +423,11 @@ var ChatAction = Widget.extend(ControlPanelMixin, { // Compute position of the 'New messages' separator, only once when joining // a channel to keep it in the thread when new messages arrive if (_.isUndefined(this.messages_separator_position)) { - var msg_id = this.last_seen_message_id; if (!this.unread_counter) { this.messages_separator_position = false; // no unread message -> don't display separator - } else if ((msg_id === false) || !_.findWhere(messages, {id: msg_id})) { - this.messages_separator_position = 'top'; // all displayed messages are unread } else { - this.messages_separator_position = msg_id; // last read message is msg_id + var msg = chat_manager.get_last_seen_message(this.channel); + this.messages_separator_position = msg ? msg.id : 'top'; } } return {