From 3ebcfe548d56183c2c38989dab41405237686d23 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Thu, 21 Sep 2023 16:26:53 +0530 Subject: [PATCH] Adds get message endpoint --- app/config/errors.php | 7 +++++++ app/controllers/api/messaging.php | 26 +++++++++++++++++++++++++- src/Appwrite/Extend/Exception.php | 3 +++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/config/errors.php b/app/config/errors.php index 8b2ccf810e..8001517d78 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -754,5 +754,12 @@ return [ 'name' => Exception::PROVIDER_INCORRECT_TYPE, 'description' => 'Provider with the requested ID is of incorrect type: ', 'code' => 400, + ], + + /** Message Errors */ + Exception::MESSAGE_NOT_FOUND => [ + 'name' => Exception::MESSAGE_NOT_FOUND, + 'description' => 'Message with the requested ID could not be found.', + 'code' => 404, ] ]; diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index d9a57e03b3..6c50578312 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -75,7 +75,7 @@ App::get('/v1/messaging/providers/:id') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) - ->param('id', null, new UID(), 'Provider ID.') + ->param('id', '', new UID(), 'Provider ID.') ->inject('dbForProject') ->inject('response') ->action(function (string $id, Database $dbForProject, Response $response) { @@ -1293,6 +1293,30 @@ App::delete('/v1/messaging/providers/:id') $response->noContent(); }); +App::get('/v1/messaging/messages/:id') + ->desc('Get Message') + ->groups(['api', 'messaging']) + ->label('scope', 'messages.read') + ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY]) + ->label('sdk.namespace', 'messaging') + ->label('sdk.method', 'getMessage') + ->label('sdk.description', '/docs/references/messaging/get-message.md') + ->label('sdk.response.code', Response::STATUS_CODE_OK) + ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) + ->label('sdk.response.model', Response::MODEL_MESSAGE) + ->param('id', '', new UID(), 'Message ID.') + ->inject('dbForProject') + ->inject('response') + ->action(function (string $id, Database $dbForProject, Response $response) { + $message = $dbForProject->getDocument('message', $id); + + if ($message->isEmpty()) { + throw new Exception(Exception::MESSAGE_NOT_FOUND); + } + + $response->dynamic($message, Response::MODEL_MESSAGE); + }); + App::post('/v1/messaging/messages/email') ->desc('Send an email.') ->groups(['api', 'messaging']) diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 2b10b0cb51..cdd2538abb 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -230,6 +230,9 @@ class Exception extends \Exception public const PROVIDER_ALREADY_EXISTS = 'provider_already_exists'; public const PROVIDER_INCORRECT_TYPE = 'provider_incorrect_type'; + /** Message */ + public const MESSAGE_NOT_FOUND = 'message_not_found'; + protected $type = ''; protected $errors = [];