From 7595783f6abe7ce6ddaf2732d0af27403c2dc04e Mon Sep 17 00:00:00 2001 From: Liam Noonan Date: Thu, 22 Oct 2026 12:43:43 +0000 Subject: [PATCH] [ADD] mail_always_notify_in_odoo Some companies may want to force users to engage incoming mail only in odoo, and so set up filters in their MTA to redirect all incoming mail with odoo references directly into odoo before reaching the user's email inbox. Odoo, however, does not notify the user if his email is already in the incoming headers of the message, just assuming he will have already been notified via email. This module ensures that all internal users, both those that are already followers of the document, and those that are included directly in the incoming To/Cc headers, are notified in odoo. Assisted-by: Cursor --- mail_always_notify_in_odoo/README.rst | 110 +++++ mail_always_notify_in_odoo/__init__.py | 1 + mail_always_notify_in_odoo/__manifest__.py | 28 ++ .../data/force_internal_inbox.xml | 5 + mail_always_notify_in_odoo/models/__init__.py | 1 + .../models/mail_thread.py | 90 ++++ .../models/res_users.py | 57 +++ mail_always_notify_in_odoo/pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 5 + mail_always_notify_in_odoo/readme/USAGE.md | 3 + .../static/description/index.html | 451 ++++++++++++++++++ .../message_notification_popover.esm.js | 24 + .../tours/user_modify_own_profile_tour.esm.js | 29 ++ mail_always_notify_in_odoo/tests/__init__.py | 4 + .../tests/test_mail_always_notify_in_odoo.py | 240 ++++++++++ .../views/res_users_views.xml | 26 + 17 files changed, 1079 insertions(+) create mode 100644 mail_always_notify_in_odoo/README.rst create mode 100644 mail_always_notify_in_odoo/__init__.py create mode 100644 mail_always_notify_in_odoo/__manifest__.py create mode 100644 mail_always_notify_in_odoo/data/force_internal_inbox.xml create mode 100644 mail_always_notify_in_odoo/models/__init__.py create mode 100644 mail_always_notify_in_odoo/models/mail_thread.py create mode 100644 mail_always_notify_in_odoo/models/res_users.py create mode 100644 mail_always_notify_in_odoo/pyproject.toml create mode 100644 mail_always_notify_in_odoo/readme/CONTRIBUTORS.md create mode 100644 mail_always_notify_in_odoo/readme/DESCRIPTION.md create mode 100644 mail_always_notify_in_odoo/readme/USAGE.md create mode 100644 mail_always_notify_in_odoo/static/description/index.html create mode 100644 mail_always_notify_in_odoo/static/src/core/common/message_notification_popover.esm.js create mode 100644 mail_always_notify_in_odoo/static/tests/tours/user_modify_own_profile_tour.esm.js create mode 100644 mail_always_notify_in_odoo/tests/__init__.py create mode 100644 mail_always_notify_in_odoo/tests/test_mail_always_notify_in_odoo.py create mode 100644 mail_always_notify_in_odoo/views/res_users_views.xml diff --git a/mail_always_notify_in_odoo/README.rst b/mail_always_notify_in_odoo/README.rst new file mode 100644 index 000000000..3e4fb51ed --- /dev/null +++ b/mail_always_notify_in_odoo/README.rst @@ -0,0 +1,110 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +===================== +Always Notify In Odoo +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:ba2ae9fedfa4aec4bf13f4060cb56c2b1d19896911b0e4d42b7cb0d39bdc7af6 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmail-lightgray.png?logo=github + :target: https://github.com/OCA/mail/tree/20.0/mail_always_notify_in_odoo + :alt: OCA/mail +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/mail-20-0/mail-20-0-mail_always_notify_in_odoo + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/mail&target_branch=20.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Force notification of internal users via odoo inbox regardless of +whether their email was in To:/Cc: + +Odoo avoids notifying internal users about inbound messages if their +email is already in the To or CC header. The assumption is that the user +will be notified via their email inbox and should not be notified again +of the same message in odoo. + +However, some companies may desire to force their users to engage with +odoo-related mail exclusively via odoo in order to avoid situations +where the user replies directly to the customer alone via email without +CC'ing the odoo alias, thereby creating a situation where communication +is divided between odoo and email. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +No special configuration needed. + +Note that on install and upgrade all internall users will be checked for +and coerced into ``mail.group_mail_notification_type_inbox``. + +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Pyxiris + +Contributors +------------ + +- `Pyxiris `__ + + - `Liam Noonan `__ + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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-ljmnoonan| image:: https://github.com/ljmnoonan.png?size=40px + :target: https://github.com/ljmnoonan + :alt: ljmnoonan + +Current `maintainer `__: + +|maintainer-ljmnoonan| + +This module is part of the `OCA/mail `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_always_notify_in_odoo/__init__.py b/mail_always_notify_in_odoo/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/mail_always_notify_in_odoo/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_always_notify_in_odoo/__manifest__.py b/mail_always_notify_in_odoo/__manifest__.py new file mode 100644 index 000000000..84a3f241a --- /dev/null +++ b/mail_always_notify_in_odoo/__manifest__.py @@ -0,0 +1,28 @@ +# Copyright 2026 Liam Noonan +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Always Notify In Odoo", + "summary": "Notify internal users in Odoo, including mail sent to them", + "version": "20.0.1.0.0", + "category": "Productivity/Discuss", + "website": "https://github.com/OCA/mail", + "author": "Pyxiris, Odoo Community Association (OCA)", + "license": "AGPL-3", + "development_status": "Beta", + "maintainers": ["ljmnoonan"], + "depends": ["mail"], + "data": [ + "data/force_internal_inbox.xml", + "views/res_users_views.xml", + ], + "assets": { + "web.assets_backend": [ + "mail_always_notify_in_odoo/static/src/core/**/*", + ], + "web.assets_tests": [ + "mail_always_notify_in_odoo/static/tests/tours/**/*", + ], + }, + "installable": True, +} diff --git a/mail_always_notify_in_odoo/data/force_internal_inbox.xml b/mail_always_notify_in_odoo/data/force_internal_inbox.xml new file mode 100644 index 000000000..0d51a11c8 --- /dev/null +++ b/mail_always_notify_in_odoo/data/force_internal_inbox.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/mail_always_notify_in_odoo/models/__init__.py b/mail_always_notify_in_odoo/models/__init__.py new file mode 100644 index 000000000..0e484ef8a --- /dev/null +++ b/mail_always_notify_in_odoo/models/__init__.py @@ -0,0 +1 @@ +from . import mail_thread, res_users diff --git a/mail_always_notify_in_odoo/models/mail_thread.py b/mail_always_notify_in_odoo/models/mail_thread.py new file mode 100644 index 000000000..97f2175fb --- /dev/null +++ b/mail_always_notify_in_odoo/models/mail_thread.py @@ -0,0 +1,90 @@ +# Copyright 2026 Liam Noonan +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command, models +from odoo.tools.mail import email_normalize, email_split_and_format + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + def _notify_get_recipients(self, message, **kwargs): + """Notify in odoo (inbox)internal users addressed on an incoming email. + + Core skips addresses on the incoming To/Cc, in order to avoid duplicate + notifications. + The point of this module is to notify internal users via odoo regardless. + In order to acheive this we stash the original headers (incoming_email_to...) + and the original partners called out on the message (partner_ids...). + Then we add the internal partners found in the email headers as if they were + mentioned or followers, and remove the addresses of the internal partners from + the incoming email headers. + This allows the super function to work as normal and build recipients_data + without throwing out the internal partners in the email headers. + Finally, we restore the original headers and partners to the message so as not + to interfere with subsequent logic. + """ + if message.message_type != "email": + return super()._notify_get_recipients(message, **kwargs) + + partners_to, partners_cc = self._incoming_internal_partners(message) + if not partners_to and not partners_cc: + return super()._notify_get_recipients(message, **kwargs) + + message = message.sudo() + saved_partners = message.partner_ids + saved_partners_cc = message.partner_cc_ids + saved_to = message.incoming_email_to + saved_cc = message.incoming_email_cc + partner_emails = { + email + for email in (partners_to | partners_cc).mapped("email_normalized") + if email + } + message.write( + { + "partner_ids": [Command.link(partner.id) for partner in partners_to], + "partner_cc_ids": [Command.link(partner.id) for partner in partners_cc], + "incoming_email_to": self._emails_without(saved_to, partner_emails), + "incoming_email_cc": self._emails_without(saved_cc, partner_emails), + } + ) + try: + return super()._notify_get_recipients(message, **kwargs) + finally: + message.write( + { + "partner_ids": [Command.set(saved_partners.ids)], + "partner_cc_ids": [Command.set(saved_partners_cc.ids)], + "incoming_email_to": saved_to, + "incoming_email_cc": saved_cc, + } + ) + + def _incoming_internal_partners(self, message): + """Internal users on the incoming To and Cc, as two recordsets.""" + message = message.sudo() + partners_to = self._internal_partners_from_emails(message.incoming_email_to) + partners_cc = self._internal_partners_from_emails(message.incoming_email_cc) + # Prefer "To" when the same address appears in both "To" and "Cc". + return partners_to, partners_cc - partners_to + + def _internal_partners_from_emails(self, value): + emails = email_split_and_format(value or "") + if not emails: + return self.env["res.partner"] + return self.env["mail.thread"]._partner_find_from_emails_single( + emails, + no_create=True, + filter_found=lambda partner: any( + not user.share for user in partner.user_ids + ), + ) + + def _emails_without(self, header_value, emails_to_remove): + """Rebuild a To/Cc header string without the given normalized addresses.""" + remaining = [] + for address in email_split_and_format(header_value or ""): + if email_normalize(address) not in emails_to_remove: + remaining.append(address) + return ", ".join(remaining) diff --git a/mail_always_notify_in_odoo/models/res_users.py b/mail_always_notify_in_odoo/models/res_users.py new file mode 100644 index 000000000..fabad6613 --- /dev/null +++ b/mail_always_notify_in_odoo/models/res_users.py @@ -0,0 +1,57 @@ +# Copyright 2026 Liam Noonan +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command, api, models + +from odoo.addons.base.models.res_users import NO_GROUP_CHANGE_LOG + + +class ResUsers(models.Model): + _inherit = "res.users" + + @api.model_create_multi + def create(self, vals_list): + users = super().create(vals_list) + # An explicit notification_type in the create values is stored as-is + # and does not run the compute. + if not self.env.context.get("forcing_notification_type_inbox"): + users._compute_notification_type() + return users + + def write(self, vals): + res = super().write(vals) + # Writing notification_type marks the field as user-set, so the group + # change made by its inverse does not recompute it. + if ( + not self.env.context.get("forcing_notification_type_inbox") + and "notification_type" in vals + ): + self._compute_notification_type() + return res + + @api.depends("share", "all_group_ids") + def _compute_notification_type(self): + if not self.env.context.get("forcing_notification_type_inbox"): + self._force_notification_type_inbox() + return super()._compute_notification_type() + + def _force_notification_type_inbox(self): + """Link the inbox group on internal users who are missing it.""" + inbox_group_id = self.env["ir.model.data"]._xmlid_to_res_id( + "mail.group_mail_notification_type_inbox" + ) + missing = self.filtered( + lambda user: not user.share and inbox_group_id not in user.group_ids.ids + ) + if not inbox_group_id or not missing: + return + missing.with_context( + forcing_notification_type_inbox=True, + no_group_change_log=NO_GROUP_CHANGE_LOG, + ).sudo().write({"group_ids": [Command.link(inbox_group_id)]}) + + @api.model + def _initial_force_notification_type_inbox(self): + """Recompute all users on install""" + users = self.search([("share", "=", False)]) + users._compute_notification_type() diff --git a/mail_always_notify_in_odoo/pyproject.toml b/mail_always_notify_in_odoo/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/mail_always_notify_in_odoo/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/mail_always_notify_in_odoo/readme/CONTRIBUTORS.md b/mail_always_notify_in_odoo/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..c72f58764 --- /dev/null +++ b/mail_always_notify_in_odoo/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Pyxiris](https://github.com/Pyxiris) + - [Liam Noonan](https://github.com/ljmnoonan) diff --git a/mail_always_notify_in_odoo/readme/DESCRIPTION.md b/mail_always_notify_in_odoo/readme/DESCRIPTION.md new file mode 100644 index 000000000..81bc8ccd2 --- /dev/null +++ b/mail_always_notify_in_odoo/readme/DESCRIPTION.md @@ -0,0 +1,5 @@ +Force notification of internal users via odoo inbox regardless of whether their email was in To:/Cc: + +Odoo avoids notifying internal users about inbound messages if their email is already in the To or CC header. The assumption is that the user will be notified via their email inbox and should not be notified again of the same message in odoo. + +However, some companies may desire to force their users to engage with odoo-related mail exclusively via odoo in order to avoid situations where the user replies directly to the customer alone via email without CC'ing the odoo alias, thereby creating a situation where communication is divided between odoo and email. diff --git a/mail_always_notify_in_odoo/readme/USAGE.md b/mail_always_notify_in_odoo/readme/USAGE.md new file mode 100644 index 000000000..a72e7894c --- /dev/null +++ b/mail_always_notify_in_odoo/readme/USAGE.md @@ -0,0 +1,3 @@ +No special configuration needed. + +Note that on install and upgrade all internall users will be checked for and coerced into `mail.group_mail_notification_type_inbox`. diff --git a/mail_always_notify_in_odoo/static/description/index.html b/mail_always_notify_in_odoo/static/description/index.html new file mode 100644 index 000000000..86134a843 --- /dev/null +++ b/mail_always_notify_in_odoo/static/description/index.html @@ -0,0 +1,451 @@ + + + + + +Always Notify In Odoo + + + +
+ + + +Odoo Community Association + +
+

