From 11e9f4398203a3de424f356dbc46d32ecab693c6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 24 Feb 2024 13:34:08 +1300 Subject: [PATCH] Allow setting APNS to sandbox mode --- app/controllers/api/messaging.php | 19 +++++++++++++++++-- src/Appwrite/Platform/Workers/Messaging.php | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 1c30e07ba5..4a75e03b47 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -766,11 +766,12 @@ App::post('/v1/messaging/providers/apns') ->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('sandbox', false, new Boolean(), 'Use APNS sandbox environment.', true) ->param('enabled', null, new Boolean(), 'Set as enabled.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, string $authKey, string $authKeyId, string $teamId, string $bundleId, ?bool $enabled, Event $queueForEvents, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, string $authKey, string $authKeyId, string $teamId, string $bundleId, bool $sandbox, ?bool $enabled, Event $queueForEvents, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; $credentials = []; @@ -803,6 +804,10 @@ App::post('/v1/messaging/providers/apns') $enabled = false; } + $options = [ + 'sandbox' => $sandbox + ]; + $provider = new Document([ '$id' => $providerId, 'name' => $name, @@ -810,6 +815,7 @@ App::post('/v1/messaging/providers/apns') 'type' => MESSAGE_TYPE_PUSH, 'enabled' => $enabled, 'credentials' => $credentials, + 'options' => $options ]); try { @@ -1808,10 +1814,11 @@ App::patch('/v1/messaging/providers/apns/:providerId') ->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('sandbox', null, new Boolean(), 'Use APNS sandbox environment.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, ?bool $enabled, string $authKey, string $authKeyId, string $teamId, string $bundleId, Event $queueForEvents, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, ?bool $enabled, string $authKey, string $authKeyId, string $teamId, string $bundleId, ?bool $sandbox, Event $queueForEvents, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $providerId); if ($provider->isEmpty()) { @@ -1847,6 +1854,14 @@ App::patch('/v1/messaging/providers/apns/:providerId') $provider->setAttribute('credentials', $credentials); + $options = $provider->getAttribute('options'); + + if (!\is_null($sandbox)) { + $options['sandbox'] = $sandbox; + } + + $provider->setAttribute('options', $options); + if (!\is_null($enabled)) { if ($enabled) { if ( diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 62e58db11a..aacd49f94a 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -469,6 +469,7 @@ class Messaging extends Action private function getPushAdapter(Document $provider): ?PushAdapter { $credentials = $provider->getAttribute('credentials'); + $options = $provider->getAttribute('options'); return match ($provider->getAttribute('provider')) { 'mock' => new Mock('username', 'password'), @@ -477,6 +478,7 @@ class Messaging extends Action $credentials['authKeyId'], $credentials['teamId'], $credentials['bundleId'], + $options['sandbox'] ), 'fcm' => new FCM(\json_encode($credentials['serviceAccountJSON'])), default => null