Merge pull request #13761 from appwrite/refactor/messaging-adapter-di

refactor(messaging): build the platform's SMS adapter as a DI resource
This commit is contained in:
Chirag Aggarwal authored and GitHub committed 2026-09-18 07:14:44 -07:00
commit 1794c85dce
3 files changed
+285 -237

No files matched your search

+15
View File
@@ -22,6 +22,7 @@ use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher;
use Appwrite\Event\Publisher\Usage as UsagePublisher;
use Appwrite\Execution\Store as ExecutionStore;
use Appwrite\Geo\Client as GeoClient;
use Appwrite\Messaging\Provider as MessagingProvider;
use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl;
use Appwrite\Screenshots\Client as ScreenshotsClient;
use Appwrite\Usage\Connection as UsageConnection;
@@ -45,6 +46,7 @@ use Utopia\Database\Validator\Authorization;
use Utopia\DI\Container;
use Utopia\DSN\DSN;
use Utopia\Lock\Distributed;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Pools\Adapter\Swoole as SwoolePoolAdapter;
use Utopia\Pools\Group;
use Utopia\Pools\Pool as Connections;
@@ -117,6 +119,19 @@ $container->set('autogravity', function (Cache $cache) {
$container->set('telemetry', fn () => new NoTelemetry(), []);
/**
* A malformed DSN is reported and read as unset rather than thrown: this resolves for every
* messaging job, so one bad platform variable must not stop a project's push and email.
*/
$container->set('adapterForSMS', function (Telemetry $telemetry): ?SMSAdapter {
try {
return (new MessagingProvider($telemetry))->internalSMS();
} catch (\Throwable $error) {
Console::error('Ignoring _APP_SMS_PROVIDER: ' . $error->getMessage());
return null;
}
}, ['telemetry']);
$container->set('authorization', fn () => new Authorization(), []);
$container->set('publisher', fn (Group $pools) => new BrokerPool(publisher: $pools->get('publisher')), ['pools']);
+256
View File
@@ -0,0 +1,256 @@
<?php
namespace Appwrite\Messaging;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\DSN\DSN;
use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Adapter\Email\Mailgun;
use Utopia\Messaging\Adapter\Email\Resend;
use Utopia\Messaging\Adapter\Email\Sendgrid;
use Utopia\Messaging\Adapter\Email\SES;
use Utopia\Messaging\Adapter\Email\SMTP;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Adapter\SMS\Fast2SMS;
use Utopia\Messaging\Adapter\SMS\GEOSMS;
use Utopia\Messaging\Adapter\SMS\Inforu;
use Utopia\Messaging\Adapter\SMS\Mock;
use Utopia\Messaging\Adapter\SMS\Msg91;
use Utopia\Messaging\Adapter\SMS\Telesign;
use Utopia\Messaging\Adapter\SMS\TextMagic;
use Utopia\Messaging\Adapter\SMS\Twilio;
use Utopia\Messaging\Adapter\SMS\Vonage;
use Utopia\System\System;
use Utopia\Telemetry\Adapter as Telemetry;
/**
* Builds the adapter a messaging provider sends through.
*
* A provider is either one a project stored as a document or the platform's own, described
* by _APP_SMS_PROVIDER. Both reach the same adapter, so the DSN becomes a provider document
* and takes the same path.
*
* Push is absent on purpose: its Appwrite provider needs the message and the project
* database, so a provider document alone does not describe it.
*/
class Provider
{
public function __construct(private readonly Telemetry $telemetry)
{
}
public function sms(Document $provider): ?SMSAdapter
{
$credentials = $provider->getAttribute('credentials');
$adapter = match ($provider->getAttribute('provider')) {
'mock' => (new Mock('username', 'password'))->setEndpoint('http://request-catcher-sms:5000/'),
'twilio' => new Twilio(
$credentials['accountSid'] ?? '',
$credentials['authToken'] ?? '',
null,
$credentials['messagingServiceSid'] ?? null
),
'textmagic' => new TextMagic(
$credentials['username'] ?? '',
$credentials['apiKey'] ?? ''
),
'telesign' => new Telesign(
$credentials['customerId'] ?? '',
$credentials['apiKey'] ?? ''
),
'msg91' => new Msg91(
$credentials['senderId'] ?? '',
$credentials['authKey'] ?? '',
$credentials['templateId'] ?? ''
),
'vonage' => new Vonage(
$credentials['apiKey'] ?? '',
$credentials['apiSecret'] ?? ''
),
'fast2sms' => new Fast2SMS(
$credentials['apiKey'] ?? '',
$credentials['senderId'] ?? '',
$credentials['messageId'] ?? '',
$credentials['useDLT'] ?? true
),
'inforu' => new Inforu(
$credentials['senderId'] ?? '',
$credentials['apiKey'] ?? '',
),
default => null
};
if ($adapter !== null) {
$adapter->setTelemetry($this->telemetry);
}
return $adapter;
}
public function email(Document $provider): ?EmailAdapter
{
$credentials = $provider->getAttribute('credentials', []);
$options = $provider->getAttribute('options', []);
$apiKey = $credentials['apiKey'] ?? '';
$adapter = match ($provider->getAttribute('provider')) {
'mock' => new Mock('username', 'password'),
'smtp' => new SMTP(
$credentials['host'] ?? '',
$credentials['port'] ?? 25,
$credentials['username'] ?? '',
$credentials['password'] ?? '',
$options['encryption'] ?? '',
$options['autoTLS'] ?? false,
$options['mailer'] ?? '',
),
'mailgun' => new Mailgun(
$apiKey,
$credentials['domain'] ?? '',
$credentials['isEuRegion'] ?? false
),
'sendgrid' => new Sendgrid($apiKey),
'resend' => new Resend($apiKey),
'ses' => new SES(
$credentials['accessKey'] ?? '',
$credentials['secretKey'] ?? '',
$credentials['region'] ?? '',
$credentials['sessionToken'] ?? null,
),
default => null
};
if ($adapter !== null) {
$adapter->setTelemetry($this->telemetry);
}
return $adapter;
}
public function internalSMS(): ?SMSAdapter
{
if (empty(System::getEnv('_APP_SMS_PROVIDER')) || empty(System::getEnv('_APP_SMS_FROM'))) {
return null;
}
$providers = System::getEnv('_APP_SMS_PROVIDER', '');
$dsns = [];
if (!empty($providers)) {
$providers = explode(',', $providers);
foreach ($providers as $provider) {
$dsns[] = new DSN($provider);
}
}
if (count($dsns) === 1) {
$provider = $this->fromDSN($dsns[0]);
$adapter = $this->sms($provider);
return $adapter;
}
$defaultDSN = null;
$localDSNs = [];
/** @var DSN $dsn */
foreach ($dsns as $dsn) {
if ($dsn->getParam('local', '') === 'default') {
$defaultDSN = $dsn;
} else {
$localDSNs[] = $dsn;
}
}
if ($defaultDSN === null) {
throw new \Exception('No default SMS provider found');
}
$defaultProvider = $this->fromDSN($defaultDSN);
$adapter = $this->sms($defaultProvider);
$geosms = new GEOSMS($adapter);
$geosms->setTelemetry($this->telemetry);
/** @var DSN $localDSN */
foreach ($localDSNs as $localDSN) {
try {
$provider = $this->fromDSN($localDSN);
$adapter = $this->sms($provider);
} catch (\Exception) {
continue;
}
$callingCode = $localDSN->getParam('local', '');
if (empty($callingCode)) {
continue;
}
$geosms->setLocal($callingCode, $adapter);
}
return $geosms;
}
private function fromDSN(DSN $dsn): Document
{
$host = $dsn->getHost();
$password = $dsn->getPassword();
$user = $dsn->getUser();
$from = System::getEnv('_APP_SMS_FROM');
$provider = new Document([
'$id' => ID::unique(),
'provider' => $host,
'type' => MESSAGE_TYPE_SMS,
'name' => 'Internal SMS',
'enabled' => true,
'credentials' => match ($host) {
'twilio' => [
'accountSid' => $user,
'authToken' => $password,
// Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG
// https://www.twilio.com/docs/messaging/api/service-resource
'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null
],
'textmagic' => [
'username' => $user,
'apiKey' => $password
],
'telesign' => [
'customerId' => $user,
'apiKey' => $password
],
'msg91' => [
'senderId' => $user,
'authKey' => $password,
'templateId' => $dsn->getParam('templateId', $from),
],
'vonage' => [
'apiKey' => $user,
'apiSecret' => $password
],
'fast2sms' => [
'senderId' => $user,
'apiKey' => $password,
'messageId' => $dsn->getParam('messageId'),
'useDLT' => $dsn->getParam('useDLT'),
],
'inforu' => [
'senderId' => $user,
'apiKey' => $password,
],
default => null
},
'options' => match ($host) {
'twilio' => [
'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from
],
default => [
'from' => $from
]
}
]);
return $provider;
}
}
+14 -237
View File
@@ -6,6 +6,7 @@ use Appwrite\Event\Message\Usage;
use Appwrite\Event\Publisher\Usage as UsagePublisher;
use Appwrite\Messaging\Adapter\Mqtt;
use Appwrite\Messaging\Adapter\Push\Appwrite as AppwritePush;
use Appwrite\Messaging\Provider;
use Appwrite\Messaging\Status as MessageStatus;
use Appwrite\OpenSSL\OpenSSL;
use Appwrite\PubSub\Adapter\Pool as PubSubPool;
@@ -19,29 +20,18 @@ use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\DSN\DSN;
use Utopia\Lock\Semaphore;
use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Adapter\Email\Mailgun;
use Utopia\Messaging\Adapter\Email\Resend;
use Utopia\Messaging\Adapter\Email\Sendgrid;
use Utopia\Messaging\Adapter\Email\SES;
use Utopia\Messaging\Adapter\Email\SMTP;
use Utopia\Messaging\Adapter\Push\APNS;
use Utopia\Messaging\Adapter\Push as PushAdapter;
use Utopia\Messaging\Adapter\Push\FCM;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Adapter\SMS\Fast2SMS;
use Utopia\Messaging\Adapter\SMS\GEOSMS;
use Utopia\Messaging\Adapter\SMS\GEOSMS\CallingCode;
use Utopia\Messaging\Adapter\SMS\Inforu;
use Utopia\Messaging\Adapter\SMS\Mock;
use Utopia\Messaging\Adapter\SMS\Msg91;
use Utopia\Messaging\Adapter\SMS\Msg91\MetadataParameter;
use Utopia\Messaging\Adapter\SMS\Telesign;
use Utopia\Messaging\Adapter\SMS\TextMagic;
use Utopia\Messaging\Adapter\SMS\Twilio;
use Utopia\Messaging\Adapter\SMS\Vonage;
use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Messages\Email\Attachment;
use Utopia\Messaging\Messages\Push;
@@ -63,10 +53,10 @@ use function Swoole\Coroutine\batch;
class Messaging extends Action
{
private ?SMSAdapter $adapter = null;
private Telemetry $telemetry;
private Provider $provider;
private Group $pools;
public static function getName(): string
@@ -88,6 +78,7 @@ class Messaging extends Action
->inject('publisherForUsage')
->inject('telemetry')
->inject('pools')
->inject('adapterForSMS')
->callback($this->action(...));
}
@@ -98,6 +89,7 @@ class Messaging extends Action
* @param Device $deviceForFiles
* @param UsagePublisher $publisherForUsage
* @param Telemetry $telemetry
* @param SMSAdapter|null $adapterForSMS
* @param Group $pools
* @return void
* @throws \Exception
@@ -109,10 +101,12 @@ class Messaging extends Action
Device $deviceForFiles,
UsagePublisher $publisherForUsage,
Telemetry $telemetry,
Group $pools
Group $pools,
?SMSAdapter $adapterForSMS
): void {
$this->telemetry = $telemetry;
$this->pools = $pools;
$this->provider = new Provider($telemetry);
$payload = $message->getPayload();
if (empty($payload)) {
@@ -128,7 +122,7 @@ class Messaging extends Action
$message = new Document($payload['message'] ?? []);
$recipients = $payload['recipients'] ?? [];
$this->sendInternalSMSMessage($message, $project, $recipients);
$this->sendInternalSMSMessage($message, $project, $recipients, $adapterForSMS);
break;
case MESSAGE_SEND_TYPE_EXTERNAL:
$messageId = $payload['messageId'];
@@ -258,9 +252,9 @@ class Messaging extends Action
$resolvedProviderType = $provider->getAttribute('type');
$adapter = match ($resolvedProviderType) {
MESSAGE_TYPE_SMS => $this->getSmsAdapter($provider),
MESSAGE_TYPE_SMS => $this->provider->sms($provider),
MESSAGE_TYPE_PUSH => $this->getPushAdapter($provider, $dbForProject, $project, $message),
MESSAGE_TYPE_EMAIL => $this->getEmailAdapter($provider),
MESSAGE_TYPE_EMAIL => $this->provider->email($provider),
default => throw new \Exception('Provider with the requested ID is of the incorrect type')
};
@@ -823,13 +817,9 @@ class Messaging extends Action
return $data;
}
private function sendInternalSMSMessage(Document $message, Document $project, array $recipients): void
private function sendInternalSMSMessage(Document $message, Document $project, array $recipients, ?SMSAdapter $adapterForSMS): void
{
if ($this->adapter === null) {
$this->adapter = $this->createInternalSMSAdapter();
}
if ($this->adapter === null) {
if ($adapterForSMS === null) {
throw new \Exception('SMS adapter is not set.');
}
@@ -860,59 +850,10 @@ class Messaging extends Action
// webhooks can be attributed back to the originating project.
$sms->setMetadata([MetadataParameter::UUID->value => $project->getId()]);
$this->adapter->send($sms);
$adapterForSMS->send($sms);
}
protected function getSmsAdapter(Document $provider): ?SMSAdapter
{
$credentials = $provider->getAttribute('credentials');
$adapter = match ($provider->getAttribute('provider')) {
'mock' => (new Mock('username', 'password'))->setEndpoint('http://request-catcher-sms:5000/'),
'twilio' => new Twilio(
$credentials['accountSid'] ?? '',
$credentials['authToken'] ?? '',
null,
$credentials['messagingServiceSid'] ?? null
),
'textmagic' => new TextMagic(
$credentials['username'] ?? '',
$credentials['apiKey'] ?? ''
),
'telesign' => new Telesign(
$credentials['customerId'] ?? '',
$credentials['apiKey'] ?? ''
),
'msg91' => new Msg91(
$credentials['senderId'] ?? '',
$credentials['authKey'] ?? '',
$credentials['templateId'] ?? ''
),
'vonage' => new Vonage(
$credentials['apiKey'] ?? '',
$credentials['apiSecret'] ?? ''
),
'fast2sms' => new Fast2SMS(
$credentials['apiKey'] ?? '',
$credentials['senderId'] ?? '',
$credentials['messageId'] ?? '',
$credentials['useDLT'] ?? true
),
'inforu' => new Inforu(
$credentials['senderId'] ?? '',
$credentials['apiKey'] ?? '',
),
default => null
};
if ($adapter !== null) {
$adapter->setTelemetry($this->telemetry);
}
return $adapter;
}
protected function getPushAdapter(Document $provider, Database $dbForProject, Document $project, Document $message): ?PushAdapter
{
$credentials = $provider->getAttribute('credentials');
@@ -946,46 +887,6 @@ class Messaging extends Action
return $adapter;
}
protected function getEmailAdapter(Document $provider): ?EmailAdapter
{
$credentials = $provider->getAttribute('credentials', []);
$options = $provider->getAttribute('options', []);
$apiKey = $credentials['apiKey'] ?? '';
$adapter = match ($provider->getAttribute('provider')) {
'mock' => new Mock('username', 'password'),
'smtp' => new SMTP(
$credentials['host'] ?? '',
$credentials['port'] ?? 25,
$credentials['username'] ?? '',
$credentials['password'] ?? '',
$options['encryption'] ?? '',
$options['autoTLS'] ?? false,
$options['mailer'] ?? '',
),
'mailgun' => new Mailgun(
$apiKey,
$credentials['domain'] ?? '',
$credentials['isEuRegion'] ?? false
),
'sendgrid' => new Sendgrid($apiKey),
'resend' => new Resend($apiKey),
'ses' => new SES(
$credentials['accessKey'] ?? '',
$credentials['secretKey'] ?? '',
$credentials['region'] ?? '',
$credentials['sessionToken'] ?? null,
),
default => null
};
if ($adapter !== null) {
$adapter->setTelemetry($this->telemetry);
}
return $adapter;
}
/**
* Materialise a message's attachments as files the adapters can read.
*
@@ -1218,128 +1119,4 @@ class Messaging extends Action
return new Local(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
}
private function createInternalSMSAdapter(): ?SMSAdapter
{
if (empty(System::getEnv('_APP_SMS_PROVIDER')) || empty(System::getEnv('_APP_SMS_FROM'))) {
return null;
}
$providers = System::getEnv('_APP_SMS_PROVIDER', '');
$dsns = [];
if (!empty($providers)) {
$providers = explode(',', $providers);
foreach ($providers as $provider) {
$dsns[] = new DSN($provider);
}
}
if (count($dsns) === 1) {
$provider = $this->createProviderFromDSN($dsns[0]);
$adapter = $this->getSmsAdapter($provider);
return $adapter;
}
$defaultDSN = null;
$localDSNs = [];
/** @var DSN $dsn */
foreach ($dsns as $dsn) {
if ($dsn->getParam('local', '') === 'default') {
$defaultDSN = $dsn;
} else {
$localDSNs[] = $dsn;
}
}
if ($defaultDSN === null) {
throw new \Exception('No default SMS provider found');
}
$defaultProvider = $this->createProviderFromDSN($defaultDSN);
$adapter = $this->getSmsAdapter($defaultProvider);
$geosms = new GEOSMS($adapter);
$geosms->setTelemetry($this->telemetry);
/** @var DSN $localDSN */
foreach ($localDSNs as $localDSN) {
try {
$provider = $this->createProviderFromDSN($localDSN);
$adapter = $this->getSmsAdapter($provider);
} catch (\Exception) {
continue;
}
$callingCode = $localDSN->getParam('local', '');
if (empty($callingCode)) {
continue;
}
$geosms->setLocal($callingCode, $adapter);
}
return $geosms;
}
private function createProviderFromDSN(DSN $dsn): Document
{
$host = $dsn->getHost();
$password = $dsn->getPassword();
$user = $dsn->getUser();
$from = System::getEnv('_APP_SMS_FROM');
$provider = new Document([
'$id' => ID::unique(),
'provider' => $host,
'type' => MESSAGE_TYPE_SMS,
'name' => 'Internal SMS',
'enabled' => true,
'credentials' => match ($host) {
'twilio' => [
'accountSid' => $user,
'authToken' => $password,
// Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG
// https://www.twilio.com/docs/messaging/api/service-resource
'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null
],
'textmagic' => [
'username' => $user,
'apiKey' => $password
],
'telesign' => [
'customerId' => $user,
'apiKey' => $password
],
'msg91' => [
'senderId' => $user,
'authKey' => $password,
'templateId' => $dsn->getParam('templateId', $from),
],
'vonage' => [
'apiKey' => $user,
'apiSecret' => $password
],
'fast2sms' => [
'senderId' => $user,
'apiKey' => $password,
'messageId' => $dsn->getParam('messageId'),
'useDLT' => $dsn->getParam('useDLT'),
],
'inforu' => [
'senderId' => $user,
'apiKey' => $password,
],
default => null
},
'options' => match ($host) {
'twilio' => [
'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from
],
default => [
'from' => $from
]
}
]);
return $provider;
}
}