Fix: Postmark tracking webhook handle SubscriptionChange events

Handle Postmark SubscriptionChange events as Anymail 
unsubscribe, subscribe, or bounce Anymail tracking events.
This commit is contained in:
puru02
2022-06-08 05:58:39 +05:30
committed by GitHub
parent a67a40957a
commit 48de044c9b
3 changed files with 120 additions and 1 deletions

View File

@@ -34,6 +34,7 @@ class PostmarkTrackingWebhookView(PostmarkBaseWebhookView):
'Delivery': EventType.DELIVERED,
'Open': EventType.OPENED,
'SpamComplaint': EventType.COMPLAINED,
'SubscriptionChange': EventType.UNSUBSCRIBED,
'Inbound': EventType.INBOUND, # future, probably
}
@@ -61,6 +62,7 @@ class PostmarkTrackingWebhookView(PostmarkBaseWebhookView):
'InboundError': (EventType.INBOUND_FAILED, None),
'DMARCPolicy': (EventType.REJECTED, RejectReason.BLOCKED),
'TemplateRenderingFailed': (EventType.FAILED, None),
'ManualSuppression': (EventType.UNSUBSCRIBED, RejectReason.UNSUBSCRIBED),
}
def esp_to_anymail_event(self, esp_event):
@@ -87,11 +89,21 @@ class PostmarkTrackingWebhookView(PostmarkBaseWebhookView):
event_type, reject_reason = self.event_types[esp_event['Type']]
except KeyError:
pass
if event_type == EventType.UNSUBSCRIBED:
if esp_event['SuppressSending']:
# Postmark doesn't provide a way to distinguish between
# explicit unsubscribes and bounces
try:
event_type, reject_reason = self.event_types[esp_event['SuppressionReason']]
except KeyError:
pass
else:
event_type, reject_reason = self.event_types['Subscribe']
recipient = getfirst(esp_event, ['Email', 'Recipient'], None) # Email for bounce; Recipient for open
try:
timestr = getfirst(esp_event, ['DeliveredAt', 'BouncedAt', 'ReceivedAt'])
timestr = getfirst(esp_event, ['DeliveredAt', 'BouncedAt', 'ReceivedAt', 'ChangedAt'])
except KeyError:
timestamp = None
else: