[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
This commit is contained in:
@@ -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'),
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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='<p>You have received a notification</p>',
|
||||
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='<p>You have received a notification</p>',
|
||||
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='<p>You have received a notification</p>',
|
||||
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. """
|
||||
|
||||
Reference in New Issue
Block a user