diff --git a/src/Connector/Event.php b/src/Connector/Event.php index d884a82..e79be66 100644 --- a/src/Connector/Event.php +++ b/src/Connector/Event.php @@ -18,6 +18,38 @@ class Event extends Base */ protected $results = []; + /** + * Given a MailgunEvent model, check if the message linked to the event is delivered + */ + public function isDelivered(MailgunEvent $event): bool + { + return $this->isDeliveredMessage((string)$event->MessageId, (string)$event->Recipient); + } + + /** + * Given a message id and recipient, check if the message linked to the event is delivered + */ + public function isDeliveredMessage(string $msgId, string $recipient): bool + { + + if($msgId === '') { + throw new \UnexpectedValueException("Empty message id when checking isDelivered"); + } + + // poll for delivered events, MG stores them for up to 30 days + $timeframe = 'now -30 days'; + $begin = Base::DateTime($timeframe); + $event_filter = MailgunEvent::DELIVERED; + $extra_params = [ + 'limit' => 25, + 'message-id' => $msgId, + 'recipient' => $recipient + ]; + + $events = $this->pollEvents($begin, $event_filter, $extra_params); + return $events !== []; + } + /** * @param string $begin an RFC 2822 formatted UTC datetime OR empty string for no begin datetime * @param string $event_filter see https://documentation.mailgun.com/en/latest/api-events.html#event-types can also be a filter expression e.g "failed OR rejected" diff --git a/src/Connector/Message.php b/src/Connector/Message.php index 40b2c41..c9bc09e 100644 --- a/src/Connector/Message.php +++ b/src/Connector/Message.php @@ -8,7 +8,6 @@ use NSWDPC\Messaging\Mailgun\Jobs\SendJob; use NSWDPC\Messaging\Mailgun\Models\MailgunEvent; use NSWDPC\Messaging\Mailgun\Services\Logger; -use NSWDPC\Messaging\Mailgun\Connector\Event as EventConnector; use SilverStripe\Assets\Folder; use SilverStripe\Assets\File; use SilverStripe\Security\Group; @@ -210,34 +209,6 @@ protected function queueAndSend(string $domain, array $parameters, mixed $in): ? } } - /** - * Lookup all events for the submission linked to this event - */ - public function isDelivered(MailgunEvent $event, $cleanup = true): bool - { - - // Query will be for this MessageId and a delivered status - if (empty($event->MessageId)) { - throw new \Exception("Tried to query a message based on MailgunEvent #{$event->ID} with no linked MessageId"); - } - - // poll for delivered events, MG stores them for up to 30 days - // @TODO an API sending key in the DSN cannot be used here as it can only send emails - $connector = EventConnector::create($this->dsn); - $timeframe = 'now -30 days'; - $begin = Base::DateTime($timeframe); - - $event_filter = MailgunEvent::DELIVERED;// no we don't want to resubmit - $extra_params = [ - 'limit' => 25, - 'message-id' => $event->MessageId, - 'recipient' => $event->Recipient,// match against the recipient of the event - ]; - - $events = $connector->pollEvents($begin, $event_filter, $extra_params); - return $events !== []; - } - /** * Trim < and > from message id * @param string $message_id