From d8b294ad9672f926cff35b5a449052cc01b8d1fc Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Wed, 7 Feb 2024 11:05:19 +0100 Subject: [PATCH] [FIX] mail, im_livechat: do not start bus for transient threads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since [1], the websocket worker is started when a transient thread is added to the mail store. This occurs because this PR introduced a call to the `addChannel` method of the bus service when the current user was not member of the thread. Since transient threads are not yet created, they have a partial state that does not necessarily include channel members hence the impression that the current user is not member of the channel. This PR prevent starting the bus service for transient threads. [1]: https://github.com/odoo/odoo/pull/146800 closes odoo/odoo#153000 Signed-off-by: Sébastien Theys (seb) --- .../src/embed/common/livechat_service.js | 9 +++--- .../src/embed/common/thread_model_patch.js | 6 +++- .../static/src/core/common/store_service.js | 2 +- .../tests/tours/lazy_frontend_bus_tour.js | 31 +++++++++++++++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/addons/im_livechat/static/src/embed/common/livechat_service.js b/addons/im_livechat/static/src/embed/common/livechat_service.js index 6488a3bb355..e86be32b4ea 100644 --- a/addons/im_livechat/static/src/embed/common/livechat_service.js +++ b/addons/im_livechat/static/src/embed/common/livechat_service.js @@ -35,7 +35,7 @@ export const ODOO_VERSION_KEY = `${location.origin.replace( )}_im_livechat.odoo_version`; export class LivechatService { - TEMPORARY_ID = "livechat_temporary_thread"; + static TEMPORARY_ID = "livechat_temporary_thread"; SESSION_COOKIE = "im_livechat_session"; OPERATOR_COOKIE = "im_livechat_previous_operator_pid"; GUEST_TOKEN_STORAGE_KEY = "im_livechat_guest_token"; @@ -160,7 +160,7 @@ export class LivechatService { this.persistThreadPromise = null; } const chatWindow = this.store.discuss.chatWindows.find( - (c) => c.thread.id === this.TEMPORARY_ID + (c) => c.thread.id === LivechatService.TEMPORARY_ID ); if (chatWindow) { chatWindow.thread?.delete(); @@ -213,7 +213,7 @@ export class LivechatService { this.updateSession(threadData); const thread = this.store.Thread.insert({ ...threadData, - id: threadData.id ?? this.TEMPORARY_ID, + id: threadData.id ?? LivechatService.TEMPORARY_ID, isLoaded: !threadData.id || isNewlyCreated, model: "discuss.channel", type: "livechat", @@ -264,7 +264,8 @@ export class LivechatService { get thread() { return Object.values(this.store.Thread.records).find( ({ id, type }) => - type === "livechat" && id === (this.sessionCookie?.id ?? this.TEMPORARY_ID) + type === "livechat" && + id === (this.sessionCookie?.id ?? LivechatService.TEMPORARY_ID) ); } diff --git a/addons/im_livechat/static/src/embed/common/thread_model_patch.js b/addons/im_livechat/static/src/embed/common/thread_model_patch.js index aaf9a0771d7..f858d68d57e 100644 --- a/addons/im_livechat/static/src/embed/common/thread_model_patch.js +++ b/addons/im_livechat/static/src/embed/common/thread_model_patch.js @@ -1,6 +1,6 @@ /* @odoo-module */ -import { SESSION_STATE } from "@im_livechat/embed/common/livechat_service"; +import { LivechatService, SESSION_STATE } from "@im_livechat/embed/common/livechat_service"; import { Record } from "@mail/core/common/record"; import { Thread } from "@mail/core/common/thread_model"; @@ -78,4 +78,8 @@ patch(Thread.prototype, { } return url(`/im_livechat/operator/${this.operator.id}/avatar`); }, + + get isTransient() { + return super.isTransient || this.id === LivechatService.TEMPORARY_ID; + }, }); diff --git a/addons/mail/static/src/core/common/store_service.js b/addons/mail/static/src/core/common/store_service.js index c2b7bfd8f18..bc311f2550e 100644 --- a/addons/mail/static/src/core/common/store_service.js +++ b/addons/mail/static/src/core/common/store_service.js @@ -207,7 +207,7 @@ export class Store extends BaseStore { const ids = Object.keys(this.Thread.records).sort(); // Ensure channels processed in same order. for (const id of ids) { const thread = this.Thread.records[id]; - if (thread.model === "discuss.channel") { + if (thread.model === "discuss.channel" && !thread.isTransient) { channelIds.push(id); if (!thread.hasSelfAsMember) { this.env.services["bus_service"].addChannel(`discuss.channel_${thread.id}`); diff --git a/addons/website_livechat/static/tests/tours/lazy_frontend_bus_tour.js b/addons/website_livechat/static/tests/tours/lazy_frontend_bus_tour.js index 7fe78bd4f82..15b448fed32 100644 --- a/addons/website_livechat/static/tests/tours/lazy_frontend_bus_tour.js +++ b/addons/website_livechat/static/tests/tours/lazy_frontend_bus_tour.js @@ -5,13 +5,40 @@ import { registry } from "@web/core/registry"; registry.category("web_tour.tours").add("website_livechat.lazy_frontend_bus", { test: true, url: "/", + shadow_dom: ".o-livechat-root", steps: () => [ { - trigger: "body", + trigger: ".o-livechat-LivechatButton", async run() { await odoo.__WOWL_DEBUG__.root.env.services["mail.messaging"].isReady; if (odoo.__WOWL_DEBUG__.root.env.services.bus_service.isActive) { - throw new Error("Bus service should not be started eagerly"); + throw new Error("Bus service should not start when loading the page"); + } + }, + }, + { + trigger: ".o-livechat-LivechatButton", + }, + { + trigger: ".o-mail-Composer-input", + run: "text Hello, I need help!", + }, + { + trigger: ".o-mail-Composer-input", + run() { + if (odoo.__WOWL_DEBUG__.root.env.services.bus_service.isActive) { + throw new Error("Bus service should not start for temporary live chat"); + } + this.$anchor[0].dispatchEvent( + new KeyboardEvent("keydown", { key: "Enter", which: 13, bubbles: true }) + ); + }, + }, + { + trigger: ".o-mail-Message:contains(Hello, I need help!)", + run() { + if (!odoo.__WOWL_DEBUG__.root.env.services.bus_service.isActive) { + throw new Error("Bus service should start after first live chat message"); } }, },