Skip to content

Commit

Permalink
[IMP] mail_tracking: display internal notifications status
Browse files Browse the repository at this point in the history
Use internal notifications status when mails are not generated
  • Loading branch information
vincent-hatakeyama committed Mar 18, 2024
1 parent d0d76c1 commit 2ad02d1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
36 changes: 36 additions & 0 deletions mail_tracking/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def _tracking_status_map_get(self):
"soft-bounced": "error",
}

def _notification_status(self, notification):
"""Map mail.notification states to be used in chatter"""
return (
"opened"
if notification.is_read
else {
"ready": "waiting",
"sent": "delivered",
"bounce": "error",
"exception": "error",
"canceled": "error",
}.get(notification.notification_status, "unknown")
)

def _partner_tracking_status_get(self, tracking_email):
"""Determine tracking status"""
tracking_status_map = self._tracking_status_map_get()
Expand Down Expand Up @@ -199,6 +213,28 @@ def tracking_status(self):
email_cc_list.discard(partner.email)
isCc = True
tracking_status = tracking_unknown_values.copy()
# Search internal mail.notifications (for users using it)
# Note that by default, read notifications older than 180 days are
# deleted.
notification = self.env["mail.notification"].search(
[
("notification_type", "=", "inbox"),
("mail_message_id", "=", message.id),
("res_partner_id", "=", partner.id),
]
)
if notification:
status = self._notification_status(notification)
tracking_status.update(
{
"status": status,
"status_human": self._partner_tracking_status_human_get(
status
),
"error_type": notification.failure_type,
"error_description": notification.failure_reason,
}
)
tracking_status.update(
{
"recipient": partner.name,
Expand Down
4 changes: 4 additions & 0 deletions mail_tracking/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* Rafael Blasco
* Alexandre Díaz

* XCG Consulting, part of `Orbeet <https://orbeet.io>`_:

* Vincent Hatakeyama

* `Eezee-IT <https://www.eezee-it.com>`_:
* Asma Elferkhsi

Expand Down
6 changes: 5 additions & 1 deletion mail_tracking/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ When user sends a message in mail_thread (chatter), for instance in partner
form, then an email tracking is created for each email notification. Then a
status icon will appear just right to name of notified partner.

For users using the internal notifications system, the status of their notifications is
used. Note that read notifications are deleted after 180 days so old messages will
display unknown status.

These are all available status icons:

.. |sent| image:: ../static/src/img/sent.png
Expand Down Expand Up @@ -55,7 +59,7 @@ If you want to see all tracking emails and events you can go to
* Settings > Technical > Email > Tracking emails
* Settings > Technical > Email > Tracking events

When the message generates an 'error' status, it will apear on discuss 'Failed'
When the message generates an 'error' status, it will appear on discuss 'Failed'
channel. Any view with chatter can show the failed messages
too.

Expand Down
48 changes: 48 additions & 0 deletions mail_tracking/tests/test_mail_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,51 @@ def mock_message_fetch(*args, **kwargs):

def test_unlink_mail_alias(self):
self.env["ir.config_parameter"].search([], limit=1).unlink()

def test_inbox_mail_notification(self):
"""Test tracking a message for a user using internal notifications."""
admin_partner = self.env.ref("base.partner_admin")
# Ensure admin settings is set to inbox
self.env.ref("base.user_admin").notification_type = "inbox"
message = self.env["mail.message"].create(
{
"subject": "Message test",
"author_id": self.sender.id,
"email_from": self.sender.email,
"message_type": "comment",
"model": "res.partner",
"res_id": admin_partner.id,
"partner_ids": [Command.link(admin_partner.id)],
"body": "<p>This is a test message</p>",
}
)
if message.is_thread_message():
self.env[message.model].browse(message.res_id)._notify_thread(message)
# Search tracking created
tracking_email = self.env["mail.tracking.email"].search(
[
("mail_message_id", "=", message.id),
("partner_id", "=", admin_partner.id),
]
)
# No tracking email exists
self.assertFalse(tracking_email)
# message_dict read by web interface
message_dict = message.message_format()[0]
status = message_dict["partner_trackings"][0]
# Tracking status must be delivered
self.assertEqual(status["status"], "delivered")
self.assertEqual(status["tracking_id"], tracking_email.id)
self.assertEqual(status["recipient"], admin_partner.name)
self.assertEqual(status["partner_id"], admin_partner.id)
self.assertEqual(status["isCc"], False)
# Mark the inbox message as read
self.env["mail.notification"].search(
[
("mail_message_id", "=", message.id),
("res_partner_id", "=", admin_partner.id),
]
).is_read = True
self.assertEqual(
message.message_format()[0]["partner_trackings"][0]["status"], "opened"
)

0 comments on commit 2ad02d1

Please sign in to comment.