From 80d26e26bba6cb3140e47eb4bb1933a5dfdbe144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Tue, 21 Feb 2017 10:39:29 +0100 Subject: [PATCH] [IMP] mail: notify author of internal notes when replied through gateway Currently if an user mentions someone in an internal note he is not notified of any reply as replies create internal notes. This commit ensure the author of the first note is notified of the reply even if it is still an internal note. Tests have been updated accordingly. --- addons/mail/models/mail_thread.py | 20 +++++++++++++++----- addons/mail/tests/test_mail_features.py | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index d08158750dd..8f3e752bccc 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1180,7 +1180,7 @@ class MailThread(models.AbstractModel): def message_route_process(self, message, message_dict, routes): self = self.with_context(attachments_mime_plainxml=True) # import XML attachments as text # postpone setting message_dict.partner_ids after message_post, to avoid double notifications - partner_ids = message_dict.pop('partner_ids', []) + original_partner_ids = message_dict.pop('partner_ids', []) thread_id = False for model, thread_id, custom_values, user_id, alias in routes or (): if model: @@ -1206,13 +1206,23 @@ class MailThread(models.AbstractModel): Model = self.env['mail.thread'] if not hasattr(Model, 'message_post'): Model = self.env['mail.thread'].with_context(thread_model=model) - internal = message_dict.pop('internal', False) - new_msg = Model.browse(thread_id).message_post(subtype=internal and 'mail.mt_note' or 'mail.mt_comment', **message_dict) - if partner_ids: + # replies to internal message are considered as notes, but parent message + # author is added in recipients to ensure he is notified of a private answer + partner_ids = [] + if message_dict.pop('internal', False): + subtype = 'mail.mt_note' + if message_dict.get('parent_id'): + parent_message = self.env['mail.message'].sudo().browse(message_dict['parent_id']) + partner_ids = [(4, parent_message.author_id.id)] + else: + subtype = 'mail.mt_comment' + new_msg = Model.browse(thread_id).message_post(subtype=subtype, partner_ids=partner_ids, **message_dict) + + if original_partner_ids: # postponed after message_post, because this is an external message and we don't want to create # duplicate emails due to notifications - new_msg.write({'partner_ids': partner_ids}) + new_msg.write({'partner_ids': original_partner_ids}) return thread_id @api.model diff --git a/addons/mail/tests/test_mail_features.py b/addons/mail/tests/test_mail_features.py index 942196ff6d3..3141644846d 100644 --- a/addons/mail/tests/test_mail_features.py +++ b/addons/mail/tests/test_mail_features.py @@ -202,7 +202,7 @@ class TestMessagePost(TestMail): reply = self.group_pigs.message_ids - msg self.assertTrue(reply) self.assertEqual(reply.subtype_id, self.env.ref('mail.mt_note')) - self.assertEqual(reply.needaction_partner_ids, self.env['res.partner']) + self.assertEqual(reply.needaction_partner_ids, self.user_employee.partner_id) @mute_logger('odoo.addons.mail.models.mail_mail') def test_message_compose(self):