[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
This commit is contained in:
Alexandre Kühn
2018-11-27 15:06:23 +00:00
parent 77a8d92196
commit 6f37e2cc5d
2 changed files with 93 additions and 2 deletions
@@ -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;
@@ -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: "<p>message 1</p>",
channel_ids: [1],
id: 1,
model: 'mail.channel',
record_name: 'channel1',
res_id: 1,
}, {
author_id: [1, "Demo"],
body: "<p>message 2</p>",
channel_ids: [1],
id: 2,
model: 'mail.channel',
record_name: 'channel1',
res_id: 1,
}, {
author_id: [1, "Demo"],
body: "<p>message 3</p>",
channel_ids: [2],
id: 3,
model: 'mail.channel',
record_name: 'channel2',
res_id: 2,
}, {
author_id: [1, "Demo"],
body: "<p>message 4</p>",
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();
});
});
});