diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js index 12142766bf1..e3609946525 100644 --- a/addons/im_livechat/static/src/js/im_livechat.js +++ b/addons/im_livechat/static/src/js/im_livechat.js @@ -42,6 +42,9 @@ var LivechatButton = Widget.extend({ bus.on('notification', this, function (notification) { this.add_message(notification[1]); this.render_messages(); + if (this.chat_window.folded) { + this.chat_window.update_unread(this.chat_window.unread_msgs+1); + } }); return this._super(); }, @@ -93,7 +96,7 @@ var LivechatButton = Widget.extend({ var options = { display_stars: false, }; - this.chat_window = new ChatWindow(this, channel.id, channel.name, false, options); + this.chat_window = new ChatWindow(this, channel.id, channel.name, false, channel.message_unread_counter, options); this.chat_window.appendTo($('body')).then(function () { self.chat_window.$el.css({right: 0, bottom: 0}); self.$el.hide(); diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index 239291628f0..7da3329a178 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -306,6 +306,11 @@ function fetch_document_messages (ids, options) { } } +function update_channel_unread_counter (channel, counter) { + channel.unread_counter = counter; + chat_manager.bus.trigger("update_channel_unread_counter", channel); +} + var channel_seen = _.throttle(function (channel) { return ChannelModel.call('channel_seen', [[channel.id]]).then(function (last_seen_message_id) { channel.last_seen_message_id = last_seen_message_id; @@ -343,7 +348,7 @@ function on_needaction_notification (message) { function on_channel_notification (message) { var channel = chat_manager.get_channel(message.channel_ids[0]); if (channel) { - channel.unread_counter++; + update_channel_unread_counter(channel, channel.unread_counter+1); add_message(message, { channel_id: channel.id, show_notification: true }); } else { chat_manager.join_channel(message.channel_ids[0], {autoswitch: false}).then(function() { @@ -492,7 +497,7 @@ var chat_manager = { }, mark_channel_as_seen: function (channel) { if (channel.unread_counter > 0) { - channel.unread_counter = 0; + update_channel_unread_counter(channel, 0); channel_seen(channel); } }, diff --git a/addons/mail/static/src/js/chat_window.js b/addons/mail/static/src/js/chat_window.js index cfc3c8c5447..8fa1e2a2536 100644 --- a/addons/mail/static/src/js/chat_window.js +++ b/addons/mail/static/src/js/chat_window.js @@ -16,7 +16,7 @@ return Widget.extend({ "click .o_chat_title": "on_click_fold", }, - init: function (parent, channel_id, title, is_folded, options) { + init: function (parent, channel_id, title, is_folded, unread_msgs, options) { this._super(parent); this.title = title; this.channel_id = channel_id; @@ -25,7 +25,7 @@ return Widget.extend({ this.options = _.defaults(options || {}, { display_stars: true, }); - this.unread_msgs = -1; + this.unread_msgs = unread_msgs || 0; }, start: function () { this.$content = this.$('.o_chat_content'); @@ -46,9 +46,6 @@ return Widget.extend({ return $.when(this._super(), def); }, render: function (messages) { - if (this.folded) { - this.unread_msgs++; - } this.update_header(); this.thread.render(messages, {display_load_more: false}); }, @@ -57,6 +54,10 @@ return Widget.extend({ this.title + ' (' + this.unread_msgs + ')' : this.title; this.$('.o_chat_title').text(title); }, + update_unread: function (counter) { + this.unread_msgs = counter; + this.update_header(); + }, scrollBottom: function () { this.$content.scrollTop(this.$content[0].scrollHeight); }, @@ -70,6 +71,7 @@ return Widget.extend({ this.folded = !this.folded; if (!this.folded) { this.unread_msgs = 0; + this.trigger('messages_read'); } this.fold(); }, diff --git a/addons/mail/static/src/js/client_action.js b/addons/mail/static/src/js/client_action.js index 7f9e3a83665..874debf6a3d 100644 --- a/addons/mail/static/src/js/client_action.js +++ b/addons/mail/static/src/js/client_action.js @@ -218,6 +218,7 @@ var ChatAction = Widget.extend(ControlPanelMixin, { }); chat_manager.bus.on('update_needaction', self, self.render_sidebar); chat_manager.bus.on('unsubscribe_from_channel', self, self.render_sidebar); + chat_manager.bus.on('update_channel_unread_counter', self, self.render_sidebar); }); }, diff --git a/addons/mail/static/src/js/window_manager.js b/addons/mail/static/src/js/window_manager.js index 2326ef14dc2..22eb60f9180 100644 --- a/addons/mail/static/src/js/window_manager.js +++ b/addons/mail/static/src/js/window_manager.js @@ -15,12 +15,16 @@ var CHAT_WINDOW_WIDTH = 260; function open_chat (session) { if (!_.findWhere(chat_sessions, {id: session.id})) { + var channel = chat_manager.get_channel(session.id); var chat_session = { id: session.id, uuid: session.uuid, name: session.name, - window: new ChatWindow(web_client, session.id, session.name, session.is_folded), + window: new ChatWindow(web_client, session.id, session.name, session.is_folded, channel.unread_counter), }; + if (!session.is_folded) { + chat_manager.mark_channel_as_seen(channel); + } chat_session.window.on("close_chat_session", null, function () { close_chat(chat_session); chat_manager.close_chat_session(chat_session.id); @@ -36,6 +40,9 @@ function open_chat (session) { chat_session.window.on("post_message", null, function (message) { chat_manager.post_message(message); }); + chat_session.window.on("messages_read", null, function () { + chat_manager.mark_channel_as_seen(channel); + }); chat_sessions.push(chat_session); chat_session.window.appendTo($('body')) @@ -74,6 +81,9 @@ function reposition_windows () { function update_sessions (message, scrollBottom) { _.each(chat_sessions, function (session) { if (_.contains(message.channel_ids, session.id)) { + if (!session.window.folded) { + chat_manager.mark_channel_as_seen(chat_manager.get_channel(session.id)); + } chat_manager.get_messages({channel_id: session.id}).then(function (messages) { session.window.render(messages); if (scrollBottom) { @@ -114,6 +124,14 @@ core.bus.on('web_client_ready', null, function () { }); }); + chat_manager.bus.on('update_channel_unread_counter', null, function (channel) { + _.each(chat_sessions, function (session) { + if (channel.id === session.id) { + session.window.update_unread(channel.unread_counter); + } + }); + }); + chat_manager.is_ready.then(function() { _.each(chat_manager.get_channels(), function (channel) { if (channel.is_detached) {