diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 753abc8432..c08d772ef4 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -2,11 +2,15 @@ use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Database\Validator\Queries\Providers; use Appwrite\Utopia\Response; use Utopia\App; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Helpers\ID; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Datetime; @@ -96,13 +100,22 @@ App::post('/v1/messaging/providers/mailgun') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('apiKey', '', new Text(0), 'Mailgun API Key.') ->param('domain', '', new Text(0), 'Mailgun Domain.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $apiKey, string $domain, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $apiKey, string $domain, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'mailgun', 'type' => 'email', @@ -184,12 +197,21 @@ App::post('/v1/messaging/providers/sendgrid') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('apiKey', '', new Text(0), 'Sendgrid API key.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $apiKey, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $apiKey, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'sendgrid', 'type' => 'email', @@ -265,13 +287,22 @@ App::post('/v1/messaging/providers/msg91') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('senderId', '', new Text(0), 'Msg91 Sender ID.') ->param('authKey', '', new Text(0), 'Msg91 Auth Key.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $senderId, string $authKey, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $senderId, string $authKey, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'msg91', 'type' => 'sms', @@ -353,13 +384,22 @@ App::post('/v1/messaging/providers/telesign') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('username', '', new Text(0), 'Telesign username.') ->param('password', '', new Text(0), 'Telesign password.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $username, string $password, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $username, string $password, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'telesign', 'type' => 'sms', @@ -441,13 +481,22 @@ App::post('/v1/messaging/providers/textmagic') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('username', '', new Text(0), 'Textmagic username.') ->param('apiKey', '', new Text(0), 'Textmagic apiKey.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $username, string $apiKey, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $username, string $apiKey, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'text-magic', 'type' => 'sms', @@ -529,13 +578,22 @@ App::post('/v1/messaging/providers/twilio') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('accountSid', '', new Text(0), 'Twilio account secret ID.') ->param('authToken', '', new Text(0), 'Twilio authentication token.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $accountSid, string $authToken, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $accountSid, string $authToken, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'twilio', 'type' => 'sms', @@ -617,13 +675,22 @@ App::post('/v1/messaging/providers/vonage') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('apiKey', '', new Text(0), 'Vonage API key.') ->param('apiSecret', '', new Text(0), 'Vonage API secret.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $apiKey, string $apiSecret, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $apiKey, string $apiSecret, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'vonage', 'type' => 'sms', @@ -708,12 +775,21 @@ App::post('/v1/messaging/providers/fcm') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('serverKey', '', new Text(0), 'FCM Server Key.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $serverKey, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $serverKey, Document $user, Database $dbForProject, Response $response) { +$id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'fcm', 'type' => 'push', @@ -784,16 +860,25 @@ App::post('/v1/messaging/providers/apns') ->label('sdk.response.code', Response::STATUS_CODE_CREATED) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_PROVIDER) + ->param('id', '', new CustomId(), 'Provider 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('name', '', new Text(128), 'Provider name.') ->param('authKey', '', new Text(0), 'APNS authentication key.') ->param('authKeyId', '', new Text(0), 'APNS authentication key ID.') ->param('teamId', '', new Text(0), 'APNS team ID.') ->param('bundleId', '', new Text(0), 'APNS bundle ID.') ->param('endpoint', '', new Text(0), 'APNS endpoint.') + ->inject('user') ->inject('dbForProject') ->inject('response') - ->action(function (string $name, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Database $dbForProject, Response $response) { - $provider = $dbForProject->createDocument('providers', new Document([ + ->action(function (string $id, string $name, string $authKey, string $authKeyId, string $teamId, string $bundleId, string $endpoint, Document $user, Database $dbForProject, Response $response) { + $id = $id == 'unique()' ? ID::unique() : $id; + $provider = $dbForProject->createDocument('providers', new Document([ + '$id' => $id, + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user($user->getId())), + Permission::delete(Role::user($user->getId())), + ], 'name' => $name, 'provider' => 'apns', 'type' => 'push', diff --git a/app/init.php b/app/init.php index a71df23bf0..0b5b75ee92 100644 --- a/app/init.php +++ b/app/init.php @@ -569,7 +569,10 @@ Database::addFilter( Query::limit(APP_LIMIT_SUBQUERY), ])) ); - return $database->find('topics', [Query::equal('$id', $topicIds)]); + if (\count($topicIds) > 0) { + return $database->find('topics', [Query::equal('$id', $topicIds)]); + } + return []; } ); @@ -586,7 +589,10 @@ Database::addFilter( Query::limit(APP_LIMIT_SUBQUERY), ])) ); - return $database->find('targets', [Query::equal('$id', $targetIds)]); + if (\count($targetIds) > 0) { + return $database->find('targets', [Query::equal('$id', $targetIds)]); + } + return []; } ); diff --git a/tests/e2e/Services/Messaging/MessagingServerTest.php b/tests/e2e/Services/Messaging/MessagingServerTest.php index 7b93ab29ac..0724b6dd2c 100644 --- a/tests/e2e/Services/Messaging/MessagingServerTest.php +++ b/tests/e2e/Services/Messaging/MessagingServerTest.php @@ -16,44 +16,53 @@ class MessagingServerTest extends Scope { $providersParams = [ 'sendgrid' => [ + 'id' => 'unique()', 'name' => 'Sengrid1', 'apiKey' => 'my-apikey', ], 'mailgun' => [ + 'id' => 'unique()', 'name' => 'Mailgun1', 'apiKey' => 'my-apikey', 'domain' => 'my-domain', ], 'twilio' => [ + 'id' => 'unique()', 'name' => 'Twilio1', 'accountSid' => 'my-accountSid', 'authToken' => 'my-authToken', ], 'telesign' => [ + 'id' => 'unique()', 'name' => 'Telesign1', 'username' => 'my-username', 'password' => 'my-password', ], 'textmagic' => [ + 'id' => 'unique()', 'name' => 'Textmagic1', 'username' => 'my-username', 'apiKey' => 'my-apikey', ], 'msg91' => [ + 'id' => 'unique()', 'name' => 'Ms91-1', 'senderId' => 'my-senderid', 'authKey' => 'my-authkey', ], 'vonage' => [ + 'id' => 'unique()', 'name' => 'Vonage1', 'apiKey' => 'my-apikey', 'apiSecret' => 'my-apisecret', ], 'fcm' => [ + 'id' => 'unique()', 'name' => 'FCM1', 'serverKey' => 'my-serverkey', ], 'apns' => [ + 'id' => 'unique()', 'name' => 'APNS1', 'authKey' => 'my-authkey', 'authKeyId' => 'my-authkeyid', @@ -65,11 +74,11 @@ class MessagingServerTest extends Scope $providers = []; foreach (\array_keys($providersParams) as $key) { - $response = $this->client->call(Client::METHOD_POST, '/messaging/providers/' . $key, [ + $response = $this->client->call(Client::METHOD_POST, '/messaging/providers/' . $key, \array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'], - ], $providersParams[$key]); + ],$this->getHeaders()), $providersParams[$key]); \array_push($providers, $response['body']); $this->assertEquals(201, $response['headers']['status-code']); $this->assertEquals($providersParams[$key]['name'], $response['body']['name']);