[FIX] mail: fix discuss.channel notification recipients computation (backport)

Backport of odoo/odoo@24ebd74dc9

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
This commit is contained in:
Thibault Delavallée
2024-01-22 15:56:54 +00:00
parent e3aa094802
commit 4882ef0a58
2 changed files with 25 additions and 34 deletions
+24 -33
View File
@@ -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'
+1 -1
View File
@@ -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