Skip to content

Commit

Permalink
[FIX] mail_tracking_mailgun: wrong failure event mappings
Browse files Browse the repository at this point in the history
Before this patch, both `failed` and `rejected` mailgun events were mapped to `error` state.

However, that was wrong because [`mail_tracking` doesn't have an `error` state][1]. So, `rejected` is now translated into `reject`, and `failed` is translated into `hard_bounce` or `soft_bounce` depending on the severity, as specified by [mailgun docs][2].

Of course, to know the severity, now the method to obtain that info must change, it' can't be a simple dict anymore.

[1]: https://github.com/OCA/social/blob/f73de421e28a43d018176f61725a3a59665f715d/mail_tracking/models/mail_tracking_event.py#L31-L42
[2]: https://documentation.mailgun.com/en/latest/api-events.html#event-types
  • Loading branch information
Jairo Llopis committed Oct 11, 2021
1 parent b108f4b commit 0e28d09
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions mail_tracking_mailgun/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ def _mailgun_mandatory_fields(self):
return ('event', 'timestamp', 'token', 'signature',
'tracking_email_id', 'odoo_db')

@property
def _mailgun_event_type_mapping(self):
return {
# Mailgun event type: tracking event type
@api.model
def _mailgun_event2state(self, event, default="UNKNOWN"):
"""Return the ``mail_tracking`` equivalent event
Args:
event: Mailgun event response from API.
default: Value to return when not found.
"""
# Mailgun event type: tracking event type
equivalents = {
'delivered': 'delivered',
'opened': 'open',
'clicked': 'click',
Expand All @@ -44,14 +50,17 @@ def _mailgun_event_type_mapping(self):
'bounced': 'hard_bounce',
'dropped': 'reject',
'accepted': 'sent',
'failed': 'error',
'rejected': 'error',
"failed": (
"hard_bounce" if event.get("severity") == "permanent" else "soft_bounce"
),
"rejected": "reject",
}
return equivalents.get(event.get("event"), default)

def _mailgun_event_type_verify(self, event):
event = event or {}
mailgun_event_type = event.get('event')
if mailgun_event_type not in self._mailgun_event_type_mapping:
if self._mailgun_event2state(event) == "UNKNOWN":
_logger.error("Mailgun: event type '%s' not supported",
mailgun_event_type)
return False
Expand Down Expand Up @@ -194,8 +203,7 @@ def event_process(self, request, post, metadata, event_type=None):
res = 'OK'
if res == 'OK':
mailgun_event_type = post.get('event')
mapped_event_type = self._mailgun_event_type_mapping.get(
mailgun_event_type) or event_type
mapped_event_type = self._mailgun_event2state(post, event_type)
if not mapped_event_type: # pragma: no cover
res = 'ERROR: Bad event'
tracking = self._mailgun_tracking_get(post)
Expand Down Expand Up @@ -251,8 +259,9 @@ def action_manual_check_mailgun(self):
[('mailgun_id', '=', item["id"])]) and (
item.get("recipient", "") ==
email_split(tracking.recipient)[0]):
mapped_event_type = self._mailgun_event_type_mapping.get(
item["event"], item["event"])
mapped_event_type = self._mailgun_event2state(
item, item["event"]
)
metadata = self._mailgun_metadata(
mapped_event_type, item, {})
tracking.event_create(mapped_event_type, metadata)

0 comments on commit 0e28d09

Please sign in to comment.