2018-03-19 16:41:26 +01:00
|
|
|
# Copyright 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
|
2019-07-03 11:12:11 +02:00
|
|
|
# Copyright 2019 Alexandre Díaz
|
2016-06-14 17:22:17 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
2020-03-19 20:30:34 +01:00
|
|
|
from email.utils import getaddresses
|
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
from odoo import _, api, fields, models
|
2020-10-02 20:23:03 +02:00
|
|
|
from odoo.osv import expression
|
2019-07-03 11:12:11 +02:00
|
|
|
from odoo.tools import email_split
|
2016-06-14 17:22:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MailMessage(models.Model):
|
|
|
|
_inherit = "mail.message"
|
|
|
|
|
2019-07-03 11:12:11 +02:00
|
|
|
# Recipients
|
2019-11-18 11:46:59 +01:00
|
|
|
email_cc = fields.Char(
|
|
|
|
"Cc", help="Additional recipients that receive a " '"Carbon Copy" of the e-mail'
|
|
|
|
)
|
2020-03-19 20:30:34 +01:00
|
|
|
email_to = fields.Char("To", help="Raw TO recipients")
|
2019-07-05 14:20:27 +02:00
|
|
|
mail_tracking_ids = fields.One2many(
|
2019-11-18 11:46:59 +01:00
|
|
|
comodel_name="mail.tracking.email",
|
|
|
|
inverse_name="mail_message_id",
|
2019-07-05 14:20:27 +02:00
|
|
|
string="Mail Trackings",
|
|
|
|
)
|
|
|
|
mail_tracking_needs_action = fields.Boolean(
|
2019-11-18 11:46:59 +01:00
|
|
|
help="The message tracking will be considered" " to filter tracking issues",
|
2019-07-05 14:20:27 +02:00
|
|
|
default=False,
|
|
|
|
)
|
2020-10-02 20:23:03 +02:00
|
|
|
is_failed_message = fields.Boolean(
|
2021-05-03 18:53:26 +02:00
|
|
|
compute="_compute_is_failed_message",
|
|
|
|
search="_search_is_failed_message",
|
2020-10-02 20:23:03 +02:00
|
|
|
)
|
2019-07-05 14:20:27 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def get_failed_states(self):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""The 'failed' states of the message"""
|
2019-11-18 11:46:59 +01:00
|
|
|
return {"error", "rejected", "spam", "bounced", "soft-bounced"}
|
2019-07-03 11:12:11 +02:00
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
@api.depends(
|
|
|
|
"mail_tracking_needs_action",
|
|
|
|
"author_id",
|
2020-10-02 20:23:03 +02:00
|
|
|
"notification_ids",
|
2019-11-18 11:46:59 +01:00
|
|
|
"mail_tracking_ids.state",
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
def _compute_is_failed_message(self):
|
|
|
|
"""Compute 'is_failed_message' field for the active user"""
|
|
|
|
failed_states = self.get_failed_states()
|
|
|
|
for message in self:
|
|
|
|
needs_action = message.mail_tracking_needs_action
|
|
|
|
involves_me = self.env.user.partner_id in (
|
2020-10-02 20:23:03 +02:00
|
|
|
message.author_id | message.notification_ids.mapped("res_partner_id")
|
2019-11-18 11:46:59 +01:00
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
has_failed_trackings = failed_states.intersection(
|
2019-11-18 11:46:59 +01:00
|
|
|
message.mapped("mail_tracking_ids.state")
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
message.is_failed_message = bool(
|
2019-11-18 11:46:59 +01:00
|
|
|
needs_action and involves_me and has_failed_trackings
|
|
|
|
)
|
2019-10-17 03:28:42 +02:00
|
|
|
|
2020-10-02 20:23:03 +02:00
|
|
|
def _search_is_failed_message(self, operator, value):
|
|
|
|
"""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)]
|
|
|
|
)
|
|
|
|
return expression.normalize_domain(
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"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),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2016-06-14 17:22:17 +02:00
|
|
|
def _tracking_status_map_get(self):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Map tracking states to be used in chatter"""
|
2016-06-14 17:22:17 +02:00
|
|
|
return {
|
2019-11-18 11:46:59 +01:00
|
|
|
"False": "waiting",
|
|
|
|
"error": "error",
|
|
|
|
"deferred": "sent",
|
|
|
|
"sent": "sent",
|
|
|
|
"delivered": "delivered",
|
|
|
|
"opened": "opened",
|
|
|
|
"rejected": "error",
|
|
|
|
"spam": "error",
|
|
|
|
"unsub": "opened",
|
|
|
|
"bounced": "error",
|
|
|
|
"soft-bounced": "error",
|
2016-06-14 17:22:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def _partner_tracking_status_get(self, tracking_email):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Determine tracking status"""
|
2016-06-14 17:22:17 +02:00
|
|
|
tracking_status_map = self._tracking_status_map_get()
|
2019-11-18 11:46:59 +01:00
|
|
|
status = "unknown"
|
2016-06-14 17:22:17 +02:00
|
|
|
if tracking_email:
|
|
|
|
tracking_email_status = str(tracking_email.state)
|
2019-11-18 11:46:59 +01:00
|
|
|
status = tracking_status_map.get(tracking_email_status, "unknown")
|
2016-06-14 17:22:17 +02:00
|
|
|
return status
|
|
|
|
|
2019-08-12 13:51:15 +02:00
|
|
|
def _partner_tracking_status_human_get(self, status):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Translations for tracking statuses to be used on qweb"""
|
2019-11-18 11:46:59 +01:00
|
|
|
statuses = {
|
|
|
|
"waiting": _("Waiting"),
|
|
|
|
"error": _("Error"),
|
|
|
|
"sent": _("Sent"),
|
|
|
|
"delivered": _("Delivered"),
|
|
|
|
"opened": _("Opened"),
|
|
|
|
"unknown": _("Unknown"),
|
|
|
|
}
|
2019-08-12 13:51:15 +02:00
|
|
|
return _("Status: %s") % statuses[status]
|
|
|
|
|
2019-10-17 03:28:42 +02:00
|
|
|
@api.model
|
|
|
|
def _get_error_description(self, tracking):
|
|
|
|
"""Translations for error descriptions to be used on qweb"""
|
2019-11-18 11:46:59 +01:00
|
|
|
descriptions = {"no_recipient": _("The partner doesn't have a defined email")}
|
|
|
|
return descriptions.get(tracking.error_type, tracking.error_description)
|
2019-10-17 03:28:42 +02:00
|
|
|
|
2016-09-09 18:33:21 +02:00
|
|
|
def tracking_status(self):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Generates a complete status tracking of the messages by partner"""
|
2016-09-09 18:33:21 +02:00
|
|
|
res = {}
|
|
|
|
for message in self:
|
|
|
|
partner_trackings = []
|
2019-11-18 11:46:59 +01:00
|
|
|
partners_already = self.env["res.partner"]
|
|
|
|
partners = self.env["res.partner"]
|
|
|
|
trackings = (
|
|
|
|
self.env["mail.tracking.email"]
|
|
|
|
.sudo()
|
|
|
|
.search([("mail_message_id", "=", message.id)])
|
|
|
|
)
|
2020-03-19 20:30:34 +01:00
|
|
|
# String to List
|
|
|
|
email_cc_list = self._drop_aliases(email_split(message.email_cc))
|
|
|
|
email_to_list = self._drop_aliases(email_split(message.email_to))
|
|
|
|
# Search related partners recipients
|
|
|
|
partners |= partners.search(
|
|
|
|
[("email", "in", email_cc_list + email_to_list)]
|
|
|
|
)
|
|
|
|
# Operate over set's instead of lists
|
2019-07-23 17:50:45 +02:00
|
|
|
email_cc_list = set(email_cc_list)
|
2020-03-19 20:30:34 +01:00
|
|
|
email_to_list = set(email_to_list) - email_cc_list
|
2016-09-09 18:33:21 +02:00
|
|
|
# Search all trackings for this message
|
|
|
|
for tracking in trackings:
|
|
|
|
status = self._partner_tracking_status_get(tracking)
|
2019-11-18 11:46:59 +01:00
|
|
|
recipient = tracking.partner_id.name or tracking.recipient
|
|
|
|
partner_trackings.append(
|
|
|
|
{
|
|
|
|
"status": status,
|
|
|
|
"status_human": self._partner_tracking_status_human_get(status),
|
|
|
|
"error_type": tracking.error_type,
|
|
|
|
"error_description": self._get_error_description(tracking),
|
|
|
|
"tracking_id": tracking.id,
|
|
|
|
"recipient": recipient,
|
|
|
|
"partner_id": tracking.partner_id.id,
|
|
|
|
"isCc": False,
|
|
|
|
}
|
|
|
|
)
|
2016-09-09 18:33:21 +02:00
|
|
|
if tracking.partner_id:
|
2020-03-19 20:30:34 +01:00
|
|
|
# Discard mails with tracking
|
2019-07-23 17:50:45 +02:00
|
|
|
email_cc_list.discard(tracking.partner_id.email)
|
2020-03-19 20:30:34 +01:00
|
|
|
email_to_list.discard(tracking.partner_id.email)
|
2016-09-09 18:33:21 +02:00
|
|
|
partners_already |= tracking.partner_id
|
2020-03-19 20:30:34 +01:00
|
|
|
# Search all partner recipients for this message
|
2016-09-09 18:33:21 +02:00
|
|
|
if message.partner_ids:
|
|
|
|
partners |= message.partner_ids
|
2019-11-18 11:34:02 +01:00
|
|
|
if message.notified_partner_ids:
|
|
|
|
partners |= message.notified_partner_ids
|
2020-03-19 20:30:34 +01:00
|
|
|
# Discard partner recipients already included
|
2016-09-09 18:33:21 +02:00
|
|
|
partners -= partners_already
|
2020-03-19 20:30:34 +01:00
|
|
|
# Default tracking values
|
|
|
|
tracking_unknown_values = {
|
2019-11-18 11:46:59 +01:00
|
|
|
"status": "unknown",
|
|
|
|
"status_human": self._partner_tracking_status_human_get("unknown"),
|
|
|
|
"error_type": False,
|
|
|
|
"error_description": False,
|
|
|
|
"tracking_id": False,
|
2019-10-17 03:28:42 +02:00
|
|
|
}
|
2020-03-19 20:30:34 +01:00
|
|
|
# Process tracking status of partner recipients without tracking
|
2016-09-09 18:33:21 +02:00
|
|
|
for partner in partners:
|
2020-03-19 20:30:34 +01:00
|
|
|
# Discard 'To' with partner
|
|
|
|
if partner.email in email_to_list:
|
|
|
|
email_to_list.discard(partner.email)
|
2016-09-09 18:33:21 +02:00
|
|
|
# If there is partners not included, then status is 'unknown'
|
2019-10-17 03:28:42 +02:00
|
|
|
# and perhaps a Cc recipient
|
2019-07-23 17:50:45 +02:00
|
|
|
isCc = False
|
|
|
|
if partner.email in email_cc_list:
|
|
|
|
email_cc_list.discard(partner.email)
|
|
|
|
isCc = True
|
2020-03-19 20:30:34 +01:00
|
|
|
tracking_status = tracking_unknown_values.copy()
|
|
|
|
tracking_status.update(
|
2019-11-18 11:46:59 +01:00
|
|
|
{"recipient": partner.name, "partner_id": partner.id, "isCc": isCc}
|
|
|
|
)
|
2020-03-19 20:30:34 +01:00
|
|
|
partner_trackings.append(tracking_status)
|
|
|
|
# Process Cc/To recipients without partner
|
|
|
|
for cc, lst in [(True, email_cc_list), (False, email_to_list)]:
|
|
|
|
for email in lst:
|
|
|
|
tracking_status = tracking_unknown_values.copy()
|
|
|
|
tracking_status.update(
|
|
|
|
{"recipient": email, "partner_id": False, "isCc": cc}
|
|
|
|
)
|
|
|
|
partner_trackings.append(tracking_status)
|
2019-10-17 03:28:42 +02:00
|
|
|
res[message.id] = {
|
2019-11-18 11:46:59 +01:00
|
|
|
"partner_trackings": partner_trackings,
|
|
|
|
"is_failed_message": message.is_failed_message,
|
2019-10-17 03:28:42 +02:00
|
|
|
}
|
2016-09-09 18:33:21 +02:00
|
|
|
return res
|
|
|
|
|
2020-03-19 20:30:34 +01:00
|
|
|
@api.model
|
|
|
|
def _drop_aliases(self, mail_list):
|
|
|
|
aliases = self.env["mail.alias"].get_aliases()
|
|
|
|
|
|
|
|
def _filter_alias(email):
|
|
|
|
email_wn = getaddresses([email])[0][1]
|
|
|
|
if email_wn not in aliases:
|
|
|
|
return email_wn
|
|
|
|
|
|
|
|
return list(filter(_filter_alias, mail_list))
|
|
|
|
|
2022-03-25 12:31:13 +01:00
|
|
|
def message_format(self, format_reply=True):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Preare values to be used by the chatter widget"""
|
2022-03-25 12:31:13 +01:00
|
|
|
res = super().message_format(format_reply)
|
2021-06-25 10:04:21 +02:00
|
|
|
mail_message_ids = {m.get("id") for m in res if m.get("id")}
|
2016-09-09 18:33:21 +02:00
|
|
|
mail_messages = self.browse(mail_message_ids)
|
2019-10-17 03:28:42 +02:00
|
|
|
tracking_statuses = mail_messages.tracking_status()
|
2021-06-25 10:04:21 +02:00
|
|
|
for message_dict in res:
|
2019-11-18 11:46:59 +01:00
|
|
|
mail_message_id = message_dict.get("id", False)
|
2016-06-14 17:22:17 +02:00
|
|
|
if mail_message_id:
|
2019-10-17 03:28:42 +02:00
|
|
|
message_dict.update(tracking_statuses[mail_message_id])
|
2016-06-14 17:22:17 +02:00
|
|
|
return res
|
2019-07-05 14:20:27 +02:00
|
|
|
|
2019-10-17 03:28:42 +02:00
|
|
|
def _prepare_dict_failed_message(self):
|
|
|
|
"""Preare values to be used by the chatter widget"""
|
|
|
|
self.ensure_one()
|
|
|
|
failed_trackings = self.mail_tracking_ids.filtered(
|
2019-11-18 11:46:59 +01:00
|
|
|
lambda x: x.state in self.get_failed_states()
|
|
|
|
)
|
2021-10-27 18:09:12 +02:00
|
|
|
if not failed_trackings or not self.mail_tracking_needs_action:
|
|
|
|
return
|
2019-11-18 11:46:59 +01:00
|
|
|
failed_partners = failed_trackings.mapped("partner_id")
|
2019-07-05 14:20:27 +02:00
|
|
|
failed_recipients = failed_partners.name_get()
|
2019-10-17 03:28:42 +02:00
|
|
|
if self.author_id:
|
|
|
|
author = self.author_id.name_get()[0]
|
|
|
|
else:
|
2019-11-18 11:46:59 +01:00
|
|
|
author = (-1, _("-Unknown Author-"))
|
2019-07-05 14:20:27 +02:00
|
|
|
return {
|
2019-11-18 11:46:59 +01:00
|
|
|
"id": self.id,
|
|
|
|
"date": self.date,
|
|
|
|
"author": author,
|
|
|
|
"body": self.body,
|
|
|
|
"failed_recipients": failed_recipients,
|
2019-07-05 14:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def get_failed_messages(self):
|
2019-10-17 03:28:42 +02:00
|
|
|
"""Returns the list of failed messages to be used by the
|
2021-05-03 18:53:26 +02:00
|
|
|
failed_messages widget"""
|
2019-11-18 11:46:59 +01:00
|
|
|
return [
|
|
|
|
msg._prepare_dict_failed_message()
|
|
|
|
for msg in self.sorted("date", reverse=True)
|
|
|
|
]
|
2019-07-05 14:20:27 +02:00
|
|
|
|
2019-11-14 18:41:24 +01:00
|
|
|
def set_need_action_done(self):
|
|
|
|
"""Set message tracking action as done
|
2019-10-17 03:28:42 +02:00
|
|
|
|
2019-11-14 18:41:24 +01:00
|
|
|
This will mark them to be ignored in the tracking issues filter.
|
2019-10-17 03:28:42 +02:00
|
|
|
"""
|
2019-11-18 11:46:59 +01:00
|
|
|
self.check_access_rule("read")
|
|
|
|
self.write({"mail_tracking_needs_action": False})
|
2022-03-25 12:31:13 +01:00
|
|
|
self.env["bus.bus"]._sendone(
|
|
|
|
self.env.user.partner_id, "toggle_tracking_status", self.ids
|
2019-11-18 11:46:59 +01:00
|
|
|
)
|
2019-07-05 14:20:27 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def get_failed_count(self):
|
2022-03-24 08:02:53 +01:00
|
|
|
"""Gets the number of failed messages used on discuss mailbox item"""
|
2020-10-02 20:23:03 +02:00
|
|
|
return self.search_count([("is_failed_message", "=", True)])
|
2019-07-05 14:20:27 +02:00
|
|
|
|
|
|
|
@api.model
|
2019-10-17 03:28:42 +02:00
|
|
|
def set_all_as_reviewed(self):
|
2021-05-03 18:53:26 +02:00
|
|
|
"""Sets all messages in the given domain as reviewed.
|
2019-07-05 14:20:27 +02:00
|
|
|
|
2021-05-03 18:53:26 +02:00
|
|
|
Used by Discuss"""
|
2019-10-17 03:28:42 +02:00
|
|
|
|
2020-10-02 20:23:03 +02:00
|
|
|
unreviewed_messages = self.search([("is_failed_message", "=", True)])
|
2019-11-18 11:46:59 +01:00
|
|
|
unreviewed_messages.write({"mail_tracking_needs_action": False})
|
2019-10-17 03:28:42 +02:00
|
|
|
ids = unreviewed_messages.ids
|
|
|
|
|
2019-11-18 11:46:59 +01:00
|
|
|
self.env["bus.bus"].sendone(
|
|
|
|
(self._cr.dbname, "res.partner", self.env.user.partner_id.id),
|
2019-10-17 03:28:42 +02:00
|
|
|
{
|
2019-11-18 11:46:59 +01:00
|
|
|
"type": "toggle_tracking_status",
|
|
|
|
"message_ids": ids,
|
|
|
|
"needs_actions": False,
|
|
|
|
},
|
2019-10-17 03:28:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return ids
|
2021-10-27 18:09:12 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def get_failed_messsage_info(self, ids, model):
|
2021-11-08 12:48:44 +01:00
|
|
|
msg_ids = self.search([("res_id", "=", ids), ("model", "=", model)])
|
|
|
|
res = [
|
|
|
|
msg._prepare_dict_failed_message()
|
|
|
|
for msg in msg_ids.sorted("date", reverse=True)
|
|
|
|
if msg._prepare_dict_failed_message()
|
|
|
|
]
|
2021-10-27 18:09:12 +02:00
|
|
|
return res
|