-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don’t log unknown events as warnings
- Loading branch information
Showing
2 changed files
with
30 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,16 +108,16 @@ public function actionAmazonSes(): ?Response | |
$body = Json::decodeIfJson($message['Message']); | ||
$eventType = $body['notificationType'] ?? null; | ||
|
||
if ($eventType == 'Complaint') { | ||
if ($eventType === 'Complaint') { | ||
$email = $body['complaint']['complainedRecipients'][0]['emailAddress']; | ||
return $this->callWebhook('complained', $email); | ||
} | ||
if ($eventType == 'Bounce' && $body['bounce']['bounceType'] == 'Permanent') { | ||
if ($eventType === 'Bounce' && $body['bounce']['bounceType'] === 'Permanent') { | ||
$email = $body['bounce']['bouncedRecipients'][0]['emailAddress']; | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
|
||
return $this->asRawFailure('Event `' . ($eventType ?? '') . '` not found.'); | ||
return $this->asRawSuccess(); | ||
} | ||
|
||
return $this->asRawFailure('No event provided.'); | ||
|
@@ -145,23 +145,19 @@ public function actionMailersend(): Response|string | |
|
||
// Check if this is a test webhook request | ||
$from = $events['data']['email']['from'] ?? ''; | ||
if ($from == '[email protected]') { | ||
if ($from === '[email protected]') { | ||
return $this->asRawSuccess('Test success.'); | ||
} | ||
|
||
if ($eventType == 'activity.spam_complaint') { | ||
if ($eventType === 'activity.spam_complaint') { | ||
return $this->callWebhook('complained', $email); | ||
} | ||
|
||
if ($eventType == 'activity.hard_bounced') { | ||
if ($eventType === 'activity.hard_bounced') { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
|
||
if ($eventType) { | ||
return $this->asRawFailure('Event type `' . $eventType . '` not found.'); | ||
} | ||
|
||
return $this->asRawFailure('No event provided.'); | ||
return $this->asRawSuccess(); | ||
} | ||
|
||
/** | ||
|
@@ -198,32 +194,28 @@ public function actionMailgun(): ?Response | |
} | ||
|
||
// Check if this is a test webhook request | ||
if ($email == '[email protected]') { | ||
if ($email === '[email protected]') { | ||
return $this->asRawSuccess('Test success.'); | ||
} | ||
|
||
if ($event == 'complained') { | ||
if ($event === 'complained') { | ||
return $this->callWebhook('complained', $email); | ||
} | ||
|
||
// Only mark as bounced if the reason indicates that it is a hard bounce. | ||
// https://github.com/putyourlightson/craft-campaign/issues/178 | ||
if ($event == 'failed' && $severity == 'permanent' | ||
&& ($reason == 'bounce' || $reason == 'suppress-bounce') | ||
if ($event === 'failed' && $severity === 'permanent' | ||
&& ($reason === 'bounce' || $reason === 'suppress-bounce') | ||
) { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
|
||
// Support legacy Mailgun webhook event. | ||
if ($event == 'bounced') { | ||
if ($event === 'bounced') { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
|
||
if ($event) { | ||
return $this->asRawFailure('Event `' . $event . '` not found.'); | ||
} | ||
|
||
return $this->asRawFailure('No event provided.'); | ||
return $this->asRawSuccess(); | ||
} | ||
|
||
/** | ||
|
@@ -241,10 +233,10 @@ public function actionMandrill(): ?Response | |
$eventType = $event['event'] ?? ''; | ||
$email = $event['msg']['email'] ?? ''; | ||
|
||
if ($eventType == 'spam') { | ||
if ($eventType === 'spam') { | ||
return $this->callWebhook('complained', $email); | ||
} | ||
if ($eventType == 'hard_bounce') { | ||
if ($eventType === 'hard_bounce') { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
} | ||
|
@@ -279,40 +271,33 @@ public function actionPostmark(): ?Response | |
$email = $this->request->getBodyParam('Email') ?: $this->request->getBodyParam('Recipient'); | ||
|
||
// https://postmarkapp.com/developer/webhooks/spam-complaint-webhook | ||
if ($eventType == 'SpamComplaint') { | ||
if ($eventType === 'SpamComplaint') { | ||
return $this->callWebhook('complained', $email); | ||
} // https://postmarkapp.com/developer/webhooks/bounce-webhook | ||
elseif ($eventType == 'Bounce') { | ||
elseif ($eventType === 'Bounce') { | ||
$bounceType = $this->request->getBodyParam('Type'); | ||
|
||
if ($bounceType == 'HardBounce') { | ||
if ($bounceType === 'HardBounce') { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
|
||
// Append the bounce type for debugging purposes. | ||
$eventType .= '.' . $bounceType; | ||
} // https://postmarkapp.com/developer/webhooks/subscription-change-webhook | ||
elseif ($eventType == 'SubscriptionChange') { | ||
elseif ($eventType === 'SubscriptionChange') { | ||
$suppress = $this->request->getBodyParam('SuppressSending'); | ||
|
||
if ($suppress) { | ||
$reason = $this->request->getBodyParam('SuppressionReason'); | ||
|
||
if ($reason == 'SpamComplaint') { | ||
if ($reason === 'SpamComplaint') { | ||
return $this->callWebhook('complained', $email); | ||
} elseif ($reason == 'HardBounce') { | ||
} elseif ($reason === 'HardBounce') { | ||
return $this->callWebhook('bounced', $email); | ||
} else { | ||
return $this->callWebhook('unsubscribed', $email); | ||
} | ||
} | ||
} | ||
|
||
if ($eventType) { | ||
return $this->asRawFailure('Event `' . $eventType . '` not found.'); | ||
} | ||
|
||
return $this->asRawFailure('No event provided.'); | ||
return $this->asRawSuccess(); | ||
} | ||
|
||
/** | ||
|
@@ -335,27 +320,23 @@ public function actionSendgrid(): ?Response | |
$email = $event['email'] ?? ''; | ||
|
||
// Check if this is a test webhook request | ||
if ($email == '[email protected]') { | ||
if ($email === '[email protected]') { | ||
return $this->asRawSuccess('Test success.'); | ||
} | ||
|
||
// https://docs.sendgrid.com/for-developers/tracking-events/event#engagement-events | ||
if ($eventType == 'spamreport') { | ||
if ($eventType === 'spamreport') { | ||
return $this->callWebhook('complained', $email); | ||
} | ||
|
||
// https://docs.sendgrid.com/for-developers/tracking-events/event#delivery-events | ||
if ($eventType == 'bounce') { | ||
if ($eventType === 'bounce') { | ||
return $this->callWebhook('bounced', $email); | ||
} | ||
} | ||
|
||
$eventTypes = array_filter(array_map(fn($event) => $event['event'] ?? null, $events)); | ||
|
||
return $this->asRawFailure('Event `' . implode(', ', $eventTypes) . '` not found.'); | ||
} | ||
|
||
return $this->asRawFailure('No event provided.'); | ||
return $this->asRawSuccess(); | ||
} | ||
|
||
/** | ||
|
@@ -375,11 +356,11 @@ private function callWebhook(string $event, string $email = null): Response | |
return $this->asRawSuccess(); | ||
} | ||
|
||
if ($event == 'complained') { | ||
if ($event === 'complained') { | ||
Campaign::$plugin->webhook->complain($contact); | ||
} elseif ($event == 'bounced') { | ||
} elseif ($event === 'bounced') { | ||
Campaign::$plugin->webhook->bounce($contact); | ||
} elseif ($event == 'unsubscribed') { | ||
} elseif ($event === 'unsubscribed') { | ||
Campaign::$plugin->webhook->unsubscribe($contact); | ||
} | ||
|
||
|