From 089e46ae5920101b01c7680b8dc4ea9dec15a9bf Mon Sep 17 00:00:00 2001 From: Rahul Prajapati Date: Fri, 6 Oct 2023 17:44:35 +0000 Subject: [PATCH] [FIX] mail: channel unpin/delete issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit: Channel was not getting unpinned after being deleted After this commit: - Channel gets unpinned for all the channel members after being deleted - If a user leaves channel/group then all of the messages of that channel/group are removed from the user's history/starred/inbox channel. Task-2814440 closes odoo/odoo#139972 X-original-commit: 4767d6001d15628768388bcac98c04ab185040c8 Signed-off-by: Sébastien Theys (seb) --- addons/mail/models/discuss/discuss_channel.py | 1 + .../src/core/web/thread_service_patch.js | 7 +++-- .../common/discuss_core_common_service.js | 30 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/discuss/discuss_channel.py b/addons/mail/models/discuss/discuss_channel.py index 3703841c2d4..d7925863b1c 100644 --- a/addons/mail/models/discuss/discuss_channel.py +++ b/addons/mail/models/discuss/discuss_channel.py @@ -240,6 +240,7 @@ class Channel(models.Model): all_emp_group = None if all_emp_group and all_emp_group in self: raise UserError(_('You cannot delete those groups, as the Whole Company group is required by other modules.')) + self.env['bus.bus']._sendmany([(channel, 'discuss.channel/delete', {'id': channel.id}) for channel in self]) def write(self, vals): if 'channel_type' in vals: diff --git a/addons/mail/static/src/core/web/thread_service_patch.js b/addons/mail/static/src/core/web/thread_service_patch.js index fdd7327ad47..f6edb33c841 100644 --- a/addons/mail/static/src/core/web/thread_service_patch.js +++ b/addons/mail/static/src/core/web/thread_service_patch.js @@ -141,11 +141,14 @@ patch(ThreadService.prototype, { } thread.suggestedRecipients = recipients; }, - async leaveChannel(channel) { + closeChatWindow(channel) { const chatWindow = this.store.discuss.chatWindows.find((c) => c.thread?.eq(channel)); if (chatWindow) { - await this.chatWindowService.close(chatWindow); + this.chatWindowService.close(chatWindow, { notifyState: false }); } + }, + async leaveChannel(channel) { + this.closeChatWindow(channel); super.leaveChannel(...arguments); }, /** @param {import("models").Thread} thread */ diff --git a/addons/mail/static/src/discuss/core/common/discuss_core_common_service.js b/addons/mail/static/src/discuss/core/common/discuss_core_common_service.js index fb6493cdefb..85294ae4ca6 100644 --- a/addons/mail/static/src/discuss/core/common/discuss_core_common_service.js +++ b/addons/mail/static/src/discuss/core/common/discuss_core_common_service.js @@ -63,6 +63,36 @@ export class DiscussCoreCommon { }); thread.delete(); }); + this.busService.subscribe("discuss.channel/delete", (payload) => { + const thread = this.store.Thread.insert({ + id: payload.id, + model: "discuss.channel", + }); + const filteredStarredMessages = []; + let starredCounter = 0; + for (const msg of this.store.discuss.starred.messages) { + if (!msg.originThread?.eq(thread)) { + filteredStarredMessages.push(msg); + } else { + starredCounter++; + } + } + this.store.discuss.starred.messages = filteredStarredMessages; + this.store.discuss.starred.counter -= starredCounter; + this.store.discuss.inbox.messages = this.store.discuss.inbox.messages.filter( + (msg) => !msg.originThread?.eq(thread) + ); + this.store.discuss.inbox.counter -= thread.message_needaction_counter; + this.store.discuss.history.messages = this.store.discuss.history.messages.filter( + (msg) => !msg.originThread?.eq(thread) + ); + this.threadService.closeChatWindow?.(thread); + if (thread.eq(this.store.discuss.thread)) { + this.threadService.setDiscussThread(this.store.discuss.inbox); + } + thread.messages.splice(0, thread.messages.length); + thread.delete(); + }); this.busService.addEventListener("notification", ({ detail: notifications }) => { // Do not handle new message notification if the channel was just left. This issue // occurs because the "discuss.channel/leave" and the "discuss.channel/new_message"