From bb495ef55ee00b2a536569dcb18185bbf33bd140 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Thu, 25 Jan 2024 09:53:09 +0100 Subject: [PATCH] [FIX] im_livechat: fix chatbot restart Before this PR, a race condition could occur when restarting the chatbot: if the rpc returned before the restart message reception on the bus, the chat would not restart. This was due to the message being inserted twice. Since the id is used as an unique `t-key` in the thread template, any duplicate would lead to an error. This PR fixes the issue by checking that the message is not already present before adding it to the thread messages. Steps to reproduce the issue: - Go to the contact us page - Start a chat with the bot and proceed until the end - Put your network in slow 3g mode - Click on the restart button - The chatbot is not restarting as expected closes odoo/odoo#150975 X-original-commit: 6c535bb71edc40aaf3fa75bab702fb9be4a24779 Signed-off-by: Matthieu Stockbauer (tsm) --- .../embed/common/chatbot/chatbot_service.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js b/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js index b28ea00ed7e..b6009e72331 100644 --- a/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js +++ b/addons/im_livechat/static/src/embed/common/chatbot/chatbot_service.js @@ -122,11 +122,19 @@ export class ChatBotService { localStorage.removeItem( `im_livechat.chatbot.state.uuid_${this.livechatService.thread.uuid}` ); - const message = await this.rpc("/chatbot/restart", { - channel_uuid: this.livechatService.thread.uuid, - chatbot_script_id: this.chatbot.scriptId, - }); - this.livechatService.thread?.messages.push({ ...message, body: markup(message.body) }); + const message = this.store.Message.insert( + await this.rpc("/chatbot/restart", { + channel_uuid: this.livechatService.thread.uuid, + chatbot_script_id: this.chatbot.scriptId, + }), + { html: true } + ); + if (!this.livechatService.thread) { + return; + } + if (message.notIn(this.livechatService.thread.messages)) { + this.livechatService.thread.messages.push(message); + } this.currentStep = null; this.start(); }