From 4882ef0a58da5c01ea363281ae55b2d71ebbe6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 11 Jan 2024 16:45:17 +0100 Subject: [PATCH] [FIX] mail: fix discuss.channel notification recipients computation (backport) Backport of odoo/odoo@24ebd74dc94ec5ee4f2bd7ac3cd6e2fd8aa2a98a Recipients on a channel notification are computed twice: once using an override of '_notify_get_recipients' that fetches information of mentioned recipients; once in override of '_notify_by_web_push' to try to add recipients for push notifications. However this is not the right way to do it. Everything should be computed in '_notify_get_recipients', setting the right 'notification_type' and then let 'notify_by_MEAN' methods deal with their recipient input. In this commit we now correctly compute recipients on a given channel * mentioned partners; * unmuted members on chat channels (push); Task-3695571 Related to Task-3669738 (Mail: Web Push Models Rename and Fixes) Part-of: odoo/odoo#150011 --- addons/mail/models/discuss/discuss_channel.py | 57 ++++++++----------- addons/test_mail_full/tests/test_web_push.py | 2 +- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/addons/mail/models/discuss/discuss_channel.py b/addons/mail/models/discuss/discuss_channel.py index ae12e035888..0a62ef8a33c 100644 --- a/addons/mail/models/discuss/discuss_channel.py +++ b/addons/mail/models/discuss/discuss_channel.py @@ -484,15 +484,11 @@ class Channel(models.Model): # notify only user input (comment or incoming / outgoing emails) if message_type not in ('comment', 'email', 'email_outgoing'): return [] - # notify only mailing lists or if mentioning recipients - if not pids: - return [] - - email_from = tools.email_normalize(msg_vals.get('email_from') or message.email_from) - author_id = msg_vals.get('author_id') or message.author_id.id recipients_data = [] if pids: + email_from = tools.email_normalize(msg_vals.get('email_from') or message.email_from) + author_id = msg_vals.get('author_id') or message.author_id.id self.env['res.partner'].flush_model(['active', 'email', 'partner_share']) self.env['res.users'].flush_model(['notification_type', 'partner_id']) sql_query = """ @@ -525,6 +521,28 @@ class Channel(models.Model): 'ushare': ushare, }) + if self.is_chat or self.channel_type == "group": + already_in_ids = [r['id'] for r in recipients_data] + recipients_data += [ + { + 'active': partner.active, + 'groups': [], + 'id': partner.id, + 'is_follower': False, + 'lang': partner.lang, + 'notif': 'web_push', + 'share': partner.partner_share, + 'type': 'customer', + 'uid': False, + 'ushare': False, + } for partner in self.channel_member_ids.filtered( + lambda member: ( + not member.mute_until_dt and + member.partner_id.id not in already_in_ids + ) + ).partner_id + ] + return recipients_data def _notify_get_recipients_groups(self, message, model_description, msg_vals=None): @@ -1269,33 +1287,6 @@ class Channel(models.Model): self._send_transient_message(self.env.user.partner_id, msg) - def _notify_thread_by_web_push(self, message, recipients_data, msg_vals=False, **kwargs): - """ Specifically handle channel members. """ - chat_channels = self.filtered(lambda channel: channel.channel_type == 'chat') - if chat_channels: - # modify rdata only for calling super. Do not deep copy as we only - # add data into list but we do not modify item content - channel_rdata = recipients_data.copy() - channel_rdata += [ - { - 'active': partner.active, - 'id': partner.id, - 'is_follower': False, - 'groups': [], - 'lang': partner.lang, - 'notif': 'web_push', - 'share': partner.partner_share, - 'type': 'customer', - 'uid': False, - 'ushare': False, - } - for partner in chat_channels.channel_member_ids.filtered(lambda member: not member.mute_until_dt).partner_id - ] - else: - channel_rdata = recipients_data - - return super()._notify_thread_by_web_push(message, channel_rdata, msg_vals=msg_vals, **kwargs) - def _notify_by_web_push_prepare_payload(self, message, msg_vals=False): payload = super()._notify_by_web_push_prepare_payload(message, msg_vals=msg_vals) payload['options']['data']['action'] = 'mail.action_discuss' diff --git a/addons/test_mail_full/tests/test_web_push.py b/addons/test_mail_full/tests/test_web_push.py index 24474c126e4..23fa04916ad 100644 --- a/addons/test_mail_full/tests/test_web_push.py +++ b/addons/test_mail_full/tests/test_web_push.py @@ -16,7 +16,7 @@ from unittest.mock import patch from types import SimpleNamespace -@tagged('post_install', '-at_install') +@tagged('post_install', '-at_install', 'mail_push') class TestWebPushNotification(SMSCommon): @classmethod