From a0858ccc4ec493e88b5f9bf27b1240632f3e409b Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Tue, 26 Sep 2023 13:12:44 +0530 Subject: [PATCH] adds message description, options to mailgun provider --- app/config/collections.php | 22 ++++++++++++++++ app/controllers/api/messaging.php | 25 ++++++++++++++----- composer.lock | 12 ++++----- .../Utopia/Response/Model/Message.php | 7 ++++++ .../Utopia/Response/Model/Provider.php | 17 +++++++++++++ .../e2e/Services/Messaging/MessagingBase.php | 1 + 6 files changed, 72 insertions(+), 12 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index fb7548e260..5506467cc4 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1449,6 +1449,17 @@ $commonCollections = [ 'array' => false, 'filters' => ['json', 'encrypt'], ], + [ + '$id' => ID::custom('options'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['json'], + ], [ '$id' => ID::custom('search'), 'type' => Database::VAR_STRING, @@ -1534,6 +1545,17 @@ $commonCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('description'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('status'), 'type' => Database::VAR_STRING, diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 275051ecb0..45c2760aba 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -108,12 +108,13 @@ App::post('/v1/messaging/providers/mailgun') ->param('name', '', new Text(128), 'Provider name.') ->param('default', false, new Boolean(), 'Set as default provider.', true) ->param('enabled', true, new Boolean(), 'Set as enabled.', true) - ->param('isEuRegion', false, new Boolean(), 'Set as eu region.', true) + ->param('isEuRegion', false, new Boolean(), 'Set as EU region.', true) + ->param('from', '', new Text(256), 'Sender Email Address.') ->param('apiKey', '', new Text(0), 'Mailgun API Key.') ->param('domain', '', new Text(0), 'Mailgun Domain.') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, bool $default, bool $enabled, bool $isEuRegion, string $apiKey, string $domain, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, bool $default, bool $enabled, bool $isEuRegion, string $from, string $apiKey, string $domain, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $provider = new Document([ @@ -129,6 +130,9 @@ App::post('/v1/messaging/providers/mailgun') 'domain' => $domain, 'isEuRegion' => $isEuRegion, ], + 'options' => [ + 'from' => $from, + ] ]); // Check if a default provider exists, if not, set this one as default @@ -167,12 +171,13 @@ App::patch('/v1/messaging/providers/:id/mailgun') ->param('id', '', new UID(), 'Provider ID.') ->param('name', '', new Text(128), 'Provider name.', true) ->param('enabled', null, new Boolean(), 'Set as enabled.', true) - ->param('isEuRegion', false, new Boolean(), 'Set as eu region.', true) + ->param('isEuRegion', null, new Boolean(), 'Set as eu region.', true) + ->param('from', '', new Text(256), 'Sender Email Address.', true) ->param('apiKey', '', new Text(0), 'Mailgun API Key.', true) ->param('domain', '', new Text(0), 'Mailgun Domain.', true) ->inject('dbForProject') ->inject('response') - ->action(function (string $id, string $name, ?bool $enabled, bool $isEuRegion, string $apiKey, string $domain, Database $dbForProject, Response $response) { + ->action(function (string $id, string $name, ?bool $enabled, ?bool $isEuRegion, string $from, string $apiKey, string $domain, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $id); if ($provider->isEmpty()) { @@ -198,6 +203,12 @@ App::patch('/v1/messaging/providers/:id/mailgun') ]); } + if (!empty($from)) { + $provider->setAttribute('options', [ + 'from' => $from, + ]); + } + if ($enabled === true || $enabled === false) { $provider->setAttribute('enabled', $enabled); } @@ -1232,6 +1243,7 @@ App::post('/v1/messaging/messages/email') ->param('providerId', '', new Text(128), 'Email Provider ID.') ->param('to', [], new ArrayList(new Text(65535)), 'List of Topic IDs or List of User IDs or List of Target IDs.') ->param('subject', '', new Text(128), 'Email Subject.') + ->param('description', '', new Text(256), 'Description for Message.') ->param('content', '', new Text(65407), 'Email Content.') ->param('from', '', new Text(128), 'Email from.', true) ->param('html', false, new Boolean(false), 'Is content of type HTML', true) @@ -1239,7 +1251,7 @@ App::post('/v1/messaging/messages/email') ->inject('project') ->inject('messaging') ->inject('response') - ->action(function (string $messageId, string $providerId, array $to, string $subject, string $content, string $from, string $html, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { + ->action(function (string $messageId, string $providerId, array $to, string $subject, string $description, string $content, string $from, string $html, Database $dbForProject, Document $project, Messaging $messaging, Response $response) { $messageId = $messageId == 'unique()' ? ID::unique() : $messageId; $provider = $dbForProject->getDocument('providers', $providerId); @@ -1257,7 +1269,8 @@ App::post('/v1/messaging/messages/email') 'subject' => $subject, 'content' => $content, 'from' => $from, - 'html' => $html + 'html' => $html, + 'description' => $description, ], 'status' => 'processing', 'search' => $subject . ' ' . $from, diff --git a/composer.lock b/composer.lock index 14b07bdce7..e739e122c9 100644 --- a/composer.lock +++ b/composer.lock @@ -1318,16 +1318,16 @@ }, { "name": "psr/http-client", - "version": "1.0.2", + "version": "1.0.3", "source": { "type": "git", "url": "https://github.com/php-fig/http-client.git", - "reference": "0955afe48220520692d2d09f7ab7e0f93ffd6a31" + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-client/zipball/0955afe48220520692d2d09f7ab7e0f93ffd6a31", - "reference": "0955afe48220520692d2d09f7ab7e0f93ffd6a31", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/bb5906edc1c324c9a05aa0873d40117941e5fa90", + "reference": "bb5906edc1c324c9a05aa0873d40117941e5fa90", "shasum": "" }, "require": { @@ -1364,9 +1364,9 @@ "psr-18" ], "support": { - "source": "https://github.com/php-fig/http-client/tree/1.0.2" + "source": "https://github.com/php-fig/http-client" }, - "time": "2023-04-10T20:12:12+00:00" + "time": "2023-09-23T14:17:50+00:00" }, { "name": "psr/http-factory", diff --git a/src/Appwrite/Utopia/Response/Model/Message.php b/src/Appwrite/Utopia/Response/Model/Message.php index 96c8c9b989..2f75997048 100644 --- a/src/Appwrite/Utopia/Response/Model/Message.php +++ b/src/Appwrite/Utopia/Response/Model/Message.php @@ -56,6 +56,13 @@ class Message extends Any 'description' => 'Status of delivery.', 'default' => 'processing', 'example' => 'Message status can be one of the following: processing, sent, failed.', + ]) + ->addRule('description', [ + 'type' => self::TYPE_STRING, + 'description' => 'Message description.', + 'required' => false, + 'default' => '', + 'example' => 'Welcome Email.', ]); } diff --git a/src/Appwrite/Utopia/Response/Model/Provider.php b/src/Appwrite/Utopia/Response/Model/Provider.php index e33e5a253d..552e9783e8 100644 --- a/src/Appwrite/Utopia/Response/Model/Provider.php +++ b/src/Appwrite/Utopia/Response/Model/Provider.php @@ -45,6 +45,23 @@ class Provider extends Model 'description' => 'Type of provider.', 'default' => '', 'example' => 'sms', + ]) + ->addRule('credentials', [ + 'type' => self::TYPE_JSON, + 'description' => 'Provider credentials.', + 'default' => [], + 'example' => [ + 'key' => '123456789' + ], + ]) + ->addRule('options', [ + 'type' => self::TYPE_JSON, + 'description' => 'Provider options.', + 'default' => [], + 'required' => false, + 'example' => [ + 'from' => 'sender-email@mydomain' + ], ]); } diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index 65def00b48..59007cce15 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -19,6 +19,7 @@ trait MessagingBase 'name' => 'Mailgun1', 'apiKey' => 'my-apikey', 'domain' => 'my-domain', + 'from' => 'sender-email@my-domain', ], 'twilio' => [ 'providerId' => 'unique()',