From 17e6cd1e14d9be24fcabdc8da3ccfde7831132e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 27 Jan 2023 08:05:02 +0000 Subject: [PATCH] [IMP] mail: add a notification parameter to allow notifying the author Currently notification process called notably by message_{post,notify} does not notify message's author by default. Rationale is that as he typed the message he is already aware of it. However in some cases we want to skip this step, notably when messages are generated in the name of the author and when we want him to be notified if he should. This is currently controllable through a context key 'mail_notify_author'. In this commit we add an explicit parameter propagated through the notify process, allowing to remove some context key usage and instead use real parameters. Note that all context keys usage cannot be removed as it is not always possible to propagate this parameter. Followup of odoo#99482 Task-2710804 (Mail: Clean MailThread API) Part-of: odoo/odoo#107356 --- .../data_recycle/models/data_recycle_model.py | 3 +- addons/mail/models/mail_thread.py | 9 +++- addons/mail_group/models/mail_group.py | 3 +- addons/purchase/models/purchase.py | 4 +- addons/sale/models/sale_order.py | 4 +- addons/test_mail/tests/test_message_post.py | 43 +++++++++++++++++++ 6 files changed, 59 insertions(+), 7 deletions(-) diff --git a/addons/data_recycle/models/data_recycle_model.py b/addons/data_recycle/models/data_recycle_model.py index bfcc1630999..6c91da62f27 100644 --- a/addons/data_recycle/models/data_recycle_model.py +++ b/addons/data_recycle/models/data_recycle_model.py @@ -175,7 +175,7 @@ class DataRecycleModel(models.Model): partner_ids = self.notify_user_ids.partner_id.ids if records_count else [] if partner_ids: menu_id = self.env.ref('data_recycle.menu_data_cleaning_root').id - self.env['mail.thread'].with_context(mail_notify_author=True).message_notify( + self.env['mail.thread'].message_notify( body=self.env['ir.qweb']._render( 'data_recycle.notification', { @@ -186,6 +186,7 @@ class DataRecycleModel(models.Model): } ), model=self._name, + notify_author=True, partner_ids=partner_ids, res_id=self.id, subject=_('Data to Recycle'), diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 74a4ae4a879..4ea545361f7 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2699,6 +2699,7 @@ class MailThread(models.AbstractModel): 'force_send', 'mail_auto_delete', 'model_description', + 'notify_author', 'resend_existing', 'scheduled_date', 'send_after_commit', @@ -3187,6 +3188,10 @@ class MailThread(models.AbstractModel): methods. See those methods for more details about supported parameters. Specific kwargs used in this method: + * ``notify_author``: allows to notify the author, which is False by + default as we don't want people to receive their own content. It is + used notably when impersonating partners or having automated + notifications send by current user, targeting current user; * ``skip_existing``: check existing notifications and skip them in order to avoid having several notifications / partner as it would make constraints crash. This is disabled by default to optimize speed; @@ -3213,9 +3218,11 @@ class MailThread(models.AbstractModel): if not res: return recipients_data + # notify author of its own messages, False by default + notify_author = kwargs.get('notify_author') or self.env.context.get('mail_notify_author') author_id = msg_vals.get('author_id') or message.author_id.id for pid, pdata in res.items(): - if pid and pid == author_id and not self.env.context.get('mail_notify_author'): # do not notify the author of its own messages + if pid and not notify_author and pid == author_id: continue if pdata['active'] is False: continue diff --git a/addons/mail_group/models/mail_group.py b/addons/mail_group/models/mail_group.py index 5f3bbde3901..d6bd7bd6345 100644 --- a/addons/mail_group/models/mail_group.py +++ b/addons/mail_group/models/mail_group.py @@ -495,7 +495,7 @@ class MailGroup(models.Model): for group in groups: moderators_to_notify = group.moderator_ids - MailThread = self.env['mail.thread'].with_context(mail_notify_author=True) + MailThread = self.env['mail.thread'] for moderator in moderators_to_notify: body = self.env['ir.qweb']._render('mail_group.mail_group_notify_moderation', { 'moderator': moderator, @@ -508,6 +508,7 @@ class MailGroup(models.Model): body=body, email_from=email_from, model='mail.group', + notify_author=True, res_id=group.id, ) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index 62703c13c68..602c50fa92a 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -334,8 +334,8 @@ class PurchaseOrder(models.Model): if self.env.context.get('mark_rfq_as_sent'): self.filtered(lambda o: o.state == 'draft').write({'state': 'sent'}) po_ctx = {'mail_post_autofollow': self.env.context.get('mail_post_autofollow', True)} - if self.env.context.get('mark_rfq_as_sent'): - po_ctx['mail_notify_author'] = self.env.user.partner_id.id in (kwargs.get('partner_ids') or []) + if self.env.context.get('mark_rfq_as_sent') and 'notify_author' not in kwargs: + kwargs['notify_author'] = self.env.user.partner_id.id in (kwargs.get('partner_ids') or []) return super(PurchaseOrder, self.with_context(**po_ctx)).message_post(**kwargs) def _notify_get_recipients_groups(self, msg_vals=None): diff --git a/addons/sale/models/sale_order.py b/addons/sale/models/sale_order.py index d8e2b3ca313..9587540c24e 100644 --- a/addons/sale/models/sale_order.py +++ b/addons/sale/models/sale_order.py @@ -1232,8 +1232,8 @@ class SaleOrder(models.Model): if self.env.context.get('mark_so_as_sent'): self.filtered(lambda o: o.state == 'draft').with_context(tracking_disable=True).write({'state': 'sent'}) so_ctx = {'mail_post_autofollow': self.env.context.get('mail_post_autofollow', True)} - if self.env.context.get('mark_so_as_sent'): - so_ctx['mail_notify_author'] = self.env.user.partner_id.id in (kwargs.get('partner_ids') or []) + if self.env.context.get('mark_so_as_sent') and 'mail_notify_author' not in kwargs: + kwargs['notify_author'] = self.env.user.partner_id.id in (kwargs.get('partner_ids') or []) return super(SaleOrder, self.with_context(**so_ctx)).message_post(**kwargs) def _notify_get_recipients_groups(self, msg_vals=None): diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index ec75ffb11fa..10d7fd9905f 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -313,6 +313,49 @@ class TestMessageNotify(TestMessagePostCommon): partner_mail_body = partner_mails[0].get('body') self.assertNotIn('/mail/view?model=', partner_mail_body, 'The email sent to customer should not contain an access link') + @users('employee') + @mute_logger('odoo.addons.mail.models.mail_mail') + def test_notify_author(self): + """ Author is not added in notified people by default, unless asked to + using the 'notify_author' parameter or context key. """ + test_record = self.env['mail.test.simple'].browse(self.test_record.ids) + + with self.mock_mail_gateway(): + new_notification = test_record.message_notify( + body='

You have received a notification

', + partner_ids=(self.partner_1 + self.partner_employee).ids, + subject='This should be a subject', + ) + + self.assertEqual(new_notification.notified_partner_ids, self.partner_1) + + with self.mock_mail_gateway(): + new_notification = test_record.message_notify( + body='

You have received a notification

', + notify_author=True, + partner_ids=(self.partner_1 + self.partner_employee).ids, + subject='This should be a subject', + ) + + self.assertEqual( + new_notification.notified_partner_ids, + self.partner_1 + self.partner_employee, + 'Notify: notify_author parameter skips the author restriction' + ) + + with self.mock_mail_gateway(): + new_notification = test_record.with_context(mail_notify_author=True).message_notify( + body='

You have received a notification

', + partner_ids=(self.partner_1 + self.partner_employee).ids, + subject='This should be a subject', + ) + + self.assertEqual( + new_notification.notified_partner_ids, + self.partner_1 + self.partner_employee, + 'Notify: mail_notify_author context key skips the author restriction' + ) + @users('employee') def test_notify_batch(self): """ Test notify in batch. Currently not supported. """