From 406b8a4896ed04cacb14d48370fe6cc2650e05aa Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Wed, 12 Jul 2023 08:56:07 +0000 Subject: [PATCH] [FIX] im_livechat: keep same livechat name after inviting members MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, inviting a channel member to a livechat channel would change its name. This is not what we want as the thread name should always be the one of the visitor (e.g. "Visitor 20", task-3332872). Steps to reproduce: - Open two tabs, one with admin log, one on the support page - Click on the livechat button and send a message from the support page - Invite a member with the admin tab - The name of the thread changed. closes odoo/odoo#128238 Signed-off-by: Sébastien Theys (seb) --- .../static/src/core/thread_model_patch.js | 16 +++++++++ .../static/src/core/thread_service_patch.js | 7 ++++ .../static/tests/thread_model_patch_tests.js | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 addons/im_livechat/static/tests/thread_model_patch_tests.js diff --git a/addons/im_livechat/static/src/core/thread_model_patch.js b/addons/im_livechat/static/src/core/thread_model_patch.js index c56d6fe4396..48fa1ee2ebc 100644 --- a/addons/im_livechat/static/src/core/thread_model_patch.js +++ b/addons/im_livechat/static/src/core/thread_model_patch.js @@ -34,6 +34,22 @@ patch(Thread.prototype, "im_livechat", { return this._super().filter((correspondent) => !correspondent.is_bot); }, + get correspondent() { + let correspondent = this._super(); + if (this.type === "livechat" && !correspondent) { + // For livechat threads, the correspondent is the first + // channel member that is not the operator. + const orderedChannelMembers = [...this.channelMembers].sort((a, b) => a.id - b.id); + const isFirstMemberOperator = + orderedChannelMembers[0]?.persona.type === "partner" && + orderedChannelMembers[0]?.persona.id === this.operator.id; + correspondent = isFirstMemberOperator + ? orderedChannelMembers[1]?.persona + : orderedChannelMembers[0]?.persona; + } + return correspondent; + }, + get displayName() { if (this.type !== "livechat" || !this.correspondent) { return this._super(); diff --git a/addons/im_livechat/static/src/core/thread_service_patch.js b/addons/im_livechat/static/src/core/thread_service_patch.js index 692db13d900..d944f2a5623 100644 --- a/addons/im_livechat/static/src/core/thread_service_patch.js +++ b/addons/im_livechat/static/src/core/thread_service_patch.js @@ -14,6 +14,13 @@ patch(ThreadService.prototype, "im_livechat", { if (data?.channel) { assignDefined(thread, data.channel, ["anonymous_name"]); } + if (data?.operator_pid) { + thread.operator = this.personaService.insert({ + type: "partner", + id: data.operator_pid[0], + displayName: data.operator_pid[1], + }); + } if (isUnknown) { this.store.discuss.livechat.threads.push(thread.localId); this.sortChannels(); diff --git a/addons/im_livechat/static/tests/thread_model_patch_tests.js b/addons/im_livechat/static/tests/thread_model_patch_tests.js new file mode 100644 index 00000000000..25a26469e47 --- /dev/null +++ b/addons/im_livechat/static/tests/thread_model_patch_tests.js @@ -0,0 +1,35 @@ +/* @odoo-module */ + +import { startServer } from "@bus/../tests/helpers/mock_python_environment"; + +import { Command } from "@mail/../tests/helpers/command"; +import { click, start } from "@mail/../tests/helpers/test_utils"; + +QUnit.module("Thread model"); + +QUnit.test("Thread name unchanged when inviting new users", async (assert) => { + const pyEnv = await startServer(); + const userId = pyEnv["res.users"].create({ name: "James" }); + pyEnv["res.partner"].create({ + name: "James", + user_ids: [userId], + }); + const channelId = pyEnv["discuss.channel"].create({ + anonymous_name: "Visitor #20", + channel_member_ids: [ + Command.create({ partner_id: pyEnv.currentPartnerId }), + Command.create({ partner_id: pyEnv.publicPartnerId }), + ], + channel_type: "livechat", + livechat_operator_id: pyEnv.currentPartnerId, + }); + const { openDiscuss } = await start(); + await openDiscuss(channelId); + assert.containsOnce($, ".o-mail-Discuss-threadName[title='Visitor #20']"); + await click("button[title='Add Users']"); + await click(".o-discuss-ChannelInvitation-selectable:contains(James) input"); + await click("button:contains(Invite)"); + await click("button[title='Show Member List']"); + assert.containsOnce($, ".o-discuss-ChannelMember:contains(James)"); + assert.containsOnce($, ".o-mail-Discuss-threadName[title='Visitor #20']"); +});