From 9180843bb94af980e4ce7a40a3fdcd7162510ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 15 Sep 2022 13:32:36 +0000 Subject: [PATCH] [MOV] mail: move "main attachment" update as an after-post hook A method ``_message_set_main_attachment_id`` exists to handle main attachment reference, used notably in discuss and accounting. It is now called inside ``_message_post_after_hook``, so that it is part of the post processing after having posted the message. Its API has been updated so that attachment_ids is now a real list of ids, which is clearer. A wrong override of '_message_post_after_hook' dealing with parameters is cleaned while passing by. Task-2710804 (Mail: Clean MailThread Posting API) Part-of: odoo/odoo#99482 --- addons/mail/models/mail_channel.py | 2 +- addons/mail/models/mail_thread.py | 29 ++++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index d33abcb0a25..ad29c900621 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -617,7 +617,7 @@ class Channel(models.Model): Automatically set the message posted by the current user as seen for themselves. """ self._set_last_seen_message(message) - return super()._message_post_after_hook(message=message, msg_vals=msg_vals) + return super()._message_post_after_hook(message, msg_vals) def _check_can_update_message_content(self, message): """ We don't call super in this override as we want to ignore the diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index e76eb1fe214..aa1a5621414 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1927,9 +1927,6 @@ class MailThread(models.AbstractModel): ) # attachement_ids, body new_message = self._message_create([msg_values]) - # Set main attachment field if necessary - self._message_set_main_attachment_id(msg_values['attachment_ids']) - if msg_values['author_id'] and msg_values['message_type'] != 'notification' and not self._context.get('mail_create_nosubscribe'): if self.env['res.partner'].browse(msg_values['author_id']).active: # we dont want to add odoobot/inactive as a follower self._message_subscribe(partner_ids=[msg_values['author_id']]) @@ -1938,19 +1935,15 @@ class MailThread(models.AbstractModel): self._notify_thread(new_message, msg_values, **notif_kwargs) return new_message - def _message_set_main_attachment_id(self, attachment_ids): # todo move this out of mail.thread - if not self._abstract and attachment_ids and not self.message_main_attachment_id: - all_attachments = self.env['ir.attachment'].browse([attachment_tuple[1] for attachment_tuple in attachment_ids]) - prioritary_attachments = all_attachments.filtered(lambda x: x.mimetype.endswith('pdf')) \ - or all_attachments.filtered(lambda x: x.mimetype.startswith('image')) \ - or all_attachments - self.sudo().with_context(tracking_disable=True).write({'message_main_attachment_id': prioritary_attachments[0].id}) - - def _message_post_after_hook(self, message, msg_vals): + def _message_post_after_hook(self, message, msg_values): """ Hook to add custom behavior after having posted the message. Both message and computed value are given, to try to lessen query count by using already-computed values instead of having to rebrowse things. """ - return + # Set main attachment field if necessary + self._message_set_main_attachment_id([ + attachment_command[1] + for attachment_command in (msg_values['attachment_ids'] or []) + ]) def _message_mail_after_hook(self, mails): """ Hook to add custom behavior after having sent an mass mailing. @@ -1958,6 +1951,16 @@ class MailThread(models.AbstractModel): :param mail.mail mails: mail.mail records about to be sent""" return + def _message_set_main_attachment_id(self, attachment_ids): + if self._name == 'mail.thread': + return + if attachment_ids and not self.message_main_attachment_id: + all_attachments = self.env['ir.attachment'].browse(attachment_ids) + prioritary_attachments = all_attachments.filtered(lambda x: x.mimetype.endswith('pdf')) \ + or all_attachments.filtered(lambda x: x.mimetype.startswith('image')) \ + or all_attachments + self.sudo().with_context(tracking_disable=True).message_main_attachment_id = prioritary_attachments[0].id + def _process_attachments_for_post(self, attachments, attachment_ids, message_values): """ Preprocess attachments for MailTread.message_post() or MailMail.create(). Purpose is to