From b3868a613c77e2169be1947ce61cbdfcd62a7bf9 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 7 Jan 2016 13:07:41 +0100 Subject: [PATCH] [IMP] mail: needaction/unread messages counters Some UI changes: - Unread messages in chat are now displayed as needactions in the Discuss's sidebar (badge with counter of unread messages, instead of simply bolding the channel name). - The counter displayed in the navbar is now the sum of needactions and total of unread messages in chat. - The counter displayed next to Inbox is still the needaction counter. For consistency between tabs/browsers/devices, the channel_seen info is now broadcasted, meaning that every tab/browser/device will be noticed. Also now correctly increment the unread counter of a channel if the received message is a needaction. --- addons/mail/models/mail_channel.py | 1 + addons/mail/static/src/js/chat_manager.js | 27 ++++++++++++++++++-- addons/mail/static/src/js/systray.js | 6 +++-- addons/mail/static/src/xml/client_action.xml | 2 +- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index 08b827b6cc7..e9ddbfd2e61 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -446,6 +446,7 @@ class Channel(models.Model): if self.channel_message_ids.ids: last_message_id = self.channel_message_ids.ids[0] # zero is the index of the last message self.env['mail.channel.partner'].search([('channel_id', 'in', self.ids), ('partner_id', '=', self.env.user.partner_id.id)]).write({'seen_message_id': last_message_id}) + self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', self.env.user.partner_id.id), {'info': 'channel_seen', 'id': self.id}) return last_message_id @api.multi diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index 150e803920c..c5b510ce8ad 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -23,6 +23,7 @@ var messages = []; var channels = []; var channels_preview_def; var channel_defs = {}; +var chat_unread_counter = 0; var emojis = []; var emoji_substitutions = {}; var needaction_counter = 0; @@ -303,7 +304,7 @@ function make_channel (data, options) { display_needactions: options.display_needactions, mass_mailing: data.mass_mailing, needaction_counter: data.message_needaction_counter || 0, - unread_counter: data.message_unread_counter || 0, + unread_counter: 0, last_seen_message_id: data.seen_message_id, cache: {'[]': { all_history_loaded: false, @@ -325,6 +326,9 @@ function make_channel (data, options) { bus.update_option('bus_presence_partner_ids', pinned_dm_partners); } channel.is_chat = !channel.type.match(/^(public|private|static)$/); + if (data.message_unread_counter) { + update_channel_unread_counter(channel, data.message_unread_counter); + } return channel; } @@ -440,6 +444,9 @@ function fetch_document_messages (ids, options) { } function update_channel_unread_counter (channel, counter) { + if (channel.is_chat) { + chat_unread_counter = chat_unread_counter - channel.unread_counter + counter; + } channel.unread_counter = counter; chat_manager.bus.trigger("update_channel_unread_counter", channel); } @@ -470,7 +477,11 @@ function on_notification (notification) { } function on_needaction_notification (message) { - message = add_message(message, { channel_id: 'channel_inbox', show_notification: true} ); + message = add_message(message, { + channel_id: 'channel_inbox', + show_notification: true, + increment_unread: true, + }); invalidate_caches(message.channel_ids); needaction_counter++; _.each(message.channel_ids, function (channel_id) { @@ -505,6 +516,8 @@ function on_partner_notification (data) { on_mark_as_read_notification(data); } else if (data.type === 'mark_as_unread') { on_mark_as_unread_notification(data); + } else if (data.info === 'channel_seen') { + on_channel_seen_notification(data); } else { on_chat_session_notification(data); } @@ -575,6 +588,13 @@ function on_mark_as_unread_notification (data) { chat_manager.bus.trigger('update_needaction', needaction_counter); } +function on_channel_seen_notification (data) { + var channel = chat_manager.get_channel(data.id); + if (channel && channel.unread_counter) { + update_channel_unread_counter(channel, 0); + } +} + function on_chat_session_notification (chat_session) { var channel; if ((chat_session.channel_type === "channel") && (chat_session.state === "open")) { @@ -760,6 +780,9 @@ var chat_manager = { get_needaction_counter: function () { return needaction_counter; }, + get_chat_unread_counter: function () { + return chat_unread_counter; + }, get_discuss_ids: function () { return discuss_ids; diff --git a/addons/mail/static/src/js/systray.js b/addons/mail/static/src/js/systray.js index 74209a967ec..342167bfed2 100644 --- a/addons/mail/static/src/js/systray.js +++ b/addons/mail/static/src/js/systray.js @@ -30,13 +30,15 @@ var MessagingMenu = Widget.extend({ this.$channels_preview = this.$('.o_mail_navbar_dropdown_channels'); this.filter = false; chat_manager.bus.on("update_needaction", this, this.update_counter); - this.update_counter(chat_manager.get_needaction_counter()); + chat_manager.bus.on("update_channel_unread_counter", this, this.update_counter); + this.update_counter(); return this._super(); }, is_open: function () { return this.$el.hasClass('open'); }, - update_counter: function (counter) { + update_counter: function () { + var counter = chat_manager.get_needaction_counter() + chat_manager.get_chat_unread_counter(); this.$('.o_notification_counter').text(counter); if (this.is_open()) { this.render_channels_preview(); diff --git a/addons/mail/static/src/xml/client_action.xml b/addons/mail/static/src/xml/client_action.xml index 55be6233d22..04d45eb9bdc 100644 --- a/addons/mail/static/src/xml/client_action.xml +++ b/addons/mail/static/src/xml/client_action.xml @@ -81,7 +81,7 @@ - +