From 2e0bee76ce09c19b7d40883b59842bb5c56297f7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 15 Nov 2023 12:09:31 +0000 Subject: [PATCH] chore: review changes --- app/init.php | 12 ---- composer.json | 2 +- src/Appwrite/Platform/Workers/Messaging.php | 75 +++++++++++++++++++-- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/app/init.php b/app/init.php index 2bbb44a05b..5b4378713a 100644 --- a/app/init.php +++ b/app/init.php @@ -47,15 +47,7 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\Structure; use Utopia\Locale\Locale; use Utopia\DSN\DSN; -use Utopia\Messaging\Adapters\SMS\Mock; use Appwrite\GraphQL\Promises\Adapter\Swoole; -use Utopia\Messaging\Adapters\SMS\Msg91; -use Utopia\Messaging\Adapters\SMS\Telesign; -use Utopia\Messaging\Adapters\SMS\TextMagic; -use Utopia\Messaging\Adapters\SMS\Twilio; -use Utopia\Messaging\Adapters\SMS\Vonage; -use Utopia\Messaging\Adapters\SMS\GEOSMS; -use Utopia\Messaging\Adapters\SMS\GEOSMS\CallingCode; use Utopia\Registry\Registry; use Utopia\Storage\Device; use Utopia\Storage\Device\Backblaze; @@ -1330,10 +1322,6 @@ App::setResource('passwordsDictionary', function ($register) { return $register->get('passwordsDictionary'); }, ['register']); -App::setResource('sms', function () { - $dsn = new DSN(App::getEnv('_APP_SMS_PROVIDER')); - return SMSFactory::createFromDSN($dsn); -}); App::setResource('servers', function () { $platforms = Config::getParam('platforms'); diff --git a/composer.json b/composer.json index a24feca83b..ff33a90065 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "utopia-php/image": "0.5.*", "utopia-php/locale": "0.4.*", "utopia-php/logger": "0.3.*", - "utopia-php/messaging": "0.2.*", + "utopia-php/messaging": "0.3.*", "utopia-php/migration": "0.3.*", "utopia-php/orchestration": "0.9.*", "utopia-php/platform": "0.5.*", diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 4082ce7119..aca0b15b4a 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -2,12 +2,14 @@ namespace Appwrite\Platform\Workers; -use Appwrite\Messaging\Adapter\SMS\SMSFactory; + + use Exception; use Utopia\App; use Utopia\CLI\Console; use Utopia\DSN\DSN; -use Utopia\Messaging\Messages\Sms; +use Utopia\Messaging\Messages\SMS; +use Utopia\Messaging\Adapters\SMS as SMSAdapter; use Utopia\Messaging\Adapters\SMS\Mock; use Utopia\Messaging\Adapters\SMS\Msg91; use Utopia\Messaging\Adapters\SMS\Telesign; @@ -15,13 +17,12 @@ use Utopia\Messaging\Adapters\SMS\TextMagic; use Utopia\Messaging\Adapters\SMS\Twilio; use Utopia\Messaging\Adapters\SMS\Vonage; use Utopia\Messaging\Adapters\SMS\GEOSMS; -use Utopia\Messaging\Adapters\SMS\GEOSMS\CallingCode; use Utopia\Platform\Action; use Utopia\Queue\Message; class Messaging extends Action { - private DSN $dsn; + private ?DSN $dsn = null; public static function getName(): string { @@ -69,12 +70,12 @@ class Messaging extends Action } - if (empty(App::getEnv('_APP_SMS_PROVIDER') && empty(App::getEnv('_APP_GEOSMS_PROVIDERS')))) { + if (empty(App::getEnv('_APP_SMS_PROVIDER'))) { Console::error('Skipped sms processing. No Phone provider has been set.'); return; } - $sms = SMSFactory::createFromDSN($this->dsn); + $sms = self::createFromDSN($this->dsn); $from = App::getEnv('_APP_SMS_FROM'); if (empty($from)) { @@ -94,4 +95,66 @@ class Messaging extends Action throw new Exception('Error sending message: ' . $error->getMessage(), 500); } } + + protected static function createFromDSN(DSN $dsn): SMSAdapter + { + $adapter = null; + + switch ($dsn->getHost()) { + case 'mock': + $adapter = new Mock($dsn->getUser(), $dsn->getPassword()); + break; + case 'msg91': + $adapter = new Msg91($dsn->getUser(), $dsn->getPassword()); + $adapter->setTemplate($dsn->getParam('template', '')); + break; + case 'telesign': + $adapter = new Telesign($dsn->getUser(), $dsn->getPassword()); + break; + case 'textmagic': + $adapter = new TextMagic($dsn->getUser(), $dsn->getPassword()); + break; + case 'twilio': + $adapter = new Twilio($dsn->getUser(), $dsn->getPassword()); + break; + case 'vonage': + $adapter = new Vonage($dsn->getUser(), $dsn->getPassword()); + break; + case 'geosms': + $adapter = self::createGEOSMS($dsn); + break; + } + + return $adapter; + } + + protected static function createGEOSMS(DSN $dsn): GEOSMS + { + $defaultDSN = new DSN($dsn->getParam('default', '')); + $geosms = new GEOSMS(self::createFromDSN($defaultDSN)); + + $geosmsConfig = []; + \parse_str($dsn->getQuery(), $geosmsConfig); + + foreach ($geosmsConfig as $key => $nestedDSN) { + // Extract the calling code in the format of local[callingCode] + // e.g. local[1] = twilio://... + $matches = []; + if (\preg_match('/^local\[[0-9]+\]$/', $key, $matches) !== 1) { + continue; + } + $callingCode = $matches[1]; + + $dsn = null; + try { + $dsn = new DSN($nestedDSN); + } catch (\Exception) { + continue; + } + + $geosms->setLocal($callingCode, self::createFromDSN($dsn)); + } + + return $geosms; + } }