[FIX] mail, im_livechat: fix perf of thread sorting

Make correspondent a compute to benefit from cache.

This reduces the time on populate medium from around 600ms to 200ms.

closes odoo/odoo#146109

Related: odoo/enterprise#52703
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Sébastien Theys
2023-12-15 19:37:26 +00:00
parent 9372de8789
commit 6d14689280
2 changed files with 11 additions and 3 deletions
@@ -25,8 +25,8 @@ patch(Thread.prototype, {
return super.correspondents.filter((correspondent) => !correspondent.is_bot);
},
get correspondent() {
let correspondent = super.correspondent;
computeCorrespondent() {
let correspondent = super.computeCorrespondent();
if (this.type === "livechat" && !correspondent) {
// For livechat threads, the correspondent is the first
// channel member that is not the operator.
@@ -186,6 +186,11 @@ export class Thread extends Record {
invitedMembers = Record.many("ChannelMember");
chatPartner = Record.one("Persona");
composer = Record.one("Composer", { inverse: "thread", onDelete: (r) => r.delete() });
correspondent2 = Record.one("Persona", {
compute() {
return this.computeCorrespondent();
},
});
counter = 0;
/** @type {string} */
custom_channel_name;
@@ -360,8 +365,11 @@ export class Thread extends Record {
.filter((p) => p.notEq(this._store.self));
}
/** @type {import("models").Persona|undefined} */
get correspondent() {
return this.correspondent2;
}
computeCorrespondent() {
if (this.type === "channel") {
return undefined;
}