Skip to content

Commit

Permalink
[MIG] mail_tracking: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Oct 28, 2024
1 parent f98b34c commit e992629
Show file tree
Hide file tree
Showing 27 changed files with 470 additions and 273 deletions.
10 changes: 10 additions & 0 deletions mail_tracking/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ Contributors

- Agustín Payen Sandoval

- `Trobz <https://www.trobz.com>`__:

- Tris Doan

Other credits
-------------

The migration of this module from 17.0 to 18.0 was financially supported
by Camptocamp.

Maintainers
-----------

Expand Down
2 changes: 1 addition & 1 deletion mail_tracking/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"name": "Email tracking",
"summary": "Email tracking system for all mails sent",
"version": "17.0.1.0.0",
"version": "18.0.1.0.0",
"category": "Social Network",
"website": "https://github.com/OCA/social",
"author": ("Tecnativa, Odoo Community Association (OCA)"),
Expand Down
8 changes: 7 additions & 1 deletion mail_tracking/controllers/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from odoo.http import request, route

from odoo.addons.mail.controllers.mailbox import MailboxController
from odoo.addons.mail.tools.discuss import Store


class MailTrackingMailBoxController(MailboxController):
Expand All @@ -19,4 +20,9 @@ def discuss_failed_messages(
around=around,
limit=limit,
)
return {**res, "messages": res["messages"].message_format()}
messages = res.pop("messages")
return {

Check warning on line 24 in mail_tracking/controllers/mailbox.py

View check run for this annotation

Codecov / codecov/patch

mail_tracking/controllers/mailbox.py#L23-L24

Added lines #L23 - L24 were not covered by tests
**res,
"data": Store(messages, for_current_user=True).get_result(),
"messages": Store.many_ids(messages),
}
8 changes: 5 additions & 3 deletions mail_tracking/models/mail_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _tracking_email_prepare(self, email):
email_to = COMMASPACE.join(email_to_list)
return {
"name": self.subject,
"timestamp": "%.6f" % ts,
"timestamp": f"{ts:.6f}",
"time": fields.Datetime.to_string(dt),
"mail_id": self.id,
"mail_message_id": self.mail_message_id.id,
Expand All @@ -28,14 +28,16 @@ def _tracking_email_prepare(self, email):
"sender": self.email_from,
}

def _prepare_outgoing_list(self, recipients_follower_status=None):
def _prepare_outgoing_list(
self, mail_server=False, recipients_follower_status=None
):
"""Creates the mail.tracking.email record and adds the image tracking
to the email. Please note that because we can't add mail headers in this
function, the added tracking image will later (IrMailServer.build_email)
also be used to extract the mail.tracking.email record id and to set the
X-Odoo-MailTracking-ID header there.
"""
emails = super()._prepare_outgoing_list(recipients_follower_status)
emails = super()._prepare_outgoing_list(mail_server, recipients_follower_status)
for email in emails:
vals = self._tracking_email_prepare(email)
tracking_email = self.env["mail.tracking.email"].sudo().create(vals)
Expand Down
78 changes: 40 additions & 38 deletions mail_tracking/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from odoo.osv import expression
from odoo.tools import email_split

from odoo.addons.mail.tools.discuss import Store


class MailMessage(models.Model):
_inherit = "mail.message"
Expand Down Expand Up @@ -59,30 +61,44 @@ def _compute_is_failed_message(self):
needs_action and involves_me and has_failed_trackings
)

def _search_is_failed_message(self, operator, value):
@api.model
def _search_is_failed_message(self, operator, operand):
"""Search for messages considered failed for the active user.
Be notice that 'notificacion_ids' is a record that change if
the user mark the message as readed.
"""
# FIXME: Due to ORM issue with auto_join and 'OR' we construct the domain
# using an extra query to get valid results.
# For more information see: https://github.com/odoo/odoo/issues/25175
notification_partner_ids = self.search(
[("notification_ids.res_partner_id", "=", self.env.user.partner_id.id)]
pid = self.env.user.partner_id.id
self.flush_model(
["author_id", "mail_tracking_ids", "mail_tracking_needs_action"]
)
self.env["mail.notification"].flush_model(["mail_message_id", "res_partner_id"])
# retrieve failed tracking email
tracking_operator = "in" if operand else "not in"
failed_states = list(self.get_failed_states())
tracking_ids = self.env["mail.tracking.email"]._search(
[("state", f"{tracking_operator}", failed_states)]
)

is_involve = expression.OR(
[
[
("notification_ids.res_partner_id", "=", pid),
],
[
("author_id", "=", pid),
],
]
)
return expression.normalize_domain(
domain = expression.AND(
[
(
"mail_tracking_ids.state",
"in" if value else "not in",
list(self.get_failed_states()),
),
("mail_tracking_needs_action", "=", True),
"|",
("author_id", "=", self.env.user.partner_id.id),
("id", "in", notification_partner_ids.ids),
[
("mail_tracking_ids", "in", tracking_ids),
("mail_tracking_needs_action", "=", True),
],
is_involve,
]
)
return domain

def _tracking_status_map_get(self):
"""Map tracking states to be used in chatter"""
Expand Down Expand Up @@ -267,7 +283,7 @@ def get_failed_messages(self):

def set_need_action_done(self):
"""This will mark the messages to be ignored in the tracking issues filter"""
self.check_access_rule("read")
self.check_access("read")
self.mail_tracking_needs_action = False
self._notify_message_notification_update()

Expand All @@ -286,29 +302,15 @@ def get_failed_messsage_info(self, ids, model):
]
return res

def _message_notification_format(self):
"""Add info for the web client"""
formatted_notifications = super()._message_notification_format()
for notification in formatted_notifications:
message = self.filtered(
lambda x, notification=notification: x.id == notification["id"]
)
notification.update(
def _extras_to_store(self, store: Store, format_reply):
res = super()._extras_to_store(store, format_reply=format_reply)
for message in self:
store.add(
message,
{
"partner_trackings": message.tracking_status(),
"mail_tracking_needs_action": message.mail_tracking_needs_action,
"is_failed_message": message.is_failed_message,
}
},
)
return formatted_notifications

def _message_format_extras(self, format_reply):
"""Add info for the web client"""
res = super()._message_format_extras(format_reply)
res.update(
{
"partner_trackings": self.tracking_status(),
"mail_tracking_needs_action": self.mail_tracking_needs_action,
"is_failed_message": self.is_failed_message,
}
)
return res
2 changes: 1 addition & 1 deletion mail_tracking/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from lxml import etree

from odoo import _, api, fields, models
from odoo.tools import email_split, email_split_and_format
from odoo.tools.mail import email_split, email_split_and_format


class MailThread(models.AbstractModel):
Expand Down
Loading

0 comments on commit e992629

Please sign in to comment.