From 6f37e2cc5d8aa8b0723610216c29772cb832b03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Tue, 27 Nov 2018 14:50:32 +0000 Subject: [PATCH] [FIX] mail: increment messaging menu counter once per unread preview Before this commit, the total amount of unread messages of a channel was considered in the computation of the messaging menu counter. As a result, if the channel contains 100 unread messages, the messaging menu was displaying a counter of more than 100. The intended behaviour is to increment the messaging menu counter once per unread channel, thus the example above should incremented the counter only once. The channel preview itself should still display that there are 100 unread messages. Task-ID 1913080 closes odoo/odoo#29080 --- .../src/js/systray/systray_messaging_menu.js | 4 +- .../systray/systray_messaging_menu_tests.js | 91 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/addons/mail/static/src/js/systray/systray_messaging_menu.js b/addons/mail/static/src/js/systray/systray_messaging_menu.js index d86e9e3913b..df35de47174 100644 --- a/addons/mail/static/src/js/systray/systray_messaging_menu.js +++ b/addons/mail/static/src/js/systray/systray_messaging_menu.js @@ -102,8 +102,8 @@ var MessagingMenu = Widget.extend({ var channelUnreadCounters = _.map(channels, function (channel) { return channel.getUnreadCounter(); }); - var unreadChannelCounter = _.reduce(channelUnreadCounters, function (c1, c2) { - return c1 + c2; + var unreadChannelCounter = _.reduce(channelUnreadCounters, function (acc, c) { + return c > 0 ? acc + 1 : acc; }, 0); var inboxCounter = this.call('mail_service', 'getMailbox', 'inbox').getMailboxCounter(); var mailFailureCounter = this.call('mail_service', 'getMailFailures').length; diff --git a/addons/mail/static/tests/systray/systray_messaging_menu_tests.js b/addons/mail/static/tests/systray/systray_messaging_menu_tests.js index f67703c4679..2f6015a0a7e 100644 --- a/addons/mail/static/tests/systray/systray_messaging_menu_tests.js +++ b/addons/mail/static/tests/systray/systray_messaging_menu_tests.js @@ -690,5 +690,96 @@ QUnit.test("messaging menu widget: sort dated previews before undated previews", messagingMenu.destroy(); }); +QUnit.test('global counter with channel previews', function (assert) { + assert.expect(5); + + var channels = [{ + id: 1, + name: "channel1", + channel_type: "channel", + channel_message_ids: [1, 2], + message_unread_counter: 2, + }, { + id: 2, + name: "channel2", + channel_type: "channel", + channel_message_ids: [3], + message_unread_counter: 1, + }, { + id: 3, + name: "channel3", + channel_type: "channel", + channel_message_ids: [4], + message_unread_counter: 0, + }]; + + var messages = [{ + author_id: [1, "Demo"], + body: "

message 1

", + channel_ids: [1], + id: 1, + model: 'mail.channel', + record_name: 'channel1', + res_id: 1, + }, { + author_id: [1, "Demo"], + body: "

message 2

", + channel_ids: [1], + id: 2, + model: 'mail.channel', + record_name: 'channel1', + res_id: 1, + }, { + author_id: [1, "Demo"], + body: "

message 3

", + channel_ids: [2], + id: 3, + model: 'mail.channel', + record_name: 'channel2', + res_id: 2, + }, { + author_id: [1, "Demo"], + body: "

message 4

", + channel_ids: [3], + id: 4, + model: 'mail.channel', + record_name: 'channel3', + res_id: 3, + }]; + + this.data['mail.channel'].records = channels; + this.data.initMessaging.channel_slots.channel_channel = channels; + this.data['mail.message'].records = messages; + + var messagingMenu = new MessagingMenu(); + testUtils.addMockEnvironment(messagingMenu, { + services: this.services, + data: this.data, + session: { + partner_id: 3, + }, + }); + messagingMenu.appendTo($('#qunit-fixture')); + assert.strictEqual(messagingMenu.$('.o_notification_counter').text(), '2', + "should display a counter of 2 on the messaging menu icon"); + + messagingMenu.$('.dropdown-toggle').click(); + + assert.strictEqual(messagingMenu.$('.o_mail_preview').length, 3, + "should display 3 previews"); + var $previewChannel1 = messagingMenu.$('.o_mail_preview[data-preview-id=1]'); + var $previewChannel2 = messagingMenu.$('.o_mail_preview[data-preview-id=2]'); + var $previewChannel3 = messagingMenu.$('.o_mail_preview[data-preview-id=3]'); + + assert.strictEqual($previewChannel1.data('unread-counter'), 2, + "preview of channel 1 should have unread counter of 2"); + assert.strictEqual($previewChannel2.data('unread-counter'), 1, + "preview of channel 2 should have unread counter of 1"); + assert.strictEqual($previewChannel3.data('unread-counter'), 0, + "preview of channel 3 should have unread counter of 0"); + + messagingMenu.destroy(); +}); + }); });