2023-08-21 22:44:50 +12:00
< ? php
2023-10-18 06:23:26 +13:00
use Appwrite\Event\Delete ;
2023-11-01 06:47:58 +13:00
use Appwrite\Event\Event ;
2023-09-06 22:10:56 +12:00
use Appwrite\Event\Messaging ;
2023-08-22 04:45:15 +12:00
use Appwrite\Extend\Exception ;
2023-10-26 06:33:23 +13:00
use Appwrite\Network\Validator\Email ;
2023-09-06 22:10:56 +12:00
use Appwrite\Permission ;
use Appwrite\Role ;
2023-08-25 08:15:17 +12:00
use Appwrite\Utopia\Database\Validator\CustomId ;
2023-10-14 01:56:54 +13:00
use Appwrite\Utopia\Database\Validator\Queries\Messages ;
2023-08-23 04:45:31 +12:00
use Appwrite\Utopia\Database\Validator\Queries\Providers ;
2023-10-18 20:49:21 +13:00
use Appwrite\Utopia\Database\Validator\Queries\Subscribers ;
2023-08-25 08:15:17 +12:00
use Appwrite\Utopia\Database\Validator\Queries\Topics ;
2023-08-21 22:44:50 +12:00
use Appwrite\Utopia\Response ;
use Utopia\App ;
use Utopia\Database\Database ;
use Utopia\Database\Document ;
2023-08-29 03:11:05 +12:00
use Utopia\Database\Exception\Duplicate as DuplicateException ;
2023-08-25 09:02:51 +12:00
use Utopia\Database\Helpers\ID ;
2023-08-23 04:45:31 +12:00
use Utopia\Database\Query ;
use Utopia\Database\Validator\Authorization ;
2023-09-06 22:10:56 +12:00
use Utopia\Database\Validator\Datetime as DatetimeValidator ;
2023-08-23 04:45:31 +12:00
use Utopia\Database\Validator\UID ;
2023-08-22 04:45:15 +12:00
use Utopia\Validator\ArrayList ;
2023-08-30 04:59:48 +12:00
use Utopia\Validator\Boolean ;
2023-10-19 20:59:48 +13:00
use Utopia\Validator\JSON ;
2023-08-21 22:44:50 +12:00
use Utopia\Validator\Text ;
2023-09-06 05:10:48 +12:00
use Utopia\Validator\WhiteList ;
2023-08-21 22:44:50 +12:00
App :: post ( '/v1/messaging/providers/mailgun' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Mailgun provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createMailgunProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-mailgun-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> param ( 'providerId' , '' , 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Email (), 'Sender email address.' )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Mailgun API Key.' )
-> param ( 'domain' , '' , new Text ( 0 ), 'Mailgun Domain.' )
2023-10-26 07:25:45 +13:00
-> param ( 'isEuRegion' , false , new Boolean (), 'Set as EU region.' )
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $apiKey , string $domain , bool $isEuRegion , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'mailgun' ,
'type' => 'email' ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'mailgun' . ' ' . 'email' ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
'domain' => $domain ,
2023-09-19 22:06:40 +12:00
'isEuRegion' => $isEuRegion ,
2023-08-24 21:01:22 +12:00
],
2023-09-26 20:42:44 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'email' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/sendgrid' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Sendgrid provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createSendgridProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-sengrid-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> param ( 'providerId' , '' , 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender email address.' )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Sendgrid API key.' )
2023-09-14 00:17:38 +12:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $apiKey , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'sendgrid' ,
'type' => 'email' ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'sendgrid' . ' ' . 'email' ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
],
2023-10-24 08:35:46 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/msg91' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Msg91 provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createMsg91Provider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-msg91-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:16:15 +12:00
-> param ( 'providerId' , '' , 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' )
2023-08-24 21:01:22 +12:00
-> param ( 'senderId' , '' , new Text ( 0 ), 'Msg91 Sender ID.' )
-> param ( 'authKey' , '' , new Text ( 0 ), 'Msg91 Auth Key.' )
2023-10-26 07:25:45 +13:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $senderId , string $authKey , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'msg91' ,
'type' => 'sms' ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'msg91' . ' ' . 'sms' ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'senderId' => $senderId ,
'authKey' => $authKey ,
],
2023-09-28 02:28:00 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-04 23:45:59 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-06 00:27:48 +13:00
2023-08-23 04:45:31 +12:00
App :: post ( '/v1/messaging/providers/telesign' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Telesign provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createTelesignProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-telesign-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> param ( 'providerId' , '' , 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' )
2023-08-24 21:01:22 +12:00
-> param ( 'username' , '' , new Text ( 0 ), 'Telesign username.' )
-> param ( 'password' , '' , new Text ( 0 ), 'Telesign password.' )
2023-10-24 08:35:46 +13:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $username , string $password , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-08-24 21:01:22 +12:00
'provider' => 'telesign' ,
'type' => 'sms' ,
2023-09-19 22:06:40 +12:00
'search' => $providerId . ' ' . $name . ' ' . 'telesign' . ' ' . 'sms' ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'username' => $username ,
'password' => $password ,
],
2023-10-24 08:35:46 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/textmagic' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create TextMagic provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-26 06:33:23 +13:00
-> label ( 'sdk.method' , 'createTextMagicProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-textmagic-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-04 23:45:59 +13:00
-> param ( 'providerId' , '' , 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.' )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' )
2023-10-04 23:45:59 +13:00
-> param ( 'username' , '' , new Text ( 0 ), 'Textmagic username.' )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Textmagic apiKey.' )
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $username , string $apiKey , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-04 23:45:59 +13:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'text-magic' ,
'type' => 'sms' ,
'search' => $providerId . ' ' . $name . ' ' . 'text-magic' . ' ' . 'sms' ,
'enabled' => $enabled ,
'credentials' => [
'username' => $username ,
'apiKey' => $apiKey ,
],
2023-10-24 08:35:46 +13:00
'options' => [
'from' => $from ,
]
2023-10-04 23:45:59 +13:00
]);
2023-08-24 21:01:22 +12:00
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-10-04 23:45:59 +13:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-10-04 23:45:59 +13:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-24 21:01:22 +12:00
}
2023-10-04 23:45:59 +13:00
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-09-14 00:17:38 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-10-04 23:45:59 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: post ( '/v1/messaging/providers/twilio' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Twilio provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-10-04 23:45:59 +13:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createTwilioProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-twilio-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'providerId' , '' , 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.' )
2023-10-24 08:35:46 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' )
2023-10-04 23:45:59 +13:00
-> param ( 'accountSid' , '' , new Text ( 0 ), 'Twilio account secret ID.' )
-> param ( 'authToken' , '' , new Text ( 0 ), 'Twilio authentication token.' )
2023-10-24 08:35:46 +13:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $accountSid , string $authToken , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-04 23:45:59 +13:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'twilio' ,
'type' => 'sms' ,
'search' => $providerId . ' ' . $name . ' ' . 'twilio' . ' ' . 'sms' ,
'enabled' => $enabled ,
'credentials' => [
'accountSid' => $accountSid ,
'authToken' => $authToken ,
],
2023-10-24 08:35:46 +13:00
'options' => [
'from' => $from ,
]
2023-10-04 23:45:59 +13:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-10-04 23:45:59 +13:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-10-04 23:45:59 +13:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-10-04 23:45:59 +13:00
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
2023-08-24 21:01:22 +12:00
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
2023-10-04 23:45:59 +13:00
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
2023-08-24 21:01:22 +12:00
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/vonage' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create Vonage provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'createVonageProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-vonage-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-08-25 11:13:40 +12:00
-> param ( 'providerId' , '' , 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.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' )
2023-10-24 08:35:46 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' )
2023-10-04 23:45:59 +13:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Vonage API key.' )
-> param ( 'apiSecret' , '' , new Text ( 0 ), 'Vonage API secret.' )
2023-10-24 08:35:46 +13:00
-> param ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , string $from , string $apiKey , string $apiSecret , bool $enabled , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:16:15 +12:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
2023-08-30 04:59:48 +12:00
$provider = new Document ([
2023-08-25 11:16:15 +12:00
'$id' => $providerId ,
'name' => $name ,
2023-10-04 23:45:59 +13:00
'provider' => 'vonage' ,
2023-08-24 21:01:22 +12:00
'type' => 'sms' ,
2023-10-04 23:45:59 +13:00
'search' => $providerId . ' ' . $name . ' ' . 'vonage' . ' ' . 'sms' ,
2023-09-14 00:17:38 +12:00
'enabled' => $enabled ,
2023-08-24 21:01:22 +12:00
'credentials' => [
'apiKey' => $apiKey ,
2023-10-04 23:45:59 +13:00
'apiSecret' => $apiSecret ,
2023-08-24 21:01:22 +12:00
],
2023-10-24 08:35:46 +13:00
'options' => [
'from' => $from ,
]
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-04 23:45:59 +13:00
Query :: equal ( 'type' , [ 'sms' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-08-30 04:59:48 +12:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-22 04:45:15 +12:00
2023-10-04 23:45:59 +13:00
App :: post ( '/v1/messaging/providers/fcm' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create FCM provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-10-04 23:45:59 +13:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'createFcmProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-fcm-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'providerId' , '' , 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 ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
2023-10-26 06:33:23 +13:00
-> param ( 'serverKey' , '' , new Text ( 0 ), 'FCM server key.' )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , bool $enabled , string $serverKey , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-04 23:45:59 +13:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'fcm' ,
'type' => 'push' ,
'search' => $providerId . ' ' . $name . ' ' . 'fcm' . ' ' . 'push' ,
'enabled' => $enabled ,
'credentials' => [
'serverKey' => $serverKey ,
2023-08-24 21:01:22 +12:00
],
2023-08-30 04:59:48 +12:00
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-08-30 04:59:48 +12:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'push' ])
2023-08-30 04:59:48 +12:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-10-04 23:45:59 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: post ( '/v1/messaging/providers/apns' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Create APNS provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].create' )
2023-10-04 23:45:59 +13:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'createApnsProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/create-apns-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
-> param ( 'providerId' , '' , 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 ( 'enabled' , true , new Boolean (), 'Set as enabled.' , true )
-> 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.' )
2023-11-01 06:47:58 +13:00
-> inject ( 'queueForEvents' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 06:47:58 +13:00
-> action ( function ( string $providerId , string $name , bool $enabled , string $authKey , string $authKeyId , string $teamId , string $bundleId , string $endpoint , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-04 23:45:59 +13:00
$providerId = $providerId == 'unique()' ? ID :: unique () : $providerId ;
$provider = new Document ([
'$id' => $providerId ,
'name' => $name ,
'provider' => 'apns' ,
'type' => 'push' ,
'search' => $providerId . ' ' . $name . ' ' . 'apns' . ' ' . 'push' ,
'enabled' => $enabled ,
'credentials' => [
'authKey' => $authKey ,
'authKeyId' => $authKeyId ,
'teamId' => $teamId ,
'bundleId' => $bundleId ,
'endpoint' => $endpoint ,
],
]);
2023-10-31 07:07:57 +13:00
// Check if a internal provider exists, if not, set this one as internal
2023-10-04 23:45:59 +13:00
if (
empty ( $dbForProject -> findOne ( 'providers' , [
2023-10-31 07:07:57 +13:00
Query :: equal ( 'internal' , [ true ]),
2023-10-06 00:27:48 +13:00
Query :: equal ( 'type' , [ 'push' ])
2023-10-04 23:45:59 +13:00
]))
) {
2023-10-31 07:07:57 +13:00
$provider -> setAttribute ( 'internal' , true );
2023-10-04 23:45:59 +13:00
}
try {
$provider = $dbForProject -> createDocument ( 'providers' , $provider );
} catch ( DuplicateException ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_ALREADY_EXISTS );
2023-10-04 23:45:59 +13:00
}
2023-11-01 06:47:58 +13:00
$queueForEvents
2023-11-01 07:23:46 +13:00
-> setParam ( 'providerId' , $provider -> getId ());
2023-11-01 06:47:58 +13:00
2023-10-04 23:45:59 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
App :: get ( '/v1/messaging/providers' )
2023-10-26 06:33:23 +13:00
-> desc ( 'List providers' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'listProviders' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-providers.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER_LIST )
-> param ( 'queries' , [], new Providers (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Providers :: ALLOWED_ATTRIBUTES ), true )
2023-10-26 06:33:23 +13:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-26 07:25:45 +13:00
-> action ( function ( array $queries , string $search , Database $dbForProject , Response $response ) {
2023-10-04 23:45:59 +13:00
$queries = Query :: parseQueries ( $queries );
2023-10-26 06:33:23 +13:00
if ( ! empty ( $search )) {
$queries [] = Query :: search ( 'search' , $search );
}
2023-10-04 23:45:59 +13:00
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$providerId = $cursor -> getValue ();
2023-10-18 06:23:26 +13:00
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'providers' , $providerId ));
2023-10-04 23:45:59 +13:00
2023-10-18 06:23:26 +13:00
if ( $cursorDocument -> isEmpty ()) {
2023-10-04 23:45:59 +13:00
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Provider ' { $providerId } ' for the 'cursor' value not found. " );
}
2023-10-07 02:53:46 +13:00
$cursor -> setValue ( $cursorDocument );
2023-10-04 23:45:59 +13:00
}
$response -> dynamic ( new Document ([
'providers' => $dbForProject -> find ( 'providers' , $queries ),
2023-10-26 06:33:23 +13:00
'total' => $dbForProject -> count ( 'providers' , $queries , APP_LIMIT_COUNT ),
2023-10-04 23:45:59 +13:00
]), Response :: MODEL_PROVIDER_LIST );
});
2023-10-06 11:00:47 +13:00
App :: get ( '/v1/messaging/providers/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Get provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'providers.read' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'getProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/get-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-06 11:00:47 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-06 11:00:47 +13:00
-> action ( function ( string $providerId , Database $dbForProject , Response $response ) {
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$response -> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/mailgun/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Mailgun provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateMailgunProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-mailgun-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'isEuRegion' , null , new Boolean (), 'Set as eu region.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender email address.' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Mailgun API Key.' , true )
-> param ( 'domain' , '' , new Text ( 0 ), 'Mailgun Domain.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , ? bool $isEuRegion , string $from , string $apiKey , string $domain , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-10-04 23:45:59 +13:00
if ( $providerAttr !== 'mailgun' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'mailgun' . ' ' . 'email' );
}
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( $isEuRegion === true || $isEuRegion === false ) {
$credentials [ 'isEuRegion' ] = $isEuRegion ;
}
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
}
if ( ! empty ( $domain )) {
$credentials [ 'domain' ] = $domain ;
2023-08-24 21:01:22 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
2023-08-24 21:01:22 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/sendgrid/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Sendgrid provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateSendgridProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-sendgrid-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Sendgrid API key.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender email address.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $apiKey , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'sendgrid' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'sendgrid' . ' ' . 'email' );
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-10-04 23:45:59 +13:00
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiKey )) {
$provider -> setAttribute ( 'credentials' , [
'apiKey' => $apiKey ,
]);
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-10-04 23:45:59 +13:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/msg91/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Msg91 provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 22:03:13 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-10-04 23:45:59 +13:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateMsg91Provider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-msg91-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'senderId' , '' , new Text ( 0 ), 'Msg91 Sender ID.' , true )
-> param ( 'authKey' , '' , new Text ( 0 ), 'Msg91 Auth Key.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $senderId , string $authKey , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'msg91' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'msg91' . ' ' . 'sms' );
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-10-04 23:45:59 +13:00
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $senderId )) {
$credentials [ 'senderId' ] = $senderId ;
}
if ( ! empty ( $authKey )) {
$credentials [ 'authKey' ] = $authKey ;
}
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-10-04 23:45:59 +13:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/telesign/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Telesign provider' )
2023-10-04 23:45:59 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-10-04 23:45:59 +13:00
-> label ( 'scope' , 'providers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateTelesignProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-telesign-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-10-04 23:45:59 +13:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'username' , '' , new Text ( 0 ), 'Telesign username.' , true )
-> param ( 'password' , '' , new Text ( 0 ), 'Telesign password.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $username , string $password , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-10-04 23:45:59 +13:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'telesign' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-10-04 23:45:59 +13:00
}
if ( ! empty ( $name )) {
$provider -> setAttribute ( 'name' , $name );
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'telesign' . ' ' . 'sms' );
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-10-04 23:45:59 +13:00
if ( $enabled === true || $enabled === false ) {
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $username )) {
$credentials [ 'username' ] = $username ;
}
if ( ! empty ( $password )) {
$credentials [ 'password' ] = $password ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-22 04:45:15 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/textmagic/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update TextMagic provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-26 06:33:23 +13:00
-> label ( 'sdk.method' , 'updateTextMagicProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/update-textmagic-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'username' , '' , new Text ( 0 ), 'Textmagic username.' , true )
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Textmagic apiKey.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $username , string $apiKey , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-08-24 21:01:22 +12:00
if ( $providerAttr !== 'text-magic' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'textmagic' . ' ' . 'sms' );
2023-08-24 21:01:22 +12:00
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( ! empty ( $username )) {
$credentials [ 'username' ] = $username ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/twilio/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Twilio provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateTwilioProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-twilio-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'accountSid' , null , new Text ( 0 ), 'Twilio account secret ID.' , true )
-> param ( 'authToken' , null , new Text ( 0 ), 'Twilio authentication token.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $accountSid , string $authToken , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-08-24 21:01:22 +12:00
if ( $providerAttr !== 'twilio' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'twilio' . ' ' . 'sms' );
2023-08-24 21:01:22 +12:00
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( ! empty ( $accountSid )) {
$credentials [ 'accountSid' ] = $accountSid ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $authToken )) {
$credentials [ 'authToken' ] = $authToken ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/vonage/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update Vonage provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.method' , 'updateVonageProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-vonage-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'apiKey' , '' , new Text ( 0 ), 'Vonage API key.' , true )
-> param ( 'apiSecret' , '' , new Text ( 0 ), 'Vonage API secret.' , true )
2023-10-26 07:25:45 +13:00
-> param ( 'from' , '' , new Text ( 256 ), 'Sender number.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $apiKey , string $apiSecret , string $from , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'vonage' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 08:23:52 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'vonage' . ' ' . 'sms' );
2023-08-24 21:01:22 +12:00
}
2023-10-24 08:35:46 +13:00
if ( ! empty ( $from )) {
$provider -> setAttribute ( 'options' , [
'from' => $from ,
]);
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
2023-08-30 04:59:48 +12:00
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiKey )) {
$credentials [ 'apiKey' ] = $apiKey ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $apiSecret )) {
$credentials [ 'apiSecret' ] = $apiSecret ;
2023-08-30 04:59:48 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/fcm/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update FCM provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'updateFcmProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/update-fcm-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'serverKey' , '' , new Text ( 0 ), 'FCM Server Key.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $serverKey , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$providerAttr = $provider -> getAttribute ( 'provider' );
if ( $providerAttr !== 'fcm' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'fcm' . ' ' . 'push' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
if ( ! empty ( $serverKey )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'credentials' , [ 'serverKey' => $serverKey ]);
}
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-07 02:53:46 +13:00
App :: patch ( '/v1/messaging/providers/apns/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Update APNS provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 06:33:23 +13:00
-> label ( 'audits.event' , 'provider.update' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'provider/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].update' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-05 09:42:04 +13:00
-> label ( 'sdk.method' , 'updateApnsProvider' )
2023-10-04 23:45:59 +13:00
-> label ( 'sdk.description' , '/docs/references/messaging/update-apns-provider.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_PROVIDER )
2023-10-07 02:53:46 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-08-24 21:01:22 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Provider name.' , true )
2023-09-20 22:25:22 +12:00
-> param ( 'enabled' , null , new Boolean (), 'Set as enabled.' , true )
2023-10-31 07:07:57 +13:00
-> param ( 'internal' , null , new Boolean (), 'Set as internal. Internal providers are used in services other than Messaging service such as Authentication service' , true )
2023-08-24 21:01:22 +12:00
-> param ( 'authKey' , '' , new Text ( 0 ), 'APNS authentication key.' , true )
-> param ( 'authKeyId' , '' , new Text ( 0 ), 'APNS authentication key ID.' , true )
-> param ( 'teamId' , '' , new Text ( 0 ), 'APNS team ID.' , true )
-> param ( 'bundleId' , '' , new Text ( 0 ), 'APNS bundle ID.' , true )
-> param ( 'endpoint' , '' , new Text ( 0 ), 'APNS endpoint.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , string $name , ? bool $enabled , ? bool $internal , string $authKey , string $authKeyId , string $teamId , string $bundleId , string $endpoint , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
2023-08-24 08:23:52 +12:00
}
2023-08-24 21:01:22 +12:00
$providerAttr = $provider -> getAttribute ( 'provider' );
2023-08-24 08:23:52 +12:00
2023-08-24 21:01:22 +12:00
if ( $providerAttr !== 'apns' ) {
2023-10-06 00:27:48 +13:00
throw new Exception ( Exception :: PROVIDER_INCORRECT_TYPE );
2023-08-24 21:01:22 +12:00
}
2023-08-24 08:23:52 +12:00
2023-09-20 22:25:22 +12:00
if ( ! empty ( $name )) {
2023-08-24 21:01:22 +12:00
$provider -> setAttribute ( 'name' , $name );
2023-09-19 22:06:40 +12:00
$provider -> setAttribute ( 'search' , $provider -> getId () . ' ' . $name . ' ' . 'apns' . ' ' . 'push' );
2023-08-24 21:01:22 +12:00
}
2023-09-19 22:06:40 +12:00
if ( $enabled === true || $enabled === false ) {
2023-09-14 00:17:38 +12:00
$provider -> setAttribute ( 'enabled' , $enabled );
}
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$provider -> setAttribute ( 'internal' , $internal );
}
2023-10-04 23:45:59 +13:00
$credentials = $provider -> getAttribute ( 'credentials' );
if ( ! empty ( $authKey )) {
$credentials [ 'authKey' ] = $authKey ;
}
if ( ! empty ( $authKeyId )) {
$credentials [ 'authKeyId' ] = $authKeyId ;
}
if ( ! empty ( $teamId )) {
$credentials [ 'teamId' ] = $teamId ;
}
if ( ! empty ( $bundleId )) {
$credentials [ 'bundle' ] = $bundleId ;
}
if ( ! empty ( $endpoint )) {
$credentials [ 'endpoint' ] = $endpoint ;
2023-08-24 21:01:22 +12:00
}
2023-10-04 23:45:59 +13:00
$provider -> setAttribute ( 'credentials' , $credentials );
2023-08-24 21:01:22 +12:00
$provider = $dbForProject -> updateDocument ( 'providers' , $provider -> getId (), $provider );
2023-10-31 07:07:57 +13:00
if ( $internal === true ) {
$internalProvider = $dbForProject -> findOne ( 'providers' , [
'internal' => true ,
'type' => 'email' ,
]);
$internalProvider -> setAttribute ( 'internal' , false );
$dbForProject -> updateDocument ( 'providers' , $internalProvider -> getId (), $internalProvider );
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> dynamic ( $provider , Response :: MODEL_PROVIDER );
});
2023-08-23 04:45:31 +12:00
2023-10-06 11:00:47 +13:00
App :: delete ( '/v1/messaging/providers/:providerId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Delete provider' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'provider.delete' )
-> label ( 'audits.resource' , 'provider/{request.id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'providers.[providerId].delete' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'providers.write' )
2023-09-21 22:48:20 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'deleteProvider' )
-> label ( 'sdk.description' , '/docs/references/messaging/delete-provider.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2023-10-06 11:00:47 +13:00
-> param ( 'providerId' , '' , new UID (), 'Provider ID.' )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $providerId , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-06 11:00:47 +13:00
$provider = $dbForProject -> getDocument ( 'providers' , $providerId );
2023-08-24 21:01:22 +12:00
if ( $provider -> isEmpty ()) {
throw new Exception ( Exception :: PROVIDER_NOT_FOUND );
}
$dbForProject -> deleteDocument ( 'providers' , $provider -> getId ());
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'providerId' , $provider -> getId ());
2023-10-17 11:11:42 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_NOCONTENT )
-> noContent ();
2023-08-24 21:01:22 +12:00
});
2023-08-23 04:45:31 +12:00
2023-10-05 00:57:56 +13:00
App :: post ( '/v1/messaging/topics' )
-> desc ( 'Create a topic.' )
-> groups ([ 'api' , 'messaging' ])
2023-11-01 07:23:46 +13:00
-> label ( 'audits.event' , 'topic.[topicId].create' )
2023-10-26 21:16:45 +13:00
-> label ( 'audits.resource' , 'topic/{response.$id}' )
2023-11-01 22:03:13 +13:00
-> label ( 'event' , 'topics.[topicId].create' )
2023-10-05 00:57:56 +13:00
-> label ( 'scope' , 'topics.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createTopic' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-topic.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TOPIC )
-> param ( 'topicId' , '' , new CustomId (), 'Topic ID. Choose a custom Topic ID or a new Topic ID.' )
-> param ( 'name' , '' , new Text ( 128 ), 'Topic Name.' )
-> param ( 'description' , '' , new Text ( 2048 ), 'Topic Description.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-05 00:57:56 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $topicId , string $name , string $description , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-05 00:57:56 +13:00
$topicId = $topicId == 'unique()' ? ID :: unique () : $topicId ;
$topic = new Document ([
'$id' => $topicId ,
'name' => $name ,
]);
if ( $description ) {
$topic -> setAttribute ( 'description' , $description );
2023-10-31 08:42:31 +13:00
$topic -> setAttribute ( 'search' , $topic -> getId () . ' ' . $name . ' ' . $description );
} else {
$topic -> setAttribute ( 'search' , $topic -> getId () . ' ' . $name );
2023-10-05 00:57:56 +13:00
}
try {
$topic = $dbForProject -> createDocument ( 'topics' , $topic );
} catch ( DuplicateException ) {
throw new Exception ( Exception :: TOPIC_ALREADY_EXISTS );
}
2023-10-14 01:56:54 +13:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'topicId' , $topic -> getId ());
2023-10-14 01:56:54 +13:00
$response
2023-10-18 06:23:26 +13:00
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $topic , Response :: MODEL_TOPIC );
2023-10-05 00:57:56 +13:00
});
2023-08-25 08:15:17 +12:00
App :: get ( '/v1/messaging/topics' )
-> desc ( 'List topics.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'topics.read' )
2023-08-25 11:06:41 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-25 08:15:17 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'listTopics' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-topics.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TOPIC_LIST )
-> param ( 'queries' , [], new Topics (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Topics :: ALLOWED_ATTRIBUTES ), true )
2023-10-26 06:33:23 +13:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
2023-08-25 08:15:17 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-26 06:33:23 +13:00
-> action ( function ( array $queries , string $search , Database $dbForProject , Response $response ) {
2023-08-25 08:15:17 +12:00
$queries = Query :: parseQueries ( $queries );
2023-10-26 06:33:23 +13:00
if ( ! empty ( $search )) {
$queries [] = Query :: search ( 'search' , $search );
}
2023-08-25 08:15:17 +12:00
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$topicId = $cursor -> getValue ();
2023-10-18 06:23:26 +13:00
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'topics' , $topicId ));
2023-08-25 08:15:17 +12:00
2023-10-18 06:23:26 +13:00
if ( $cursorDocument -> isEmpty ()) {
2023-08-25 08:15:17 +12:00
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Topic ' { $topicId } ' for the 'cursor' value not found. " );
}
$cursor -> setValue ( $cursorDocument [ 0 ]);
}
$response -> dynamic ( new Document ([
2023-08-25 11:06:41 +12:00
'topics' => $dbForProject -> find ( 'topics' , $queries ),
2023-10-26 06:33:23 +13:00
'total' => $dbForProject -> count ( 'topics' , $queries , APP_LIMIT_COUNT ),
2023-08-25 08:15:17 +12:00
]), Response :: MODEL_TOPIC_LIST );
});
2023-08-29 03:11:05 +12:00
App :: get ( '/v1/messaging/topics/:topicId' )
2023-08-25 08:15:17 +12:00
-> desc ( 'Get a topic.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'topics.read' )
2023-08-25 11:06:41 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-25 08:15:17 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'getTopic' )
-> label ( 'sdk.description' , '/docs/references/messaging/get-topic.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TOPIC )
2023-08-29 03:11:05 +12:00
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
2023-08-25 08:15:17 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-08-29 03:11:05 +12:00
-> action ( function ( string $topicId , Database $dbForProject , Response $response ) {
$topic = $dbForProject -> getDocument ( 'topics' , $topicId );
2023-08-25 08:15:17 +12:00
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
2023-08-29 03:11:05 +12:00
$topic = $dbForProject -> getDocument ( 'topics' , $topicId );
2023-08-25 08:15:17 +12:00
$response
-> dynamic ( $topic , Response :: MODEL_TOPIC );
});
2023-08-25 11:06:41 +12:00
App :: patch ( '/v1/messaging/topics/:topicId' )
2023-08-25 08:15:17 +12:00
-> desc ( 'Update a topic.' )
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'topic.update' )
-> label ( 'audits.resource' , 'topic/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'topics.[topicId].update' )
2023-08-25 08:15:17 +12:00
-> label ( 'scope' , 'topics.write' )
2023-08-25 11:06:41 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-25 08:15:17 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateTopic' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-topic.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_TOPIC )
2023-08-25 11:06:41 +12:00
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
2023-08-25 08:15:17 +12:00
-> param ( 'name' , '' , new Text ( 128 ), 'Topic Name.' , true )
2023-10-18 06:23:26 +13:00
-> param ( 'description' , '' , new Text ( 2048 ), 'Topic Description.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-25 08:15:17 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $topicId , string $name , string $description , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-25 11:06:41 +12:00
$topic = $dbForProject -> getDocument ( 'topics' , $topicId );
2023-08-25 08:15:17 +12:00
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
2023-10-18 06:23:26 +13:00
if ( ! empty ( $name )) {
2023-08-25 08:15:17 +12:00
$topic -> setAttribute ( 'name' , $name );
}
2023-10-18 06:23:26 +13:00
if ( ! empty ( $description )) {
2023-08-25 08:15:17 +12:00
$topic -> setAttribute ( 'description' , $description );
}
2023-10-31 08:42:31 +13:00
if ( ! empty ( $name ) || ! empty ( $description )) {
if ( ! empty ( $name ) && ! empty ( $description )) {
$topic -> setAttribute ( 'search' , $topic -> getId () . ' ' . $name . ' ' . $description );
} elseif ( ! empty ( $name )) {
$topic -> setAttribute ( 'search' , $topic -> getId () . ' ' . $name . ' ' . $topic -> getAttribute ( 'description' ));
} else {
$topic -> setAttribute ( 'search' , $topic -> getId () . ' ' . $topic -> getAttribute ( 'name' ) . ' ' . $description );
}
}
2023-08-25 11:06:41 +12:00
$topic = $dbForProject -> updateDocument ( 'topics' , $topicId , $topic );
2023-08-25 08:15:17 +12:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'topicId' , $topic -> getId ());
2023-08-25 08:15:17 +12:00
$response
-> dynamic ( $topic , Response :: MODEL_TOPIC );
});
2023-08-25 11:06:41 +12:00
App :: delete ( '/v1/messaging/topics/:topicId' )
2023-08-25 08:15:17 +12:00
-> desc ( 'Delete a topic.' )
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'topic.delete' )
-> label ( 'audits.resource' , 'topic/{request.topicId}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'topics.[topicId].delete' )
2023-08-25 08:15:17 +12:00
-> label ( 'scope' , 'topics.write' )
2023-08-25 11:06:41 +12:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-25 08:15:17 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'deleteTopic' )
-> label ( 'sdk.description' , '/docs/references/messaging/delete-topic.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
2023-08-25 11:06:41 +12:00
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-25 08:15:17 +12:00
-> inject ( 'dbForProject' )
2023-10-21 00:32:13 +13:00
-> inject ( 'queueForDeletes' )
2023-08-25 08:15:17 +12:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $topicId , Event $queueForEvents , Database $dbForProject , Delete $queueForDeletes , Response $response ) {
2023-08-25 11:06:41 +12:00
$topic = $dbForProject -> getDocument ( 'topics' , $topicId );
2023-08-25 08:15:17 +12:00
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
2023-10-18 06:23:26 +13:00
$dbForProject -> deleteDocument ( 'topics' , $topicId );
2023-10-21 00:32:13 +13:00
$queueForDeletes
2023-10-26 06:33:23 +13:00
-> setType ( DELETE_TYPE_TOPIC )
2023-10-18 06:23:26 +13:00
-> setDocument ( $topic );
2023-10-17 11:11:42 +13:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'topicId' , $topic -> getId ());
2023-10-17 11:11:42 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_NOCONTENT )
-> noContent ();
2023-08-25 08:15:17 +12:00
});
2023-10-05 00:57:56 +13:00
App :: post ( '/v1/messaging/topics/:topicId/subscribers' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Adds a subscriber to a topic.' )
2023-10-05 00:57:56 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'subscriber.create' )
-> label ( 'audits.resource' , 'subscriber/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'topics.[topicId].subscribers.[subscriberId].create' )
2023-10-05 00:57:56 +13:00
-> label ( 'scope' , 'subscribers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_JWT , APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-14 01:56:54 +13:00
-> label ( 'sdk.method' , 'createSubscriber' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-subscriber.md' )
2023-10-05 00:57:56 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_SUBSCRIBER )
-> param ( 'subscriberId' , '' , new CustomId (), 'Subscriber ID. Choose a custom Topic ID or a new Topic ID.' )
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
-> param ( 'targetId' , '' , new UID (), 'Target ID.' )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-05 00:57:56 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $subscriberId , string $topicId , string $targetId , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-10-05 00:57:56 +13:00
$subscriberId = $subscriberId == 'unique()' ? ID :: unique () : $subscriberId ;
$topic = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'topics' , $topicId ));
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
$target = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'targets' , $targetId ));
if ( $target -> isEmpty ()) {
throw new Exception ( Exception :: USER_TARGET_NOT_FOUND );
}
$subscriber = new Document ([
'$id' => $subscriberId ,
'$permissions' => [
Permission :: read ( Role :: user ( $target -> getAttribute ( 'userId' ))),
Permission :: delete ( Role :: user ( $target -> getAttribute ( 'userId' ))),
],
'topicId' => $topicId ,
'topicInternalId' => $topic -> getInternalId (),
'targetId' => $targetId ,
'targetInternalId' => $target -> getInternalId (),
]);
try {
$subscriber = $dbForProject -> createDocument ( 'subscribers' , $subscriber );
2023-10-26 20:07:39 +13:00
Authorization :: skip ( fn () => $dbForProject -> increaseDocumentAttribute ( 'topics' , $topicId , 'total' , 1 ));
2023-10-05 00:57:56 +13:00
} catch ( DuplicateException ) {
throw new Exception ( Exception :: SUBSCRIBER_ALREADY_EXISTS );
}
2023-10-14 01:56:54 +13:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'topicId' , $topic -> getId ())
-> setParam ( 'subscriberId' , $subscriber -> getId ());
2023-10-14 01:56:54 +13:00
$response
2023-10-18 06:23:26 +13:00
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $subscriber , Response :: MODEL_SUBSCRIBER );
2023-10-05 00:57:56 +13:00
});
2023-08-29 03:11:05 +12:00
App :: get ( '/v1/messaging/topics/:topicId/subscribers' )
-> desc ( 'List topic\'s subscribers.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'subscribers.read' )
2023-10-14 01:56:54 +13:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-29 03:11:05 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'listSubscribers' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-subscribers.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_SUBSCRIBER_LIST )
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
2023-10-18 20:49:21 +13:00
-> param ( 'queries' , [], new Subscribers (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Providers :: ALLOWED_ATTRIBUTES ), true )
2023-08-29 03:11:05 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-18 20:49:21 +13:00
-> action ( function ( string $topicId , array $queries , Database $dbForProject , Response $response ) {
$queries = Query :: parseQueries ( $queries );
2023-08-29 03:11:05 +12:00
$topic = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'topics' , $topicId ));
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
2023-10-18 20:49:21 +13:00
\array_push ( $queries , Query :: equal ( 'topicInternalId' , [ $topic -> getInternalId ()]));
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$subscriberId = $cursor -> getValue ();
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'subscribers' , $subscriberId ));
if ( $cursorDocument -> isEmpty ()) {
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Subscriber ' { $subscriberId } ' for the 'cursor' value not found. " );
}
$cursor -> setValue ( $cursorDocument );
}
2023-08-29 03:11:05 +12:00
$response
-> dynamic ( new Document ([
2023-10-18 20:49:21 +13:00
'subscribers' => $dbForProject -> find ( 'subscribers' , $queries ),
2023-10-26 06:33:23 +13:00
'total' => $dbForProject -> count ( 'subscribers' , $queries , APP_LIMIT_COUNT ),
2023-08-29 03:11:05 +12:00
]), Response :: MODEL_SUBSCRIBER_LIST );
2023-08-29 03:13:18 +12:00
});
2023-08-29 03:11:05 +12:00
App :: get ( '/v1/messaging/topics/:topicId/subscriber/:subscriberId' )
-> desc ( 'Get a topic\'s subscriber.' )
-> groups ([ 'api' , 'messaging' ])
-> label ( 'scope' , 'subscribers.read' )
2023-10-14 01:56:54 +13:00
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
2023-08-29 03:11:05 +12:00
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'getSubscriber' )
-> label ( 'sdk.description' , '/docs/references/messaging/get-subscriber.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_SUBSCRIBER )
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
-> param ( 'subscriberId' , '' , new UID (), 'Subscriber ID.' )
-> inject ( 'dbForProject' )
-> inject ( 'response' )
-> action ( function ( string $topicId , string $subscriberId , Database $dbForProject , Response $response ) {
$topic = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'topics' , $topicId ));
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
2023-08-29 03:13:18 +12:00
2023-08-29 03:11:05 +12:00
$subscriber = $dbForProject -> getDocument ( 'subscribers' , $subscriberId );
2023-08-29 03:13:18 +12:00
if ( $subscriber -> isEmpty () || $subscriber -> getAttribute ( 'topicId' ) !== $topicId ) {
2023-08-29 03:11:05 +12:00
throw new Exception ( Exception :: SUBSCRIBER_NOT_FOUND );
}
$response
-> dynamic ( $subscriber , Response :: MODEL_SUBSCRIBER );
});
App :: delete ( '/v1/messaging/topics/:topicId/subscriber/:subscriberId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Delete a subscriber from a topic.' )
2023-08-29 03:11:05 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'subscriber.delete' )
-> label ( 'audits.resource' , 'subscriber/{request.subscriberId}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'topics.[topicId].subscribers.[subscriberId].delete' )
2023-08-29 03:11:05 +12:00
-> label ( 'scope' , 'subscribers.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_JWT , APP_AUTH_TYPE_SESSION , APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'deleteSubscriber' )
-> label ( 'sdk.description' , '/docs/references/messaging/delete-subscriber.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_NOCONTENT )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_NONE )
-> param ( 'topicId' , '' , new UID (), 'Topic ID.' )
-> param ( 'subscriberId' , '' , new UID (), 'Subscriber ID.' )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-29 03:11:05 +12:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $topicId , string $subscriberId , Event $queueForEvents , Database $dbForProject , Response $response ) {
2023-08-29 03:11:05 +12:00
$topic = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'topics' , $topicId ));
if ( $topic -> isEmpty ()) {
throw new Exception ( Exception :: TOPIC_NOT_FOUND );
}
$subscriber = $dbForProject -> getDocument ( 'subscribers' , $subscriberId );
if ( $subscriber -> isEmpty () || $subscriber -> getAttribute ( 'topicId' ) !== $topicId ) {
throw new Exception ( Exception :: SUBSCRIBER_NOT_FOUND );
}
2023-10-26 20:07:39 +13:00
2023-11-01 07:23:46 +13:00
$dbForProject -> deleteDocument ( 'subscribers' , $subscriberId );
2023-10-26 20:07:39 +13:00
Authorization :: skip ( fn () => $dbForProject -> decreaseDocumentAttribute ( 'topics' , $topicId , 'total' , 1 ));
2023-08-29 03:11:05 +12:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'topicId' , $topic -> getId ())
-> setParam ( 'subscriberId' , $subscriber -> getId ());
2023-08-29 03:11:05 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_NOCONTENT )
-> noContent ();
});
2023-08-22 04:45:15 +12:00
App :: post ( '/v1/messaging/messages/email' )
2023-10-14 01:56:54 +13:00
-> desc ( 'Create an email.' )
2023-08-24 21:01:22 +12:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.create' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].create' )
2023-08-24 21:01:22 +12:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
2023-10-14 01:56:54 +13:00
-> label ( 'sdk.method' , 'createEmail' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-email.md' )
2023-08-24 21:01:22 +12:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
2023-09-22 03:47:15 +12:00
-> 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.' )
2023-10-04 23:45:59 +13:00
-> param ( 'subject' , '' , new Text ( 998 ), 'Email Subject.' )
2023-10-06 00:27:48 +13:00
-> param ( 'content' , '' , new Text ( 64230 ), 'Email Content.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-26 06:33:23 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for message.' , true )
2023-10-18 20:49:21 +13:00
-> param ( 'status' , 'processing' , new WhiteList ([ 'draft' , 'processing' ]), 'Message Status. Value must be either draft or processing.' , true )
2023-10-04 23:45:59 +13:00
-> param ( 'html' , false , new Boolean (), 'Is content of type HTML' , true )
2023-10-18 20:49:21 +13:00
-> param ( 'deliveryTime' , null , new DatetimeValidator ( requireDateInFuture : true ), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-08-24 21:01:22 +12:00
-> inject ( 'dbForProject' )
2023-09-06 22:10:56 +12:00
-> inject ( 'project' )
2023-10-20 22:25:17 +13:00
-> inject ( 'queueForMessaging' )
2023-08-24 21:01:22 +12:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , string $subject , string $content , array $topics , array $users , array $targets , string $description , string $status , bool $html , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-09-22 03:47:15 +12:00
$messageId = $messageId == 'unique()' ? ID :: unique () : $messageId ;
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) === 0 && \count ( $users ) === 0 && \count ( $targets ) === 0 ) {
throw new Exception ( Exception :: MESSAGE_MISSING_TARGET );
}
2023-08-24 21:01:22 +12:00
$message = $dbForProject -> createDocument ( 'messages' , new Document ([
2023-09-22 03:47:15 +12:00
'$id' => $messageId ,
2023-10-31 07:07:57 +13:00
'topics' => $topics ,
'users' => $users ,
'targets' => $targets ,
2023-10-19 20:59:48 +13:00
'description' => $description ,
2023-08-24 21:01:22 +12:00
'data' => [
'subject' => $subject ,
'content' => $content ,
2023-09-26 20:42:44 +13:00
'html' => $html ,
2023-08-24 21:01:22 +12:00
],
2023-10-07 02:53:46 +13:00
'status' => $status ,
2023-10-27 03:14:06 +13:00
'search' => $messageId . ' ' . $description . ' ' . $subject ,
2023-10-19 20:59:48 +13:00
]));
if ( $status === 'processing' ) {
2023-10-20 22:30:22 +13:00
$queueForMessaging
2023-10-19 20:59:48 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:30:22 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-19 20:59:48 +13:00
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-10-19 20:59:48 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});
App :: post ( '/v1/messaging/messages/sms' )
2023-10-20 20:58:23 +13:00
-> desc ( 'Create an SMS.' )
2023-10-19 20:59:48 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.create' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].create' )
2023-10-19 20:59:48 +13:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createSMS' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-sms.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
-> 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 ( 'content' , '' , new Text ( 64230 ), 'SMS Content.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-20 20:58:23 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
2023-10-19 20:59:48 +13:00
-> 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 )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-19 20:59:48 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
2023-10-20 22:30:22 +13:00
-> inject ( 'queueForMessaging' )
2023-10-19 20:59:48 +13:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , string $content , array $topics , array $users , array $targets , string $description , string $status , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-10-19 20:59:48 +13:00
$messageId = $messageId == 'unique()' ? ID :: unique () : $messageId ;
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) === 0 && \count ( $users ) === 0 && \count ( $targets ) === 0 ) {
throw new Exception ( Exception :: MESSAGE_MISSING_TARGET );
}
2023-10-19 20:59:48 +13:00
$message = $dbForProject -> createDocument ( 'messages' , new Document ([
'$id' => $messageId ,
2023-10-31 07:07:57 +13:00
'topics' => $topics ,
'users' => $users ,
'targets' => $targets ,
2023-10-19 20:59:48 +13:00
'description' => $description ,
'data' => [
'content' => $content ,
],
'status' => $status ,
2023-10-27 03:14:06 +13:00
'search' => $messageId . ' ' . $description ,
2023-10-19 20:59:48 +13:00
]));
if ( $status === 'processing' ) {
2023-10-20 22:30:22 +13:00
$queueForMessaging
2023-10-19 20:59:48 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:30:22 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-19 20:59:48 +13:00
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-10-19 20:59:48 +13:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});
App :: post ( '/v1/messaging/messages/push' )
-> desc ( 'Create a push notification.' )
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.create' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].create' )
2023-10-19 20:59:48 +13:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'createPushNotification' )
-> label ( 'sdk.description' , '/docs/references/messaging/create-push-notification.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_CREATED )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
-> 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 ( 'title' , '' , new Text ( 256 ), 'Title for push notification.' )
-> param ( 'body' , '' , new Text ( 64230 ), 'Body for push notification.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-20 20:58:23 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
2023-10-19 20:59:48 +13:00
-> param ( 'data' , null , new JSON (), 'Additional Data for push notification.' , true )
-> param ( 'action' , '' , new Text ( 256 ), 'Action for push notification.' , true )
2023-10-20 20:58:23 +13:00
-> 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 )
2023-10-19 20:59:48 +13:00
-> 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 )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-19 20:59:48 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
2023-10-20 22:30:22 +13:00
-> inject ( 'queueForMessaging' )
2023-10-19 20:59:48 +13:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , string $title , string $body , array $topics , array $users , array $targets , string $description , ? array $data , string $action , string $icon , string $sound , string $color , string $tag , string $badge , string $status , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-10-19 20:59:48 +13:00
$messageId = $messageId == 'unique()' ? ID :: unique () : $messageId ;
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) === 0 && \count ( $users ) === 0 && \count ( $targets ) === 0 ) {
throw new Exception ( Exception :: MESSAGE_MISSING_TARGET );
}
2023-10-19 20:59:48 +13:00
$pushData = [
'title' => $title ,
'body' => $body ,
];
if ( ! is_null ( $data )) {
$pushData [ 'data' ] = $data ;
}
if ( $action ) {
$pushData [ 'action' ] = $action ;
}
if ( $icon ) {
$pushData [ 'icon' ] = $icon ;
}
if ( $sound ) {
$pushData [ 'sound' ] = $sound ;
}
if ( $color ) {
$pushData [ 'color' ] = $color ;
}
if ( $tag ) {
$pushData [ 'tag' ] = $tag ;
}
if ( $badge ) {
$pushData [ 'badge' ] = $badge ;
}
$message = $dbForProject -> createDocument ( 'messages' , new Document ([
'$id' => $messageId ,
2023-10-31 07:07:57 +13:00
'topics' => $topics ,
'users' => $users ,
'targets' => $targets ,
2023-10-19 20:59:48 +13:00
'description' => $description ,
2023-10-20 21:01:56 +13:00
'deliveryTime' => $deliveryTime ,
2023-10-19 20:59:48 +13:00
'data' => $pushData ,
'status' => $status ,
2023-10-27 03:14:06 +13:00
'search' => $messageId . ' ' . $description . ' ' . $title ,
2023-08-24 21:01:22 +12:00
]));
2023-10-07 02:53:46 +13:00
if ( $status === 'processing' ) {
2023-10-20 22:25:17 +13:00
$queueForMessaging
2023-10-07 02:53:46 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:25:17 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-07 02:53:46 +13:00
}
2023-08-24 21:01:22 +12:00
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-08-24 21:01:22 +12:00
$response
-> setStatusCode ( Response :: STATUS_CODE_CREATED )
2023-09-06 23:00:46 +12:00
-> dynamic ( $message , Response :: MODEL_MESSAGE );
2023-08-24 21:01:22 +12:00
});
2023-10-04 23:45:59 +13:00
2023-10-07 02:53:46 +13:00
App :: get ( '/v1/messaging/messages' )
2023-10-26 06:33:23 +13:00
-> desc ( 'List messages' )
2023-10-07 02:53:46 +13:00
-> 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' , 'listMessages' )
-> label ( 'sdk.description' , '/docs/references/messaging/list-messages.md' )
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE_LIST )
2023-10-14 01:56:54 +13:00
-> param ( 'queries' , [], new Messages (), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode ( ', ' , Providers :: ALLOWED_ATTRIBUTES ), true )
2023-10-26 06:33:23 +13:00
-> param ( 'search' , '' , new Text ( 256 ), 'Search term to filter your list results. Max length: 256 chars.' , true )
2023-10-07 02:53:46 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-26 06:33:23 +13:00
-> action ( function ( array $queries , string $search , Database $dbForProject , Response $response ) {
2023-10-07 02:53:46 +13:00
$queries = Query :: parseQueries ( $queries );
2023-10-26 06:33:23 +13:00
if ( ! empty ( $search )) {
$queries [] = Query :: search ( 'search' , $search );
}
2023-10-07 02:53:46 +13:00
// Get cursor document if there was a cursor query
$cursor = Query :: getByType ( $queries , [ Query :: TYPE_CURSORAFTER , Query :: TYPE_CURSORBEFORE ]);
$cursor = reset ( $cursor );
if ( $cursor ) {
$messageId = $cursor -> getValue ();
2023-10-18 06:23:26 +13:00
$cursorDocument = Authorization :: skip ( fn () => $dbForProject -> getDocument ( 'messages' , $messageId ));
2023-10-07 02:53:46 +13:00
2023-10-18 06:23:26 +13:00
if ( $cursorDocument -> isEmpty ()) {
2023-10-07 02:53:46 +13:00
throw new Exception ( Exception :: GENERAL_CURSOR_NOT_FOUND , " Message ' { $messageId } ' for the 'cursor' value not found. " );
}
$cursor -> setValue ( $cursorDocument );
}
$response -> dynamic ( new Document ([
'messages' => $dbForProject -> find ( 'messages' , $queries ),
2023-10-26 06:33:23 +13:00
'total' => $dbForProject -> count ( 'messages' , $queries , APP_LIMIT_COUNT ),
2023-10-07 02:53:46 +13:00
]), Response :: MODEL_MESSAGE_LIST );
});
2023-10-06 11:00:47 +13:00
App :: get ( '/v1/messaging/messages/:messageId' )
2023-10-26 06:33:23 +13:00
-> desc ( 'Get a message' )
2023-10-04 23:45:59 +13:00
-> 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 )
2023-10-06 11:00:47 +13:00
-> param ( 'messageId' , '' , new UID (), 'Message ID.' )
2023-10-04 23:45:59 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'response' )
2023-10-06 11:00:47 +13:00
-> action ( function ( string $messageId , Database $dbForProject , Response $response ) {
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
2023-10-04 23:45:59 +13:00
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
$response -> dynamic ( $message , Response :: MODEL_MESSAGE );
});
2023-10-07 02:53:46 +13:00
2023-10-12 19:18:26 +13:00
App :: patch ( '/v1/messaging/messages/email/:messageId' )
2023-10-07 02:53:46 +13:00
-> desc ( 'Update an email.' )
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.update' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].update' )
2023-10-07 02:53:46 +13:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateEmail' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-email.md' )
2023-10-19 20:59:48 +13:00
-> label ( 'sdk.response.code' , Response :: STATUS_CODE_OK )
2023-10-07 02:53:46 +13:00
-> label ( 'sdk.response.type' , Response :: CONTENT_TYPE_JSON )
-> label ( 'sdk.response.model' , Response :: MODEL_MESSAGE )
-> param ( 'messageId' , '' , new UID (), 'Message ID.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-07 02:53:46 +13:00
-> param ( 'subject' , '' , new Text ( 998 ), 'Email Subject.' , true )
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
-> param ( 'content' , '' , new Text ( 64230 ), 'Email Content.' , true )
2023-10-18 20:49:21 +13:00
-> param ( 'status' , '' , new WhiteList ([ 'draft' , 'processing' ]), 'Message Status. Value must be either draft or processing.' , true )
2023-10-07 02:53:46 +13:00
-> param ( 'html' , false , new Boolean (), 'Is content of type HTML' , true )
2023-10-18 20:49:21 +13:00
-> param ( 'deliveryTime' , null , new DatetimeValidator ( requireDateInFuture : true ), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-07 02:53:46 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
2023-10-20 22:25:17 +13:00
-> inject ( 'queueForMessaging' )
2023-10-07 02:53:46 +13:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , array $topics , array $users , array $targets , string $subject , string $description , string $content , string $status , bool $html , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-10-07 02:53:46 +13:00
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
2023-10-14 02:05:49 +13:00
if ( $message -> getAttribute ( 'status' ) === 'sent' ) {
throw new Exception ( Exception :: MESSAGE_ALREADY_SENT );
}
2023-10-20 21:21:13 +13:00
if ( ! is_null ( $message -> getAttribute ( 'deliveryTime' )) && $message -> getAttribute ( 'deliveryTime' ) < new \DateTime ()) {
2023-10-20 20:58:23 +13:00
throw new Exception ( Exception :: MESSAGE_ALREADY_SCHEDULED );
}
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) > 0 ) {
$message -> setAttribute ( 'topics' , $topics );
}
if ( \count ( $users ) > 0 ) {
$message -> setAttribute ( 'users' , $users );
}
if ( \count ( $targets ) > 0 ) {
$message -> setAttribute ( 'targets' , $targets );
2023-10-07 02:53:46 +13:00
}
$data = $message -> getAttribute ( 'data' );
if ( ! empty ( $subject )) {
$data [ 'subject' ] = $subject ;
}
if ( ! empty ( $content )) {
$data [ 'content' ] = $content ;
}
2023-10-19 20:59:48 +13:00
if ( ! empty ( $html )) {
$data [ 'html' ] = $html ;
}
$message -> setAttribute ( 'data' , $data );
2023-10-07 02:53:46 +13:00
if ( ! empty ( $description )) {
2023-10-19 20:59:48 +13:00
$message -> setAttribute ( 'description' , $description );
2023-10-07 02:53:46 +13:00
}
2023-10-21 00:32:13 +13:00
$message -> setAttribute ( 'search' , $message -> getId () . ' ' . $message -> getAttribute ( 'description' ) . ' ' . $data [ 'subject' ] . ' ' . $message -> getAttribute ( 'providerId' ));
2023-10-19 20:59:48 +13:00
if ( ! empty ( $status )) {
$message -> setAttribute ( 'status' , $status );
}
2023-10-20 21:01:56 +13:00
if ( ! is_null ( $deliveryTime )) {
$message -> setAttribute ( 'deliveryTime' , $deliveryTime );
}
2023-10-19 20:59:48 +13:00
$message = $dbForProject -> updateDocument ( 'messages' , $message -> getId (), $message );
if ( $status === 'processing' ) {
2023-10-20 22:30:22 +13:00
$queueForMessaging
2023-10-19 20:59:48 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:30:22 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-19 20:59:48 +13:00
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-10-19 20:59:48 +13:00
$response
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});
App :: patch ( '/v1/messaging/messages/sms/:messageId' )
2023-10-20 21:03:27 +13:00
-> desc ( 'Update an SMS.' )
2023-10-19 20:59:48 +13:00
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.update' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].update' )
2023-10-19 20:59:48 +13:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updateSMS' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-email.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 ( 'messageId' , '' , new UID (), 'Message ID.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-19 20:59:48 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
-> param ( 'content' , '' , new Text ( 64230 ), 'Email Content.' , true )
-> param ( 'status' , '' , 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 )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-19 20:59:48 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
2023-10-20 22:30:22 +13:00
-> inject ( 'queueForMessaging' )
2023-10-19 20:59:48 +13:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , array $topics , array $users , array $targets , string $description , string $content , string $status , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-10-19 20:59:48 +13:00
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
if ( $message -> getAttribute ( 'status' ) === 'sent' ) {
throw new Exception ( Exception :: MESSAGE_ALREADY_SENT );
}
2023-10-20 21:21:13 +13:00
if ( ! is_null ( $message -> getAttribute ( 'deliveryTime' )) && $message -> getAttribute ( 'deliveryTime' ) < new \DateTime ()) {
2023-10-20 20:58:23 +13:00
throw new Exception ( Exception :: MESSAGE_ALREADY_SCHEDULED );
}
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) > 0 ) {
$message -> setAttribute ( 'topics' , $topics );
}
if ( \count ( $users ) > 0 ) {
$message -> setAttribute ( 'users' , $users );
}
if ( \count ( $targets ) > 0 ) {
$message -> setAttribute ( 'targets' , $targets );
2023-10-19 20:59:48 +13:00
}
$data = $message -> getAttribute ( 'data' );
if ( ! empty ( $content )) {
$data [ 'content' ] = $content ;
2023-10-07 02:53:46 +13:00
}
$message -> setAttribute ( 'data' , $data );
if ( ! empty ( $status )) {
$message -> setAttribute ( 'status' , $status );
}
2023-10-19 20:59:48 +13:00
if ( ! empty ( $description )) {
$message -> setAttribute ( 'description' , $description );
}
2023-10-20 21:01:56 +13:00
if ( ! is_null ( $deliveryTime )) {
$message -> setAttribute ( 'deliveryTime' , $deliveryTime );
}
2023-10-21 00:32:13 +13:00
$message -> setAttribute ( 'search' , $message -> getId () . ' ' . $message -> getAttribute ( 'description' ) . ' ' . $message -> getAttribute ( 'providerId' ));
2023-10-19 20:59:48 +13:00
$message = $dbForProject -> updateDocument ( 'messages' , $message -> getId (), $message );
if ( $status === 'processing' ) {
2023-10-20 22:30:22 +13:00
$queueForMessaging
2023-10-19 20:59:48 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:30:22 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-19 20:59:48 +13:00
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-10-19 20:59:48 +13:00
$response
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});
App :: patch ( '/v1/messaging/messages/push/:messageId' )
-> desc ( 'Update a push notification.' )
-> groups ([ 'api' , 'messaging' ])
2023-10-26 21:16:45 +13:00
-> label ( 'audits.event' , 'message.update' )
-> label ( 'audits.resource' , 'message/{response.$id}' )
2023-11-01 07:23:46 +13:00
-> label ( 'event' , 'messages.[messageId].update' )
2023-10-19 20:59:48 +13:00
-> label ( 'scope' , 'messages.write' )
-> label ( 'sdk.auth' , [ APP_AUTH_TYPE_ADMIN , APP_AUTH_TYPE_KEY ])
-> label ( 'sdk.namespace' , 'messaging' )
-> label ( 'sdk.method' , 'updatePushNotification' )
-> label ( 'sdk.description' , '/docs/references/messaging/update-push-notification.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 ( 'messageId' , '' , new UID (), 'Message ID.' )
2023-10-31 07:07:57 +13:00
-> param ( 'topics' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Topic IDs.' , true )
-> param ( 'users' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of User IDs.' , true )
-> param ( 'targets' , [], new ArrayList ( new Text ( Database :: LENGTH_KEY ), 1 ), 'List of Targets IDs.' , true )
2023-10-19 20:59:48 +13:00
-> param ( 'description' , '' , new Text ( 256 ), 'Description for Message.' , true )
-> param ( 'title' , '' , new Text ( 256 ), 'Title for push notification.' , true )
-> 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 )
2023-10-20 20:58:23 +13:00
-> 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 )
2023-10-19 20:59:48 +13:00
-> param ( 'deliveryTime' , null , new DatetimeValidator ( requireDateInFuture : true ), 'Delivery time for message in ISO 8601 format. DateTime value must be in future.' , true )
2023-11-01 07:23:46 +13:00
-> inject ( 'queueForEvents' )
2023-10-19 20:59:48 +13:00
-> inject ( 'dbForProject' )
-> inject ( 'project' )
2023-10-20 22:30:22 +13:00
-> inject ( 'queueForMessaging' )
2023-10-19 20:59:48 +13:00
-> inject ( 'response' )
2023-11-01 07:23:46 +13:00
-> action ( function ( string $messageId , array $topics , array $users , array $targets , string $description , string $title , string $body , ? array $data , string $action , string $icon , string $sound , string $color , string $tag , string $badge , string $status , ? string $deliveryTime , Event $queueForEvents , Database $dbForProject , Document $project , Messaging $queueForMessaging , Response $response ) {
2023-10-19 20:59:48 +13:00
$message = $dbForProject -> getDocument ( 'messages' , $messageId );
if ( $message -> isEmpty ()) {
throw new Exception ( Exception :: MESSAGE_NOT_FOUND );
}
if ( $message -> getAttribute ( 'status' ) === 'sent' ) {
throw new Exception ( Exception :: MESSAGE_ALREADY_SENT );
}
2023-10-20 21:21:13 +13:00
if ( ! is_null ( $message -> getAttribute ( 'deliveryTime' )) && $message -> getAttribute ( 'deliveryTime' ) < new \DateTime ()) {
2023-10-20 20:58:23 +13:00
throw new Exception ( Exception :: MESSAGE_ALREADY_SCHEDULED );
}
2023-10-31 07:07:57 +13:00
if ( \count ( $topics ) > 0 ) {
$message -> setAttribute ( 'topics' , $topics );
}
if ( \count ( $users ) > 0 ) {
$message -> setAttribute ( 'users' , $users );
}
if ( \count ( $targets ) > 0 ) {
$message -> setAttribute ( 'targets' , $targets );
2023-10-19 20:59:48 +13:00
}
$pushData = $message -> getAttribute ( 'data' );
if ( $title ) {
$pushData [ 'title' ] = $title ;
}
if ( $body ) {
$pushData [ 'body' ] = $body ;
}
if ( ! is_null ( $data )) {
$pushData [ 'data' ] = $data ;
}
if ( $action ) {
$pushData [ 'action' ] = $action ;
}
if ( $icon ) {
$pushData [ 'icon' ] = $icon ;
}
if ( $sound ) {
$pushData [ 'sound' ] = $sound ;
}
if ( $color ) {
$pushData [ 'color' ] = $color ;
}
if ( $tag ) {
$pushData [ 'tag' ] = $tag ;
}
if ( $badge ) {
$pushData [ 'badge' ] = $badge ;
}
$message -> setAttribute ( 'data' , $pushData );
2023-10-07 02:53:46 +13:00
if ( ! empty ( $status )) {
$message -> setAttribute ( 'status' , $status );
}
2023-10-19 20:59:48 +13:00
if ( ! empty ( $description )) {
$message -> setAttribute ( 'description' , $description );
}
2023-10-20 21:01:56 +13:00
if ( ! is_null ( $deliveryTime )) {
$message -> setAttribute ( 'deliveryTime' , $deliveryTime );
}
2023-10-19 20:59:48 +13:00
2023-10-21 00:32:13 +13:00
$message -> setAttribute ( 'search' , $message -> getId () . ' ' . $message -> getAttribute ( 'description' ) . ' ' . $pushData [ 'title' ] . ' ' . $message -> getAttribute ( 'providerId' ));
2023-10-07 02:53:46 +13:00
$message = $dbForProject -> updateDocument ( 'messages' , $message -> getId (), $message );
if ( $status === 'processing' ) {
2023-10-20 22:25:17 +13:00
$queueForMessaging
2023-10-07 02:53:46 +13:00
-> setMessageId ( $message -> getId ())
2023-10-20 22:25:17 +13:00
-> setProject ( $project )
-> trigger ();
2023-10-07 02:53:46 +13:00
}
2023-11-01 07:23:46 +13:00
$queueForEvents
-> setParam ( 'messageId' , $message -> getId ());
2023-10-07 02:53:46 +13:00
$response
-> dynamic ( $message , Response :: MODEL_MESSAGE );
});