Skip to content

Commit

Permalink
Don’t log unknown events as warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Dec 29, 2024
1 parent 6cfb424 commit 33088e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Changed the field icons for campaigns, contacts and mailing lists ([#509](https://github.com/putyourlightson/craft-campaign/issues/509)).
- The webhook controller no longer logs unknown events as warnings.

## 3.5.8 - 2024-12-09

Expand Down
77 changes: 29 additions & 48 deletions src/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 33088e3

Please sign in to comment.