From 3a661beb3ab3b15e0ae47d46060eedd9815f5c8a Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Wed, 21 Jun 2023 10:03:54 +0000 Subject: [PATCH] [IMP] im_livechat: improve rating views MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit improves the tree/kanban rating view for livechat channels: - Open the channel in discuss if the member is a member of it. Open the form view otherwise. part of task-3332872 closes odoo/odoo#125902 Signed-off-by: Alexandre Kühn (aku) --- addons/im_livechat/models/rating_rating.py | 8 +++ .../discuss_channel_list_view_controller.js | 12 +--- .../im_livechat/views/rating_rating_views.xml | 36 +++++++++++- .../src/core/common/messaging_service.js | 11 ---- .../src/core/web/discuss_client_action.js | 58 ++++++++++++++++++- 5 files changed, 101 insertions(+), 24 deletions(-) diff --git a/addons/im_livechat/models/rating_rating.py b/addons/im_livechat/models/rating_rating.py index ebed47ef329..057d04fd1cb 100644 --- a/addons/im_livechat/models/rating_rating.py +++ b/addons/im_livechat/models/rating_rating.py @@ -22,6 +22,14 @@ class Rating(models.Model): def action_open_rated_object(self): action = super(Rating, self).action_open_rated_object() if self.res_model == 'discuss.channel': + if self.env[self.res_model].browse(self.res_id).is_member: + ctx = self.env.context.copy() + ctx.update({'active_id': self.res_id}) + return { + 'type': 'ir.actions.client', + 'tag': 'mail.action_discuss', + 'context': ctx, + } view_id = self.env.ref('im_livechat.discuss_channel_view_form').id action['views'] = [[view_id, 'form']] return action diff --git a/addons/im_livechat/static/src/views/discuss_channel_list/discuss_channel_list_view_controller.js b/addons/im_livechat/static/src/views/discuss_channel_list/discuss_channel_list_view_controller.js index 260410c8f21..783c2a4dbd1 100644 --- a/addons/im_livechat/static/src/views/discuss_channel_list/discuss_channel_list_view_controller.js +++ b/addons/im_livechat/static/src/views/discuss_channel_list/discuss_channel_list_view_controller.js @@ -1,30 +1,20 @@ /* @odoo-module */ -import { createLocalId } from "@mail/utils/common/misc"; import { _t } from "@web/core/l10n/translation"; -import { useService } from "@web/core/utils/hooks"; import { ListController } from "@web/views/list/list_controller"; export class DiscussChannelListController extends ListController { setup() { super.setup(...arguments); - this.threadService = useService("mail.thread"); - this.store = useService("mail.store"); } async openRecord(record) { if (!record.data.is_member) { return super.openRecord(record); } - const channel = - this.store.threads[createLocalId(record.resModel, record.resId)] ?? - (await this.threadService.fetchChannel(record.resId)); - if (!channel.is_pinned) { - this.threadService.pin(channel); - } - this.threadService.setDiscussThread(channel); this.actionService.doAction("mail.action_discuss", { name: _t("Discuss"), + additionalContext: { active_id: record.resId }, }); } } diff --git a/addons/im_livechat/views/rating_rating_views.xml b/addons/im_livechat/views/rating_rating_views.xml index c7eeb37190e..b7d9cae1ca4 100644 --- a/addons/im_livechat/views/rating_rating_views.xml +++ b/addons/im_livechat/views/rating_rating_views.xml @@ -35,11 +35,45 @@ {'search_default_rating_last_7_days': 1} + + im_livechat.rating.rating.view.kanban + rating.rating + + + + object + action_open_rated_object + + + pointer-events: none; + + + + + + im_livechat.rating.rating.view.tree + rating.rating + + + + object + action_open_rated_object + + + + kanban - + + + + + + tree + + diff --git a/addons/mail/static/src/core/common/messaging_service.js b/addons/mail/static/src/core/common/messaging_service.js index b1957d23f5f..64384e609ae 100644 --- a/addons/mail/static/src/core/common/messaging_service.js +++ b/addons/mail/static/src/core/common/messaging_service.js @@ -669,17 +669,6 @@ export const messagingService = { "mail.out_of_focus", ], start(env, services) { - // compute initial discuss thread if not on public page - if (!services["mail.store"].inPublicPage) { - const activeId = services.router.current.hash.active_id ?? "mail.box_inbox"; - let [model, id] = - typeof activeId === "number" ? ["discuss.channel", activeId] : activeId.split("_"); - if (model === "mail.channel") { - // legacy format (sent in old emails, shared links, ...) - model = "discuss.channel"; - } - services["mail.store"].discuss.threadLocalId = createLocalId(model, id); - } const messaging = new Messaging(env, services); messaging.initialize(); messaging.isReady.then(() => { diff --git a/addons/mail/static/src/core/web/discuss_client_action.js b/addons/mail/static/src/core/web/discuss_client_action.js index bfb831cfdc2..e016883c19d 100644 --- a/addons/mail/static/src/core/web/discuss_client_action.js +++ b/addons/mail/static/src/core/web/discuss_client_action.js @@ -1,15 +1,71 @@ /* @odoo-module */ import { Discuss } from "@mail/core/common/discuss"; +import { useMessaging, useStore } from "@mail/core/common/messaging_hook"; -import { Component } from "@odoo/owl"; +import { Component, onWillStart, onWillUpdateProps } from "@odoo/owl"; import { registry } from "@web/core/registry"; +import { useService } from "@web/core/utils/hooks"; +import { createLocalId } from "@mail/utils/common/misc"; +/** + * @typedef {Object} Props + * @property {Object} action + * @property {Object} action.context + * @property {number} [action.context.active_id] + * @property {Object} [action.params] + * @property {number} [action.params.active_id] + * @extends {Component} + */ export class DiscussClientAction extends Component { static components = { Discuss }; static props = ["*"]; static template = "mail.DiscussClientAction"; + + setup() { + this.store = useStore(); + this.messaging = useMessaging(); + this.threadService = useService("mail.thread"); + onWillStart(() => this.restoreDiscussThread(this.props)); + onWillUpdateProps((nextProps) => this.restoreDiscussThread(nextProps)); + } + + /** + * Restore the discuss thread according to the active_id in the action + * if necessary: thread is different than the one already displayed and + * we are not in a public page. If the thread is not yet known, fetch it. + * + * @param {Props} props + */ + async restoreDiscussThread(props) { + await this.messaging.isReady; + if (this.store.inPublicPage) { + return; + } + const rawActiveId = + props.action.context.active_id ?? + props.action.params?.active_id ?? + this.store.discuss.threadLocalId?.replace(",", "_") ?? + "mail.box_inbox"; + const activeId = + typeof rawActiveId === "number" ? `discuss.channel_${rawActiveId}` : rawActiveId; + let [model, id] = activeId.split("_"); + if (model === "mail.channel") { + // legacy format (sent in old emails, shared links, ...) + model = "discuss.channel"; + } + const activeThreadLocalId = createLocalId(model, id); + if (activeThreadLocalId !== this.store.discuss.threadLocalId) { + const thread = + this.store.threads[createLocalId(model, id)] ?? + (await this.threadService.fetchChannel(parseInt(id))); + if (!thread.is_pinned) { + await this.threadService.pin(thread); + } + this.threadService.setDiscussThread(thread); + } + } } registry.category("actions").add("mail.action_discuss", DiscussClientAction);