Always Notify In Odoo

+ +

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

+

Force notification of internal users via odoo inbox regardless of +whether their email was in To:/Cc:

+

Odoo avoids notifying internal users about inbound messages if their +email is already in the To or CC header. The assumption is that the user +will be notified via their email inbox and should not be notified again +of the same message in odoo.

+

However, some companies may desire to force their users to engage with +odoo-related mail exclusively via odoo in order to avoid situations +where the user replies directly to the customer alone via email without +CC’ing the odoo alias, thereby creating a situation where communication +is divided between odoo and email.

+

Table of contents

+ +
+

Usage

+

No special configuration needed.

+

Note that on install and upgrade all internall users will be checked for +and coerced into mail.group_mail_notification_type_inbox.

+
+
+

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 +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Pyxiris
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

Current maintainer:

+

ljmnoonan

+

This module is part of the OCA/mail project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+
+ + diff --git a/mail_always_notify_in_odoo/static/src/core/common/message_notification_popover.esm.js b/mail_always_notify_in_odoo/static/src/core/common/message_notification_popover.esm.js new file mode 100644 index 000000000..fb319a4a5 --- /dev/null +++ b/mail_always_notify_in_odoo/static/src/core/common/message_notification_popover.esm.js @@ -0,0 +1,24 @@ +import {MessageNotificationPopover} from "@mail/core/common/message_notification_popover"; +import {patch} from "@web/core/utils/patch"; + +patch(MessageNotificationPopover.prototype, { + /** + * An incoming address means this person was on the original email. + * The inbox notification is already implied for an internal user, so the + * popover keeps the email row and skips that notification. + */ + get recipientsData() { + const recipients = super.recipientsData; + const incomingEmails = new Set( + recipients + .filter((recipient) => !recipient.notification && recipient.email) + .map((recipient) => recipient.email.trim().toLowerCase()) + ); + return recipients.filter((recipient) => { + if (!recipient.notification || recipient.failure || !recipient.email) { + return true; + } + return !incomingEmails.has(recipient.email.trim().toLowerCase()); + }); + }, +}); diff --git a/mail_always_notify_in_odoo/static/tests/tours/user_modify_own_profile_tour.esm.js b/mail_always_notify_in_odoo/static/tests/tours/user_modify_own_profile_tour.esm.js new file mode 100644 index 000000000..50788e13c --- /dev/null +++ b/mail_always_notify_in_odoo/static/tests/tours/user_modify_own_profile_tour.esm.js @@ -0,0 +1,29 @@ +import "@mail/../tests/tours/user_modify_own_profile_tour"; +import {patch} from "@web/core/utils/patch"; +import {registry} from "@web/core/registry"; + +const TOUR = "mail/static/tests/tours/user_modify_own_profile_tour.js"; + +/** + * Core's profile tour clicks the notification_type radio to dirty the form. + * This module hides that field and forces inbox, so replace that step with a + * normal preference edit. + */ +patch(registry.category("web_tour.tours").get(TOUR), { + steps() { + const steps = super.steps(); + const index = steps.findIndex((step) => + step.trigger?.includes('div[name="notification_type"]') + ); + if (index === -1) { + return steps; + } + steps.splice(index, 1, { + content: + "Update signature (notification type is hidden and forced to inbox)", + trigger: '.modal .o_field_html[name="signature"] .odoo-editor-editable', + run: "editor Tour signature", + }); + return steps; + }, +}); diff --git a/mail_always_notify_in_odoo/tests/__init__.py b/mail_always_notify_in_odoo/tests/__init__.py new file mode 100644 index 000000000..9f3e93bbd --- /dev/null +++ b/mail_always_notify_in_odoo/tests/__init__.py @@ -0,0 +1,4 @@ +# Copyright 2026 Pyxiris +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_mail_always_notify_in_odoo diff --git a/mail_always_notify_in_odoo/tests/test_mail_always_notify_in_odoo.py b/mail_always_notify_in_odoo/tests/test_mail_always_notify_in_odoo.py new file mode 100644 index 000000000..ab72a0cd8 --- /dev/null +++ b/mail_always_notify_in_odoo/tests/test_mail_always_notify_in_odoo.py @@ -0,0 +1,240 @@ +# Copyright 2026 Liam Noonan +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command +from odoo.tests import tagged + +from odoo.addons.mail.tests.common import MailCommon, mail_new_test_user + + +@tagged("post_install", "-at_install") +class TestMailAlwaysNotifyInOdoo(MailCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.inbox_group = cls.env.ref("mail.group_mail_notification_type_inbox") + cls.internal_user = mail_new_test_user( + cls.env, + login="always_notify_internal", + groups="base.group_user", + name="Always Notify Internal", + email="always.notify.internal@example.com", + notification_type="email", + ) + cls.customer = cls.env["res.partner"].create( + { + "name": "Always Notify Customer", + "email": "always.notify.customer@example.com", + } + ) + cls.document = cls.env["res.partner"].create( + {"name": "Always Notify Document", "email": "always.notify.doc@example.com"} + ) + + def _notification(self, message, partner): + return self.env["mail.notification"].search( + [ + ("mail_message_id", "=", message.id), + ("res_partner_id", "=", partner.id), + ] + ) + + def _recipients_by_id(self, message): + return { + recipient["id"]: recipient + for recipient in self.document._notify_get_recipients(message) + if recipient.get("id") + } + + def test_internal_user_is_forced_to_inbox(self): + """Create/write cannot leave an internal user on email notifications.""" + self.assertEqual(self.internal_user.notification_type, "inbox") + self.assertIn(self.inbox_group, self.internal_user.group_ids) + self.assertIn(self.env.ref("base.group_user"), self.internal_user.all_group_ids) + + self.internal_user.write({"notification_type": "email"}) + self.assertEqual(self.internal_user.notification_type, "inbox") + self.assertIn(self.inbox_group, self.internal_user.group_ids) + + self.internal_user.with_user(self.internal_user).write( + {"notification_type": "email"} + ) + self.assertEqual(self.internal_user.notification_type, "inbox") + + # Unlinking the inbox group alone is corrected on the next compute. + self.internal_user.write({"group_ids": [Command.unlink(self.inbox_group.id)]}) + self.assertEqual(self.internal_user.notification_type, "inbox") + self.assertIn(self.inbox_group, self.internal_user.group_ids) + + def test_portal_user_stays_on_email(self): + """Portal users are left on email; the force only targets internals.""" + portal_user = mail_new_test_user( + self.env, + login="always_notify_portal", + groups="base.group_portal", + name="Always Notify Portal", + email="always.notify.portal@example.com", + notification_type="email", + ) + self.assertEqual(portal_user.notification_type, "email") + self.assertNotIn(self.inbox_group, portal_user.group_ids) + + def test_internal_user_converted_to_portal_uses_email(self): + """Once share=True, core can move the user off inbox and we do not + force the group back. + """ + user = mail_new_test_user( + self.env, + login="always_notify_convert", + groups="base.group_user", + name="Always Notify Convert", + email="always.notify.convert@example.com", + notification_type="email", + ) + self.assertEqual(user.notification_type, "inbox") + user.write( + { + "group_ids": [ + Command.unlink(self.env.ref("base.group_user").id), + Command.link(self.env.ref("base.group_portal").id), + ] + } + ) + self.assertTrue(user._is_portal()) + self.assertEqual(user.notification_type, "email") + self.assertNotIn(self.inbox_group, user.group_ids) + + def test_incoming_to_internal_user_gets_inbox_notification(self): + """Internal To addresses get an inbox notification; headers and + partner_ids are restored after notify so the message still looks like + a gateway email. + """ + self.document.message_subscribe(partner_ids=self.customer.ids) + with self.mock_mail_gateway(): + message = self.document.message_post( + body="Hello from the customer", + message_type="email", + subtype_xmlid="mail.mt_comment", + author_id=self.customer.id, + email_from=self.customer.email_formatted, + incoming_email_to=( + f"{self.internal_user.partner_id.email_formatted}, " + f"{self.customer.email_formatted}" + ), + ) + internal_notif = self._notification(message, self.internal_user.partner_id) + self.assertEqual(len(internal_notif), 1) + self.assertEqual(internal_notif.notification_type, "inbox") + # Customer is already on incoming To, so core skips notifying them. + self.assertFalse(self._notification(message, self.customer)) + self.assertNotSentEmail([self.internal_user.partner_id, self.customer]) + self.assertIn(self.internal_user.email, message.incoming_email_to) + self.assertIn(self.customer.email, message.incoming_email_to) + self.assertNotIn(self.internal_user.partner_id, message.partner_ids) + self.assertNotIn(self.internal_user.partner_id, message.partner_cc_ids) + + def test_incoming_cc_internal_user_gets_inbox_notification(self): + """Internal Cc addresses get an inbox notification; Cc header and + partner links are restored after notify. + """ + with self.mock_mail_gateway(): + message = self.document.message_post( + body="Copied to an internal user", + message_type="email", + subtype_xmlid="mail.mt_comment", + author_id=self.customer.id, + email_from=self.customer.email_formatted, + incoming_email_cc=self.internal_user.partner_id.email_formatted, + ) + internal_notif = self._notification(message, self.internal_user.partner_id) + self.assertEqual(len(internal_notif), 1) + self.assertEqual(internal_notif.notification_type, "inbox") + self.assertNotSentEmail([self.internal_user.partner_id]) + self.assertIn(self.internal_user.email, message.incoming_email_cc) + self.assertNotIn(self.internal_user.partner_id, message.partner_ids) + self.assertNotIn(self.internal_user.partner_id, message.partner_cc_ids) + + def test_incoming_to_and_cc_set_is_cc_correctly(self): + """Temporary partner_cc_ids links make Cc recipients is_cc during + notify; To wins when the same address is listed on both headers. + """ + cc_user = mail_new_test_user( + self.env, + login="always_notify_cc", + groups="base.group_user", + name="Always Notify Cc", + email="always.notify.cc@example.com", + notification_type="email", + ) + both_user = mail_new_test_user( + self.env, + login="always_notify_both", + groups="base.group_user", + name="Always Notify Both", + email="always.notify.both@example.com", + notification_type="email", + ) + with self.mock_mail_gateway(): + message = self.document.message_post( + body="To and Cc internals", + message_type="email", + subtype_xmlid="mail.mt_comment", + author_id=self.customer.id, + email_from=self.customer.email_formatted, + incoming_email_to=( + f"{self.internal_user.partner_id.email_formatted}, " + f"{both_user.partner_id.email_formatted}" + ), + incoming_email_cc=( + f"{cc_user.partner_id.email_formatted}, " + f"{both_user.partner_id.email_formatted}" + ), + ) + + for partner in ( + self.internal_user.partner_id, + cc_user.partner_id, + both_user.partner_id, + ): + notif = self._notification(message, partner) + self.assertEqual(len(notif), 1) + self.assertEqual(notif.notification_type, "inbox") + + recipients = self._recipients_by_id(message) + self.assertFalse(recipients[self.internal_user.partner_id.id]["is_cc"]) + self.assertTrue(recipients[cc_user.partner_id.id]["is_cc"]) + self.assertFalse(recipients[both_user.partner_id.id]["is_cc"]) + + self.assertIn(self.internal_user.email, message.incoming_email_to) + self.assertIn(both_user.email, message.incoming_email_to) + self.assertIn(cc_user.email, message.incoming_email_cc) + self.assertIn(both_user.email, message.incoming_email_cc) + self.assertFalse( + message.partner_ids + & ( + self.internal_user.partner_id + | cc_user.partner_id + | both_user.partner_id + ) + ) + self.assertFalse( + message.partner_cc_ids + & ( + self.internal_user.partner_id + | cc_user.partner_id + | both_user.partner_id + ) + ) + + def test_comment_does_not_notify_address_from_incoming_headers(self): + """Only message_type=email triggers the incoming To/Cc inbox path.""" + with self.mock_mail_gateway(): + message = self.document.message_post( + body="A normal comment", + message_type="comment", + subtype_xmlid="mail.mt_comment", + author_id=self.customer.id, + email_from=self.customer.email_formatted, + incoming_email_to=self.internal_user.partner_id.email_formatted, + ) + self.assertFalse(self._notification(message, self.internal_user.partner_id)) diff --git a/mail_always_notify_in_odoo/views/res_users_views.xml b/mail_always_notify_in_odoo/views/res_users_views.xml new file mode 100644 index 000000000..9f3786843 --- /dev/null +++ b/mail_always_notify_in_odoo/views/res_users_views.xml @@ -0,0 +1,26 @@ + + + + res.users.preferences.form.mail.always.notify + res.users + + + + 1 + 1 + + + + + + res.users.form.mail.always.notify + res.users + + + + 1 + 1 + + + +