[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
This commit is contained in:
Thibault Delavallée
2023-01-17 20:58:35 +01:00
parent ea3e4d2dc0
commit 9180843bb9
2 changed files with 17 additions and 14 deletions
+1 -1
View File
@@ -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
+16 -13
View File
@@ -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