From aa87b65dc87a427f0244e6b82c98a59b5e17559d Mon Sep 17 00:00:00 2001 From: zel-odoo Date: Tue, 27 Feb 2024 14:41:01 +0100 Subject: [PATCH] [FIX] mail: thread search works for all channels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. channel mention should work for channels, chats, and groups, even if there is a group restriction for the channel. 2. channel mention in livechat is disabled because the visitor can't get access to the channel. 3. remove the fetch and search for channel mentions in livechat so that there is no extra rpc call for unuseful data. 4. add a test for the channel mention in thread. closes odoo/odoo#155380 Signed-off-by: Alexandre Kühn (aku) --- .../src/core/web/suggestion_service_patch.js | 14 ++++++++++ .../static/tests/suggestions_tests.js | 23 +++++++++++++++ .../src/core/common/suggestion_service.js | 22 ++------------- .../mail/static/tests/thread/thread_tests.js | 28 +++++++++++++++++++ 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/addons/im_livechat/static/src/core/web/suggestion_service_patch.js b/addons/im_livechat/static/src/core/web/suggestion_service_patch.js index f312cf2bbd7..9cfd68bdf9a 100644 --- a/addons/im_livechat/static/src/core/web/suggestion_service_patch.js +++ b/addons/im_livechat/static/src/core/web/suggestion_service_patch.js @@ -11,8 +11,15 @@ patch(SuggestionService.prototype, { ? [...super.getSupportedDelimiters(...arguments), [":"]] : super.getSupportedDelimiters(...arguments); }, + async fetchSuggestions({ delimiter, term }, { thread } = {}) { + if (thread.type === "livechat" && delimiter === "#") { + return; + } + return super.fetchSuggestions(...arguments); + }, /** * Returns suggestions that match the given search term from specified type. + * Searching on channels is disabled in livechat since the visitor don't have access to channels. * * @param {Object} [param0={}] * @param {String} [param0.delimiter] can be one one of the following: ["@", ":", "#", "/"] @@ -23,6 +30,13 @@ patch(SuggestionService.prototype, { * @returns {[mainSuggestion[], extraSuggestion[]]} */ searchSuggestions({ delimiter, term }, { thread } = {}, sort = false) { + if (thread.type === "livechat" && delimiter === "#") { + return { + type: undefined, + mainSuggestions: [], + extraSuggestions: [], + }; + } if (delimiter === ":") { return this.searchCannedResponseSuggestions(cleanTerm(term), sort); } diff --git a/addons/im_livechat/static/tests/suggestions_tests.js b/addons/im_livechat/static/tests/suggestions_tests.js index b95418f5ed1..70c16972e82 100644 --- a/addons/im_livechat/static/tests/suggestions_tests.js +++ b/addons/im_livechat/static/tests/suggestions_tests.js @@ -32,3 +32,26 @@ QUnit.test("Suggestions are shown after delimiter was used in text (:)", async ( await insertText(".o-mail-Composer-input", " :"); await contains(".o-mail-Composer-suggestion strong", { text: "hello" }); }); + +QUnit.test("Cannot mention other channels in a livechat", async () => { + const pyEnv = await startServer(); + const [channelId] = pyEnv["discuss.channel"].create([ + { + anonymous_name: "Visitor", + channel_type: "livechat", + channel_member_ids: [ + Command.create({ partner_id: pyEnv.currentPartnerId }), + Command.create({ partner_id: pyEnv.publicPartnerId }), + ], + }, + { + channel_type: "channel", + group_public_id: false, + name: "Link and Zelda", + }, + ]); + const { openDiscuss } = await start(); + await openDiscuss(channelId); + await insertText(".o-mail-Composer-input", "#"); + await contains(".o-mail-Composer-suggestion", { count: 0 }); +}); diff --git a/addons/mail/static/src/core/common/suggestion_service.js b/addons/mail/static/src/core/common/suggestion_service.js index 14d50508e11..5b75e4952ab 100644 --- a/addons/mail/static/src/core/common/suggestion_service.js +++ b/addons/mail/static/src/core/common/suggestion_service.js @@ -85,7 +85,7 @@ export class SuggestionService { return this.searchPartnerSuggestions(cleanedSearchTerm, thread, sort); } case "#": - return this.searchChannelSuggestions(cleanedSearchTerm, thread, sort); + return this.searchChannelSuggestions(cleanedSearchTerm, sort); } return { type: undefined, @@ -168,24 +168,8 @@ export class SuggestionService { }); } - searchChannelSuggestions(cleanedSearchTerm, thread, sort) { - let threads; - if ( - thread && - (thread.type === "group" || - thread.type === "chat" || - (thread.type === "channel" && thread.authorizedGroupFullName)) - ) { - // Only return the current channel when in the context of a - // group restricted channel or group or chat. Indeed, the message with the mention - // would appear in the target channel, so this prevents from - // inadvertently leaking the private message into the mentioned - // channel. - threads = [thread]; - } else { - threads = Object.values(this.store.Thread.records); - } - const suggestionList = threads.filter( + searchChannelSuggestions(cleanedSearchTerm, sort) { + const suggestionList = Object.values(this.store.Thread.records).filter( (thread) => thread.type === "channel" && thread.displayName && diff --git a/addons/mail/static/tests/thread/thread_tests.js b/addons/mail/static/tests/thread/thread_tests.js index 0aa8b5a82ce..11eb6db060e 100644 --- a/addons/mail/static/tests/thread/thread_tests.js +++ b/addons/mail/static/tests/thread/thread_tests.js @@ -213,6 +213,34 @@ QUnit.test("thread is still scrolling after scrolling up then to bottom", async await contains(".o-mail-Thread", { scroll: "bottom" }); }); +QUnit.test("Can mention other channels in a group-restricted channel", async () => { + const pyEnv = await startServer(); + const groupId = pyEnv["res.groups"].create({ + name: "Mario Group", + }); + const [channelId1] = pyEnv["discuss.channel"].create([ + { + channel_type: "channel", + group_public_id: groupId, + name: "Marios", + }, + { + channel_type: "channel", + group_public_id: false, + name: "Link and Zelda", + }, + ]); + const { openDiscuss } = await start(); + openDiscuss(channelId1); + await insertText(".o-mail-Composer-input", "#"); + await contains(".o-mail-Composer-suggestion", { text: "#Marios" }); + await contains(".o-mail-Composer-suggestion", { text: "#Link and Zelda" }); + await click(".o-mail-Composer-suggestion", { text: "#Link and Zelda" }); + await contains(".o-mail-Composer-input", { value: "#Link and Zelda " }); + await click(".o-mail-Composer-send:enabled"); + await contains(".o-mail-Message-body .o_channel_redirect", { text: "#Link and Zelda" }); +}); + QUnit.test("mention a channel with space in the name", async () => { const pyEnv = await startServer(); const channelId = pyEnv["discuss.channel"].create({ name: "General good boy" });