diff --git a/app/init.php b/app/init.php index a32ab08344..80c3670eec 100644 --- a/app/init.php +++ b/app/init.php @@ -195,6 +195,7 @@ const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length']; // Usage metrics const METRIC_TEAMS = 'teams'; const METRIC_USERS = 'users'; +const METRIC_MESSAGES = 'messages'; const METRIC_SESSIONS = 'sessions'; const METRIC_DATABASES = 'databases'; const METRIC_COLLECTIONS = 'collections'; diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 92f9e8fdf4..1f3e29c8d5 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Workers; use Exception; use Utopia\App; use Utopia\CLI\Console; +use Utopia\Database\Document; use Utopia\DSN\DSN; use Utopia\Messaging\Messages\SMS; use Utopia\Messaging\Adapters\SMS\Mock; @@ -15,6 +16,7 @@ use Utopia\Messaging\Adapters\SMS\Twilio; use Utopia\Messaging\Adapters\SMS\Vonage; use Utopia\Platform\Action; use Utopia\Queue\Message; +use Appwrite\Event\Usage; class Messaging extends Action { @@ -43,35 +45,39 @@ class Messaging extends Action $this ->desc('Messaging worker') ->inject('message') - ->callback(fn($message) => $this->action($message)); + ->inject('queueForUsage') + ->callback(fn(Message $message, Usage $queueForUsage) => $this->action($message, $queueForUsage)); } /** * @param Message $message + * @param Usage $queueForUsage * @return void * @throws Exception */ - public function action(Message $message): void + public function action(Message $message, Usage $queueForUsage): void { $payload = $message->getPayload() ?? []; + if (empty($payload)) { + throw new Exception('Missing payload'); + } + if (empty($payload['project'])) { throw new Exception('Project not set in payload'); } - Console::log($payload['project']['$id']); + $project = new Document($payload['project'] ?? []); + + Console::log('Project: ' . $project->getId()); + $denyList = App::getEnv('_APP_SMS_PROJECTS_DENY_LIST', ''); $denyList = explode(',', $denyList); - if (in_array($payload['project']['$id'], $denyList)) { + if (in_array($project->getId(), $denyList)) { Console::error("Project is in the deny list. Skipping ..."); return; } - if (empty($payload)) { - Console::error('Payload arg not found'); - return; - } - if (empty($payload['recipient'])) { Console::error('Recipient arg not found'); return; @@ -112,6 +118,11 @@ class Messaging extends Action try { $sms->send($message); + + $queueForUsage + ->setProject($project) + ->addMetric(METRIC_MESSAGES, 1) + ->trigger(); } catch (\Exception $error) { throw new Exception('Error sending message: ' . $error->getMessage(), 500); }