[FIX] mail: improve performance of messaging menu counter

Avoid iterating over all channels every time a new channel is added.

Part of task-2702450

closes odoo/odoo#81157

X-original-commit: 6c64781f60599571ef92b011eca7d299e76bf5eb
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Sébastien Theys
2021-12-09 16:14:51 +00:00
committed by Alexandre Kühn
parent 7dc796f8d6
commit 9dca2aa499
2 changed files with 26 additions and 6 deletions
@@ -1,7 +1,7 @@
/** @odoo-module **/
import { registerModel } from '@mail/model/model_core';
import { attr } from '@mail/model/model_field';
import { attr, one2many } from '@mail/model/model_field';
registerModel({
name: 'mail.messaging_menu',
@@ -40,11 +40,7 @@ registerModel({
return 0;
}
const inboxCounter = this.messaging.inbox ? this.messaging.inbox.counter : 0;
const unreadChannelsCounter = this.messaging.models['mail.thread'].all(channel => (
channel.model === 'mail.channel' &&
channel.isPinned &&
channel.localMessageUnreadCounter > 0
)).length;
const unreadChannelsCounter = this.pinnedAndUnreadChannels.length;
const notificationGroupsCounter = this.messaging.models['mail.notification_group'].all().reduce(
(total, group) => total + group.notifications.length,
0
@@ -81,5 +77,12 @@ registerModel({
isOpen: attr({
default: false,
}),
/**
* States all the pinned channels that have unread messages.
*/
pinnedAndUnreadChannels: one2many('mail.thread', {
inverse: 'messagingMenuAsPinnedAndUnreadChannel',
readonly: true,
}),
},
});
@@ -1546,6 +1546,18 @@ registerModel({
}
return clear();
},
/**
* @private
* @returns {FieldCommand}
*/
_computeMessagingMenuAsPinnedAndUnreadChannel() {
if (!this.messaging.messagingMenu) {
return clear();
}
return (this.model === 'mail.channel' && this.isPinned && this.localMessageUnreadCounter > 0)
? replace(this.messaging.messagingMenu)
: clear();
},
/**
* @private
* @returns {mail.message[]}
@@ -2177,6 +2189,11 @@ registerModel({
inverse: 'ringingThreads',
readonly: true,
}),
messagingMenuAsPinnedAndUnreadChannel: many2one('mail.messaging_menu', {
compute: '_computeMessagingMenuAsPinnedAndUnreadChannel',
inverse: 'pinnedAndUnreadChannels',
readonly: true,
}),
model: attr({
readonly: true,
required: true,