From db9e7b0199b39f27dfb009446a9c79cf86986ab7 Mon Sep 17 00:00:00 2001 From: prateek banga Date: Fri, 20 Oct 2023 13:28:23 +0530 Subject: [PATCH] review changes --- app/config/errors.php | 5 +++ app/controllers/api/messaging.php | 65 +++++++++++++++++++++---------- src/Appwrite/Extend/Exception.php | 1 + 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 5514e8e68e..07741eddac 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -790,5 +790,10 @@ return [ 'name' => Exception::MESSAGE_ALREADY_SENT, 'description' => 'Message with the requested ID has already been sent.', 'code' => 400, + ], + Exception::MESSAGE_ALREADY_SCHEDULED => [ + 'name' => Exception::MESSAGE_ALREADY_SCHEDULED, + 'description' => 'Message with the requested ID has already been scheduled for delivery.', + 'code' => 400, ] ]; diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 8735cf57de..ae048020ac 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -1580,8 +1580,8 @@ App::post('/v1/messaging/messages/email') ->param('providerId', '', new UID(), 'Email Provider ID.') ->param('to', [], new ArrayList(new Text(Database::LENGTH_KEY)), 'List of Topic IDs or List of User IDs or List of Target IDs.') ->param('subject', '', new Text(998), 'Email Subject.') - ->param('description', '', new Text(256), 'Description for Message.', true) ->param('content', '', new Text(64230), 'Email Content.') + ->param('description', '', new Text(256), 'Description for Message.', true) ->param('status', 'processing', new WhiteList(['draft', 'processing']), 'Message Status. Value must be either draft or processing.', true) ->param('html', false, new Boolean(), 'Is content of type HTML', true) ->param('deliveryTime', null, new DatetimeValidator(requireDateInFuture: true), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.', true) @@ -1589,15 +1589,19 @@ App::post('/v1/messaging/messages/email') ->inject('project') ->inject('messaging') ->inject('response') - ->action(function (string $messageId, string $providerId, array $to, string $subject, string $description, string $content, string $status, bool $html, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { + ->action(function (string $messageId, string $providerId, array $to, string $subject, string $content, string $description, string $status, bool $html, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { $messageId = $messageId == 'unique()' ? ID::unique() : $messageId; $provider = $dbForProject->getDocument('providers', $providerId); - if ($provider->isEmpty() || $provider->getAttribute('type') !== 'email') { + if ($provider->isEmpty()) { throw new Exception(Exception::PROVIDER_NOT_FOUND); } + if ($provider->getAttribute('type') !== 'email') { + throw new Exception(Exception::PROVIDER_INCORRECT_TYPE); + } + $message = $dbForProject->createDocument('messages', new Document([ '$id' => $messageId, 'providerId' => $provider->getId(), @@ -1633,7 +1637,7 @@ App::post('/v1/messaging/messages/email') }); App::post('/v1/messaging/messages/sms') - ->desc('Create an sms.') + ->desc('Create an SMS.') ->groups(['api', 'messaging']) ->label('audits.event', 'messages.create') ->label('audits.resource', 'messages/{response.$id}') @@ -1648,23 +1652,27 @@ App::post('/v1/messaging/messages/sms') ->param('messageId', '', new CustomId(), 'Message ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('providerId', '', new UID(), 'SMS Provider ID.') ->param('to', [], new ArrayList(new Text(Database::LENGTH_KEY)), 'List of Topic IDs or List of User IDs or List of Target IDs.') - ->param('description', '', new Text(256), 'Description for Message.', true) ->param('content', '', new Text(64230), 'SMS Content.') + ->param('description', '', new Text(256), 'Description for Message.', true) ->param('status', 'processing', new WhiteList(['draft', 'processing']), 'Message Status. Value must be either draft or processing.', true) ->param('deliveryTime', null, new DatetimeValidator(requireDateInFuture: true), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.', true) ->inject('dbForProject') ->inject('project') ->inject('messaging') ->inject('response') - ->action(function (string $messageId, string $providerId, array $to, string $description, string $content, string $status, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { + ->action(function (string $messageId, string $providerId, array $to, string $content, string $description, string $status, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { $messageId = $messageId == 'unique()' ? ID::unique() : $messageId; $provider = $dbForProject->getDocument('providers', $providerId); - if ($provider->isEmpty() || $provider->getAttribute('type') !== 'sms') { + if ($provider->isEmpty()) { throw new Exception(Exception::PROVIDER_NOT_FOUND); } + if ($provider->getAttribute('type') !== 'sms') { + throw new Exception(Exception::PROVIDER_INCORRECT_TYPE); + } + $message = $dbForProject->createDocument('messages', new Document([ '$id' => $messageId, 'providerId' => $provider->getId(), @@ -1713,31 +1721,35 @@ App::post('/v1/messaging/messages/push') ->param('messageId', '', new CustomId(), 'Message ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('providerId', '', new UID(), 'Push Provider ID.') ->param('to', [], new ArrayList(new Text(Database::LENGTH_KEY)), 'List of Topic IDs or List of User IDs or List of Target IDs.') - ->param('description', '', new Text(256), 'Description for Message.', true) ->param('title', '', new Text(256), 'Title for push notification.') ->param('body', '', new Text(64230), 'Body for push notification.') + ->param('description', '', new Text(256), 'Description for Message.', true) ->param('data', null, new JSON(), 'Additional Data for push notification.', true) ->param('action', '', new Text(256), 'Action for push notification.', true) - ->param('icon', '', new Text(256), 'Icon for push notification.', true) - ->param('sound', '', new Text(256), 'Sound for push notification.', true) - ->param('color', '', new Text(256), 'Color for push notification.', true) - ->param('tag', '', new Text(256), 'Tag for push notification.', true) - ->param('badge', '', new Text(256), 'Badge for push notification.', true) + ->param('icon', '', new Text(256), 'Icon for push notification. Available only for Android and Web Platform.', true) + ->param('sound', '', new Text(256), 'Sound for push notification. Available only for Android and IOS Platform.', true) + ->param('color', '', new Text(256), 'Color for push notification. Available only for Android Platform.', true) + ->param('tag', '', new Text(256), 'Tag for push notification. Available only for Android Platform.', true) + ->param('badge', '', new Text(256), 'Badge for push notification. Available only for IOS Platform.', true) ->param('status', 'processing', new WhiteList(['draft', 'processing']), 'Message Status. Value must be either draft or processing.', true) ->param('deliveryTime', null, new DatetimeValidator(requireDateInFuture: true), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.', true) ->inject('dbForProject') ->inject('project') ->inject('messaging') ->inject('response') - ->action(function (string $messageId, string $providerId, array $to, string $description, string $title, string $body, ?array $data, string $action, string $icon, string $sound, string $color, string $tag, string $badge, string $status, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { + ->action(function (string $messageId, string $providerId, array $to, string $title, string $body, string $description, ?array $data, string $action, string $icon, string $sound, string $color, string $tag, string $badge, string $status, ?string $deliveryTime, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { $messageId = $messageId == 'unique()' ? ID::unique() : $messageId; $provider = $dbForProject->getDocument('providers', $providerId); - if ($provider->isEmpty() || $provider->getAttribute('type') !== 'push') { + if ($provider->isEmpty()) { throw new Exception(Exception::PROVIDER_NOT_FOUND); } + if ($provider->getAttribute('type') !== 'push') { + throw new Exception(Exception::PROVIDER_INCORRECT_TYPE); + } + $pushData = [ 'title' => $title, 'body' => $body, @@ -1900,6 +1912,10 @@ App::patch('/v1/messaging/messages/email/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_SENT); } + if (!is_null($message->getAttribute('deliveryTime')) && is_null($deliveryTime) && $message->getAttribute('deliveryTime') < new \DateTime()) { + throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); + } + if (\count($to) > 0) { $message->setAttribute('to', $to); } @@ -1983,6 +1999,10 @@ App::patch('/v1/messaging/messages/sms/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_SENT); } + if (!is_null($message->getAttribute('deliveryTime')) && is_null($deliveryTime) && $message->getAttribute('deliveryTime') < new \DateTime()) { + throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); + } + if (\count($to) > 0) { $message->setAttribute('to', $to); } @@ -2044,12 +2064,11 @@ App::patch('/v1/messaging/messages/push/:messageId') ->param('body', '', new Text(64230), 'Body for push notification.', true) ->param('data', null, new JSON(), 'Additional Data for push notification.', true) ->param('action', '', new Text(256), 'Action for push notification.', true) - ->param('icon', '', new Text(256), 'Icon for push notification.', true) - ->param('sound', '', new Text(256), 'Sound for push notification.', true) - ->param('color', '', new Text(256), 'Color for push notification.', true) - ->param('tag', '', new Text(256), 'Tag for push notification.', true) - ->param('badge', '', new Text(256), 'Badge for push notification.', true) - ->param('status', 'processing', new WhiteList(['draft', 'processing']), 'Message Status. Value must be either draft or processing.', true) + ->param('icon', '', new Text(256), 'Icon for push notification. Available only for Android and Web Platform.', true) + ->param('sound', '', new Text(256), 'Sound for push notification. Available only for Android and IOS Platform.', true) + ->param('color', '', new Text(256), 'Color for push notification. Available only for Android Platform.', true) + ->param('tag', '', new Text(256), 'Tag for push notification. Available only for Android Platform.', true) + ->param('badge', '', new Text(256), 'Badge for push notification. Available only for IOS Platform.', true) ->param('status', 'processing', new WhiteList(['draft', 'processing']), 'Message Status. Value must be either draft or processing.', true) ->param('deliveryTime', null, new DatetimeValidator(requireDateInFuture: true), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.', true) ->inject('dbForProject') ->inject('project') @@ -2066,6 +2085,10 @@ App::patch('/v1/messaging/messages/push/:messageId') throw new Exception(Exception::MESSAGE_ALREADY_SENT); } + if (!is_null($message->getAttribute('deliveryTime')) && is_null($deliveryTime) && $message->getAttribute('deliveryTime') < new \DateTime()) { + throw new Exception(Exception::MESSAGE_ALREADY_SCHEDULED); + } + if (\count($to) > 0) { $message->setAttribute('to', $to); } diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index ee17ccef0c..6650accb9d 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -241,6 +241,7 @@ class Exception extends \Exception /** Message */ public const MESSAGE_NOT_FOUND = 'message_not_found'; public const MESSAGE_ALREADY_SENT = 'message_already_sent'; + public const MESSAGE_ALREADY_SCHEDULED = 'message_already_scheduled'; protected $type = ''; protected $errors = [];