From 300ef2dddadc1a5e2ebf7a32c4d2bd2d7fce165a Mon Sep 17 00:00:00 2001 From: Carlos Lopez Date: Fri, 24 Jan 2025 12:38:24 -0500 Subject: [PATCH] =?UTF-8?q?[IMP]=20mail=5Fforward:=20Add=20option=20to=20f?= =?UTF-8?q?orward=20a=20message=20to=20another=20thread=20This=20option=20?= =?UTF-8?q?posts=20a=20new=20message=20in=20the=20other=20thread,=20includ?= =?UTF-8?q?ing=20the=20attachments,=20but=20does=20not=20add=20them=20as?= =?UTF-8?q?=20followers=E2=80=94only=20notifying=20the=20current=20followe?= =?UTF-8?q?rs=20of=20the=20other=20thread.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mail_forward/README.rst | 35 +++- mail_forward/__manifest__.py | 5 +- mail_forward/i18n/es.po | 54 +++++- mail_forward/i18n/mail_forward.pot | 42 ++++ mail_forward/models/__init__.py | 1 + mail_forward/models/ir_model.py | 9 + mail_forward/models/mail_message.py | 2 - mail_forward/models/mail_thread.py | 5 +- mail_forward/readme/CONFIGURE.md | 5 + mail_forward/readme/DESCRIPTION.md | 6 +- mail_forward/readme/USAGE.md | 7 +- mail_forward/static/description/index.html | 55 ++++-- mail_forward/tests/test_mail_forward.py | 60 +++++- mail_forward/views/ir_model_views.xml | 19 ++ mail_forward/wizards/mail_compose_message.py | 179 ++++++++++++++++-- .../wizards/mail_compose_message_view.xml | 19 +- 16 files changed, 441 insertions(+), 62 deletions(-) create mode 100644 mail_forward/models/ir_model.py create mode 100644 mail_forward/readme/CONFIGURE.md create mode 100644 mail_forward/views/ir_model_views.xml diff --git a/mail_forward/README.rst b/mail_forward/README.rst index 383983f52..720a51958 100644 --- a/mail_forward/README.rst +++ b/mail_forward/README.rst @@ -29,14 +29,29 @@ Mail Forward Message |badge1| |badge2| |badge3| |badge4| |badge5| This module allows users to forward messages from the chatter of any -document to other users, adding them as followers of the document -without notifying the current followers. +document to: + +- Other users in the same thread, adding them as followers of the + document without notifying the current followers. +- Another thread, but not adding them as followers—only notifying the + current followers of the other thread. **Table of contents** .. contents:: :local: +Configuration +============= + +To enable a model to forward messages to another thread: + +- With debug mode activated, go to + ``Settings -> Technical -> Database Structure -> Models``. +- Search for the model you wish to enable. +- Mark the option ``Enable Forward To`` and save. Transient models or + those without mail thread won't have this check visible. + Usage ===== @@ -47,9 +62,11 @@ To use this module, follow these steps: notes). - A Forward icon will appear next to the message. - Click the button to display a wizard with the message. -- Select the users to forward the message to. -- Click the 'Send Mail' button to send the message to the selected - users. +- Select the forward type (current thread or another thread). +- Select the users to forward the message to, or select the other thread + according to the previous step. +- Click the 'Send Mail' button to send the message to the selected users + or thread. Bug Tracker =========== @@ -89,6 +106,14 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. +.. |maintainer-carlos-lopez-tecnativa| image:: https://github.com/carlos-lopez-tecnativa.png?size=40px + :target: https://github.com/carlos-lopez-tecnativa + :alt: carlos-lopez-tecnativa + +Current `maintainer `__: + +|maintainer-carlos-lopez-tecnativa| + This module is part of the `OCA/social `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_forward/__manifest__.py b/mail_forward/__manifest__.py index 19534c2d1..32dc18d4a 100644 --- a/mail_forward/__manifest__.py +++ b/mail_forward/__manifest__.py @@ -7,9 +7,7 @@ "author": "Tecnativa, Odoo Community Association (OCA)", "website": "https://github.com/OCA/social", "depends": ["mail", "contacts"], - "data": [ - "wizards/mail_compose_message_view.xml", - ], + "data": ["wizards/mail_compose_message_view.xml", "views/ir_model_views.xml"], "assets": { "web.assets_backend": [ "mail_forward/static/src/**/*.esm.js", @@ -22,4 +20,5 @@ "installable": True, "auto_install": False, "license": "AGPL-3", + "maintainers": ["carlos-lopez-tecnativa"], } diff --git a/mail_forward/i18n/es.po b/mail_forward/i18n/es.po index 79f1c7433..62f8ebcf7 100644 --- a/mail_forward/i18n/es.po +++ b/mail_forward/i18n/es.po @@ -4,10 +4,10 @@ # msgid "" msgstr "" -"Project-Id-Version: Odoo Server 15.0\n" +"Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-10-02 12:25+0000\n" -"PO-Revision-Date: 2024-10-02 07:27-0500\n" +"POT-Creation-Date: 2025-01-27 17:26+0000\n" +"PO-Revision-Date: 2025-01-27 12:28-0500\n" "Last-Translator: \n" "Language-Team: \n" "Language: es\n" @@ -15,7 +15,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -"X-Generator: Poedit 3.0.1\n" +"X-Generator: Poedit 3.5\n" #. module: mail_forward #. odoo-python @@ -24,6 +24,16 @@ msgstr "" msgid "---------- Forwarded message ---------" msgstr "---------- Mensaje reenviado ---------" +#. module: mail_forward +#: model:ir.model.fields.selection,name:mail_forward.selection__mail_compose_message__forward_type__another_thread +msgid "Another thread" +msgstr "Otro hilo" + +#. module: mail_forward +#: model:ir.model.fields.selection,name:mail_forward.selection__mail_compose_message__forward_type__current_thread +msgid "Current thread" +msgstr "Hilo actual" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 @@ -39,7 +49,22 @@ msgstr "Hilo de correos electrónicos" #. module: mail_forward #: model:ir.model,name:mail_forward.model_mail_compose_message msgid "Email composition wizard" -msgstr "Asistente de composición de correos electrónicos" +msgstr "Asistente de redacción de correo electrónico" + +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_ir_model__enable_forward_to +msgid "Enable Forward To" +msgstr "Habilitar reenvío a" + +#. module: mail_forward +#: model:ir.model.fields,help:mail_forward.field_ir_model__enable_forward_to +msgid "Enable forwarding messages to records of this model." +msgstr "Habilita el reenvío de mensajes a registros de este modelo." + +#. module: mail_forward +#: model_terms:ir.ui.view,arch_db:mail_forward.mail_compose_message_forward_form +msgid "Followers to notify" +msgstr "Seguidores a notificar" #. module: mail_forward #. odoo-javascript @@ -47,7 +72,7 @@ msgstr "Asistente de composición de correos electrónicos" #: code:addons/mail_forward/static/src/core/common/message_actions.esm.js:0 #, python-format msgid "Forward" -msgstr "" +msgstr "Reenviar" #. module: mail_forward #. odoo-javascript @@ -79,6 +104,11 @@ msgstr "" msgid "Forward Message Right" msgstr "" +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_mail_compose_message__forward_type +msgid "Forward Type" +msgstr "Tipo de reenvío" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 @@ -91,13 +121,18 @@ msgstr "De" #: code:addons/mail_forward/wizards/mail_compose_message.py:0 #, python-format msgid "Fwd:" -msgstr "" +msgstr "RV:" #. module: mail_forward #: model:ir.model,name:mail_forward.model_mail_message msgid "Message" msgstr "Mensaje" +#. module: mail_forward +#: model:ir.model,name:mail_forward.model_ir_model +msgid "Models" +msgstr "Modelos" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/wizards/mail_compose_message.py:0 @@ -112,6 +147,11 @@ msgstr "" msgid "Subject" msgstr "Asunto" +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_mail_compose_message__forward_thread +msgid "Thread to forward" +msgstr "Hilo a reenviar" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 diff --git a/mail_forward/i18n/mail_forward.pot b/mail_forward/i18n/mail_forward.pot index 17da3d1ee..09edaff31 100644 --- a/mail_forward/i18n/mail_forward.pot +++ b/mail_forward/i18n/mail_forward.pot @@ -6,6 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-01-27 17:26+0000\n" +"PO-Revision-Date: 2025-01-27 17:26+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -20,6 +22,16 @@ msgstr "" msgid "---------- Forwarded message ---------" msgstr "" +#. module: mail_forward +#: model:ir.model.fields.selection,name:mail_forward.selection__mail_compose_message__forward_type__another_thread +msgid "Another thread" +msgstr "" + +#. module: mail_forward +#: model:ir.model.fields.selection,name:mail_forward.selection__mail_compose_message__forward_type__current_thread +msgid "Current thread" +msgstr "" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 @@ -37,6 +49,21 @@ msgstr "" msgid "Email composition wizard" msgstr "" +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_ir_model__enable_forward_to +msgid "Enable Forward To" +msgstr "" + +#. module: mail_forward +#: model:ir.model.fields,help:mail_forward.field_ir_model__enable_forward_to +msgid "Enable forwarding messages to records of this model." +msgstr "" + +#. module: mail_forward +#: model_terms:ir.ui.view,arch_db:mail_forward.mail_compose_message_forward_form +msgid "Followers to notify" +msgstr "" + #. module: mail_forward #. odoo-javascript #: code:addons/mail_forward/static/src/components/forward_message/forward_message.xml:0 @@ -76,6 +103,11 @@ msgstr "" msgid "Forward Message Right" msgstr "" +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_mail_compose_message__forward_type +msgid "Forward Type" +msgstr "" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 @@ -95,6 +127,11 @@ msgstr "" msgid "Message" msgstr "" +#. module: mail_forward +#: model:ir.model,name:mail_forward.model_ir_model +msgid "Models" +msgstr "" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/wizards/mail_compose_message.py:0 @@ -109,6 +146,11 @@ msgstr "" msgid "Subject" msgstr "" +#. module: mail_forward +#: model:ir.model.fields,field_description:mail_forward.field_mail_compose_message__forward_thread +msgid "Thread to forward" +msgstr "" + #. module: mail_forward #. odoo-python #: code:addons/mail_forward/models/mail_message.py:0 diff --git a/mail_forward/models/__init__.py b/mail_forward/models/__init__.py index eccc2881b..c5692d315 100644 --- a/mail_forward/models/__init__.py +++ b/mail_forward/models/__init__.py @@ -1,2 +1,3 @@ +from . import ir_model from . import mail_message from . import mail_thread diff --git a/mail_forward/models/ir_model.py b/mail_forward/models/ir_model.py new file mode 100644 index 000000000..8ded28e17 --- /dev/null +++ b/mail_forward/models/ir_model.py @@ -0,0 +1,9 @@ +from odoo import fields, models + + +class IrModel(models.Model): + _inherit = "ir.model" + + enable_forward_to = fields.Boolean( + help="Enable forwarding messages to records of this model." + ) diff --git a/mail_forward/models/mail_message.py b/mail_forward/models/mail_message.py index 57a68cfbe..a6c18e3db 100644 --- a/mail_forward/models/mail_message.py +++ b/mail_forward/models/mail_message.py @@ -19,8 +19,6 @@ class MailMessage(models.Model): "default_model": self.model, "default_res_ids": [self.res_id], "default_composition_mode": "comment", - "default_body": self._build_message_body_for_forward(), - "default_attachment_ids": self.attachment_ids.ids, "default_is_log": False, "default_notify": True, "force_email": True, diff --git a/mail_forward/models/mail_thread.py b/mail_forward/models/mail_thread.py index babb29db5..919c8747d 100644 --- a/mail_forward/models/mail_thread.py +++ b/mail_forward/models/mail_thread.py @@ -9,7 +9,10 @@ class MailThread(models.AbstractModel): def _notify_get_recipients(self, message, msg_vals, **kwargs): recipients_data = super()._notify_get_recipients(message, msg_vals, **kwargs) # only notify to explicit partners, remove others(followers). - if self.env.context.get("message_forwarded_id"): + if ( + self.env.context.get("message_forwarded_id") + and self.env.context.get("forward_type", "") == "current_thread" + ): current_partners_ids = message.partner_ids.ids new_recipeints = [] for recipeint in recipients_data: diff --git a/mail_forward/readme/CONFIGURE.md b/mail_forward/readme/CONFIGURE.md new file mode 100644 index 000000000..68629226b --- /dev/null +++ b/mail_forward/readme/CONFIGURE.md @@ -0,0 +1,5 @@ +To enable a model to forward messages to another thread: + +- With debug mode activated, go to `Settings -> Technical -> Database Structure -> Models`. +- Search for the model you wish to enable. +- Mark the option `Enable Forward To` and save. Transient models or those without mail thread won't have this check visible. \ No newline at end of file diff --git a/mail_forward/readme/DESCRIPTION.md b/mail_forward/readme/DESCRIPTION.md index fe56c5546..872600afc 100644 --- a/mail_forward/readme/DESCRIPTION.md +++ b/mail_forward/readme/DESCRIPTION.md @@ -1,3 +1,3 @@ -This module allows users to forward messages from the chatter of any -document to other users, adding them as followers of the document -without notifying the current followers. +This module allows users to forward messages from the chatter of any document to: +- Other users in the same thread, adding them as followers of the document without notifying the current followers. +- Another thread, but not adding them as followers—only notifying the current followers of the other thread. \ No newline at end of file diff --git a/mail_forward/readme/USAGE.md b/mail_forward/readme/USAGE.md index 0befa7862..18fc63e58 100644 --- a/mail_forward/readme/USAGE.md +++ b/mail_forward/readme/USAGE.md @@ -5,6 +5,7 @@ To use this module, follow these steps: notes). - A Forward icon will appear next to the message. - Click the button to display a wizard with the message. -- Select the users to forward the message to. -- Click the 'Send Mail' button to send the message to the selected - users. +- Select the forward type (current thread or another thread). +- Select the users to forward the message to, or select the other thread according to the previous step. +- Click the 'Send Mail' button to send the message to the selected users or thread. + \ No newline at end of file diff --git a/mail_forward/static/description/index.html b/mail_forward/static/description/index.html index 3d1338e7d..9c5e1ba35 100644 --- a/mail_forward/static/description/index.html +++ b/mail_forward/static/description/index.html @@ -371,23 +371,40 @@ ul.auto-toc { !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Beta License: AGPL-3 OCA/social Translate me on Weblate Try me on Runboat

This module allows users to forward messages from the chatter of any -document to other users, adding them as followers of the document -without notifying the current followers.

+document to:

+
    +
  • Other users in the same thread, adding them as followers of the +document without notifying the current followers.
  • +
  • Another thread, but not adding them as followers—only notifying the +current followers of the other thread.
  • +

Table of contents

+
+

Configuration

+

To enable a model to forward messages to another thread:

+
    +
  • With debug mode activated, go to +Settings -> Technical -> Database Structure -> Models.
  • +
  • Search for the model you wish to enable.
  • +
  • Mark the option Enable Forward To and save. Transient models or +those without mail thread won’t have this check visible.
  • +
+
-

Usage

+

Usage

To use this module, follow these steps:

  • Navigate to the chatter of any document.
  • @@ -395,13 +412,15 @@ without notifying the current followers.

    notes).
  • A Forward icon will appear next to the message.
  • Click the button to display a wizard with the message.
  • -
  • Select the users to forward the message to.
  • -
  • Click the ‘Send Mail’ button to send the message to the selected -users.
  • +
  • Select the forward type (current thread or another thread).
  • +
  • Select the users to forward the message to, or select the other thread +according to the previous step.
  • +
  • Click the ‘Send Mail’ button to send the message to the selected users +or thread.
