mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
feat: use nested DSN config
This commit is contained in:
@@ -447,15 +447,6 @@ return [
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_GEOSMS_PROVIDERS',
|
||||
'description' => "Comma seperated list of providers used for delivering SMS for Phone authentication. Use the following format: 'sms://[USER]:[SECRET]@[PROVIDER],sms://[USER]:[SECRET]@[PROVIDER]'.\n\nEnsure `[USER]` and `[SECRET]` are URL encoded if they contain any non-alphanumeric characters.\n\nAvailable providers are twilio, text-magic, telesign, msg91, and vonage.",
|
||||
'introduction' => '1.4.10',
|
||||
'default' => '',
|
||||
'required' => false,
|
||||
'question' => '',
|
||||
'filter' => ''
|
||||
],
|
||||
[
|
||||
'name' => '_APP_SMS_FROM',
|
||||
'description' => 'Phone number used for sending out messages. Must start with a leading \'+\' and maximum of 15 digits without spaces (+123456789).',
|
||||
|
||||
+3
-26
@@ -74,6 +74,7 @@ use Ahc\Jwt\JWTException;
|
||||
use Appwrite\Event\Build;
|
||||
use Appwrite\Event\Certificate;
|
||||
use Appwrite\Event\Func;
|
||||
use Appwrite\Messaging\Adapter\SMS\SMSFactory;
|
||||
use MaxMind\Db\Reader;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use Swoole\Database\PDOProxy;
|
||||
@@ -1331,32 +1332,8 @@ App::setResource('passwordsDictionary', function ($register) {
|
||||
|
||||
App::setResource('sms', function () {
|
||||
$dsn = new DSN(App::getEnv('_APP_SMS_PROVIDER'));
|
||||
|
||||
if (empty(App::getEnv('_APP_GEOSMS_PROVIDERS'))) {
|
||||
return match ($dsn->getHost()) {
|
||||
'mock' => new Mock($dsn->getUser(), $dsn->getPassword()), // used for tests
|
||||
'twilio' => new Twilio($dsn->getUser(), $dsn->getPassword()),
|
||||
'text-magic' => new TextMagic($dsn->getUser(), $dsn->getPassword()),
|
||||
'telesign' => new Telesign($dsn->getUser(), $dsn->getPassword()),
|
||||
'msg91' => new Msg91($dsn->getUser(), $dsn->getPassword()),
|
||||
'vonage' => new Vonage($dsn->getUser(), $dsn->getPassword()),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
$geosmsProviders = explode(',', App::getEnv('_APP_GEOSMS_PROVIDERS', ''));
|
||||
$geosmsDSNs = [];
|
||||
|
||||
foreach ($geosmsProviders as $geosmsProvider) {
|
||||
$dsn = new DSN($geosmsProvider);
|
||||
$geosmsDSNs[$dsn->getHost()] = $dsn;
|
||||
}
|
||||
|
||||
$twilio = new Twilio($this->geosmsDSNs['twilio']->getUser(), $this->geosmsDSNs['twilio']->getPassword());
|
||||
$msg91 = new Msg91($this->geosmsDSNs['msg91']-> getUser(), $this->geosmsDSNs['msg91']->getPassword());
|
||||
$msg91->setTemplate('654cad00d6fc050612135b33');
|
||||
$sms = new GEOSMS($twilio);
|
||||
return $sms->setLocal(CallingCode::INDIA, $msg91);
|
||||
$sms = SMSFactory::createFromDSN($dsn);
|
||||
return $sms;
|
||||
});
|
||||
|
||||
App::setResource('servers', function () {
|
||||
|
||||
@@ -154,7 +154,6 @@ services:
|
||||
- _APP_MAINTENANCE_RETENTION_USAGE_HOURLY
|
||||
- _APP_MAINTENANCE_RETENTION_SCHEDULES
|
||||
- _APP_SMS_PROVIDER
|
||||
_ _APP_GEOSMS_PROVIDERS
|
||||
- _APP_SMS_FROM
|
||||
- _APP_GRAPHQL_MAX_BATCH_SIZE
|
||||
- _APP_GRAPHQL_MAX_COMPLEXITY
|
||||
|
||||
@@ -175,7 +175,6 @@ services:
|
||||
- _APP_MAINTENANCE_RETENTION_USAGE_HOURLY
|
||||
- _APP_MAINTENANCE_RETENTION_SCHEDULES
|
||||
- _APP_SMS_PROVIDER
|
||||
- _APP_GEOSMS_PROVIDERS
|
||||
- _APP_SMS_FROM
|
||||
- _APP_GRAPHQL_MAX_BATCH_SIZE
|
||||
- _APP_GRAPHQL_MAX_COMPLEXITY
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Messaging\Adapter\SMS;
|
||||
|
||||
use Utopia\Messaging\Adapters\SMS;
|
||||
use Utopia\Messaging\Adapters\SMS\Mock;
|
||||
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\DSN\DSN;
|
||||
|
||||
|
||||
|
||||
class SMSFactory
|
||||
{
|
||||
public static function createFromDSN(DSN $dsn): SMS
|
||||
{
|
||||
$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 = SMSFactory::createGEOSMS($dsn);
|
||||
break;
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
protected static function createGEOSMS(DSN $dsn) {
|
||||
|
||||
$defaultParam = $dsn->getParam('default', '');
|
||||
$defaultDSN = new DSN($defaultParam);
|
||||
$defaultAdapter = SMSFactory::createFromDSN($defaultDSN);
|
||||
|
||||
$geosms = new GEOSMS($defaultAdapter);
|
||||
|
||||
foreach (CallingCode::getCodes() as $callingCode) {
|
||||
$paramKey = "local[{$callingCode}]";
|
||||
$param = $dsn->getParam($paramKey, '');
|
||||
|
||||
if (empty($param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dsn = null;
|
||||
try {
|
||||
$dsn = new DSN($param);
|
||||
} catch (\Exception $e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$geosms->setLocal($callingCode, SMSFactory::createFromDSN($dsn));
|
||||
}
|
||||
|
||||
return $geosms;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Appwrite\Platform\Workers;
|
||||
|
||||
use Appwrite\Messaging\Adapter\SMS\SMSFactory;
|
||||
use Exception;
|
||||
use Utopia\App;
|
||||
use Utopia\CLI\Console;
|
||||
@@ -21,7 +22,6 @@ use Utopia\Queue\Message;
|
||||
class Messaging extends Action
|
||||
{
|
||||
private DSN $dsn;
|
||||
private array $geosmsDSNs = [];
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
@@ -38,14 +38,6 @@ class Messaging extends Action
|
||||
$this->dsn = new DSN($provider);
|
||||
}
|
||||
|
||||
$geoProviders = App::getEnv('_APP_GEOSMS_PROVIDERS', '');
|
||||
if (!empty($geoProviders)) {
|
||||
foreach (explode(',', $geoProviders) as $geoProvider) {
|
||||
$dsn = new DSN($geoProvider);
|
||||
$this->geosmsDSNs[$dsn->getHost()] = $dsn;
|
||||
}
|
||||
}
|
||||
|
||||
$this
|
||||
->desc('Messaging worker')
|
||||
->inject('message')
|
||||
@@ -82,23 +74,7 @@ class Messaging extends Action
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty(App::getEnv('_APP_GEOSMS_PROVIDERS'))) {
|
||||
$sms = match ($this->dsn->getHost()) {
|
||||
'mock' => new Mock($this->dsn->getUser(), $this->dsn->getPassword()), // used for tests
|
||||
'twilio' => new Twilio($this->dsn->getUser(), $this->dsn->getPassword()),
|
||||
'text-magic' => new TextMagic($this->dsn->getUser(), $this->dsn->getPassword()),
|
||||
'telesign' => new Telesign($this->dsn->getUser(), $this->dsn->getPassword()),
|
||||
'msg91' => new Msg91($this->dsn->getUser(), $this->dsn->getPassword()),
|
||||
'vonage' => new Vonage($this->dsn->getUser(), $this->dsn->getPassword()),
|
||||
default => null
|
||||
};
|
||||
} else {
|
||||
$twilio = new Twilio($this->geosmsDSNs['twilio']->getUser(), $this->geosmsDSNs['twilio']->getPassword());
|
||||
$msg91 = new Msg91($this->geosmsDSNs['msg91'] > getUser(), $this->geosmsDSNs['msg91']->getPassword());
|
||||
$sms = new GEOSMS($twilio);
|
||||
$sms->setLocal(CallingCode::INDIA, $msg91);
|
||||
}
|
||||
|
||||
$sms = SMSFactory::createFromDSN($this->dsn);
|
||||
$from = App::getEnv('_APP_SMS_FROM');
|
||||
|
||||
if (empty($from)) {
|
||||
|
||||
Reference in New Issue
Block a user