From 74cc99f72c63ed3820dc0a6a13d7c8d9fc781417 Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Fri, 6 Oct 2023 11:00:17 +0200 Subject: [PATCH] [FIX] im_livechat: error when loading a page with outdated guest This PR fixes two issues: - Incorrect write on `channel_member_ids` (trying to link guest instead of channel member) - Incorrect condition in `livechatService.leaveSession` that relies on the live chat to be initialized while it can acutally be called before if the current session is outdated. This could result in server errors in the following scenario: - Open a live chat - Log in - Open a live chat from the website app - Log out - Reload the page Part-of: odoo/odoo#137834 --- addons/im_livechat/static/src/embed/common/livechat_service.js | 3 +-- addons/website_livechat/models/website.py | 2 +- 2 files changed, 2 insertions(+), 3 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 528fdd2779f..7c8de6e86fa 100644 --- a/addons/im_livechat/static/src/embed/common/livechat_service.js +++ b/addons/im_livechat/static/src/embed/common/livechat_service.js @@ -91,7 +91,6 @@ export class LivechatService { (this.sessionCookie && visitorUid !== userId) ) { this.leaveSession(); - this.state = SESSION_STATE.NONE; } browser.localStorage.setItem(ODOO_VERSION_KEY, currOdooVersion); } @@ -132,7 +131,7 @@ export class LivechatService { async leaveSession({ notifyServer = true } = {}) { const session = JSON.parse(cookie.get(this.SESSION_COOKIE) ?? "{}"); try { - if (this.state === SESSION_STATE.PERSISTED && notifyServer) { + if (session?.uuid && notifyServer) { this.busService.deleteChannel(session.uuid); await this.rpc("/im_livechat/visitor_leave_session", { uuid: session.uuid }); } diff --git a/addons/website_livechat/models/website.py b/addons/website_livechat/models/website.py index f5b903337ed..282e316a837 100644 --- a/addons/website_livechat/models/website.py +++ b/addons/website_livechat/models/website.py @@ -54,7 +54,7 @@ class Website(models.Model): # linked to another guest in the meantime. We need to # update the channel to link it to the current guest. channel_guest_member = chat_request_channel.channel_member_ids.filtered(lambda m: m.guest_id == current_guest) - chat_request_channel.write({'channel_member_ids': [Command.unlink(channel_guest_member), Command.link(current_guest)]}) + chat_request_channel.write({'channel_member_ids': [Command.unlink(channel_guest_member), Command.create({'guest_id': current_guest.id})]}) if not current_guest: channel_guest._set_auth_cookie() return {