Skip to content

Commit

Permalink
Merge pull request #74 from utopia-php/feat-email-extra-params
Browse files Browse the repository at this point in the history
adds expiration message for fcm provider
  • Loading branch information
abnegate authored Jan 10, 2024
2 parents 64eca3f + 57609f2 commit bfb5014
Show file tree
Hide file tree
Showing 27 changed files with 139 additions and 88 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"minimum-stability": "stable",
"scripts": {
"test": "./vendor/bin/phpunit",
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint",
"lint": "./vendor/bin/pint --preset psr12 --test",
"format": "./vendor/bin/pint --preset psr12",
"analyse": "./vendor/bin/phpstan analyse --memory-limit=2G --level=6 src tests"
},
"autoload": {
Expand Down
10 changes: 5 additions & 5 deletions src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ abstract public function getMaxMessagesPerRequest(): int;
* deliveredTo: int,
* type: string,
* results: array<array<string, mixed>>
* }>
* }> GEOSMS adapter returns an array of results keyed by adapter name.
*
* @throws \Exception
*/
public function send(Message $message): array
{
if (! \is_a($message, $this->getMessageType())) {
if (!\is_a($message, $this->getMessageType())) {
throw new \Exception('Invalid message type.');
}
if (\method_exists($message, 'getTo') && \count($message->getTo()) > $this->getMaxMessagesPerRequest()) {
throw new \Exception("{$this->getName()} can only send {$this->getMaxMessagesPerRequest()} messages per request.");
}
if (! \method_exists($this, 'process')) {
if (!\method_exists($this, 'process')) {
throw new \Exception('Adapter does not implement process method.');
}

Expand Down Expand Up @@ -80,7 +80,7 @@ protected function request(
): array {
$ch = \curl_init();

if (! \is_null($body)) {
if (!\is_null($body)) {
$headers[] = 'Content-Length: '.\strlen($body);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ protected function requestMulti(
}

foreach (\array_combine($urls, $bodies) as $url => $body) {
if (! empty($body)) {
if (!empty($body)) {
$headers[] = 'Content-Length: '.\strlen($body);
}

Expand Down
14 changes: 9 additions & 5 deletions src/Utopia/Messaging/Adapter/Chat/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class Discord extends Adapter
{
protected const NAME = 'Discord';
protected const TYPE = 'chat';
protected const MESSAGE_TYPE = DiscordMessage::class;

/**
* @param string $webhookId Your Discord webhook ID.
* @param string $webhookToken Your Discord webhook token.
Expand All @@ -20,17 +24,17 @@ public function __construct(

public function getName(): string
{
return 'Discord';
return static::NAME;
}

public function getType(): string
{
return 'app';
return static::TYPE;
}

public function getMessageType(): string
{
return DiscordMessage::class;
return static::MESSAGE_TYPE;
}

public function getMaxMessagesPerRequest(): int
Expand All @@ -47,10 +51,10 @@ protected function process(DiscordMessage $message): array
{
$query = [];

if (! \is_null($message->getWait())) {
if (!\is_null($message->getWait())) {
$query['wait'] = $message->getWait();
}
if (! \is_null($message->getThreadId())) {
if (!\is_null($message->getThreadId())) {
$query['thread_id'] = $message->getThreadId();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Utopia/Messaging/Adapter/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

abstract class Email extends Adapter
{
protected const TYPE = 'email';
protected const MESSAGE_TYPE = EmailMessage::class;

public function getType(): string
{
return 'email';
return static::TYPE;
}

public function getMessageType(): string
{
return EmailMessage::class;
return static::MESSAGE_TYPE;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Utopia/Messaging/Adapter/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Mailgun extends EmailAdapter
{
protected const NAME = 'Mailgun';

/**
* @param string $apiKey Your Mailgun API key to authenticate with the API.
* @param string $domain Your Mailgun domain to send messages from.
Expand All @@ -24,7 +26,7 @@ public function __construct(
*/
public function getName(): string
{
return 'Mailgun';
return static::NAME;
}

/**
Expand Down Expand Up @@ -53,19 +55,19 @@ protected function process(EmailMessage $message): array
'html' => $message->isHtml() ? $message->getContent() : null,
];

if (! \is_null($message->getCC())) {
if (!\is_null($message->getCC())) {
foreach ($message->getCC() as $cc) {
if (! empty($cc['name'])) {
if (!empty($cc['name'])) {
$body['cc'] = "{$body['cc']},{$cc['name']}<{$cc['email']}>";
} else {
$body['cc'] = "{$body['cc']}, <{$cc['email']}>";
}
}
}

if (! \is_null($message->getBCC())) {
if (!\is_null($message->getBCC())) {
foreach ($message->getBCC() as $bcc) {
if (! empty($bcc['name'])) {
if (!empty($bcc['name'])) {
$body['bcc'] = "{$body['bcc']},{$bcc['name']}<{$bcc['email']}>";
} else {
$body['bcc'] = "{$body['bcc']}, <{$bcc['email']}>";
Expand Down
6 changes: 4 additions & 2 deletions src/Utopia/Messaging/Adapter/Email/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

class Mock extends EmailAdapter
{
protected const NAME = 'Mock';

public function getName(): string
{
return 'Mock';
return static::NAME;
}

public function getMaxMessagesPerRequest(): int
Expand Down Expand Up @@ -48,7 +50,7 @@ protected function process(EmailMessage $message): array
$mail->addAddress($to);
}

if (! $mail->send()) {
if (!$mail->send()) {
foreach ($message->getTo() as $to) {
$response->addResultForRecipient($to, $mail->ErrorInfo);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Utopia/Messaging/Adapter/Email/Sendgrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Sendgrid extends EmailAdapter
{
protected const NAME = 'Sendgrid';

/**
* @param string $apiKey Your Sendgrid API key to authenticate with the API.
* @return void
Expand All @@ -21,7 +23,7 @@ public function __construct(private string $apiKey)
*/
public function getName(): string
{
return 'Sendgrid';
return static::NAME;
}

/**
Expand All @@ -47,9 +49,9 @@ protected function process(EmailMessage $message): array
],
];

if (! \is_null($message->getCC())) {
if (!\is_null($message->getCC())) {
foreach ($message->getCC() as $cc) {
if (! empty($cc['name'])) {
if (!empty($cc['name'])) {
$personalizations[0]['cc'][] = [
'name' => $cc['name'],
'email' => $cc['email'],
Expand All @@ -62,9 +64,9 @@ protected function process(EmailMessage $message): array
}
}

if (! \is_null($message->getBCC())) {
if (!\is_null($message->getBCC())) {
foreach ($message->getBCC() as $bcc) {
if (! empty($bcc['name'])) {
if (!empty($bcc['name'])) {
$personalizations[0]['bcc'][] = [
'name' => $bcc['name'],
'email' => $bcc['email'],
Expand Down
13 changes: 11 additions & 2 deletions src/Utopia/Messaging/Adapter/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@

abstract class Push extends Adapter
{
protected const TYPE = 'push';
protected const MESSAGE_TYPE = PushMessage::class;
protected const EXPIRED_MESSAGE = 'Expired device token.';

public function getType(): string
{
return 'push';
return static::TYPE;
}

public function getMessageType(): string
{
return PushMessage::class;
return static::MESSAGE_TYPE;
}

protected function getExpiredErrorMessage(): string
{
return static::EXPIRED_MESSAGE;
}

/**
Expand Down
23 changes: 7 additions & 16 deletions src/Utopia/Messaging/Adapter/Push/APNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

class APNS extends PushAdapter
{
protected const NAME = 'APNS';

/**
* @return void
*/
Expand All @@ -26,7 +28,7 @@ public function __construct(
*/
public function getName(): string
{
return 'APNS';
return static::NAME;
}

/**
Expand Down Expand Up @@ -103,26 +105,15 @@ public function process(PushMessage $message): array
default:
$response->addResultForRecipient(
$device,
self::getSpecificErrorMessage($result['response']['reason'])
$result['response']['reason'] === 'ExpiredToken' ||
$result['response']['reason'] === 'BadDeviceToken'
? $this->getExpiredErrorMessage()
: $result['response']['reason'],
);
break;
}
}

return $response->toArray();
}

private static function getSpecificErrorMessage(string $error): string
{
return match ($error) {
'MissingDeviceToken' => 'Bad Request. Missing token.',
'BadDeviceToken' => 'Invalid device token.',
'ExpiredToken' => 'Expired device token.',
'PayloadTooLarge' => 'Payload is too large. Messages must be less than 4096 bytes.',
'TooManyRequests' => 'Too many requests were made to the same device token.',
'InternalServerError' => 'Internal server error.',
'PayloadEmpty' => 'Missing payload.',
default => $error,
};
}
}
45 changes: 27 additions & 18 deletions src/Utopia/Messaging/Adapter/Push/FCM.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@

class FCM extends PushAdapter
{
private const DEFAULT_EXPIRY_SECONDS = 3600; // 1 hour

private const DEFAULT_SKEW_SECONDS = 60; // 1 minute

private const GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token';
protected const NAME = 'FCM';
protected const DEFAULT_EXPIRY_SECONDS = 3600; // 1 hour
protected const DEFAULT_SKEW_SECONDS = 60; // 1 minute
protected const GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token';

/**
* @param string $serviceAccountJSON Service account JSON file contents
Expand All @@ -28,7 +27,7 @@ public function __construct(
*/
public function getName(): string
{
return 'FCM';
return static::NAME;
}

/**
Expand Down Expand Up @@ -91,32 +90,32 @@ protected function process(PushMessage $message): array
],
];

if (! \is_null($message->getData())) {
if (!\is_null($message->getData())) {
$shared['message']['data'] = $message->getData();
}
if (! \is_null($message->getAction())) {
if (!\is_null($message->getAction())) {
$shared['message']['android']['notification']['click_action'] = $message->getAction();
$shared['message']['apns']['payload']['aps']['category'] = $message->getAction();
}
if (! \is_null($message->getImage())) {
if (!\is_null($message->getImage())) {
$shared['message']['android']['notification']['image'] = $message->getImage();
$shared['message']['apns']['payload']['aps']['mutable-content'] = 1;
$shared['message']['apns']['fcm_options']['image'] = $message->getImage();
}
if (! \is_null($message->getSound())) {
if (!\is_null($message->getSound())) {
$shared['message']['android']['notification']['sound'] = $message->getSound();
$shared['message']['apns']['payload']['aps']['sound'] = $message->getSound();
}
if (! \is_null($message->getIcon())) {
if (!\is_null($message->getIcon())) {
$shared['message']['android']['notification']['icon'] = $message->getIcon();
}
if (! \is_null($message->getColor())) {
if (!\is_null($message->getColor())) {
$shared['message']['android']['notification']['color'] = $message->getColor();
}
if (! \is_null($message->getTag())) {
if (!\is_null($message->getTag())) {
$shared['message']['android']['notification']['tag'] = $message->getTag();
}
if (! \is_null($message->getBadge())) {
if (!\is_null($message->getBadge())) {
$shared['message']['apns']['payload']['aps']['badge'] = $message->getBadge();
}

Expand Down Expand Up @@ -144,10 +143,20 @@ protected function process(PushMessage $message): array
if ($result['statusCode'] === 200) {
$response->incrementDeliveredTo();
}
$response->addResultForRecipient(
$message->getTo()[$index],
$result['response']['error']['message'] ?? ''
);

if (isset($result['response']['error'])) {
$response->addResultForRecipient(
$message->getTo()[$index],
$result['response']['error']['status'] === 'UNREGISTERED' ||
$result['response']['error']['status'] === 'INVALID_ARGUMENT'
? $this->getExpiredErrorMessage()
: $result['response']['error']['message'] ?? ''
);

continue;
}

$response->addResultForRecipient($message->getTo()[$index]);
}

return $response->toArray();
Expand Down
Loading

0 comments on commit bfb5014

Please sign in to comment.