-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -409,15 +428,15 @@ If you spotted it first, help us to smash it by providing a detailed and welcome

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Tecnativa
-

Contributors

+

Contributors

  • Tecnativa:
    • Carlos López
    • @@ -426,7 +445,7 @@ If you spotted it first, help us to smash it by providing a detailed and welcome
diff --git a/mail_forward/tests/test_mail_forward.py b/mail_forward/tests/test_mail_forward.py index 88f311bf4..412c6c36d 100644 --- a/mail_forward/tests/test_mail_forward.py +++ b/mail_forward/tests/test_mail_forward.py @@ -22,6 +22,7 @@ class TestMailForward(TestMailComposer, HttpCase): cls.partner_forward = cls.env["res.partner"].create( {"name": "Forward", "email": "forward@example.com"} ) + cls.env["ir.model"]._get("res.partner").enable_forward_to = True def test_01_mail_forward(self): """ @@ -39,7 +40,7 @@ class TestMailForward(TestMailComposer, HttpCase): composer = composer_form.save() with self.mock_mail_gateway(): composer._action_send_mail() - # Verify recipients of mail.message + # Verify the followers of mail.message message = self.test_record.message_ids[0] self.assertEqual(len(message.partner_ids), 2) self.assertIn(self.partner_follower1, message.partner_ids) @@ -62,13 +63,68 @@ class TestMailForward(TestMailComposer, HttpCase): with RecordCapturer(self.env["mail.message"], message_domain) as capture: with self.mock_mail_gateway(): composer._action_send_mail() - # Verify recipients of mail.message + # Verify the followers of mail.message forward_message = capture.records self.assertEqual(len(forward_message.partner_ids), 1) self.assertNotIn(self.partner_follower1, forward_message.partner_ids) self.assertIn(self.partner_forward, forward_message.partner_ids) self.assertIn("---------- Forwarded message ---------", forward_message.body) + def test_mail_forward_another_thread(self): + """ + Check that the email is forwarded to another thread. + and the email is sent to the followers of the another thread. + """ + ctx = { + "default_model": self.test_record._name, + "default_res_ids": [self.test_record.id], + } + composer_form = Form(self.env["mail.compose.message"].with_context(**ctx)) + composer_form.body = "

Hello

" + composer_form.subject = "Test Forward" + composer_form.partner_ids.add(self.partner_follower1) + composer = composer_form.save() + with self.mock_mail_gateway(): + composer._action_send_mail() + # Verify the followers of mail.message + message = self.test_record.message_ids[0] + self.assertEqual(len(message.partner_ids), 1) + self.assertIn(self.partner_follower1, message.partner_ids) + self.assertNotIn(self.partner_follower2, message.partner_ids) + self.assertNotIn(self.partner_forward, message.partner_ids) + self.assertNotIn("---------- Forwarded message ---------", message.body) + # Forward the email to another record(self.partner_forward) + action_forward = message.action_wizard_forward() + Message = self.env["mail.compose.message"].with_context( + **action_forward["context"] + ) + composer_form = Form(Message, view=action_forward["views"][0][0]) + composer_form.partner_ids.add(self.partner_follower2) + composer_form.forward_type = "another_thread" + composer_form.forward_thread = ( + f"{self.partner_forward._name},{self.partner_forward.id}" + ) + composer = composer_form.save() + message_domain = [ + ("model", "=", self.partner_forward._name), + ("res_id", "=", self.partner_forward.id), + ] + with RecordCapturer(self.env["mail.message"], message_domain) as capture: + with self.mock_mail_gateway(): + composer._action_send_mail() + # Verify the followers of mail.message + forward_message = capture.records + self.assertEqual(forward_message.subject, "Fwd: Test Forward") + self.assertEqual(len(forward_message.partner_ids), 1) + self.assertNotIn(self.partner_follower1, forward_message.partner_ids) + # the partner partner_follower2 is added to the message + # but is not added as a follower automatically. + self.assertIn(self.partner_follower2, forward_message.partner_ids) + self.assertNotIn( + self.partner_follower2, self.partner_forward.message_partner_ids + ) + self.assertIn("---------- Forwarded message ---------", forward_message.body) + def test_02_mail_forward_tour(self): self.test_record.message_post( body="Hello World", message_type="comment", subtype_xmlid="mail.mt_comment" diff --git a/mail_forward/views/ir_model_views.xml b/mail_forward/views/ir_model_views.xml new file mode 100644 index 000000000..d45e61f33 --- /dev/null +++ b/mail_forward/views/ir_model_views.xml @@ -0,0 +1,19 @@ + + + + + view.ir.model.form + ir.model + + + + + + + + + diff --git a/mail_forward/wizards/mail_compose_message.py b/mail_forward/wizards/mail_compose_message.py index cb36069e2..8b50e86c7 100644 --- a/mail_forward/wizards/mail_compose_message.py +++ b/mail_forward/wizards/mail_compose_message.py @@ -1,11 +1,168 @@ # Copyright 2024 Tecnativa - Carlos Lopez # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import Command, _, api, models +from odoo import _, api, fields, models class MailComposeMessage(models.TransientModel): _inherit = "mail.compose.message" + forward_type = fields.Selection( + [ + ("current_thread", "Current thread"), + ("another_thread", "Another thread"), + ], + default="current_thread", + ) + forward_thread = fields.Reference( + selection="_selection_forward_thread", string="Thread to forward" + ) + + @api.model + def _selection_forward_thread(self): + # Get all models available to be selected by the user. + # Only consider models that support posted messages and are not transient. + models = ( + self.env["ir.model"] + .sudo() + .search( + [ + ("transient", "=", False), + ("is_mail_thread", "=", True), + ("enable_forward_to", "=", True), + ], + order="name asc", + ) + ) + selection_values = [] + for model in models: + if ( + model.model in self.env and self.env[model.model]._auto + ): # No Abstract models or reports + selection_values.append((model.model, model.name)) + return selection_values + + @api.depends( + "composition_mode", + "model", + "res_domain", + "res_ids", + "template_id", + "forward_type", + ) + @api.depends_context("message_forwarded_id") + def _compute_attachment_ids(self): + # Save the attachments before calling super() to avoid losing them + # because when template_id is not set, + # attachment_ids is set to False in the super() call. + old_attachments = {composer.id: composer.attachment_ids for composer in self} + res = super()._compute_attachment_ids() + if self.env.context.get("message_forwarded_id"): + # Add the attachments from the original message. + message_forwarded = self.env["mail.message"].browse( + self.env.context["message_forwarded_id"] + ) + for composer in self: + composer.attachment_ids |= old_attachments[composer.id] + for attachment in message_forwarded.attachment_ids: + composer.attachment_ids |= attachment + return res + + @api.depends( + "composition_mode", + "model", + "res_domain", + "res_ids", + "template_id", + "forward_type", + "forward_thread", + ) + @api.depends_context("message_forwarded_id") + def _compute_body(self): + res = super()._compute_body() + if self.env.context.get("message_forwarded_id"): + # Set the body by default, taking it from the original message. + message_forwarded = self.env["mail.message"].browse( + self.env.context["message_forwarded_id"] + ) + for composer in self.filtered(lambda c: not c.body): + composer.body = message_forwarded._build_message_body_for_forward() + return res + + @api.depends( + "composition_mode", + "model", + "parent_id", + "record_name", + "res_domain", + "res_ids", + "template_id", + "forward_type", + "forward_thread", + ) + @api.depends_context("message_forwarded_id") + def _compute_subject(self): + res = super()._compute_subject() + if self.env.context.get("message_forwarded_id"): + # Set the subject by default, + # because when change the model and res_ids, + # the subject is taken from the new record. + message_forwarded = self.env["mail.message"].browse( + self.env.context["message_forwarded_id"] + ) + for composer in self: + composer.subject = f"{_('Fwd:')} {message_forwarded.subject}" + return res + + @api.depends("composition_mode", "parent_id", "forward_type", "forward_thread") + @api.depends_context("message_forwarded_id") + def _compute_model(self): + res = super()._compute_model() + if self.env.context.get("message_forwarded_id"): + # Set the model to the record to be forwarded + # if the composer is set to forward a record + # it sends the message to the record to be forwarded + for composer in self.filtered( + lambda c: c.forward_type == "another_thread" and c.forward_thread + ): + composer.model = composer.forward_thread._name + return res + + @api.depends("composition_mode", "parent_id", "forward_type", "forward_thread") + @api.depends_context("message_forwarded_id") + def _compute_res_ids(self): + res = super()._compute_res_ids() + if self.env.context.get("message_forwarded_id"): + # Set res_ids to the record to be forwarded + # if the composer is set to forward a record + # it sends the message to the record to be forwarded + for composer in self.filtered( + lambda c: c.forward_type == "another_thread" and c.forward_thread + ): + composer.res_ids = composer.forward_thread.ids + return res + + @api.depends( + "composition_mode", + "model", + "parent_id", + "res_domain", + "res_ids", + "template_id", + "forward_type", + ) + @api.depends_context("message_forwarded_id") + def _compute_partner_ids(self): + # Save the partner_ids before calling super() to avoid losing them + # because when template_id is not set, + # partner_ids is set to False in the super() call. + old_partners = {composer.id: composer.partner_ids for composer in self} + res = super()._compute_partner_ids() + if self.env.context.get("message_forwarded_id"): + # Add the attachments from the original message. + for composer in self: + composer.partner_ids |= old_partners[composer.id] + return res + @api.model def get_record_data(self, values): result = super().get_record_data(values) @@ -20,20 +177,6 @@ class MailComposeMessage(models.TransientModel): return result def _action_send_mail(self, auto_commit=False): - # duplicate attachments from original message - message_forwarded_id = self.env.context.get("message_forwarded_id") - if message_forwarded_id: - message_forwarded = self.env["mail.message"].browse(message_forwarded_id) - for wizard in self: - new_attachment_ids = [] - for attachment in wizard.attachment_ids: - if attachment in message_forwarded.attachment_ids: - new_attachment = attachment.copy( - {"res_model": "mail.compose.message", "res_id": wizard.id} - ) - new_attachment_ids.append(new_attachment.id) - else: - new_attachment_ids.append(attachment.id) - new_attachment_ids.reverse() - wizard.write({"attachment_ids": [Command.set(new_attachment_ids)]}) - return super()._action_send_mail(auto_commit=auto_commit) + return super( + MailComposeMessage, self.with_context(forward_type=self.forward_type) + )._action_send_mail(auto_commit=auto_commit) diff --git a/mail_forward/wizards/mail_compose_message_view.xml b/mail_forward/wizards/mail_compose_message_view.xml index 1327be8d2..d6e758777 100644 --- a/mail_forward/wizards/mail_compose_message_view.xml +++ b/mail_forward/wizards/mail_compose_message_view.xml @@ -7,8 +7,25 @@ + + + + - 1 + forward_type == 'current_thread' + + + Followers to notify 1