From cdae935731555d4b2a46bc9da23cbf9af3048378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 11 Aug 2023 14:27:59 +0200 Subject: [PATCH] [IMP] mail: allow to link partners to logs In this commit we allow to give 'partner_ids' when using '_message_log'. This allows to link a message to recipients but those are not notified in any way. Main purpose is to link a message which has a side effect to the main record customer. First usage will be in WhatsApp implementation to link a sent WhatsApp to a recipient, easing finding it back when receiving replies. Task-2377154 (Add WhatsApp Support) Part-of: odoo/odoo#134377 --- addons/mail/models/mail_thread.py | 10 +++++-- addons/test_mail/tests/test_message_post.py | 33 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 2e424380c37..145065894bd 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2570,6 +2570,7 @@ class MailThread(models.AbstractModel): body='', subject=False, author_id=None, email_from=None, message_type='notification', + partner_ids=False, attachment_ids=False, tracking_value_ids=False): """ Shortcut allowing to post note on a document. See ``_message_log_batch`` for more details. """ @@ -2579,12 +2580,14 @@ class MailThread(models.AbstractModel): {self.id: body}, subject=subject, author_id=author_id, email_from=email_from, message_type=message_type, + partner_ids=partner_ids, attachment_ids=attachment_ids, tracking_value_ids=tracking_value_ids ) def _message_log_batch(self, bodies, subject=False, author_id=None, email_from=None, message_type='notification', + partner_ids=False, attachment_ids=False, tracking_value_ids=False): """ Shortcut allowing to post notes on a batch of documents. It does not perform any notification and pre-computes some values to have a short code @@ -2594,7 +2597,9 @@ class MailThread(models.AbstractModel): access rights are already granted to avoid privilege escalation. :param bodies: dict {record_id: body} - + :param list partner_ids: optional partners, not used in any notification + mechanism. This is mainly used to link a log to a specific customer + like SMS or WhatsApp log; :return: created messages (as sudo) """ # protect against side-effect prone usage @@ -2620,6 +2625,7 @@ class MailThread(models.AbstractModel): # recipients 'email_add_signature': False, # False as no notification -> no need to compute signature 'message_id': tools.generate_tracking_message_id('message-notify'), # why? this is all but a notify + 'partner_ids': partner_ids, 'reply_to': self.env['mail.thread']._notify_get_reply_to(default=email_from)[False], } @@ -2713,7 +2719,7 @@ class MailThread(models.AbstractModel): # Avoid warnings about non-existing fields for x in ('from', 'to', 'cc', 'canned_response_ids'): create_values.pop(x, None) - create_values['partner_ids'] = [Command.link(pid) for pid in create_values.get('partner_ids', [])] + create_values['partner_ids'] = [Command.link(pid) for pid in (create_values.get('partner_ids') or [])] create_values_list.append(create_values) # remove context, notably for default keys, as this thread method is not diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index 2d0ead3f7f3..81661445e69 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -546,6 +546,7 @@ class TestMessageLog(TestMessagePostCommon): 'message_type': 'notification', 'model': test_record._name, 'notified_partner_ids': self.env['res.partner'], + 'partner_ids': self.env['res.partner'], 'reply_to': formataddr((self.company_admin.name, '%s@%s' % (self.alias_catchall, self.alias_domain))), 'res_id': test_record.id, 'subtype_id': self.env.ref('mail.mt_note'), @@ -573,6 +574,38 @@ class TestMessageLog(TestMessagePostCommon): 'message_type': 'notification', 'model': test_record._name, 'notified_partner_ids': self.env['res.partner'], + 'partner_ids': self.env['res.partner'], + 'reply_to': formataddr((self.company_admin.name, '%s@%s' % (self.alias_catchall, self.alias_domain))), + 'res_id': test_record.id, + 'subtype_id': self.env.ref('mail.mt_note'), + } + ) + + @users('employee') + def test_message_log_batch_with_partners(self): + """ Partners can be given to log, but this should not generate any + notification. """ + test_records = self.test_records.with_env(self.env) + test_records.message_subscribe(self.partner_employee_2.ids) + + new_notes = test_records._message_log_batch( + bodies={ + test_record.id: Markup('

Test _message_log_batch

') + for test_record in test_records + }, + partner_ids=self.test_partners[:5].ids, + ) + for test_record, new_note in zip(test_records, new_notes): + self.assertMessageFields( + new_note, + {'author_id': self.partner_employee, + 'body': '

Test _message_log_batch

', + 'email_from': formataddr((self.partner_employee.name, self.partner_employee.email_normalized)), + 'is_internal': True, + 'message_type': 'notification', + 'model': test_record._name, + 'notified_partner_ids': self.env['res.partner'], + 'partner_ids': self.test_partners[:5], 'reply_to': formataddr((self.company_admin.name, '%s@%s' % (self.alias_catchall, self.alias_domain))), 'res_id': test_record.id, 'subtype_id': self.env.ref('mail.mt_note'),