[FIX] mail: channel unpin/delete issues

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) <seb@odoo.com>
This commit is contained in:
Rahul Prajapati
2023-10-31 13:26:40 +00:00
parent ad8feceedf
commit 089e46ae59
3 changed files with 36 additions and 2 deletions
@@ -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:
@@ -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 */
@@ -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"