chore: review changes

This commit is contained in:
loks0n
2023-11-15 12:09:31 +00:00
parent 1b78ae1eaf
commit 2e0bee76ce
3 changed files with 70 additions and 19 deletions
-12
View File
@@ -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');
+1 -1
View File
@@ -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.*",
+69 -6
View File
@@ -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;
}
}