From 34935b8eeddc418bf884b166dcd196bb37e8120f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 12:07:32 +1200 Subject: [PATCH 01/80] feat(notifications): add Notification event class Replaces the Mail event with a multi-channel Notification event capable of dispatching to email, console, and webhook recipients in a single payload. Maintains the existing Mail surface (setSubject, setRecipient, setBody, etc.) so existing call sites can be migrated mechanically. --- src/Appwrite/Event/Event.php | 4 +- src/Appwrite/Event/Notification.php | 445 ++++++++++++++++++++++++++++ 2 files changed, 447 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Event/Notification.php diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 357442a07c..06af738857 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -18,8 +18,8 @@ class Event public const AUDITS_QUEUE_NAME = 'v1-audits'; public const AUDITS_CLASS_NAME = 'AuditsV1'; - public const MAILS_QUEUE_NAME = 'v1-mails'; - public const MAILS_CLASS_NAME = 'MailsV1'; + public const NOTIFICATIONS_QUEUE_NAME = 'v1-notifications'; + public const NOTIFICATIONS_CLASS_NAME = 'NotificationsV1'; public const FUNCTIONS_QUEUE_NAME = 'v1-functions'; public const FUNCTIONS_CLASS_NAME = 'FunctionsV1'; diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php new file mode 100644 index 0000000000..fa490f3405 --- /dev/null +++ b/src/Appwrite/Event/Notification.php @@ -0,0 +1,445 @@ + + */ + protected array $recipients = []; + + /** + * @var array + */ + protected array $channels = []; + + protected string $template = ''; + protected array $templateParams = []; + protected string $dedupKey = ''; + + /** + * @var array + */ + protected array $permissions = []; + + public function __construct(protected Publisher $publisher) + { + parent::__construct($publisher); + + $this + ->setQueue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) + ->setClass(System::getEnv('_APP_NOTIFICATIONS_CLASS_NAME', Event::NOTIFICATIONS_CLASS_NAME)); + } + + public function setSubject(string $subject): self + { + $this->subject = $subject; + return $this; + } + + public function getSubject(): string + { + return $this->subject; + } + + public function setRecipient(string $recipient): self + { + $this->recipient = $recipient; + return $this; + } + + public function getRecipient(): string + { + return $this->recipient; + } + + public function setBody(string $body): self + { + $this->body = $body; + return $this; + } + + public function getBody(): string + { + return $this->body; + } + + public function setPreview(string $preview): self + { + $this->preview = $preview; + return $this; + } + + public function getPreview(): string + { + return $this->preview; + } + + public function setName(string $name): self + { + $this->name = $name; + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setBodyTemplate(string $bodyTemplate): self + { + $this->bodyTemplate = $bodyTemplate; + return $this; + } + + public function getBodyTemplate(): string + { + return $this->bodyTemplate; + } + + public function setSmtpHost(string $host): self + { + $this->smtp['host'] = $host; + return $this; + } + + public function setSmtpPort(int $port): self + { + $this->smtp['port'] = $port; + return $this; + } + + public function setSmtpUsername(string $username): self + { + $this->smtp['username'] = $username; + return $this; + } + + public function setSmtpPassword(string $password): self + { + $this->smtp['password'] = $password; + return $this; + } + + public function setSmtpSecure(string $secure): self + { + $this->smtp['secure'] = $secure; + return $this; + } + + public function setSmtpSenderEmail(string $senderEmail): self + { + $this->smtp['senderEmail'] = $senderEmail; + return $this; + } + + public function setSmtpSenderName(string $senderName): self + { + $this->smtp['senderName'] = $senderName; + return $this; + } + + public function setSmtpReplyToEmail(string $email): self + { + $this->smtp['replyToEmail'] = $email; + return $this; + } + + public function setSmtpReplyToName(string $name): self + { + $this->smtp['replyToName'] = $name; + return $this; + } + + public function getSmtpHost(): string + { + return $this->smtp['host'] ?? ''; + } + + public function getSmtpPort(): int + { + return $this->smtp['port'] ?? 0; + } + + public function getSmtpUsername(): string + { + return $this->smtp['username'] ?? ''; + } + + public function getSmtpPassword(): string + { + return $this->smtp['password'] ?? ''; + } + + public function getSmtpSecure(): string + { + return $this->smtp['secure'] ?? ''; + } + + public function getSmtpSenderEmail(): string + { + return $this->smtp['senderEmail'] ?? ''; + } + + public function getSmtpSenderName(): string + { + return $this->smtp['senderName'] ?? ''; + } + + public function getSmtpReplyToEmail(): string + { + return $this->smtp['replyToEmail'] ?? ''; + } + + public function getSmtpReplyToName(): string + { + return $this->smtp['replyToName'] ?? ''; + } + + public function getVariables(): array + { + return $this->variables; + } + + public function setVariables(array $variables): self + { + $this->variables = $variables; + return $this; + } + + public function appendVariables(array $variables): self + { + $this->variables = \array_merge($this->variables, $variables); + return $this; + } + + public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text'): self + { + $this->attachment = [ + 'content' => \base64_encode($content), + 'filename' => $filename, + 'encoding' => $encoding, + 'type' => $type, + ]; + return $this; + } + + public function getAttachment(): array + { + return $this->attachment; + } + + public function resetAttachment(): self + { + $this->attachment = []; + return $this; + } + + public function setSenderEmail(string $email): self + { + $this->customMailOptions['senderEmail'] = $email; + return $this; + } + + public function getSenderEmail(): string + { + return $this->customMailOptions['senderEmail'] ?? ''; + } + + public function setSenderName(string $name): self + { + $this->customMailOptions['senderName'] = $name; + return $this; + } + + public function getSenderName(): string + { + return $this->customMailOptions['senderName'] ?? ''; + } + + public function setReplyToEmail(string $email): self + { + $this->customMailOptions['replyToEmail'] = $email; + return $this; + } + + public function getReplyToEmail(): string + { + return $this->customMailOptions['replyToEmail'] ?? ''; + } + + public function setReplyToName(string $name): self + { + $this->customMailOptions['replyToName'] = $name; + return $this; + } + + public function getReplyToName(): string + { + return $this->customMailOptions['replyToName'] ?? ''; + } + + /** + * @param array $recipients + */ + public function setRecipients(array $recipients): self + { + $this->recipients = $recipients; + return $this; + } + + /** + * @return array + */ + public function getRecipients(): array + { + return $this->recipients; + } + + public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL): self + { + $this->recipients[] = ['address' => $address, 'channel' => $channel]; + return $this; + } + + /** + * @param array $channels + */ + public function setChannels(array $channels): self + { + $this->channels = $channels; + return $this; + } + + /** + * @return array + */ + public function getChannels(): array + { + return $this->channels; + } + + public function setTemplate(string $template): self + { + $this->template = $template; + return $this; + } + + public function getTemplate(): string + { + return $this->template; + } + + public function setTemplateParams(array $params): self + { + $this->templateParams = $params; + return $this; + } + + public function getTemplateParams(): array + { + return $this->templateParams; + } + + public function setDedupKey(string $dedupKey): self + { + $this->dedupKey = $dedupKey; + return $this; + } + + public function getDedupKey(): string + { + return $this->dedupKey; + } + + /** + * @param array $permissions + */ + public function setPermissions(array $permissions): self + { + $this->permissions = $permissions; + return $this; + } + + /** + * @return array + */ + public function getPermissions(): array + { + return $this->permissions; + } + + public function reset(): self + { + $this->project = null; + $this->recipient = ''; + $this->name = ''; + $this->subject = ''; + $this->body = ''; + $this->variables = []; + $this->bodyTemplate = ''; + $this->attachment = []; + $this->customMailOptions = []; + $this->recipients = []; + $this->channels = []; + $this->template = ''; + $this->templateParams = []; + $this->dedupKey = ''; + $this->permissions = []; + return $this; + } + + protected function preparePayload(): array + { + $platform = $this->platform; + if (empty($platform)) { + $platform = Config::getParam('platform', []); + } + + $recipients = $this->recipients; + if (empty($recipients) && !empty($this->recipient)) { + $recipients = [[ + 'address' => $this->recipient, + 'channel' => NOTIFICATION_CHANNEL_EMAIL, + ]]; + } + + return [ + 'project' => $this->project, + 'recipient' => $this->recipient, + 'recipients' => $recipients, + 'channels' => $this->channels, + 'template' => $this->template, + 'templateParams' => $this->templateParams, + 'dedupKey' => $this->dedupKey, + 'permissions' => $this->permissions, + 'name' => $this->name, + 'subject' => $this->subject, + 'bodyTemplate' => $this->bodyTemplate, + 'body' => $this->body, + 'preview' => $this->preview, + 'smtp' => $this->smtp, + 'variables' => $this->variables, + 'attachment' => $this->attachment, + 'customMailOptions' => $this->customMailOptions, + 'events' => Event::generateEvents($this->getEvent(), $this->getParams()), + 'platform' => $platform, + ]; + } +} From 99641f24a52d0d4079e158e4ddf17e19e2689ad2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 12:07:38 +1200 Subject: [PATCH 02/80] feat(notifications): add Console and Webhook provider adapters Console adapter persists alerts directly to the project database. Webhook adapter dispatches signed JSON payloads (HMAC-SHA256) to subscriber URLs. --- src/Appwrite/Messaging/Adapter/Console.php | 108 +++++++++++++++++++ src/Appwrite/Messaging/Adapter/Webhook.php | 113 ++++++++++++++++++++ src/Appwrite/Messaging/Messages/Console.php | 62 +++++++++++ src/Appwrite/Messaging/Messages/Webhook.php | 66 ++++++++++++ 4 files changed, 349 insertions(+) create mode 100644 src/Appwrite/Messaging/Adapter/Console.php create mode 100644 src/Appwrite/Messaging/Adapter/Webhook.php create mode 100644 src/Appwrite/Messaging/Messages/Console.php create mode 100644 src/Appwrite/Messaging/Messages/Webhook.php diff --git a/src/Appwrite/Messaging/Adapter/Console.php b/src/Appwrite/Messaging/Adapter/Console.php new file mode 100644 index 0000000000..aa88f59472 --- /dev/null +++ b/src/Appwrite/Messaging/Adapter/Console.php @@ -0,0 +1,108 @@ +process($message); + } + + protected function process(ConsoleMessage $message): array + { + $response = new Response($this->getType()); + $delivered = 0; + + foreach ($message->getRecipients() as $recipient) { + $userId = $recipient['userId'] ?? ''; + $teamId = $recipient['teamId'] ?? ''; + $key = $userId !== '' ? $userId : $teamId; + + try { + $document = new Document([ + '$id' => $message->getMessageId() ?? ID::unique(), + '$permissions' => $this->buildPermissions($userId, $teamId), + 'messageId' => $message->getMessageId(), + 'type' => $message->getType(), + 'channel' => self::TYPE, + 'userId' => $userId !== '' ? $userId : null, + 'teamId' => $teamId !== '' ? $teamId : null, + 'projectId' => $message->getProjectId(), + 'title' => $message->getTitle(), + 'body' => $message->getBody(), + ]); + + $this->database->createDocument('alerts', $document); + $delivered++; + $response->addResult($key); + } catch (\Throwable $error) { + $response->addResult($key, $error->getMessage()); + } + } + + $response->setDeliveredTo($delivered); + return $response->toArray(); + } + + /** + * @return array + */ + private function buildPermissions(string $userId, string $teamId): array + { + $permissions = []; + if ($userId !== '') { + $permissions[] = Permission::read(Role::user($userId)); + $permissions[] = Permission::update(Role::user($userId)); + $permissions[] = Permission::delete(Role::user($userId)); + } + if ($teamId !== '') { + $permissions[] = Permission::read(Role::team($teamId)); + $permissions[] = Permission::update(Role::team($teamId, 'owner')); + $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + } + return $permissions; + } +} diff --git a/src/Appwrite/Messaging/Adapter/Webhook.php b/src/Appwrite/Messaging/Adapter/Webhook.php new file mode 100644 index 0000000000..e2cd3183d5 --- /dev/null +++ b/src/Appwrite/Messaging/Adapter/Webhook.php @@ -0,0 +1,113 @@ +process($message); + } + + protected function process(WebhookMessage $message): array + { + $response = new Response($this->getType()); + $body = \json_encode($message->getPayload(), JSON_THROW_ON_ERROR); + $timestamp = (string) \time(); + + $headers = [ + 'Content-Type: application/json', + self::TIMESTAMP_HEADER . ': ' . $timestamp, + ]; + + $secret = $message->getSigningSecret(); + if ($secret !== null && $secret !== '') { + $signature = \hash_hmac('sha256', $timestamp . '.' . $body, $secret); + $headers[] = self::SIGNATURE_HEADER . ': sha256=' . $signature; + } + + foreach ($message->getHeaders() as $name => $value) { + $headers[] = $name . ': ' . $value; + } + + $delivered = 0; + foreach ($message->getUrls() as $url) { + $result = $this->dispatch('POST', $url, $headers, $body, $message->getTimeout()); + if ($result['statusCode'] >= 200 && $result['statusCode'] < 300 && empty($result['error'])) { + $delivered++; + $response->addResult($url); + } else { + $response->addResult($url, $result['error'] ?: ('HTTP ' . $result['statusCode'])); + } + } + + $response->setDeliveredTo($delivered); + return $response->toArray(); + } + + /** + * @param array $headers + * @return array{statusCode: int, response: string|null, error: string|null} + */ + protected function dispatch(string $method, string $url, array $headers, string $body, int $timeout): array + { + $handle = \curl_init(); + \curl_setopt_array($handle, [ + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_URL => $url, + CURLOPT_HTTPHEADER => $headers, + CURLOPT_POSTFIELDS => $body, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => $timeout, + CURLOPT_CONNECTTIMEOUT => \min(10, $timeout), + CURLOPT_USERAGENT => 'Appwrite Webhook', + ]); + + $output = \curl_exec($handle); + $statusCode = (int) \curl_getinfo($handle, CURLINFO_RESPONSE_CODE); + $error = \curl_error($handle); + \curl_close($handle); + + return [ + 'statusCode' => $statusCode, + 'response' => \is_string($output) ? $output : null, + 'error' => $error !== '' ? $error : null, + ]; + } +} diff --git a/src/Appwrite/Messaging/Messages/Console.php b/src/Appwrite/Messaging/Messages/Console.php new file mode 100644 index 0000000000..6d08c0f0e8 --- /dev/null +++ b/src/Appwrite/Messaging/Messages/Console.php @@ -0,0 +1,62 @@ + $recipients + */ + public function __construct( + protected array $recipients, + protected string $title, + protected string $body, + protected string $type = 'info', + protected ?string $messageId = null, + protected ?string $projectId = null, + ) { + } + + /** + * @return array + */ + public function getRecipients(): array + { + return $this->recipients; + } + + /** + * @return array + */ + public function getTo(): array + { + return $this->recipients; + } + + public function getTitle(): string + { + return $this->title; + } + + public function getBody(): string + { + return $this->body; + } + + public function getType(): string + { + return $this->type; + } + + public function getMessageId(): ?string + { + return $this->messageId; + } + + public function getProjectId(): ?string + { + return $this->projectId; + } +} diff --git a/src/Appwrite/Messaging/Messages/Webhook.php b/src/Appwrite/Messaging/Messages/Webhook.php new file mode 100644 index 0000000000..8572959b6c --- /dev/null +++ b/src/Appwrite/Messaging/Messages/Webhook.php @@ -0,0 +1,66 @@ + $urls + * @param array $payload + * @param array $headers + */ + public function __construct( + protected array $urls, + protected array $payload, + protected ?string $signingSecret = null, + protected array $headers = [], + protected int $timeout = 30, + ) { + } + + /** + * @return array + */ + public function getUrls(): array + { + return $this->urls; + } + + /** + * Alias used by the base adapter to bound max messages per request. + * + * @return array + */ + public function getTo(): array + { + return $this->urls; + } + + /** + * @return array + */ + public function getPayload(): array + { + return $this->payload; + } + + public function getSigningSecret(): ?string + { + return $this->signingSecret; + } + + /** + * @return array + */ + public function getHeaders(): array + { + return $this->headers; + } + + public function getTimeout(): int + { + return $this->timeout; + } +} From 725ba86239c1c8a8c420fd9736161ae6003eafaa Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 12:07:40 +1200 Subject: [PATCH 03/80] feat(notifications): add notifications schema and channel constants Adds the alerts collection used by the console channel, plus channel and provider type constants shared by the worker and adapters. --- app/config/collections/common.php | 126 ++++++++++++++++++++++++++++++ app/init/constants.php | 10 +++ 2 files changed, 136 insertions(+) diff --git a/app/config/collections/common.php b/app/config/collections/common.php index 80bb717423..aec4f393fc 100644 --- a/app/config/collections/common.php +++ b/app/config/collections/common.php @@ -1888,6 +1888,132 @@ return [ ], ], + 'alerts' => [ + '$collection' => ID::custom(Database::METADATA), + '$id' => ID::custom('alerts'), + 'name' => 'Alerts', + 'attributes' => [ + [ + '$id' => ID::custom('messageId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('type'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => false, + 'default' => 'info', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('channel'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('userId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('teamId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('projectId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('title'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('body'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + ], + 'indexes' => [ + [ + '$id' => ID::custom('_key_messageId'), + 'type' => Database::INDEX_UNIQUE, + 'attributes' => ['messageId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_userId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_teamId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['teamId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_projectId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + ], + ], + 'messages' => [ '$collection' => ID::custom(DATABASE::METADATA), '$id' => ID::custom('messages'), diff --git a/app/init/constants.php b/app/init/constants.php index f27d0c7c70..d3bbdaef49 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -254,6 +254,16 @@ const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length']; const MESSAGE_TYPE_EMAIL = 'email'; const MESSAGE_TYPE_SMS = 'sms'; const MESSAGE_TYPE_PUSH = 'push'; +// Notification channels +const NOTIFICATION_CHANNEL_EMAIL = 'email'; +const NOTIFICATION_CHANNEL_SMS = 'sms'; +const NOTIFICATION_CHANNEL_PUSH = 'push'; +const NOTIFICATION_CHANNEL_CONSOLE = 'console'; +const NOTIFICATION_CHANNEL_WEBHOOK = 'webhook'; +// Notification provider types (custom — extend MESSAGE_TYPE_*) +const MESSAGE_TYPE_CONSOLE = 'console'; +const MESSAGE_TYPE_WEBHOOK = 'webhook'; +const RESOURCE_TYPE_ALERTS = 'alerts'; // API key types const API_KEY_STANDARD = 'standard'; const API_KEY_EPHEMERAL = 'ephemeral'; From 90ef2d74879f5ad2193f1855de385c2151773529 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 12:07:42 +1200 Subject: [PATCH 04/80] feat(notifications): add Notifications worker and register it Replaces the Mails worker. Resolves recipients into email, console, and webhook channels, deduplicates deliveries via a hashed dedupKey, and persists alerts for the console channel. --- src/Appwrite/Platform/Services/Workers.php | 3 +- .../Platform/Workers/Notifications.php | 369 ++++++++++++++++++ 2 files changed, 370 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Platform/Workers/Notifications.php diff --git a/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index fa77725f7a..8b4925c5a5 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -7,7 +7,6 @@ use Appwrite\Platform\Workers\Certificates; use Appwrite\Platform\Workers\Deletes; use Appwrite\Platform\Workers\Executions; use Appwrite\Platform\Workers\Functions; -use Appwrite\Platform\Workers\Mails; use Appwrite\Platform\Workers\Messaging; use Appwrite\Platform\Workers\Migrations; use Appwrite\Platform\Workers\StatsResources; @@ -26,8 +25,8 @@ class Workers extends Service ->addAction(Deletes::getName(), new Deletes()) ->addAction(Executions::getName(), new Executions()) ->addAction(Functions::getName(), new Functions()) - ->addAction(Mails::getName(), new Mails()) ->addAction(Messaging::getName(), new Messaging()) + ->addAction(Notifications::getName(), new Notifications()) ->addAction(Webhooks::getName(), new Webhooks()) ->addAction(StatsUsage::getName(), new StatsUsage()) ->addAction(Migrations::getName(), new Migrations()) diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php new file mode 100644 index 0000000000..b38f3dde1c --- /dev/null +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -0,0 +1,369 @@ + + */ + protected array $richTextParams = [ + 'b' => '', + '/b' => '', + ]; + + public static function getName(): string + { + return 'notifications'; + } + + public function __construct() + { + $this + ->desc('Notifications worker') + ->inject('message') + ->inject('project') + ->inject('register') + ->inject('dbForProject') + ->inject('log') + ->callback($this->action(...)); + } + + public function action(Message $message, Document $project, Registry $register, Database $dbForProject, Log $log): void + { + Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); + $payload = $message->getPayload(); + + if (empty($payload)) { + throw new Exception('Missing payload'); + } + + $dedupKey = $payload['dedupKey'] ?? ''; + $messageId = $dedupKey !== '' ? \md5($dedupKey) : ''; + + if ($messageId !== '' && $this->alreadyDelivered($dbForProject, $messageId)) { + $log->addTag('dedup', 'hit'); + return; + } + + $recipients = $this->resolveRecipients($payload); + if (empty($recipients)) { + throw new Exception('No recipients in payload'); + } + + foreach ($recipients as $recipient) { + $channel = $recipient['channel']; + try { + $this->dispatch($channel, $recipient['address'], $payload, $register, $dbForProject, $log); + if ($messageId !== '') { + $this->persistAlert($dbForProject, $messageId, $channel, $recipient['address'], $payload); + } + } catch (Throwable $error) { + $log->addTag('channel', $channel); + $log->addTag('error', $error->getMessage()); + throw $error; + } + } + } + + /** + * @return array + */ + private function resolveRecipients(array $payload): array + { + $recipients = $payload['recipients'] ?? []; + if (!empty($recipients)) { + return $recipients; + } + + $address = $payload['recipient'] ?? ''; + if ($address === '') { + return []; + } + + return [['address' => $address, 'channel' => NOTIFICATION_CHANNEL_EMAIL]]; + } + + private function alreadyDelivered(Database $database, string $messageId): bool + { + try { + $existing = $database->getDocument('alerts', $messageId); + return !$existing->isEmpty(); + } catch (Throwable) { + return false; + } + } + + private function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void + { + switch ($channel) { + case NOTIFICATION_CHANNEL_EMAIL: + $this->dispatchEmail($address, $payload, $register, $log); + return; + case NOTIFICATION_CHANNEL_CONSOLE: + $this->dispatchConsole($address, $payload, $database); + return; + case NOTIFICATION_CHANNEL_WEBHOOK: + $this->dispatchWebhook($address, $payload); + return; + default: + throw new Exception('Unsupported notification channel: ' . $channel); + } + } + + private function dispatchEmail(string $address, array $payload, Registry $register, Log $log): void + { + $smtp = $payload['smtp'] ?? []; + if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { + throw new Exception('Skipped email notification. No SMTP configuration has been set.'); + } + + $type = empty($smtp) ? 'cloud' : 'smtp'; + $log->addTag('type', $type); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; + $hostname = System::getEnv('_APP_CONSOLE_DOMAIN'); + + $subject = $payload['subject'] ?? ''; + $variables = $payload['variables'] ?? []; + $variables = \array_merge($variables, $payload['templateParams'] ?? []); + $variables['host'] = $protocol . '://' . $hostname; + $name = $payload['name'] ?? ''; + $body = $payload['body'] ?? ''; + $preview = $payload['preview'] ?? ''; + + $variables['subject'] = $subject; + $variables['heading'] = $variables['heading'] ?? $subject; + $variables['year'] = date('Y'); + + $attachment = $payload['attachment'] ?? []; + $bodyTemplate = $payload['bodyTemplate'] ?? ''; + if (empty($bodyTemplate)) { + $bodyTemplate = $payload['template'] ?? ''; + } + if (empty($bodyTemplate)) { + $bodyTemplate = __DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'; + } + + $bodyTemplate = Template::fromFile($bodyTemplate); + $bodyTemplate->setParam('{{body}}', $body, escapeHtml: false); + foreach ($variables as $key => $value) { + $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: $key !== 'redirect'); + } + foreach ($this->richTextParams as $key => $value) { + $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: false); + } + + $previewWhitespace = ''; + if (!empty($preview)) { + $previewTemplate = Template::fromString($preview); + foreach ($variables as $key => $value) { + $previewTemplate->setParam('{{' . $key . '}}', $value); + } + $preview = \strip_tags($previewTemplate->render()); + + $previewLen = \strlen($preview); + if ($previewLen < $this->previewMaxLen) { + $previewWhitespace = \str_repeat($this->whitespaceCodes, $this->previewMaxLen - $previewLen); + } + } + + $bodyTemplate->setParam('{{preview}}', $preview); + $bodyTemplate->setParam('{{previewWhitespace}}', $previewWhitespace, false); + + $body = $bodyTemplate->render(); + + $subjectTemplate = Template::fromString($subject); + foreach ($variables as $key => $value) { + $subjectTemplate->setParam('{{' . $key . '}}', $value); + } + $subject = \strip_tags($subjectTemplate->render()); + + /** @var EmailAdapter $adapter */ + $adapter = empty($smtp) + ? $register->get('smtp') + : new SMTP( + host: $smtp['host'], + port: (int) $smtp['port'], + username: $smtp['username'] ?? '', + password: $smtp['password'] ?? '', + smtpSecure: $smtp['secure'] ?? '', + smtpAutoTLS: false, + xMailer: 'Appwrite Mailer', + timeout: 10, + keepAlive: true, + timelimit: 30, + ); + + $defaultFromEmail = System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM); + $defaultFromName = \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); + + $fromEmail = !empty($smtp) ? ($smtp['senderEmail'] ?? $defaultFromEmail) : $defaultFromEmail; + $fromName = !empty($smtp) ? ($smtp['senderName'] ?? $defaultFromName) : $defaultFromName; + $replyTo = $defaultFromEmail; + $replyToName = $defaultFromName; + + $customMailOptions = $payload['customMailOptions'] ?? []; + + if (!empty($customMailOptions['senderEmail'])) { + $fromEmail = $customMailOptions['senderEmail']; + } + if (!empty($customMailOptions['senderName'])) { + $fromName = $customMailOptions['senderName']; + } + + if (!empty($customMailOptions['replyToEmail']) || !empty($customMailOptions['replyToName'])) { + $replyTo = $customMailOptions['replyToEmail'] ?? $replyTo; + $replyToName = $customMailOptions['replyToName'] ?? $replyToName; + } elseif (!empty($smtp)) { + $smtpReplyToEmail = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; + $replyTo = !empty($smtpReplyToEmail) ? $smtpReplyToEmail : ($smtp['senderEmail'] ?? $replyTo); + $replyToName = !empty($smtp['replyToName']) ? $smtp['replyToName'] : ($smtp['senderName'] ?? $replyToName); + } + + $attachments = null; + if (!empty($attachment['content'] ?? '')) { + $attachments = [ + new Attachment( + name: $attachment['filename'] ?? 'unknown.file', + path: '', + type: $attachment['type'] ?? 'plain/text', + content: \base64_decode($attachment['content']), + ), + ]; + } + + $emailMessage = new EmailMessage( + to: [['email' => $address, 'name' => $name]], + subject: $subject, + content: $body, + fromName: $fromName, + fromEmail: $fromEmail, + replyToName: $replyToName, + replyToEmail: $replyTo, + attachments: $attachments, + html: true, + ); + + try { + $adapter->send($emailMessage); + } catch (\Throwable $error) { + if ($type === 'smtp') { + throw new Exception('Error sending notification: ' . $error->getMessage(), 401); + } + throw new Exception('Error sending notification: ' . $error->getMessage(), 500); + } + } + + private function dispatchConsole(string $address, array $payload, Database $database): void + { + $project = $payload['project'] ?? null; + $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; + + $title = $payload['subject'] ?? ''; + $body = $payload['body'] ?? ''; + $params = $payload['templateParams'] ?? ($payload['variables'] ?? []); + if ($title !== '' && !empty($params)) { + $rendered = Template::fromString($title); + foreach ($params as $key => $value) { + $rendered->setParam('{{' . $key . '}}', (string) $value); + } + $title = \strip_tags($rendered->render()); + } + + $recipients = [['userId' => $address]]; + + $dedupKey = $payload['dedupKey'] ?? ''; + $messageId = $dedupKey !== '' ? \md5($dedupKey) : null; + + $consoleMessage = new ConsoleMessage( + recipients: $recipients, + title: $title, + body: $body, + type: 'info', + messageId: $messageId, + projectId: $projectId, + ); + + $adapter = new ConsoleAdapter($database); + $adapter->send($consoleMessage); + } + + private function dispatchWebhook(string $address, array $payload): void + { + $body = [ + 'subject' => $payload['subject'] ?? '', + 'body' => $payload['body'] ?? '', + 'template' => $payload['template'] ?? '', + 'params' => $payload['templateParams'] ?? [], + 'project' => \is_array($payload['project'] ?? null) ? ($payload['project']['$id'] ?? null) : null, + 'dedupKey' => $payload['dedupKey'] ?? '', + 'events' => $payload['events'] ?? [], + ]; + + $secret = System::getEnv('_APP_NOTIFICATIONS_WEBHOOK_SECRET', '') ?: null; + + $message = new WebhookMessage( + urls: [$address], + payload: $body, + signingSecret: $secret, + ); + + $adapter = new WebhookAdapter(); + $result = $adapter->send($message); + + if (($result['deliveredTo'] ?? 0) === 0) { + $error = $result['results'][0]['error'] ?? 'Unknown error'; + throw new Exception('Webhook delivery failed: ' . $error); + } + } + + private function persistAlert(Database $database, string $messageId, string $channel, string $address, array $payload): void + { + $project = $payload['project'] ?? null; + $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; + $permissions = $payload['permissions'] ?? []; + + $document = new Document([ + '$id' => $messageId . '_' . \substr(\md5($channel . $address), 0, 8), + '$permissions' => $permissions, + 'messageId' => $messageId, + 'type' => 'info', + 'channel' => $channel, + 'userId' => $channel === NOTIFICATION_CHANNEL_CONSOLE ? $address : null, + 'projectId' => $projectId, + 'title' => $payload['subject'] ?? '', + 'body' => $payload['body'] ?? '', + ]); + + try { + $database->createDocument('alerts', $document); + } catch (DuplicateException) { + // Idempotent — duplicate persistence is fine + } + } +} From 6bde54675e479e2a740463f9fc5fb98526c78f67 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:10:13 +1200 Subject: [PATCH 05/80] test(notifications): unit tests for worker and adapters Covers the dedup short-circuit, per-channel dispatch routing, alert persistence, error tagging, and legacy single-recipient fallback in the Notifications worker, plus the Console adapter's permission shape and the Webhook adapter's HMAC-SHA256 signing contract, header layout, response handling, and unsigned-when-secret-missing behaviour. Worker dispatch helpers move from private to protected so a test spy can override them without monkey-patching. The Swoole runtime hook flag mutation is now guarded by class_exists so the action can run under bare PHPUnit (no Swoole extension on the test host). Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Platform/Workers/Notifications.php | 12 +- tests/unit/Messaging/Adapter/ConsoleTest.php | 125 +++++++++ tests/unit/Messaging/Adapter/WebhookTest.php | 195 ++++++++++++++ .../Platform/Workers/NotificationsTest.php | 249 ++++++++++++++++++ 4 files changed, 576 insertions(+), 5 deletions(-) create mode 100644 tests/unit/Messaging/Adapter/ConsoleTest.php create mode 100644 tests/unit/Messaging/Adapter/WebhookTest.php create mode 100644 tests/unit/Platform/Workers/NotificationsTest.php diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index b38f3dde1c..c50e61a323 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -55,7 +55,9 @@ class Notifications extends Action public function action(Message $message, Document $project, Registry $register, Database $dbForProject, Log $log): void { - Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); + if (\class_exists(Runtime::class)) { + Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); + } $payload = $message->getPayload(); if (empty($payload)) { @@ -118,7 +120,7 @@ class Notifications extends Action } } - private function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void + protected function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void { switch ($channel) { case NOTIFICATION_CHANNEL_EMAIL: @@ -135,7 +137,7 @@ class Notifications extends Action } } - private function dispatchEmail(string $address, array $payload, Registry $register, Log $log): void + protected function dispatchEmail(string $address, array $payload, Registry $register, Log $log): void { $smtp = $payload['smtp'] ?? []; if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { @@ -279,7 +281,7 @@ class Notifications extends Action } } - private function dispatchConsole(string $address, array $payload, Database $database): void + protected function dispatchConsole(string $address, array $payload, Database $database): void { $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; @@ -313,7 +315,7 @@ class Notifications extends Action $adapter->send($consoleMessage); } - private function dispatchWebhook(string $address, array $payload): void + protected function dispatchWebhook(string $address, array $payload): void { $body = [ 'subject' => $payload['subject'] ?? '', diff --git a/tests/unit/Messaging/Adapter/ConsoleTest.php b/tests/unit/Messaging/Adapter/ConsoleTest.php new file mode 100644 index 0000000000..1f5ed96ff4 --- /dev/null +++ b/tests/unit/Messaging/Adapter/ConsoleTest.php @@ -0,0 +1,125 @@ +authorization = new Authorization(); + $this->authorization->addRole(Role::any()->toString()); + + $this->database = new Database(new Memory(), new Cache(new NoCache())); + $this->database + ->setAuthorization($this->authorization) + ->setDatabase('alertsTests') + ->setNamespace('alerts_' . \uniqid()); + + $this->database->create(); + $this->database->createCollection('alerts', [], [], [Permission::create(Role::any()), Permission::read(Role::any())], false); + $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); + } + + protected function tearDown(): void + { + $this->authorization->cleanRoles(); + $this->authorization->addRole(Role::any()->toString()); + } + + public function testWritesAlertWithCorrectSchema(): void + { + $message = new ConsoleMessage( + recipients: [['userId' => 'user-1']], + title: 'Hello', + body: 'World', + type: 'info', + messageId: ID::custom('msg-aaa'), + projectId: 'project-1', + ); + + $adapter = new Console($this->database); + $result = $adapter->send($message); + + $this->assertSame(1, $result['deliveredTo']); + + $stored = $this->database->getDocument('alerts', 'msg-aaa'); + $this->assertFalse($stored->isEmpty()); + $this->assertSame('msg-aaa', $stored->getAttribute('messageId')); + $this->assertSame('console', $stored->getAttribute('channel')); + $this->assertSame('user-1', $stored->getAttribute('userId')); + $this->assertSame('project-1', $stored->getAttribute('projectId')); + $this->assertSame('Hello', $stored->getAttribute('title')); + $this->assertSame('World', $stored->getAttribute('body')); + $this->assertSame('info', $stored->getAttribute('type')); + } + + public function testUserPermissionsScopedToRecipient(): void + { + $message = new ConsoleMessage( + recipients: [['userId' => 'user-2']], + title: 'Title', + body: 'Body', + messageId: ID::custom('msg-perms-user'), + ); + + (new Console($this->database))->send($message); + + $stored = $this->database->getDocument('alerts', 'msg-perms-user'); + $permissions = $stored->getPermissions(); + + $this->assertContains(Permission::read(Role::user('user-2')), $permissions); + $this->assertContains(Permission::update(Role::user('user-2')), $permissions); + $this->assertContains(Permission::delete(Role::user('user-2')), $permissions); + } + + public function testTeamRecipientGrantsTeamReadAndOwnerWrite(): void + { + $message = new ConsoleMessage( + recipients: [['teamId' => 'team-9']], + title: 'Heads up', + body: '...', + messageId: ID::custom('msg-team'), + ); + + (new Console($this->database))->send($message); + + $stored = $this->database->getDocument('alerts', 'msg-team'); + $permissions = $stored->getPermissions(); + + $this->assertContains(Permission::read(Role::team('team-9')), $permissions); + $this->assertContains(Permission::update(Role::team('team-9', 'owner')), $permissions); + $this->assertContains(Permission::delete(Role::team('team-9', 'owner')), $permissions); + } + + public function testRejectsForeignMessageType(): void + { + $adapter = new Console($this->database); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Invalid message type.'); + + // ConsoleMessage extends nothing — pass an unrelated Message implementation + $adapter->send(new \Appwrite\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); + } +} diff --git a/tests/unit/Messaging/Adapter/WebhookTest.php b/tests/unit/Messaging/Adapter/WebhookTest.php new file mode 100644 index 0000000000..5ff7db987f --- /dev/null +++ b/tests/unit/Messaging/Adapter/WebhookTest.php @@ -0,0 +1,195 @@ +, body: string, timeout: int}> + */ + public array $captured = []; + + /** @var array{statusCode: int, response: string|null, error: string|null} */ + public array $response = ['statusCode' => 200, 'response' => 'OK', 'error' => null]; + + protected function dispatch(string $method, string $url, array $headers, string $body, int $timeout): array + { + $this->captured[] = [ + 'method' => $method, + 'url' => $url, + 'headers' => $headers, + 'body' => $body, + 'timeout' => $timeout, + ]; + return $this->response; + } +} + +class WebhookTest extends TestCase +{ + public function testPostsExpectedBodyShape(): void + { + $adapter = new CapturingWebhook(); + $payload = [ + 'subject' => 'Hello', + 'body' => 'World', + 'recipient' => 'ops@example.test', + 'metadata' => ['foo' => 'bar'], + ]; + + $message = new WebhookMessage( + urls: ['https://hooks.example.test/notify'], + payload: $payload, + ); + + $result = $adapter->send($message); + + $this->assertSame(1, $result['deliveredTo']); + $this->assertCount(1, $adapter->captured); + + $request = $adapter->captured[0]; + $this->assertSame('POST', $request['method']); + $this->assertSame('https://hooks.example.test/notify', $request['url']); + + $sent = \json_decode($request['body'], true); + $this->assertSame($payload, $sent); + + $headerLine = \implode("\n", $request['headers']); + $this->assertStringContainsString('Content-Type: application/json', $headerLine); + $this->assertStringContainsString('X-Appwrite-Webhook-Timestamp:', $headerLine); + } + + public function testSigningSecretProducesHmacSha256Signature(): void + { + $adapter = new CapturingWebhook(); + $payload = ['subject' => 'Signed', 'body' => 'B']; + $secret = 'super-secret'; + + $message = new WebhookMessage( + urls: ['https://hooks.example.test/signed'], + payload: $payload, + signingSecret: $secret, + ); + + $adapter->send($message); + + $headers = $adapter->captured[0]['headers']; + $body = $adapter->captured[0]['body']; + + $timestamp = null; + $signature = null; + foreach ($headers as $header) { + if (\str_starts_with($header, 'X-Appwrite-Webhook-Timestamp: ')) { + $timestamp = \substr($header, \strlen('X-Appwrite-Webhook-Timestamp: ')); + } elseif (\str_starts_with($header, 'X-Appwrite-Webhook-Signature: ')) { + $signature = \substr($header, \strlen('X-Appwrite-Webhook-Signature: ')); + } + } + + $this->assertNotNull($timestamp, 'timestamp header must be present'); + $this->assertNotNull($signature, 'signature header must be present when secret is set'); + $this->assertStringStartsWith('sha256=', $signature); + + $expected = 'sha256=' . \hash_hmac('sha256', $timestamp . '.' . $body, $secret); + $this->assertSame($expected, $signature); + } + + public function testNoSecretLeavesPayloadUnsigned(): void + { + $adapter = new CapturingWebhook(); + $message = new WebhookMessage( + urls: ['https://hooks.example.test/unsigned'], + payload: ['x' => 1], + signingSecret: null, + ); + + $adapter->send($message); + + $headerLine = \implode("\n", $adapter->captured[0]['headers']); + $this->assertStringNotContainsString('X-Appwrite-Webhook-Signature', $headerLine); + } + + public function testEmptySecretIsTreatedAsUnsigned(): void + { + $adapter = new CapturingWebhook(); + $message = new WebhookMessage( + urls: ['https://hooks.example.test/empty-secret'], + payload: ['x' => 1], + signingSecret: '', + ); + + $adapter->send($message); + + $headerLine = \implode("\n", $adapter->captured[0]['headers']); + $this->assertStringNotContainsString('X-Appwrite-Webhook-Signature', $headerLine); + } + + public function testTwoXxIsSuccess(): void + { + $adapter = new CapturingWebhook(); + $adapter->response = ['statusCode' => 204, 'response' => '', 'error' => null]; + $message = new WebhookMessage(urls: ['https://hooks.example.test/ok'], payload: []); + + $result = $adapter->send($message); + + $this->assertSame(1, $result['deliveredTo']); + } + + public function testNonTwoXxSurfacesError(): void + { + $adapter = new CapturingWebhook(); + $adapter->response = ['statusCode' => 503, 'response' => 'Server', 'error' => null]; + $message = new WebhookMessage(urls: ['https://hooks.example.test/fail'], payload: []); + + $result = $adapter->send($message); + + $this->assertSame(0, $result['deliveredTo']); + $error = $result['results'][0]['error'] ?? null; + $this->assertSame('HTTP 503', $error); + } + + public function testCurlErrorSurfacesAsResultError(): void + { + $adapter = new CapturingWebhook(); + $adapter->response = ['statusCode' => 0, 'response' => null, 'error' => 'connection refused']; + $message = new WebhookMessage(urls: ['https://hooks.example.test/down'], payload: []); + + $result = $adapter->send($message); + + $this->assertSame(0, $result['deliveredTo']); + $this->assertSame('connection refused', $result['results'][0]['error']); + } + + public function testCustomHeadersForwarded(): void + { + $adapter = new CapturingWebhook(); + $message = new WebhookMessage( + urls: ['https://hooks.example.test/with-headers'], + payload: [], + headers: ['X-Custom' => 'value'], + ); + + $adapter->send($message); + + $headerLine = \implode("\n", $adapter->captured[0]['headers']); + $this->assertStringContainsString('X-Custom: value', $headerLine); + } + + public function testRejectsForeignMessageType(): void + { + $adapter = new CapturingWebhook(); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Invalid message type.'); + + $adapter->send(new \Appwrite\Messaging\Messages\Console(recipients: [['userId' => 'u']], title: 't', body: 'b')); + } +} diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php new file mode 100644 index 0000000000..c7889851a8 --- /dev/null +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -0,0 +1,249 @@ +}> */ + public array $dispatched = []; + + /** @var array */ + public array $throwOn = []; + + protected function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void + { + $this->dispatched[] = [ + 'channel' => $channel, + 'address' => $address, + 'payload' => $payload, + ]; + + if (isset($this->throwOn[$channel])) { + throw $this->throwOn[$channel]; + } + } +} + +class NotificationsTest extends TestCase +{ + private Database $database; + private Authorization $authorization; + private Registry $registry; + private Document $project; + private Log $log; + + protected function setUp(): void + { + $this->authorization = new Authorization(); + $this->authorization->addRole(Role::any()->toString()); + + $this->database = new Database(new Memory(), new Cache(new NoCache())); + $this->database + ->setAuthorization($this->authorization) + ->setDatabase('notifTests') + ->setNamespace('notif_' . \uniqid()); + + $this->database->create(); + $this->database->createCollection( + 'alerts', + [], + [], + [Permission::create(Role::any()), Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any())], + false, + ); + $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); + + $this->registry = new Registry(); + $this->project = new Document(['$id' => 'project-x']); + $this->log = new Log(); + } + + protected function tearDown(): void + { + $this->authorization->cleanRoles(); + $this->authorization->addRole(Role::any()->toString()); + } + + private function buildMessage(array $payload): Message + { + return new Message([ + 'pid' => 'pid', + 'queue' => 'v1-notifications', + 'timestamp' => \time(), + 'payload' => $payload, + ]); + } + + public function testDispatchesPerChannelToCorrectAdapter(): void + { + $worker = new SpyNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'user@example.test', 'channel' => NOTIFICATION_CHANNEL_EMAIL], + ['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], + ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK], + ], + 'subject' => 'Hi', + 'body' => 'Body', + 'dedupKey' => 'event-1', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(3, $worker->dispatched); + $channels = \array_map(static fn ($d) => $d['channel'], $worker->dispatched); + $this->assertSame([NOTIFICATION_CHANNEL_EMAIL, NOTIFICATION_CHANNEL_CONSOLE, NOTIFICATION_CHANNEL_WEBHOOK], $channels); + } + + public function testPersistsOneAlertPerRecipientChannel(): void + { + $worker = new SpyNotifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], + ['address' => 'user-2', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], + ], + 'subject' => 'Heads up', + 'body' => 'Read me', + 'dedupKey' => 'evt-multi', + 'permissions' => [Permission::read(Role::any())], + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $rows = $this->database->find('alerts'); + $this->assertCount(2, $rows); + $userIds = \array_map(static fn (Document $row) => $row->getAttribute('userId'), $rows); + \sort($userIds); + $this->assertSame(['user-1', 'user-2'], $userIds); + + foreach ($rows as $row) { + $this->assertSame(\md5('evt-multi'), $row->getAttribute('messageId')); + $this->assertSame('console', $row->getAttribute('channel')); + $this->assertSame('project-x', $row->getAttribute('projectId')); + $this->assertSame('Heads up', $row->getAttribute('title')); + } + } + + public function testDedupHitShortCircuitsBeforeDispatch(): void + { + $worker = new SpyNotifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE]], + 'subject' => 'Sub', + 'body' => 'B', + 'dedupKey' => 'dup-key', + ]; + + // First run delivers and persists. + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + $this->assertCount(1, $worker->dispatched); + + // Manually insert a row with the dedup messageId so alreadyDelivered() returns true. + $messageId = \md5('dup-key'); + $this->database->createDocument('alerts', new Document([ + '$id' => $messageId, + '$permissions' => [Permission::read(Role::any())], + 'messageId' => $messageId, + 'channel' => 'console', + 'title' => 'x', + 'body' => 'y', + ])); + + $worker->dispatched = []; + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + $this->assertCount(0, $worker->dispatched, 'second invocation must short-circuit on dedup hit'); + } + + public function testMissingRecipientsAndAddressThrows(): void + { + $worker = new SpyNotifications(); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('No recipients in payload'); + + $worker->action( + $this->buildMessage(['project' => ['$id' => 'project-x'], 'subject' => '', 'body' => '']), + $this->project, + $this->registry, + $this->database, + $this->log, + ); + } + + public function testFallbackToLegacyRecipient(): void + { + $worker = new SpyNotifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipient' => 'legacy@example.test', + 'subject' => 'X', + 'body' => 'Y', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(1, $worker->dispatched); + $this->assertSame('legacy@example.test', $worker->dispatched[0]['address']); + $this->assertSame(NOTIFICATION_CHANNEL_EMAIL, $worker->dispatched[0]['channel']); + } + + public function testDispatchErrorTagsLogAndPropagates(): void + { + $worker = new SpyNotifications(); + $worker->throwOn[NOTIFICATION_CHANNEL_WEBHOOK] = new \RuntimeException('boom'); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK]], + 'subject' => 's', + 'body' => 'b', + 'dedupKey' => 'err-1', + ]; + + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + $this->fail('expected exception to propagate'); + } catch (\Throwable $error) { + $this->assertSame('boom', $error->getMessage()); + } + + $tags = $this->log->getTags(); + $this->assertSame(NOTIFICATION_CHANNEL_WEBHOOK, $tags['channel'] ?? null); + $this->assertSame('boom', $tags['error'] ?? null); + + $rows = $this->database->find('alerts'); + $this->assertCount(0, $rows, 'failed dispatch must not persist alert'); + } +} From df16c84b51a3ebe4baba2b7adedcb85c8f367b69 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:10:20 +1200 Subject: [PATCH 06/80] test(notifications): e2e coverage for notifications queue health MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the Notifications e2e suite under tests/e2e/Services/Notifications. Asserts that the live notifications queue depth is reported via GET /v1/health/queue/notifications, that the threshold guard is honoured, and that the failed-jobs endpoint accepts the v1-notifications queue name. Dispatch routing, dedup, and webhook signing are covered by the unit suite — the worker cannot be deterministically driven through the live queue from a test client without an admin enqueue endpoint, so the e2e file pins the public health contract that ops dashboards and KEDA scale on. Co-Authored-By: Claude Opus 4.7 (1M context) --- phpunit.xml | 1 + .../NotificationsCustomServerTest.php | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/e2e/Services/Notifications/NotificationsCustomServerTest.php diff --git a/phpunit.xml b/phpunit.xml index 9748c5a5c8..4d15f60ce0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -37,6 +37,7 @@ ./tests/e2e/Services/ProjectWebhooks ./tests/e2e/Services/Messaging ./tests/e2e/Services/Migrations + ./tests/e2e/Services/Notifications ./tests/e2e/Services/Project ./tests/e2e/Services/Functions/FunctionsBase.php ./tests/e2e/Services/Functions/FunctionsCustomServerTest.php diff --git a/tests/e2e/Services/Notifications/NotificationsCustomServerTest.php b/tests/e2e/Services/Notifications/NotificationsCustomServerTest.php new file mode 100644 index 0000000000..37f3dead10 --- /dev/null +++ b/tests/e2e/Services/Notifications/NotificationsCustomServerTest.php @@ -0,0 +1,67 @@ +client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['size']); + $this->assertGreaterThanOrEqual(0, $response['body']['size']); + } + + public function testHealthQueueNotificationsThresholdGuard(): void + { + $response = $this->client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), ['threshold' => '0']); + + // threshold=0 means any non-zero queue length is unhealthy. The size + // is racy under load, so accept either response — the contract is + // that the parameter is honoured, not that the queue is hot. + $this->assertContains($response['headers']['status-code'], [200, 503]); + } + + public function testHealthQueueFailedAcceptsNotifications(): void + { + $response = $this->client->call(Client::METHOD_GET, '/health/queue/failed/v1-notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['size']); + } +} From a546c416620196207f03868ce096e4562d820465 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:10:28 +1200 Subject: [PATCH 07/80] docs(notifications): document _APP_NOTIFICATIONS_WEBHOOK_SECRET MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Registers the webhook signing secret in app/config/variables.php under a new Notifications category, threads it through the worker container in docker-compose.yml and tests/resources/docker/docker-compose.yml, and adds an empty default to .env so operators see the knob alongside the existing SMTP block. When set, outbound webhook deliveries from the notifications worker include an X-Appwrite-Webhook-Signature header carrying sha256=. When unset, the worker delivers payloads unsigned — receivers must decide whether to accept them. Co-Authored-By: Claude Opus 4.7 (1M context) --- .env | 1 + app/config/variables.php | 15 +++++++++++++++ docker-compose.yml | 1 + tests/resources/docker/docker-compose.yml | 1 + 4 files changed, 18 insertions(+) diff --git a/.env b/.env index 4a6a3ac344..47c0e2a607 100644 --- a/.env +++ b/.env @@ -88,6 +88,7 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME=user _APP_SMTP_PASSWORD=password +_APP_NOTIFICATIONS_WEBHOOK_SECRET= _APP_SMS_PROVIDER=sms://username:password@mock _APP_SMS_FROM=+123456789 _APP_SMS_PROJECTS_DENY_LIST= diff --git a/app/config/variables.php b/app/config/variables.php index c834656ff4..ee539928f6 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -579,6 +579,21 @@ return [ ], ], ], + [ + 'category' => 'Notifications', + 'description' => 'Settings for the notifications worker. Notifications fan out to email, in-app console alerts, and outbound webhooks via the dedicated worker.', + 'variables' => [ + [ + 'name' => '_APP_NOTIFICATIONS_WEBHOOK_SECRET', + 'description' => 'Shared secret used to sign outbound notification webhooks with HMAC-SHA256. When set, every webhook delivery includes an X-Appwrite-Webhook-Signature header carrying `sha256=` over `.`. Leave empty to deliver unsigned payloads. Empty by default.', + 'introduction' => '', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + ], + ], [ 'category' => 'Phone', 'description' => '', diff --git a/docker-compose.yml b/docker-compose.yml index da5efac438..dfe6769b72 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -801,6 +801,7 @@ services: - _APP_SMTP_SECURE - _APP_SMTP_USERNAME - _APP_SMTP_PASSWORD + - _APP_NOTIFICATIONS_WEBHOOK_SECRET - _APP_LOGGING_CONFIG - _APP_DOMAIN - _APP_OPTIONS_FORCE_HTTPS diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 47a69f077b..2d4b0f82a5 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -291,6 +291,7 @@ services: - _APP_DB_ADAPTER - _APP_SMTP_HOST - _APP_SMTP_PORT + - _APP_NOTIFICATIONS_WEBHOOK_SECRET appwrite-worker-builds: entrypoint: worker-builds From 5ee92e184400964e44f4c661dae7461d652c987c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:55:03 +1200 Subject: [PATCH 08/80] fix(notifications): move alerts collection to platform DB Alerts is a platform-DB-only concern. Move from common.php to platform.php where it belongs alongside other platform collections. --- app/config/collections/common.php | 126 ---------------------------- app/config/collections/platform.php | 126 ++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 126 deletions(-) diff --git a/app/config/collections/common.php b/app/config/collections/common.php index aec4f393fc..80bb717423 100644 --- a/app/config/collections/common.php +++ b/app/config/collections/common.php @@ -1888,132 +1888,6 @@ return [ ], ], - 'alerts' => [ - '$collection' => ID::custom(Database::METADATA), - '$id' => ID::custom('alerts'), - 'name' => 'Alerts', - 'attributes' => [ - [ - '$id' => ID::custom('messageId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('type'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 64, - 'signed' => true, - 'required' => false, - 'default' => 'info', - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('channel'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 64, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('userId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('teamId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('projectId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('title'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 256, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('body'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 16384, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - ], - 'indexes' => [ - [ - '$id' => ID::custom('_key_messageId'), - 'type' => Database::INDEX_UNIQUE, - 'attributes' => ['messageId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_userId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['userId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_teamId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['teamId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_projectId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['projectId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - ], - ], - 'messages' => [ '$collection' => ID::custom(DATABASE::METADATA), '$id' => ID::custom('messages'), diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 6195c11724..b23d170a58 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1011,6 +1011,132 @@ $platformCollections = [ ], ], + 'alerts' => [ + '$collection' => ID::custom(Database::METADATA), + '$id' => ID::custom('alerts'), + 'name' => 'Alerts', + 'attributes' => [ + [ + '$id' => ID::custom('messageId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('type'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => false, + 'default' => 'info', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('channel'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('userId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('teamId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('projectId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('title'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 256, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('body'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + ], + 'indexes' => [ + [ + '$id' => ID::custom('_key_messageId'), + 'type' => Database::INDEX_UNIQUE, + 'attributes' => ['messageId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_userId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_teamId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['teamId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_projectId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + ], + ], + 'certificates' => [ '$collection' => ID::custom(Database::METADATA), '$id' => ID::custom('certificates'), From 5cbbacaae574d65ba3fed18b20a17264c2eb69d6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:56:35 +1200 Subject: [PATCH 09/80] refactor(notifications): move adapters under Appwrite\Utopia\Messaging Mirror the upstream Utopia\Messaging package namespace under the Appwrite\Utopia\Messaging prefix, matching the convention used by Appwrite\Utopia\Database and Appwrite\Utopia\Response. --- src/Appwrite/Platform/Workers/Notifications.php | 8 ++++---- src/Appwrite/{ => Utopia}/Messaging/Adapter/Console.php | 4 ++-- src/Appwrite/{ => Utopia}/Messaging/Adapter/Webhook.php | 4 ++-- src/Appwrite/{ => Utopia}/Messaging/Messages/Console.php | 2 +- src/Appwrite/{ => Utopia}/Messaging/Messages/Webhook.php | 2 +- tests/unit/{ => Utopia}/Messaging/Adapter/ConsoleTest.php | 8 ++++---- tests/unit/{ => Utopia}/Messaging/Adapter/WebhookTest.php | 8 ++++---- 7 files changed, 18 insertions(+), 18 deletions(-) rename src/Appwrite/{ => Utopia}/Messaging/Adapter/Console.php (96%) rename src/Appwrite/{ => Utopia}/Messaging/Adapter/Webhook.php (96%) rename src/Appwrite/{ => Utopia}/Messaging/Messages/Console.php (96%) rename src/Appwrite/{ => Utopia}/Messaging/Messages/Webhook.php (96%) rename tests/unit/{ => Utopia}/Messaging/Adapter/ConsoleTest.php (94%) rename tests/unit/{ => Utopia}/Messaging/Adapter/WebhookTest.php (95%) diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index c50e61a323..5e86a8b021 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -2,11 +2,11 @@ namespace Appwrite\Platform\Workers; -use Appwrite\Messaging\Adapter\Console as ConsoleAdapter; -use Appwrite\Messaging\Adapter\Webhook as WebhookAdapter; -use Appwrite\Messaging\Messages\Console as ConsoleMessage; -use Appwrite\Messaging\Messages\Webhook as WebhookMessage; use Appwrite\Template\Template; +use Appwrite\Utopia\Messaging\Adapter\Console as ConsoleAdapter; +use Appwrite\Utopia\Messaging\Adapter\Webhook as WebhookAdapter; +use Appwrite\Utopia\Messaging\Messages\Console as ConsoleMessage; +use Appwrite\Utopia\Messaging\Messages\Webhook as WebhookMessage; use Exception; use Swoole\Runtime; use Throwable; diff --git a/src/Appwrite/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php similarity index 96% rename from src/Appwrite/Messaging/Adapter/Console.php rename to src/Appwrite/Utopia/Messaging/Adapter/Console.php index aa88f59472..121206abca 100644 --- a/src/Appwrite/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -1,8 +1,8 @@ expectExceptionMessage('Invalid message type.'); // ConsoleMessage extends nothing — pass an unrelated Message implementation - $adapter->send(new \Appwrite\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); + $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); } } diff --git a/tests/unit/Messaging/Adapter/WebhookTest.php b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php similarity index 95% rename from tests/unit/Messaging/Adapter/WebhookTest.php rename to tests/unit/Utopia/Messaging/Adapter/WebhookTest.php index 5ff7db987f..5ad1a8c348 100644 --- a/tests/unit/Messaging/Adapter/WebhookTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php @@ -1,9 +1,9 @@ expectException(\Exception::class); $this->expectExceptionMessage('Invalid message type.'); - $adapter->send(new \Appwrite\Messaging\Messages\Console(recipients: [['userId' => 'u']], title: 't', body: 'b')); + $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Console(recipients: [['userId' => 'u']], title: 't', body: 'b')); } } From 8ad73aa6bb2280fa64785c71001c451e085aa251 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:57:52 +1200 Subject: [PATCH 10/80] refactor(notifications): use Utopia Fetch client in Webhook adapter Replace raw curl with Utopia\Fetch\Client to align with the rest of the codebase (Github OAuth adapter, Install task). The dispatch seam is preserved so existing tests can still substitute responses. --- .../Utopia/Messaging/Adapter/Webhook.php | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php b/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php index e9bf488ad8..76e7234261 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php @@ -3,6 +3,8 @@ namespace Appwrite\Utopia\Messaging\Adapter; use Appwrite\Utopia\Messaging\Messages\Webhook as WebhookMessage; +use Utopia\Fetch\Client as FetchClient; +use Utopia\Fetch\Exception as FetchException; use Utopia\Messaging\Adapter; use Utopia\Messaging\Message; use Utopia\Messaging\Response; @@ -87,27 +89,36 @@ class Webhook extends Adapter */ protected function dispatch(string $method, string $url, array $headers, string $body, int $timeout): array { - $handle = \curl_init(); - \curl_setopt_array($handle, [ - CURLOPT_CUSTOMREQUEST => $method, - CURLOPT_URL => $url, - CURLOPT_HTTPHEADER => $headers, - CURLOPT_POSTFIELDS => $body, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => $timeout, - CURLOPT_CONNECTTIMEOUT => \min(10, $timeout), - CURLOPT_USERAGENT => 'Appwrite Webhook', - ]); + $client = new FetchClient(); + $client + ->setTimeout($timeout * 1000) + ->setConnectTimeout(\min(10, $timeout) * 1000) + ->setAllowRedirects(false) + ->setUserAgent('Appwrite Webhook'); - $output = \curl_exec($handle); - $statusCode = (int) \curl_getinfo($handle, CURLINFO_RESPONSE_CODE); - $error = \curl_error($handle); - \curl_close($handle); + foreach ($headers as $header) { + $parts = \explode(':', $header, 2); + if (\count($parts) === 2) { + $client->addHeader(\trim($parts[0]), \trim($parts[1])); + } + } + + try { + $response = $client->fetch($url, $method, $body); + } catch (FetchException $exception) { + return [ + 'statusCode' => 0, + 'response' => null, + 'error' => $exception->getMessage(), + ]; + } + + $output = $response->getBody(); return [ - 'statusCode' => $statusCode, + 'statusCode' => $response->getStatusCode(), 'response' => \is_string($output) ? $output : null, - 'error' => $error !== '' ? $error : null, + 'error' => null, ]; } } From 5320c9441b5ca417fe5334aad78b7e549e44bed7 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 13:58:13 +1200 Subject: [PATCH 11/80] refactor(notifications): rename dedupKey to deduplicationKey No abbreviations in identifier names. --- src/Appwrite/Event/Notification.php | 14 +++++++------- src/Appwrite/Platform/Workers/Notifications.php | 10 +++++----- tests/unit/Platform/Workers/NotificationsTest.php | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index fa490f3405..f4956ffa9b 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -31,7 +31,7 @@ class Notification extends Event protected string $template = ''; protected array $templateParams = []; - protected string $dedupKey = ''; + protected string $deduplicationKey = ''; /** * @var array @@ -357,15 +357,15 @@ class Notification extends Event return $this->templateParams; } - public function setDedupKey(string $dedupKey): self + public function setDeduplicationKey(string $deduplicationKey): self { - $this->dedupKey = $dedupKey; + $this->deduplicationKey = $deduplicationKey; return $this; } - public function getDedupKey(): string + public function getDeduplicationKey(): string { - return $this->dedupKey; + return $this->deduplicationKey; } /** @@ -400,7 +400,7 @@ class Notification extends Event $this->channels = []; $this->template = ''; $this->templateParams = []; - $this->dedupKey = ''; + $this->deduplicationKey = ''; $this->permissions = []; return $this; } @@ -427,7 +427,7 @@ class Notification extends Event 'channels' => $this->channels, 'template' => $this->template, 'templateParams' => $this->templateParams, - 'dedupKey' => $this->dedupKey, + 'deduplicationKey' => $this->deduplicationKey, 'permissions' => $this->permissions, 'name' => $this->name, 'subject' => $this->subject, diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 5e86a8b021..15cde66a3a 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -64,8 +64,8 @@ class Notifications extends Action throw new Exception('Missing payload'); } - $dedupKey = $payload['dedupKey'] ?? ''; - $messageId = $dedupKey !== '' ? \md5($dedupKey) : ''; + $deduplicationKey = $payload['deduplicationKey'] ?? ''; + $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : ''; if ($messageId !== '' && $this->alreadyDelivered($dbForProject, $messageId)) { $log->addTag('dedup', 'hit'); @@ -299,8 +299,8 @@ class Notifications extends Action $recipients = [['userId' => $address]]; - $dedupKey = $payload['dedupKey'] ?? ''; - $messageId = $dedupKey !== '' ? \md5($dedupKey) : null; + $deduplicationKey = $payload['deduplicationKey'] ?? ''; + $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : null; $consoleMessage = new ConsoleMessage( recipients: $recipients, @@ -323,7 +323,7 @@ class Notifications extends Action 'template' => $payload['template'] ?? '', 'params' => $payload['templateParams'] ?? [], 'project' => \is_array($payload['project'] ?? null) ? ($payload['project']['$id'] ?? null) : null, - 'dedupKey' => $payload['dedupKey'] ?? '', + 'deduplicationKey' => $payload['deduplicationKey'] ?? '', 'events' => $payload['events'] ?? [], ]; diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index c7889851a8..8876dd1dea 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -115,7 +115,7 @@ class NotificationsTest extends TestCase ], 'subject' => 'Hi', 'body' => 'Body', - 'dedupKey' => 'event-1', + 'deduplicationKey' => 'event-1', ]; $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); @@ -136,7 +136,7 @@ class NotificationsTest extends TestCase ], 'subject' => 'Heads up', 'body' => 'Read me', - 'dedupKey' => 'evt-multi', + 'deduplicationKey' => 'evt-multi', 'permissions' => [Permission::read(Role::any())], ]; @@ -164,7 +164,7 @@ class NotificationsTest extends TestCase 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE]], 'subject' => 'Sub', 'body' => 'B', - 'dedupKey' => 'dup-key', + 'deduplicationKey' => 'dup-key', ]; // First run delivers and persists. @@ -229,7 +229,7 @@ class NotificationsTest extends TestCase 'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK]], 'subject' => 's', 'body' => 'b', - 'dedupKey' => 'err-1', + 'deduplicationKey' => 'err-1', ]; try { From 7839b3bb3fd933c7aec9a7fd6e60b30ccb03a31c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 14:01:29 +1200 Subject: [PATCH 12/80] refactor(notifications): align webhook signing with per-recipient signatureKey Drop the global _APP_NOTIFICATIONS_WEBHOOK_SECRET env var. There's no analogous global webhook secret in Appwrite; the existing Webhooks worker carries a per-webhook signatureKey on the webhook document. Move the same pattern into the Notification event: each webhook recipient may carry an optional signatureKey, which the worker forwards to the Webhook adapter for HMAC-SHA256 signing. Recipients without a key are delivered unsigned and a tag is logged for audit. --- .env | 1 - app/config/variables.php | 15 -------- docker-compose.yml | 1 - src/Appwrite/Event/Notification.php | 23 +++++++++--- .../Platform/Workers/Notifications.php | 22 ++++++++---- tests/resources/docker/docker-compose.yml | 1 - .../Platform/Workers/NotificationsTest.php | 35 +++++++++++++++++-- 7 files changed, 65 insertions(+), 33 deletions(-) diff --git a/.env b/.env index 47c0e2a607..4a6a3ac344 100644 --- a/.env +++ b/.env @@ -88,7 +88,6 @@ _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= _APP_SMTP_USERNAME=user _APP_SMTP_PASSWORD=password -_APP_NOTIFICATIONS_WEBHOOK_SECRET= _APP_SMS_PROVIDER=sms://username:password@mock _APP_SMS_FROM=+123456789 _APP_SMS_PROJECTS_DENY_LIST= diff --git a/app/config/variables.php b/app/config/variables.php index ee539928f6..c834656ff4 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -579,21 +579,6 @@ return [ ], ], ], - [ - 'category' => 'Notifications', - 'description' => 'Settings for the notifications worker. Notifications fan out to email, in-app console alerts, and outbound webhooks via the dedicated worker.', - 'variables' => [ - [ - 'name' => '_APP_NOTIFICATIONS_WEBHOOK_SECRET', - 'description' => 'Shared secret used to sign outbound notification webhooks with HMAC-SHA256. When set, every webhook delivery includes an X-Appwrite-Webhook-Signature header carrying `sha256=` over `.`. Leave empty to deliver unsigned payloads. Empty by default.', - 'introduction' => '', - 'default' => '', - 'required' => false, - 'question' => '', - 'filter' => '' - ], - ], - ], [ 'category' => 'Phone', 'description' => '', diff --git a/docker-compose.yml b/docker-compose.yml index dfe6769b72..da5efac438 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -801,7 +801,6 @@ services: - _APP_SMTP_SECURE - _APP_SMTP_USERNAME - _APP_SMTP_PASSWORD - - _APP_NOTIFICATIONS_WEBHOOK_SECRET - _APP_LOGGING_CONFIG - _APP_DOMAIN - _APP_OPTIONS_FORCE_HTTPS diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index f4956ffa9b..a1844ccbab 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -20,7 +20,16 @@ class Notification extends Event protected array $customMailOptions = []; /** - * @var array + * Recipients to deliver the notification to. + * + * Each entry has an `address` (channel-specific identifier — email, + * userId, or webhook URL) and a `channel`. Webhook recipients may + * additionally carry an optional `signatureKey`; when set, the + * webhook adapter signs the request body with HMAC-SHA256 and adds + * the `X-Appwrite-Webhook-Signature` header. Without a key the + * payload is sent unsigned. + * + * @var array */ protected array $recipients = []; @@ -296,7 +305,7 @@ class Notification extends Event } /** - * @param array $recipients + * @param array $recipients */ public function setRecipients(array $recipients): self { @@ -305,16 +314,20 @@ class Notification extends Event } /** - * @return array + * @return array */ public function getRecipients(): array { return $this->recipients; } - public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL): self + public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL, ?string $signatureKey = null): self { - $this->recipients[] = ['address' => $address, 'channel' => $channel]; + $recipient = ['address' => $address, 'channel' => $channel]; + if ($signatureKey !== null && $signatureKey !== '') { + $recipient['signatureKey'] = $signatureKey; + } + $this->recipients[] = $recipient; return $this; } diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 15cde66a3a..7c085a56fc 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -80,7 +80,7 @@ class Notifications extends Action foreach ($recipients as $recipient) { $channel = $recipient['channel']; try { - $this->dispatch($channel, $recipient['address'], $payload, $register, $dbForProject, $log); + $this->dispatch($recipient, $payload, $register, $dbForProject, $log); if ($messageId !== '') { $this->persistAlert($dbForProject, $messageId, $channel, $recipient['address'], $payload); } @@ -93,7 +93,7 @@ class Notifications extends Action } /** - * @return array + * @return array */ private function resolveRecipients(array $payload): array { @@ -120,8 +120,14 @@ class Notifications extends Action } } - protected function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void + /** + * @param array{address: string, channel: string, signatureKey?: string} $recipient + */ + protected function dispatch(array $recipient, array $payload, Registry $register, Database $database, Log $log): void { + $channel = $recipient['channel']; + $address = $recipient['address']; + switch ($channel) { case NOTIFICATION_CHANNEL_EMAIL: $this->dispatchEmail($address, $payload, $register, $log); @@ -130,7 +136,7 @@ class Notifications extends Action $this->dispatchConsole($address, $payload, $database); return; case NOTIFICATION_CHANNEL_WEBHOOK: - $this->dispatchWebhook($address, $payload); + $this->dispatchWebhook($address, $payload, $recipient['signatureKey'] ?? null, $log); return; default: throw new Exception('Unsupported notification channel: ' . $channel); @@ -315,7 +321,7 @@ class Notifications extends Action $adapter->send($consoleMessage); } - protected function dispatchWebhook(string $address, array $payload): void + protected function dispatchWebhook(string $address, array $payload, ?string $signatureKey, Log $log): void { $body = [ 'subject' => $payload['subject'] ?? '', @@ -327,12 +333,14 @@ class Notifications extends Action 'events' => $payload['events'] ?? [], ]; - $secret = System::getEnv('_APP_NOTIFICATIONS_WEBHOOK_SECRET', '') ?: null; + if ($signatureKey === null || $signatureKey === '') { + $log->addTag('webhook_signed', 'false'); + } $message = new WebhookMessage( urls: [$address], payload: $body, - signingSecret: $secret, + signingSecret: $signatureKey, ); $adapter = new WebhookAdapter(); diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 2d4b0f82a5..47a69f077b 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -291,7 +291,6 @@ services: - _APP_DB_ADAPTER - _APP_SMTP_HOST - _APP_SMTP_PORT - - _APP_NOTIFICATIONS_WEBHOOK_SECRET appwrite-worker-builds: entrypoint: worker-builds diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 8876dd1dea..f0eeb7cfb7 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -25,17 +25,19 @@ require_once __DIR__ . '/../../../../app/init.php'; */ class SpyNotifications extends Notifications { - /** @var array}> */ + /** @var array}> */ public array $dispatched = []; /** @var array */ public array $throwOn = []; - protected function dispatch(string $channel, string $address, array $payload, Registry $register, Database $database, Log $log): void + protected function dispatch(array $recipient, array $payload, Registry $register, Database $database, Log $log): void { + $channel = $recipient['channel']; $this->dispatched[] = [ 'channel' => $channel, - 'address' => $address, + 'address' => $recipient['address'], + 'signatureKey' => $recipient['signatureKey'] ?? null, 'payload' => $payload, ]; @@ -219,6 +221,33 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_CHANNEL_EMAIL, $worker->dispatched[0]['channel']); } + public function testWebhookRecipientForwardsSignatureKey(): void + { + $worker = new SpyNotifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'https://hooks.example.test/signed', + 'channel' => NOTIFICATION_CHANNEL_WEBHOOK, + 'signatureKey' => 'tenant-secret', + ], + [ + 'address' => 'https://hooks.example.test/unsigned', + 'channel' => NOTIFICATION_CHANNEL_WEBHOOK, + ], + ], + 'subject' => 's', + 'body' => 'b', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(2, $worker->dispatched); + $this->assertSame('tenant-secret', $worker->dispatched[0]['signatureKey']); + $this->assertNull($worker->dispatched[1]['signatureKey']); + } + public function testDispatchErrorTagsLogAndPropagates(): void { $worker = new SpyNotifications(); From d6be6e165be308f9dfec41f1b35a1658e4e829c0 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 14:02:37 +1200 Subject: [PATCH 13/80] test(notifications): split e2e into base + server/console overlays Mirrors the dominant e2e pattern (FunctionsBase/MigrationsBase): the shared health-queue assertions live in a NotificationsBase trait, with thin per-side overlays for ProjectCustom + SideServer and ProjectCustom + SideConsole. --- .../Notifications/NotificationsBase.php | 58 +++++++++++++++++++ .../NotificationsCustomConsoleTest.php | 14 +++++ .../NotificationsCustomServerTest.php | 55 +----------------- 3 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 tests/e2e/Services/Notifications/NotificationsBase.php create mode 100644 tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php new file mode 100644 index 0000000000..adefbaa04b --- /dev/null +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -0,0 +1,58 @@ +client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['size']); + $this->assertGreaterThanOrEqual(0, $response['body']['size']); + } + + public function testHealthQueueNotificationsThresholdGuard(): void + { + $response = $this->client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), ['threshold' => '0']); + + $this->assertContains($response['headers']['status-code'], [200, 503]); + } + + public function testHealthQueueFailedAcceptsNotifications(): void + { + $response = $this->client->call(Client::METHOD_GET, '/health/queue/failed/v1-notifications', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsInt($response['body']['size']); + } +} diff --git a/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php b/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php new file mode 100644 index 0000000000..740c100c25 --- /dev/null +++ b/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php @@ -0,0 +1,14 @@ +client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - $this->assertGreaterThanOrEqual(0, $response['body']['size']); - } - - public function testHealthQueueNotificationsThresholdGuard(): void - { - $response = $this->client->call(Client::METHOD_GET, '/health/queue/notifications', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), ['threshold' => '0']); - - // threshold=0 means any non-zero queue length is unhealthy. The size - // is racy under load, so accept either response — the contract is - // that the parameter is honoured, not that the queue is hot. - $this->assertContains($response['headers']['status-code'], [200, 503]); - } - - public function testHealthQueueFailedAcceptsNotifications(): void - { - $response = $this->client->call(Client::METHOD_GET, '/health/queue/failed/v1-notifications', \array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertIsInt($response['body']['size']); - } } From 0ce3978f2c4b50f46d7ada12c511be1ed030f2be Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 15:56:43 +1200 Subject: [PATCH 14/80] feat(notifications): add worker entrypoint and registration alongside Mails Adds the bin/worker-notifications entrypoint, Dockerfile chmod, and docker-compose service definitions for the new Notifications worker without disturbing the existing Mails worker, and re-adds the Mails queue/class constants on Event so the legacy callers still resolve. The full mail->notification swap (caller migration, Mails removal, worker rename) will land as a follow-up. Co-Authored-By: Claude Opus 4.7 (1M context) --- Dockerfile | 1 + bin/worker-notifications | 3 ++ docker-compose.yml | 41 ++++++++++++++++++++++ src/Appwrite/Event/Event.php | 3 ++ src/Appwrite/Platform/Services/Workers.php | 3 ++ tests/resources/docker/docker-compose.yml | 24 +++++++++++++ 6 files changed, 75 insertions(+) create mode 100755 bin/worker-notifications diff --git a/Dockerfile b/Dockerfile index 1922a0d2b9..8e13b83fc0 100755 --- a/Dockerfile +++ b/Dockerfile @@ -93,6 +93,7 @@ RUN chmod +x /usr/local/bin/doctor && \ chmod +x /usr/local/bin/worker-functions && \ chmod +x /usr/local/bin/worker-mails && \ chmod +x /usr/local/bin/worker-messaging && \ + chmod +x /usr/local/bin/worker-notifications && \ chmod +x /usr/local/bin/worker-migrations && \ chmod +x /usr/local/bin/worker-webhooks && \ chmod +x /usr/local/bin/worker-stats-usage && \ diff --git a/bin/worker-notifications b/bin/worker-notifications new file mode 100755 index 0000000000..1b7bf28745 --- /dev/null +++ b/bin/worker-notifications @@ -0,0 +1,3 @@ +#!/bin/sh + +exec php /usr/src/code/app/worker.php notifications "$@" diff --git a/docker-compose.yml b/docker-compose.yml index da5efac438..189177829c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -806,6 +806,47 @@ services: - _APP_OPTIONS_FORCE_HTTPS - _APP_DATABASE_SHARED_TABLES + appwrite-worker-notifications: + entrypoint: worker-notifications + <<: [*x-logging, *x-build] + container_name: appwrite-worker-notifications + image: appwrite-dev + networks: + - appwrite + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + depends_on: + - redis + - maildev + - ${_APP_DB_HOST:-mariadb} + environment: + - _APP_ENV + - _APP_WORKER_PER_CORE + - _APP_POOL_ADAPTER + - _APP_OPENSSL_KEY_V1 + - _APP_SYSTEM_EMAIL_NAME + - _APP_SYSTEM_EMAIL_ADDRESS + - _APP_REDIS_HOST + - _APP_REDIS_PORT + - _APP_REDIS_USER + - _APP_REDIS_PASS + - _APP_DB_HOST + - _APP_DB_PORT + - _APP_DB_SCHEMA + - _APP_DB_USER + - _APP_DB_PASS + - _APP_DB_ADAPTER + - _APP_SMTP_HOST + - _APP_SMTP_PORT + - _APP_SMTP_SECURE + - _APP_SMTP_USERNAME + - _APP_SMTP_PASSWORD + - _APP_LOGGING_CONFIG + - _APP_DOMAIN + - _APP_OPTIONS_FORCE_HTTPS + - _APP_DATABASE_SHARED_TABLES + appwrite-worker-messaging: entrypoint: worker-messaging <<: [*x-logging, *x-build] diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 06af738857..54841343e7 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -18,6 +18,9 @@ class Event public const AUDITS_QUEUE_NAME = 'v1-audits'; public const AUDITS_CLASS_NAME = 'AuditsV1'; + public const MAILS_QUEUE_NAME = 'v1-mails'; + public const MAILS_CLASS_NAME = 'MailsV1'; + public const NOTIFICATIONS_QUEUE_NAME = 'v1-notifications'; public const NOTIFICATIONS_CLASS_NAME = 'NotificationsV1'; diff --git a/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index 8b4925c5a5..5af3d078de 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -7,8 +7,10 @@ use Appwrite\Platform\Workers\Certificates; use Appwrite\Platform\Workers\Deletes; use Appwrite\Platform\Workers\Executions; use Appwrite\Platform\Workers\Functions; +use Appwrite\Platform\Workers\Mails; use Appwrite\Platform\Workers\Messaging; use Appwrite\Platform\Workers\Migrations; +use Appwrite\Platform\Workers\Notifications; use Appwrite\Platform\Workers\StatsResources; use Appwrite\Platform\Workers\StatsUsage; use Appwrite\Platform\Workers\Webhooks; @@ -25,6 +27,7 @@ class Workers extends Service ->addAction(Deletes::getName(), new Deletes()) ->addAction(Executions::getName(), new Executions()) ->addAction(Functions::getName(), new Functions()) + ->addAction(Mails::getName(), new Mails()) ->addAction(Messaging::getName(), new Messaging()) ->addAction(Notifications::getName(), new Notifications()) ->addAction(Webhooks::getName(), new Webhooks()) diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 47a69f077b..5942eea65f 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -292,6 +292,30 @@ services: - _APP_SMTP_HOST - _APP_SMTP_PORT + appwrite-worker-notifications: + entrypoint: worker-notifications + container_name: appwrite-worker-notifications + build: + context: . + restart: unless-stopped + networks: + - appwrite + depends_on: + - redis + - maildev + environment: + - _APP_ENV + - _APP_REDIS_HOST + - _APP_REDIS_PORT + - _APP_DB_HOST + - _APP_DB_PORT + - _APP_DB_SCHEMA + - _APP_DB_USER + - _APP_DB_PASS + - _APP_DB_ADAPTER + - _APP_SMTP_HOST + - _APP_SMTP_PORT + appwrite-worker-builds: entrypoint: worker-builds container_name: appwrite-worker-builds From 36e1615498193aa8fcfaa98b405315330f293977 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:41:11 +1200 Subject: [PATCH 15/80] fix(notifications): suffix Console adapter $id with recipient hash to avoid multi-recipient collision Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Utopia/Messaging/Adapter/Console.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index 121206abca..28765e3242 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -61,11 +61,17 @@ class Console extends Adapter $teamId = $recipient['teamId'] ?? ''; $key = $userId !== '' ? $userId : $teamId; + $messageId = $message->getMessageId(); + $recipientKey = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; + $documentId = $messageId !== null + ? $messageId . '_' . \substr(\md5($recipientKey), 0, 8) + : ID::unique(); + try { $document = new Document([ - '$id' => $message->getMessageId() ?? ID::unique(), + '$id' => $documentId, '$permissions' => $this->buildPermissions($userId, $teamId), - 'messageId' => $message->getMessageId(), + 'messageId' => $messageId, 'type' => $message->getType(), 'channel' => self::TYPE, 'userId' => $userId !== '' ? $userId : null, From 3df219df662c7ecc5de1d0ebdbfe91e7834c4eba Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:41:59 +1200 Subject: [PATCH 16/80] refactor(notifications): consolidate channel constants under NOTIFICATION_TYPE_* prefix Co-Authored-By: Claude Opus 4.7 (1M context) --- app/init/constants.php | 15 +++++------ src/Appwrite/Event/Notification.php | 4 +-- .../Platform/Workers/Notifications.php | 10 +++---- .../Platform/Workers/NotificationsTest.php | 26 +++++++++---------- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index d3bbdaef49..7a1dfaf2cf 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -254,15 +254,12 @@ const FUNCTION_ALLOWLIST_HEADERS_RESPONSE = ['content-type', 'content-length']; const MESSAGE_TYPE_EMAIL = 'email'; const MESSAGE_TYPE_SMS = 'sms'; const MESSAGE_TYPE_PUSH = 'push'; -// Notification channels -const NOTIFICATION_CHANNEL_EMAIL = 'email'; -const NOTIFICATION_CHANNEL_SMS = 'sms'; -const NOTIFICATION_CHANNEL_PUSH = 'push'; -const NOTIFICATION_CHANNEL_CONSOLE = 'console'; -const NOTIFICATION_CHANNEL_WEBHOOK = 'webhook'; -// Notification provider types (custom — extend MESSAGE_TYPE_*) -const MESSAGE_TYPE_CONSOLE = 'console'; -const MESSAGE_TYPE_WEBHOOK = 'webhook'; +// Notification types +const NOTIFICATION_TYPE_EMAIL = 'email'; +const NOTIFICATION_TYPE_SMS = 'sms'; +const NOTIFICATION_TYPE_PUSH = 'push'; +const NOTIFICATION_TYPE_CONSOLE = 'console'; +const NOTIFICATION_TYPE_WEBHOOK = 'webhook'; const RESOURCE_TYPE_ALERTS = 'alerts'; // API key types const API_KEY_STANDARD = 'standard'; diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index a1844ccbab..e1e1965e54 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -321,7 +321,7 @@ class Notification extends Event return $this->recipients; } - public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL, ?string $signatureKey = null): self + public function addRecipient(string $address, string $channel = NOTIFICATION_TYPE_EMAIL, ?string $signatureKey = null): self { $recipient = ['address' => $address, 'channel' => $channel]; if ($signatureKey !== null && $signatureKey !== '') { @@ -429,7 +429,7 @@ class Notification extends Event if (empty($recipients) && !empty($this->recipient)) { $recipients = [[ 'address' => $this->recipient, - 'channel' => NOTIFICATION_CHANNEL_EMAIL, + 'channel' => NOTIFICATION_TYPE_EMAIL, ]]; } diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 7c085a56fc..e5b811d424 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -107,7 +107,7 @@ class Notifications extends Action return []; } - return [['address' => $address, 'channel' => NOTIFICATION_CHANNEL_EMAIL]]; + return [['address' => $address, 'channel' => NOTIFICATION_TYPE_EMAIL]]; } private function alreadyDelivered(Database $database, string $messageId): bool @@ -129,13 +129,13 @@ class Notifications extends Action $address = $recipient['address']; switch ($channel) { - case NOTIFICATION_CHANNEL_EMAIL: + case NOTIFICATION_TYPE_EMAIL: $this->dispatchEmail($address, $payload, $register, $log); return; - case NOTIFICATION_CHANNEL_CONSOLE: + case NOTIFICATION_TYPE_CONSOLE: $this->dispatchConsole($address, $payload, $database); return; - case NOTIFICATION_CHANNEL_WEBHOOK: + case NOTIFICATION_TYPE_WEBHOOK: $this->dispatchWebhook($address, $payload, $recipient['signatureKey'] ?? null, $log); return; default: @@ -364,7 +364,7 @@ class Notifications extends Action 'messageId' => $messageId, 'type' => 'info', 'channel' => $channel, - 'userId' => $channel === NOTIFICATION_CHANNEL_CONSOLE ? $address : null, + 'userId' => $channel === NOTIFICATION_TYPE_CONSOLE ? $address : null, 'projectId' => $projectId, 'title' => $payload['subject'] ?? '', 'body' => $payload['body'] ?? '', diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index f0eeb7cfb7..2e0c9d82f4 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -111,9 +111,9 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user@example.test', 'channel' => NOTIFICATION_CHANNEL_EMAIL], - ['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], - ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK], + ['address' => 'user@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL], + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE], + ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK], ], 'subject' => 'Hi', 'body' => 'Body', @@ -124,7 +124,7 @@ class NotificationsTest extends TestCase $this->assertCount(3, $worker->dispatched); $channels = \array_map(static fn ($d) => $d['channel'], $worker->dispatched); - $this->assertSame([NOTIFICATION_CHANNEL_EMAIL, NOTIFICATION_CHANNEL_CONSOLE, NOTIFICATION_CHANNEL_WEBHOOK], $channels); + $this->assertSame([NOTIFICATION_TYPE_EMAIL, NOTIFICATION_TYPE_CONSOLE, NOTIFICATION_TYPE_WEBHOOK], $channels); } public function testPersistsOneAlertPerRecipientChannel(): void @@ -133,8 +133,8 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], - ['address' => 'user-2', 'channel' => NOTIFICATION_CHANNEL_CONSOLE], + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE], + ['address' => 'user-2', 'channel' => NOTIFICATION_TYPE_CONSOLE], ], 'subject' => 'Heads up', 'body' => 'Read me', @@ -163,7 +163,7 @@ class NotificationsTest extends TestCase $worker = new SpyNotifications(); $payload = [ 'project' => ['$id' => 'project-x'], - 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_CHANNEL_CONSOLE]], + 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE]], 'subject' => 'Sub', 'body' => 'B', 'deduplicationKey' => 'dup-key', @@ -218,7 +218,7 @@ class NotificationsTest extends TestCase $this->assertCount(1, $worker->dispatched); $this->assertSame('legacy@example.test', $worker->dispatched[0]['address']); - $this->assertSame(NOTIFICATION_CHANNEL_EMAIL, $worker->dispatched[0]['channel']); + $this->assertSame(NOTIFICATION_TYPE_EMAIL, $worker->dispatched[0]['channel']); } public function testWebhookRecipientForwardsSignatureKey(): void @@ -229,12 +229,12 @@ class NotificationsTest extends TestCase 'recipients' => [ [ 'address' => 'https://hooks.example.test/signed', - 'channel' => NOTIFICATION_CHANNEL_WEBHOOK, + 'channel' => NOTIFICATION_TYPE_WEBHOOK, 'signatureKey' => 'tenant-secret', ], [ 'address' => 'https://hooks.example.test/unsigned', - 'channel' => NOTIFICATION_CHANNEL_WEBHOOK, + 'channel' => NOTIFICATION_TYPE_WEBHOOK, ], ], 'subject' => 's', @@ -251,11 +251,11 @@ class NotificationsTest extends TestCase public function testDispatchErrorTagsLogAndPropagates(): void { $worker = new SpyNotifications(); - $worker->throwOn[NOTIFICATION_CHANNEL_WEBHOOK] = new \RuntimeException('boom'); + $worker->throwOn[NOTIFICATION_TYPE_WEBHOOK] = new \RuntimeException('boom'); $payload = [ 'project' => ['$id' => 'project-x'], - 'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_CHANNEL_WEBHOOK]], + 'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_TYPE_WEBHOOK]], 'subject' => 's', 'body' => 'b', 'deduplicationKey' => 'err-1', @@ -269,7 +269,7 @@ class NotificationsTest extends TestCase } $tags = $this->log->getTags(); - $this->assertSame(NOTIFICATION_CHANNEL_WEBHOOK, $tags['channel'] ?? null); + $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $tags['channel'] ?? null); $this->assertSame('boom', $tags['error'] ?? null); $rows = $this->database->find('alerts'); From a5587ff096817617e881727ca8a26516d7bb2aa1 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:42:10 +1200 Subject: [PATCH 17/80] feat(notifications): track read state and enable per-recipient alert idempotency Co-Authored-By: Claude Opus 4.7 (1M context) --- app/config/collections/platform.php | 38 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index b23d170a58..eed45322a4 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1104,28 +1104,46 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('read'), + 'type' => Database::VAR_BOOLEAN, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => false, + 'default' => false, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ '$id' => ID::custom('_key_messageId'), - 'type' => Database::INDEX_UNIQUE, + 'type' => Database::INDEX_KEY, 'attributes' => ['messageId'], 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['userId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], + '$id' => ID::custom('_key_recipient'), + 'type' => Database::INDEX_UNIQUE, + 'attributes' => ['messageId', 'channel', 'userId', 'teamId'], + 'lengths' => [Database::LENGTH_KEY, 64, Database::LENGTH_KEY, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_teamId'), + '$id' => ID::custom('_key_userId_read'), 'type' => Database::INDEX_KEY, - 'attributes' => ['teamId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], + 'attributes' => ['userId', 'read'], + 'lengths' => [Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_teamId_read'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['teamId', 'read'], + 'lengths' => [Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_projectId'), From 4351dffb88bf165c2ccf61e7e14cb859d072d84e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:42:22 +1200 Subject: [PATCH 18/80] refactor(notifications): drop SMTP plumbing from event and extend recipient struct with userId/teamId Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Event/Notification.php | 174 +++------------------------- 1 file changed, 19 insertions(+), 155 deletions(-) diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index a1844ccbab..acb4c8b62d 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -13,11 +13,9 @@ class Notification extends Event protected string $subject = ''; protected string $body = ''; protected string $preview = ''; - protected array $smtp = []; protected array $variables = []; protected string $bodyTemplate = ''; protected array $attachment = []; - protected array $customMailOptions = []; /** * Recipients to deliver the notification to. @@ -27,9 +25,10 @@ class Notification extends Event * additionally carry an optional `signatureKey`; when set, the * webhook adapter signs the request body with HMAC-SHA256 and adds * the `X-Appwrite-Webhook-Signature` header. Without a key the - * payload is sent unsigned. + * payload is sent unsigned. Optional `userId` and `teamId` identify + * the owner of the alert (used by C2/C3 budget/limit alerts). * - * @var array + * @var array */ protected array $recipients = []; @@ -122,105 +121,6 @@ class Notification extends Event return $this->bodyTemplate; } - public function setSmtpHost(string $host): self - { - $this->smtp['host'] = $host; - return $this; - } - - public function setSmtpPort(int $port): self - { - $this->smtp['port'] = $port; - return $this; - } - - public function setSmtpUsername(string $username): self - { - $this->smtp['username'] = $username; - return $this; - } - - public function setSmtpPassword(string $password): self - { - $this->smtp['password'] = $password; - return $this; - } - - public function setSmtpSecure(string $secure): self - { - $this->smtp['secure'] = $secure; - return $this; - } - - public function setSmtpSenderEmail(string $senderEmail): self - { - $this->smtp['senderEmail'] = $senderEmail; - return $this; - } - - public function setSmtpSenderName(string $senderName): self - { - $this->smtp['senderName'] = $senderName; - return $this; - } - - public function setSmtpReplyToEmail(string $email): self - { - $this->smtp['replyToEmail'] = $email; - return $this; - } - - public function setSmtpReplyToName(string $name): self - { - $this->smtp['replyToName'] = $name; - return $this; - } - - public function getSmtpHost(): string - { - return $this->smtp['host'] ?? ''; - } - - public function getSmtpPort(): int - { - return $this->smtp['port'] ?? 0; - } - - public function getSmtpUsername(): string - { - return $this->smtp['username'] ?? ''; - } - - public function getSmtpPassword(): string - { - return $this->smtp['password'] ?? ''; - } - - public function getSmtpSecure(): string - { - return $this->smtp['secure'] ?? ''; - } - - public function getSmtpSenderEmail(): string - { - return $this->smtp['senderEmail'] ?? ''; - } - - public function getSmtpSenderName(): string - { - return $this->smtp['senderName'] ?? ''; - } - - public function getSmtpReplyToEmail(): string - { - return $this->smtp['replyToEmail'] ?? ''; - } - - public function getSmtpReplyToName(): string - { - return $this->smtp['replyToName'] ?? ''; - } - public function getVariables(): array { return $this->variables; @@ -260,52 +160,8 @@ class Notification extends Event return $this; } - public function setSenderEmail(string $email): self - { - $this->customMailOptions['senderEmail'] = $email; - return $this; - } - - public function getSenderEmail(): string - { - return $this->customMailOptions['senderEmail'] ?? ''; - } - - public function setSenderName(string $name): self - { - $this->customMailOptions['senderName'] = $name; - return $this; - } - - public function getSenderName(): string - { - return $this->customMailOptions['senderName'] ?? ''; - } - - public function setReplyToEmail(string $email): self - { - $this->customMailOptions['replyToEmail'] = $email; - return $this; - } - - public function getReplyToEmail(): string - { - return $this->customMailOptions['replyToEmail'] ?? ''; - } - - public function setReplyToName(string $name): self - { - $this->customMailOptions['replyToName'] = $name; - return $this; - } - - public function getReplyToName(): string - { - return $this->customMailOptions['replyToName'] ?? ''; - } - /** - * @param array $recipients + * @param array $recipients */ public function setRecipients(array $recipients): self { @@ -314,19 +170,30 @@ class Notification extends Event } /** - * @return array + * @return array */ public function getRecipients(): array { return $this->recipients; } - public function addRecipient(string $address, string $channel = NOTIFICATION_CHANNEL_EMAIL, ?string $signatureKey = null): self - { + public function addRecipient( + string $address, + string $channel = NOTIFICATION_TYPE_EMAIL, + ?string $signatureKey = null, + ?string $userId = null, + ?string $teamId = null, + ): self { $recipient = ['address' => $address, 'channel' => $channel]; if ($signatureKey !== null && $signatureKey !== '') { $recipient['signatureKey'] = $signatureKey; } + if ($userId !== null && $userId !== '') { + $recipient['userId'] = $userId; + } + if ($teamId !== null && $teamId !== '') { + $recipient['teamId'] = $teamId; + } $this->recipients[] = $recipient; return $this; } @@ -408,7 +275,6 @@ class Notification extends Event $this->variables = []; $this->bodyTemplate = ''; $this->attachment = []; - $this->customMailOptions = []; $this->recipients = []; $this->channels = []; $this->template = ''; @@ -429,7 +295,7 @@ class Notification extends Event if (empty($recipients) && !empty($this->recipient)) { $recipients = [[ 'address' => $this->recipient, - 'channel' => NOTIFICATION_CHANNEL_EMAIL, + 'channel' => NOTIFICATION_TYPE_EMAIL, ]]; } @@ -447,10 +313,8 @@ class Notification extends Event 'bodyTemplate' => $this->bodyTemplate, 'body' => $this->body, 'preview' => $this->preview, - 'smtp' => $this->smtp, 'variables' => $this->variables, 'attachment' => $this->attachment, - 'customMailOptions' => $this->customMailOptions, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()), 'platform' => $platform, ]; From 2c2e920a1abdd72efda2ba581e9ac3e80adfd85d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:42:23 +1200 Subject: [PATCH 19/80] feat(notifications): add alerts queries validator Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Database/Validator/Queries/Alerts.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php new file mode 100644 index 0000000000..c0eee2d153 --- /dev/null +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php @@ -0,0 +1,23 @@ + Date: Wed, 6 May 2026 13:44:09 +1200 Subject: [PATCH 20/80] feat(notifications): scaffold GET /v1/account/alerts endpoint Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Account/Http/Alerts/XList.php | 65 +++++++++++++++++++ .../Modules/Account/Services/Http.php | 4 +- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php new file mode 100644 index 0000000000..3f4b60d3b7 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php @@ -0,0 +1,65 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/account/alerts') + ->desc('List alerts') + ->groups(['api', 'account']) + ->label('scope', 'account') + ->label('sdk', new Method( + namespace: 'account', + name: 'listAlerts', + description: '/docs/references/account/list-alerts.md', + auth: [AuthType::SESSION, AuthType::JWT], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_ALERT_LIST, + ) + ] + )) + ->param('queries', [], new Alerts(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Alerts::ALLOWED_ATTRIBUTES), true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('user') + ->callback($this->action(...)); + } + + /** + * @param array $queries + */ + public function action( + array $queries, + Response $response, + Database $dbForPlatform, + Document $user + ): void { + $response->dynamic(new Document([ + 'alerts' => [], + 'total' => 0, + ]), Response::MODEL_ALERT_LIST); + } +} diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index ae2e841636..4e59807a4d 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,6 +12,7 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; +use Appwrite\Platform\Modules\Account\Http\Alerts\XList as ListAlerts; use Utopia\Platform\Service; class Http extends Service @@ -29,6 +30,7 @@ class Http extends Service ->addAction(UpdateRecoveryCodes::getName(), new UpdateRecoveryCodes()) ->addAction(GetRecoveryCodes::getName(), new GetRecoveryCodes()) ->addAction(CreateChallenge::getName(), new CreateChallenge()) - ->addAction(UpdateChallenge::getName(), new UpdateChallenge()); + ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) + ->addAction(ListAlerts::getName(), new ListAlerts()); } } From d79be3a6e354948214a23f87c83f1afbf07cf3ac Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:44:27 +1200 Subject: [PATCH 21/80] feat(notifications): add alert and alertList response models Adds Alert response model and MODEL_ALERT/MODEL_ALERT_LIST constants so future GET /v1/account/alerts endpoint can return MODEL_ALERT_LIST. Co-Authored-By: Claude Opus 4.7 (1M context) --- app/init/models.php | 3 + src/Appwrite/Utopia/Response.php | 4 + src/Appwrite/Utopia/Response/Model/Alert.php | 112 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 src/Appwrite/Utopia/Response/Model/Alert.php diff --git a/app/init/models.php b/app/init/models.php index 9530b4b98b..ac95448cf0 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -2,6 +2,7 @@ use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Account; +use Appwrite\Utopia\Response\Model\Alert; use Appwrite\Utopia\Response\Model\AlgoArgon2; use Appwrite\Utopia\Response\Model\AlgoBcrypt; use Appwrite\Utopia\Response\Model\AlgoMd5; @@ -237,6 +238,7 @@ Response::setModel(new BaseList('Column Indexes List', Response::MODEL_COLUMN_IN Response::setModel(new BaseList('Users List', Response::MODEL_USER_LIST, 'users', Response::MODEL_USER)); Response::setModel(new BaseList('Sessions List', Response::MODEL_SESSION_LIST, 'sessions', Response::MODEL_SESSION)); Response::setModel(new BaseList('Identities List', Response::MODEL_IDENTITY_LIST, 'identities', Response::MODEL_IDENTITY)); +Response::setModel(new BaseList('Alerts List', Response::MODEL_ALERT_LIST, 'alerts', Response::MODEL_ALERT)); Response::setModel(new BaseList('Logs List', Response::MODEL_LOG_LIST, 'logs', Response::MODEL_LOG)); Response::setModel(new BaseList('Files List', Response::MODEL_FILE_LIST, 'files', Response::MODEL_FILE)); Response::setModel(new BaseList('Buckets List', Response::MODEL_BUCKET_LIST, 'buckets', Response::MODEL_BUCKET)); @@ -362,6 +364,7 @@ Response::setModel(new Account()); Response::setModel(new Preferences()); Response::setModel(new Session()); Response::setModel(new Identity()); +Response::setModel(new Alert()); Response::setModel(new Token()); Response::setModel(new JWT()); Response::setModel(new Locale()); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 899cdc086a..2c8dc3c69b 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -67,6 +67,10 @@ class Response extends SwooleResponse public const MODEL_ROW = 'row'; public const MODEL_ROW_LIST = 'rowList'; + // Alerts + public const MODEL_ALERT = 'alert'; + public const MODEL_ALERT_LIST = 'alertList'; + // Database Attributes public const MODEL_ATTRIBUTE = 'attribute'; public const MODEL_ATTRIBUTE_LIST = 'attributeList'; diff --git a/src/Appwrite/Utopia/Response/Model/Alert.php b/src/Appwrite/Utopia/Response/Model/Alert.php new file mode 100644 index 0000000000..9a9e89e9dd --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Alert.php @@ -0,0 +1,112 @@ +addRule('$id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Alert ID.', + 'default' => '', + 'example' => '5e5ea5c16897e', + ]) + ->addRule('$createdAt', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'Alert creation date in ISO 8601 format.', + 'default' => '', + 'example' => self::TYPE_DATETIME_EXAMPLE, + ]) + ->addRule('$updatedAt', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'Alert update date in ISO 8601 format.', + 'default' => '', + 'example' => self::TYPE_DATETIME_EXAMPLE, + ]) + ->addRule('messageId', [ + 'type' => self::TYPE_STRING, + 'description' => 'Stable message ID used for dedup.', + 'default' => '', + 'example' => 'session.create', + 'required' => false, + ]) + ->addRule('type', [ + 'type' => self::TYPE_STRING, + 'description' => 'Alert type: info, warning, error.', + 'default' => 'info', + 'example' => 'info', + ]) + ->addRule('channel', [ + 'type' => self::TYPE_STRING, + 'description' => 'Channel: email, sms, push, console, webhook.', + 'default' => '', + 'example' => 'email', + ]) + ->addRule('userId', [ + 'type' => self::TYPE_STRING, + 'description' => 'User this alert is addressed to.', + 'default' => '', + 'example' => '5e5bb8c16897e', + 'required' => false, + ]) + ->addRule('teamId', [ + 'type' => self::TYPE_STRING, + 'description' => 'Team this alert is addressed to.', + 'default' => '', + 'example' => '5e5bb8c16897e', + 'required' => false, + ]) + ->addRule('projectId', [ + 'type' => self::TYPE_STRING, + 'description' => 'Project the alert pertains to.', + 'default' => '', + 'example' => '5e5bb8c16897e', + 'required' => false, + ]) + ->addRule('title', [ + 'type' => self::TYPE_STRING, + 'description' => 'Alert title.', + 'default' => '', + 'example' => 'New sign-in detected', + ]) + ->addRule('body', [ + 'type' => self::TYPE_STRING, + 'description' => 'Alert body.', + 'default' => '', + 'example' => 'A new device signed in to your account.', + ]) + ->addRule('read', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Whether the alert has been read.', + 'default' => false, + 'example' => false, + 'required' => false, + ]) + ; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'Alert'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_ALERT; + } +} From a0400d2e93e64f68c7ec205aab97ba1007d83cbc Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:44:58 +1200 Subject: [PATCH 22/80] feat(notifications): scaffold GET /v1/account/alerts/:alertId/track endpoint Adds a public-scope tracking-pixel endpoint skeleton that always returns a 1x1 transparent PNG. The body is intentionally minimal so the email-tracking read-flag flip can be wired up cleanly in Wave 3 (ST14) once the JWT decode and DB write paths are designed. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Account/Http/Alerts/Track/Get.php | 68 +++++++++++++++++++ .../Modules/Account/Services/Http.php | 4 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php new file mode 100644 index 0000000000..7e9a9d9e44 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -0,0 +1,68 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/account/alerts/:alertId/track') + ->desc('Track alert') + ->groups(['api', 'account']) + ->label('scope', 'public') + ->label('sdk', new Method( + namespace: 'account', + name: 'getAlertTrack', + description: '/docs/references/account/get-alert-track.md', + auth: [AuthType::SESSION, AuthType::JWT], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_NONE, + ), + ], + contentType: ContentType::IMAGE_PNG, + )) + ->param('alertId', '', new UID(), 'Alert ID.') + ->param('jwt', '', new Text(2048, 0), 'Tracking token.', true) + ->inject('response') + ->inject('dbForPlatform') + ->callback($this->action(...)); + } + + public function action( + string $alertId, + string $jwt, + Response $response, + Database $dbForPlatform, + ): void { + // 1x1 transparent PNG. Wave 3 (ST14) will replace this body with + // JWT decode + read-flag flip; the skeleton always returns the pixel. + $pixel = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='); + + $response + ->setContentType('image/png') + ->addHeader('Cache-Control', 'no-store') + ->send($pixel); + } +} diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index ae2e841636..baaed61f4e 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,6 +12,7 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; +use Appwrite\Platform\Modules\Account\Http\Alerts\Track\Get as TrackAlert; use Utopia\Platform\Service; class Http extends Service @@ -29,6 +30,7 @@ class Http extends Service ->addAction(UpdateRecoveryCodes::getName(), new UpdateRecoveryCodes()) ->addAction(GetRecoveryCodes::getName(), new GetRecoveryCodes()) ->addAction(CreateChallenge::getName(), new CreateChallenge()) - ->addAction(UpdateChallenge::getName(), new UpdateChallenge()); + ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) + ->addAction(TrackAlert::getName(), new TrackAlert()); } } From 8c7ecd6cdb8db9b16d8a3f8cb107c5bde82c88ac Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 13:57:07 +1200 Subject: [PATCH 23/80] fix(notifications): add SDK Method group to alerts skeleton actions PHPStan flagged missing required `group` parameter on the SDK Method constructors for the alerts XList and Track\Get skeleton actions. Group set to 'alerts' to align with the resource grouping convention used elsewhere in the account namespace. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php | 1 + src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 7e9a9d9e44..32e1656af1 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -32,6 +32,7 @@ class Get extends Action ->label('scope', 'public') ->label('sdk', new Method( namespace: 'account', + group: 'alerts', name: 'getAlertTrack', description: '/docs/references/account/get-alert-track.md', auth: [AuthType::SESSION, AuthType::JWT], diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php index 3f4b60d3b7..18b3243071 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php @@ -31,6 +31,7 @@ class XList extends Action ->label('scope', 'account') ->label('sdk', new Method( namespace: 'account', + group: 'alerts', name: 'listAlerts', description: '/docs/references/account/list-alerts.md', auth: [AuthType::SESSION, AuthType::JWT], From 4e7236a3663d2d61bd2380fdc2c8e2b7025b68c2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:00:45 +1200 Subject: [PATCH 24/80] feat(notifications): register queueForNotifications resource in worker and request containers Co-Authored-By: Claude Opus 4.7 (1M context) --- app/init/resources/request.php | 4 ++++ app/init/worker/message.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 70d691370d..1ee6d79d4a 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -12,6 +12,7 @@ use Appwrite\Event\Event; use Appwrite\Event\Func; use Appwrite\Event\Mail; use Appwrite\Event\Messaging; +use Appwrite\Event\Notification; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Extend\Exception; @@ -123,6 +124,9 @@ return function (Container $container): void { $container->set('queueForMails', function (Publisher $publisher) { return new Mail($publisher); }, ['publisher']); + $container->set('queueForNotifications', function (Publisher $publisher) { + return new Notification($publisher); + }, ['publisher']); $container->set('queueForBuilds', function (Publisher $publisher) { return new Build($publisher); }, ['publisher']); diff --git a/app/init/worker/message.php b/app/init/worker/message.php index 17796fadcd..c61ed64f06 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -7,6 +7,7 @@ use Appwrite\Event\Event; use Appwrite\Event\Func; use Appwrite\Event\Mail; use Appwrite\Event\Messaging; +use Appwrite\Event\Notification; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Usage\Context; @@ -342,6 +343,10 @@ return function (Container $container): void { return new Mail($publisher); }, ['publisher']); + $container->set('queueForNotifications', function (Publisher $publisher) { + return new Notification($publisher); + }, ['publisher']); + $container->set('queueForBuilds', function (Publisher $publisher) { return new Build($publisher); }, ['publisher']); From 6f01c1492fe1a50f97fa672684c89ce21f637a68 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:16:25 +1200 Subject: [PATCH 25/80] fix(notifications): worker dedup by attribute, throw on console-zero-delivery, thread recipient userId, inject email tracking pixel Apply the four Greptile P1 fixes to the Notifications worker and extend it for C3 email read tracking and ST4's stripped SMTP plumbing. P1 #1: alreadyDelivered() now queries the indexed messageId attribute instead of getDocument($messageId). The action loop and Console adapter both write compound `$id`s (messageId + recipient hash), so the previous direct-id lookup always missed. P1 #3: action() no longer calls persistAlert after dispatchConsole; ConsoleAdapter persists internally. Email persists inside dispatchEmail BEFORE the adapter send so the alertId is available for the tracking pixel; webhook persists in the action loop after a successful HTTP send. P1 #4: dispatchConsole now throws when the adapter reports `deliveredTo === 0`, surfacing the per-recipient error. Recipient threading: dispatch() now takes the full recipient map and returns the alertId (or null when persistence is the caller's responsibility). persistAlert() reads userId/teamId from the recipient and grants per-user / per-team-owner CRUD permissions, falling back to payload permissions only when neither is set. The returned alertId lets dispatchEmail splice a 1x1 tracking pixel before the last `` tag, signed with a 30-day HS256 JWT (_APP_OPENSSL_KEY_V1) carrying {alertId, userId}. SMTP resolution: ST4 stripped `smtp` and `customMailOptions` from the Notification event payload, so the worker now resolves SMTP from the injected project Document (mirroring Mails.php / Memberships/Create.php), falling back to the env-driven cloud SMTP adapter when the project has no enabled override. Tests updated: SpyNotifications.dispatch() matches the new signature and emulates per-channel persistence so existing routing assertions keep their semantics. Memory `alerts` collection adds the `read` boolean attribute to mirror platform.php. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Platform/Workers/Notifications.php | 299 ++++++++++++++---- .../Platform/Workers/NotificationsTest.php | 25 +- 2 files changed, 263 insertions(+), 61 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index e5b811d424..26a6ff3793 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Workers; +use Ahc\Jwt\JWT; use Appwrite\Template\Template; use Appwrite\Utopia\Messaging\Adapter\Console as ConsoleAdapter; use Appwrite\Utopia\Messaging\Adapter\Webhook as WebhookAdapter; @@ -13,6 +14,9 @@ use Throwable; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; +use Utopia\Database\Query; use Utopia\Logger\Log; use Utopia\Messaging\Adapter\Email as EmailAdapter; use Utopia\Messaging\Adapter\Email\SMTP; @@ -28,6 +32,11 @@ class Notifications extends Action protected int $previewMaxLen = 150; protected string $whitespaceCodes = ' ‌​‍‎‏'; + /** + * Tracking pixel JWT lifetime: 30 days. + */ + private const TRACKING_JWT_TTL = 2592000; + /** * @var array */ @@ -80,9 +89,9 @@ class Notifications extends Action foreach ($recipients as $recipient) { $channel = $recipient['channel']; try { - $this->dispatch($recipient, $payload, $register, $dbForProject, $log); - if ($messageId !== '') { - $this->persistAlert($dbForProject, $messageId, $channel, $recipient['address'], $payload); + $alertId = $this->dispatch($recipient, $messageId, $payload, $project, $register, $dbForProject, $log); + if ($messageId !== '' && $channel === NOTIFICATION_TYPE_WEBHOOK && $alertId === null) { + $this->persistAlert($dbForProject, $messageId, $recipient, $payload); } } catch (Throwable $error) { $log->addTag('channel', $channel); @@ -93,7 +102,7 @@ class Notifications extends Action } /** - * @return array + * @return array */ private function resolveRecipients(array $payload): array { @@ -110,42 +119,69 @@ class Notifications extends Action return [['address' => $address, 'channel' => NOTIFICATION_TYPE_EMAIL]]; } + /** + * Look up an existing alert by the indexed `messageId` attribute. + * + * Greptile P1 #1: persistAlert and the Console adapter both write + * compound `$id`s (messageId + recipient hash), so a direct + * `getDocument($messageId)` would always miss. Query the attribute. + */ private function alreadyDelivered(Database $database, string $messageId): bool { try { - $existing = $database->getDocument('alerts', $messageId); - return !$existing->isEmpty(); + $matches = $database->find('alerts', [ + Query::equal('messageId', [$messageId]), + Query::limit(1), + ]); + return !empty($matches); } catch (Throwable) { return false; } } /** - * @param array{address: string, channel: string, signatureKey?: string} $recipient + * Dispatch a single recipient through the channel-appropriate adapter. + * + * Returns the alertId when the dispatcher (or its adapter) has already + * persisted an alert row, so the action loop knows to skip persistence. + * Returns null when persistence is the caller's responsibility. + * + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ - protected function dispatch(array $recipient, array $payload, Registry $register, Database $database, Log $log): void - { + protected function dispatch( + array $recipient, + string $messageId, + array $payload, + Document $project, + Registry $register, + Database $database, + Log $log, + ): ?string { $channel = $recipient['channel']; - $address = $recipient['address']; - switch ($channel) { - case NOTIFICATION_TYPE_EMAIL: - $this->dispatchEmail($address, $payload, $register, $log); - return; - case NOTIFICATION_TYPE_CONSOLE: - $this->dispatchConsole($address, $payload, $database); - return; - case NOTIFICATION_TYPE_WEBHOOK: - $this->dispatchWebhook($address, $payload, $recipient['signatureKey'] ?? null, $log); - return; - default: - throw new Exception('Unsupported notification channel: ' . $channel); - } + return match ($channel) { + NOTIFICATION_TYPE_EMAIL => $this->dispatchEmail($recipient, $messageId, $payload, $project, $register, $database, $log), + NOTIFICATION_TYPE_CONSOLE => $this->dispatchConsole($recipient, $messageId, $payload, $database), + NOTIFICATION_TYPE_WEBHOOK => $this->dispatchWebhook($recipient, $payload, $log), + default => throw new Exception('Unsupported notification channel: ' . $channel), + }; } - protected function dispatchEmail(string $address, array $payload, Registry $register, Log $log): void - { - $smtp = $payload['smtp'] ?? []; + /** + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + protected function dispatchEmail( + array $recipient, + string $messageId, + array $payload, + Document $project, + Registry $register, + Database $database, + Log $log, + ): ?string { + $address = $recipient['address']; + $smtp = $this->resolveSmtpConfig($project); + if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { throw new Exception('Skipped email notification. No SMTP configuration has been set.'); } @@ -153,20 +189,20 @@ class Notifications extends Action $type = empty($smtp) ? 'cloud' : 'smtp'; $log->addTag('type', $type); - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; - $hostname = System::getEnv('_APP_CONSOLE_DOMAIN'); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; + $consoleHostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', 'localhost')); $subject = $payload['subject'] ?? ''; $variables = $payload['variables'] ?? []; $variables = \array_merge($variables, $payload['templateParams'] ?? []); - $variables['host'] = $protocol . '://' . $hostname; + $variables['host'] = $protocol . '://' . $consoleHostname; $name = $payload['name'] ?? ''; $body = $payload['body'] ?? ''; $preview = $payload['preview'] ?? ''; $variables['subject'] = $subject; $variables['heading'] = $variables['heading'] ?? $subject; - $variables['year'] = date('Y'); + $variables['year'] = \date('Y'); $attachment = $payload['attachment'] ?? []; $bodyTemplate = $payload['bodyTemplate'] ?? ''; @@ -211,6 +247,22 @@ class Notifications extends Action } $subject = \strip_tags($subjectTemplate->render()); + // Persist alert BEFORE adapter send so the alertId is available for + // the tracking pixel. Failure to persist still allows the email to + // go out unsignals (we degrade gracefully). + $alertId = null; + if ($messageId !== '') { + $alertId = $this->persistAlert($database, $messageId, $recipient, $payload); + } + + // C3 tracking pixel: only injectable when we have a userId AND a + // persisted alertId AND a signing key. + $userId = $recipient['userId'] ?? ''; + $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); + if ($alertId !== null && $userId !== '' && !empty($opensslKey)) { + $body = $this->injectTrackingPixel($body, $alertId, $userId, $opensslKey); + } + /** @var EmailAdapter $adapter */ $adapter = empty($smtp) ? $register->get('smtp') @@ -235,19 +287,7 @@ class Notifications extends Action $replyTo = $defaultFromEmail; $replyToName = $defaultFromName; - $customMailOptions = $payload['customMailOptions'] ?? []; - - if (!empty($customMailOptions['senderEmail'])) { - $fromEmail = $customMailOptions['senderEmail']; - } - if (!empty($customMailOptions['senderName'])) { - $fromName = $customMailOptions['senderName']; - } - - if (!empty($customMailOptions['replyToEmail']) || !empty($customMailOptions['replyToName'])) { - $replyTo = $customMailOptions['replyToEmail'] ?? $replyTo; - $replyToName = $customMailOptions['replyToName'] ?? $replyToName; - } elseif (!empty($smtp)) { + if (!empty($smtp)) { $smtpReplyToEmail = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; $replyTo = !empty($smtpReplyToEmail) ? $smtpReplyToEmail : ($smtp['senderEmail'] ?? $replyTo); $replyToName = !empty($smtp['replyToName']) ? $smtp['replyToName'] : ($smtp['senderName'] ?? $replyToName); @@ -279,15 +319,20 @@ class Notifications extends Action try { $adapter->send($emailMessage); - } catch (\Throwable $error) { + } catch (Throwable $error) { if ($type === 'smtp') { throw new Exception('Error sending notification: ' . $error->getMessage(), 401); } throw new Exception('Error sending notification: ' . $error->getMessage(), 500); } + + return $alertId; } - protected function dispatchConsole(string $address, array $payload, Database $database): void + /** + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $database): ?string { $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; @@ -303,26 +348,51 @@ class Notifications extends Action $title = \strip_tags($rendered->render()); } - $recipients = [['userId' => $address]]; + $userId = $recipient['userId'] ?? $recipient['address']; + $teamId = $recipient['teamId'] ?? ''; - $deduplicationKey = $payload['deduplicationKey'] ?? ''; - $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : null; + $consoleRecipient = []; + if ($userId !== '') { + $consoleRecipient['userId'] = $userId; + } + if ($teamId !== '') { + $consoleRecipient['teamId'] = $teamId; + } $consoleMessage = new ConsoleMessage( - recipients: $recipients, + recipients: [$consoleRecipient], title: $title, body: $body, - type: 'info', - messageId: $messageId, + type: $payload['type'] ?? 'info', + messageId: $messageId !== '' ? $messageId : null, projectId: $projectId, ); $adapter = new ConsoleAdapter($database); - $adapter->send($consoleMessage); + $result = $adapter->send($consoleMessage); + + // Greptile P1 #4: surface adapter failures. The Console adapter + // catches per-recipient exceptions and reports zero deliveries via + // `deliveredTo`; without this throw the worker would silently + // succeed on a hard write failure. + if (($result['deliveredTo'] ?? 0) === 0) { + $error = $result['results'][0]['error'] ?? 'unknown error'; + throw new Exception('Console alert delivery failed: ' . $error); + } + + // Adapter persisted the alert, so the action loop must NOT + // call persistAlert again (Greptile P1 #3). + return null; } - protected function dispatchWebhook(string $address, array $payload, ?string $signatureKey, Log $log): void + /** + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + protected function dispatchWebhook(array $recipient, array $payload, Log $log): ?string { + $address = $recipient['address']; + $signatureKey = $recipient['signatureKey'] ?? null; + $body = [ 'subject' => $payload['subject'] ?? '', 'body' => $payload['body'] ?? '', @@ -350,30 +420,141 @@ class Notifications extends Action $error = $result['results'][0]['error'] ?? 'Unknown error'; throw new Exception('Webhook delivery failed: ' . $error); } + + // Caller persists the alert AFTER successful dispatch. + return null; } - private function persistAlert(Database $database, string $messageId, string $channel, string $address, array $payload): void + /** + * Persist an alert row. Returns the alertId so callers can build a + * tracking-pixel URL or otherwise reference the row. + * + * Idempotent: on a duplicate composite-key violation the existing + * row's id is returned. + * + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + protected function persistAlert(Database $database, string $messageId, array $recipient, array $payload): string { $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; - $permissions = $payload['permissions'] ?? []; + + $channel = $recipient['channel']; + $address = $recipient['address']; + $userId = $recipient['userId'] ?? ''; + $teamId = $recipient['teamId'] ?? ''; + + // Console alerts derive userId from address when no explicit + // userId is supplied (matches Console adapter's own bookkeeping). + if ($channel === NOTIFICATION_TYPE_CONSOLE && $userId === '' && $teamId === '') { + $userId = $address; + } + + $idSuffix = \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 8); + $alertId = $messageId . '_' . $idSuffix; + + $permissions = $this->buildAlertPermissions($userId, $teamId); + if (empty($permissions)) { + $permissions = $payload['permissions'] ?? []; + } $document = new Document([ - '$id' => $messageId . '_' . \substr(\md5($channel . $address), 0, 8), + '$id' => $alertId, '$permissions' => $permissions, 'messageId' => $messageId, - 'type' => 'info', + 'type' => $payload['type'] ?? 'info', 'channel' => $channel, - 'userId' => $channel === NOTIFICATION_TYPE_CONSOLE ? $address : null, + 'userId' => $userId !== '' ? $userId : null, + 'teamId' => $teamId !== '' ? $teamId : null, 'projectId' => $projectId, 'title' => $payload['subject'] ?? '', 'body' => $payload['body'] ?? '', + 'read' => false, ]); try { $database->createDocument('alerts', $document); + return $alertId; } catch (DuplicateException) { - // Idempotent — duplicate persistence is fine + $existing = $database->getDocument('alerts', $alertId); + return $existing->isEmpty() ? $alertId : $existing->getId(); } } + + /** + * @return array + */ + private function buildAlertPermissions(string $userId, string $teamId): array + { + $permissions = []; + if ($userId !== '') { + $permissions[] = Permission::read(Role::user($userId)); + $permissions[] = Permission::update(Role::user($userId)); + $permissions[] = Permission::delete(Role::user($userId)); + } + if ($teamId !== '') { + $permissions[] = Permission::read(Role::team($teamId)); + $permissions[] = Permission::update(Role::team($teamId, 'owner')); + $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + } + return $permissions; + } + + /** + * Resolve project SMTP config to the wire shape Mails.php expects. + * ST4 stripped `smtp` and `customMailOptions` from the Notification + * event payload, so the worker now reads from the project Document. + * Falls back to env-driven cloud SMTP when the project has not + * configured custom SMTP. + * + * @return array + */ + private function resolveSmtpConfig(Document $project): array + { + $smtp = $project->getAttribute('smtp', []); + if (!\is_array($smtp) || empty($smtp['enabled'] ?? false)) { + return []; + } + + return [ + 'host' => $smtp['host'] ?? '', + 'port' => $smtp['port'] ?? '', + 'username' => $smtp['username'] ?? '', + 'password' => $smtp['password'] ?? '', + 'secure' => $smtp['secure'] ?? '', + 'senderEmail' => $smtp['senderEmail'] ?? '', + 'senderName' => $smtp['senderName'] ?? '', + 'replyToEmail' => $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? '', + 'replyToName' => $smtp['replyToName'] ?? '', + ]; + } + + /** + * Splice a 1x1 tracking pixel before the last `` tag (or + * append at the end if the body has no closing tag). The pixel + * carries a 30-day JWT identifying the alert and user, which the + * `/v1/account/alerts/:alertId/track` endpoint verifies before + * marking the alert as read. + */ + private function injectTrackingPixel(string $body, string $alertId, string $userId, string $opensslKey): string + { + $jwt = (new JWT($opensslKey, 'HS256', self::TRACKING_JWT_TTL, 0)) + ->encode([ + 'alertId' => $alertId, + 'userId' => $userId, + ]); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; + $hostname = System::getEnv('_APP_DOMAIN', 'localhost'); + + $pixelUrl = $protocol . '://' . $hostname . '/v1/account/alerts/' . $alertId . '/track?jwt=' . \urlencode($jwt); + $pixel = ''; + + // Case-insensitive splice before the LAST . + if (\preg_match('/<\/body\s*>(?!.*<\/body\s*>)/is', $body)) { + return \preg_replace('/<\/body\s*>(?!.*<\/body\s*>)/is', $pixel . '$0', $body, 1) ?? ($body . $pixel); + } + + return $body . $pixel; + } } diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 2e0c9d82f4..423e0846ad 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -31,8 +31,15 @@ class SpyNotifications extends Notifications /** @var array */ public array $throwOn = []; - protected function dispatch(array $recipient, array $payload, Registry $register, Database $database, Log $log): void - { + protected function dispatch( + array $recipient, + string $messageId, + array $payload, + Document $project, + Registry $register, + Database $database, + Log $log, + ): ?string { $channel = $recipient['channel']; $this->dispatched[] = [ 'channel' => $channel, @@ -44,6 +51,19 @@ class SpyNotifications extends Notifications if (isset($this->throwOn[$channel])) { throw $this->throwOn[$channel]; } + + // Mirror the real adapters' persistence contract so the action + // loop's branching (console/email persist internally; webhook + // persists in caller) is exercised end-to-end. + if ($messageId === '') { + return null; + } + + if ($channel === NOTIFICATION_TYPE_CONSOLE || $channel === NOTIFICATION_TYPE_EMAIL) { + return $this->persistAlert($database, $messageId, $recipient, $payload); + } + + return null; } } @@ -82,6 +102,7 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); $this->registry = new Registry(); $this->project = new Document(['$id' => 'project-x']); From e54fbf3b3f94792fcec1b2046a3a5ce6547d1e41 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:35:53 +1200 Subject: [PATCH 26/80] feat(notifications): mark alert read via PATCH /v1/account/alerts/:alertId/read Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Account/Http/Alerts/Read/Update.php | 75 +++++++++++++++++++ .../Modules/Account/Services/Http.php | 2 + 2 files changed, 77 insertions(+) create mode 100644 src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php new file mode 100644 index 0000000000..1aa71013fd --- /dev/null +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php @@ -0,0 +1,75 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) + ->setHttpPath('/v1/account/alerts/:alertId/read') + ->desc('Mark alert read') + ->groups(['api', 'account']) + ->label('scope', 'account') + ->label('sdk', new Method( + namespace: 'account', + group: 'alerts', + name: 'updateAlertRead', + description: '/docs/references/account/update-alert-read.md', + auth: [AuthType::SESSION, AuthType::JWT], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_ALERT, + ) + ] + )) + ->param('alertId', '', new UID(), 'Alert ID.') + ->inject('response') + ->inject('dbForPlatform') + ->inject('user') + ->callback($this->action(...)); + } + + public function action( + string $alertId, + Response $response, + Database $dbForPlatform, + Document $user, + ): void { + $alert = $dbForPlatform->getDocument('alerts', $alertId); + + if ($alert->isEmpty()) { + throw new Exception(Exception::DOCUMENT_NOT_FOUND); + } + + if ($alert->getAttribute('userId') !== $user->getId()) { + throw new Exception(Exception::USER_UNAUTHORIZED); + } + + $updated = $dbForPlatform->updateDocument('alerts', $alertId, new Document([ + 'read' => true, + ])); + + $response->dynamic($updated, Response::MODEL_ALERT); + } +} diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index fbdf2aecdf..1547412dee 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,6 +12,7 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; +use Appwrite\Platform\Modules\Account\Http\Alerts\Read\Update as UpdateAlertRead; use Appwrite\Platform\Modules\Account\Http\Alerts\Track\Get as TrackAlert; use Appwrite\Platform\Modules\Account\Http\Alerts\XList as ListAlerts; use Utopia\Platform\Service; @@ -33,6 +34,7 @@ class Http extends Service ->addAction(CreateChallenge::getName(), new CreateChallenge()) ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) ->addAction(ListAlerts::getName(), new ListAlerts()) + ->addAction(UpdateAlertRead::getName(), new UpdateAlertRead()) ->addAction(TrackAlert::getName(), new TrackAlert()); } } From 5dd987711b55425fe33f8d1d85dcf2cc67f14ec9 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:46:22 +1200 Subject: [PATCH 27/80] feat(notifications): implement GET /v1/account/alerts list endpoint body Replace the placeholder body of the listAlerts action with real implementation: scope alerts to the current user via userId equality, parse incoming queries, support cursor pagination over the alerts collection, run filters through count for total, and return MODEL_ALERT_LIST. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Account/Http/Alerts/XList.php | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php index 18b3243071..c91ec1232d 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Account\Http\Alerts; +use Appwrite\Extend\Exception; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; @@ -9,6 +10,10 @@ use Appwrite\Utopia\Database\Validator\Queries\Alerts; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Order as OrderException; +use Utopia\Database\Exception\Query as QueryException; +use Utopia\Database\Query; +use Utopia\Database\Validator\Query\Cursor; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -58,9 +63,51 @@ class XList extends Action Database $dbForPlatform, Document $user ): void { + try { + $queries = Query::parseQueries($queries); + } catch (QueryException $e) { + throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); + } + + $queries[] = Query::equal('userId', [$user->getId()]); + + /** + * Get cursor document if there was a cursor query, we use array_filter and reset for reference $cursor to $queries + */ + $cursor = \array_filter($queries, function ($query) { + return \in_array($query->getMethod(), [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]); + }); + $cursor = reset($cursor); + if ($cursor) { + /** @var Query $cursor */ + + $validator = new Cursor(); + if (!$validator->isValid($cursor)) { + throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription()); + } + + $alertId = $cursor->getValue(); + $cursorDocument = $dbForPlatform->getDocument('alerts', $alertId); + + if ($cursorDocument->isEmpty()) { + throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Alert '{$alertId}' for the 'cursor' value not found."); + } + + $cursor->setValue($cursorDocument); + } + + $filterQueries = Query::groupByType($queries)['filters']; + + try { + $results = $dbForPlatform->find('alerts', $queries); + $total = $dbForPlatform->count('alerts', $filterQueries, APP_LIMIT_COUNT); + } catch (OrderException $e) { + throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); + } + $response->dynamic(new Document([ - 'alerts' => [], - 'total' => 0, + 'alerts' => $results, + 'total' => $total, ]), Response::MODEL_ALERT_LIST); } } From dae98bf240592497c7699166ead7bb9da5ac8e9d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:46:33 +1200 Subject: [PATCH 28/80] feat(notifications): track email opens by decoding pixel JWT and marking alert read Replace placeholder body with JWT decode, alertId/userId match validation, sparse update of read=true via injected Authorization::skip. Always returns 1x1 PNG regardless of decode outcome to avoid leaking JWT validity through response status. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Account/Http/Alerts/Track/Get.php | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 32e1656af1..7c5a48346c 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -2,15 +2,19 @@ namespace Appwrite\Platform\Modules\Account\Http\Alerts\Track; +use Ahc\Jwt\JWT; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; +use Utopia\System\System; use Utopia\Validator\Text; class Get extends Action @@ -48,6 +52,7 @@ class Get extends Action ->param('jwt', '', new Text(2048, 0), 'Tracking token.', true) ->inject('response') ->inject('dbForPlatform') + ->inject('authorization') ->callback($this->action(...)); } @@ -56,9 +61,39 @@ class Get extends Action string $jwt, Response $response, Database $dbForPlatform, + Authorization $authorization, ): void { - // 1x1 transparent PNG. Wave 3 (ST14) will replace this body with - // JWT decode + read-flag flip; the skeleton always returns the pixel. + $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); + + if ($secret !== '' && $jwt !== '') { + try { + $decoder = new JWT($secret, 'HS256', 2592000, 0); + $decoded = $decoder->decode($jwt); + + if ( + isset($decoded['alertId'], $decoded['userId']) + && $decoded['alertId'] === $alertId + ) { + $authorization->skip(function () use ($dbForPlatform, $alertId, $decoded) { + $alert = $dbForPlatform->getDocument('alerts', $alertId); + + if ( + !$alert->isEmpty() + && $alert->getAttribute('userId') === $decoded['userId'] + && $alert->getAttribute('read') !== true + ) { + $dbForPlatform->updateDocument('alerts', $alertId, new Document([ + 'read' => true, + ])); + } + }); + } + } catch (\Throwable) { + // Silent fail — never reveal JWT validity through response status + } + } + + // 1x1 transparent PNG (canonical 67-byte payload) $pixel = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='); $response From 067bf582dc2a241a532dd2a659c40877d9ab9c57 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 14:46:57 +1200 Subject: [PATCH 29/80] feat(webhooks): emit pausing alerts via notifications worker to owners on email and console When a webhook is permanently paused after exceeding the max failure threshold, fan out the alert through the new Notifications worker instead of the legacy Mails worker. Recipients are filtered to project owners only and receive both an email and a console notification, with a single deduplication key per webhook + attempts so duplicate triggers collapse downstream. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Platform/Workers/Webhooks.php | 80 ++++++++++++++++------ 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index a7f4595966..c5abf4f755 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -2,8 +2,8 @@ namespace Appwrite\Platform\Workers; -use Appwrite\Event\Mail; use Appwrite\Event\Message\Usage as UsageMessage; +use Appwrite\Event\Notification; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Template\Template; use Appwrite\Usage\Context as UsageContext; @@ -36,7 +36,7 @@ class Webhooks extends Action ->inject('message') ->inject('project') ->inject('dbForPlatform') - ->inject('queueForMails') + ->inject('queueForNotifications') ->inject('publisherForUsage') ->inject('log') ->inject('plan') @@ -47,14 +47,14 @@ class Webhooks extends Action * @param Message $message * @param Document $project * @param Database $dbForPlatform - * @param Mail $queueForMails + * @param Notification $queueForNotifications * @param UsagePublisher $publisherForUsage * @param Log $log * @param array $plan * @return void * @throws Exception */ - public function action(Message $message, Document $project, Database $dbForPlatform, Mail $queueForMails, UsagePublisher $publisherForUsage, Log $log, array $plan): void + public function action(Message $message, Document $project, Database $dbForPlatform, Notification $queueForNotifications, UsagePublisher $publisherForUsage, Log $log, array $plan): void { $this->errors = []; $payload = $message->getPayload(); @@ -73,7 +73,7 @@ class Webhooks extends Action foreach ($project->getAttribute('webhooks', []) as $webhook) { if (array_intersect($webhook->getAttribute('events', []), $events)) { - $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $queueForMails, $publisherForUsage, $plan); + $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $queueForNotifications, $publisherForUsage, $plan); } } @@ -89,11 +89,11 @@ class Webhooks extends Action * @param Document $user * @param Document $project * @param Database $dbForPlatform - * @param Mail $queueForMails + * @param Notification $queueForNotifications * @param array $plan * @return void */ - private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, Mail $queueForMails, UsagePublisher $publisherForUsage, array $plan): void + private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, Notification $queueForNotifications, UsagePublisher $publisherForUsage, array $plan): void { if ($webhook->getAttribute('enabled') !== true) { return; @@ -171,7 +171,7 @@ class Webhooks extends Action if ($attempts >= \intval(System::getEnv('_APP_WEBHOOK_MAX_FAILED_ATTEMPTS', '10'))) { $webhook->setAttribute('enabled', false); $updatePayload['enabled'] = false; - $this->sendEmailAlert($attempts, $statusCode, $webhook, $project, $dbForPlatform, $queueForMails, $plan); + $this->sendAlert($attempts, $statusCode, $webhook, $project, $dbForPlatform, $queueForNotifications, $plan); } $dbForPlatform->updateDocument('webhooks', $webhook->getId(), new Document($updatePayload)); @@ -203,26 +203,49 @@ class Webhooks extends Action * @param Document $webhook * @param Document $project * @param Database $dbForPlatform - * @param Mail $queueForMails + * @param Notification $queueForNotifications * @param array $plan * @return void */ - public function sendEmailAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, Mail $queueForMails, array $plan): void + public function sendAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, Notification $queueForNotifications, array $plan): void { $memberships = $dbForPlatform->find('memberships', [ Query::equal('teamInternalId', [$project->getAttribute('teamInternalId')]), Query::limit(APP_LIMIT_SUBQUERY) ]); - $userIds = array_column(\array_map(fn ($membership) => $membership->getArrayCopy(), $memberships), 'userId'); + // Webhook-paused alerts go only to project owners — non-owner team members do not receive them. + $ownerMemberships = \array_filter( + $memberships, + fn (Document $membership) => \in_array('owner', $membership->getAttribute('roles', []), true) + ); + + if (empty($ownerMemberships)) { + return; + } + + $userIds = \array_values(\array_unique(\array_filter(\array_map( + fn (Document $membership) => $membership->getAttribute('userId'), + $ownerMemberships + )))); + + if (empty($userIds)) { + return; + } $users = $dbForPlatform->find('users', [ Query::equal('$id', $userIds), + Query::limit(APP_LIMIT_SUBQUERY), ]); + if (empty($users)) { + return; + } + $projectId = $project->getId(); $region = $project->getAttribute('region', 'default'); $webhookId = $webhook->getId(); + $teamId = $project->getAttribute('teamId'); $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); @@ -241,7 +264,6 @@ class Webhooks extends Action $template->setParam('{{termsUrl}}', $plan['termsUrl'] ?? APP_EMAIL_TERMS_URL); $template->setParam('{{privacyUrl}}', $plan['privacyUrl'] ?? APP_EMAIL_PRIVACY_URL); - // TODO: Use setbodyTemplate once #7307 is merged $subject = 'Webhook deliveries have been paused'; $preview = 'Webhook deliveries to your endpoint have been paused.'; $body = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'); @@ -249,20 +271,38 @@ class Webhooks extends Action $body ->setParam('{{subject}}', $subject) ->setParam('{{message}}', $template->render()) - ->setParam('{{year}}', date("Y")); + ->setParam('{{year}}', date('Y')); - $queueForMails + $queueForNotifications ->setProject($project) ->setSubject($subject) ->setPreview($preview) - ->setBody($body->render()); + ->setBody($body->render()) + ->setDeduplicationKey('webhook:' . $webhook->getId() . ':paused:' . $attempts); foreach ($users as $user) { - $queueForMails - ->setVariables(['user' => $user->getAttribute('name', '')]) - ->setName($user->getAttribute('name', '')) - ->setRecipient($user->getAttribute('email')) - ->trigger(); + $email = $user->getAttribute('email'); + $userId = $user->getId(); + + if (!empty($email)) { + $queueForNotifications->addRecipient( + $email, + NOTIFICATION_TYPE_EMAIL, + null, + $userId, + $teamId, + ); + } + + $queueForNotifications->addRecipient( + $userId, + NOTIFICATION_TYPE_CONSOLE, + null, + $userId, + $teamId, + ); } + + $queueForNotifications->trigger(); } } From 3e0df70f5cf7fd9cab59478b30d4c0124ddc4876 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 15:09:28 +1200 Subject: [PATCH 30/80] test(notifications): e2e tests for list, mark-read, tracking pixel, and webhook-failure fanout Add 6 new test methods to NotificationsBase that exercise the wave3 account-alerts surface end-to-end: - testListAccountAlertsEmpty: GET /v1/account/alerts shape check - testWebhookFailureCreatesConsoleAlert: drives a webhook past _APP_WEBHOOK_MAX_FAILED_ATTEMPTS via user-create events and polls /account/alerts until the worker fans the paused alert out to the project owner on the console channel - testMarkAlertReadTogglesFlag: PATCH /:alertId/read happy path - testMarkAlertReadUnauthorized: stranger console user cannot mark someone else's alert as read; alert remains unread for the owner - testTrackingPixelTogglesRead: GET /:alertId/track with a valid HS256 JWT signed with _APP_OPENSSL_KEY_V1 returns the canonical 1x1 PNG and atomically marks the alert as read - testTrackingPixelInvalidTokenReturnsPng: tampered JWT still gets a PNG (no information disclosure) but performs no DB write Helpers: - seedWebhookFailureAlert: registers a webhook pointing at an unroutable address (http://127.0.0.1:1/), drives max+2 user-create events through the project, polls assertEventually with a 60s budget for the paused alert keyed by the deterministic md5 of 'webhook::paused:' - createConsoleUser: spins up a fresh, unrelated console user with its own session for the unauthorized assertion - getConsoleAlertHeaders: console-session auth bundle reused across every alerts call, so the trait works identically under SideServer and SideConsole hosts --- .../Notifications/NotificationsBase.php | 391 +++++++++++++++++- 1 file changed, 386 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index adefbaa04b..6d1e1a0783 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -2,20 +2,30 @@ namespace Tests\E2E\Services\Notifications; +use Ahc\Jwt\JWT; use Tests\E2E\Client; +use Utopia\Database\Helpers\ID; +use Utopia\System\System; /** - * End-to-end coverage for the notifications queue health surface. + * End-to-end coverage for the notifications queue health surface and the + * account-alerts user-facing API. * - * The worker itself is exercised in unit tests with the queue payload pinned - * to a controlled shape — the server side cannot deterministically inject a - * Notification onto the live queue without an admin endpoint, so this suite - * validates the public health contract that ops and KEDA scale on: + * The notification worker itself is exercised in unit tests with a pinned + * queue payload — the server side cannot deterministically inject a + * Notification onto the live queue without an admin endpoint, so the health + * portion validates the public contract that ops and KEDA scale on: * * - GET /v1/health/queue/notifications returns the live queue depth * - the threshold guard returns 503 when the depth exceeds the budget * - the failed-jobs surface accepts the notifications queue name * + * The alerts portion exercises the full webhook-paused fanout end-to-end: + * + * - GET /v1/account/alerts (empty + populated) + * - PATCH /v1/account/alerts/:alertId/read (happy + unauthorized) + * - GET /v1/account/alerts/:alertId/track (valid JWT + invalid JWT) + * * Dedup, per-channel dispatch, and webhook signing are covered by: * - tests/unit/Platform/Workers/NotificationsTest.php * - tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -55,4 +65,375 @@ trait NotificationsBase $this->assertSame(200, $response['headers']['status-code']); $this->assertIsInt($response['body']['size']); } + + public function testListAccountAlertsEmpty(): void + { + // Always read alerts as the console-authenticated owner of the team. + // The /v1/account/alerts endpoint is platform-scoped (dbForPlatform) and + // requires a session — server-mode API keys do not satisfy it. + $response = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertArrayHasKey('alerts', $response['body']); + $this->assertArrayHasKey('total', $response['body']); + $this->assertIsArray($response['body']['alerts']); + $this->assertIsInt($response['body']['total']); + + // The shared root console user may carry alerts from prior tests in the + // same suite — assert only that the response shape is correct and that + // counts agree. + $this->assertSame(\count($response['body']['alerts']), \min(\count($response['body']['alerts']), $response['body']['total'])); + } + + public function testWebhookFailureCreatesConsoleAlert(): void + { + $alertId = $this->seedWebhookFailureAlert(); + + $this->assertNotEmpty($alertId); + + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + + $this->assertNotNull($found, 'Seeded alert not present in /account/alerts response.'); + $this->assertSame('console', $found['channel']); + $this->assertStringContainsStringIgnoringCase('webhook', $found['title']); + + // Cache the seeded alert id for downstream tests in the same process. + self::$seededAlertId = $alertId; + } + + public function testMarkAlertReadTogglesFlag(): void + { + $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + $patch = $this->client->call( + Client::METHOD_PATCH, + '/account/alerts/' . $alertId . '/read', + $this->getConsoleAlertHeaders(), + [] + ); + + $this->assertSame(200, $patch['headers']['status-code']); + $this->assertSame($alertId, $patch['body']['$id']); + $this->assertTrue($patch['body']['read']); + + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + + $this->assertNotNull($found); + $this->assertTrue($found['read']); + + self::$seededAlertId = null; // alert is read — downstream tests will seed fresh + } + + public function testMarkAlertReadUnauthorized(): void + { + $alertId = $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + // Create a stranger console user with their own session. + $stranger = $this->createConsoleUser(); + + $unauthorized = $this->client->call( + Client::METHOD_PATCH, + '/account/alerts/' . $alertId . '/read', + [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $stranger['session'], + 'x-appwrite-project' => 'console', + 'x-appwrite-mode' => 'admin', + ], + [] + ); + + $this->assertSame(401, $unauthorized['headers']['status-code']); + $this->assertSame('user_unauthorized', $unauthorized['body']['type'] ?? ''); + + // Owner re-fetches — alert must still be unread. + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + + $this->assertNotNull($found); + $this->assertFalse($found['read']); + + self::$seededAlertId = $alertId; + } + + public function testTrackingPixelTogglesRead(): void + { + $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + $secret = System::getEnv('_APP_OPENSSL_KEY_V1') ?: 'your-secret-key'; + $userId = $this->getRoot()['$id']; + + $jwt = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + 'alertId' => $alertId, + 'userId' => $userId, + ]); + + $response = $this->client->call( + Client::METHOD_GET, + '/account/alerts/' . $alertId . '/track', + ['x-appwrite-project' => 'console'], + ['jwt' => $jwt] + ); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertStringContainsString('image/png', $response['headers']['content-type']); + $this->assertNotEmpty($response['body']); + $this->assertSame("\x89PNG\r\n\x1a\n", \substr($response['body'], 0, 8), 'Response body must be a PNG.'); + + // Subsequent listing should report alert as read. + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + + $this->assertNotNull($found); + $this->assertTrue($found['read']); + + self::$seededAlertId = null; + } + + public function testTrackingPixelInvalidTokenReturnsPng(): void + { + $alertId = $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + $response = $this->client->call( + Client::METHOD_GET, + '/account/alerts/' . $alertId . '/track', + ['x-appwrite-project' => 'console'], + ['jwt' => 'tampered-or-empty'] + ); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertStringContainsString('image/png', $response['headers']['content-type']); + $this->assertNotEmpty($response['body']); + $this->assertSame("\x89PNG\r\n\x1a\n", \substr($response['body'], 0, 8), 'Response body must be a PNG.'); + + // Alert must remain unread — invalid JWT is silently ignored, no DB write. + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + + $this->assertNotNull($found); + $this->assertFalse($found['read']); + + self::$seededAlertId = $alertId; + } + + /** + * @var string|null Cached seeded alert id so consecutive tests can reuse it + * without paying the cost of another 10-failure webhook drive. + */ + protected static ?string $seededAlertId = null; + + /** + * Build the auth header set used to talk to the platform-scoped + * /v1/account/alerts endpoints. Always console session, regardless of + * the trait's host (server vs console) — the endpoint requires a session + * and the root user owns the project team. + * + * @return array + */ + protected function getConsoleAlertHeaders(): array + { + return [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-project' => 'console', + 'x-appwrite-mode' => 'admin', + ]; + } + + /** + * Drive a webhook past the failure threshold so the + * Webhooks worker emits a console+email alert fanout to the project + * owner. Returns the alert id. + * + * Uses a unique webhook-per-call so concurrent tests don't share + * attempt counters. + */ + protected function seedWebhookFailureAlert(): string + { + $project = $this->getProject(); + $projectId = $project['$id']; + + // Register a webhook pointing at an unroutable address. The Webhook + // worker will fail every delivery and after 10 attempts pause it + // and enqueue a console+email alert for the project owner. + $webhook = $this->client->call(Client::METHOD_POST, '/webhooks', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-project' => $projectId, + 'x-appwrite-mode' => 'admin', + ], [ + 'webhookId' => ID::unique(), + 'name' => 'Failing Webhook ' . \uniqid(), + 'events' => ['users.*.create'], + 'url' => 'http://127.0.0.1:1/', + 'tls' => false, + ]); + + $this->assertSame(201, $webhook['headers']['status-code']); + $webhookId = $webhook['body']['$id']; + + $maxAttempts = (int) System::getEnv('_APP_WEBHOOK_MAX_FAILED_ATTEMPTS', '10'); + + // Drive the webhook past its failure threshold by issuing user-create + // events, each of which triggers a delivery attempt the worker will + // fail. Each create event is also dispatched to the project's + // pre-existing reachable webhook — that one stays healthy. + for ($i = 0; $i < $maxAttempts + 2; $i++) { + $email = \uniqid('alert-seed-', true) . '@localhost.test'; + $created = $this->client->call(Client::METHOD_POST, '/users', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'userId' => ID::unique(), + 'email' => $email, + 'password' => 'password', + 'name' => 'Webhook Failure Driver', + ]); + + // Tolerate transient 409s under parallel load. + $this->assertContains( + $created['headers']['status-code'], + [201, 409], + 'User create failed while seeding webhook failure: ' . ($created['body']['message'] ?? '') + ); + } + + // The deduplication key (and therefore the alert's messageId hash) is + // unique per (webhook, attempts) tuple — see Webhooks worker. Compute + // the expected messageId so we can deterministically match the alert + // for *this* test instance even when other tests in the same process + // have seeded their own webhook-failure alerts. + // Driver loops max+2 events; the worker may pause anywhere from + // attempts==max to attempts==max+2 depending on which delivery + // crossed the threshold. Record possible message ids. + $expectedMessageIds = []; + for ($attempts = $maxAttempts; $attempts <= $maxAttempts + 2; $attempts++) { + $expectedMessageIds[] = \md5('webhook:' . $webhookId . ':paused:' . $attempts); + } + + // Poll for the alert. Alert creation is async (notification worker) + // and webhook deliveries also queue up — give them generous time. + $alertId = null; + $this->assertEventually(function () use (&$alertId, $webhookId, $expectedMessageIds) { + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + foreach ($list['body']['alerts'] as $alert) { + if ( + ($alert['channel'] ?? '') === 'console' + && \in_array($alert['messageId'] ?? '', $expectedMessageIds, true) + ) { + $alertId = $alert['$id']; + return; + } + } + + $this->fail('No webhook-paused console alert observed yet for webhook ' . $webhookId); + }, 60000, 1000); + + // Cleanup the failing webhook so it doesn't keep firing in the + // background for subsequent tests in the same process. + $this->client->call(Client::METHOD_DELETE, '/webhooks/' . $webhookId, [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-project' => $projectId, + 'x-appwrite-mode' => 'admin', + ]); + + return $alertId; + } + + /** + * Create a fresh console user with its own session, separate from the + * shared root user. Used to assert that strangers cannot mark someone + * else's alert as read. + * + * @return array{$id: string, email: string, session: string} + */ + protected function createConsoleUser(): array + { + $email = \uniqid('stranger-', true) . \getmypid() . \bin2hex(\random_bytes(4)) . '@localhost.test'; + $password = 'password'; + + $user = $this->client->call(Client::METHOD_POST, '/account', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'userId' => ID::unique(), + 'email' => $email, + 'password' => $password, + 'name' => 'Stranger', + ]); + + $this->assertSame(201, $user['headers']['status-code']); + + $session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'email' => $email, + 'password' => $password, + ]); + + $this->assertSame(201, $session['headers']['status-code']); + $this->assertNotEmpty($session['cookies']['a_session_console'] ?? ''); + + return [ + '$id' => $user['body']['$id'], + 'email' => $email, + 'session' => $session['cookies']['a_session_console'], + ]; + } } From 534123cd87737994c23a531cc9dd4e26114d1d89 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 15:10:38 +1200 Subject: [PATCH 31/80] test(notifications): add unit tests for dedup, fanout, console skip, zero-delivery, recipient struct, and tracking pixel Cover the new worker behaviors introduced in waves 1-3: - testDedupQueriesByAttributeNotById: prove alreadyDelivered() queries the messageId attribute, not getDocument($messageId), by seeding a row with a non-matching $id but matching messageId. - testConsoleChannelSkipsPersistAlert: confirm the action loop does NOT call persistAlert for console recipients (the adapter persists). - testConsoleZeroDeliveryThrows: surface adapter failures via the worker. - testMultiRecipientFanoutNoCollision: same messageId across recipients must produce distinct $id values. - testRecipientStructRoundtripsUserIdAndTeamId: persisted alert carries recipient userId/teamId. - testTrackingPixelInjectedIntoEmailHtml: verify the tracking pixel is spliced before the last tag with the expected URL shape. - testPersistAlertReturnsAlertIdAndStoresUserId: dispatchEmail's persistAlert returns a resolvable id and stores userId + read=false. ConsoleTest: add testMultiRecipientWithSameMessageIdGeneratesDistinctIds and update existing tests to use the post-ST2 compound `$id` form (messageId + 8-hex md5 suffix). Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Platform/Workers/NotificationsTest.php | 298 ++++++++++++++++++ .../Utopia/Messaging/Adapter/ConsoleTest.php | 53 +++- 2 files changed, 348 insertions(+), 3 deletions(-) diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 423e0846ad..3b77cc7c24 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -13,6 +13,8 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log; +use Utopia\Messaging\Adapter\Email as EmailAdapter; +use Utopia\Messaging\Messages\Email as EmailMessage; use Utopia\Queue\Message; use Utopia\Registry\Registry; @@ -67,6 +69,72 @@ class SpyNotifications extends Notifications } } +/** + * Worker that runs the real `dispatchConsole` (so the ConsoleAdapter writes + * the alert row) but counts how many times the action loop calls + * `persistAlert`. Used to assert that the loop does NOT double-persist for + * console recipients (Greptile P1 #3). + */ +class CountingPersistAlertNotifications extends Notifications +{ + public int $persistAlertCalls = 0; + + /** @var array */ + public array $persistedIds = []; + + protected function persistAlert(Database $database, string $messageId, array $recipient, array $payload): string + { + $this->persistAlertCalls++; + $alertId = parent::persistAlert($database, $messageId, $recipient, $payload); + $this->persistedIds[] = $alertId; + return $alertId; + } +} + +/** + * Worker that simulates a console-adapter zero-delivery outcome (e.g. + * permission denied on createDocument) without depending on a real adapter + * failure mode. + */ +class ZeroDeliveryConsoleNotifications extends Notifications +{ + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $database): ?string + { + // Simulate the Console adapter swallowing a per-recipient + // exception and reporting zero deliveries. + throw new \Exception('Console alert delivery failed: permission denied'); + } +} + +/** + * Captures the EmailMessage handed to the SMTP adapter so tests can assert + * on the rendered HTML body without touching a real mail server. + */ +class SpyEmailAdapter extends EmailAdapter +{ + public ?EmailMessage $captured = null; + + public function getName(): string + { + return 'SpySMTP'; + } + + public function getMaxMessagesPerRequest(): int + { + return 1000; + } + + protected function process(EmailMessage $message): array + { + $this->captured = $message; + return [ + 'deliveredTo' => 1, + 'type' => $this->getType(), + 'results' => [['recipient' => $message->getTo()[0]['email'] ?? '', 'status' => 'sent']], + ]; + } +} + class NotificationsTest extends TestCase { private Database $database; @@ -296,4 +364,234 @@ class NotificationsTest extends TestCase $rows = $this->database->find('alerts'); $this->assertCount(0, $rows, 'failed dispatch must not persist alert'); } + + public function testDedupQueriesByAttributeNotById(): void + { + $worker = new SpyNotifications(); + + // Seed an alert row with an arbitrary $id but the matching dedup + // messageId attribute. If alreadyDelivered() short-circuits via + // getDocument($messageId) it will miss this seed and dispatch + // anyway. Querying by the `messageId` attribute is the only way to + // see the seed. + $messageId = \md5('dup-key'); + $this->database->createDocument('alerts', new Document([ + '$id' => 'random-id-123', + '$permissions' => [Permission::read(Role::any())], + 'messageId' => $messageId, + 'channel' => 'console', + 'title' => 'seed', + 'body' => 'seed', + ])); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE]], + 'subject' => 'Sub', + 'body' => 'B', + 'deduplicationKey' => 'dup-key', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(0, $worker->dispatched, 'attribute-keyed seed must trigger dedup short-circuit'); + $this->assertSame('hit', $this->log->getTags()['dedup'] ?? null); + } + + public function testConsoleChannelSkipsPersistAlert(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + ], + 'subject' => 'Heads up', + 'body' => 'console body', + 'deduplicationKey' => 'console-skip', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + // The Console adapter wrote exactly one alert; the action loop + // must NOT have called persistAlert (otherwise we'd see 2 rows or + // a duplicate-key swallow plus a non-zero counter). + $consoleRows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('channel', ['console']), + ]); + $this->assertCount(1, $consoleRows); + $this->assertSame(0, $worker->persistAlertCalls, 'console channel must NOT trigger action-loop persistAlert'); + } + + public function testConsoleZeroDeliveryThrows(): void + { + $worker = new ZeroDeliveryConsoleNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + ], + 'subject' => 'Heads up', + 'body' => 'console body', + 'deduplicationKey' => 'console-fail', + ]; + + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + $this->fail('expected console zero-delivery to throw'); + } catch (\Throwable $error) { + $this->assertStringContainsString('Console alert delivery failed', $error->getMessage()); + } + + $this->assertCount(0, $this->database->find('alerts'), 'failed console dispatch must not persist alert'); + } + + public function testMultiRecipientFanoutNoCollision(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + ['address' => 'user-2', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-2'], + ], + 'subject' => 'Heads up', + 'body' => 'console body', + 'deduplicationKey' => 'fanout', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $rows = $this->database->find('alerts'); + $this->assertCount(2, $rows, 'two recipients must produce two distinct alert rows'); + + $messageId = \md5('fanout'); + $ids = []; + foreach ($rows as $row) { + $this->assertSame($messageId, $row->getAttribute('messageId'), 'all rows share the dedup messageId'); + $ids[] = $row->getId(); + } + $this->assertCount(2, \array_unique($ids), 'recipient suffixes must keep $id values distinct'); + } + + public function testRecipientStructRoundtripsUserIdAndTeamId(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'console-recipient', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'userId' => 'u1', + 'teamId' => 't1', + ], + ], + 'subject' => 'Heads up', + 'body' => 'b', + 'deduplicationKey' => 'roundtrip', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $rows = $this->database->find('alerts'); + $this->assertCount(1, $rows); + $this->assertSame('u1', $rows[0]->getAttribute('userId')); + $this->assertSame('t1', $rows[0]->getAttribute('teamId')); + } + + public function testTrackingPixelInjectedIntoEmailHtml(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + // Force the cloud SMTP branch (project has no smtp config) and + // provide an OpenSSL key so injectTrackingPixel actually runs. + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + \putenv('_APP_OPENSSL_KEY_V1=test-key-32bytes-min-aaaaaaaaaaaaaa'); + + try { + $worker = new Notifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-7', + ], + ], + 'subject' => 'Heads up', + 'body' => 'plain body', + 'deduplicationKey' => 'pixel-key', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + \putenv($previousOpensslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpensslKey); + } + + $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); + + $body = $spy->captured->getContent(); + $this->assertStringContainsString(' must be present'); + $this->assertStringContainsString('/v1/account/alerts/', $body); + $this->assertStringContainsString('/track?jwt=', $body); + + // The pixel must sit BEFORE the last . + $lastBodyClose = \strripos($body, ''); + $pixelPosition = \strripos($body, ''); + $this->assertNotFalse($pixelPosition); + $this->assertLessThan($lastBodyClose, $pixelPosition, 'pixel must be spliced before the final '); + } + + public function testPersistAlertReturnsAlertIdAndStoresUserId(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + try { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-7', + ], + ], + 'subject' => 'Heads up', + 'body' => 'b', + 'deduplicationKey' => 'persist-email', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + + $this->assertSame(1, $worker->persistAlertCalls, 'email channel must persist exactly once'); + $this->assertCount(1, $worker->persistedIds); + + $alertId = $worker->persistedIds[0]; + $row = $this->database->getDocument('alerts', $alertId); + $this->assertFalse($row->isEmpty(), 'persistAlert must return an id resolvable via getDocument'); + $this->assertSame('user-7', $row->getAttribute('userId')); + $this->assertFalse($row->getAttribute('read'), 'new alerts default to unread'); + $this->assertSame(\md5('persist-email'), $row->getAttribute('messageId')); + } } diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index 283fd17819..d1165fa002 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -48,6 +48,16 @@ class ConsoleTest extends TestCase $this->authorization->addRole(Role::any()->toString()); } + /** + * Mirrors the per-recipient `$id` derivation in + * Appwrite\Utopia\Messaging\Adapter\Console::process(). + */ + private function alertId(string $messageId, string $userId = '', string $teamId = ''): string + { + $key = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; + return $messageId . '_' . \substr(\md5($key), 0, 8); + } + public function testWritesAlertWithCorrectSchema(): void { $message = new ConsoleMessage( @@ -64,7 +74,7 @@ class ConsoleTest extends TestCase $this->assertSame(1, $result['deliveredTo']); - $stored = $this->database->getDocument('alerts', 'msg-aaa'); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-aaa', userId: 'user-1')); $this->assertFalse($stored->isEmpty()); $this->assertSame('msg-aaa', $stored->getAttribute('messageId')); $this->assertSame('console', $stored->getAttribute('channel')); @@ -86,7 +96,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', 'msg-perms-user'); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-perms-user', userId: 'user-2')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::user('user-2')), $permissions); @@ -105,7 +115,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', 'msg-team'); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-team', teamId: 'team-9')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::team('team-9')), $permissions); @@ -113,6 +123,43 @@ class ConsoleTest extends TestCase $this->assertContains(Permission::delete(Role::team('team-9', 'owner')), $permissions); } + public function testMultiRecipientWithSameMessageIdGeneratesDistinctIds(): void + { + $message = new ConsoleMessage( + recipients: [ + ['userId' => 'a'], + ['userId' => 'b'], + ], + title: 'Heads up', + body: 'multi', + messageId: ID::custom('same-msg'), + ); + + $adapter = new Console($this->database); + $result = $adapter->send($message); + + $this->assertSame(2, $result['deliveredTo']); + + $idA = $this->alertId('same-msg', userId: 'a'); + $idB = $this->alertId('same-msg', userId: 'b'); + + $this->assertNotSame($idA, $idB, 'recipient suffixes must be distinct'); + + $rowA = $this->database->getDocument('alerts', $idA); + $rowB = $this->database->getDocument('alerts', $idB); + $this->assertFalse($rowA->isEmpty(), 'first recipient row must exist'); + $this->assertFalse($rowB->isEmpty(), 'second recipient row must exist'); + + $this->assertSame('same-msg', $rowA->getAttribute('messageId')); + $this->assertSame('same-msg', $rowB->getAttribute('messageId')); + $this->assertSame('a', $rowA->getAttribute('userId')); + $this->assertSame('b', $rowB->getAttribute('userId')); + + // $id values must be `messageId_<8-hex>` and the suffixes differ. + $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{8}$/', $idA); + $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{8}$/', $idB); + } + public function testRejectsForeignMessageType(): void { $adapter = new Console($this->database); From 5357e7dd356d0171485cca26c21be4aa24719cdd Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 15:24:25 +1200 Subject: [PATCH 32/80] fix(notifications): quote $id key in PHPDoc array shape PHPStan rejects unquoted $id as a variable token in array shape syntax. Quoting the key satisfies the parser without changing the runtime contract. --- tests/e2e/Services/Notifications/NotificationsBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 6d1e1a0783..81f7cf7ebb 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -398,7 +398,7 @@ trait NotificationsBase * shared root user. Used to assert that strangers cannot mark someone * else's alert as read. * - * @return array{$id: string, email: string, session: string} + * @return array{'$id': string, email: string, session: string} */ protected function createConsoleUser(): array { From aa9b45b5a196ee166221c128959c25a068b16804 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 15:25:45 +1200 Subject: [PATCH 33/80] fix(notifications): persist alerts to platform db regardless of dispatching project The `alerts` collection lives in the platform database, but the Notifications worker was injecting `dbForProject` and writing alerts there. Webhooks dispatches with the user's project context, so alerts were being written to the user's project DB (which has no `alerts` collection), and `/v1/account/alerts` (which reads from `dbForPlatform`) returned nothing. Switch the worker to inject `dbForPlatform` instead. All alert reads (dedup lookup) and writes (persistAlert + ConsoleAdapter) now target the platform DB, independent of which project the dispatching event belongs to. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Platform/Workers/Notifications.php | 34 +++++++++---------- .../Platform/Workers/NotificationsTest.php | 10 +++--- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 26a6ff3793..b7d02fc03f 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -57,12 +57,12 @@ class Notifications extends Action ->inject('message') ->inject('project') ->inject('register') - ->inject('dbForProject') + ->inject('dbForPlatform') ->inject('log') ->callback($this->action(...)); } - public function action(Message $message, Document $project, Registry $register, Database $dbForProject, Log $log): void + public function action(Message $message, Document $project, Registry $register, Database $dbForPlatform, Log $log): void { if (\class_exists(Runtime::class)) { Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); @@ -76,7 +76,7 @@ class Notifications extends Action $deduplicationKey = $payload['deduplicationKey'] ?? ''; $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : ''; - if ($messageId !== '' && $this->alreadyDelivered($dbForProject, $messageId)) { + if ($messageId !== '' && $this->alreadyDelivered($dbForPlatform, $messageId)) { $log->addTag('dedup', 'hit'); return; } @@ -89,9 +89,9 @@ class Notifications extends Action foreach ($recipients as $recipient) { $channel = $recipient['channel']; try { - $alertId = $this->dispatch($recipient, $messageId, $payload, $project, $register, $dbForProject, $log); + $alertId = $this->dispatch($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log); if ($messageId !== '' && $channel === NOTIFICATION_TYPE_WEBHOOK && $alertId === null) { - $this->persistAlert($dbForProject, $messageId, $recipient, $payload); + $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); } } catch (Throwable $error) { $log->addTag('channel', $channel); @@ -126,10 +126,10 @@ class Notifications extends Action * compound `$id`s (messageId + recipient hash), so a direct * `getDocument($messageId)` would always miss. Query the attribute. */ - private function alreadyDelivered(Database $database, string $messageId): bool + private function alreadyDelivered(Database $dbForPlatform, string $messageId): bool { try { - $matches = $database->find('alerts', [ + $matches = $dbForPlatform->find('alerts', [ Query::equal('messageId', [$messageId]), Query::limit(1), ]); @@ -154,14 +154,14 @@ class Notifications extends Action array $payload, Document $project, Registry $register, - Database $database, + Database $dbForPlatform, Log $log, ): ?string { $channel = $recipient['channel']; return match ($channel) { - NOTIFICATION_TYPE_EMAIL => $this->dispatchEmail($recipient, $messageId, $payload, $project, $register, $database, $log), - NOTIFICATION_TYPE_CONSOLE => $this->dispatchConsole($recipient, $messageId, $payload, $database), + NOTIFICATION_TYPE_EMAIL => $this->dispatchEmail($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log), + NOTIFICATION_TYPE_CONSOLE => $this->dispatchConsole($recipient, $messageId, $payload, $dbForPlatform), NOTIFICATION_TYPE_WEBHOOK => $this->dispatchWebhook($recipient, $payload, $log), default => throw new Exception('Unsupported notification channel: ' . $channel), }; @@ -176,7 +176,7 @@ class Notifications extends Action array $payload, Document $project, Registry $register, - Database $database, + Database $dbForPlatform, Log $log, ): ?string { $address = $recipient['address']; @@ -252,7 +252,7 @@ class Notifications extends Action // go out unsignals (we degrade gracefully). $alertId = null; if ($messageId !== '') { - $alertId = $this->persistAlert($database, $messageId, $recipient, $payload); + $alertId = $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); } // C3 tracking pixel: only injectable when we have a userId AND a @@ -332,7 +332,7 @@ class Notifications extends Action /** * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ - protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $database): ?string + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $dbForPlatform): ?string { $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; @@ -368,7 +368,7 @@ class Notifications extends Action projectId: $projectId, ); - $adapter = new ConsoleAdapter($database); + $adapter = new ConsoleAdapter($dbForPlatform); $result = $adapter->send($consoleMessage); // Greptile P1 #4: surface adapter failures. The Console adapter @@ -434,7 +434,7 @@ class Notifications extends Action * * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ - protected function persistAlert(Database $database, string $messageId, array $recipient, array $payload): string + protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload): string { $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; @@ -473,10 +473,10 @@ class Notifications extends Action ]); try { - $database->createDocument('alerts', $document); + $dbForPlatform->createDocument('alerts', $document); return $alertId; } catch (DuplicateException) { - $existing = $database->getDocument('alerts', $alertId); + $existing = $dbForPlatform->getDocument('alerts', $alertId); return $existing->isEmpty() ? $alertId : $existing->getId(); } } diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 3b77cc7c24..dfc07bf506 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -39,7 +39,7 @@ class SpyNotifications extends Notifications array $payload, Document $project, Registry $register, - Database $database, + Database $dbForPlatform, Log $log, ): ?string { $channel = $recipient['channel']; @@ -62,7 +62,7 @@ class SpyNotifications extends Notifications } if ($channel === NOTIFICATION_TYPE_CONSOLE || $channel === NOTIFICATION_TYPE_EMAIL) { - return $this->persistAlert($database, $messageId, $recipient, $payload); + return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); } return null; @@ -82,10 +82,10 @@ class CountingPersistAlertNotifications extends Notifications /** @var array */ public array $persistedIds = []; - protected function persistAlert(Database $database, string $messageId, array $recipient, array $payload): string + protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload): string { $this->persistAlertCalls++; - $alertId = parent::persistAlert($database, $messageId, $recipient, $payload); + $alertId = parent::persistAlert($dbForPlatform, $messageId, $recipient, $payload); $this->persistedIds[] = $alertId; return $alertId; } @@ -98,7 +98,7 @@ class CountingPersistAlertNotifications extends Notifications */ class ZeroDeliveryConsoleNotifications extends Notifications { - protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $database): ?string + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $dbForPlatform): ?string { // Simulate the Console adapter swallowing a per-recipient // exception and reporting zero deliveries. From 5f8cc27c9f7b120b6961b49a9912452c76a2b800 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 16:06:49 +1200 Subject: [PATCH 34/80] fix(notifications): defer persistence until after send, reset event state, distinguish duplicate writes, scope tracking jwt purpose - C1: dispatchEmail now writes the alert row only after a successful adapter send. The deterministic alertId is pre-computed via the new buildAlertId() helper so the tracking pixel URL stays stable, but the database write is deferred. Previously an SMTP failure left an orphan dedup row that permanently swallowed the email on retry. - C2: Notification::reset() now clears $preview alongside the other per-trigger fields so state cannot leak across triggers. - C3: Webhooks::sendAlert() resets the DI-shared notification queue at the top, preventing recipients/subject/body accumulation across multiple webhook failures in one worker invocation. - C4: Console adapter catches DuplicateException separately and treats it as a successful (already-delivered) result, so an idempotent retry no longer surfaces as a delivery failure when combined with dispatchConsole's zero-delivery throw. - M3: dispatchEmail throws plain 500 for any SMTP delivery failure; 401 mis-signalled auth errors for timeouts/DNS/connection refused. - M5: buildAlertId() is the single source of truth shared by persistAlert() and dispatchEmail(); no more drift between the tracking-pixel id and the persisted row's id. - M7: Tracking JWT now carries a purpose='alert_track' claim and the /v1/account/alerts/:alertId/track decoder rejects tokens missing or mismatching that purpose, isolating the shared signing key from cross-endpoint replay. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Event/Notification.php | 1 + .../Modules/Account/Http/Alerts/Track/Get.php | 3 +- .../Platform/Workers/Notifications.php | 61 +++++++++++++------ src/Appwrite/Platform/Workers/Webhooks.php | 5 ++ .../Utopia/Messaging/Adapter/Console.php | 7 +++ 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index acb4c8b62d..b7a31755ba 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -272,6 +272,7 @@ class Notification extends Event $this->name = ''; $this->subject = ''; $this->body = ''; + $this->preview = ''; $this->variables = []; $this->bodyTemplate = ''; $this->attachment = []; diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 7c5a48346c..acad23c7d4 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -71,7 +71,8 @@ class Get extends Action $decoded = $decoder->decode($jwt); if ( - isset($decoded['alertId'], $decoded['userId']) + isset($decoded['alertId'], $decoded['userId'], $decoded['purpose']) + && $decoded['purpose'] === 'alert_track' && $decoded['alertId'] === $alertId ) { $authorization->skip(function () use ($dbForPlatform, $alertId, $decoded) { diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index b7d02fc03f..d9ac17e592 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -247,20 +247,20 @@ class Notifications extends Action } $subject = \strip_tags($subjectTemplate->render()); - // Persist alert BEFORE adapter send so the alertId is available for - // the tracking pixel. Failure to persist still allows the email to - // go out unsignals (we degrade gracefully). - $alertId = null; - if ($messageId !== '') { - $alertId = $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); - } + // Pre-compute the alertId WITHOUT persisting so the tracking pixel + // URL is stable, but defer the database write until after a + // successful adapter send. Persisting first risks a dedup-by-attribute + // hit on retry if SMTP throws — the email would be permanently lost. + $deterministicAlertId = $messageId !== '' + ? self::buildAlertId($messageId, $recipient) + : null; - // C3 tracking pixel: only injectable when we have a userId AND a - // persisted alertId AND a signing key. + // Tracking pixel: only injectable when we have a userId AND a + // deterministic alertId AND a signing key. $userId = $recipient['userId'] ?? ''; $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); - if ($alertId !== null && $userId !== '' && !empty($opensslKey)) { - $body = $this->injectTrackingPixel($body, $alertId, $userId, $opensslKey); + if ($deterministicAlertId !== null && $userId !== '' && !empty($opensslKey)) { + $body = $this->injectTrackingPixel($body, $deterministicAlertId, $userId, $opensslKey); } /** @var EmailAdapter $adapter */ @@ -320,13 +320,21 @@ class Notifications extends Action try { $adapter->send($emailMessage); } catch (Throwable $error) { - if ($type === 'smtp') { - throw new Exception('Error sending notification: ' . $error->getMessage(), 401); - } + // Use 500 for any SMTP delivery failure: timeouts, DNS, refused + // connections, and authentication errors are all infrastructure + // problems, not HTTP-style 401s. The queue infrastructure handles + // retries based on the throw, not the code. throw new Exception('Error sending notification: ' . $error->getMessage(), 500); } - return $alertId; + // Persist the alert ONLY after a successful send. The Track endpoint + // tolerates a missing alert row, so a tracking pixel fetched between + // send and persistence is harmless. + if ($messageId !== '') { + return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); + } + + return null; } /** @@ -448,10 +456,10 @@ class Notifications extends Action // userId is supplied (matches Console adapter's own bookkeeping). if ($channel === NOTIFICATION_TYPE_CONSOLE && $userId === '' && $teamId === '') { $userId = $address; + $recipient['userId'] = $userId; } - $idSuffix = \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 8); - $alertId = $messageId . '_' . $idSuffix; + $alertId = self::buildAlertId($messageId, $recipient); $permissions = $this->buildAlertPermissions($userId, $teamId); if (empty($permissions)) { @@ -481,6 +489,24 @@ class Notifications extends Action } } + /** + * Build the deterministic alertId composed of the messageId plus an + * 8-char hash over the recipient identity. Single source of truth used + * by both `persistAlert()` and `dispatchEmail()` so the tracking pixel + * URL matches the eventually-persisted row exactly. + * + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + private static function buildAlertId(string $messageId, array $recipient): string + { + $channel = $recipient['channel']; + $address = $recipient['address']; + $userId = $recipient['userId'] ?? ''; + $teamId = $recipient['teamId'] ?? ''; + + return $messageId . '_' . \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 8); + } + /** * @return array */ @@ -542,6 +568,7 @@ class Notifications extends Action ->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'purpose' => 'alert_track', ]); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index c5abf4f755..91485c2906 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -209,6 +209,11 @@ class Webhooks extends Action */ public function sendAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, Notification $queueForNotifications, array $plan): void { + // The DI-shared Notification event accumulates state across calls. Reset + // before configuring this alert so multiple webhook failures in a single + // worker invocation do not bleed recipients/subject/body between alerts. + $queueForNotifications->reset(); + $memberships = $dbForPlatform->find('memberships', [ Query::equal('teamInternalId', [$project->getAttribute('teamInternalId')]), Query::limit(APP_LIMIT_SUBQUERY) diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index 28765e3242..bb705e59e4 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -5,6 +5,7 @@ namespace Appwrite\Utopia\Messaging\Adapter; use Appwrite\Utopia\Messaging\Messages\Console as ConsoleMessage; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -84,6 +85,12 @@ class Console extends Adapter $this->database->createDocument('alerts', $document); $delivered++; $response->addResult($key); + } catch (DuplicateException) { + // Idempotent retry: row already exists for this messageId/recipient. + // Treat as a successful (already-delivered) result so the worker + // does not throw and re-queue. + $delivered++; + $response->addResult($key); } catch (\Throwable $error) { $response->addResult($key, $error->getMessage()); } From 1aa6a05ad88b833573b0620bb9fb7c0e8d64d0c6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 16:13:52 +1200 Subject: [PATCH 35/80] fix(notifications): drop misleading auth metadata, exercise unique-index dedup, fail loudly on missing tracking secret in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Account/Alerts/Track endpoint is scope:public (email clients have no session); SDK Method now declares auth: [] so generators do not require an auth header for an unauthenticated endpoint. - NotificationsTest setUp now creates the production `_key_recipient` UNIQUE composite index on (messageId, channel, userId, teamId), and testPersistAlertReturnsExistingAlertIdOnDuplicate exercises the DuplicateException → return-existing-alertId branch end-to-end (both primary-key collision and unique-index collision via a sibling $id). - Tracking-pixel e2e now asserts _APP_OPENSSL_KEY_V1 is set instead of silently falling back to the .env.example placeholder, so a missing CI secret fails loudly rather than passing against the wrong key. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Modules/Account/Http/Alerts/Track/Get.php | 3 +- .../Notifications/NotificationsBase.php | 3 +- .../Platform/Workers/NotificationsTest.php | 103 ++++++++++++++++++ 3 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 7c5a48346c..8690b5e8a7 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -3,7 +3,6 @@ namespace Appwrite\Platform\Modules\Account\Http\Alerts\Track; use Ahc\Jwt\JWT; -use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; @@ -39,7 +38,7 @@ class Get extends Action group: 'alerts', name: 'getAlertTrack', description: '/docs/references/account/get-alert-track.md', - auth: [AuthType::SESSION, AuthType::JWT], + auth: [], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 81f7cf7ebb..19a64411ac 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -190,7 +190,8 @@ trait NotificationsBase $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); - $secret = System::getEnv('_APP_OPENSSL_KEY_V1') ?: 'your-secret-key'; + $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); + $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for tracking pixel test'); $userId = $this->getRoot()['$id']; $jwt = (new JWT($secret, 'HS256', 2592000, 0))->encode([ diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index dfc07bf506..c0aca4485f 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -172,6 +172,18 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); + // Mirror the production `_key_recipient` UNIQUE composite index so the + // duplicate-handling branch in persistAlert (catch DuplicateException → + // return existing alertId) is actually exercised by tests. + $this->database->createIndex( + 'alerts', + '_key_recipient', + Database::INDEX_UNIQUE, + ['messageId', 'channel', 'userId', 'teamId'], + [Database::LENGTH_KEY, 64, Database::LENGTH_KEY, Database::LENGTH_KEY], + [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], + ); + $this->registry = new Registry(); $this->project = new Document(['$id' => 'project-x']); $this->log = new Log(); @@ -554,6 +566,97 @@ class NotificationsTest extends TestCase $this->assertLessThan($lastBodyClose, $pixelPosition, 'pixel must be spliced before the final '); } + public function testPersistAlertReturnsExistingAlertIdOnDuplicate(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + try { + $worker = new CountingPersistAlertNotifications(); + + // All four unique-index fields populated so the + // `_key_recipient` UNIQUE composite (messageId, channel, + // userId, teamId) actually fires — SQL UNIQUE semantics treat + // NULL as not-equal, so any null in the tuple disables it. + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-7', + 'teamId' => 'team-7', + ], + ], + 'subject' => 'Heads up', + 'body' => 'b', + 'deduplicationKey' => 'persist-dup', + ]; + + // First dispatch: writes a row through the action loop and + // returns the deterministic alertId. + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertSame(1, $worker->persistAlertCalls); + $firstAlertId = $worker->persistedIds[0]; + + $messageId = \md5('persist-dup'); + $recipient = [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-7', + 'teamId' => 'team-7', + ]; + + // Second invocation with the SAME messageId/recipient. The + // action loop's alreadyDelivered() check short-circuits before + // persistAlert, so call persistAlert directly to actually hit + // the duplicate branch. The deterministic $id collides on the + // primary key → DuplicateException → branch returns the + // existing alertId without throwing. + $reflection = new \ReflectionMethod($worker, 'persistAlert'); + $secondAlertId = $reflection->invoke($worker, $this->database, $messageId, $recipient, $payload); + + $this->assertSame($firstAlertId, $secondAlertId, 'duplicate persist must return the existing alertId'); + + // Third write: bypass the deterministic $id path and use a + // distinct $id with the same recipient tuple. The + // `_key_recipient` UNIQUE composite must reject it — proving + // the unique-index (not just primary-key) is what backstops the + // duplicate-handling branch. + $sameTupleDoc = new Document([ + '$id' => 'sibling-id-' . \uniqid(), + '$permissions' => [Permission::read(Role::any())], + 'messageId' => $messageId, + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-7', + 'teamId' => 'team-7', + 'projectId' => 'project-x', + 'title' => 'sibling', + 'body' => 'sibling', + 'read' => false, + ]); + + $threw = false; + try { + $this->database->createDocument('alerts', $sameTupleDoc); + } catch (\Utopia\Database\Exception\Duplicate) { + $threw = true; + } + $this->assertTrue($threw, 'unique-index `_key_recipient` must reject a second row sharing the recipient tuple'); + + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(1, $rows, 'unique-index must prevent a second row from being persisted'); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + } + public function testPersistAlertReturnsAlertIdAndStoresUserId(): void { $spy = new SpyEmailAdapter(); From ac87c0e2d61b38763c888f89d583153e23d05a77 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 18:02:12 +1200 Subject: [PATCH 36/80] test(notifications): add regression and happy-path coverage for review-fix critical gaps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Locks the bug-fix invariants from PR #12195's last review pass and rounds out worker channel coverage: C1 testEmailSendFailureDoesNotPersistAlert — SMTP throw must NOT leave a dedup row behind, retry must deliver and persist exactly once. C2 testNotificationEventResetClearsAllState — reset() drops every state-bearing field including preview (regression: missed in original reset body). C3 testWebhookSendAlertResetsBetweenCalls — Webhooks::sendAlert must reset() the DI-shared Notification event so two paused-webhook alerts in one worker pass do not bleed recipients/subject/body into each other. C4 testConsoleAdapterTreatsDuplicateAsDelivered — Duplicate on createDocument must surface as a successful idempotent send, not a per-recipient error. M7 testTrackingPixelRejectsJwtWithoutPurposeClaim — Track endpoint silently ignores JWTs missing or with the wrong purpose claim (defends against replaying session/reset JWTs to mark alerts read). Worker happy-path tests: testEmailChannelHappyPath, testConsoleChannelHappyPath, testWebhookChannelHappyPath cover the full per-channel dispatch contract end to end, including HMAC signing for webhooks and the tracking pixel injection + post-send persistence for email. Also extracts CapturingWebhook into its own PSR-4 file so reused across tests. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Notifications/NotificationsBase.php | 80 ++++ .../Platform/Workers/NotificationsTest.php | 441 ++++++++++++++++++ .../Messaging/Adapter/CapturingWebhook.php | 34 ++ .../Utopia/Messaging/Adapter/ConsoleTest.php | 50 ++ .../Utopia/Messaging/Adapter/WebhookTest.php | 29 -- 5 files changed, 605 insertions(+), 29 deletions(-) create mode 100644 tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 19a64411ac..5423e4de3d 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -194,9 +194,13 @@ trait NotificationsBase $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for tracking pixel test'); $userId = $this->getRoot()['$id']; + // Track endpoint requires `purpose: 'alert_track'` — see C/M7 in + // PR #12195 review. Other claim purposes are silently ignored (which + // testTrackingPixelRejectsJwtWithoutPurposeClaim covers). $jwt = (new JWT($secret, 'HS256', 2592000, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'purpose' => 'alert_track', ]); $response = $this->client->call( @@ -229,6 +233,82 @@ trait NotificationsBase self::$seededAlertId = null; } + /** + * Reviewer M7: a tracking JWT without a `purpose: 'alert_track'` claim + * must be silently rejected. Without the purpose check, any JWT minted + * with the same secret (sessions, password reset, etc.) could be + * replayed against this endpoint to mark arbitrary alerts as read. + * + * The endpoint always returns the 1x1 PNG (200 image/png) — the only + * observable difference is whether the alert flips to `read: true`. + */ + public function testTrackingPixelRejectsJwtWithoutPurposeClaim(): void + { + $alertId = $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); + $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for the JWT purpose-claim test'); + $userId = $this->getRoot()['$id']; + + // Mint a JWT with valid alertId/userId but NO purpose claim. + $jwtNoPurpose = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + 'alertId' => $alertId, + 'userId' => $userId, + ]); + + $response = $this->client->call( + Client::METHOD_GET, + '/account/alerts/' . $alertId . '/track', + ['x-appwrite-project' => 'console'], + ['jwt' => $jwtNoPurpose] + ); + + $this->assertSame(200, $response['headers']['status-code'], 'endpoint must always return 200'); + $this->assertStringContainsString('image/png', $response['headers']['content-type']); + + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + $this->assertNotNull($found); + $this->assertFalse($found['read'], 'JWT without purpose claim must not flip the read flag'); + + // Mint a JWT with a wrong purpose value — same expectation: silently rejected. + $jwtWrongPurpose = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + 'alertId' => $alertId, + 'userId' => $userId, + 'purpose' => 'something_else', + ]); + + $response = $this->client->call( + Client::METHOD_GET, + '/account/alerts/' . $alertId . '/track', + ['x-appwrite-project' => 'console'], + ['jwt' => $jwtWrongPurpose] + ); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertStringContainsString('image/png', $response['headers']['content-type']); + + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $found = null; + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + $found = $alert; + break; + } + } + $this->assertNotNull($found); + $this->assertFalse($found['read'], 'JWT with wrong purpose value must not flip the read flag'); + + self::$seededAlertId = $alertId; + } + public function testTrackingPixelInvalidTokenReturnsPng(): void { $alertId = $this->seedWebhookFailureAlert(); diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index c0aca4485f..f5cc60c04c 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -2,8 +2,10 @@ namespace Tests\Unit\Platform\Workers; +use Appwrite\Event\Notification; use Appwrite\Platform\Workers\Notifications; use PHPUnit\Framework\TestCase; +use Tests\Unit\Event\MockPublisher; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\Memory; @@ -109,10 +111,16 @@ class ZeroDeliveryConsoleNotifications extends Notifications /** * Captures the EmailMessage handed to the SMTP adapter so tests can assert * on the rendered HTML body without touching a real mail server. + * + * Set `$throwOnSend = true` to simulate a hard SMTP failure (DNS, refused + * connection, auth error). The adapter's `send()` calls `process()` directly, + * so a throw from here propagates exactly like a real PHPMailer error. */ class SpyEmailAdapter extends EmailAdapter { public ?EmailMessage $captured = null; + public int $sendCount = 0; + public bool $throwOnSend = false; public function getName(): string { @@ -126,7 +134,13 @@ class SpyEmailAdapter extends EmailAdapter protected function process(EmailMessage $message): array { + $this->sendCount++; $this->captured = $message; + + if ($this->throwOnSend) { + throw new \Exception('SMTP unavailable'); + } + return [ 'deliveredTo' => 1, 'type' => $this->getType(), @@ -697,4 +711,431 @@ class NotificationsTest extends TestCase $this->assertFalse($row->getAttribute('read'), 'new alerts default to unread'); $this->assertSame(\md5('persist-email'), $row->getAttribute('messageId')); } + + /** + * Reviewer C1: SMTP failure must NOT orphan a dedup row. + * + * Order in the worker matters: persist BEFORE send leaves a poisoned + * `messageId` row that the next retry will dedup-hit and never deliver. + * The fix is to persist only after a successful adapter send. A retry + * with the same payload must therefore actually deliver and produce + * exactly one alert row. + */ + public function testEmailSendFailureDoesNotPersistAlert(): void + { + $failing = new SpyEmailAdapter(); + $failing->throwOnSend = true; + $this->registry->set('smtp', static fn () => $failing); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-9', + ], + ], + 'subject' => 'Subj', + 'body' => 'Body', + 'deduplicationKey' => 'smtp-fail-key', + ]; + + $messageId = \md5('smtp-fail-key'); + + try { + $worker = new Notifications(); + + $threw = false; + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } catch (\Throwable $error) { + $threw = true; + $this->assertStringContainsString('SMTP unavailable', $error->getMessage()); + } + $this->assertTrue($threw, 'SMTP failure must propagate so the queue retries'); + $this->assertSame(1, $failing->sendCount, 'adapter must have been invoked exactly once'); + + // Critical: no orphan dedup row. If there is one, the retry below + // will short-circuit and the user never gets the email. + $orphans = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(0, $orphans, 'failed SMTP send must not leave a dedup row behind'); + + // Retry with a working adapter using the same payload — must deliver + // AND persist exactly one alert row. + $working = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $working); + + $retryWorker = new Notifications(); + $retryWorker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertSame(1, $working->sendCount, 'retry must invoke the working adapter'); + + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(1, $rows, 'retry must persist exactly one alert row'); + $this->assertSame('user-9', $rows[0]->getAttribute('userId')); + $this->assertFalse($rows[0]->getAttribute('read')); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + } + + /** + * Reviewer C2: `Notification::reset()` must clear EVERY state-bearing + * field — `$preview` was missing from the original reset() body and + * leaked across DI-shared event reuses (alarming when a webhook-paused + * preview line bleeds into an unrelated alert). + */ + public function testNotificationEventResetClearsAllState(): void + { + $event = new Notification(new MockPublisher()); + + $event + ->setProject(new Document(['$id' => 'project-x'])) + ->setRecipient('legacy@example.test') + ->setName('Some Name') + ->setSubject('Subject Line') + ->setBody('Body content') + ->setPreview('Preview snippet') + ->setVariables(['key' => 'value']) + ->setBodyTemplate('/tmp/template.tpl') + ->setAttachment('content', 'file.txt') + ->setRecipients([ + ['address' => 'a@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL], + ]) + ->setChannels([NOTIFICATION_TYPE_EMAIL]) + ->setTemplate('template-id') + ->setTemplateParams(['x' => 1]) + ->setDeduplicationKey('dedup') + ->setPermissions([Permission::read(Role::any())]); + + $event->reset(); + + $this->assertSame('', $event->getRecipient(), 'recipient must reset to empty string'); + $this->assertSame('', $event->getName(), 'name must reset to empty string'); + $this->assertSame('', $event->getSubject(), 'subject must reset to empty string'); + $this->assertSame('', $event->getBody(), 'body must reset to empty string'); + $this->assertSame('', $event->getPreview(), 'preview must reset to empty string (C2 regression)'); + $this->assertSame([], $event->getVariables(), 'variables must reset to empty array'); + $this->assertSame('', $event->getBodyTemplate(), 'bodyTemplate must reset to empty string'); + $this->assertSame([], $event->getAttachment(), 'attachment must reset to empty array'); + $this->assertSame([], $event->getRecipients(), 'recipients must reset to empty array'); + $this->assertSame([], $event->getChannels(), 'channels must reset to empty array'); + $this->assertSame('', $event->getTemplate(), 'template must reset to empty string'); + $this->assertSame([], $event->getTemplateParams(), 'templateParams must reset to empty array'); + $this->assertSame('', $event->getDeduplicationKey(), 'deduplicationKey must reset to empty string'); + $this->assertSame([], $event->getPermissions(), 'permissions must reset to empty array'); + $this->assertNull($event->getProject(), 'project must reset to null'); + } + + /** + * Reviewer C3: `Webhooks::sendAlert` resets the DI-shared Notification + * event before configuring the alert, so a second invocation in the same + * worker pass cannot bleed recipients, subject, body, preview, or dedup + * key from the first. + * + * We exercise this at the event level (rather than standing up the full + * Webhooks worker fixture, which needs `dbForPlatform.memberships` and + * full project shape) by replicating sendAlert's add → trigger → reset → + * add → trigger flow on a single Notification instance and asserting + * each enqueued payload is fully isolated. + */ + public function testWebhookSendAlertResetsBetweenCalls(): void + { + $publisher = new MockPublisher(); + $event = new Notification($publisher); + + // First "sendAlert" pass: a single user, webhook A. + $event + ->setProject(new Document(['$id' => 'project-a'])) + ->setSubject('Webhook A paused') + ->setPreview('Webhook A preview') + ->setBody('Webhook A body') + ->setDeduplicationKey('webhook:hookA:paused:10') + ->addRecipient('alice@example.test', NOTIFICATION_TYPE_EMAIL, null, 'user-A', 'team-1') + ->addRecipient('user-A', NOTIFICATION_TYPE_CONSOLE, null, 'user-A', 'team-1') + ->trigger(); + + // Mirror the production sendAlert: reset before configuring next alert. + $event->reset(); + + // Second pass: a different user/webhook entirely. + $event + ->setProject(new Document(['$id' => 'project-b'])) + ->setSubject('Webhook B paused') + ->setPreview('Webhook B preview') + ->setBody('Webhook B body') + ->setDeduplicationKey('webhook:hookB:paused:10') + ->addRecipient('bob@example.test', NOTIFICATION_TYPE_EMAIL, null, 'user-B', 'team-2') + ->addRecipient('user-B', NOTIFICATION_TYPE_CONSOLE, null, 'user-B', 'team-2') + ->trigger(); + + $events = $publisher->getEvents('v1-notifications'); + $this->assertNotNull($events, 'two trigger() calls must enqueue at least one batch'); + $this->assertCount(2, $events, 'each sendAlert pass must produce its own enqueued event'); + + $first = $events[0]; + $second = $events[1]; + + $this->assertSame('Webhook A paused', $first['subject']); + $this->assertSame('Webhook A preview', $first['preview']); + $this->assertSame('Webhook A body', $first['body']); + $this->assertSame('webhook:hookA:paused:10', $first['deduplicationKey']); + $this->assertCount(2, $first['recipients']); + + $this->assertSame('Webhook B paused', $second['subject']); + $this->assertSame('Webhook B preview', $second['preview']); + $this->assertSame('Webhook B body', $second['body']); + $this->assertSame('webhook:hookB:paused:10', $second['deduplicationKey']); + $this->assertCount(2, $second['recipients'], 'second event must hold ONLY its own recipients'); + + $secondAddresses = \array_map(static fn ($r) => $r['address'], $second['recipients']); + $this->assertNotContains('alice@example.test', $secondAddresses, 'reset() must drop prior email recipient'); + $this->assertNotContains('user-A', $secondAddresses, 'reset() must drop prior console recipient'); + $this->assertContains('bob@example.test', $secondAddresses); + $this->assertContains('user-B', $secondAddresses); + } + + /** + * Worker happy-path: email channel. + * + * Asserts the SMTP adapter is invoked once with the expected + * to/subject/body, the rendered body carries the tracking pixel before + * ``, and an alert row is persisted AFTER the send returns + * successfully (see C1: persist-after-send invariant). + */ + public function testEmailChannelHappyPath(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + \putenv('_APP_OPENSSL_KEY_V1=test-key-32bytes-min-aaaaaaaaaaaaaa'); + + try { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'happy@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-happy', + ], + ], + 'subject' => 'Welcome aboard', + 'body' => 'plain body', + 'deduplicationKey' => 'happy-email', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + \putenv($previousOpensslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpensslKey); + } + + $this->assertSame(1, $spy->sendCount, 'SMTP send must be invoked exactly once'); + $this->assertNotNull($spy->captured); + + $message = $spy->captured; + $this->assertSame('happy@example.test', $message->getTo()[0]['email'] ?? ''); + $this->assertSame('Welcome aboard', $message->getSubject()); + + $body = $message->getContent(); + $this->assertStringContainsString('assertStringContainsString('/v1/account/alerts/', $body); + $this->assertStringContainsString('/track?jwt=', $body); + + $closing = \strripos($body, ''); + $pixel = \strripos($body, ''); + + $this->assertSame(1, $worker->persistAlertCalls, 'email channel must persist exactly once after a successful send'); + $messageId = \md5('happy-email'); + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(1, $rows); + $row = $rows[0]; + $this->assertSame('user-happy', $row->getAttribute('userId')); + $this->assertSame(NOTIFICATION_TYPE_EMAIL, $row->getAttribute('channel')); + $this->assertFalse($row->getAttribute('read')); + + // dispatchEmail's returned alertId must match the row $id (used by the + // tracking pixel URL). + $this->assertSame($worker->persistedIds[0], $row->getId()); + } + + /** + * Worker happy-path: console channel. + * + * The Console adapter writes the alert directly; the action loop must + * NOT call `persistAlert` for console recipients. Permissions must grant + * the recipient user AND team owners read/update/delete. + */ + public function testConsoleChannelHappyPath(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'console-recipient', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'userId' => 'u1', + 'teamId' => 't1', + ], + ], + 'subject' => 'Heads up', + 'body' => 'console body', + 'deduplicationKey' => 'happy-console', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('channel', ['console']), + ]); + $this->assertCount(1, $rows, 'console adapter must write exactly one alert'); + + $row = $rows[0]; + $this->assertSame('u1', $row->getAttribute('userId')); + $this->assertSame('t1', $row->getAttribute('teamId')); + $this->assertSame(NOTIFICATION_TYPE_CONSOLE, $row->getAttribute('channel')); + $this->assertFalse($row->getAttribute('read')); + + // Per-recipient suffix scheme used by the Console adapter is + // `messageId . '_' . substr(md5('user:' . userId), 0, 8)`. + $messageId = \md5('happy-console'); + $expectedId = $messageId . '_' . \substr(\md5('user:u1'), 0, 8); + $this->assertSame($expectedId, $row->getId(), 'row $id must match adapter suffix scheme'); + + $permissions = $row->getPermissions(); + $this->assertContains(Permission::read(Role::user('u1')), $permissions); + $this->assertContains(Permission::update(Role::user('u1')), $permissions); + $this->assertContains(Permission::delete(Role::user('u1')), $permissions); + $this->assertContains(Permission::read(Role::team('t1')), $permissions); + $this->assertContains(Permission::update(Role::team('t1', 'owner')), $permissions); + $this->assertContains(Permission::delete(Role::team('t1', 'owner')), $permissions); + + $this->assertSame(0, $worker->persistAlertCalls, 'console channel must NOT trigger action-loop persistAlert'); + } + + /** + * Worker happy-path: webhook channel. + * + * The webhook adapter receives a POST with the rendered subject/body, + * an `X-Appwrite-Webhook-Signature` header derived from the per-recipient + * `signatureKey`, and a single alert row is persisted by the action loop + * AFTER the send returns successfully (webhook adapters do not persist + * themselves). + * + * We swap in a worker subclass that uses the in-process `CapturingWebhook` + * adapter so we exercise the real signing path without touching the network. + */ + public function testWebhookChannelHappyPath(): void + { + $captured = []; + $worker = new class ($captured) extends CountingPersistAlertNotifications { + /** @param array> $captured */ + public function __construct(public array &$captured) + { + parent::__construct(); + } + + protected function dispatchWebhook(array $recipient, array $payload, Log $log): ?string + { + $adapter = new \Tests\Unit\Utopia\Messaging\Adapter\CapturingWebhook(); + $message = new \Appwrite\Utopia\Messaging\Messages\Webhook( + urls: [$recipient['address']], + payload: [ + 'subject' => $payload['subject'] ?? '', + 'body' => $payload['body'] ?? '', + 'template' => $payload['template'] ?? '', + 'params' => $payload['templateParams'] ?? [], + 'project' => \is_array($payload['project'] ?? null) ? ($payload['project']['$id'] ?? null) : null, + 'deduplicationKey' => $payload['deduplicationKey'] ?? '', + 'events' => $payload['events'] ?? [], + ], + signingSecret: $recipient['signatureKey'] ?? null, + ); + + $result = $adapter->send($message); + $this->captured = $adapter->captured; + + if (($result['deliveredTo'] ?? 0) === 0) { + throw new \Exception('Webhook delivery failed'); + } + return null; + } + }; + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'https://hooks.example.test/in', + 'channel' => NOTIFICATION_TYPE_WEBHOOK, + 'signatureKey' => 'tenant-secret', + 'userId' => 'user-h', + ], + ], + 'subject' => 'Heads up', + 'body' => 'webhook body', + 'deduplicationKey' => 'happy-webhook', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(1, $worker->captured, 'adapter must POST exactly once'); + $request = $worker->captured[0]; + + $this->assertSame('POST', $request['method']); + $this->assertSame('https://hooks.example.test/in', $request['url']); + + $sent = \json_decode($request['body'], true); + $this->assertSame('Heads up', $sent['subject']); + $this->assertSame('webhook body', $sent['body']); + + $headerLine = \implode("\n", $request['headers']); + $this->assertStringContainsString('Content-Type: application/json', $headerLine); + $this->assertStringContainsString('X-Appwrite-Webhook-Signature: sha256=', $headerLine); + + // Verify the HMAC matches the signing secret on the recipient struct. + $signature = null; + $timestamp = null; + foreach ($request['headers'] as $header) { + if (\str_starts_with($header, 'X-Appwrite-Webhook-Signature: ')) { + $signature = \substr($header, \strlen('X-Appwrite-Webhook-Signature: ')); + } elseif (\str_starts_with($header, 'X-Appwrite-Webhook-Timestamp: ')) { + $timestamp = \substr($header, \strlen('X-Appwrite-Webhook-Timestamp: ')); + } + } + $this->assertNotNull($signature); + $this->assertNotNull($timestamp); + $expected = 'sha256=' . \hash_hmac('sha256', $timestamp . '.' . $request['body'], 'tenant-secret'); + $this->assertSame($expected, $signature); + + // Action loop must persist exactly one webhook alert row AFTER the send. + $this->assertSame(1, $worker->persistAlertCalls); + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [\md5('happy-webhook')]), + ]); + $this->assertCount(1, $rows); + $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $rows[0]->getAttribute('channel')); + $this->assertFalse($rows[0]->getAttribute('read')); + } } diff --git a/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php b/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php new file mode 100644 index 0000000000..093a3cffb7 --- /dev/null +++ b/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php @@ -0,0 +1,34 @@ +, body: string, timeout: int}> + */ + public array $captured = []; + + /** @var array{statusCode: int, response: string|null, error: string|null} */ + public array $response = ['statusCode' => 200, 'response' => 'OK', 'error' => null]; + + protected function dispatch(string $method, string $url, array $headers, string $body, int $timeout): array + { + $this->captured[] = [ + 'method' => $method, + 'url' => $url, + 'headers' => $headers, + 'body' => $body, + 'timeout' => $timeout, + ]; + return $this->response; + } +} diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index d1165fa002..5ed51d8db1 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -169,4 +169,54 @@ class ConsoleTest extends TestCase // ConsoleMessage extends nothing — pass an unrelated Message implementation $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); } + + /** + * Reviewer C4: a `DuplicateException` thrown by `createDocument` must be + * treated as a SUCCESSFUL delivery, not a failure. The adapter previously + * lumped Duplicate into the generic Throwable catch, which surfaced as a + * per-recipient `error` and caused the worker to throw — re-queueing the + * notification and never marking the duplicate as delivered. + */ + public function testConsoleAdapterTreatsDuplicateAsDelivered(): void + { + $userId = 'user-dup'; + $messageId = 'msg-dup'; + $documentId = $this->alertId($messageId, userId: $userId); + + // Pre-insert an alert with the SAME id the adapter will compute. The + // adapter's createDocument will hit the primary-key DuplicateException + // and must treat it as a successful (idempotent) send. + $this->database->createDocument('alerts', new \Utopia\Database\Document([ + '$id' => $documentId, + '$permissions' => [Permission::read(Role::any())], + 'messageId' => $messageId, + 'channel' => 'console', + 'userId' => $userId, + 'title' => 'pre-existing', + 'body' => 'pre-existing', + ])); + + $message = new ConsoleMessage( + recipients: [['userId' => $userId]], + title: 'Same alert resent', + body: 'b', + messageId: ID::custom($messageId), + projectId: 'project-x', + ); + + $adapter = new Console($this->database); + $result = $adapter->send($message); + + $this->assertSame(1, $result['deliveredTo'], 'duplicate must count as a successful delivery'); + $this->assertCount(1, $result['results']); + $this->assertSame('success', $result['results'][0]['status'] ?? '', 'duplicate must report success status'); + $this->assertSame('', $result['results'][0]['error'] ?? 'unset', 'duplicate must not surface a per-recipient error'); + + // Still exactly ONE row — the pre-existing one. The adapter must not + // overwrite it nor create a sibling. + $rows = $this->database->find('alerts'); + $this->assertCount(1, $rows); + $this->assertSame($documentId, $rows[0]->getId()); + $this->assertSame('pre-existing', $rows[0]->getAttribute('title'), 'duplicate path must not overwrite existing row'); + } } diff --git a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php index 5ad1a8c348..f396954949 100644 --- a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php @@ -2,38 +2,9 @@ namespace Tests\Unit\Utopia\Messaging\Adapter; -use Appwrite\Utopia\Messaging\Adapter\Webhook; use Appwrite\Utopia\Messaging\Messages\Webhook as WebhookMessage; use PHPUnit\Framework\TestCase; -/** - * Test double that captures the curl request the adapter would issue and - * returns a scripted response, so we exercise the real signing/header logic - * without touching the network. - */ -class CapturingWebhook extends Webhook -{ - /** - * @var array, body: string, timeout: int}> - */ - public array $captured = []; - - /** @var array{statusCode: int, response: string|null, error: string|null} */ - public array $response = ['statusCode' => 200, 'response' => 'OK', 'error' => null]; - - protected function dispatch(string $method, string $url, array $headers, string $body, int $timeout): array - { - $this->captured[] = [ - 'method' => $method, - 'url' => $url, - 'headers' => $headers, - 'body' => $body, - 'timeout' => $timeout, - ]; - return $this->response; - } -} - class WebhookTest extends TestCase { public function testPostsExpectedBodyShape(): void From 1f1a02f026169a98563fa97bae416b6313cab691 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 6 May 2026 18:29:32 +1200 Subject: [PATCH 37/80] fix(ci): add missing alert endpoint docs and update compose test for notifications worker service Co-Authored-By: Claude Opus 4.7 (1M context) --- docs/references/account/get-alert-track.md | 1 + docs/references/account/list-alerts.md | 1 + docs/references/account/update-alert-read.md | 1 + tests/unit/Docker/ComposeTest.php | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 docs/references/account/get-alert-track.md create mode 100644 docs/references/account/list-alerts.md create mode 100644 docs/references/account/update-alert-read.md diff --git a/docs/references/account/get-alert-track.md b/docs/references/account/get-alert-track.md new file mode 100644 index 0000000000..8b313fe5c4 --- /dev/null +++ b/docs/references/account/get-alert-track.md @@ -0,0 +1 @@ +Track an alert as opened by returning a 1x1 transparent tracking pixel. When loaded with a valid tracking token, the alert is automatically marked as read. diff --git a/docs/references/account/list-alerts.md b/docs/references/account/list-alerts.md new file mode 100644 index 0000000000..5b7c063bec --- /dev/null +++ b/docs/references/account/list-alerts.md @@ -0,0 +1 @@ +Get the list of alerts for the currently logged in user. Use queries to filter the results by attributes such as read status or creation date. diff --git a/docs/references/account/update-alert-read.md b/docs/references/account/update-alert-read.md new file mode 100644 index 0000000000..782b834326 --- /dev/null +++ b/docs/references/account/update-alert-read.md @@ -0,0 +1 @@ +Mark an alert as read for the currently logged in user. diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index e63d877baf..570904f7c3 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -23,7 +23,7 @@ class ComposeTest extends TestCase public function testServices(): void { - $this->assertCount(16, $this->object->getServices()); + $this->assertCount(17, $this->object->getServices()); $this->assertEquals('appwrite', $this->object->getService('appwrite')->getContainerName()); $this->assertEquals('', $this->object->getService('appwrite')->getImageVersion()); $this->assertEquals('3.6', $this->object->getService('traefik')->getImageVersion()); From 70df121e6fa3624eb0694176e268715dd3dc4be4 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 19:37:21 +1200 Subject: [PATCH 38/80] fix: address notifications review comments --- app/config/collections/platform.php | 45 +++- app/init/constants.php | 8 +- src/Appwrite/Event/Notification.php | 8 +- .../Modules/Account/Http/Alerts/Track/Get.php | 2 +- .../Platform/Workers/Notifications.php | 166 ++++++++------- src/Appwrite/Platform/Workers/Webhooks.php | 22 +- .../Utopia/Messaging/Adapter/Console.php | 8 +- .../Utopia/Messaging/Messages/Console.php | 6 +- .../Notifications/NotificationsBase.php | 12 +- .../Platform/Workers/NotificationsTest.php | 199 ++++++++++++++---- tests/unit/Platform/Workers/WebhooksTest.php | 36 ++++ .../Utopia/Messaging/Adapter/ConsoleTest.php | 15 +- 12 files changed, 382 insertions(+), 145 deletions(-) create mode 100644 tests/unit/Platform/Workers/WebhooksTest.php diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index c9f03e180b..d1a5a9b518 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1048,6 +1048,17 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('recipientHash'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('type'), 'type' => Database::VAR_STRING, @@ -1148,9 +1159,9 @@ $platformCollections = [ [ '$id' => ID::custom('_key_recipient'), 'type' => Database::INDEX_UNIQUE, - 'attributes' => ['messageId', 'channel', 'userId', 'teamId'], - 'lengths' => [Database::LENGTH_KEY, 64, Database::LENGTH_KEY, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['messageId', 'channel', 'recipientHash'], + 'lengths' => [Database::LENGTH_KEY, 64, 64], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_userId_read'), @@ -1173,6 +1184,34 @@ $platformCollections = [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_userId_type'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId', 'type'], + 'lengths' => [Database::LENGTH_KEY, 64], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_userId_channel'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId', 'channel'], + 'lengths' => [Database::LENGTH_KEY, 64], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_userId_messageId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId', 'messageId'], + 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_userId_projectId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId', 'projectId'], + 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], ], ], diff --git a/app/init/constants.php b/app/init/constants.php index 87c897ecb4..a8696a8fbf 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -263,12 +263,12 @@ const MESSAGE_TYPE_EMAIL = 'email'; const MESSAGE_TYPE_SMS = 'sms'; const MESSAGE_TYPE_PUSH = 'push'; // Notification types -const NOTIFICATION_TYPE_EMAIL = 'email'; -const NOTIFICATION_TYPE_SMS = 'sms'; -const NOTIFICATION_TYPE_PUSH = 'push'; +const NOTIFICATION_TYPE_EMAIL = MESSAGE_TYPE_EMAIL; +const NOTIFICATION_TYPE_SMS = MESSAGE_TYPE_SMS; +const NOTIFICATION_TYPE_PUSH = MESSAGE_TYPE_PUSH; const NOTIFICATION_TYPE_CONSOLE = 'console'; const NOTIFICATION_TYPE_WEBHOOK = 'webhook'; -const RESOURCE_TYPE_ALERTS = 'alerts'; +const ALERT_TRACKING_JWT_TTL = 60 * 60 * 24 * 7; // API key types const API_KEY_STANDARD = 'standard'; const API_KEY_EPHEMERAL = 'ephemeral'; diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index b7a31755ba..3c2c9d6eaa 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -20,7 +20,7 @@ class Notification extends Event /** * Recipients to deliver the notification to. * - * Each entry has an `address` (channel-specific identifier — email, + * Each entry has an `address` (channel-specific identifier: email, * userId, or webhook URL) and a `channel`. Webhook recipients may * additionally carry an optional `signatureKey`; when set, the * webhook adapter signs the request body with HMAC-SHA256 and adds @@ -267,7 +267,13 @@ class Notification extends Event public function reset(): self { + parent::reset(); + $this->project = null; + $this->context = []; + $this->platform = []; + $this->user = null; + $this->userId = null; $this->recipient = ''; $this->name = ''; $this->subject = ''; diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 9a9beab0ff..73cb857cda 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -66,7 +66,7 @@ class Get extends Action if ($secret !== '' && $jwt !== '') { try { - $decoder = new JWT($secret, 'HS256', 2592000, 0); + $decoder = new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0); $decoded = $decoder->decode($jwt); if ( diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index d9ac17e592..fc3225d256 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -16,7 +16,7 @@ use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; -use Utopia\Database\Query; +use Utopia\Database\Validator\UID; use Utopia\Logger\Log; use Utopia\Messaging\Adapter\Email as EmailAdapter; use Utopia\Messaging\Adapter\Email\SMTP; @@ -32,11 +32,6 @@ class Notifications extends Action protected int $previewMaxLen = 150; protected string $whitespaceCodes = ' ‌​‍‎‏'; - /** - * Tracking pixel JWT lifetime: 30 days. - */ - private const TRACKING_JWT_TTL = 2592000; - /** * @var array */ @@ -76,18 +71,21 @@ class Notifications extends Action $deduplicationKey = $payload['deduplicationKey'] ?? ''; $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : ''; - if ($messageId !== '' && $this->alreadyDelivered($dbForPlatform, $messageId)) { - $log->addTag('dedup', 'hit'); - return; - } - $recipients = $this->resolveRecipients($payload); if (empty($recipients)) { throw new Exception('No recipients in payload'); } foreach ($recipients as $recipient) { + $recipient = $this->normalizeRecipient($recipient); $channel = $recipient['channel']; + + if ($messageId !== '' && $this->alreadyDelivered($dbForPlatform, self::buildAlertId($messageId, $recipient))) { + $log->addTag('dedup', 'hit'); + $log->addTag('channel', $channel); + continue; + } + try { $alertId = $this->dispatch($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log); if ($messageId !== '' && $channel === NOTIFICATION_TYPE_WEBHOOK && $alertId === null) { @@ -120,23 +118,28 @@ class Notifications extends Action } /** - * Look up an existing alert by the indexed `messageId` attribute. - * - * Greptile P1 #1: persistAlert and the Console adapter both write - * compound `$id`s (messageId + recipient hash), so a direct - * `getDocument($messageId)` would always miss. Query the attribute. + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @return array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} */ - private function alreadyDelivered(Database $dbForPlatform, string $messageId): bool + private function normalizeRecipient(array $recipient): array { - try { - $matches = $dbForPlatform->find('alerts', [ - Query::equal('messageId', [$messageId]), - Query::limit(1), - ]); - return !empty($matches); - } catch (Throwable) { - return false; + $recipient['userId'] = $recipient['userId'] ?? ''; + $recipient['teamId'] = $recipient['teamId'] ?? ''; + + if ( + $recipient['channel'] === NOTIFICATION_TYPE_CONSOLE + && $recipient['userId'] === '' + && $recipient['teamId'] === '' + ) { + $recipient['userId'] = $recipient['address']; } + + return $recipient; + } + + private function alreadyDelivered(Database $dbForPlatform, string $alertId): bool + { + return !$dbForPlatform->getDocument('alerts', $alertId)->isEmpty(); } /** @@ -183,7 +186,8 @@ class Notifications extends Action $smtp = $this->resolveSmtpConfig($project); if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { - throw new Exception('Skipped email notification. No SMTP configuration has been set.'); + $log->addTag('email_skipped', 'no_smtp'); + return null; } $type = empty($smtp) ? 'cloud' : 'smtp'; @@ -247,16 +251,10 @@ class Notifications extends Action } $subject = \strip_tags($subjectTemplate->render()); - // Pre-compute the alertId WITHOUT persisting so the tracking pixel - // URL is stable, but defer the database write until after a - // successful adapter send. Persisting first risks a dedup-by-attribute - // hit on retry if SMTP throws — the email would be permanently lost. $deterministicAlertId = $messageId !== '' ? self::buildAlertId($messageId, $recipient) : null; - // Tracking pixel: only injectable when we have a userId AND a - // deterministic alertId AND a signing key. $userId = $recipient['userId'] ?? ''; $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); if ($deterministicAlertId !== null && $userId !== '' && !empty($opensslKey)) { @@ -320,16 +318,9 @@ class Notifications extends Action try { $adapter->send($emailMessage); } catch (Throwable $error) { - // Use 500 for any SMTP delivery failure: timeouts, DNS, refused - // connections, and authentication errors are all infrastructure - // problems, not HTTP-style 401s. The queue infrastructure handles - // retries based on the throw, not the code. throw new Exception('Error sending notification: ' . $error->getMessage(), 500); } - // Persist the alert ONLY after a successful send. The Track endpoint - // tolerates a missing alert row, so a tracking pixel fetched between - // send and persistence is harmless. if ($messageId !== '') { return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); } @@ -342,22 +333,22 @@ class Notifications extends Action */ protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $dbForPlatform): ?string { + $this->validateConsoleRecipient($recipient); + $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; - $title = $payload['subject'] ?? ''; - $body = $payload['body'] ?? ''; $params = $payload['templateParams'] ?? ($payload['variables'] ?? []); - if ($title !== '' && !empty($params)) { - $rendered = Template::fromString($title); - foreach ($params as $key => $value) { - $rendered->setParam('{{' . $key . '}}', (string) $value); - } - $title = \strip_tags($rendered->render()); + $title = self::renderText($payload['subject'] ?? '', $params); + $body = self::renderText($payload['preview'] ?? '', $params); + if ($body === '') { + $body = self::renderText($payload['body'] ?? '', $params); } $userId = $recipient['userId'] ?? $recipient['address']; $teamId = $recipient['teamId'] ?? ''; + $alertId = $messageId !== '' ? self::buildAlertId($messageId, $recipient) : null; + $recipientHash = $messageId !== '' ? self::buildRecipientHash($recipient) : null; $consoleRecipient = []; if ($userId !== '') { @@ -366,6 +357,10 @@ class Notifications extends Action if ($teamId !== '') { $consoleRecipient['teamId'] = $teamId; } + if ($alertId !== null) { + $consoleRecipient['alertId'] = $alertId; + $consoleRecipient['recipientHash'] = $recipientHash; + } $consoleMessage = new ConsoleMessage( recipients: [$consoleRecipient], @@ -379,18 +374,12 @@ class Notifications extends Action $adapter = new ConsoleAdapter($dbForPlatform); $result = $adapter->send($consoleMessage); - // Greptile P1 #4: surface adapter failures. The Console adapter - // catches per-recipient exceptions and reports zero deliveries via - // `deliveredTo`; without this throw the worker would silently - // succeed on a hard write failure. if (($result['deliveredTo'] ?? 0) === 0) { $error = $result['results'][0]['error'] ?? 'unknown error'; throw new Exception('Console alert delivery failed: ' . $error); } - // Adapter persisted the alert, so the action loop must NOT - // call persistAlert again (Greptile P1 #3). - return null; + return $alertId; } /** @@ -444,39 +433,41 @@ class Notifications extends Action */ protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload): string { + $recipient = $this->normalizeRecipient($recipient); + $project = $payload['project'] ?? null; $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; $channel = $recipient['channel']; - $address = $recipient['address']; $userId = $recipient['userId'] ?? ''; $teamId = $recipient['teamId'] ?? ''; - // Console alerts derive userId from address when no explicit - // userId is supplied (matches Console adapter's own bookkeeping). - if ($channel === NOTIFICATION_TYPE_CONSOLE && $userId === '' && $teamId === '') { - $userId = $address; - $recipient['userId'] = $userId; - } - $alertId = self::buildAlertId($messageId, $recipient); + $recipientHash = self::buildRecipientHash($recipient); $permissions = $this->buildAlertPermissions($userId, $teamId); if (empty($permissions)) { $permissions = $payload['permissions'] ?? []; } + $params = $payload['templateParams'] ?? ($payload['variables'] ?? []); + $body = self::renderText($payload['preview'] ?? '', $params); + if ($body === '') { + $body = self::renderText($payload['body'] ?? '', $params); + } + $document = new Document([ '$id' => $alertId, '$permissions' => $permissions, 'messageId' => $messageId, + 'recipientHash' => $recipientHash, 'type' => $payload['type'] ?? 'info', 'channel' => $channel, - 'userId' => $userId !== '' ? $userId : null, - 'teamId' => $teamId !== '' ? $teamId : null, + 'userId' => $userId, + 'teamId' => $teamId, 'projectId' => $projectId, - 'title' => $payload['subject'] ?? '', - 'body' => $payload['body'] ?? '', + 'title' => self::renderText($payload['subject'] ?? '', $params), + 'body' => $body, 'read' => false, ]); @@ -490,21 +481,29 @@ class Notifications extends Action } /** - * Build the deterministic alertId composed of the messageId plus an - * 8-char hash over the recipient identity. Single source of truth used + * Build the deterministic alertId composed of the messageId plus a + * recipient hash. Single source of truth used * by both `persistAlert()` and `dispatchEmail()` so the tracking pixel * URL matches the eventually-persisted row exactly. * * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ private static function buildAlertId(string $messageId, array $recipient): string + { + return \substr($messageId, 0, 19) . '_' . self::buildRecipientHash($recipient); + } + + /** + * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + */ + private static function buildRecipientHash(array $recipient): string { $channel = $recipient['channel']; $address = $recipient['address']; $userId = $recipient['userId'] ?? ''; $teamId = $recipient['teamId'] ?? ''; - return $messageId . '_' . \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 8); + return \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 16); } /** @@ -555,16 +554,41 @@ class Notifications extends Action ]; } + private function validateConsoleRecipient(array $recipient): void + { + $validator = new UID(); + + foreach (['userId', 'teamId'] as $key) { + $value = $recipient[$key] ?? ''; + if ($value !== '' && !$validator->isValid($value)) { + throw new Exception('Invalid console alert ' . $key . ': ' . $validator->getDescription()); + } + } + } + + private static function renderText(string $value, array $params): string + { + if ($value !== '' && !empty($params)) { + $template = Template::fromString($value); + foreach ($params as $key => $param) { + $template->setParam('{{' . $key . '}}', (string) $param); + } + $value = $template->render(); + } + + return \trim(\strip_tags($value)); + } + /** * Splice a 1x1 tracking pixel before the last `` tag (or * append at the end if the body has no closing tag). The pixel - * carries a 30-day JWT identifying the alert and user, which the + * carries a signed JWT identifying the alert and user, which the * `/v1/account/alerts/:alertId/track` endpoint verifies before * marking the alert as read. */ private function injectTrackingPixel(string $body, string $alertId, string $userId, string $opensslKey): string { - $jwt = (new JWT($opensslKey, 'HS256', self::TRACKING_JWT_TTL, 0)) + $jwt = (new JWT($opensslKey, 'HS256', ALERT_TRACKING_JWT_TTL, 0)) ->encode([ 'alertId' => $alertId, 'userId' => $userId, diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 91485c2906..b46de5a49a 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -219,10 +219,9 @@ class Webhooks extends Action Query::limit(APP_LIMIT_SUBQUERY) ]); - // Webhook-paused alerts go only to project owners — non-owner team members do not receive them. $ownerMemberships = \array_filter( $memberships, - fn (Document $membership) => \in_array('owner', $membership->getAttribute('roles', []), true) + fn (Document $membership) => self::hasOwnerRole($membership) ); if (empty($ownerMemberships)) { @@ -310,4 +309,23 @@ class Webhooks extends Action $queueForNotifications->trigger(); } + + private static function hasOwnerRole(Document $membership): bool + { + $roles = $membership->getAttribute('roles', []); + if (\is_string($roles)) { + $roles = \array_map('trim', \explode(',', $roles)); + } + if (!\is_array($roles)) { + return false; + } + + foreach ($roles as $role) { + if (\is_string($role) && \strtolower($role) === 'owner') { + return true; + } + } + + return false; + } } diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index bb705e59e4..8e93f6557e 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -64,8 +64,9 @@ class Console extends Adapter $messageId = $message->getMessageId(); $recipientKey = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; + $recipientHash = $recipient['recipientHash'] ?? \substr(\md5($recipientKey), 0, 16); $documentId = $messageId !== null - ? $messageId . '_' . \substr(\md5($recipientKey), 0, 8) + ? ($recipient['alertId'] ?? \substr($messageId, 0, 19) . '_' . $recipientHash) : ID::unique(); try { @@ -73,10 +74,11 @@ class Console extends Adapter '$id' => $documentId, '$permissions' => $this->buildPermissions($userId, $teamId), 'messageId' => $messageId, + 'recipientHash' => $recipientHash, 'type' => $message->getType(), 'channel' => self::TYPE, - 'userId' => $userId !== '' ? $userId : null, - 'teamId' => $teamId !== '' ? $teamId : null, + 'userId' => $userId, + 'teamId' => $teamId, 'projectId' => $message->getProjectId(), 'title' => $message->getTitle(), 'body' => $message->getBody(), diff --git a/src/Appwrite/Utopia/Messaging/Messages/Console.php b/src/Appwrite/Utopia/Messaging/Messages/Console.php index 2226927b10..23fc791233 100644 --- a/src/Appwrite/Utopia/Messaging/Messages/Console.php +++ b/src/Appwrite/Utopia/Messaging/Messages/Console.php @@ -7,7 +7,7 @@ use Utopia\Messaging\Message; class Console implements Message { /** - * @param array $recipients + * @param array $recipients */ public function __construct( protected array $recipients, @@ -20,7 +20,7 @@ class Console implements Message } /** - * @return array + * @return array */ public function getRecipients(): array { @@ -28,7 +28,7 @@ class Console implements Message } /** - * @return array + * @return array */ public function getTo(): array { diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 5423e4de3d..ba4ee6fb26 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -12,7 +12,7 @@ use Utopia\System\System; * account-alerts user-facing API. * * The notification worker itself is exercised in unit tests with a pinned - * queue payload — the server side cannot deterministically inject a + * queue payload; the server side cannot deterministically inject a * Notification onto the live queue without an admin endpoint, so the health * portion validates the public contract that ops and KEDA scale on: * @@ -70,7 +70,7 @@ trait NotificationsBase { // Always read alerts as the console-authenticated owner of the team. // The /v1/account/alerts endpoint is platform-scoped (dbForPlatform) and - // requires a session — server-mode API keys do not satisfy it. + // requires a session; server-mode API keys do not satisfy it. $response = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); $this->assertSame(200, $response['headers']['status-code']); @@ -197,7 +197,7 @@ trait NotificationsBase // Track endpoint requires `purpose: 'alert_track'` — see C/M7 in // PR #12195 review. Other claim purposes are silently ignored (which // testTrackingPixelRejectsJwtWithoutPurposeClaim covers). - $jwt = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + $jwt = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, 'purpose' => 'alert_track', @@ -252,7 +252,7 @@ trait NotificationsBase $userId = $this->getRoot()['$id']; // Mint a JWT with valid alertId/userId but NO purpose claim. - $jwtNoPurpose = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + $jwtNoPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, ]); @@ -278,8 +278,8 @@ trait NotificationsBase $this->assertNotNull($found); $this->assertFalse($found['read'], 'JWT without purpose claim must not flip the read flag'); - // Mint a JWT with a wrong purpose value — same expectation: silently rejected. - $jwtWrongPurpose = (new JWT($secret, 'HS256', 2592000, 0))->encode([ + // Mint a JWT with a wrong purpose value: same expectation, silently rejected. + $jwtWrongPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, 'purpose' => 'something_else', diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index f5cc60c04c..5f147f3de5 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -177,6 +177,7 @@ class NotificationsTest extends TestCase false, ); $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); @@ -187,15 +188,15 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); // Mirror the production `_key_recipient` UNIQUE composite index so the - // duplicate-handling branch in persistAlert (catch DuplicateException → + // duplicate-handling branch in persistAlert (catch DuplicateException -> // return existing alertId) is actually exercised by tests. $this->database->createIndex( 'alerts', '_key_recipient', Database::INDEX_UNIQUE, - ['messageId', 'channel', 'userId', 'teamId'], - [Database::LENGTH_KEY, 64, Database::LENGTH_KEY, Database::LENGTH_KEY], - [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], + ['messageId', 'channel', 'recipientHash'], + [Database::LENGTH_KEY, 64, 64], + [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ); $this->registry = new Registry(); @@ -219,6 +220,16 @@ class NotificationsTest extends TestCase ]); } + private function recipientHash(string $channel, string $address, string $userId = '', string $teamId = ''): string + { + return \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 16); + } + + private function alertId(string $messageId, string $channel, string $address, string $userId = '', string $teamId = ''): string + { + return \substr($messageId, 0, 19) . '_' . $this->recipientHash($channel, $address, $userId, $teamId); + } + public function testDispatchesPerChannelToCorrectAdapter(): void { $worker = new SpyNotifications(); @@ -284,21 +295,9 @@ class NotificationsTest extends TestCase 'deduplicationKey' => 'dup-key', ]; - // First run delivers and persists. $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); $this->assertCount(1, $worker->dispatched); - // Manually insert a row with the dedup messageId so alreadyDelivered() returns true. - $messageId = \md5('dup-key'); - $this->database->createDocument('alerts', new Document([ - '$id' => $messageId, - '$permissions' => [Permission::read(Role::any())], - 'messageId' => $messageId, - 'channel' => 'console', - 'title' => 'x', - 'body' => 'y', - ])); - $worker->dispatched = []; $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); $this->assertCount(0, $worker->dispatched, 'second invocation must short-circuit on dedup hit'); @@ -391,37 +390,46 @@ class NotificationsTest extends TestCase $this->assertCount(0, $rows, 'failed dispatch must not persist alert'); } - public function testDedupQueriesByAttributeNotById(): void + public function testDedupSkipsOnlyDeliveredRecipientAfterPartialFanoutFailure(): void { $worker = new SpyNotifications(); + $worker->throwOn[NOTIFICATION_TYPE_WEBHOOK] = new \RuntimeException('webhook down'); - // Seed an alert row with an arbitrary $id but the matching dedup - // messageId attribute. If alreadyDelivered() short-circuits via - // getDocument($messageId) it will miss this seed and dispatch - // anyway. Querying by the `messageId` attribute is the only way to - // see the seed. - $messageId = \md5('dup-key'); - $this->database->createDocument('alerts', new Document([ - '$id' => 'random-id-123', - '$permissions' => [Permission::read(Role::any())], - 'messageId' => $messageId, - 'channel' => 'console', - 'title' => 'seed', - 'body' => 'seed', - ])); - + $messageId = \md5('partial-key'); $payload = [ 'project' => ['$id' => 'project-x'], - 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE]], + 'recipients' => [ + ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK], + ], 'subject' => 'Sub', 'body' => 'B', - 'deduplicationKey' => 'dup-key', + 'deduplicationKey' => 'partial-key', ]; - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + $this->fail('expected webhook failure to propagate'); + } catch (\RuntimeException $error) { + $this->assertSame('webhook down', $error->getMessage()); + } - $this->assertCount(0, $worker->dispatched, 'attribute-keyed seed must trigger dedup short-circuit'); - $this->assertSame('hit', $this->log->getTags()['dedup'] ?? null); + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(1, $rows, 'first attempt should persist only the successful console recipient'); + $this->assertSame(NOTIFICATION_TYPE_CONSOLE, $rows[0]->getAttribute('channel')); + + $retry = new SpyNotifications(); + $retry->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertCount(1, $retry->dispatched, 'retry should dispatch only the previously undelivered webhook'); + $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $retry->dispatched[0]['channel']); + + $rows = $this->database->find('alerts', [ + \Utopia\Database\Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(2, $rows, 'retry must complete the missing recipient without duplicating console'); } public function testConsoleChannelSkipsPersistAlert(): void @@ -539,8 +547,12 @@ class NotificationsTest extends TestCase // provide an OpenSSL key so injectTrackingPixel actually runs. $previousSmtpHost = \getenv('_APP_SMTP_HOST'); $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); + $previousDomain = \getenv('_APP_DOMAIN'); + $previousConsoleDomain = \getenv('_APP_CONSOLE_DOMAIN'); \putenv('_APP_SMTP_HOST=spy.smtp.test'); \putenv('_APP_OPENSSL_KEY_V1=test-key-32bytes-min-aaaaaaaaaaaaaa'); + \putenv('_APP_DOMAIN=api.example.test'); + \putenv('_APP_CONSOLE_DOMAIN=console.example.test'); try { $worker = new Notifications(); @@ -563,6 +575,8 @@ class NotificationsTest extends TestCase } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); \putenv($previousOpensslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpensslKey); + \putenv($previousDomain === false ? '_APP_DOMAIN' : '_APP_DOMAIN=' . $previousDomain); + \putenv($previousConsoleDomain === false ? '_APP_CONSOLE_DOMAIN' : '_APP_CONSOLE_DOMAIN=' . $previousConsoleDomain); } $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); @@ -571,6 +585,8 @@ class NotificationsTest extends TestCase $this->assertStringContainsString(' must be present'); $this->assertStringContainsString('/v1/account/alerts/', $body); $this->assertStringContainsString('/track?jwt=', $body); + $this->assertStringContainsString('http://api.example.test/v1/account/alerts/', \html_entity_decode($body)); + $this->assertStringNotContainsString('console.example.test/v1/account/alerts/', \html_entity_decode($body)); // The pixel must sit BEFORE the last . $lastBodyClose = \strripos($body, ''); @@ -593,7 +609,7 @@ class NotificationsTest extends TestCase // All four unique-index fields populated so the // `_key_recipient` UNIQUE composite (messageId, channel, - // userId, teamId) actually fires — SQL UNIQUE semantics treat + // userId, teamId) actually fires. SQL UNIQUE semantics treat // NULL as not-equal, so any null in the tuple disables it. $payload = [ 'project' => ['$id' => 'project-x'], @@ -629,7 +645,7 @@ class NotificationsTest extends TestCase // action loop's alreadyDelivered() check short-circuits before // persistAlert, so call persistAlert directly to actually hit // the duplicate branch. The deterministic $id collides on the - // primary key → DuplicateException → branch returns the + // primary key -> DuplicateException -> branch returns the // existing alertId without throwing. $reflection = new \ReflectionMethod($worker, 'persistAlert'); $secondAlertId = $reflection->invoke($worker, $this->database, $messageId, $recipient, $payload); @@ -638,13 +654,14 @@ class NotificationsTest extends TestCase // Third write: bypass the deterministic $id path and use a // distinct $id with the same recipient tuple. The - // `_key_recipient` UNIQUE composite must reject it — proving + // `_key_recipient` UNIQUE composite must reject it, proving // the unique-index (not just primary-key) is what backstops the // duplicate-handling branch. $sameTupleDoc = new Document([ '$id' => 'sibling-id-' . \uniqid(), '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, + 'recipientHash' => $this->recipientHash(NOTIFICATION_TYPE_EMAIL, 'user@example.test', 'user-7', 'team-7'), 'channel' => NOTIFICATION_TYPE_EMAIL, 'userId' => 'user-7', 'teamId' => 'team-7', @@ -814,7 +831,14 @@ class NotificationsTest extends TestCase ->setTemplate('template-id') ->setTemplateParams(['x' => 1]) ->setDeduplicationKey('dedup') - ->setPermissions([Permission::read(Role::any())]); + ->setPermissions([Permission::read(Role::any())]) + ->setEvent('databases.*.documents.*.create') + ->setParam('databaseId', 'database-1') + ->setPayload(['before' => 'reset'], ['before']) + ->setUser(new Document(['$id' => 'user-before'])) + ->setUserId('user-before') + ->setPlatform(['name' => 'platform-before']) + ->setContext('actor', new Document(['$id' => 'actor-before'])); $event->reset(); @@ -833,6 +857,13 @@ class NotificationsTest extends TestCase $this->assertSame('', $event->getDeduplicationKey(), 'deduplicationKey must reset to empty string'); $this->assertSame([], $event->getPermissions(), 'permissions must reset to empty array'); $this->assertNull($event->getProject(), 'project must reset to null'); + $this->assertSame('', $event->getEvent(), 'inherited event must reset to empty string'); + $this->assertSame([], $event->getParams(), 'inherited params must reset to empty array'); + $this->assertSame([], $event->getPayload(), 'inherited payload must reset to empty array'); + $this->assertNull($event->getUser(), 'inherited user must reset to null'); + $this->assertNull($event->getUserId(), 'inherited userId must reset to null'); + $this->assertSame([], $event->getPlatform(), 'inherited platform must reset to empty array'); + $this->assertNull($event->getContext('actor'), 'inherited context must reset'); } /** @@ -976,6 +1007,7 @@ class NotificationsTest extends TestCase // dispatchEmail's returned alertId must match the row $id (used by the // tracking pixel URL). $this->assertSame($worker->persistedIds[0], $row->getId()); + $this->assertLessThanOrEqual(36, \strlen($row->getId()), 'alert ids must pass the UID route validator'); } /** @@ -1017,11 +1049,10 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_TYPE_CONSOLE, $row->getAttribute('channel')); $this->assertFalse($row->getAttribute('read')); - // Per-recipient suffix scheme used by the Console adapter is - // `messageId . '_' . substr(md5('user:' . userId), 0, 8)`. $messageId = \md5('happy-console'); - $expectedId = $messageId . '_' . \substr(\md5('user:u1'), 0, 8); + $expectedId = $this->alertId($messageId, NOTIFICATION_TYPE_CONSOLE, 'console-recipient', 'u1', 't1'); $this->assertSame($expectedId, $row->getId(), 'row $id must match adapter suffix scheme'); + $this->assertLessThanOrEqual(36, \strlen($row->getId()), 'alert ids must pass the UID route validator'); $permissions = $row->getPermissions(); $this->assertContains(Permission::read(Role::user('u1')), $permissions); @@ -1034,6 +1065,86 @@ class NotificationsTest extends TestCase $this->assertSame(0, $worker->persistAlertCalls, 'console channel must NOT trigger action-loop persistAlert'); } + public function testConsoleChannelUsesPreviewBodyInsteadOfRenderedEmailHtml(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'user-preview', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'userId' => 'user-preview', + ], + ], + 'subject' => 'Webhook {{name}} paused', + 'preview' => 'Plain alert for {{name}}.', + 'body' => 'Email-only HTML', + 'templateParams' => ['name' => 'orders'], + 'deduplicationKey' => 'console-preview', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $rows = $this->database->find('alerts'); + $this->assertCount(1, $rows); + $this->assertSame('Webhook orders paused', $rows[0]->getAttribute('title')); + $this->assertSame('Plain alert for orders.', $rows[0]->getAttribute('body')); + $this->assertStringNotContainsString('', $rows[0]->getAttribute('body')); + } + + public function testEmailChannelSkipsWhenSmtpIsNotConfigured(): void + { + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST='); + + try { + $worker = new CountingPersistAlertNotifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + [ + 'address' => 'missing-smtp@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-smtp', + ], + ], + 'subject' => 'No SMTP', + 'body' => 'Body', + 'deduplicationKey' => 'missing-smtp', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + + $this->assertSame(0, $worker->persistAlertCalls); + $this->assertCount(0, $this->database->find('alerts')); + $this->assertSame('no_smtp', $this->log->getTags()['email_skipped'] ?? null); + } + + public function testConsoleChannelRejectsInvalidImplicitUserId(): void + { + $worker = new CountingPersistAlertNotifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + ['address' => 'not an appwrite user id', 'channel' => NOTIFICATION_TYPE_CONSOLE], + ], + 'subject' => 'Invalid', + 'body' => 'Body', + 'deduplicationKey' => 'invalid-console-user', + ]; + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Invalid console alert userId'); + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } + /** * Worker happy-path: webhook channel. * diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php new file mode 100644 index 0000000000..00fbd7878e --- /dev/null +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -0,0 +1,36 @@ + 'membership-1', + 'roles' => $roles, + ]); + + $this->assertSame($expected, $method->invoke(null, $membership)); + } + + public static function ownerRoleProvider(): array + { + return [ + 'array owner' => [['owner'], true], + 'array mixed case owner' => [['Owner'], true], + 'comma string owner' => ['developer, owner', true], + 'non owner' => [['developer'], false], + 'invalid roles' => [null, false], + ]; + } +} diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index 5ed51d8db1..fe21f2d261 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -33,6 +33,7 @@ class ConsoleTest extends TestCase $this->database->create(); $this->database->createCollection('alerts', [], [], [Permission::create(Role::any()), Permission::read(Role::any())], false); $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); @@ -55,7 +56,7 @@ class ConsoleTest extends TestCase private function alertId(string $messageId, string $userId = '', string $teamId = ''): string { $key = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; - return $messageId . '_' . \substr(\md5($key), 0, 8); + return \substr($messageId, 0, 19) . '_' . \substr(\md5($key), 0, 16); } public function testWritesAlertWithCorrectSchema(): void @@ -155,9 +156,8 @@ class ConsoleTest extends TestCase $this->assertSame('a', $rowA->getAttribute('userId')); $this->assertSame('b', $rowB->getAttribute('userId')); - // $id values must be `messageId_<8-hex>` and the suffixes differ. - $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{8}$/', $idA); - $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{8}$/', $idB); + $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{16}$/', $idA); + $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{16}$/', $idB); } public function testRejectsForeignMessageType(): void @@ -166,7 +166,7 @@ class ConsoleTest extends TestCase $this->expectException(\Exception::class); $this->expectExceptionMessage('Invalid message type.'); - // ConsoleMessage extends nothing — pass an unrelated Message implementation + // ConsoleMessage extends nothing; pass an unrelated Message implementation $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); } @@ -174,7 +174,7 @@ class ConsoleTest extends TestCase * Reviewer C4: a `DuplicateException` thrown by `createDocument` must be * treated as a SUCCESSFUL delivery, not a failure. The adapter previously * lumped Duplicate into the generic Throwable catch, which surfaced as a - * per-recipient `error` and caused the worker to throw — re-queueing the + * per-recipient `error` and caused the worker to throw, re-queueing the * notification and never marking the duplicate as delivered. */ public function testConsoleAdapterTreatsDuplicateAsDelivered(): void @@ -190,6 +190,7 @@ class ConsoleTest extends TestCase '$id' => $documentId, '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, + 'recipientHash' => \substr(\md5('user:' . $userId), 0, 16), 'channel' => 'console', 'userId' => $userId, 'title' => 'pre-existing', @@ -212,7 +213,7 @@ class ConsoleTest extends TestCase $this->assertSame('success', $result['results'][0]['status'] ?? '', 'duplicate must report success status'); $this->assertSame('', $result['results'][0]['error'] ?? 'unset', 'duplicate must not surface a per-recipient error'); - // Still exactly ONE row — the pre-existing one. The adapter must not + // Still exactly ONE row: the pre-existing one. The adapter must not // overwrite it nor create a sibling. $rows = $this->database->find('alerts'); $this->assertCount(1, $rows); From 62b0314bcb04fcd1d22c2c955f35220cd69639ba Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 21:58:45 +1200 Subject: [PATCH 39/80] fix: scope notification alerts by project --- .github/workflows/ci.yml | 1 + app/config/collections/platform.php | 52 +++++++- app/controllers/api/messaging.php | 9 ++ .../health/get-queue-notifications.md | 1 + src/Appwrite/Migration/Version/V19.php | 1 + src/Appwrite/Migration/Version/V24.php | 34 +++++ .../Account/Http/Alerts/Read/Update.php | 6 + .../Modules/Account/Http/Alerts/Track/Get.php | 6 +- .../Modules/Account/Http/Alerts/XList.php | 8 +- .../Functions/Http/Executions/Create.php | 1 + .../Functions/Http/Functions/Create.php | 1 + .../Functions/Http/Functions/Update.php | 2 + .../Health/Http/Health/Queue/Failed/Get.php | 5 + .../Http/Health/Queue/Notifications/Get.php | 60 +++++++++ .../Platform/Modules/Health/Services/Http.php | 2 + .../Projects/Http/Schedules/Create.php | 1 + .../Modules/Projects/Http/Schedules/Get.php | 5 +- .../Modules/Projects/Http/Schedules/XList.php | 7 +- src/Appwrite/Platform/Workers/Deletes.php | 1 + .../Platform/Workers/Notifications.php | 44 ++++--- src/Appwrite/Platform/Workers/Webhooks.php | 27 ++-- .../Utopia/Messaging/Adapter/Console.php | 10 +- .../Utopia/Messaging/Messages/Console.php | 6 + .../Notifications/NotificationsBase.php | 51 ++++++++ .../Platform/Workers/NotificationsTest.php | 118 +++++++++++++++--- tests/unit/Platform/Workers/WebhooksTest.php | 6 +- .../Utopia/Messaging/Adapter/ConsoleTest.php | 23 +++- 27 files changed, 431 insertions(+), 57 deletions(-) create mode 100644 docs/references/health/get-queue-notifications.md create mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac16c80264..abd2c50714 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -441,6 +441,7 @@ jobs: Webhooks, VCS, Messaging, + Notifications, Migrations, Project ] diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index d1a5a9b518..01f6acef95 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -474,6 +474,17 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('projectInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('schedule'), 'type' => Database::VAR_STRING, @@ -534,6 +545,13 @@ $platformCollections = [ 'lengths' => [], 'orders' => [], ], + [ + '$id' => ID::custom('_key_region_resourceType_projectInternalId_resourceId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['region', 'resourceType', 'projectInternalId', 'resourceId'], + 'lengths' => [], + 'orders' => [], + ], [ '$id' => ID::custom('_key_project_id_region'), 'type' => Database::INDEX_KEY, @@ -541,6 +559,13 @@ $platformCollections = [ 'lengths' => [], 'orders' => [], ], + [ + '$id' => ID::custom('_key_project_internal_id_region'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectInternalId', 'region'], + 'lengths' => [], + 'orders' => [], + ], [ '$id' => ID::custom('_key_region_rt_active'), 'type' => Database::INDEX_KEY, @@ -1109,7 +1134,18 @@ $platformCollections = [ 'format' => '', 'size' => Database::LENGTH_KEY, 'signed' => true, - 'required' => false, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('projectInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => true, 'default' => null, 'array' => false, 'filters' => [], @@ -1184,6 +1220,13 @@ $platformCollections = [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_projectInternalId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectInternalId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => ID::custom('_key_userId_type'), 'type' => Database::INDEX_KEY, @@ -1212,6 +1255,13 @@ $platformCollections = [ 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_userId_projectInternalId'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['userId', 'projectInternalId'], + 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], ], ], diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index d1ffa2e478..4d16219864 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -3292,6 +3292,7 @@ Http::post('/v1/messaging/messages/email') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -3438,6 +3439,7 @@ Http::post('/v1/messaging/messages/sms') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -3660,6 +3662,7 @@ Http::post('/v1/messaging/messages/push') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -4054,6 +4057,7 @@ Http::patch('/v1/messaging/messages/email/:messageId') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => $status === MessageStatus::SCHEDULED, ])); @@ -4070,6 +4074,7 @@ Http::patch('/v1/messaging/messages/email/:messageId') } $schedule + ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); @@ -4278,6 +4283,7 @@ Http::patch('/v1/messaging/messages/sms/:messageId') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => $status === MessageStatus::SCHEDULED, ])); @@ -4294,6 +4300,7 @@ Http::patch('/v1/messaging/messages/sms/:messageId') } $schedule + ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); @@ -4455,6 +4462,7 @@ Http::patch('/v1/messaging/messages/push/:messageId') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => $status === MessageStatus::SCHEDULED, ])); @@ -4471,6 +4479,7 @@ Http::patch('/v1/messaging/messages/push/:messageId') } $schedule + ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); diff --git a/docs/references/health/get-queue-notifications.md b/docs/references/health/get-queue-notifications.md new file mode 100644 index 0000000000..529d1056e9 --- /dev/null +++ b/docs/references/health/get-queue-notifications.md @@ -0,0 +1 @@ +Get the number of jobs in the notifications queue. diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 2526d03009..6f939b4ce4 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -736,6 +736,7 @@ class V19 extends Migration 'resourceInternalId' => $document->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $this->project->getId(), + 'projectInternalId' => $this->project->getSequence(), 'schedule' => $document->getAttribute('schedule'), 'active' => !empty($document->getAttribute('schedule')) && !empty($document->getAttribute('deployment')), ])); diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index a2d9d7907b..204a7a22e4 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -144,6 +144,29 @@ class V24 extends Migration $this->dbForProject->purgeCachedCollection($id); break; + case 'schedules': + if ($collectionType === 'console') { + try { + $this->createAttributeFromCollection($this->dbForProject, $id, 'projectInternalId'); + } catch (Throwable $th) { + Console::warning("Failed to create attribute \"projectInternalId\" in collection {$id}: {$th->getMessage()}"); + } + + $indexes = [ + '_key_region_resourceType_projectInternalId_resourceId', + '_key_project_internal_id_region', + ]; + foreach ($indexes as $index) { + try { + $this->createIndexFromCollection($this->dbForProject, $id, $index); + } catch (Throwable $th) { + Console::warning("Failed to create index \"{$index}\" from {$id}: {$th->getMessage()}"); + } + } + } + $this->dbForProject->purgeCachedCollection($id); + break; + case 'rules': if ($collectionType === 'console') { try { @@ -372,6 +395,17 @@ class V24 extends Migration $document->setAttribute('resourceInternalId', $projectInternalId); } break; + case 'schedules': + if (empty($document->getAttribute('projectInternalId'))) { + $projectId = $document->getAttribute('projectId', ''); + if ($projectId !== '') { + $project = $this->dbForProject->getDocument('projects', $projectId); + if (!$project->isEmpty()) { + $document->setAttribute('projectInternalId', $project->getSequence()); + } + } + } + break; default: break; } diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php index 1aa71013fd..3909c92834 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php @@ -46,6 +46,7 @@ class Update extends Action ->param('alertId', '', new UID(), 'Alert ID.') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->inject('user') ->callback($this->action(...)); } @@ -54,8 +55,13 @@ class Update extends Action string $alertId, Response $response, Database $dbForPlatform, + Document $project, Document $user, ): void { + if ($project->getId() !== 'console') { + throw new Exception(Exception::USER_UNAUTHORIZED); + } + $alert = $dbForPlatform->getDocument('alerts', $alertId); if ($alert->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 73cb857cda..9f84ef1679 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -64,13 +64,13 @@ class Get extends Action ): void { $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); - if ($secret !== '' && $jwt !== '') { + if (!empty($secret) && $jwt !== '') { try { $decoder = new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0); $decoded = $decoder->decode($jwt); if ( - isset($decoded['alertId'], $decoded['userId'], $decoded['purpose']) + isset($decoded['alertId'], $decoded['userId'], $decoded['projectId'], $decoded['purpose']) && $decoded['purpose'] === 'alert_track' && $decoded['alertId'] === $alertId ) { @@ -80,6 +80,8 @@ class Get extends Action if ( !$alert->isEmpty() && $alert->getAttribute('userId') === $decoded['userId'] + && $alert->getAttribute('projectId') === $decoded['projectId'] + && (!isset($decoded['projectInternalId']) || $alert->getAttribute('projectInternalId') === $decoded['projectInternalId']) && $alert->getAttribute('read') !== true ) { $dbForPlatform->updateDocument('alerts', $alertId, new Document([ diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php index c91ec1232d..820cb7061d 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php @@ -50,6 +50,7 @@ class XList extends Action ->param('queries', [], new Alerts(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Alerts::ALLOWED_ATTRIBUTES), true) ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->inject('user') ->callback($this->action(...)); } @@ -61,8 +62,13 @@ class XList extends Action array $queries, Response $response, Database $dbForPlatform, + Document $project, Document $user ): void { + if ($project->getId() !== 'console') { + throw new Exception(Exception::USER_UNAUTHORIZED); + } + try { $queries = Query::parseQueries($queries); } catch (QueryException $e) { @@ -89,7 +95,7 @@ class XList extends Action $alertId = $cursor->getValue(); $cursorDocument = $dbForPlatform->getDocument('alerts', $alertId); - if ($cursorDocument->isEmpty()) { + if ($cursorDocument->isEmpty() || $cursorDocument->getAttribute('userId') !== $user->getId()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Alert '{$alertId}' for the 'cursor' value not found."); } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 35264730f8..24684a3a51 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -325,6 +325,7 @@ class Create extends Base 'resourceInternalId' => $execution->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'data' => $data, 'active' => true, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index 148f0945ac..1799e51a12 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -263,6 +263,7 @@ class Create extends Base 'resourceInternalId' => $function->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $function->getAttribute('schedule'), 'active' => false, ])) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index e8713a179d..ee9e930e16 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -299,6 +299,7 @@ class Update extends Base 'resourceInternalId' => $function->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $function->getAttribute('schedule'), 'active' => false, ])) @@ -311,6 +312,7 @@ class Update extends Base } $schedule + ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('schedule', $function->getAttribute('schedule')) ->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deploymentId'))); diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php index d3b760d01b..f1c6867379 100644 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php +++ b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Health\Http\Health\Queue\Failed; use Appwrite\Event\Event; +use Appwrite\Event\Notification; use Appwrite\Event\Publisher\Audit; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Event\Publisher\Certificate; @@ -71,6 +72,7 @@ class Get extends Base System::getEnv('_APP_SCREENSHOTS_QUEUE_NAME', Event::SCREENSHOTS_QUEUE_NAME), System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME), System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME), + System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME), ]), 'The name of the queue') ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) ->inject('response') @@ -87,6 +89,7 @@ class Get extends Base ->inject('publisherForMessaging') ->inject('publisherForMigrations') ->inject('publisherForScreenshots') + ->inject('queueForNotifications') ->callback($this->action(...)); } @@ -107,6 +110,7 @@ class Get extends Base MessagingPublisher $publisherForMessaging, MigrationPublisher $publisherForMigrations, Screenshot $publisherForScreenshots, + Notification $queueForNotifications, ): void { $threshold = (int) $threshold; @@ -124,6 +128,7 @@ class Get extends Base System::getEnv('_APP_SCREENSHOTS_QUEUE_NAME', Event::SCREENSHOTS_QUEUE_NAME) => $publisherForScreenshots, System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME) => $publisherForMessaging, System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME) => $publisherForMigrations, + System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME) => $queueForNotifications, default => throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Unknown queue name: ' . $name), }; $failed = $queue->getSize(failed: true); diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php new file mode 100644 index 0000000000..9377f8852a --- /dev/null +++ b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php @@ -0,0 +1,60 @@ +setHttpMethod(Base::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/health/queue/notifications') + ->desc('Get notifications queue') + ->groups(['api', 'health']) + ->label('scope', 'health.read') + ->label('sdk', new Method( + namespace: 'health', + group: 'queue', + name: 'getQueueNotifications', + description: '/docs/references/health/get-queue-notifications.md', + auth: [AuthType::ADMIN, AuthType::KEY], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_HEALTH_QUEUE, + ) + ], + contentType: ContentType::JSON + )) + ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) + ->inject('queueForNotifications') + ->inject('response') + ->callback($this->action(...)); + } + + public function action(int|string $threshold, Notification $queueForNotifications, Response $response): void + { + $threshold = (int) $threshold; + + $size = $queueForNotifications->getSize(); + + $this->assertQueueThreshold($size, $threshold); + + $response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE); + } +} diff --git a/src/Appwrite/Platform/Modules/Health/Services/Http.php b/src/Appwrite/Platform/Modules/Health/Services/Http.php index 54c6f9ad6d..a6acbcb4d4 100644 --- a/src/Appwrite/Platform/Modules/Health/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Health/Services/Http.php @@ -19,6 +19,7 @@ use Appwrite\Platform\Modules\Health\Http\Health\Queue\Logs\Get as GetQueueLogs; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Mails\Get as GetQueueMails; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Messaging\Get as GetQueueMessaging; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Migrations\Get as GetQueueMigrations; +use Appwrite\Platform\Modules\Health\Http\Health\Queue\Notifications\Get as GetQueueNotifications; use Appwrite\Platform\Modules\Health\Http\Health\Queue\StatsResources\Get as GetQueueStatsResources; use Appwrite\Platform\Modules\Health\Http\Health\Queue\StatsUsage\Get as GetQueueUsage; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Webhooks\Get as GetQueueWebhooks; @@ -56,6 +57,7 @@ class Http extends Service $this->addAction(GetQueueMails::getName(), new GetQueueMails()); $this->addAction(GetQueueMessaging::getName(), new GetQueueMessaging()); $this->addAction(GetQueueMigrations::getName(), new GetQueueMigrations()); + $this->addAction(GetQueueNotifications::getName(), new GetQueueNotifications()); $this->addAction(GetQueueFunctions::getName(), new GetQueueFunctions()); $this->addAction(GetQueueStatsResources::getName(), new GetQueueStatsResources()); $this->addAction(GetQueueUsage::getName(), new GetQueueUsage()); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php index e00809300d..2380af8997 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php @@ -131,6 +131,7 @@ class Create extends Action 'resourceInternalId' => $resource->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'schedule' => $schedule, 'active' => $active, ]; diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php index d14d4c361d..f89eecd1a0 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php @@ -67,7 +67,10 @@ class Get extends Action throw new Exception(Exception::SCHEDULE_NOT_FOUND); } - if ($schedule->getAttribute('projectId') !== $project->getId()) { + if ( + $schedule->getAttribute('projectId') !== $project->getId() + || $schedule->getAttribute('projectInternalId') !== $project->getSequence() + ) { throw new Exception(Exception::SCHEDULE_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php index 66879f798f..d0e6ad3036 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php @@ -77,6 +77,7 @@ class XList extends Action } $queries[] = Query::equal('projectId', [$project->getId()]); + $queries[] = Query::equal('projectInternalId', [$project->getSequence()]); $cursor = Query::getCursorQueries($queries, false); $cursor = \reset($cursor); @@ -90,7 +91,11 @@ class XList extends Action $scheduleId = $cursor->getValue(); $cursorDocument = $dbForPlatform->getDocument('schedules', $scheduleId); - if ($cursorDocument->isEmpty()) { + if ( + $cursorDocument->isEmpty() + || $cursorDocument->getAttribute('projectId') !== $project->getId() + || $cursorDocument->getAttribute('projectInternalId') !== $project->getSequence() + ) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Schedule '{$scheduleId}' for the 'cursor' value not found."); } diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index a58fc48098..a5b1e6578f 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -726,6 +726,7 @@ class Deletes extends Action try { $this->deleteByGroup('schedules', [ Query::equal('projectId', [$projectId]), + Query::equal('projectInternalId', [$projectInternalId]), Query::orderAsc() ], $dbForPlatform); } catch (Throwable $th) { diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index fc3225d256..be6e9a5061 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -76,6 +76,7 @@ class Notifications extends Action throw new Exception('No recipients in payload'); } + $failure = null; foreach ($recipients as $recipient) { $recipient = $this->normalizeRecipient($recipient); $channel = $recipient['channel']; @@ -89,14 +90,18 @@ class Notifications extends Action try { $alertId = $this->dispatch($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log); if ($messageId !== '' && $channel === NOTIFICATION_TYPE_WEBHOOK && $alertId === null) { - $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); + $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); } } catch (Throwable $error) { $log->addTag('channel', $channel); $log->addTag('error', $error->getMessage()); - throw $error; + $failure ??= $error; } } + + if ($failure !== null) { + throw $failure; + } } /** @@ -164,7 +169,7 @@ class Notifications extends Action return match ($channel) { NOTIFICATION_TYPE_EMAIL => $this->dispatchEmail($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log), - NOTIFICATION_TYPE_CONSOLE => $this->dispatchConsole($recipient, $messageId, $payload, $dbForPlatform), + NOTIFICATION_TYPE_CONSOLE => $this->dispatchConsole($recipient, $messageId, $payload, $project, $dbForPlatform), NOTIFICATION_TYPE_WEBHOOK => $this->dispatchWebhook($recipient, $payload, $log), default => throw new Exception('Unsupported notification channel: ' . $channel), }; @@ -258,7 +263,7 @@ class Notifications extends Action $userId = $recipient['userId'] ?? ''; $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); if ($deterministicAlertId !== null && $userId !== '' && !empty($opensslKey)) { - $body = $this->injectTrackingPixel($body, $deterministicAlertId, $userId, $opensslKey); + $body = $this->injectTrackingPixel($body, $deterministicAlertId, $userId, $project, $opensslKey); } /** @var EmailAdapter $adapter */ @@ -322,7 +327,7 @@ class Notifications extends Action } if ($messageId !== '') { - return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); + return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); } return null; @@ -331,13 +336,10 @@ class Notifications extends Action /** * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ - protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $dbForPlatform): ?string + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Document $project, Database $dbForPlatform): ?string { $this->validateConsoleRecipient($recipient); - $project = $payload['project'] ?? null; - $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; - $params = $payload['templateParams'] ?? ($payload['variables'] ?? []); $title = self::renderText($payload['subject'] ?? '', $params); $body = self::renderText($payload['preview'] ?? '', $params); @@ -368,7 +370,8 @@ class Notifications extends Action body: $body, type: $payload['type'] ?? 'info', messageId: $messageId !== '' ? $messageId : null, - projectId: $projectId, + projectId: $project->getId(), + projectInternalId: $project->getSequence(), ); $adapter = new ConsoleAdapter($dbForPlatform); @@ -431,13 +434,10 @@ class Notifications extends Action * * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient */ - protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload): string + protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload, Document $project): string { $recipient = $this->normalizeRecipient($recipient); - $project = $payload['project'] ?? null; - $projectId = \is_array($project) ? ($project['$id'] ?? null) : null; - $channel = $recipient['channel']; $userId = $recipient['userId'] ?? ''; $teamId = $recipient['teamId'] ?? ''; @@ -445,7 +445,7 @@ class Notifications extends Action $alertId = self::buildAlertId($messageId, $recipient); $recipientHash = self::buildRecipientHash($recipient); - $permissions = $this->buildAlertPermissions($userId, $teamId); + $permissions = $this->buildAlertPermissions($userId, $teamId, $project->getId()); if (empty($permissions)) { $permissions = $payload['permissions'] ?? []; } @@ -465,7 +465,8 @@ class Notifications extends Action 'channel' => $channel, 'userId' => $userId, 'teamId' => $teamId, - 'projectId' => $projectId, + 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'title' => self::renderText($payload['subject'] ?? '', $params), 'body' => $body, 'read' => false, @@ -509,7 +510,7 @@ class Notifications extends Action /** * @return array */ - private function buildAlertPermissions(string $userId, string $teamId): array + private function buildAlertPermissions(string $userId, string $teamId, string $projectId): array { $permissions = []; if ($userId !== '') { @@ -521,6 +522,11 @@ class Notifications extends Action $permissions[] = Permission::read(Role::team($teamId)); $permissions[] = Permission::update(Role::team($teamId, 'owner')); $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + if ($projectId !== '') { + $permissions[] = Permission::read(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::update(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::delete(Role::team($teamId, 'project-' . $projectId . '-owner')); + } } return $permissions; } @@ -586,12 +592,14 @@ class Notifications extends Action * `/v1/account/alerts/:alertId/track` endpoint verifies before * marking the alert as read. */ - private function injectTrackingPixel(string $body, string $alertId, string $userId, string $opensslKey): string + private function injectTrackingPixel(string $body, string $alertId, string $userId, Document $project, string $opensslKey): string { $jwt = (new JWT($opensslKey, 'HS256', ALERT_TRACKING_JWT_TTL, 0)) ->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'projectId' => $project->getId(), + 'projectInternalId' => $project->getSequence(), 'purpose' => 'alert_track', ]); diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index b46de5a49a..a5e6cfc672 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -221,7 +221,7 @@ class Webhooks extends Action $ownerMemberships = \array_filter( $memberships, - fn (Document $membership) => self::hasOwnerRole($membership) + fn (Document $membership) => self::hasOwnerRole($membership, $project->getId()) ); if (empty($ownerMemberships)) { @@ -288,6 +288,14 @@ class Webhooks extends Action $email = $user->getAttribute('email'); $userId = $user->getId(); + $queueForNotifications->addRecipient( + $userId, + NOTIFICATION_TYPE_CONSOLE, + null, + $userId, + $teamId, + ); + if (!empty($email)) { $queueForNotifications->addRecipient( $email, @@ -297,20 +305,12 @@ class Webhooks extends Action $teamId, ); } - - $queueForNotifications->addRecipient( - $userId, - NOTIFICATION_TYPE_CONSOLE, - null, - $userId, - $teamId, - ); } $queueForNotifications->trigger(); } - private static function hasOwnerRole(Document $membership): bool + private static function hasOwnerRole(Document $membership, string $projectId): bool { $roles = $membership->getAttribute('roles', []); if (\is_string($roles)) { @@ -321,7 +321,12 @@ class Webhooks extends Action } foreach ($roles as $role) { - if (\is_string($role) && \strtolower($role) === 'owner') { + if (!\is_string($role)) { + continue; + } + + $role = \strtolower($role); + if ($role === 'owner' || $role === 'project-' . \strtolower($projectId) . '-owner') { return true; } } diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index 8e93f6557e..f3aa4332ed 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -72,7 +72,7 @@ class Console extends Adapter try { $document = new Document([ '$id' => $documentId, - '$permissions' => $this->buildPermissions($userId, $teamId), + '$permissions' => $this->buildPermissions($userId, $teamId, $message->getProjectId() ?? ''), 'messageId' => $messageId, 'recipientHash' => $recipientHash, 'type' => $message->getType(), @@ -80,6 +80,7 @@ class Console extends Adapter 'userId' => $userId, 'teamId' => $teamId, 'projectId' => $message->getProjectId(), + 'projectInternalId' => $message->getProjectInternalId(), 'title' => $message->getTitle(), 'body' => $message->getBody(), ]); @@ -105,7 +106,7 @@ class Console extends Adapter /** * @return array */ - private function buildPermissions(string $userId, string $teamId): array + private function buildPermissions(string $userId, string $teamId, string $projectId): array { $permissions = []; if ($userId !== '') { @@ -117,6 +118,11 @@ class Console extends Adapter $permissions[] = Permission::read(Role::team($teamId)); $permissions[] = Permission::update(Role::team($teamId, 'owner')); $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + if ($projectId !== '') { + $permissions[] = Permission::read(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::update(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::delete(Role::team($teamId, 'project-' . $projectId . '-owner')); + } } return $permissions; } diff --git a/src/Appwrite/Utopia/Messaging/Messages/Console.php b/src/Appwrite/Utopia/Messaging/Messages/Console.php index 23fc791233..70573613dc 100644 --- a/src/Appwrite/Utopia/Messaging/Messages/Console.php +++ b/src/Appwrite/Utopia/Messaging/Messages/Console.php @@ -16,6 +16,7 @@ class Console implements Message protected string $type = 'info', protected ?string $messageId = null, protected ?string $projectId = null, + protected ?string $projectInternalId = null, ) { } @@ -59,4 +60,9 @@ class Console implements Message { return $this->projectId; } + + public function getProjectInternalId(): ?string + { + return $this->projectInternalId; + } } diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index ba4ee6fb26..5891df8e4b 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -185,6 +185,54 @@ trait NotificationsBase self::$seededAlertId = $alertId; } + public function testProjectAccountCannotListPlatformAlertsWithCollidingUserId(): void + { + $alertId = $this->seedWebhookFailureAlert(); + $this->assertNotEmpty($alertId); + + $attackerProject = $this->getProject(true); + $projectId = $attackerProject['$id']; + $email = \uniqid('colliding-alert-user-', true) . '@localhost.test'; + $password = 'password'; + + $created = $this->client->call(Client::METHOD_POST, '/users', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-key' => $attackerProject['apiKey'], + ], [ + 'userId' => $this->getRoot()['$id'], + 'email' => $email, + 'password' => $password, + 'name' => 'Colliding Alert User', + ]); + + $this->assertSame(201, $created['headers']['status-code']); + + $session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], [ + 'email' => $email, + 'password' => $password, + ]); + + $this->assertSame(201, $session['headers']['status-code']); + $this->assertNotEmpty($session['cookies']['a_session_' . $projectId] ?? ''); + + $response = $this->client->call(Client::METHOD_GET, '/account/alerts', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_' . $projectId . '=' . $session['cookies']['a_session_' . $projectId], + 'x-appwrite-project' => $projectId, + ]); + + $this->assertSame(401, $response['headers']['status-code']); + $this->assertSame('user_unauthorized', $response['body']['type'] ?? ''); + + self::$seededAlertId = $alertId; + } + public function testTrackingPixelTogglesRead(): void { $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); @@ -200,6 +248,7 @@ trait NotificationsBase $jwt = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'projectId' => $this->getProject()['$id'], 'purpose' => 'alert_track', ]); @@ -255,6 +304,7 @@ trait NotificationsBase $jwtNoPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'projectId' => $this->getProject()['$id'], ]); $response = $this->client->call( @@ -282,6 +332,7 @@ trait NotificationsBase $jwtWrongPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, 'userId' => $userId, + 'projectId' => $this->getProject()['$id'], 'purpose' => 'something_else', ]); diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 5f147f3de5..f20b2100cc 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Platform\Workers; +use Ahc\Jwt\JWT; use Appwrite\Event\Notification; use Appwrite\Platform\Workers\Notifications; use PHPUnit\Framework\TestCase; @@ -13,6 +14,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; +use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Logger\Log; use Utopia\Messaging\Adapter\Email as EmailAdapter; @@ -64,7 +66,7 @@ class SpyNotifications extends Notifications } if ($channel === NOTIFICATION_TYPE_CONSOLE || $channel === NOTIFICATION_TYPE_EMAIL) { - return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload); + return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); } return null; @@ -84,10 +86,10 @@ class CountingPersistAlertNotifications extends Notifications /** @var array */ public array $persistedIds = []; - protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload): string + protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload, Document $project): string { $this->persistAlertCalls++; - $alertId = parent::persistAlert($dbForPlatform, $messageId, $recipient, $payload); + $alertId = parent::persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); $this->persistedIds[] = $alertId; return $alertId; } @@ -100,7 +102,7 @@ class CountingPersistAlertNotifications extends Notifications */ class ZeroDeliveryConsoleNotifications extends Notifications { - protected function dispatchConsole(array $recipient, string $messageId, array $payload, Database $dbForPlatform): ?string + protected function dispatchConsole(array $recipient, string $messageId, array $payload, Document $project, Database $dbForPlatform): ?string { // Simulate the Console adapter swallowing a per-recipient // exception and reporting zero deliveries. @@ -182,7 +184,8 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_STRING, 255, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); @@ -200,7 +203,7 @@ class NotificationsTest extends TestCase ); $this->registry = new Registry(); - $this->project = new Document(['$id' => 'project-x']); + $this->project = new Document(['$id' => 'project-x', '$sequence' => 'project-internal-x']); $this->log = new Log(); } @@ -415,7 +418,7 @@ class NotificationsTest extends TestCase } $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'first attempt should persist only the successful console recipient'); $this->assertSame(NOTIFICATION_TYPE_CONSOLE, $rows[0]->getAttribute('channel')); @@ -427,7 +430,7 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $retry->dispatched[0]['channel']); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(2, $rows, 'retry must complete the missing recipient without duplicating console'); } @@ -452,7 +455,7 @@ class NotificationsTest extends TestCase // must NOT have called persistAlert (otherwise we'd see 2 rows or // a duplicate-key swallow plus a non-zero counter). $consoleRows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('channel', ['console']), + Query::equal('channel', ['console']), ]); $this->assertCount(1, $consoleRows); $this->assertSame(0, $worker->persistAlertCalls, 'console channel must NOT trigger action-loop persistAlert'); @@ -588,6 +591,14 @@ class NotificationsTest extends TestCase $this->assertStringContainsString('http://api.example.test/v1/account/alerts/', \html_entity_decode($body)); $this->assertStringNotContainsString('console.example.test/v1/account/alerts/', \html_entity_decode($body)); + \preg_match('/track\?jwt=([^"&]+)/', \html_entity_decode($body), $matches); + $this->assertNotEmpty($matches[1] ?? ''); + + $claims = (new JWT('test-key-32bytes-min-aaaaaaaaaaaaaa', 'HS256', ALERT_TRACKING_JWT_TTL, 0)) + ->decode(\urldecode($matches[1])); + $this->assertSame('project-x', $claims['projectId'] ?? null); + $this->assertSame('project-internal-x', $claims['projectInternalId'] ?? null); + // The pixel must sit BEFORE the last . $lastBodyClose = \strripos($body, ''); $pixelPosition = \strripos($body, 'invoke($worker, $this->database, $messageId, $recipient, $payload); + $secondAlertId = $reflection->invoke($worker, $this->database, $messageId, $recipient, $payload, $this->project); $this->assertSame($firstAlertId, $secondAlertId, 'duplicate persist must return the existing alertId'); @@ -666,6 +677,7 @@ class NotificationsTest extends TestCase 'userId' => 'user-7', 'teamId' => 'team-7', 'projectId' => 'project-x', + 'projectInternalId' => 'project-internal-x', 'title' => 'sibling', 'body' => 'sibling', 'read' => false, @@ -680,7 +692,7 @@ class NotificationsTest extends TestCase $this->assertTrue($threw, 'unique-index `_key_recipient` must reject a second row sharing the recipient tuple'); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'unique-index must prevent a second row from being persisted'); } finally { @@ -779,7 +791,7 @@ class NotificationsTest extends TestCase // Critical: no orphan dedup row. If there is one, the retry below // will short-circuit and the user never gets the email. $orphans = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(0, $orphans, 'failed SMTP send must not leave a dedup row behind'); @@ -794,7 +806,7 @@ class NotificationsTest extends TestCase $this->assertSame(1, $working->sendCount, 'retry must invoke the working adapter'); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'retry must persist exactly one alert row'); $this->assertSame('user-9', $rows[0]->getAttribute('userId')); @@ -804,6 +816,80 @@ class NotificationsTest extends TestCase } } + public function testEmailFailureDoesNotBlockConsoleRecipient(): void + { + $failing = new SpyEmailAdapter(); + $failing->throwOnSend = true; + $this->registry->set('smtp', static fn () => $failing); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + $payload = [ + 'project' => ['$id' => 'project-x', '$sequence' => 'project-internal-x'], + 'recipients' => [ + [ + 'address' => 'user@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'userId' => 'user-9', + 'teamId' => 'team-9', + ], + [ + 'address' => 'user-9', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'userId' => 'user-9', + 'teamId' => 'team-9', + ], + ], + 'subject' => 'Subj', + 'body' => 'Body', + 'deduplicationKey' => 'smtp-fail-console-key', + ]; + + $messageId = \md5('smtp-fail-console-key'); + + try { + $worker = new Notifications(); + + $threw = false; + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } catch (\Throwable $error) { + $threw = true; + $this->assertStringContainsString('SMTP unavailable', $error->getMessage()); + } + $this->assertTrue($threw, 'SMTP failure must still propagate so the email recipient is retried'); + + $consoleRows = $this->database->find('alerts', [ + Query::equal('messageId', [$messageId]), + Query::equal('channel', [NOTIFICATION_TYPE_CONSOLE]), + ]); + $this->assertCount(1, $consoleRows, 'console recipient must be persisted even when email fails first'); + $this->assertSame('project-x', $consoleRows[0]->getAttribute('projectId')); + $this->assertSame('project-internal-x', $consoleRows[0]->getAttribute('projectInternalId')); + + $emailRows = $this->database->find('alerts', [ + Query::equal('messageId', [$messageId]), + Query::equal('channel', [NOTIFICATION_TYPE_EMAIL]), + ]); + $this->assertCount(0, $emailRows, 'failed email recipient must not leave an orphan dedup row'); + + $working = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $working); + + $retryWorker = new Notifications(); + $retryWorker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + + $this->assertSame(1, $working->sendCount, 'retry must still deliver the email recipient'); + $rows = $this->database->find('alerts', [ + Query::equal('messageId', [$messageId]), + ]); + $this->assertCount(2, $rows, 'retry must add email without duplicating the already-delivered console alert'); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + } + /** * Reviewer C2: `Notification::reset()` must clear EVERY state-bearing * field — `$preview` was missing from the original reset() body and @@ -996,7 +1082,7 @@ class NotificationsTest extends TestCase $this->assertSame(1, $worker->persistAlertCalls, 'email channel must persist exactly once after a successful send'); $messageId = \md5('happy-email'); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [$messageId]), + Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows); $row = $rows[0]; @@ -1039,7 +1125,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('channel', ['console']), + Query::equal('channel', ['console']), ]); $this->assertCount(1, $rows, 'console adapter must write exactly one alert'); @@ -1243,7 +1329,7 @@ class NotificationsTest extends TestCase // Action loop must persist exactly one webhook alert row AFTER the send. $this->assertSame(1, $worker->persistAlertCalls); $rows = $this->database->find('alerts', [ - \Utopia\Database\Query::equal('messageId', [\md5('happy-webhook')]), + Query::equal('messageId', [\md5('happy-webhook')]), ]); $this->assertCount(1, $rows); $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $rows[0]->getAttribute('channel')); diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 00fbd7878e..86f23fd417 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -20,7 +20,7 @@ class WebhooksTest extends TestCase 'roles' => $roles, ]); - $this->assertSame($expected, $method->invoke(null, $membership)); + $this->assertSame($expected, $method->invoke(null, $membership, 'project-1')); } public static function ownerRoleProvider(): array @@ -28,7 +28,11 @@ class WebhooksTest extends TestCase return [ 'array owner' => [['owner'], true], 'array mixed case owner' => [['Owner'], true], + 'project owner' => [['project-project-1-owner'], true], + 'mixed case project owner' => [['Project-Project-1-Owner'], true], 'comma string owner' => ['developer, owner', true], + 'comma string project owner' => ['developer, project-project-1-owner', true], + 'other project owner' => [['project-project-2-owner'], false], 'non owner' => [['developer'], false], 'invalid roles' => [null, false], ]; diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index fe21f2d261..0bb6b4574d 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -4,11 +4,13 @@ namespace Tests\Unit\Utopia\Messaging\Adapter; use Appwrite\Utopia\Messaging\Adapter\Console; use Appwrite\Utopia\Messaging\Messages\Console as ConsoleMessage; +use Appwrite\Utopia\Messaging\Messages\Webhook as WebhookMessage; use PHPUnit\Framework\TestCase; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\Memory; use Utopia\Database\Database; +use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -38,7 +40,8 @@ class ConsoleTest extends TestCase $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_STRING, 255, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); } @@ -68,6 +71,7 @@ class ConsoleTest extends TestCase type: 'info', messageId: ID::custom('msg-aaa'), projectId: 'project-1', + projectInternalId: 'project-internal-1', ); $adapter = new Console($this->database); @@ -81,6 +85,7 @@ class ConsoleTest extends TestCase $this->assertSame('console', $stored->getAttribute('channel')); $this->assertSame('user-1', $stored->getAttribute('userId')); $this->assertSame('project-1', $stored->getAttribute('projectId')); + $this->assertSame('project-internal-1', $stored->getAttribute('projectInternalId')); $this->assertSame('Hello', $stored->getAttribute('title')); $this->assertSame('World', $stored->getAttribute('body')); $this->assertSame('info', $stored->getAttribute('type')); @@ -93,6 +98,8 @@ class ConsoleTest extends TestCase title: 'Title', body: 'Body', messageId: ID::custom('msg-perms-user'), + projectId: 'project-1', + projectInternalId: 'project-internal-1', ); (new Console($this->database))->send($message); @@ -112,6 +119,8 @@ class ConsoleTest extends TestCase title: 'Heads up', body: '...', messageId: ID::custom('msg-team'), + projectId: 'project-1', + projectInternalId: 'project-internal-1', ); (new Console($this->database))->send($message); @@ -122,6 +131,9 @@ class ConsoleTest extends TestCase $this->assertContains(Permission::read(Role::team('team-9')), $permissions); $this->assertContains(Permission::update(Role::team('team-9', 'owner')), $permissions); $this->assertContains(Permission::delete(Role::team('team-9', 'owner')), $permissions); + $this->assertContains(Permission::read(Role::team('team-9', 'project-project-1-owner')), $permissions); + $this->assertContains(Permission::update(Role::team('team-9', 'project-project-1-owner')), $permissions); + $this->assertContains(Permission::delete(Role::team('team-9', 'project-project-1-owner')), $permissions); } public function testMultiRecipientWithSameMessageIdGeneratesDistinctIds(): void @@ -134,6 +146,8 @@ class ConsoleTest extends TestCase title: 'Heads up', body: 'multi', messageId: ID::custom('same-msg'), + projectId: 'project-1', + projectInternalId: 'project-internal-1', ); $adapter = new Console($this->database); @@ -167,7 +181,7 @@ class ConsoleTest extends TestCase $this->expectExceptionMessage('Invalid message type.'); // ConsoleMessage extends nothing; pass an unrelated Message implementation - $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Webhook(urls: ['https://example.test'], payload: [])); + $adapter->send(new WebhookMessage(urls: ['https://example.test'], payload: [])); } /** @@ -186,13 +200,15 @@ class ConsoleTest extends TestCase // Pre-insert an alert with the SAME id the adapter will compute. The // adapter's createDocument will hit the primary-key DuplicateException // and must treat it as a successful (idempotent) send. - $this->database->createDocument('alerts', new \Utopia\Database\Document([ + $this->database->createDocument('alerts', new Document([ '$id' => $documentId, '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, 'recipientHash' => \substr(\md5('user:' . $userId), 0, 16), 'channel' => 'console', 'userId' => $userId, + 'projectId' => 'project-x', + 'projectInternalId' => 'project-internal-x', 'title' => 'pre-existing', 'body' => 'pre-existing', ])); @@ -203,6 +219,7 @@ class ConsoleTest extends TestCase body: 'b', messageId: ID::custom($messageId), projectId: 'project-x', + projectInternalId: 'project-internal-x', ); $adapter = new Console($this->database); From 21d73af3e1d15f28ab182c7f7a7aa3a06129efea Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 16 May 2026 10:06:48 +0000 Subject: [PATCH 40/80] fix(platform): use VAR_ID for schedules projectInternalId Agent-Logs-Url: https://github.com/appwrite/appwrite/sessions/c0a8700e-3763-4cf0-b19e-cc83b2e4318e Co-authored-by: abnegate <5857008+abnegate@users.noreply.github.com> --- app/config/collections/platform.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 01f6acef95..0fcf37c5bf 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -476,9 +476,9 @@ $platformCollections = [ ], [ '$id' => ID::custom('projectInternalId'), - 'type' => Database::VAR_STRING, + 'type' => Database::VAR_ID, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 0, 'signed' => true, 'required' => false, 'default' => null, From c5738b6a8ed91143828da9edeb22220846d19f8f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 16 May 2026 10:09:36 +0000 Subject: [PATCH 41/80] fix(alerts): use VAR_ID for projectInternalId Agent-Logs-Url: https://github.com/appwrite/appwrite/sessions/e5a13d2c-8eba-4a7e-b98a-68faa7d2cdf3 Co-authored-by: abnegate <5857008+abnegate@users.noreply.github.com> --- app/config/collections/platform.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 0fcf37c5bf..ec5661e22a 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1141,9 +1141,9 @@ $platformCollections = [ ], [ '$id' => ID::custom('projectInternalId'), - 'type' => Database::VAR_STRING, + 'type' => Database::VAR_ID, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 0, 'signed' => true, 'required' => true, 'default' => null, From 730b1cb723493ae0900e0d830d8f9da3b4b6e8d6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 22:29:53 +1200 Subject: [PATCH 42/80] fix: use resource scoped alert records --- app/config/collections/platform.php | 109 +++++--- app/init/constants.php | 2 + src/Appwrite/Event/Notification.php | 39 ++- .../Account/Http/Alerts/Read/Update.php | 11 +- .../Modules/Account/Http/Alerts/Track/Get.php | 4 +- .../Modules/Account/Http/Alerts/XList.php | 4 +- src/Appwrite/Platform/Workers/Deletes.php | 1 - .../Platform/Workers/Notifications.php | 147 ++++++----- src/Appwrite/Platform/Workers/Webhooks.php | 14 +- .../Database/Validator/Queries/Alerts.php | 4 + .../Utopia/Messaging/Adapter/Console.php | 56 ++-- .../Utopia/Messaging/Messages/Console.php | 6 +- src/Appwrite/Utopia/Response/Model/Alert.php | 24 +- .../Notifications/NotificationsBase.php | 12 +- .../Platform/Workers/NotificationsTest.php | 239 ++++++++++-------- .../Utopia/Messaging/Adapter/ConsoleTest.php | 78 ++++-- .../Utopia/Messaging/Adapter/WebhookTest.php | 14 +- 17 files changed, 481 insertions(+), 283 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index ec5661e22a..43d31ee883 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1107,23 +1107,23 @@ $platformCollections = [ 'filters' => [], ], [ - '$id' => ID::custom('userId'), + '$id' => ID::custom('resourceType'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 64, 'signed' => true, - 'required' => false, + 'required' => true, 'default' => null, 'array' => false, 'filters' => [], ], [ - '$id' => ID::custom('teamId'), + '$id' => ID::custom('resourceId'), 'type' => Database::VAR_STRING, 'format' => '', 'size' => Database::LENGTH_KEY, 'signed' => true, - 'required' => false, + 'required' => true, 'default' => null, 'array' => false, 'filters' => [], @@ -1150,6 +1150,50 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('resourceInternalId'), + 'type' => Database::VAR_ID, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('parentResourceType'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 64, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('parentResourceId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('parentResourceInternalId'), + 'type' => Database::VAR_ID, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('title'), 'type' => Database::VAR_STRING, @@ -1200,18 +1244,11 @@ $platformCollections = [ 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_read'), + '$id' => ID::custom('_key_resource_read'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'read'], - 'lengths' => [Database::LENGTH_KEY, 0], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_teamId_read'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['teamId', 'read'], - 'lengths' => [Database::LENGTH_KEY, 0], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['resourceType', 'resourceId', 'read'], + 'lengths' => [64, Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_projectId'), @@ -1228,39 +1265,39 @@ $platformCollections = [ 'orders' => [Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_type'), + '$id' => ID::custom('_key_project_resource'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'type'], - 'lengths' => [Database::LENGTH_KEY, 64], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['projectInternalId', 'resourceType', 'resourceId'], + 'lengths' => [0, 64, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_channel'), + '$id' => ID::custom('_key_project_parent_resource'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'channel'], - 'lengths' => [Database::LENGTH_KEY, 64], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['projectInternalId', 'parentResourceType', 'parentResourceId'], + 'lengths' => [0, 64, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_messageId'), + '$id' => ID::custom('_key_resource_type'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'messageId'], - 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['resourceType', 'resourceId', 'type'], + 'lengths' => [64, Database::LENGTH_KEY, 64], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_projectId'), + '$id' => ID::custom('_key_resource_channel'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'projectId'], - 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['resourceType', 'resourceId', 'channel'], + 'lengths' => [64, Database::LENGTH_KEY, 64], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ - '$id' => ID::custom('_key_userId_projectInternalId'), + '$id' => ID::custom('_key_resource_messageId'), 'type' => Database::INDEX_KEY, - 'attributes' => ['userId', 'projectInternalId'], - 'lengths' => [Database::LENGTH_KEY, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['resourceType', 'resourceId', 'messageId'], + 'lengths' => [64, Database::LENGTH_KEY, Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], ], ], diff --git a/app/init/constants.php b/app/init/constants.php index a8696a8fbf..255fc8ee1e 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -437,6 +437,8 @@ const RESOURCE_TYPE_SUBSCRIBERS = 'subscribers'; const RESOURCE_TYPE_MESSAGES = 'messages'; const RESOURCE_TYPE_EXECUTIONS = 'executions'; const RESOURCE_TYPE_VCS = 'vcs'; +const RESOURCE_TYPE_USERS = 'users'; +const RESOURCE_TYPE_TEAMS = 'teams'; const RESOURCE_TYPE_EMBEDDINGS_TEXT = 'embeddingsText'; const RESOURCE_TYPE_INSIGHTS = 'insights'; const RESOURCE_TYPE_REPORTS = 'reports'; diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php index 3c2c9d6eaa..ef64726ce1 100644 --- a/src/Appwrite/Event/Notification.php +++ b/src/Appwrite/Event/Notification.php @@ -21,14 +21,13 @@ class Notification extends Event * Recipients to deliver the notification to. * * Each entry has an `address` (channel-specific identifier: email, - * userId, or webhook URL) and a `channel`. Webhook recipients may + * user ID, or webhook URL) and a `channel`. Webhook recipients may * additionally carry an optional `signatureKey`; when set, the * webhook adapter signs the request body with HMAC-SHA256 and adds * the `X-Appwrite-Webhook-Signature` header. Without a key the - * payload is sent unsigned. Optional `userId` and `teamId` identify - * the owner of the alert (used by C2/C3 budget/limit alerts). + * payload is sent unsigned. Resource fields identify the alert owner. * - * @var array + * @var array */ protected array $recipients = []; @@ -161,7 +160,7 @@ class Notification extends Event } /** - * @param array $recipients + * @param array $recipients */ public function setRecipients(array $recipients): self { @@ -170,7 +169,7 @@ class Notification extends Event } /** - * @return array + * @return array */ public function getRecipients(): array { @@ -181,18 +180,34 @@ class Notification extends Event string $address, string $channel = NOTIFICATION_TYPE_EMAIL, ?string $signatureKey = null, - ?string $userId = null, - ?string $teamId = null, + ?string $resourceType = null, + ?string $resourceId = null, + ?string $resourceInternalId = null, + ?string $parentResourceType = null, + ?string $parentResourceId = null, + ?string $parentResourceInternalId = null, ): self { $recipient = ['address' => $address, 'channel' => $channel]; if ($signatureKey !== null && $signatureKey !== '') { $recipient['signatureKey'] = $signatureKey; } - if ($userId !== null && $userId !== '') { - $recipient['userId'] = $userId; + if ($resourceType !== null && $resourceType !== '') { + $recipient['resourceType'] = $resourceType; } - if ($teamId !== null && $teamId !== '') { - $recipient['teamId'] = $teamId; + if ($resourceId !== null && $resourceId !== '') { + $recipient['resourceId'] = $resourceId; + } + if ($resourceInternalId !== null && $resourceInternalId !== '') { + $recipient['resourceInternalId'] = $resourceInternalId; + } + if ($parentResourceType !== null && $parentResourceType !== '') { + $recipient['parentResourceType'] = $parentResourceType; + } + if ($parentResourceId !== null && $parentResourceId !== '') { + $recipient['parentResourceId'] = $parentResourceId; + } + if ($parentResourceInternalId !== null && $parentResourceInternalId !== '') { + $recipient['parentResourceInternalId'] = $parentResourceInternalId; } $this->recipients[] = $recipient; return $this; diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php index 3909c92834..83885fb927 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php @@ -9,6 +9,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -46,6 +47,7 @@ class Update extends Action ->param('alertId', '', new UID(), 'Alert ID.') ->inject('response') ->inject('dbForPlatform') + ->inject('authorization') ->inject('project') ->inject('user') ->callback($this->action(...)); @@ -55,6 +57,7 @@ class Update extends Action string $alertId, Response $response, Database $dbForPlatform, + Authorization $authorization, Document $project, Document $user, ): void { @@ -65,13 +68,13 @@ class Update extends Action $alert = $dbForPlatform->getDocument('alerts', $alertId); if ($alert->isEmpty()) { + $exists = $authorization->skip(fn () => !$dbForPlatform->getDocument('alerts', $alertId)->isEmpty()); + if ($exists) { + throw new Exception(Exception::USER_UNAUTHORIZED); + } throw new Exception(Exception::DOCUMENT_NOT_FOUND); } - if ($alert->getAttribute('userId') !== $user->getId()) { - throw new Exception(Exception::USER_UNAUTHORIZED); - } - $updated = $dbForPlatform->updateDocument('alerts', $alertId, new Document([ 'read' => true, ])); diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php index 9f84ef1679..704ad46763 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php @@ -70,7 +70,7 @@ class Get extends Action $decoded = $decoder->decode($jwt); if ( - isset($decoded['alertId'], $decoded['userId'], $decoded['projectId'], $decoded['purpose']) + isset($decoded['alertId'], $decoded['recipientHash'], $decoded['projectId'], $decoded['purpose']) && $decoded['purpose'] === 'alert_track' && $decoded['alertId'] === $alertId ) { @@ -79,7 +79,7 @@ class Get extends Action if ( !$alert->isEmpty() - && $alert->getAttribute('userId') === $decoded['userId'] + && $alert->getAttribute('recipientHash') === $decoded['recipientHash'] && $alert->getAttribute('projectId') === $decoded['projectId'] && (!isset($decoded['projectInternalId']) || $alert->getAttribute('projectInternalId') === $decoded['projectInternalId']) && $alert->getAttribute('read') !== true diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php index 820cb7061d..015ae32d9e 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php @@ -75,8 +75,6 @@ class XList extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } - $queries[] = Query::equal('userId', [$user->getId()]); - /** * Get cursor document if there was a cursor query, we use array_filter and reset for reference $cursor to $queries */ @@ -95,7 +93,7 @@ class XList extends Action $alertId = $cursor->getValue(); $cursorDocument = $dbForPlatform->getDocument('alerts', $alertId); - if ($cursorDocument->isEmpty() || $cursorDocument->getAttribute('userId') !== $user->getId()) { + if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Alert '{$alertId}' for the 'cursor' value not found."); } diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index a5b1e6578f..a58fc48098 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -726,7 +726,6 @@ class Deletes extends Action try { $this->deleteByGroup('schedules', [ Query::equal('projectId', [$projectId]), - Query::equal('projectInternalId', [$projectInternalId]), Query::orderAsc() ], $dbForPlatform); } catch (Throwable $th) { diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index be6e9a5061..43250f2f79 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -78,9 +78,16 @@ class Notifications extends Action $failure = null; foreach ($recipients as $recipient) { - $recipient = $this->normalizeRecipient($recipient); + $recipient = $this->normalizeRecipient($recipient, $project); $channel = $recipient['channel']; + if ($messageId !== '') { + if ($channel === NOTIFICATION_TYPE_CONSOLE) { + $this->validateConsoleRecipient($recipient); + } + $this->validateAlertResource($recipient); + } + if ($messageId !== '' && $this->alreadyDelivered($dbForPlatform, self::buildAlertId($messageId, $recipient))) { $log->addTag('dedup', 'hit'); $log->addTag('channel', $channel); @@ -105,7 +112,7 @@ class Notifications extends Action } /** - * @return array + * @return array */ private function resolveRecipients(array $payload): array { @@ -123,20 +130,28 @@ class Notifications extends Action } /** - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient - * @return array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} + * @param array{address: string, channel: string, signatureKey?: string, resourceType?: string, resourceId?: string, resourceInternalId?: string, parentResourceType?: string, parentResourceId?: string, parentResourceInternalId?: string} $recipient + * @return array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} */ - private function normalizeRecipient(array $recipient): array + private function normalizeRecipient(array $recipient, Document $project): array { - $recipient['userId'] = $recipient['userId'] ?? ''; - $recipient['teamId'] = $recipient['teamId'] ?? ''; + $recipient['resourceType'] = $recipient['resourceType'] ?? ''; + $recipient['resourceId'] = $recipient['resourceId'] ?? ''; + $recipient['resourceInternalId'] = $recipient['resourceInternalId'] ?? ''; + $recipient['parentResourceType'] = $recipient['parentResourceType'] ?? RESOURCE_TYPE_PROJECTS; + $recipient['parentResourceId'] = $recipient['parentResourceId'] ?? (string) $project->getId(); + $recipient['parentResourceInternalId'] = $recipient['parentResourceInternalId'] ?? (string) $project->getSequence(); if ( $recipient['channel'] === NOTIFICATION_TYPE_CONSOLE - && $recipient['userId'] === '' - && $recipient['teamId'] === '' + && $recipient['resourceId'] === '' ) { - $recipient['userId'] = $recipient['address']; + $recipient['resourceType'] = RESOURCE_TYPE_USERS; + $recipient['resourceId'] = $recipient['address']; + } + + foreach (['resourceType', 'resourceId', 'resourceInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'] as $key) { + $recipient[$key] = (string) $recipient[$key]; } return $recipient; @@ -154,7 +169,7 @@ class Notifications extends Action * persisted an alert row, so the action loop knows to skip persistence. * Returns null when persistence is the caller's responsibility. * - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ protected function dispatch( array $recipient, @@ -176,7 +191,7 @@ class Notifications extends Action } /** - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ protected function dispatchEmail( array $recipient, @@ -260,10 +275,10 @@ class Notifications extends Action ? self::buildAlertId($messageId, $recipient) : null; - $userId = $recipient['userId'] ?? ''; + $recipientHash = self::buildRecipientHash($recipient); $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); - if ($deterministicAlertId !== null && $userId !== '' && !empty($opensslKey)) { - $body = $this->injectTrackingPixel($body, $deterministicAlertId, $userId, $project, $opensslKey); + if ($deterministicAlertId !== null && !empty($opensslKey)) { + $body = $this->injectTrackingPixel($body, $deterministicAlertId, $recipientHash, $project, $opensslKey); } /** @var EmailAdapter $adapter */ @@ -334,7 +349,7 @@ class Notifications extends Action } /** - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ protected function dispatchConsole(array $recipient, string $messageId, array $payload, Document $project, Database $dbForPlatform): ?string { @@ -347,18 +362,18 @@ class Notifications extends Action $body = self::renderText($payload['body'] ?? '', $params); } - $userId = $recipient['userId'] ?? $recipient['address']; - $teamId = $recipient['teamId'] ?? ''; $alertId = $messageId !== '' ? self::buildAlertId($messageId, $recipient) : null; $recipientHash = $messageId !== '' ? self::buildRecipientHash($recipient) : null; - $consoleRecipient = []; - if ($userId !== '') { - $consoleRecipient['userId'] = $userId; - } - if ($teamId !== '') { - $consoleRecipient['teamId'] = $teamId; - } + $consoleRecipient = [ + 'address' => $recipient['address'], + 'resourceType' => $recipient['resourceType'], + 'resourceId' => $recipient['resourceId'], + 'resourceInternalId' => $recipient['resourceInternalId'], + 'parentResourceType' => $recipient['parentResourceType'], + 'parentResourceId' => $recipient['parentResourceId'], + 'parentResourceInternalId' => $recipient['parentResourceInternalId'], + ]; if ($alertId !== null) { $consoleRecipient['alertId'] = $alertId; $consoleRecipient['recipientHash'] = $recipientHash; @@ -386,7 +401,7 @@ class Notifications extends Action } /** - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ protected function dispatchWebhook(array $recipient, array $payload, Log $log): ?string { @@ -432,20 +447,18 @@ class Notifications extends Action * Idempotent: on a duplicate composite-key violation the existing * row's id is returned. * - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType?: string, resourceId?: string, resourceInternalId?: string, parentResourceType?: string, parentResourceId?: string, parentResourceInternalId?: string} $recipient */ protected function persistAlert(Database $dbForPlatform, string $messageId, array $recipient, array $payload, Document $project): string { - $recipient = $this->normalizeRecipient($recipient); + $recipient = $this->normalizeRecipient($recipient, $project); $channel = $recipient['channel']; - $userId = $recipient['userId'] ?? ''; - $teamId = $recipient['teamId'] ?? ''; $alertId = self::buildAlertId($messageId, $recipient); $recipientHash = self::buildRecipientHash($recipient); - $permissions = $this->buildAlertPermissions($userId, $teamId, $project->getId()); + $permissions = $this->buildAlertPermissions($recipient['resourceType'], $recipient['resourceId'], $project->getId()); if (empty($permissions)) { $permissions = $payload['permissions'] ?? []; } @@ -463,10 +476,14 @@ class Notifications extends Action 'recipientHash' => $recipientHash, 'type' => $payload['type'] ?? 'info', 'channel' => $channel, - 'userId' => $userId, - 'teamId' => $teamId, 'projectId' => $project->getId(), 'projectInternalId' => $project->getSequence(), + 'resourceType' => $recipient['resourceType'], + 'resourceId' => $recipient['resourceId'], + 'resourceInternalId' => $recipient['resourceInternalId'], + 'parentResourceType' => $recipient['parentResourceType'], + 'parentResourceId' => $recipient['parentResourceId'], + 'parentResourceInternalId' => $recipient['parentResourceInternalId'], 'title' => self::renderText($payload['subject'] ?? '', $params), 'body' => $body, 'read' => false, @@ -487,7 +504,7 @@ class Notifications extends Action * by both `persistAlert()` and `dispatchEmail()` so the tracking pixel * URL matches the eventually-persisted row exactly. * - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ private static function buildAlertId(string $messageId, array $recipient): string { @@ -495,37 +512,44 @@ class Notifications extends Action } /** - * @param array{address: string, channel: string, signatureKey?: string, userId?: string, teamId?: string} $recipient + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ private static function buildRecipientHash(array $recipient): string { $channel = $recipient['channel']; $address = $recipient['address']; - $userId = $recipient['userId'] ?? ''; - $teamId = $recipient['teamId'] ?? ''; - return \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 16); + return \substr(\md5( + $channel + . ':' . $address + . ':' . $recipient['resourceType'] + . ':' . $recipient['resourceId'] + . ':' . $recipient['resourceInternalId'] + . ':' . $recipient['parentResourceType'] + . ':' . $recipient['parentResourceId'] + . ':' . $recipient['parentResourceInternalId'] + ), 0, 16); } /** * @return array */ - private function buildAlertPermissions(string $userId, string $teamId, string $projectId): array + private function buildAlertPermissions(string $resourceType, string $resourceId, string $projectId): array { $permissions = []; - if ($userId !== '') { - $permissions[] = Permission::read(Role::user($userId)); - $permissions[] = Permission::update(Role::user($userId)); - $permissions[] = Permission::delete(Role::user($userId)); + if ($resourceType === RESOURCE_TYPE_USERS) { + $permissions[] = Permission::read(Role::user($resourceId)); + $permissions[] = Permission::update(Role::user($resourceId)); + $permissions[] = Permission::delete(Role::user($resourceId)); } - if ($teamId !== '') { - $permissions[] = Permission::read(Role::team($teamId)); - $permissions[] = Permission::update(Role::team($teamId, 'owner')); - $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + if ($resourceType === RESOURCE_TYPE_TEAMS) { + $permissions[] = Permission::read(Role::team($resourceId)); + $permissions[] = Permission::update(Role::team($resourceId, 'owner')); + $permissions[] = Permission::delete(Role::team($resourceId, 'owner')); if ($projectId !== '') { - $permissions[] = Permission::read(Role::team($teamId, 'project-' . $projectId . '-owner')); - $permissions[] = Permission::update(Role::team($teamId, 'project-' . $projectId . '-owner')); - $permissions[] = Permission::delete(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::read(Role::team($resourceId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::update(Role::team($resourceId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::delete(Role::team($resourceId, 'project-' . $projectId . '-owner')); } } return $permissions; @@ -564,10 +588,19 @@ class Notifications extends Action { $validator = new UID(); - foreach (['userId', 'teamId'] as $key) { - $value = $recipient[$key] ?? ''; - if ($value !== '' && !$validator->isValid($value)) { - throw new Exception('Invalid console alert ' . $key . ': ' . $validator->getDescription()); + if (!$validator->isValid($recipient['resourceId'])) { + throw new Exception('Invalid console alert resourceId: ' . $validator->getDescription()); + } + } + + /** + * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient + */ + private function validateAlertResource(array $recipient): void + { + foreach (['resourceType', 'resourceId', 'resourceInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'] as $key) { + if ($recipient[$key] === '') { + throw new Exception('Missing notification alert ' . $key); } } } @@ -588,16 +621,16 @@ class Notifications extends Action /** * Splice a 1x1 tracking pixel before the last `` tag (or * append at the end if the body has no closing tag). The pixel - * carries a signed JWT identifying the alert and user, which the + * carries a signed JWT identifying the alert recipient, which the * `/v1/account/alerts/:alertId/track` endpoint verifies before * marking the alert as read. */ - private function injectTrackingPixel(string $body, string $alertId, string $userId, Document $project, string $opensslKey): string + private function injectTrackingPixel(string $body, string $alertId, string $recipientHash, Document $project, string $opensslKey): string { $jwt = (new JWT($opensslKey, 'HS256', ALERT_TRACKING_JWT_TTL, 0)) ->encode([ 'alertId' => $alertId, - 'userId' => $userId, + 'recipientHash' => $recipientHash, 'projectId' => $project->getId(), 'projectInternalId' => $project->getSequence(), 'purpose' => 'alert_track', diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index a5e6cfc672..1cdf9bea7f 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -247,9 +247,9 @@ class Webhooks extends Action } $projectId = $project->getId(); + $projectInternalId = $project->getSequence(); $region = $project->getAttribute('region', 'default'); $webhookId = $webhook->getId(); - $teamId = $project->getAttribute('teamId'); $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); @@ -292,8 +292,12 @@ class Webhooks extends Action $userId, NOTIFICATION_TYPE_CONSOLE, null, + RESOURCE_TYPE_USERS, $userId, - $teamId, + (string) $user->getSequence(), + RESOURCE_TYPE_PROJECTS, + $projectId, + (string) $projectInternalId, ); if (!empty($email)) { @@ -301,8 +305,12 @@ class Webhooks extends Action $email, NOTIFICATION_TYPE_EMAIL, null, + RESOURCE_TYPE_USERS, $userId, - $teamId, + (string) $user->getSequence(), + RESOURCE_TYPE_PROJECTS, + $projectId, + (string) $projectInternalId, ); } } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php index c0eee2d153..d0c7490cd7 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php @@ -10,6 +10,10 @@ class Alerts extends Base 'channel', 'messageId', 'projectId', + 'resourceType', + 'resourceId', + 'parentResourceType', + 'parentResourceId', ]; /** diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index f3aa4332ed..46a17ec675 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -58,12 +58,12 @@ class Console extends Adapter $delivered = 0; foreach ($message->getRecipients() as $recipient) { - $userId = $recipient['userId'] ?? ''; - $teamId = $recipient['teamId'] ?? ''; - $key = $userId !== '' ? $userId : $teamId; + $resourceType = $recipient['resourceType']; + $resourceId = $recipient['resourceId']; + $key = $resourceType . ':' . $resourceId; $messageId = $message->getMessageId(); - $recipientKey = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; + $recipientKey = self::buildRecipientKey($recipient); $recipientHash = $recipient['recipientHash'] ?? \substr(\md5($recipientKey), 0, 16); $documentId = $messageId !== null ? ($recipient['alertId'] ?? \substr($messageId, 0, 19) . '_' . $recipientHash) @@ -72,15 +72,19 @@ class Console extends Adapter try { $document = new Document([ '$id' => $documentId, - '$permissions' => $this->buildPermissions($userId, $teamId, $message->getProjectId() ?? ''), + '$permissions' => $this->buildPermissions($resourceType, $resourceId, $message->getProjectId() ?? ''), 'messageId' => $messageId, 'recipientHash' => $recipientHash, 'type' => $message->getType(), 'channel' => self::TYPE, - 'userId' => $userId, - 'teamId' => $teamId, 'projectId' => $message->getProjectId(), 'projectInternalId' => $message->getProjectInternalId(), + 'resourceType' => $resourceType, + 'resourceId' => $resourceId, + 'resourceInternalId' => $recipient['resourceInternalId'], + 'parentResourceType' => $recipient['parentResourceType'], + 'parentResourceId' => $recipient['parentResourceId'], + 'parentResourceInternalId' => $recipient['parentResourceInternalId'], 'title' => $message->getTitle(), 'body' => $message->getBody(), ]); @@ -106,24 +110,38 @@ class Console extends Adapter /** * @return array */ - private function buildPermissions(string $userId, string $teamId, string $projectId): array + private function buildPermissions(string $resourceType, string $resourceId, string $projectId): array { $permissions = []; - if ($userId !== '') { - $permissions[] = Permission::read(Role::user($userId)); - $permissions[] = Permission::update(Role::user($userId)); - $permissions[] = Permission::delete(Role::user($userId)); + if ($resourceType === RESOURCE_TYPE_USERS) { + $permissions[] = Permission::read(Role::user($resourceId)); + $permissions[] = Permission::update(Role::user($resourceId)); + $permissions[] = Permission::delete(Role::user($resourceId)); } - if ($teamId !== '') { - $permissions[] = Permission::read(Role::team($teamId)); - $permissions[] = Permission::update(Role::team($teamId, 'owner')); - $permissions[] = Permission::delete(Role::team($teamId, 'owner')); + if ($resourceType === RESOURCE_TYPE_TEAMS) { + $permissions[] = Permission::read(Role::team($resourceId)); + $permissions[] = Permission::update(Role::team($resourceId, 'owner')); + $permissions[] = Permission::delete(Role::team($resourceId, 'owner')); if ($projectId !== '') { - $permissions[] = Permission::read(Role::team($teamId, 'project-' . $projectId . '-owner')); - $permissions[] = Permission::update(Role::team($teamId, 'project-' . $projectId . '-owner')); - $permissions[] = Permission::delete(Role::team($teamId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::read(Role::team($resourceId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::update(Role::team($resourceId, 'project-' . $projectId . '-owner')); + $permissions[] = Permission::delete(Role::team($resourceId, 'project-' . $projectId . '-owner')); } } return $permissions; } + + /** + * @param array{address?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient + */ + private static function buildRecipientKey(array $recipient): string + { + return ($recipient['address'] ?? '') + . ':' . $recipient['resourceType'] + . ':' . $recipient['resourceId'] + . ':' . $recipient['resourceInternalId'] + . ':' . $recipient['parentResourceType'] + . ':' . $recipient['parentResourceId'] + . ':' . $recipient['parentResourceInternalId']; + } } diff --git a/src/Appwrite/Utopia/Messaging/Messages/Console.php b/src/Appwrite/Utopia/Messaging/Messages/Console.php index 70573613dc..f56b592af6 100644 --- a/src/Appwrite/Utopia/Messaging/Messages/Console.php +++ b/src/Appwrite/Utopia/Messaging/Messages/Console.php @@ -7,7 +7,7 @@ use Utopia\Messaging\Message; class Console implements Message { /** - * @param array $recipients + * @param array $recipients */ public function __construct( protected array $recipients, @@ -21,7 +21,7 @@ class Console implements Message } /** - * @return array + * @return array */ public function getRecipients(): array { @@ -29,7 +29,7 @@ class Console implements Message } /** - * @return array + * @return array */ public function getTo(): array { diff --git a/src/Appwrite/Utopia/Response/Model/Alert.php b/src/Appwrite/Utopia/Response/Model/Alert.php index 9a9e89e9dd..84f66ecb4c 100644 --- a/src/Appwrite/Utopia/Response/Model/Alert.php +++ b/src/Appwrite/Utopia/Response/Model/Alert.php @@ -47,19 +47,29 @@ class Alert extends Model 'default' => '', 'example' => 'email', ]) - ->addRule('userId', [ + ->addRule('resourceType', [ 'type' => self::TYPE_STRING, - 'description' => 'User this alert is addressed to.', + 'description' => 'Resource type this alert is addressed to.', 'default' => '', - 'example' => '5e5bb8c16897e', - 'required' => false, + 'example' => 'users', ]) - ->addRule('teamId', [ + ->addRule('resourceId', [ 'type' => self::TYPE_STRING, - 'description' => 'Team this alert is addressed to.', + 'description' => 'Resource ID this alert is addressed to.', + 'default' => '', + 'example' => '5e5bb8c16897e', + ]) + ->addRule('parentResourceType', [ + 'type' => self::TYPE_STRING, + 'description' => 'Parent resource type for the alert.', + 'default' => '', + 'example' => 'projects', + ]) + ->addRule('parentResourceId', [ + 'type' => self::TYPE_STRING, + 'description' => 'Parent resource ID for the alert.', 'default' => '', 'example' => '5e5bb8c16897e', - 'required' => false, ]) ->addRule('projectId', [ 'type' => self::TYPE_STRING, diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 5891df8e4b..862264c4d9 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -240,14 +240,14 @@ trait NotificationsBase $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for tracking pixel test'); - $userId = $this->getRoot()['$id']; + $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); // Track endpoint requires `purpose: 'alert_track'` — see C/M7 in // PR #12195 review. Other claim purposes are silently ignored (which // testTrackingPixelRejectsJwtWithoutPurposeClaim covers). $jwt = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, - 'userId' => $userId, + 'recipientHash' => $recipientHash, 'projectId' => $this->getProject()['$id'], 'purpose' => 'alert_track', ]); @@ -298,12 +298,12 @@ trait NotificationsBase $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for the JWT purpose-claim test'); - $userId = $this->getRoot()['$id']; + $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); - // Mint a JWT with valid alertId/userId but NO purpose claim. + // Mint a JWT with valid alertId/recipientHash but NO purpose claim. $jwtNoPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, - 'userId' => $userId, + 'recipientHash' => $recipientHash, 'projectId' => $this->getProject()['$id'], ]); @@ -331,7 +331,7 @@ trait NotificationsBase // Mint a JWT with a wrong purpose value: same expectation, silently rejected. $jwtWrongPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ 'alertId' => $alertId, - 'userId' => $userId, + 'recipientHash' => $recipientHash, 'projectId' => $this->getProject()['$id'], 'purpose' => 'something_else', ]); diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index f20b2100cc..2c6c9e0961 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -182,10 +182,14 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); @@ -223,14 +227,48 @@ class NotificationsTest extends TestCase ]); } - private function recipientHash(string $channel, string $address, string $userId = '', string $teamId = ''): string - { - return \substr(\md5($channel . ':' . $address . ':' . $userId . ':' . $teamId), 0, 16); + private function recipientHash( + string $channel, + string $address, + string $resourceType, + string $resourceId, + string $resourceInternalId, + string $parentResourceType = RESOURCE_TYPE_PROJECTS, + string $parentResourceId = 'project-x', + string $parentResourceInternalId = 'project-internal-x', + ): string { + return \substr(\md5( + $channel + . ':' . $address + . ':' . $resourceType + . ':' . $resourceId + . ':' . $resourceInternalId + . ':' . $parentResourceType + . ':' . $parentResourceId + . ':' . $parentResourceInternalId + ), 0, 16); } - private function alertId(string $messageId, string $channel, string $address, string $userId = '', string $teamId = ''): string + private function alertId(string $messageId, string $channel, string $address, string $resourceType, string $resourceId): string { - return \substr($messageId, 0, 19) . '_' . $this->recipientHash($channel, $address, $userId, $teamId); + return \substr($messageId, 0, 19) . '_' . $this->recipientHash($channel, $address, $resourceType, $resourceId, $resourceId . '-internal'); + } + + /** + * @return array{address: string, channel: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} + */ + private function userRecipient(string $address, string $channel, string $resourceId): array + { + return [ + 'address' => $address, + 'channel' => $channel, + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => $resourceId, + 'resourceInternalId' => $resourceId . '-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', + ]; } public function testDispatchesPerChannelToCorrectAdapter(): void @@ -240,9 +278,9 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL], - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE], - ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-1'), + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), + $this->userRecipient('https://hooks.example.test/in', NOTIFICATION_TYPE_WEBHOOK, 'user-1'), ], 'subject' => 'Hi', 'body' => 'Body', @@ -262,8 +300,8 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE], - ['address' => 'user-2', 'channel' => NOTIFICATION_TYPE_CONSOLE], + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), + $this->userRecipient('user-2', NOTIFICATION_TYPE_CONSOLE, 'user-2'), ], 'subject' => 'Heads up', 'body' => 'Read me', @@ -275,14 +313,19 @@ class NotificationsTest extends TestCase $rows = $this->database->find('alerts'); $this->assertCount(2, $rows); - $userIds = \array_map(static fn (Document $row) => $row->getAttribute('userId'), $rows); - \sort($userIds); - $this->assertSame(['user-1', 'user-2'], $userIds); + $resourceIds = \array_map(static fn (Document $row) => $row->getAttribute('resourceId'), $rows); + \sort($resourceIds); + $this->assertSame(['user-1', 'user-2'], $resourceIds); foreach ($rows as $row) { $this->assertSame(\md5('evt-multi'), $row->getAttribute('messageId')); $this->assertSame('console', $row->getAttribute('channel')); $this->assertSame('project-x', $row->getAttribute('projectId')); + $this->assertSame('project-internal-x', $row->getAttribute('projectInternalId')); + $this->assertSame(RESOURCE_TYPE_USERS, $row->getAttribute('resourceType')); + $this->assertSame(RESOURCE_TYPE_PROJECTS, $row->getAttribute('parentResourceType')); + $this->assertSame('project-x', $row->getAttribute('parentResourceId')); + $this->assertSame('project-internal-x', $row->getAttribute('parentResourceInternalId')); $this->assertSame('Heads up', $row->getAttribute('title')); } } @@ -292,7 +335,7 @@ class NotificationsTest extends TestCase $worker = new SpyNotifications(); $payload = [ 'project' => ['$id' => 'project-x'], - 'recipients' => [['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE]], + 'recipients' => [$this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1')], 'subject' => 'Sub', 'body' => 'B', 'deduplicationKey' => 'dup-key', @@ -372,7 +415,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], - 'recipients' => [['address' => 'https://h.example.test', 'channel' => NOTIFICATION_TYPE_WEBHOOK]], + 'recipients' => [$this->userRecipient('https://h.example.test', NOTIFICATION_TYPE_WEBHOOK, 'user-1')], 'subject' => 's', 'body' => 'b', 'deduplicationKey' => 'err-1', @@ -402,8 +445,8 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], - ['address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK], + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), + $this->userRecipient('https://hooks.example.test/in', NOTIFICATION_TYPE_WEBHOOK, 'user-1'), ], 'subject' => 'Sub', 'body' => 'B', @@ -442,7 +485,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), ], 'subject' => 'Heads up', 'body' => 'console body', @@ -468,7 +511,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), ], 'subject' => 'Heads up', 'body' => 'console body', @@ -492,8 +535,8 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - ['address' => 'user-1', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-1'], - ['address' => 'user-2', 'channel' => NOTIFICATION_TYPE_CONSOLE, 'userId' => 'user-2'], + $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), + $this->userRecipient('user-2', NOTIFICATION_TYPE_CONSOLE, 'user-2'), ], 'subject' => 'Heads up', 'body' => 'console body', @@ -514,7 +557,7 @@ class NotificationsTest extends TestCase $this->assertCount(2, \array_unique($ids), 'recipient suffixes must keep $id values distinct'); } - public function testRecipientStructRoundtripsUserIdAndTeamId(): void + public function testRecipientStructRoundtripsResourceFields(): void { $worker = new CountingPersistAlertNotifications(); @@ -524,8 +567,12 @@ class NotificationsTest extends TestCase [ 'address' => 'console-recipient', 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'userId' => 'u1', - 'teamId' => 't1', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'u1', + 'resourceInternalId' => 'u1-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', ], ], 'subject' => 'Heads up', @@ -537,8 +584,12 @@ class NotificationsTest extends TestCase $rows = $this->database->find('alerts'); $this->assertCount(1, $rows); - $this->assertSame('u1', $rows[0]->getAttribute('userId')); - $this->assertSame('t1', $rows[0]->getAttribute('teamId')); + $this->assertSame(RESOURCE_TYPE_USERS, $rows[0]->getAttribute('resourceType')); + $this->assertSame('u1', $rows[0]->getAttribute('resourceId')); + $this->assertSame('u1-internal', $rows[0]->getAttribute('resourceInternalId')); + $this->assertSame(RESOURCE_TYPE_PROJECTS, $rows[0]->getAttribute('parentResourceType')); + $this->assertSame('project-x', $rows[0]->getAttribute('parentResourceId')); + $this->assertSame('project-internal-x', $rows[0]->getAttribute('parentResourceInternalId')); } public function testTrackingPixelInjectedIntoEmailHtml(): void @@ -563,11 +614,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'user@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-7', - ], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-7'), ], 'subject' => 'Heads up', 'body' => 'plain body', @@ -618,19 +665,10 @@ class NotificationsTest extends TestCase try { $worker = new CountingPersistAlertNotifications(); - // All four unique-index fields populated so the - // `_key_recipient` UNIQUE composite (messageId, channel, - // userId, teamId) actually fires. SQL UNIQUE semantics treat - // NULL as not-equal, so any null in the tuple disables it. $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'user@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-7', - 'teamId' => 'team-7', - ], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-7'), ], 'subject' => 'Heads up', 'body' => 'b', @@ -648,8 +686,12 @@ class NotificationsTest extends TestCase $recipient = [ 'address' => 'user@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-7', - 'teamId' => 'team-7', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'user-7', + 'resourceInternalId' => 'user-7-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', ]; // Second invocation with the SAME messageId/recipient. The @@ -672,12 +714,16 @@ class NotificationsTest extends TestCase '$id' => 'sibling-id-' . \uniqid(), '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, - 'recipientHash' => $this->recipientHash(NOTIFICATION_TYPE_EMAIL, 'user@example.test', 'user-7', 'team-7'), + 'recipientHash' => $this->recipientHash(NOTIFICATION_TYPE_EMAIL, 'user@example.test', RESOURCE_TYPE_USERS, 'user-7', 'user-7-internal'), 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-7', - 'teamId' => 'team-7', 'projectId' => 'project-x', 'projectInternalId' => 'project-internal-x', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'user-7', + 'resourceInternalId' => 'user-7-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', 'title' => 'sibling', 'body' => 'sibling', 'read' => false, @@ -700,7 +746,7 @@ class NotificationsTest extends TestCase } } - public function testPersistAlertReturnsAlertIdAndStoresUserId(): void + public function testPersistAlertReturnsAlertIdAndStoresResource(): void { $spy = new SpyEmailAdapter(); $this->registry->set('smtp', static fn () => $spy); @@ -714,11 +760,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'user@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-7', - ], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-7'), ], 'subject' => 'Heads up', 'body' => 'b', @@ -736,7 +778,9 @@ class NotificationsTest extends TestCase $alertId = $worker->persistedIds[0]; $row = $this->database->getDocument('alerts', $alertId); $this->assertFalse($row->isEmpty(), 'persistAlert must return an id resolvable via getDocument'); - $this->assertSame('user-7', $row->getAttribute('userId')); + $this->assertSame(RESOURCE_TYPE_USERS, $row->getAttribute('resourceType')); + $this->assertSame('user-7', $row->getAttribute('resourceId')); + $this->assertSame('user-7-internal', $row->getAttribute('resourceInternalId')); $this->assertFalse($row->getAttribute('read'), 'new alerts default to unread'); $this->assertSame(\md5('persist-email'), $row->getAttribute('messageId')); } @@ -762,11 +806,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'user@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-9', - ], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-9'), ], 'subject' => 'Subj', 'body' => 'Body', @@ -809,7 +849,7 @@ class NotificationsTest extends TestCase Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'retry must persist exactly one alert row'); - $this->assertSame('user-9', $rows[0]->getAttribute('userId')); + $this->assertSame('user-9', $rows[0]->getAttribute('resourceId')); $this->assertFalse($rows[0]->getAttribute('read')); } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); @@ -828,18 +868,8 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x', '$sequence' => 'project-internal-x'], 'recipients' => [ - [ - 'address' => 'user@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-9', - 'teamId' => 'team-9', - ], - [ - 'address' => 'user-9', - 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'userId' => 'user-9', - 'teamId' => 'team-9', - ], + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-9'), + $this->userRecipient('user-9', NOTIFICATION_TYPE_CONSOLE, 'user-9'), ], 'subject' => 'Subj', 'body' => 'Body', @@ -971,13 +1001,13 @@ class NotificationsTest extends TestCase // First "sendAlert" pass: a single user, webhook A. $event - ->setProject(new Document(['$id' => 'project-a'])) + ->setProject(new Document(['$id' => 'project-a', '$sequence' => 'project-a-internal'])) ->setSubject('Webhook A paused') ->setPreview('Webhook A preview') ->setBody('Webhook A body') ->setDeduplicationKey('webhook:hookA:paused:10') - ->addRecipient('alice@example.test', NOTIFICATION_TYPE_EMAIL, null, 'user-A', 'team-1') - ->addRecipient('user-A', NOTIFICATION_TYPE_CONSOLE, null, 'user-A', 'team-1') + ->addRecipient('alice@example.test', NOTIFICATION_TYPE_EMAIL, null, RESOURCE_TYPE_USERS, 'user-A', 'user-A-internal', RESOURCE_TYPE_PROJECTS, 'project-a', 'project-a-internal') + ->addRecipient('user-A', NOTIFICATION_TYPE_CONSOLE, null, RESOURCE_TYPE_USERS, 'user-A', 'user-A-internal', RESOURCE_TYPE_PROJECTS, 'project-a', 'project-a-internal') ->trigger(); // Mirror the production sendAlert: reset before configuring next alert. @@ -985,13 +1015,13 @@ class NotificationsTest extends TestCase // Second pass: a different user/webhook entirely. $event - ->setProject(new Document(['$id' => 'project-b'])) + ->setProject(new Document(['$id' => 'project-b', '$sequence' => 'project-b-internal'])) ->setSubject('Webhook B paused') ->setPreview('Webhook B preview') ->setBody('Webhook B body') ->setDeduplicationKey('webhook:hookB:paused:10') - ->addRecipient('bob@example.test', NOTIFICATION_TYPE_EMAIL, null, 'user-B', 'team-2') - ->addRecipient('user-B', NOTIFICATION_TYPE_CONSOLE, null, 'user-B', 'team-2') + ->addRecipient('bob@example.test', NOTIFICATION_TYPE_EMAIL, null, RESOURCE_TYPE_USERS, 'user-B', 'user-B-internal', RESOURCE_TYPE_PROJECTS, 'project-b', 'project-b-internal') + ->addRecipient('user-B', NOTIFICATION_TYPE_CONSOLE, null, RESOURCE_TYPE_USERS, 'user-B', 'user-B-internal', RESOURCE_TYPE_PROJECTS, 'project-b', 'project-b-internal') ->trigger(); $events = $publisher->getEvents('v1-notifications'); @@ -1044,11 +1074,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'happy@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-happy', - ], + $this->userRecipient('happy@example.test', NOTIFICATION_TYPE_EMAIL, 'user-happy'), ], 'subject' => 'Welcome aboard', 'body' => 'plain body', @@ -1086,7 +1112,7 @@ class NotificationsTest extends TestCase ]); $this->assertCount(1, $rows); $row = $rows[0]; - $this->assertSame('user-happy', $row->getAttribute('userId')); + $this->assertSame('user-happy', $row->getAttribute('resourceId')); $this->assertSame(NOTIFICATION_TYPE_EMAIL, $row->getAttribute('channel')); $this->assertFalse($row->getAttribute('read')); @@ -1101,7 +1127,7 @@ class NotificationsTest extends TestCase * * The Console adapter writes the alert directly; the action loop must * NOT call `persistAlert` for console recipients. Permissions must grant - * the recipient user AND team owners read/update/delete. + * the recipient resource read/update/delete access. */ public function testConsoleChannelHappyPath(): void { @@ -1113,8 +1139,12 @@ class NotificationsTest extends TestCase [ 'address' => 'console-recipient', 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'userId' => 'u1', - 'teamId' => 't1', + 'resourceType' => RESOURCE_TYPE_TEAMS, + 'resourceId' => 't1', + 'resourceInternalId' => 't1-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', ], ], 'subject' => 'Heads up', @@ -1130,20 +1160,18 @@ class NotificationsTest extends TestCase $this->assertCount(1, $rows, 'console adapter must write exactly one alert'); $row = $rows[0]; - $this->assertSame('u1', $row->getAttribute('userId')); - $this->assertSame('t1', $row->getAttribute('teamId')); + $this->assertSame(RESOURCE_TYPE_TEAMS, $row->getAttribute('resourceType')); + $this->assertSame('t1', $row->getAttribute('resourceId')); + $this->assertSame('t1-internal', $row->getAttribute('resourceInternalId')); $this->assertSame(NOTIFICATION_TYPE_CONSOLE, $row->getAttribute('channel')); $this->assertFalse($row->getAttribute('read')); $messageId = \md5('happy-console'); - $expectedId = $this->alertId($messageId, NOTIFICATION_TYPE_CONSOLE, 'console-recipient', 'u1', 't1'); + $expectedId = $this->alertId($messageId, NOTIFICATION_TYPE_CONSOLE, 'console-recipient', RESOURCE_TYPE_TEAMS, 't1'); $this->assertSame($expectedId, $row->getId(), 'row $id must match adapter suffix scheme'); $this->assertLessThanOrEqual(36, \strlen($row->getId()), 'alert ids must pass the UID route validator'); $permissions = $row->getPermissions(); - $this->assertContains(Permission::read(Role::user('u1')), $permissions); - $this->assertContains(Permission::update(Role::user('u1')), $permissions); - $this->assertContains(Permission::delete(Role::user('u1')), $permissions); $this->assertContains(Permission::read(Role::team('t1')), $permissions); $this->assertContains(Permission::update(Role::team('t1', 'owner')), $permissions); $this->assertContains(Permission::delete(Role::team('t1', 'owner')), $permissions); @@ -1158,11 +1186,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'user-preview', - 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'userId' => 'user-preview', - ], + $this->userRecipient('user-preview', NOTIFICATION_TYPE_CONSOLE, 'user-preview'), ], 'subject' => 'Webhook {{name}} paused', 'preview' => 'Plain alert for {{name}}.', @@ -1190,11 +1214,7 @@ class NotificationsTest extends TestCase $payload = [ 'project' => ['$id' => 'project-x'], 'recipients' => [ - [ - 'address' => 'missing-smtp@example.test', - 'channel' => NOTIFICATION_TYPE_EMAIL, - 'userId' => 'user-smtp', - ], + $this->userRecipient('missing-smtp@example.test', NOTIFICATION_TYPE_EMAIL, 'user-smtp'), ], 'subject' => 'No SMTP', 'body' => 'Body', @@ -1226,7 +1246,7 @@ class NotificationsTest extends TestCase ]; $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid console alert userId'); + $this->expectExceptionMessage('Invalid console alert resourceId'); $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); } @@ -1287,7 +1307,12 @@ class NotificationsTest extends TestCase 'address' => 'https://hooks.example.test/in', 'channel' => NOTIFICATION_TYPE_WEBHOOK, 'signatureKey' => 'tenant-secret', - 'userId' => 'user-h', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'user-h', + 'resourceInternalId' => 'user-h-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-x', + 'parentResourceInternalId' => 'project-internal-x', ], ], 'subject' => 'Heads up', diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index 0bb6b4574d..dc84baddf3 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -38,10 +38,14 @@ class ConsoleTest extends TestCase $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'userId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'teamId', Database::VAR_STRING, 255, false); $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); } @@ -56,16 +60,36 @@ class ConsoleTest extends TestCase * Mirrors the per-recipient `$id` derivation in * Appwrite\Utopia\Messaging\Adapter\Console::process(). */ - private function alertId(string $messageId, string $userId = '', string $teamId = ''): string + private function alertId(string $messageId, string $address, string $resourceType, string $resourceId): string { - $key = $userId !== '' ? 'user:' . $userId : 'team:' . $teamId; + $key = $address + . ':' . $resourceType + . ':' . $resourceId + . ':' . $resourceId . '-internal' + . ':projects:project-1:project-internal-1'; return \substr($messageId, 0, 19) . '_' . \substr(\md5($key), 0, 16); } + /** + * @return array{address: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} + */ + private function recipient(string $address, string $resourceId, string $resourceType = RESOURCE_TYPE_USERS): array + { + return [ + 'address' => $address, + 'resourceType' => $resourceType, + 'resourceId' => $resourceId, + 'resourceInternalId' => $resourceId . '-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-1', + 'parentResourceInternalId' => 'project-internal-1', + ]; + } + public function testWritesAlertWithCorrectSchema(): void { $message = new ConsoleMessage( - recipients: [['userId' => 'user-1']], + recipients: [$this->recipient('user-1', 'user-1')], title: 'Hello', body: 'World', type: 'info', @@ -79,13 +103,18 @@ class ConsoleTest extends TestCase $this->assertSame(1, $result['deliveredTo']); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-aaa', userId: 'user-1')); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-aaa', 'user-1', RESOURCE_TYPE_USERS, 'user-1')); $this->assertFalse($stored->isEmpty()); $this->assertSame('msg-aaa', $stored->getAttribute('messageId')); $this->assertSame('console', $stored->getAttribute('channel')); - $this->assertSame('user-1', $stored->getAttribute('userId')); $this->assertSame('project-1', $stored->getAttribute('projectId')); $this->assertSame('project-internal-1', $stored->getAttribute('projectInternalId')); + $this->assertSame(RESOURCE_TYPE_USERS, $stored->getAttribute('resourceType')); + $this->assertSame('user-1', $stored->getAttribute('resourceId')); + $this->assertSame('user-1-internal', $stored->getAttribute('resourceInternalId')); + $this->assertSame(RESOURCE_TYPE_PROJECTS, $stored->getAttribute('parentResourceType')); + $this->assertSame('project-1', $stored->getAttribute('parentResourceId')); + $this->assertSame('project-internal-1', $stored->getAttribute('parentResourceInternalId')); $this->assertSame('Hello', $stored->getAttribute('title')); $this->assertSame('World', $stored->getAttribute('body')); $this->assertSame('info', $stored->getAttribute('type')); @@ -94,7 +123,7 @@ class ConsoleTest extends TestCase public function testUserPermissionsScopedToRecipient(): void { $message = new ConsoleMessage( - recipients: [['userId' => 'user-2']], + recipients: [$this->recipient('user-2', 'user-2')], title: 'Title', body: 'Body', messageId: ID::custom('msg-perms-user'), @@ -104,7 +133,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-perms-user', userId: 'user-2')); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-perms-user', 'user-2', RESOURCE_TYPE_USERS, 'user-2')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::user('user-2')), $permissions); @@ -115,7 +144,7 @@ class ConsoleTest extends TestCase public function testTeamRecipientGrantsTeamReadAndOwnerWrite(): void { $message = new ConsoleMessage( - recipients: [['teamId' => 'team-9']], + recipients: [$this->recipient('team-9', 'team-9', RESOURCE_TYPE_TEAMS)], title: 'Heads up', body: '...', messageId: ID::custom('msg-team'), @@ -125,7 +154,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-team', teamId: 'team-9')); + $stored = $this->database->getDocument('alerts', $this->alertId('msg-team', 'team-9', RESOURCE_TYPE_TEAMS, 'team-9')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::team('team-9')), $permissions); @@ -140,8 +169,8 @@ class ConsoleTest extends TestCase { $message = new ConsoleMessage( recipients: [ - ['userId' => 'a'], - ['userId' => 'b'], + $this->recipient('a', 'a'), + $this->recipient('b', 'b'), ], title: 'Heads up', body: 'multi', @@ -155,8 +184,8 @@ class ConsoleTest extends TestCase $this->assertSame(2, $result['deliveredTo']); - $idA = $this->alertId('same-msg', userId: 'a'); - $idB = $this->alertId('same-msg', userId: 'b'); + $idA = $this->alertId('same-msg', 'a', RESOURCE_TYPE_USERS, 'a'); + $idB = $this->alertId('same-msg', 'b', RESOURCE_TYPE_USERS, 'b'); $this->assertNotSame($idA, $idB, 'recipient suffixes must be distinct'); @@ -167,8 +196,8 @@ class ConsoleTest extends TestCase $this->assertSame('same-msg', $rowA->getAttribute('messageId')); $this->assertSame('same-msg', $rowB->getAttribute('messageId')); - $this->assertSame('a', $rowA->getAttribute('userId')); - $this->assertSame('b', $rowB->getAttribute('userId')); + $this->assertSame('a', $rowA->getAttribute('resourceId')); + $this->assertSame('b', $rowB->getAttribute('resourceId')); $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{16}$/', $idA); $this->assertMatchesRegularExpression('/^same-msg_[0-9a-f]{16}$/', $idB); @@ -195,7 +224,7 @@ class ConsoleTest extends TestCase { $userId = 'user-dup'; $messageId = 'msg-dup'; - $documentId = $this->alertId($messageId, userId: $userId); + $documentId = $this->alertId($messageId, $userId, RESOURCE_TYPE_USERS, $userId); // Pre-insert an alert with the SAME id the adapter will compute. The // adapter's createDocument will hit the primary-key DuplicateException @@ -204,17 +233,22 @@ class ConsoleTest extends TestCase '$id' => $documentId, '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, - 'recipientHash' => \substr(\md5('user:' . $userId), 0, 16), + 'recipientHash' => \substr(\md5($userId . ':' . RESOURCE_TYPE_USERS . ':' . $userId . ':' . $userId . '-internal:projects:project-1:project-internal-1'), 0, 16), 'channel' => 'console', - 'userId' => $userId, 'projectId' => 'project-x', 'projectInternalId' => 'project-internal-x', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => $userId, + 'resourceInternalId' => $userId . '-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-1', + 'parentResourceInternalId' => 'project-internal-1', 'title' => 'pre-existing', 'body' => 'pre-existing', ])); $message = new ConsoleMessage( - recipients: [['userId' => $userId]], + recipients: [$this->recipient($userId, $userId)], title: 'Same alert resent', body: 'b', messageId: ID::custom($messageId), diff --git a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php index f396954949..a1947eaaea 100644 --- a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php @@ -161,6 +161,18 @@ class WebhookTest extends TestCase $this->expectException(\Exception::class); $this->expectExceptionMessage('Invalid message type.'); - $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Console(recipients: [['userId' => 'u']], title: 't', body: 'b')); + $adapter->send(new \Appwrite\Utopia\Messaging\Messages\Console( + recipients: [[ + 'address' => 'u', + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'u', + 'resourceInternalId' => 'u-internal', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project', + 'parentResourceInternalId' => 'project-internal', + ]], + title: 't', + body: 'b', + )); } } From d76ef18b57190fa2a292f6db6e3d36c01cd1b78e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 22:35:28 +1200 Subject: [PATCH 43/80] fix: provision and clean up alert records --- app/config/collections/platform.php | 9 +- src/Appwrite/Migration/Version/V24.php | 4 + src/Appwrite/Platform/Workers/Deletes.php | 11 +++ .../unit/Migration/MigrationVersionsTest.php | 47 ++++++++++ tests/unit/Platform/Workers/DeletesTest.php | 91 +++++++++++++++++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Platform/Workers/DeletesTest.php diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 43d31ee883..61e85a8f09 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1261,9 +1261,16 @@ $platformCollections = [ '$id' => ID::custom('_key_projectInternalId'), 'type' => Database::INDEX_KEY, 'attributes' => ['projectInternalId'], - 'lengths' => [Database::LENGTH_KEY], + 'lengths' => [0], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => ID::custom('_key_project'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['projectId', 'projectInternalId'], + 'lengths' => [Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC], + ], [ '$id' => ID::custom('_key_project_resource'), 'type' => Database::INDEX_KEY, diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index 204a7a22e4..ffb83417ce 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -88,6 +88,10 @@ class V24 extends Migration $collections = $this->collections[$collectionType]; + if ($collectionType === 'console') { + $this->createCollection('alerts'); + } + foreach ($collections as $collection) { $id = $collection['$id']; diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index a58fc48098..78380abc87 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -732,6 +732,17 @@ class Deletes extends Action Console::error('Failed to delete schedules: ' . $th->getMessage()); } + // Delete Alerts + try { + $this->deleteByGroup('alerts', [ + Query::equal('projectId', [$projectId]), + Query::equal('projectInternalId', [$projectInternalId]), + Query::orderAsc() + ], $dbForPlatform); + } catch (Throwable $th) { + Console::error('Failed to delete alerts: ' . $th->getMessage()); + } + // Delete Advisor insights try { $this->deleteByGroup('insights', [ diff --git a/tests/unit/Migration/MigrationVersionsTest.php b/tests/unit/Migration/MigrationVersionsTest.php index 3c8bae7222..ba3a36aa5e 100644 --- a/tests/unit/Migration/MigrationVersionsTest.php +++ b/tests/unit/Migration/MigrationVersionsTest.php @@ -3,7 +3,14 @@ namespace Tests\Unit\Migration; use Appwrite\Migration\Migration; +use Appwrite\Migration\Version\V24; use PHPUnit\Framework\TestCase; +use Utopia\Cache\Adapter\None as NoCache; +use Utopia\Cache\Cache; +use Utopia\Database\Adapter\Memory; +use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Validator\Authorization; class MigrationVersionsTest extends TestCase { @@ -24,4 +31,44 @@ class MigrationVersionsTest extends TestCase $this->assertArrayHasKey(APP_VERSION_STABLE, Migration::$versions); } } + + public function testV24CreatesAlertsCollectionForConsoleProject(): void + { + require_once __DIR__ . '/../../../app/init.php'; + + $authorization = new Authorization(); + $database = new Database(new Memory(), new Cache(new NoCache())); + $database + ->setAuthorization($authorization) + ->setDatabase('migrationV24') + ->setNamespace('migration_' . \uniqid()); + $database->create(); + + $migration = new V24(); + $migration->setProject( + new Document(['$id' => 'console', '$sequence' => 'console']), + $database, + $database, + $authorization, + ); + + $migrateCollections = new \ReflectionMethod($migration, 'migrateCollections'); + \ob_start(); + try { + $migrateCollections->invoke($migration); + } finally { + \ob_end_clean(); + } + + $collection = $database->getCollection('alerts'); + $this->assertFalse($collection->isEmpty()); + + $attributes = []; + foreach ($collection->getAttribute('attributes', []) as $attribute) { + $id = $attribute instanceof Document ? $attribute->getAttribute('$id') : ($attribute['$id'] ?? ''); + $attributes[$id] = $attribute; + } + $this->assertArrayHasKey('resourceInternalId', $attributes); + $this->assertArrayHasKey('parentResourceInternalId', $attributes); + } } diff --git a/tests/unit/Platform/Workers/DeletesTest.php b/tests/unit/Platform/Workers/DeletesTest.php new file mode 100644 index 0000000000..04941b422b --- /dev/null +++ b/tests/unit/Platform/Workers/DeletesTest.php @@ -0,0 +1,91 @@ + 'project-1', + '$sequence' => 'project-internal-1', + 'database' => 'mysql://localhost/appwrite', + ]); + + $worker = new class () extends Deletes { + /** + * @var array> + */ + public array $groups = []; + + public function runProjectDelete( + Database $database, + Document $project, + callable $getProjectDB, + callable $getDatabasesDB, + Device $device, + CertificatesAdapter $certificates, + ): void { + $this->deleteProject( + $database, + $getProjectDB, + $getDatabasesDB, + $device, + $device, + $device, + $device, + $device, + $certificates, + $project, + ); + } + + protected function deleteByGroup(string $collection, array $queries, Database $database, ?callable $callback = null): void + { + $this->groups[$collection] = $queries; + } + }; + $getProjectDB = static fn () => throw new \RuntimeException('stop'); + $getDatabasesDB = static fn () => $database; + + try { + $worker->runProjectDelete( + $database, + $project, + $getProjectDB, + $getDatabasesDB, + $this->createStub(Device::class), + $this->createStub(CertificatesAdapter::class), + ); + } catch (\RuntimeException $exception) { + $this->assertSame('stop', $exception->getMessage()); + } + + $this->assertArrayHasKey('alerts', $worker->groups); + + $queries = $worker->groups['alerts']; + $this->assertSame(Query::TYPE_EQUAL, $queries[0]->getMethod()); + $this->assertSame('projectId', $queries[0]->getAttribute()); + $this->assertSame(['project-1'], $queries[0]->getValues()); + + $this->assertSame(Query::TYPE_EQUAL, $queries[1]->getMethod()); + $this->assertSame('projectInternalId', $queries[1]->getAttribute()); + $this->assertSame(['project-internal-1'], $queries[1]->getValues()); + + $this->assertSame(Query::TYPE_ORDER_ASC, $queries[2]->getMethod()); + } +} From 5a2c5d6b550768024ed8e9c487b9426951a97fe3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 16 May 2026 10:44:19 +0000 Subject: [PATCH 44/80] refactor(account): patch alerts via read boolean Agent-Logs-Url: https://github.com/appwrite/appwrite/sessions/d60871ef-6caf-48b7-b18a-4db70377eb20 Co-authored-by: abnegate <5857008+abnegate@users.noreply.github.com> --- .../Account/Http/Alerts/{Read => }/Update.php | 17 ++++++++++------- .../Platform/Modules/Account/Services/Http.php | 4 ++-- .../Notifications/NotificationsBase.php | 10 +++++----- 3 files changed, 17 insertions(+), 14 deletions(-) rename src/Appwrite/Platform/Modules/Account/Http/Alerts/{Read => }/Update.php (86%) diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php similarity index 86% rename from src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php rename to src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php index 83885fb927..30555e247e 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Read/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/account/alerts/:alertId/read') - ->desc('Mark alert read') + ->setHttpPath('/v1/account/alerts/:alertId') + ->desc('Update alert') ->groups(['api', 'account']) ->label('scope', 'account') ->label('sdk', new Method( namespace: 'account', group: 'alerts', - name: 'updateAlertRead', - description: '/docs/references/account/update-alert-read.md', + name: 'updateAlert', + description: '/docs/references/account/update-alert.md', auth: [AuthType::SESSION, AuthType::JWT], responses: [ new SDKResponse( @@ -45,6 +46,7 @@ class Update extends Action ] )) ->param('alertId', '', new UID(), 'Alert ID.') + ->param('read', null, new Boolean(), 'Alert read status.') ->inject('response') ->inject('dbForPlatform') ->inject('authorization') @@ -55,6 +57,7 @@ class Update extends Action public function action( string $alertId, + bool $read, Response $response, Database $dbForPlatform, Authorization $authorization, @@ -76,7 +79,7 @@ class Update extends Action } $updated = $dbForPlatform->updateDocument('alerts', $alertId, new Document([ - 'read' => true, + 'read' => $read, ])); $response->dynamic($updated, Response::MODEL_ALERT); diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index 1547412dee..c7d69fa45c 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,8 +12,8 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; -use Appwrite\Platform\Modules\Account\Http\Alerts\Read\Update as UpdateAlertRead; use Appwrite\Platform\Modules\Account\Http\Alerts\Track\Get as TrackAlert; +use Appwrite\Platform\Modules\Account\Http\Alerts\Update as UpdateAlert; use Appwrite\Platform\Modules\Account\Http\Alerts\XList as ListAlerts; use Utopia\Platform\Service; @@ -34,7 +34,7 @@ class Http extends Service ->addAction(CreateChallenge::getName(), new CreateChallenge()) ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) ->addAction(ListAlerts::getName(), new ListAlerts()) - ->addAction(UpdateAlertRead::getName(), new UpdateAlertRead()) + ->addAction(UpdateAlert::getName(), new UpdateAlert()) ->addAction(TrackAlert::getName(), new TrackAlert()); } } diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 862264c4d9..9c82478338 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -23,7 +23,7 @@ use Utopia\System\System; * The alerts portion exercises the full webhook-paused fanout end-to-end: * * - GET /v1/account/alerts (empty + populated) - * - PATCH /v1/account/alerts/:alertId/read (happy + unauthorized) + * - PATCH /v1/account/alerts/:alertId (happy + unauthorized) * - GET /v1/account/alerts/:alertId/track (valid JWT + invalid JWT) * * Dedup, per-channel dispatch, and webhook signing are covered by: @@ -117,9 +117,9 @@ trait NotificationsBase $patch = $this->client->call( Client::METHOD_PATCH, - '/account/alerts/' . $alertId . '/read', + '/account/alerts/' . $alertId, $this->getConsoleAlertHeaders(), - [] + ['read' => true] ); $this->assertSame(200, $patch['headers']['status-code']); @@ -153,7 +153,7 @@ trait NotificationsBase $unauthorized = $this->client->call( Client::METHOD_PATCH, - '/account/alerts/' . $alertId . '/read', + '/account/alerts/' . $alertId, [ 'origin' => 'http://localhost', 'content-type' => 'application/json', @@ -161,7 +161,7 @@ trait NotificationsBase 'x-appwrite-project' => 'console', 'x-appwrite-mode' => 'admin', ], - [] + ['read' => true] ); $this->assertSame(401, $unauthorized['headers']['status-code']); From c3642804e6a86b3b19d28b854490a2e80c896012 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 22:44:39 +1200 Subject: [PATCH 45/80] fix: repair notifications e2e fixture --- tests/e2e/Services/Notifications/NotificationsBase.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 9c82478338..3e635ee157 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -159,7 +159,6 @@ trait NotificationsBase 'content-type' => 'application/json', 'cookie' => 'a_session_console=' . $stranger['session'], 'x-appwrite-project' => 'console', - 'x-appwrite-mode' => 'admin', ], ['read' => true] ); @@ -416,7 +415,6 @@ trait NotificationsBase 'content-type' => 'application/json', 'cookie' => 'a_session_console=' . $this->getRoot()['session'], 'x-appwrite-project' => 'console', - 'x-appwrite-mode' => 'admin', ]; } @@ -446,7 +444,7 @@ trait NotificationsBase 'webhookId' => ID::unique(), 'name' => 'Failing Webhook ' . \uniqid(), 'events' => ['users.*.create'], - 'url' => 'http://127.0.0.1:1/', + 'url' => 'http://request-catcher-webhook:1/', 'tls' => false, ]); From db7210eb698e77dc8ee9813053c4c83e97dadf9c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 22:47:17 +1200 Subject: [PATCH 46/80] docs: update alert reference --- docs/references/account/update-alert-read.md | 1 - docs/references/account/update-alert.md | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 docs/references/account/update-alert-read.md create mode 100644 docs/references/account/update-alert.md diff --git a/docs/references/account/update-alert-read.md b/docs/references/account/update-alert-read.md deleted file mode 100644 index 782b834326..0000000000 --- a/docs/references/account/update-alert-read.md +++ /dev/null @@ -1 +0,0 @@ -Mark an alert as read for the currently logged in user. diff --git a/docs/references/account/update-alert.md b/docs/references/account/update-alert.md new file mode 100644 index 0000000000..288a3d1b1a --- /dev/null +++ b/docs/references/account/update-alert.md @@ -0,0 +1 @@ +Update an alert for the currently logged in user. Use the read parameter to mark the alert as read or unread. From 90b7d6ac2644fab0959944c9b8af1ae6331bbc8b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 23:14:34 +1200 Subject: [PATCH 47/80] fix: trim alert indexes --- app/config/collections/platform.php | 54 +++---------------- .../unit/Migration/MigrationVersionsTest.php | 17 ++++++ 2 files changed, 23 insertions(+), 48 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 61e85a8f09..262161a0d5 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1243,27 +1243,6 @@ $platformCollections = [ 'lengths' => [Database::LENGTH_KEY, 64, 64], 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], - [ - '$id' => ID::custom('_key_resource_read'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceType', 'resourceId', 'read'], - 'lengths' => [64, Database::LENGTH_KEY, 0], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_projectId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['projectId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_projectInternalId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['projectInternalId'], - 'lengths' => [0], - 'orders' => [Database::ORDER_ASC], - ], [ '$id' => ID::custom('_key_project'), 'type' => Database::INDEX_KEY, @@ -1274,37 +1253,16 @@ $platformCollections = [ [ '$id' => ID::custom('_key_project_resource'), 'type' => Database::INDEX_KEY, - 'attributes' => ['projectInternalId', 'resourceType', 'resourceId'], - 'lengths' => [0, 64, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['projectId', 'projectInternalId', 'resourceType', 'resourceId', 'resourceInternalId'], + 'lengths' => [Database::LENGTH_KEY, 0, 64, Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_project_parent_resource'), 'type' => Database::INDEX_KEY, - 'attributes' => ['projectInternalId', 'parentResourceType', 'parentResourceId'], - 'lengths' => [0, 64, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_resource_type'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceType', 'resourceId', 'type'], - 'lengths' => [64, Database::LENGTH_KEY, 64], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_resource_channel'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceType', 'resourceId', 'channel'], - 'lengths' => [64, Database::LENGTH_KEY, 64], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_resource_messageId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['resourceType', 'resourceId', 'messageId'], - 'lengths' => [64, Database::LENGTH_KEY, Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], + 'attributes' => ['projectId', 'projectInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'], + 'lengths' => [Database::LENGTH_KEY, 0, 64, Database::LENGTH_KEY, 0], + 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], ], ], ], diff --git a/tests/unit/Migration/MigrationVersionsTest.php b/tests/unit/Migration/MigrationVersionsTest.php index ba3a36aa5e..499c2fdeb3 100644 --- a/tests/unit/Migration/MigrationVersionsTest.php +++ b/tests/unit/Migration/MigrationVersionsTest.php @@ -70,5 +70,22 @@ class MigrationVersionsTest extends TestCase } $this->assertArrayHasKey('resourceInternalId', $attributes); $this->assertArrayHasKey('parentResourceInternalId', $attributes); + + $indexes = []; + foreach ($collection->getAttribute('indexes', []) as $index) { + $id = $index instanceof Document ? $index->getAttribute('$id') : ($index['$id'] ?? ''); + $indexes[$id] = $index instanceof Document ? $index->getAttribute('attributes') : ($index['attributes'] ?? []); + } + + $this->assertSame([ + '_key_messageId', + '_key_recipient', + '_key_project', + '_key_project_resource', + '_key_project_parent_resource', + ], \array_keys($indexes)); + $this->assertSame(['projectId', 'projectInternalId'], $indexes['_key_project']); + $this->assertSame(['projectId', 'projectInternalId', 'resourceType', 'resourceId', 'resourceInternalId'], $indexes['_key_project_resource']); + $this->assertSame(['projectId', 'projectInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'], $indexes['_key_project_parent_resource']); } } From fde18b7c3938613c415f5f81ee784df9894cbe00 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 16 May 2026 23:49:26 +1200 Subject: [PATCH 48/80] refactor: publish notification messages --- app/init/resources.php | 5 + app/init/resources/request.php | 2 - app/init/worker/message.php | 9 +- src/Appwrite/Event/Message/Notification.php | 116 ++++++ src/Appwrite/Event/Notification.php | 344 ------------------ src/Appwrite/Event/Publisher/Notification.php | 27 ++ .../Health/Http/Health/Queue/Failed/Get.php | 8 +- .../Http/Health/Queue/Notifications/Get.php | 8 +- src/Appwrite/Platform/Workers/Webhooks.php | 115 +++--- tests/unit/Event/Message/NotificationTest.php | 84 +++++ tests/unit/Event/MockPublisher.php | 2 +- .../Platform/Workers/NotificationsTest.php | 132 ------- tests/unit/Platform/Workers/WebhooksTest.php | 131 +++++++ 13 files changed, 434 insertions(+), 549 deletions(-) create mode 100644 src/Appwrite/Event/Message/Notification.php delete mode 100644 src/Appwrite/Event/Notification.php create mode 100644 src/Appwrite/Event/Publisher/Notification.php create mode 100644 tests/unit/Event/Message/NotificationTest.php diff --git a/app/init/resources.php b/app/init/resources.php index d48a60c06c..0135212974 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -11,6 +11,7 @@ use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Messaging as MessagingPublisher; use Appwrite\Event\Publisher\Migration as MigrationPublisher; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; @@ -144,6 +145,10 @@ $container->set('publisherForMessaging', fn (Publisher $publisher) => new Messag $publisher, new Queue(System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME)) ), ['publisher']); +$container->set('publisherForNotifications', fn (Publisher $publisher) => new NotificationPublisher( + $publisher, + new Queue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) +), ['publisher']); /** * Platform configuration diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 2ab3ecc3f5..85d8db3698 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -7,7 +7,6 @@ use Appwrite\Databases\TransactionState; use Appwrite\Event\Context\Audit as AuditContext; use Appwrite\Event\Event; use Appwrite\Event\Message\Func as FunctionMessage; -use Appwrite\Event\Notification; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; @@ -112,7 +111,6 @@ return function (Container $context): void { $context->set('queueForRealtime', fn () => new Realtime(), []); $context->set('usage', fn () => new UsageContext(), []); $context->set('auditContext', fn () => new AuditContext(), []); - $context->set('queueForNotifications', fn (Publisher $publisher) => new Notification($publisher), ['publisher']); $context->set('publisherForFunctions', fn (Publisher $publisher) => new FunctionPublisher( $publisher, new Queue(System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME), 'utopia-queue', Event::FUNCTIONS_QUEUE_TTL) diff --git a/app/init/worker/message.php b/app/init/worker/message.php index 4b87c78f9d..7806d8c600 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -1,8 +1,8 @@ set('queueForNotifications', function (Publisher $publisher) { - return new Notification($publisher); - }, ['publisher']); + $container->set('publisherForNotifications', fn (Publisher $publisher) => new NotificationPublisher( + $publisher, + new Queue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) + ), ['publisher']); $container->set('publisherForFunctions', fn (Publisher $publisher) => new FunctionPublisher( $publisher, diff --git a/src/Appwrite/Event/Message/Notification.php b/src/Appwrite/Event/Message/Notification.php new file mode 100644 index 0000000000..644d29d300 --- /dev/null +++ b/src/Appwrite/Event/Message/Notification.php @@ -0,0 +1,116 @@ + $recipients + * @param array $channels + * @param array $templateParams + * @param array $permissions + * @param array $variables + * @param array $attachment + * @param array $events + * @param array $params + * @param array $platform + */ + public function __construct( + public readonly ?Document $project = null, + public readonly string $recipient = '', + public readonly array $recipients = [], + public readonly array $channels = [], + public readonly string $template = '', + public readonly array $templateParams = [], + public readonly string $deduplicationKey = '', + public readonly array $permissions = [], + public readonly string $name = '', + public readonly string $subject = '', + public readonly string $bodyTemplate = '', + public readonly string $body = '', + public readonly string $preview = '', + public readonly array $variables = [], + public readonly array $attachment = [], + public readonly array $events = [], + public readonly string $event = '', + public readonly array $params = [], + public readonly array $platform = [], + ) { + } + + public function toArray(): array + { + $recipients = $this->recipients; + if (empty($recipients) && $this->recipient !== '') { + $recipients = [[ + 'address' => $this->recipient, + 'channel' => NOTIFICATION_TYPE_EMAIL, + ]]; + } + + $platform = !empty($this->platform) ? $this->platform : Config::getParam('platform', []); + + return [ + 'project' => $this->project === null ? null : [ + '$id' => $this->project->getId(), + '$sequence' => $this->project->getSequence(), + 'database' => $this->project->getAttribute('database'), + ], + 'recipient' => $this->recipient, + 'recipients' => $recipients, + 'channels' => $this->channels, + 'template' => $this->template, + 'templateParams' => $this->templateParams, + 'deduplicationKey' => $this->deduplicationKey, + 'permissions' => $this->permissions, + 'name' => $this->name, + 'subject' => $this->subject, + 'bodyTemplate' => $this->bodyTemplate, + 'body' => $this->body, + 'preview' => $this->preview, + 'variables' => $this->variables, + 'attachment' => $this->attachment, + 'events' => !empty($this->events) ? $this->events : $this->generateEvents(), + 'platform' => $platform, + ]; + } + + public static function fromArray(array $data): static + { + return new self( + project: !empty($data['project']) ? new Document($data['project']) : null, + recipient: $data['recipient'] ?? '', + recipients: $data['recipients'] ?? [], + channels: $data['channels'] ?? [], + template: $data['template'] ?? '', + templateParams: $data['templateParams'] ?? [], + deduplicationKey: $data['deduplicationKey'] ?? '', + permissions: $data['permissions'] ?? [], + name: $data['name'] ?? '', + subject: $data['subject'] ?? '', + bodyTemplate: $data['bodyTemplate'] ?? '', + body: $data['body'] ?? '', + preview: $data['preview'] ?? '', + variables: $data['variables'] ?? [], + attachment: $data['attachment'] ?? [], + events: $data['events'] ?? [], + platform: $data['platform'] ?? [], + ); + } + + /** + * @return array + */ + private function generateEvents(): array + { + if ($this->event === '') { + return []; + } + + return Event::generateEvents($this->event, $this->params); + } +} diff --git a/src/Appwrite/Event/Notification.php b/src/Appwrite/Event/Notification.php deleted file mode 100644 index ef64726ce1..0000000000 --- a/src/Appwrite/Event/Notification.php +++ /dev/null @@ -1,344 +0,0 @@ - - */ - protected array $recipients = []; - - /** - * @var array - */ - protected array $channels = []; - - protected string $template = ''; - protected array $templateParams = []; - protected string $deduplicationKey = ''; - - /** - * @var array - */ - protected array $permissions = []; - - public function __construct(protected Publisher $publisher) - { - parent::__construct($publisher); - - $this - ->setQueue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) - ->setClass(System::getEnv('_APP_NOTIFICATIONS_CLASS_NAME', Event::NOTIFICATIONS_CLASS_NAME)); - } - - public function setSubject(string $subject): self - { - $this->subject = $subject; - return $this; - } - - public function getSubject(): string - { - return $this->subject; - } - - public function setRecipient(string $recipient): self - { - $this->recipient = $recipient; - return $this; - } - - public function getRecipient(): string - { - return $this->recipient; - } - - public function setBody(string $body): self - { - $this->body = $body; - return $this; - } - - public function getBody(): string - { - return $this->body; - } - - public function setPreview(string $preview): self - { - $this->preview = $preview; - return $this; - } - - public function getPreview(): string - { - return $this->preview; - } - - public function setName(string $name): self - { - $this->name = $name; - return $this; - } - - public function getName(): string - { - return $this->name; - } - - public function setBodyTemplate(string $bodyTemplate): self - { - $this->bodyTemplate = $bodyTemplate; - return $this; - } - - public function getBodyTemplate(): string - { - return $this->bodyTemplate; - } - - public function getVariables(): array - { - return $this->variables; - } - - public function setVariables(array $variables): self - { - $this->variables = $variables; - return $this; - } - - public function appendVariables(array $variables): self - { - $this->variables = \array_merge($this->variables, $variables); - return $this; - } - - public function setAttachment(string $content, string $filename, string $encoding = 'base64', string $type = 'plain/text'): self - { - $this->attachment = [ - 'content' => \base64_encode($content), - 'filename' => $filename, - 'encoding' => $encoding, - 'type' => $type, - ]; - return $this; - } - - public function getAttachment(): array - { - return $this->attachment; - } - - public function resetAttachment(): self - { - $this->attachment = []; - return $this; - } - - /** - * @param array $recipients - */ - public function setRecipients(array $recipients): self - { - $this->recipients = $recipients; - return $this; - } - - /** - * @return array - */ - public function getRecipients(): array - { - return $this->recipients; - } - - public function addRecipient( - string $address, - string $channel = NOTIFICATION_TYPE_EMAIL, - ?string $signatureKey = null, - ?string $resourceType = null, - ?string $resourceId = null, - ?string $resourceInternalId = null, - ?string $parentResourceType = null, - ?string $parentResourceId = null, - ?string $parentResourceInternalId = null, - ): self { - $recipient = ['address' => $address, 'channel' => $channel]; - if ($signatureKey !== null && $signatureKey !== '') { - $recipient['signatureKey'] = $signatureKey; - } - if ($resourceType !== null && $resourceType !== '') { - $recipient['resourceType'] = $resourceType; - } - if ($resourceId !== null && $resourceId !== '') { - $recipient['resourceId'] = $resourceId; - } - if ($resourceInternalId !== null && $resourceInternalId !== '') { - $recipient['resourceInternalId'] = $resourceInternalId; - } - if ($parentResourceType !== null && $parentResourceType !== '') { - $recipient['parentResourceType'] = $parentResourceType; - } - if ($parentResourceId !== null && $parentResourceId !== '') { - $recipient['parentResourceId'] = $parentResourceId; - } - if ($parentResourceInternalId !== null && $parentResourceInternalId !== '') { - $recipient['parentResourceInternalId'] = $parentResourceInternalId; - } - $this->recipients[] = $recipient; - return $this; - } - - /** - * @param array $channels - */ - public function setChannels(array $channels): self - { - $this->channels = $channels; - return $this; - } - - /** - * @return array - */ - public function getChannels(): array - { - return $this->channels; - } - - public function setTemplate(string $template): self - { - $this->template = $template; - return $this; - } - - public function getTemplate(): string - { - return $this->template; - } - - public function setTemplateParams(array $params): self - { - $this->templateParams = $params; - return $this; - } - - public function getTemplateParams(): array - { - return $this->templateParams; - } - - public function setDeduplicationKey(string $deduplicationKey): self - { - $this->deduplicationKey = $deduplicationKey; - return $this; - } - - public function getDeduplicationKey(): string - { - return $this->deduplicationKey; - } - - /** - * @param array $permissions - */ - public function setPermissions(array $permissions): self - { - $this->permissions = $permissions; - return $this; - } - - /** - * @return array - */ - public function getPermissions(): array - { - return $this->permissions; - } - - public function reset(): self - { - parent::reset(); - - $this->project = null; - $this->context = []; - $this->platform = []; - $this->user = null; - $this->userId = null; - $this->recipient = ''; - $this->name = ''; - $this->subject = ''; - $this->body = ''; - $this->preview = ''; - $this->variables = []; - $this->bodyTemplate = ''; - $this->attachment = []; - $this->recipients = []; - $this->channels = []; - $this->template = ''; - $this->templateParams = []; - $this->deduplicationKey = ''; - $this->permissions = []; - return $this; - } - - protected function preparePayload(): array - { - $platform = $this->platform; - if (empty($platform)) { - $platform = Config::getParam('platform', []); - } - - $recipients = $this->recipients; - if (empty($recipients) && !empty($this->recipient)) { - $recipients = [[ - 'address' => $this->recipient, - 'channel' => NOTIFICATION_TYPE_EMAIL, - ]]; - } - - return [ - 'project' => $this->project, - 'recipient' => $this->recipient, - 'recipients' => $recipients, - 'channels' => $this->channels, - 'template' => $this->template, - 'templateParams' => $this->templateParams, - 'deduplicationKey' => $this->deduplicationKey, - 'permissions' => $this->permissions, - 'name' => $this->name, - 'subject' => $this->subject, - 'bodyTemplate' => $this->bodyTemplate, - 'body' => $this->body, - 'preview' => $this->preview, - 'variables' => $this->variables, - 'attachment' => $this->attachment, - 'events' => Event::generateEvents($this->getEvent(), $this->getParams()), - 'platform' => $platform, - ]; - } -} diff --git a/src/Appwrite/Event/Publisher/Notification.php b/src/Appwrite/Event/Publisher/Notification.php new file mode 100644 index 0000000000..2fe0353276 --- /dev/null +++ b/src/Appwrite/Event/Publisher/Notification.php @@ -0,0 +1,27 @@ +publish($queue ?? $this->queue, $message); + } + + public function getSize(bool $failed = false, ?Queue $queue = null): int + { + return $this->getQueueSize($queue ?? $this->queue, $failed); + } +} diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php index f1c6867379..ea8cda5448 100644 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php +++ b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Failed/Get.php @@ -3,7 +3,6 @@ namespace Appwrite\Platform\Modules\Health\Http\Health\Queue\Failed; use Appwrite\Event\Event; -use Appwrite\Event\Notification; use Appwrite\Event\Publisher\Audit; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Event\Publisher\Certificate; @@ -13,6 +12,7 @@ use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Messaging as MessagingPublisher; use Appwrite\Event\Publisher\Migration as MigrationPublisher; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Screenshot; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; @@ -89,7 +89,7 @@ class Get extends Base ->inject('publisherForMessaging') ->inject('publisherForMigrations') ->inject('publisherForScreenshots') - ->inject('queueForNotifications') + ->inject('publisherForNotifications') ->callback($this->action(...)); } @@ -110,7 +110,7 @@ class Get extends Base MessagingPublisher $publisherForMessaging, MigrationPublisher $publisherForMigrations, Screenshot $publisherForScreenshots, - Notification $queueForNotifications, + NotificationPublisher $publisherForNotifications, ): void { $threshold = (int) $threshold; @@ -128,7 +128,7 @@ class Get extends Base System::getEnv('_APP_SCREENSHOTS_QUEUE_NAME', Event::SCREENSHOTS_QUEUE_NAME) => $publisherForScreenshots, System::getEnv('_APP_MESSAGING_QUEUE_NAME', Event::MESSAGING_QUEUE_NAME) => $publisherForMessaging, System::getEnv('_APP_MIGRATIONS_QUEUE_NAME', Event::MIGRATIONS_QUEUE_NAME) => $publisherForMigrations, - System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME) => $queueForNotifications, + System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME) => $publisherForNotifications, default => throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Unknown queue name: ' . $name), }; $failed = $queue->getSize(failed: true); diff --git a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php index 9377f8852a..50337cf9d0 100644 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php +++ b/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php @@ -2,7 +2,7 @@ namespace Appwrite\Platform\Modules\Health\Http\Health\Queue\Notifications; -use Appwrite\Event\Notification; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Platform\Modules\Health\Http\Health\Queue\Base; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; @@ -42,16 +42,16 @@ class Get extends Base contentType: ContentType::JSON )) ->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true) - ->inject('queueForNotifications') + ->inject('publisherForNotifications') ->inject('response') ->callback($this->action(...)); } - public function action(int|string $threshold, Notification $queueForNotifications, Response $response): void + public function action(int|string $threshold, NotificationPublisher $publisherForNotifications, Response $response): void { $threshold = (int) $threshold; - $size = $queueForNotifications->getSize(); + $size = $publisherForNotifications->getSize(); $this->assertQueueThreshold($size, $threshold); diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 1cdf9bea7f..25ab716b7f 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -2,8 +2,9 @@ namespace Appwrite\Platform\Workers; +use Appwrite\Event\Message\Notification as NotificationMessage; use Appwrite\Event\Message\Usage as UsageMessage; -use Appwrite\Event\Notification; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Template\Template; use Appwrite\Usage\Context as UsageContext; @@ -36,7 +37,7 @@ class Webhooks extends Action ->inject('message') ->inject('project') ->inject('dbForPlatform') - ->inject('queueForNotifications') + ->inject('publisherForNotifications') ->inject('publisherForUsage') ->inject('log') ->inject('plan') @@ -47,14 +48,14 @@ class Webhooks extends Action * @param Message $message * @param Document $project * @param Database $dbForPlatform - * @param Notification $queueForNotifications + * @param NotificationPublisher $publisherForNotifications * @param UsagePublisher $publisherForUsage * @param Log $log * @param array $plan * @return void * @throws Exception */ - public function action(Message $message, Document $project, Database $dbForPlatform, Notification $queueForNotifications, UsagePublisher $publisherForUsage, Log $log, array $plan): void + public function action(Message $message, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, UsagePublisher $publisherForUsage, Log $log, array $plan): void { $this->errors = []; $payload = $message->getPayload(); @@ -73,7 +74,7 @@ class Webhooks extends Action foreach ($project->getAttribute('webhooks', []) as $webhook) { if (array_intersect($webhook->getAttribute('events', []), $events)) { - $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $queueForNotifications, $publisherForUsage, $plan); + $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $publisherForNotifications, $publisherForUsage, $plan); } } @@ -89,11 +90,12 @@ class Webhooks extends Action * @param Document $user * @param Document $project * @param Database $dbForPlatform - * @param Notification $queueForNotifications + * @param NotificationPublisher $publisherForNotifications + * @param UsagePublisher $publisherForUsage * @param array $plan * @return void */ - private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, Notification $queueForNotifications, UsagePublisher $publisherForUsage, array $plan): void + private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, UsagePublisher $publisherForUsage, array $plan): void { if ($webhook->getAttribute('enabled') !== true) { return; @@ -171,7 +173,7 @@ class Webhooks extends Action if ($attempts >= \intval(System::getEnv('_APP_WEBHOOK_MAX_FAILED_ATTEMPTS', '10'))) { $webhook->setAttribute('enabled', false); $updatePayload['enabled'] = false; - $this->sendAlert($attempts, $statusCode, $webhook, $project, $dbForPlatform, $queueForNotifications, $plan); + $this->sendAlert($attempts, $statusCode, $webhook, $project, $dbForPlatform, $publisherForNotifications, $plan); } $dbForPlatform->updateDocument('webhooks', $webhook->getId(), new Document($updatePayload)); @@ -203,17 +205,12 @@ class Webhooks extends Action * @param Document $webhook * @param Document $project * @param Database $dbForPlatform - * @param Notification $queueForNotifications + * @param NotificationPublisher $publisherForNotifications * @param array $plan * @return void */ - public function sendAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, Notification $queueForNotifications, array $plan): void + public function sendAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, array $plan): void { - // The DI-shared Notification event accumulates state across calls. Reset - // before configuring this alert so multiple webhook failures in a single - // worker invocation do not bleed recipients/subject/body between alerts. - $queueForNotifications->reset(); - $memberships = $dbForPlatform->find('memberships', [ Query::equal('teamInternalId', [$project->getAttribute('teamInternalId')]), Query::limit(APP_LIMIT_SUBQUERY) @@ -253,69 +250,71 @@ class Webhooks extends Action $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; + $consoleHostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', 'localhost')); + + $template->setParam('{{user}}', 'there'); $template->setParam('{{webhook}}', $webhook->getAttribute('name')); $template->setParam('{{project}}', $project->getAttribute('name')); $template->setParam('{{url}}', $webhook->getAttribute('url')); $template->setParam('{{error}}', 'The server returned ' . $statusCode . ' status code'); + $template->setParam('{{host}}', $protocol . '://' . $consoleHostname); $template->setParam('{{path}}', "/console/project-$region-$projectId/settings/webhooks/$webhookId"); $template->setParam('{{attempts}}', $attempts); - $template->setParam('{{logoUrl}}', $plan['logoUrl'] ?? APP_EMAIL_LOGO_URL); - $template->setParam('{{accentColor}}', $plan['accentColor'] ?? APP_EMAIL_ACCENT_COLOR); - $template->setParam('{{twitterUrl}}', $plan['twitterUrl'] ?? APP_SOCIAL_TWITTER); - $template->setParam('{{discordUrl}}', $plan['discordUrl'] ?? APP_SOCIAL_DISCORD); - $template->setParam('{{githubUrl}}', $plan['githubUrl'] ?? APP_SOCIAL_GITHUB_APPWRITE); - $template->setParam('{{termsUrl}}', $plan['termsUrl'] ?? APP_EMAIL_TERMS_URL); - $template->setParam('{{privacyUrl}}', $plan['privacyUrl'] ?? APP_EMAIL_PRIVACY_URL); - $subject = 'Webhook deliveries have been paused'; $preview = 'Webhook deliveries to your endpoint have been paused.'; - $body = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'); - $body - ->setParam('{{subject}}', $subject) - ->setParam('{{message}}', $template->render()) - ->setParam('{{year}}', date('Y')); - - $queueForNotifications - ->setProject($project) - ->setSubject($subject) - ->setPreview($preview) - ->setBody($body->render()) - ->setDeduplicationKey('webhook:' . $webhook->getId() . ':paused:' . $attempts); + $recipients = []; foreach ($users as $user) { $email = $user->getAttribute('email'); $userId = $user->getId(); - $queueForNotifications->addRecipient( - $userId, - NOTIFICATION_TYPE_CONSOLE, - null, - RESOURCE_TYPE_USERS, - $userId, - (string) $user->getSequence(), - RESOURCE_TYPE_PROJECTS, - $projectId, - (string) $projectInternalId, - ); + $recipients[] = [ + 'address' => $userId, + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => $userId, + 'resourceInternalId' => (string) $user->getSequence(), + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => $projectId, + 'parentResourceInternalId' => (string) $projectInternalId, + ]; if (!empty($email)) { - $queueForNotifications->addRecipient( - $email, - NOTIFICATION_TYPE_EMAIL, - null, - RESOURCE_TYPE_USERS, - $userId, - (string) $user->getSequence(), - RESOURCE_TYPE_PROJECTS, - $projectId, - (string) $projectInternalId, - ); + $recipients[] = [ + 'address' => $email, + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => $userId, + 'resourceInternalId' => (string) $user->getSequence(), + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => $projectId, + 'parentResourceInternalId' => (string) $projectInternalId, + ]; } } - $queueForNotifications->trigger(); + $publisherForNotifications->enqueue(new NotificationMessage( + project: $project, + recipients: $recipients, + deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $attempts, + subject: $subject, + bodyTemplate: __DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl', + body: $template->render(), + preview: $preview, + variables: [ + 'logoUrl' => $plan['logoUrl'] ?? APP_EMAIL_LOGO_URL, + 'accentColor' => $plan['accentColor'] ?? APP_EMAIL_ACCENT_COLOR, + 'twitter' => $plan['twitterUrl'] ?? APP_SOCIAL_TWITTER, + 'discord' => $plan['discordUrl'] ?? APP_SOCIAL_DISCORD, + 'github' => $plan['githubUrl'] ?? APP_SOCIAL_GITHUB_APPWRITE, + 'terms' => $plan['termsUrl'] ?? APP_EMAIL_TERMS_URL, + 'privacy' => $plan['privacyUrl'] ?? APP_EMAIL_PRIVACY_URL, + 'platform' => $plan['platformName'] ?? APP_NAME, + ], + )); } private static function hasOwnerRole(Document $membership, string $projectId): bool diff --git a/tests/unit/Event/Message/NotificationTest.php b/tests/unit/Event/Message/NotificationTest.php new file mode 100644 index 0000000000..5eb5739af0 --- /dev/null +++ b/tests/unit/Event/Message/NotificationTest.php @@ -0,0 +1,84 @@ + 'project-1', + '$sequence' => 'project-internal-1', + 'database' => 'mysql://database', + ]), + recipient: 'owner@example.test', + subject: 'Subject', + body: 'Body', + preview: 'Preview', + event: 'databases.[databaseId].collections.[collectionId].documents.[documentId].create', + params: [ + 'databaseId' => 'database-1', + 'collectionId' => 'collection-1', + 'documentId' => 'document-1', + ], + platform: ['name' => 'test-platform'], + ); + + $payload = $message->toArray(); + + $this->assertSame([ + '$id' => 'project-1', + '$sequence' => 'project-internal-1', + 'database' => 'mysql://database', + ], $payload['project']); + $this->assertSame([ + [ + 'address' => 'owner@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + ], + ], $payload['recipients']); + $this->assertSame('Subject', $payload['subject']); + $this->assertSame('Body', $payload['body']); + $this->assertSame('Preview', $payload['preview']); + $this->assertContains( + 'databases.database-1.collections.collection-1.documents.document-1.create', + $payload['events'] + ); + $this->assertSame(['name' => 'test-platform'], $payload['platform']); + + $this->assertSame($payload, NotificationMessage::fromArray($payload)->toArray()); + } + + public function testNotificationPublisherUsesConfiguredQueue(): void + { + $publisher = new MockPublisher(); + $publisherForNotifications = new NotificationPublisher($publisher, new Queue('custom-notifications')); + + $publisherForNotifications->enqueue(new NotificationMessage( + project: new Document(['$id' => 'project-1']), + recipients: [ + [ + 'address' => 'user-1', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + ], + ], + subject: 'Alert', + )); + + $events = $publisher->getEvents('custom-notifications'); + + $this->assertCount(1, $events); + $this->assertSame('Alert', $events[0]['subject']); + $this->assertSame(1, $publisherForNotifications->getSize()); + } +} diff --git a/tests/unit/Event/MockPublisher.php b/tests/unit/Event/MockPublisher.php index 8363ed4e85..b990179969 100644 --- a/tests/unit/Event/MockPublisher.php +++ b/tests/unit/Event/MockPublisher.php @@ -30,6 +30,6 @@ class MockPublisher implements Publisher public function getQueueSize(Queue $queue, bool $failedJobs = false): int { - return count($this->events[$queue->name]); + return count($this->events[$queue->name] ?? []); } } diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 2c6c9e0961..f1f74a5a87 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -3,10 +3,8 @@ namespace Tests\Unit\Platform\Workers; use Ahc\Jwt\JWT; -use Appwrite\Event\Notification; use Appwrite\Platform\Workers\Notifications; use PHPUnit\Framework\TestCase; -use Tests\Unit\Event\MockPublisher; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Cache; use Utopia\Database\Adapter\Memory; @@ -920,136 +918,6 @@ class NotificationsTest extends TestCase } } - /** - * Reviewer C2: `Notification::reset()` must clear EVERY state-bearing - * field — `$preview` was missing from the original reset() body and - * leaked across DI-shared event reuses (alarming when a webhook-paused - * preview line bleeds into an unrelated alert). - */ - public function testNotificationEventResetClearsAllState(): void - { - $event = new Notification(new MockPublisher()); - - $event - ->setProject(new Document(['$id' => 'project-x'])) - ->setRecipient('legacy@example.test') - ->setName('Some Name') - ->setSubject('Subject Line') - ->setBody('Body content') - ->setPreview('Preview snippet') - ->setVariables(['key' => 'value']) - ->setBodyTemplate('/tmp/template.tpl') - ->setAttachment('content', 'file.txt') - ->setRecipients([ - ['address' => 'a@example.test', 'channel' => NOTIFICATION_TYPE_EMAIL], - ]) - ->setChannels([NOTIFICATION_TYPE_EMAIL]) - ->setTemplate('template-id') - ->setTemplateParams(['x' => 1]) - ->setDeduplicationKey('dedup') - ->setPermissions([Permission::read(Role::any())]) - ->setEvent('databases.*.documents.*.create') - ->setParam('databaseId', 'database-1') - ->setPayload(['before' => 'reset'], ['before']) - ->setUser(new Document(['$id' => 'user-before'])) - ->setUserId('user-before') - ->setPlatform(['name' => 'platform-before']) - ->setContext('actor', new Document(['$id' => 'actor-before'])); - - $event->reset(); - - $this->assertSame('', $event->getRecipient(), 'recipient must reset to empty string'); - $this->assertSame('', $event->getName(), 'name must reset to empty string'); - $this->assertSame('', $event->getSubject(), 'subject must reset to empty string'); - $this->assertSame('', $event->getBody(), 'body must reset to empty string'); - $this->assertSame('', $event->getPreview(), 'preview must reset to empty string (C2 regression)'); - $this->assertSame([], $event->getVariables(), 'variables must reset to empty array'); - $this->assertSame('', $event->getBodyTemplate(), 'bodyTemplate must reset to empty string'); - $this->assertSame([], $event->getAttachment(), 'attachment must reset to empty array'); - $this->assertSame([], $event->getRecipients(), 'recipients must reset to empty array'); - $this->assertSame([], $event->getChannels(), 'channels must reset to empty array'); - $this->assertSame('', $event->getTemplate(), 'template must reset to empty string'); - $this->assertSame([], $event->getTemplateParams(), 'templateParams must reset to empty array'); - $this->assertSame('', $event->getDeduplicationKey(), 'deduplicationKey must reset to empty string'); - $this->assertSame([], $event->getPermissions(), 'permissions must reset to empty array'); - $this->assertNull($event->getProject(), 'project must reset to null'); - $this->assertSame('', $event->getEvent(), 'inherited event must reset to empty string'); - $this->assertSame([], $event->getParams(), 'inherited params must reset to empty array'); - $this->assertSame([], $event->getPayload(), 'inherited payload must reset to empty array'); - $this->assertNull($event->getUser(), 'inherited user must reset to null'); - $this->assertNull($event->getUserId(), 'inherited userId must reset to null'); - $this->assertSame([], $event->getPlatform(), 'inherited platform must reset to empty array'); - $this->assertNull($event->getContext('actor'), 'inherited context must reset'); - } - - /** - * Reviewer C3: `Webhooks::sendAlert` resets the DI-shared Notification - * event before configuring the alert, so a second invocation in the same - * worker pass cannot bleed recipients, subject, body, preview, or dedup - * key from the first. - * - * We exercise this at the event level (rather than standing up the full - * Webhooks worker fixture, which needs `dbForPlatform.memberships` and - * full project shape) by replicating sendAlert's add → trigger → reset → - * add → trigger flow on a single Notification instance and asserting - * each enqueued payload is fully isolated. - */ - public function testWebhookSendAlertResetsBetweenCalls(): void - { - $publisher = new MockPublisher(); - $event = new Notification($publisher); - - // First "sendAlert" pass: a single user, webhook A. - $event - ->setProject(new Document(['$id' => 'project-a', '$sequence' => 'project-a-internal'])) - ->setSubject('Webhook A paused') - ->setPreview('Webhook A preview') - ->setBody('Webhook A body') - ->setDeduplicationKey('webhook:hookA:paused:10') - ->addRecipient('alice@example.test', NOTIFICATION_TYPE_EMAIL, null, RESOURCE_TYPE_USERS, 'user-A', 'user-A-internal', RESOURCE_TYPE_PROJECTS, 'project-a', 'project-a-internal') - ->addRecipient('user-A', NOTIFICATION_TYPE_CONSOLE, null, RESOURCE_TYPE_USERS, 'user-A', 'user-A-internal', RESOURCE_TYPE_PROJECTS, 'project-a', 'project-a-internal') - ->trigger(); - - // Mirror the production sendAlert: reset before configuring next alert. - $event->reset(); - - // Second pass: a different user/webhook entirely. - $event - ->setProject(new Document(['$id' => 'project-b', '$sequence' => 'project-b-internal'])) - ->setSubject('Webhook B paused') - ->setPreview('Webhook B preview') - ->setBody('Webhook B body') - ->setDeduplicationKey('webhook:hookB:paused:10') - ->addRecipient('bob@example.test', NOTIFICATION_TYPE_EMAIL, null, RESOURCE_TYPE_USERS, 'user-B', 'user-B-internal', RESOURCE_TYPE_PROJECTS, 'project-b', 'project-b-internal') - ->addRecipient('user-B', NOTIFICATION_TYPE_CONSOLE, null, RESOURCE_TYPE_USERS, 'user-B', 'user-B-internal', RESOURCE_TYPE_PROJECTS, 'project-b', 'project-b-internal') - ->trigger(); - - $events = $publisher->getEvents('v1-notifications'); - $this->assertNotNull($events, 'two trigger() calls must enqueue at least one batch'); - $this->assertCount(2, $events, 'each sendAlert pass must produce its own enqueued event'); - - $first = $events[0]; - $second = $events[1]; - - $this->assertSame('Webhook A paused', $first['subject']); - $this->assertSame('Webhook A preview', $first['preview']); - $this->assertSame('Webhook A body', $first['body']); - $this->assertSame('webhook:hookA:paused:10', $first['deduplicationKey']); - $this->assertCount(2, $first['recipients']); - - $this->assertSame('Webhook B paused', $second['subject']); - $this->assertSame('Webhook B preview', $second['preview']); - $this->assertSame('Webhook B body', $second['body']); - $this->assertSame('webhook:hookB:paused:10', $second['deduplicationKey']); - $this->assertCount(2, $second['recipients'], 'second event must hold ONLY its own recipients'); - - $secondAddresses = \array_map(static fn ($r) => $r['address'], $second['recipients']); - $this->assertNotContains('alice@example.test', $secondAddresses, 'reset() must drop prior email recipient'); - $this->assertNotContains('user-A', $secondAddresses, 'reset() must drop prior console recipient'); - $this->assertContains('bob@example.test', $secondAddresses); - $this->assertContains('user-B', $secondAddresses); - } - /** * Worker happy-path: email channel. * diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 86f23fd417..0bc8157179 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -2,15 +2,117 @@ namespace Tests\Unit\Platform\Workers; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Platform\Workers\Webhooks; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use Tests\Unit\Event\MockPublisher; +use Utopia\Cache\Adapter\None as NoCache; +use Utopia\Cache\Cache; +use Utopia\Database\Adapter\Memory; +use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; +use Utopia\Database\Validator\Authorization; +use Utopia\Queue\Queue; require_once __DIR__ . '/../../../../app/init.php'; class WebhooksTest extends TestCase { + public function testSendAlertPublishesNotificationMessage(): void + { + $database = $this->createPlatformDatabase(); + $database->createDocument('memberships', new Document([ + '$id' => 'membership-1', + 'teamInternalId' => 'team-internal-1', + 'userId' => 'user-1', + 'roles' => 'owner', + ])); + $database->createDocument('memberships', new Document([ + '$id' => 'membership-2', + 'teamInternalId' => 'team-internal-1', + 'userId' => 'user-2', + 'roles' => 'developer', + ])); + $database->createDocument('users', new Document([ + '$id' => 'user-1', + '$sequence' => 101, + 'email' => 'owner@example.test', + ])); + $database->createDocument('users', new Document([ + '$id' => 'user-2', + '$sequence' => 102, + 'email' => 'developer@example.test', + ])); + + $publisher = new MockPublisher(); + $publisherForNotifications = new NotificationPublisher($publisher, new Queue('v1-notifications')); + $worker = new Webhooks(); + + $worker->sendAlert( + attempts: 10, + statusCode: 500, + webhook: new Document([ + '$id' => 'webhook-1', + 'name' => 'Payments', + 'url' => 'https://example.test/webhook', + ]), + project: new Document([ + '$id' => 'project-1', + '$sequence' => 'project-internal-1', + 'name' => 'Production', + 'teamInternalId' => 'team-internal-1', + 'region' => 'fra', + ]), + dbForPlatform: $database, + publisherForNotifications: $publisherForNotifications, + plan: [] + ); + + $events = $publisher->getEvents('v1-notifications'); + + $this->assertCount(1, $events); + + $payload = $events[0]; + $this->assertSame('project-1', $payload['project']['$id']); + $this->assertSame('project-internal-1', $payload['project']['$sequence']); + $this->assertSame('Webhook deliveries have been paused', $payload['subject']); + $this->assertSame('Webhook deliveries to your endpoint have been paused.', $payload['preview']); + $this->assertSame('webhook:webhook-1:paused:10', $payload['deduplicationKey']); + $this->assertSame( + \realpath(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'), + \realpath($payload['bodyTemplate']) + ); + $this->assertStringContainsString('Payments', $payload['body']); + $this->assertStringContainsString('/console/project-fra-project-1/settings/webhooks/webhook-1', $payload['body']); + $this->assertStringNotContainsString('{{', $payload['body']); + $this->assertSame(APP_NAME, $payload['variables']['platform']); + $this->assertSame(APP_EMAIL_LOGO_URL, $payload['variables']['logoUrl']); + $this->assertCount(2, $payload['recipients']); + $this->assertSame([ + 'address' => 'user-1', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'user-1', + 'resourceInternalId' => '101', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-1', + 'parentResourceInternalId' => 'project-internal-1', + ], $payload['recipients'][0]); + $this->assertSame([ + 'address' => 'owner@example.test', + 'channel' => NOTIFICATION_TYPE_EMAIL, + 'resourceType' => RESOURCE_TYPE_USERS, + 'resourceId' => 'user-1', + 'resourceInternalId' => '101', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project-1', + 'parentResourceInternalId' => 'project-internal-1', + ], $payload['recipients'][1]); + } + #[DataProvider('ownerRoleProvider')] public function testOwnerRoleDetectionAcceptsArrayAndLegacyStringRoles(mixed $roles, bool $expected): void { @@ -37,4 +139,33 @@ class WebhooksTest extends TestCase 'invalid roles' => [null, false], ]; } + + private function createPlatformDatabase(): Database + { + $authorization = new Authorization(); + $authorization->addRole(Role::any()->toString()); + + $database = new Database(new Memory(), new Cache(new NoCache())); + $database + ->setAuthorization($authorization) + ->setDatabase('webhookTests') + ->setNamespace('webhook_' . \uniqid()); + + $permissions = [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ]; + + $database->create(); + $database->createCollection('memberships', [], [], $permissions, false); + $database->createAttribute('memberships', 'teamInternalId', Database::VAR_STRING, 255, true); + $database->createAttribute('memberships', 'userId', Database::VAR_STRING, 255, true); + $database->createAttribute('memberships', 'roles', Database::VAR_STRING, 1024, true); + $database->createCollection('users', [], [], $permissions, false); + $database->createAttribute('users', 'email', Database::VAR_STRING, 320, false); + + return $database; + } } From 32aea1f8635f44560c4368bc84ad43ee192c3ffd Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 00:33:03 +1200 Subject: [PATCH 49/80] fix: preserve legacy schedule access --- .../Modules/Projects/Http/Schedules/Get.php | 4 +- .../Modules/Projects/Http/Schedules/XList.php | 16 +- .../Modules/Projects/SchedulesTest.php | 159 ++++++++++++++++++ 3 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 tests/unit/Platform/Modules/Projects/SchedulesTest.php diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php index f89eecd1a0..05c4a925db 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php @@ -67,9 +67,11 @@ class Get extends Action throw new Exception(Exception::SCHEDULE_NOT_FOUND); } + $projectInternalId = $schedule->getAttribute('projectInternalId'); + if ( $schedule->getAttribute('projectId') !== $project->getId() - || $schedule->getAttribute('projectInternalId') !== $project->getSequence() + || ($projectInternalId !== null && $projectInternalId !== '' && $projectInternalId !== $project->getSequence()) ) { throw new Exception(Exception::SCHEDULE_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php index d0e6ad3036..7f82162a2c 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php @@ -77,7 +77,10 @@ class XList extends Action } $queries[] = Query::equal('projectId', [$project->getId()]); - $queries[] = Query::equal('projectInternalId', [$project->getSequence()]); + $queries[] = Query::or([ + Query::equal('projectInternalId', [$project->getSequence()]), + Query::isNull('projectInternalId'), + ]); $cursor = Query::getCursorQueries($queries, false); $cursor = \reset($cursor); @@ -94,7 +97,7 @@ class XList extends Action if ( $cursorDocument->isEmpty() || $cursorDocument->getAttribute('projectId') !== $project->getId() - || $cursorDocument->getAttribute('projectInternalId') !== $project->getSequence() + || !$this->matchesProjectInternalId($cursorDocument, $project) ) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Schedule '{$scheduleId}' for the 'cursor' value not found."); } @@ -116,4 +119,13 @@ class XList extends Action 'total' => $total, ]), Response::MODEL_SCHEDULE_LIST); } + + private function matchesProjectInternalId(Document $schedule, Document $project): bool + { + $projectInternalId = $schedule->getAttribute('projectInternalId'); + + return $projectInternalId === null + || $projectInternalId === '' + || $projectInternalId === $project->getSequence(); + } } diff --git a/tests/unit/Platform/Modules/Projects/SchedulesTest.php b/tests/unit/Platform/Modules/Projects/SchedulesTest.php new file mode 100644 index 0000000000..ad714d3d99 --- /dev/null +++ b/tests/unit/Platform/Modules/Projects/SchedulesTest.php @@ -0,0 +1,159 @@ +document = $document; + $this->model = $model; + } +} + +class SchedulesTest extends TestCase +{ + private Authorization $authorization; + private Database $database; + private Document $project; + + protected function setUp(): void + { + $this->authorization = new Authorization(); + $this->authorization->addRole(Role::any()->toString()); + + $this->database = new Database(new Memory(), new Cache(new NoCache())); + $this->database + ->setAuthorization($this->authorization) + ->setDatabase('scheduleTests') + ->setNamespace('schedules_' . \uniqid()); + + $permissions = [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ]; + + $this->database->create(); + $this->database->createCollection('projects', [], [], $permissions, false); + $this->database->createCollection('schedules', [], [], $permissions, false); + $this->database->createAttribute('schedules', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('schedules', 'projectInternalId', Database::VAR_ID, 0, false); + + $this->project = $this->database->createDocument('projects', new Document([ + '$id' => 'project-a', + ])); + } + + protected function tearDown(): void + { + $this->authorization->cleanRoles(); + $this->authorization->addRole(Role::any()->toString()); + } + + public function testGetAllowsLegacyScheduleWithoutProjectInternalId(): void + { + $this->createSchedule('legacy'); + + $response = new CapturingSchedulesResponse(); + + (new Get())->action('project-a', 'legacy', $response, $this->database); + + $this->assertSame('legacy', $response->document->getId()); + $this->assertSame(Response::MODEL_SCHEDULE, $response->model); + } + + public function testGetRejectsScheduleWithMismatchedProjectInternalId(): void + { + $this->createSchedule('mismatch', projectInternalId: '999'); + + $this->expectException(AppwriteException::class); + + (new Get())->action('project-a', 'mismatch', new CapturingSchedulesResponse(), $this->database); + } + + public function testListIncludesLegacyAndCurrentProjectSchedules(): void + { + $this->createSchedule('legacy'); + $this->createSchedule('current', projectInternalId: $this->project->getSequence()); + $this->createSchedule('mismatch', projectInternalId: '999'); + $this->createSchedule('other-project', projectId: 'project-b'); + + $response = new CapturingSchedulesResponse(); + + (new XList())->action('project-a', [], true, $response, $this->database); + + $schedules = $response->document->getAttribute('schedules'); + $scheduleIds = \array_map( + fn (Document $schedule) => $schedule->getId(), + $schedules, + ); + + $this->assertContains('legacy', $scheduleIds); + $this->assertContains('current', $scheduleIds); + $this->assertNotContains('mismatch', $scheduleIds); + $this->assertNotContains('other-project', $scheduleIds); + $this->assertSame(2, $response->document->getAttribute('total')); + $this->assertSame(Response::MODEL_SCHEDULE_LIST, $response->model); + } + + public function testListCursorAllowsLegacyScheduleWithoutProjectInternalId(): void + { + $legacy = $this->createSchedule('legacy'); + $this->createSchedule('current', projectInternalId: $this->project->getSequence()); + + $response = new CapturingSchedulesResponse(); + + (new XList())->action( + 'project-a', + [Query::cursorAfter($legacy)->toString()], + true, + $response, + $this->database, + ); + + $this->assertSame(Response::MODEL_SCHEDULE_LIST, $response->model); + } + + private function createSchedule( + string $id, + string $projectId = 'project-a', + int|string|null $projectInternalId = null, + ): Document { + $attributes = [ + '$id' => $id, + 'projectId' => $projectId, + ]; + + if ($projectInternalId !== null) { + $attributes['projectInternalId'] = $projectInternalId; + } + + return $this->database->createDocument('schedules', new Document($attributes)); + } +} From 6c571aa5c7c4d3faa209049176e60bbe20737d67 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 00:47:48 +1200 Subject: [PATCH 50/80] fix: refresh webhook pause dedup keys --- src/Appwrite/Platform/Workers/Webhooks.php | 2 +- tests/unit/Platform/Workers/WebhooksTest.php | 91 +++++++++++++++----- 2 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 25ab716b7f..bc6653a6bc 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -299,7 +299,7 @@ class Webhooks extends Action $publisherForNotifications->enqueue(new NotificationMessage( project: $project, recipients: $recipients, - deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $attempts, + deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $webhook->getUpdatedAt(), subject: $subject, bodyTemplate: __DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl', body: $template->render(), diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 0bc8157179..1e66948337 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -24,28 +24,7 @@ class WebhooksTest extends TestCase public function testSendAlertPublishesNotificationMessage(): void { $database = $this->createPlatformDatabase(); - $database->createDocument('memberships', new Document([ - '$id' => 'membership-1', - 'teamInternalId' => 'team-internal-1', - 'userId' => 'user-1', - 'roles' => 'owner', - ])); - $database->createDocument('memberships', new Document([ - '$id' => 'membership-2', - 'teamInternalId' => 'team-internal-1', - 'userId' => 'user-2', - 'roles' => 'developer', - ])); - $database->createDocument('users', new Document([ - '$id' => 'user-1', - '$sequence' => 101, - 'email' => 'owner@example.test', - ])); - $database->createDocument('users', new Document([ - '$id' => 'user-2', - '$sequence' => 102, - 'email' => 'developer@example.test', - ])); + $this->seedOwnerUser($database); $publisher = new MockPublisher(); $publisherForNotifications = new NotificationPublisher($publisher, new Queue('v1-notifications')); @@ -56,6 +35,7 @@ class WebhooksTest extends TestCase statusCode: 500, webhook: new Document([ '$id' => 'webhook-1', + '$updatedAt' => '2026-01-01T00:00:00.000+00:00', 'name' => 'Payments', 'url' => 'https://example.test/webhook', ]), @@ -80,7 +60,7 @@ class WebhooksTest extends TestCase $this->assertSame('project-internal-1', $payload['project']['$sequence']); $this->assertSame('Webhook deliveries have been paused', $payload['subject']); $this->assertSame('Webhook deliveries to your endpoint have been paused.', $payload['preview']); - $this->assertSame('webhook:webhook-1:paused:10', $payload['deduplicationKey']); + $this->assertSame('webhook:webhook-1:paused:2026-01-01T00:00:00.000+00:00', $payload['deduplicationKey']); $this->assertSame( \realpath(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'), \realpath($payload['bodyTemplate']) @@ -113,6 +93,45 @@ class WebhooksTest extends TestCase ], $payload['recipients'][1]); } + public function testSendAlertDeduplicationKeyChangesPerPauseCycle(): void + { + $database = $this->createPlatformDatabase(); + $this->seedOwnerUser($database); + $publisher = new MockPublisher(); + $publisherForNotifications = new NotificationPublisher($publisher, new Queue('v1-notifications')); + $worker = new Webhooks(); + $project = new Document([ + '$id' => 'project-1', + '$sequence' => 'project-internal-1', + 'name' => 'Production', + 'teamInternalId' => 'team-internal-1', + 'region' => 'fra', + ]); + + foreach (['2026-01-01T00:00:00.000+00:00', '2026-01-02T00:00:00.000+00:00'] as $updatedAt) { + $worker->sendAlert( + attempts: 10, + statusCode: 500, + webhook: new Document([ + '$id' => 'webhook-1', + '$updatedAt' => $updatedAt, + 'name' => 'Payments', + 'url' => 'https://example.test/webhook', + ]), + project: $project, + dbForPlatform: $database, + publisherForNotifications: $publisherForNotifications, + plan: [] + ); + } + + $events = $publisher->getEvents('v1-notifications'); + + $this->assertCount(2, $events); + $this->assertSame('webhook:webhook-1:paused:2026-01-01T00:00:00.000+00:00', $events[0]['deduplicationKey']); + $this->assertSame('webhook:webhook-1:paused:2026-01-02T00:00:00.000+00:00', $events[1]['deduplicationKey']); + } + #[DataProvider('ownerRoleProvider')] public function testOwnerRoleDetectionAcceptsArrayAndLegacyStringRoles(mixed $roles, bool $expected): void { @@ -168,4 +187,30 @@ class WebhooksTest extends TestCase return $database; } + + private function seedOwnerUser(Database $database): void + { + $database->createDocument('memberships', new Document([ + '$id' => 'membership-1', + 'teamInternalId' => 'team-internal-1', + 'userId' => 'user-1', + 'roles' => 'owner', + ])); + $database->createDocument('memberships', new Document([ + '$id' => 'membership-2', + 'teamInternalId' => 'team-internal-1', + 'userId' => 'user-2', + 'roles' => 'developer', + ])); + $database->createDocument('users', new Document([ + '$id' => 'user-1', + '$sequence' => 101, + 'email' => 'owner@example.test', + ])); + $database->createDocument('users', new Document([ + '$id' => 'user-2', + '$sequence' => 102, + 'email' => 'developer@example.test', + ])); + } } From a5593c0e3081e6a84737f309903c104ceb1e7a2d Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 01:08:52 +1200 Subject: [PATCH 51/80] test: match webhook pause alerts by content --- src/Appwrite/Platform/Workers/Webhooks.php | 2 +- .../Notifications/NotificationsBase.php | 23 ++++++------------- tests/unit/Platform/Workers/WebhooksTest.php | 2 +- 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index bc6653a6bc..67cc6cb3b6 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -263,7 +263,7 @@ class Webhooks extends Action $template->setParam('{{attempts}}', $attempts); $subject = 'Webhook deliveries have been paused'; - $preview = 'Webhook deliveries to your endpoint have been paused.'; + $preview = 'Webhook "' . $webhook->getAttribute('name') . '" has been paused after ' . $attempts . ' failed delivery attempts.'; $recipients = []; diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 3e635ee157..a610d89e15 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -434,6 +434,7 @@ trait NotificationsBase // Register a webhook pointing at an unroutable address. The Webhook // worker will fail every delivery and after 10 attempts pause it // and enqueue a console+email alert for the project owner. + $webhookName = 'Failing Webhook ' . \uniqid(); $webhook = $this->client->call(Client::METHOD_POST, '/webhooks', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', @@ -442,7 +443,7 @@ trait NotificationsBase 'x-appwrite-mode' => 'admin', ], [ 'webhookId' => ID::unique(), - 'name' => 'Failing Webhook ' . \uniqid(), + 'name' => $webhookName, 'events' => ['users.*.create'], 'url' => 'http://request-catcher-webhook:1/', 'tls' => false, @@ -477,30 +478,20 @@ trait NotificationsBase ); } - // The deduplication key (and therefore the alert's messageId hash) is - // unique per (webhook, attempts) tuple — see Webhooks worker. Compute - // the expected messageId so we can deterministically match the alert - // for *this* test instance even when other tests in the same process - // have seeded their own webhook-failure alerts. - // Driver loops max+2 events; the worker may pause anywhere from - // attempts==max to attempts==max+2 depending on which delivery - // crossed the threshold. Record possible message ids. - $expectedMessageIds = []; - for ($attempts = $maxAttempts; $attempts <= $maxAttempts + 2; $attempts++) { - $expectedMessageIds[] = \md5('webhook:' . $webhookId . ':paused:' . $attempts); - } - // Poll for the alert. Alert creation is async (notification worker) // and webhook deliveries also queue up — give them generous time. $alertId = null; - $this->assertEventually(function () use (&$alertId, $webhookId, $expectedMessageIds) { + $this->assertEventually(function () use (&$alertId, $projectId, $webhookId, $webhookName) { $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); foreach ($list['body']['alerts'] as $alert) { if ( ($alert['channel'] ?? '') === 'console' - && \in_array($alert['messageId'] ?? '', $expectedMessageIds, true) + && ($alert['projectId'] ?? '') === $projectId + && ($alert['parentResourceId'] ?? '') === $projectId + && ($alert['title'] ?? '') === 'Webhook deliveries have been paused' + && \str_contains($alert['body'] ?? '', $webhookName) ) { $alertId = $alert['$id']; return; diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 1e66948337..1d681394f9 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -59,7 +59,7 @@ class WebhooksTest extends TestCase $this->assertSame('project-1', $payload['project']['$id']); $this->assertSame('project-internal-1', $payload['project']['$sequence']); $this->assertSame('Webhook deliveries have been paused', $payload['subject']); - $this->assertSame('Webhook deliveries to your endpoint have been paused.', $payload['preview']); + $this->assertSame('Webhook "Payments" has been paused after 10 failed delivery attempts.', $payload['preview']); $this->assertSame('webhook:webhook-1:paused:2026-01-01T00:00:00.000+00:00', $payload['deduplicationKey']); $this->assertSame( \realpath(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'), From 3924afb2e08a5b7830d74b14841911752438015b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 11:15:49 +1200 Subject: [PATCH 52/80] refactor: replace mails worker --- app/init/resources.php | 2 +- app/views/install/compose.phtml | 6 +- app/worker.php | 3 + bin/worker-mails | 2 +- docker-compose.yml | 42 ---- src/Appwrite/Platform/Services/Workers.php | 2 - src/Appwrite/Platform/Workers/Mails.php | 215 ------------------ .../Platform/Workers/Notifications.php | 38 +++- tests/resources/docker/docker-compose.yml | 25 -- .../Platform/Workers/NotificationsTest.php | 93 ++++++++ 10 files changed, 129 insertions(+), 299 deletions(-) delete mode 100644 src/Appwrite/Platform/Workers/Mails.php diff --git a/app/init/resources.php b/app/init/resources.php index 0135212974..22e36fe431 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -139,7 +139,7 @@ $container->set('publisherForDeletes', fn (Publisher $publisher) => new DeletePu ), ['publisher']); $container->set('publisherForMails', fn (Publisher $publisher) => new MailPublisher( $publisher, - new Queue(System::getEnv('_APP_MAILS_QUEUE_NAME', Event::MAILS_QUEUE_NAME)) + new Queue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) ), ['publisher']); $container->set('publisherForMessaging', fn (Publisher $publisher) => new MessagingPublisher( $publisher, diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 6ce1fb5cea..1c5783dce6 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -596,11 +596,11 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_DOCKER_HUB_PASSWORD - _APP_LOGGING_CONFIG - appwrite-worker-mails: + appwrite-worker-notifications: image: /: - entrypoint: worker-mails + entrypoint: worker-notifications <<: *x-logging - container_name: appwrite-worker-mails + container_name: appwrite-worker-notifications restart: unless-stopped networks: - appwrite diff --git a/app/worker.php b/app/worker.php index 169b8e9770..a064ee8f76 100644 --- a/app/worker.php +++ b/app/worker.php @@ -73,6 +73,9 @@ if (! isset($args[1])) { \array_shift($args); $workerName = $args[0]; +if ($workerName === 'mails') { + $workerName = 'notifications'; +} if (\str_starts_with($workerName, 'databases')) { $queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main'); diff --git a/bin/worker-mails b/bin/worker-mails index 3b1415f45f..1b7bf28745 100644 --- a/bin/worker-mails +++ b/bin/worker-mails @@ -1,3 +1,3 @@ #!/bin/sh -exec php /usr/src/code/app/worker.php mails "$@" \ No newline at end of file +exec php /usr/src/code/app/worker.php notifications "$@" diff --git a/docker-compose.yml b/docker-compose.yml index ca18dd64cd..3ac1121290 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -764,48 +764,6 @@ services: - _APP_LOGGING_PROVIDER - _APP_DATABASE_SHARED_TABLES - appwrite-worker-mails: - entrypoint: worker-mails - <<: [*x-logging, *x-build] - container_name: appwrite-worker-mails - image: appwrite-dev - networks: - - appwrite - volumes: - - ./app:/usr/src/code/app - - ./src:/usr/src/code/src - depends_on: - - redis - - maildev - - ${_APP_DB_HOST:-mariadb} - # - smtp - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_POOL_ADAPTER - - _APP_OPENSSL_KEY_V1 - - _APP_SYSTEM_EMAIL_NAME - - _APP_SYSTEM_EMAIL_ADDRESS - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_DB_ADAPTER - - _APP_SMTP_HOST - - _APP_SMTP_PORT - - _APP_SMTP_SECURE - - _APP_SMTP_USERNAME - - _APP_SMTP_PASSWORD - - _APP_LOGGING_CONFIG - - _APP_DOMAIN - - _APP_OPTIONS_FORCE_HTTPS - - _APP_DATABASE_SHARED_TABLES - appwrite-worker-notifications: entrypoint: worker-notifications <<: [*x-logging, *x-build] diff --git a/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index 5af3d078de..9ab26a41b7 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -7,7 +7,6 @@ use Appwrite\Platform\Workers\Certificates; use Appwrite\Platform\Workers\Deletes; use Appwrite\Platform\Workers\Executions; use Appwrite\Platform\Workers\Functions; -use Appwrite\Platform\Workers\Mails; use Appwrite\Platform\Workers\Messaging; use Appwrite\Platform\Workers\Migrations; use Appwrite\Platform\Workers\Notifications; @@ -27,7 +26,6 @@ class Workers extends Service ->addAction(Deletes::getName(), new Deletes()) ->addAction(Executions::getName(), new Executions()) ->addAction(Functions::getName(), new Functions()) - ->addAction(Mails::getName(), new Mails()) ->addAction(Messaging::getName(), new Messaging()) ->addAction(Notifications::getName(), new Notifications()) ->addAction(Webhooks::getName(), new Webhooks()) diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php deleted file mode 100644 index 5cd4639988..0000000000 --- a/src/Appwrite/Platform/Workers/Mails.php +++ /dev/null @@ -1,215 +0,0 @@ -desc('Mails worker') - ->inject('message') - ->inject('project') - ->inject('register') - ->inject('log') - ->callback($this->action(...)); - } - - /** - * @var array - */ - protected array $richTextParams = [ - 'b' => '', - '/b' => '', - ]; - - /** - * @param Message $message - * @param Document $project - * @param Registry $register - * @param Log $log - * @return void - * @throws Exception - */ - public function action(Message $message, Document $project, Registry $register, Log $log): void - { - Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); - $payload = $message->getPayload(); - - if (empty($payload)) { - throw new Exception('Missing payload'); - } - - $smtp = $payload['smtp']; - - if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { - throw new Exception('Skipped mail processing. No SMTP configuration has been set.'); - } - - $type = empty($smtp) ? 'cloud' : 'smtp'; - $log->addTag('type', $type); - - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; - $hostname = System::getEnv('_APP_CONSOLE_DOMAIN'); - - $recipient = $payload['recipient']; - $subject = $payload['subject']; - $variables = $payload['variables']; - $variables['host'] = $protocol . '://' . $hostname; - $name = $payload['name']; - $body = $payload['body']; - $preview = $payload['preview'] ?? ''; - - $variables['subject'] = $subject; - $variables['heading'] = $variables['heading'] ?? $subject; - $variables['year'] = date("Y"); - - $attachment = $payload['attachment'] ?? []; - $bodyTemplate = $payload['bodyTemplate']; - if (empty($bodyTemplate)) { - $bodyTemplate = __DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'; - } - $bodyTemplate = Template::fromFile($bodyTemplate); - $bodyTemplate->setParam('{{body}}', $body, escapeHtml: false); - foreach ($variables as $key => $value) { - // TODO: hotfix for redirect param - $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: $key !== 'redirect'); - } - foreach ($this->richTextParams as $key => $value) { - $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: false); - } - - $previewWhitespace = ''; - - if (!empty($preview)) { - $previewTemplate = Template::fromString($preview); - foreach ($variables as $key => $value) { - $previewTemplate->setParam('{{' . $key . '}}', $value); - } - // render() will return the subject in

tags, so use strip_tags() to remove them - $preview = \strip_tags($previewTemplate->render()); - - $previewLen = strlen($preview); - if ($previewLen < $this->previewMaxLen) { - $previewWhitespace = str_repeat($this->whitespaceCodes, $this->previewMaxLen - $previewLen); - } - } - - - $bodyTemplate->setParam('{{preview}}', $preview); - $bodyTemplate->setParam('{{previewWhitespace}}', $previewWhitespace, false); - - $body = $bodyTemplate->render(); - - $subjectTemplate = Template::fromString($subject); - foreach ($variables as $key => $value) { - $subjectTemplate->setParam('{{' . $key . '}}', $value); - } - // render() will return the subject in

tags, so use strip_tags() to remove them - $subject = \strip_tags($subjectTemplate->render()); - - /** @var EmailAdapter $adapter */ - $adapter = empty($smtp) - ? $register->get('smtp') - : new SMTP( - host: $smtp['host'], - port: (int) $smtp['port'], - username: $smtp['username'] ?? '', - password: $smtp['password'] ?? '', - smtpSecure: $smtp['secure'] ?? '', - smtpAutoTLS: false, - xMailer: 'Appwrite Mailer', - timeout: 10, - keepAlive: true, - timelimit: 30, - ); - - // Resolve from/replyTo using fallback hierarchy: Custom options > SMTP config > Defaults - $defaultFromEmail = System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM); - $defaultFromName = \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); - - $fromEmail = !empty($smtp) ? ($smtp['senderEmail'] ?? $defaultFromEmail) : $defaultFromEmail; - $fromName = !empty($smtp) ? ($smtp['senderName'] ?? $defaultFromName) : $defaultFromName; - $replyTo = $defaultFromEmail; - $replyToName = $defaultFromName; - - $customMailOptions = $payload['customMailOptions'] ?? []; - - if (!empty($customMailOptions['senderEmail'])) { - $fromEmail = $customMailOptions['senderEmail']; - } - if (!empty($customMailOptions['senderName'])) { - $fromName = $customMailOptions['senderName']; - } - - if (!empty($customMailOptions['replyToEmail']) || !empty($customMailOptions['replyToName'])) { - $replyTo = $customMailOptions['replyToEmail'] ?? $replyTo; - $replyToName = $customMailOptions['replyToName'] ?? $replyToName; - } elseif (!empty($smtp)) { - // Includes backwards compatibility: fall back to legacy `replyTo` key - $smtpReplyToEmail = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; - $replyTo = !empty($smtpReplyToEmail) ? $smtpReplyToEmail : ($smtp['senderEmail'] ?? $replyTo); - $replyToName = !empty($smtp['replyToName']) ? $smtp['replyToName'] : ($smtp['senderName'] ?? $replyToName); - } - - $attachments = null; - if (!empty($attachment['content'] ?? '')) { - $attachments = [ - new Attachment( - name: $attachment['filename'] ?? 'unknown.file', - path: '', - type: $attachment['type'] ?? 'plain/text', - content: \base64_decode($attachment['content']), - ), - ]; - } - - $emailMessage = new EmailMessage( - to: [['email' => $recipient, 'name' => $name]], - subject: $subject, - content: $body, - fromName: $fromName, - fromEmail: $fromEmail, - replyToName: $replyToName, - replyToEmail: $replyTo, - attachments: $attachments, - html: true, - ); - - try { - $adapter->send($emailMessage); - } catch (\Throwable $error) { - if ($type === 'smtp') { - throw new Exception('Error sending mail: ' . $error->getMessage(), 401); - } - throw new Exception('Error sending mail: ' . $error->getMessage(), 500); - } - } -} diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 43250f2f79..3a82a405d9 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -203,9 +203,12 @@ class Notifications extends Action Log $log, ): ?string { $address = $recipient['address']; - $smtp = $this->resolveSmtpConfig($project); + $smtp = $this->resolveSmtpConfig($project, $payload); if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { + if (empty($payload['recipients']) && !empty($payload['recipient'] ?? '')) { + throw new Exception('Skipped mail processing. No SMTP configuration has been set.'); + } $log->addTag('email_skipped', 'no_smtp'); return null; } @@ -305,7 +308,22 @@ class Notifications extends Action $replyTo = $defaultFromEmail; $replyToName = $defaultFromName; - if (!empty($smtp)) { + $customMailOptions = $payload['customMailOptions'] ?? []; + if (!\is_array($customMailOptions)) { + $customMailOptions = []; + } + + if (!empty($customMailOptions['senderEmail'])) { + $fromEmail = (string) $customMailOptions['senderEmail']; + } + if (!empty($customMailOptions['senderName'])) { + $fromName = (string) $customMailOptions['senderName']; + } + + if (!empty($customMailOptions['replyToEmail']) || !empty($customMailOptions['replyToName'])) { + $replyTo = (string) ($customMailOptions['replyToEmail'] ?? $replyTo); + $replyToName = (string) ($customMailOptions['replyToName'] ?? $replyToName); + } elseif (!empty($smtp)) { $smtpReplyToEmail = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; $replyTo = !empty($smtpReplyToEmail) ? $smtpReplyToEmail : ($smtp['senderEmail'] ?? $replyTo); $replyToName = !empty($smtp['replyToName']) ? $smtp['replyToName'] : ($smtp['senderName'] ?? $replyToName); @@ -338,7 +356,7 @@ class Notifications extends Action try { $adapter->send($emailMessage); } catch (Throwable $error) { - throw new Exception('Error sending notification: ' . $error->getMessage(), 500); + throw new Exception('Error sending notification: ' . $error->getMessage(), $type === 'smtp' ? 401 : 500); } if ($messageId !== '') { @@ -556,16 +574,16 @@ class Notifications extends Action } /** - * Resolve project SMTP config to the wire shape Mails.php expects. - * ST4 stripped `smtp` and `customMailOptions` from the Notification - * event payload, so the worker now reads from the project Document. - * Falls back to env-driven cloud SMTP when the project has not - * configured custom SMTP. - * + * @param array $payload * @return array */ - private function resolveSmtpConfig(Document $project): array + private function resolveSmtpConfig(Document $project, array $payload): array { + $payloadSmtp = $payload['smtp'] ?? []; + if (\is_array($payloadSmtp) && !empty($payloadSmtp)) { + return $payloadSmtp; + } + $smtp = $project->getAttribute('smtp', []); if (!\is_array($smtp) || empty($smtp['enabled'] ?? false)) { return []; diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 5942eea65f..c25796dc08 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -267,31 +267,6 @@ services: - _APP_COMPUTE_MEMORY - _APP_EXECUTOR_HOST - appwrite-worker-mails: - entrypoint: worker-mails - container_name: appwrite-worker-mails - build: - context: . - restart: unless-stopped - networks: - - appwrite - depends_on: - - redis - - maildev - # - smtp - environment: - - _APP_ENV - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_DB_ADAPTER - - _APP_SMTP_HOST - - _APP_SMTP_PORT - appwrite-worker-notifications: entrypoint: worker-notifications container_name: appwrite-worker-notifications diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index f1f74a5a87..62166071f2 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -379,6 +379,99 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_TYPE_EMAIL, $worker->dispatched[0]['channel']); } + public function testLegacyMailPayloadOptionsAreAppliedByEmailChannel(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + try { + $worker = new Notifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipient' => 'legacy@example.test', + 'name' => 'Legacy User', + 'subject' => 'Hello {{name}}', + 'body' => 'Body {{name}}', + 'variables' => ['name' => 'Legacy'], + 'customMailOptions' => [ + 'senderEmail' => 'sender@example.test', + 'senderName' => 'Custom Sender', + 'replyToEmail' => 'reply@example.test', + 'replyToName' => 'Custom Reply', + ], + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + + $this->assertSame(1, $spy->sendCount); + $this->assertNotNull($spy->captured); + + $message = $spy->captured; + $this->assertSame('legacy@example.test', $message->getTo()[0]['email'] ?? ''); + $this->assertSame('Legacy User', $message->getTo()[0]['name'] ?? ''); + $this->assertSame('Hello Legacy', $message->getSubject()); + $this->assertSame('sender@example.test', $message->getFromEmail()); + $this->assertSame('Custom Sender', $message->getFromName()); + $this->assertSame('reply@example.test', $message->getReplyToEmail()); + $this->assertSame('Custom Reply', $message->getReplyToName()); + } + + public function testLegacyPayloadSmtpOverridesProjectSmtp(): void + { + $worker = new Notifications(); + $project = new Document([ + 'smtp' => [ + 'enabled' => true, + 'host' => 'project.smtp.test', + 'port' => 2525, + 'senderEmail' => 'project@example.test', + ], + ]); + + $reflection = new \ReflectionMethod($worker, 'resolveSmtpConfig'); + $reflection->setAccessible(true); + /** @var array $smtp */ + $smtp = $reflection->invoke($worker, $project, [ + 'smtp' => [ + 'host' => 'payload.smtp.test', + 'port' => 587, + 'senderEmail' => 'payload@example.test', + ], + ]); + + $this->assertSame('payload.smtp.test', $smtp['host']); + $this->assertSame(587, $smtp['port']); + $this->assertSame('payload@example.test', $smtp['senderEmail']); + } + + public function testLegacyMailPayloadThrowsWhenSmtpIsNotConfigured(): void + { + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST='); + + try { + $worker = new Notifications(); + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipient' => 'legacy@example.test', + 'subject' => 'No SMTP', + 'body' => 'Body', + ]; + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Skipped mail processing. No SMTP configuration has been set.'); + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + } + public function testWebhookRecipientForwardsSignatureKey(): void { $worker = new SpyNotifications(); From 41e4483c3fe11564bc49ea495fc4be3e0f5ad35f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 11:21:28 +1200 Subject: [PATCH 53/80] test: update compose worker count --- tests/unit/Docker/ComposeTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index 570904f7c3..dceed2200c 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -23,11 +23,13 @@ class ComposeTest extends TestCase public function testServices(): void { - $this->assertCount(17, $this->object->getServices()); + $this->assertCount(16, $this->object->getServices()); $this->assertEquals('appwrite', $this->object->getService('appwrite')->getContainerName()); $this->assertEquals('', $this->object->getService('appwrite')->getImageVersion()); $this->assertEquals('3.6', $this->object->getService('traefik')->getImageVersion()); $this->assertEquals(['2080' => '80', '2443' => '443', '8080' => '8080'], $this->object->getService('traefik')->getPorts()); + $this->assertArrayNotHasKey('appwrite-worker-mails', $this->object->getServices()); + $this->assertEquals('appwrite-worker-notifications', $this->object->getService('appwrite-worker-notifications')->getContainerName()); } public function testNetworks(): void From 7479cf495ac436e21fc900d0fdc3e41d3a482621 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 11:44:38 +1200 Subject: [PATCH 54/80] fix: personalize webhook pause emails --- src/Appwrite/Platform/Workers/Webhooks.php | 68 ++++++++++---------- tests/unit/Platform/Workers/WebhooksTest.php | 67 +++++++++++++++++++ 2 files changed, 100 insertions(+), 35 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 67cc6cb3b6..51cbfc3798 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -248,30 +248,17 @@ class Webhooks extends Action $region = $project->getAttribute('region', 'default'); $webhookId = $webhook->getId(); - $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; $consoleHostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', 'localhost')); - $template->setParam('{{user}}', 'there'); - $template->setParam('{{webhook}}', $webhook->getAttribute('name')); - $template->setParam('{{project}}', $project->getAttribute('name')); - $template->setParam('{{url}}', $webhook->getAttribute('url')); - $template->setParam('{{error}}', 'The server returned ' . $statusCode . ' status code'); - $template->setParam('{{host}}', $protocol . '://' . $consoleHostname); - $template->setParam('{{path}}', "/console/project-$region-$projectId/settings/webhooks/$webhookId"); - $template->setParam('{{attempts}}', $attempts); - $subject = 'Webhook deliveries have been paused'; $preview = 'Webhook "' . $webhook->getAttribute('name') . '" has been paused after ' . $attempts . ' failed delivery attempts.'; - $recipients = []; - foreach ($users as $user) { $email = $user->getAttribute('email'); $userId = $user->getId(); - $recipients[] = [ + $recipients = [[ 'address' => $userId, 'channel' => NOTIFICATION_TYPE_CONSOLE, 'resourceType' => RESOURCE_TYPE_USERS, @@ -280,7 +267,7 @@ class Webhooks extends Action 'parentResourceType' => RESOURCE_TYPE_PROJECTS, 'parentResourceId' => $projectId, 'parentResourceInternalId' => (string) $projectInternalId, - ]; + ]]; if (!empty($email)) { $recipients[] = [ @@ -294,27 +281,38 @@ class Webhooks extends Action 'parentResourceInternalId' => (string) $projectInternalId, ]; } - } - $publisherForNotifications->enqueue(new NotificationMessage( - project: $project, - recipients: $recipients, - deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $webhook->getUpdatedAt(), - subject: $subject, - bodyTemplate: __DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl', - body: $template->render(), - preview: $preview, - variables: [ - 'logoUrl' => $plan['logoUrl'] ?? APP_EMAIL_LOGO_URL, - 'accentColor' => $plan['accentColor'] ?? APP_EMAIL_ACCENT_COLOR, - 'twitter' => $plan['twitterUrl'] ?? APP_SOCIAL_TWITTER, - 'discord' => $plan['discordUrl'] ?? APP_SOCIAL_DISCORD, - 'github' => $plan['githubUrl'] ?? APP_SOCIAL_GITHUB_APPWRITE, - 'terms' => $plan['termsUrl'] ?? APP_EMAIL_TERMS_URL, - 'privacy' => $plan['privacyUrl'] ?? APP_EMAIL_PRIVACY_URL, - 'platform' => $plan['platformName'] ?? APP_NAME, - ], - )); + $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); + $userName = (string) ($user->getAttribute('name', 'there') ?: 'there'); + $template->setParam('{{user}}', $userName); + $template->setParam('{{webhook}}', $webhook->getAttribute('name')); + $template->setParam('{{project}}', $project->getAttribute('name')); + $template->setParam('{{url}}', $webhook->getAttribute('url')); + $template->setParam('{{error}}', 'The server returned ' . $statusCode . ' status code'); + $template->setParam('{{host}}', $protocol . '://' . $consoleHostname); + $template->setParam('{{path}}', "/console/project-$region-$projectId/settings/webhooks/$webhookId"); + $template->setParam('{{attempts}}', $attempts); + + $publisherForNotifications->enqueue(new NotificationMessage( + project: $project, + recipients: $recipients, + deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $webhook->getUpdatedAt(), + subject: $subject, + bodyTemplate: __DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl', + body: $template->render(), + preview: $preview, + variables: [ + 'logoUrl' => $plan['logoUrl'] ?? APP_EMAIL_LOGO_URL, + 'accentColor' => $plan['accentColor'] ?? APP_EMAIL_ACCENT_COLOR, + 'twitter' => $plan['twitterUrl'] ?? APP_SOCIAL_TWITTER, + 'discord' => $plan['discordUrl'] ?? APP_SOCIAL_DISCORD, + 'github' => $plan['githubUrl'] ?? APP_SOCIAL_GITHUB_APPWRITE, + 'terms' => $plan['termsUrl'] ?? APP_EMAIL_TERMS_URL, + 'privacy' => $plan['privacyUrl'] ?? APP_EMAIL_PRIVACY_URL, + 'platform' => $plan['platformName'] ?? APP_NAME, + ], + )); + } } private static function hasOwnerRole(Document $membership, string $projectId): bool diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 1d681394f9..28568f9476 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -66,6 +66,7 @@ class WebhooksTest extends TestCase \realpath($payload['bodyTemplate']) ); $this->assertStringContainsString('Payments', $payload['body']); + $this->assertStringContainsString('Ada Lovelace', $payload['body']); $this->assertStringContainsString('/console/project-fra-project-1/settings/webhooks/webhook-1', $payload['body']); $this->assertStringNotContainsString('{{', $payload['body']); $this->assertSame(APP_NAME, $payload['variables']['platform']); @@ -93,6 +94,69 @@ class WebhooksTest extends TestCase ], $payload['recipients'][1]); } + public function testSendAlertPersonalizesBodyPerOwner(): void + { + $database = $this->createPlatformDatabase(); + $this->seedOwnerUser($database); + $database->createDocument('memberships', new Document([ + '$id' => 'membership-3', + 'teamInternalId' => 'team-internal-1', + 'userId' => 'user-3', + 'roles' => 'owner', + ])); + $database->createDocument('users', new Document([ + '$id' => 'user-3', + '$sequence' => 103, + 'email' => 'grace@example.test', + 'name' => 'Grace Hopper', + ])); + + $publisher = new MockPublisher(); + $publisherForNotifications = new NotificationPublisher($publisher, new Queue('v1-notifications')); + $worker = new Webhooks(); + + $worker->sendAlert( + attempts: 10, + statusCode: 500, + webhook: new Document([ + '$id' => 'webhook-1', + '$updatedAt' => '2026-01-01T00:00:00.000+00:00', + 'name' => 'Payments', + 'url' => 'https://example.test/webhook', + ]), + project: new Document([ + '$id' => 'project-1', + '$sequence' => 'project-internal-1', + 'name' => 'Production', + 'teamInternalId' => 'team-internal-1', + 'region' => 'fra', + ]), + dbForPlatform: $database, + publisherForNotifications: $publisherForNotifications, + plan: [] + ); + + $events = $publisher->getEvents('v1-notifications'); + + $this->assertCount(2, $events); + + $bodies = []; + foreach ($events as $event) { + foreach ($event['recipients'] as $recipient) { + if ($recipient['channel'] === NOTIFICATION_TYPE_EMAIL) { + $bodies[$recipient['address']] = $event['body']; + } + } + } + + $this->assertArrayHasKey('owner@example.test', $bodies); + $this->assertArrayHasKey('grace@example.test', $bodies); + $this->assertStringContainsString('Ada Lovelace', $bodies['owner@example.test']); + $this->assertStringNotContainsString('Grace Hopper', $bodies['owner@example.test']); + $this->assertStringContainsString('Grace Hopper', $bodies['grace@example.test']); + $this->assertStringNotContainsString('Ada Lovelace', $bodies['grace@example.test']); + } + public function testSendAlertDeduplicationKeyChangesPerPauseCycle(): void { $database = $this->createPlatformDatabase(); @@ -184,6 +248,7 @@ class WebhooksTest extends TestCase $database->createAttribute('memberships', 'roles', Database::VAR_STRING, 1024, true); $database->createCollection('users', [], [], $permissions, false); $database->createAttribute('users', 'email', Database::VAR_STRING, 320, false); + $database->createAttribute('users', 'name', Database::VAR_STRING, 256, false); return $database; } @@ -206,11 +271,13 @@ class WebhooksTest extends TestCase '$id' => 'user-1', '$sequence' => 101, 'email' => 'owner@example.test', + 'name' => 'Ada Lovelace', ])); $database->createDocument('users', new Document([ '$id' => 'user-2', '$sequence' => 102, 'email' => 'developer@example.test', + 'name' => 'Developer User', ])); } } From a8772a53b8bac28c7101f85228b31521d7da30a4 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sun, 17 May 2026 12:04:51 +1200 Subject: [PATCH 55/80] fix: drain legacy mail queue --- app/views/install/compose.phtml | 38 ++++++++++++++++++++ app/worker.php | 6 ++-- bin/worker-mails | 2 +- docker-compose.yml | 42 +++++++++++++++++++++++ src/Appwrite/Docker/Compose/Service.php | 5 +++ tests/resources/docker/docker-compose.yml | 22 ++++++++++++ tests/unit/Docker/ComposeTest.php | 22 ++++++------ 7 files changed, 122 insertions(+), 15 deletions(-) diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 1c5783dce6..a7c69bf0c7 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -596,6 +596,44 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_DOCKER_HUB_PASSWORD - _APP_LOGGING_CONFIG + appwrite-worker-mails: + image: /: + entrypoint: worker-mails + <<: *x-logging + container_name: appwrite-worker-mails + restart: unless-stopped + networks: + - appwrite + depends_on: + redis: + condition: service_healthy + : + condition: service_healthy + environment: + - _APP_ENV + - _APP_WORKER_PER_CORE + - _APP_OPENSSL_KEY_V1 + - _APP_SYSTEM_EMAIL_NAME + - _APP_SYSTEM_EMAIL_ADDRESS + - _APP_DB_ADAPTER + - _APP_DB_HOST + - _APP_DB_PORT + - _APP_DB_SCHEMA + - _APP_DB_USER + - _APP_DB_PASS + - _APP_REDIS_HOST + - _APP_REDIS_PORT + - _APP_REDIS_USER + - _APP_REDIS_PASS + - _APP_SMTP_HOST + - _APP_SMTP_PORT + - _APP_SMTP_SECURE + - _APP_SMTP_USERNAME + - _APP_SMTP_PASSWORD + - _APP_LOGGING_CONFIG + - _APP_DOMAIN + - _APP_OPTIONS_FORCE_HTTPS + appwrite-worker-notifications: image: /: entrypoint: worker-notifications diff --git a/app/worker.php b/app/worker.php index a064ee8f76..b9e5cbd2a7 100644 --- a/app/worker.php +++ b/app/worker.php @@ -73,9 +73,7 @@ if (! isset($args[1])) { \array_shift($args); $workerName = $args[0]; -if ($workerName === 'mails') { - $workerName = 'notifications'; -} +$actionName = $workerName === 'mails' ? 'notifications' : $workerName; if (\str_starts_with($workerName, 'databases')) { $queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main'); @@ -112,7 +110,7 @@ try { $platform->setWorker($worker); $platform->init(Service::TYPE_WORKER, [ - 'workerName' => strtolower($workerName), + 'workerName' => strtolower($actionName), ]); } catch (\Throwable $e) { Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine()); diff --git a/bin/worker-mails b/bin/worker-mails index 1b7bf28745..a28b1ddc67 100644 --- a/bin/worker-mails +++ b/bin/worker-mails @@ -1,3 +1,3 @@ #!/bin/sh -exec php /usr/src/code/app/worker.php notifications "$@" +exec php /usr/src/code/app/worker.php mails "$@" diff --git a/docker-compose.yml b/docker-compose.yml index 3ac1121290..ca18dd64cd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -764,6 +764,48 @@ services: - _APP_LOGGING_PROVIDER - _APP_DATABASE_SHARED_TABLES + appwrite-worker-mails: + entrypoint: worker-mails + <<: [*x-logging, *x-build] + container_name: appwrite-worker-mails + image: appwrite-dev + networks: + - appwrite + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + depends_on: + - redis + - maildev + - ${_APP_DB_HOST:-mariadb} + # - smtp + environment: + - _APP_ENV + - _APP_WORKER_PER_CORE + - _APP_POOL_ADAPTER + - _APP_OPENSSL_KEY_V1 + - _APP_SYSTEM_EMAIL_NAME + - _APP_SYSTEM_EMAIL_ADDRESS + - _APP_REDIS_HOST + - _APP_REDIS_PORT + - _APP_REDIS_USER + - _APP_REDIS_PASS + - _APP_DB_HOST + - _APP_DB_PORT + - _APP_DB_SCHEMA + - _APP_DB_USER + - _APP_DB_PASS + - _APP_DB_ADAPTER + - _APP_SMTP_HOST + - _APP_SMTP_PORT + - _APP_SMTP_SECURE + - _APP_SMTP_USERNAME + - _APP_SMTP_PASSWORD + - _APP_LOGGING_CONFIG + - _APP_DOMAIN + - _APP_OPTIONS_FORCE_HTTPS + - _APP_DATABASE_SHARED_TABLES + appwrite-worker-notifications: entrypoint: worker-notifications <<: [*x-logging, *x-build] diff --git a/src/Appwrite/Docker/Compose/Service.php b/src/Appwrite/Docker/Compose/Service.php index e7993d6927..0ea84c12f6 100644 --- a/src/Appwrite/Docker/Compose/Service.php +++ b/src/Appwrite/Docker/Compose/Service.php @@ -54,6 +54,11 @@ class Service return substr($image, ((int)strpos($image, ':')) + 1); } + public function getEntrypoint(): string + { + return (isset($this->service['entrypoint'])) ? $this->service['entrypoint'] : ''; + } + /** * @return Env */ diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index c25796dc08..5d59d3ae77 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -267,6 +267,28 @@ services: - _APP_COMPUTE_MEMORY - _APP_EXECUTOR_HOST + appwrite-worker-mails: + entrypoint: worker-mails + container_name: appwrite-worker-mails + build: + context: . + restart: unless-stopped + networks: + - appwrite + depends_on: + - redis + - maildev + # - smtp + environment: + - _APP_ENV + - _APP_REDIS_HOST + - _APP_REDIS_PORT + - _APP_DB_HOST + - _APP_DB_PORT + - _APP_DB_SCHEMA + - _APP_DB_USER + - _APP_DB_PASS + appwrite-worker-notifications: entrypoint: worker-notifications container_name: appwrite-worker-notifications diff --git a/tests/unit/Docker/ComposeTest.php b/tests/unit/Docker/ComposeTest.php index dceed2200c..c7efbda710 100644 --- a/tests/unit/Docker/ComposeTest.php +++ b/tests/unit/Docker/ComposeTest.php @@ -23,13 +23,15 @@ class ComposeTest extends TestCase public function testServices(): void { - $this->assertCount(16, $this->object->getServices()); - $this->assertEquals('appwrite', $this->object->getService('appwrite')->getContainerName()); - $this->assertEquals('', $this->object->getService('appwrite')->getImageVersion()); - $this->assertEquals('3.6', $this->object->getService('traefik')->getImageVersion()); - $this->assertEquals(['2080' => '80', '2443' => '443', '8080' => '8080'], $this->object->getService('traefik')->getPorts()); - $this->assertArrayNotHasKey('appwrite-worker-mails', $this->object->getServices()); - $this->assertEquals('appwrite-worker-notifications', $this->object->getService('appwrite-worker-notifications')->getContainerName()); + $this->assertCount(17, $this->object->getServices()); + $this->assertSame('appwrite', $this->object->getService('appwrite')->getContainerName()); + $this->assertSame('', $this->object->getService('appwrite')->getImageVersion()); + $this->assertSame('3.6', $this->object->getService('traefik')->getImageVersion()); + $this->assertSame(['2080' => '80', '2443' => '443', '8080' => '8080'], $this->object->getService('traefik')->getPorts()); + $this->assertSame('appwrite-worker-mails', $this->object->getService('appwrite-worker-mails')->getContainerName()); + $this->assertSame('worker-mails', $this->object->getService('appwrite-worker-mails')->getEntrypoint()); + $this->assertSame('appwrite-worker-notifications', $this->object->getService('appwrite-worker-notifications')->getContainerName()); + $this->assertSame('worker-notifications', $this->object->getService('appwrite-worker-notifications')->getEntrypoint()); } public function testNetworks(): void @@ -40,8 +42,8 @@ class ComposeTest extends TestCase public function testVolumes(): void { $this->assertCount(7, $this->object->getVolumes()); - $this->assertEquals('appwrite-mariadb', $this->object->getVolumes()[0]); - $this->assertEquals('appwrite-redis', $this->object->getVolumes()[1]); - $this->assertEquals('appwrite-cache', $this->object->getVolumes()[2]); + $this->assertSame('appwrite-mariadb', $this->object->getVolumes()[0]); + $this->assertSame('appwrite-redis', $this->object->getVolumes()[1]); + $this->assertSame('appwrite-cache', $this->object->getVolumes()[2]); } } From a4b719a666b5e76818689e1c7ecd54829291cf3a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 15:16:08 +1200 Subject: [PATCH 56/80] feat: track notification logo views --- app/config/collections/platform.php | 22 ++ app/init/constants.php | 1 + docs/references/account/get-alert-track.md | 1 - docs/references/account/list-alerts.md | 2 +- docs/references/notifications/get-logo.md | 1 + src/Appwrite/Migration/Version/V24.php | 15 ++ src/Appwrite/Platform/Appwrite.php | 2 + .../Modules/Account/Http/Alerts/Track/Get.php | 106 --------- .../Modules/Account/Services/Http.php | 4 +- .../Http/Notifications/Logos/Appwrite/Get.php | 209 ++++++++++++++++++ .../Platform/Modules/Notifications/Module.php | 14 ++ .../Modules/Notifications/Services/Http.php | 16 ++ .../Platform/Workers/Notifications.php | 36 ++- src/Appwrite/SDK/ContentType.php | 1 + .../SDK/Specification/Format/OpenAPI3.php | 1 + .../SDK/Specification/Format/Swagger2.php | 1 + .../Database/Validator/Queries/Alerts.php | 2 + src/Appwrite/Utopia/Response/Model/Alert.php | 14 ++ src/Appwrite/Utopia/Response/Model/Log.php | 2 +- .../Notifications/NotificationsBase.php | 157 +++++++------ .../unit/Migration/MigrationVersionsTest.php | 42 ++++ .../Platform/Workers/NotificationsTest.php | 58 +++-- .../Utopia/Messaging/Adapter/ConsoleTest.php | 6 + 23 files changed, 486 insertions(+), 227 deletions(-) delete mode 100644 docs/references/account/get-alert-track.md create mode 100644 docs/references/notifications/get-logo.md delete mode 100644 src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php create mode 100644 src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php create mode 100644 src/Appwrite/Platform/Modules/Notifications/Module.php create mode 100644 src/Appwrite/Platform/Modules/Notifications/Services/Http.php diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 262161a0d5..3c144a989b 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1227,6 +1227,28 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('firstSeen'), + 'type' => Database::VAR_DATETIME, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['datetime'], + ], + [ + '$id' => ID::custom('lastSeen'), + 'type' => Database::VAR_DATETIME, + 'format' => '', + 'size' => 0, + 'signed' => false, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => ['datetime'], + ], ], 'indexes' => [ [ diff --git a/app/init/constants.php b/app/init/constants.php index 255fc8ee1e..ffb2a1ec80 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -164,6 +164,7 @@ const SESSION_PROVIDER_SERVER = 'server'; const ACTIVITY_TYPE_USER = 'user'; const ACTIVITY_TYPE_ADMIN = 'admin'; const ACTIVITY_TYPE_GUEST = 'guest'; +const ACTIVITY_TYPE_HIDDEN = 'hidden'; const ACTIVITY_TYPE_KEY_PROJECT = 'keyProject'; const ACTIVITY_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTIVITY_TYPE_KEY_ORGANIZATION = 'keyOrganization'; diff --git a/docs/references/account/get-alert-track.md b/docs/references/account/get-alert-track.md deleted file mode 100644 index 8b313fe5c4..0000000000 --- a/docs/references/account/get-alert-track.md +++ /dev/null @@ -1 +0,0 @@ -Track an alert as opened by returning a 1x1 transparent tracking pixel. When loaded with a valid tracking token, the alert is automatically marked as read. diff --git a/docs/references/account/list-alerts.md b/docs/references/account/list-alerts.md index 5b7c063bec..ff20fcda76 100644 --- a/docs/references/account/list-alerts.md +++ b/docs/references/account/list-alerts.md @@ -1 +1 @@ -Get the list of alerts for the currently logged in user. Use queries to filter the results by attributes such as read status or creation date. +Get the list of alerts for the currently logged in user. Use queries to filter the results by attributes such as read status, view timestamps, or creation date. diff --git a/docs/references/notifications/get-logo.md b/docs/references/notifications/get-logo.md new file mode 100644 index 0000000000..c58b48cb52 --- /dev/null +++ b/docs/references/notifications/get-logo.md @@ -0,0 +1 @@ +Get the Appwrite logo SVG used in notification emails. When loaded with a valid tracking token, the related alert is marked as read and its view timestamps are updated. diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index ffb83417ce..027b948c69 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -87,8 +87,10 @@ class V24 extends Migration }; $collections = $this->collections[$collectionType]; + $alertsCollectionExists = false; if ($collectionType === 'console') { + $alertsCollectionExists = !$this->dbForProject->getCollection('alerts')->isEmpty(); $this->createCollection('alerts'); } @@ -171,6 +173,19 @@ class V24 extends Migration $this->dbForProject->purgeCachedCollection($id); break; + case 'alerts': + if ($collectionType === 'console' && $alertsCollectionExists) { + foreach (['firstSeen', 'lastSeen'] as $attribute) { + try { + $this->createAttributeFromCollection($this->dbForProject, $id, $attribute); + } catch (Throwable $th) { + Console::warning("Failed to create attribute \"{$attribute}\" in collection {$id}: {$th->getMessage()}"); + } + } + } + $this->dbForProject->purgeCachedCollection($id); + break; + case 'rules': if ($collectionType === 'console') { try { diff --git a/src/Appwrite/Platform/Appwrite.php b/src/Appwrite/Platform/Appwrite.php index a9cd1a8e2f..e40879e2a4 100644 --- a/src/Appwrite/Platform/Appwrite.php +++ b/src/Appwrite/Platform/Appwrite.php @@ -11,6 +11,7 @@ use Appwrite\Platform\Modules\Databases; use Appwrite\Platform\Modules\Functions; use Appwrite\Platform\Modules\Health; use Appwrite\Platform\Modules\Migrations; +use Appwrite\Platform\Modules\Notifications; use Appwrite\Platform\Modules\Project; use Appwrite\Platform\Modules\Projects; use Appwrite\Platform\Modules\Proxy; @@ -33,6 +34,7 @@ class Appwrite extends Platform $this->addModule(new Projects\Module()); $this->addModule(new Functions\Module()); $this->addModule(new Health\Module()); + $this->addModule(new Notifications\Module()); $this->addModule(new Sites\Module()); $this->addModule(new Console\Module()); $this->addModule(new Proxy\Module()); diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php b/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php deleted file mode 100644 index 704ad46763..0000000000 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Track/Get.php +++ /dev/null @@ -1,106 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/account/alerts/:alertId/track') - ->desc('Track alert') - ->groups(['api', 'account']) - ->label('scope', 'public') - ->label('sdk', new Method( - namespace: 'account', - group: 'alerts', - name: 'getAlertTrack', - description: '/docs/references/account/get-alert-track.md', - auth: [], - responses: [ - new SDKResponse( - code: Response::STATUS_CODE_OK, - model: Response::MODEL_NONE, - ), - ], - contentType: ContentType::IMAGE_PNG, - )) - ->param('alertId', '', new UID(), 'Alert ID.') - ->param('jwt', '', new Text(2048, 0), 'Tracking token.', true) - ->inject('response') - ->inject('dbForPlatform') - ->inject('authorization') - ->callback($this->action(...)); - } - - public function action( - string $alertId, - string $jwt, - Response $response, - Database $dbForPlatform, - Authorization $authorization, - ): void { - $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); - - if (!empty($secret) && $jwt !== '') { - try { - $decoder = new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0); - $decoded = $decoder->decode($jwt); - - if ( - isset($decoded['alertId'], $decoded['recipientHash'], $decoded['projectId'], $decoded['purpose']) - && $decoded['purpose'] === 'alert_track' - && $decoded['alertId'] === $alertId - ) { - $authorization->skip(function () use ($dbForPlatform, $alertId, $decoded) { - $alert = $dbForPlatform->getDocument('alerts', $alertId); - - if ( - !$alert->isEmpty() - && $alert->getAttribute('recipientHash') === $decoded['recipientHash'] - && $alert->getAttribute('projectId') === $decoded['projectId'] - && (!isset($decoded['projectInternalId']) || $alert->getAttribute('projectInternalId') === $decoded['projectInternalId']) - && $alert->getAttribute('read') !== true - ) { - $dbForPlatform->updateDocument('alerts', $alertId, new Document([ - 'read' => true, - ])); - } - }); - } - } catch (\Throwable) { - // Silent fail — never reveal JWT validity through response status - } - } - - // 1x1 transparent PNG (canonical 67-byte payload) - $pixel = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='); - - $response - ->setContentType('image/png') - ->addHeader('Cache-Control', 'no-store') - ->send($pixel); - } -} diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index c7d69fa45c..6297cbd487 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,7 +12,6 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; -use Appwrite\Platform\Modules\Account\Http\Alerts\Track\Get as TrackAlert; use Appwrite\Platform\Modules\Account\Http\Alerts\Update as UpdateAlert; use Appwrite\Platform\Modules\Account\Http\Alerts\XList as ListAlerts; use Utopia\Platform\Service; @@ -34,7 +33,6 @@ class Http extends Service ->addAction(CreateChallenge::getName(), new CreateChallenge()) ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) ->addAction(ListAlerts::getName(), new ListAlerts()) - ->addAction(UpdateAlert::getName(), new UpdateAlert()) - ->addAction(TrackAlert::getName(), new TrackAlert()); + ->addAction(UpdateAlert::getName(), new UpdateAlert()); } } diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php new file mode 100644 index 0000000000..de2194bf3d --- /dev/null +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php @@ -0,0 +1,209 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/notifications/logos/appwrite') + ->desc('Get notification logo') + ->groups(['api', 'notifications']) + ->label('scope', 'public') + ->label('sdk', new Method( + namespace: 'notifications', + group: 'logos', + name: 'getLogo', + description: '/docs/references/notifications/get-logo.md', + auth: [], + type: MethodType::LOCATION, + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_NONE, + ), + ], + contentType: ContentType::IMAGE_SVG, + )) + ->param('jwt', '', new Text(2048, 0), 'Tracking token.', true) + ->param('theme', 'system', new WhiteList(['system', 'light', 'dark']), 'Logo color theme.', true) + ->inject('request') + ->inject('response') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('publisherForAudits') + ->callback($this->action(...)); + } + + public function action( + string $jwt, + string $theme, + Request $request, + Response $response, + Database $dbForPlatform, + Authorization $authorization, + AuditPublisher $publisherForAudits, + ): void { + $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); + + if (!empty($secret) && $jwt !== '') { + try { + $decoder = new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0); + $decoded = $decoder->decode($jwt); + + $result = $authorization->skip(fn () => $this->trackView($decoded, $dbForPlatform)); + + if ($result !== null) { + $this->logView( + $result['alert'], + $result['project'], + $result['seenAt'], + $request, + $publisherForAudits, + ); + } + } catch (\Throwable) { + // Always return the logo and avoid exposing token validity. + } + } + + $response + ->setContentType('image/svg+xml') + ->addHeader('Cache-Control', 'no-store') + ->send($this->renderLogo($theme)); + } + + /** + * @return array{alert: Document, project: Document, seenAt: string}|null + */ + private function trackView(array $decoded, Database $dbForPlatform): ?array + { + if ( + !isset($decoded['messageId'], $decoded['recipientHash'], $decoded['channel'], $decoded['projectId'], $decoded['purpose']) + || $decoded['purpose'] !== 'alert_track' + ) { + return null; + } + + $alerts = $dbForPlatform->find('alerts', [ + Query::equal('messageId', [(string) $decoded['messageId']]), + Query::equal('channel', [(string) $decoded['channel']]), + Query::equal('recipientHash', [(string) $decoded['recipientHash']]), + Query::limit(1), + ]); + + if (empty($alerts)) { + return null; + } + + $alert = $alerts[0]; + if ( + $alert->getAttribute('projectId') !== (string) $decoded['projectId'] + || (isset($decoded['projectInternalId']) && $alert->getAttribute('projectInternalId') !== (string) $decoded['projectInternalId']) + ) { + return null; + } + + $seenAt = DateTime::now(); + $updates = [ + 'read' => true, + 'lastSeen' => $seenAt, + ]; + + if (empty($alert->getAttribute('firstSeen'))) { + $updates['firstSeen'] = $seenAt; + } + + $updated = $dbForPlatform->updateDocument('alerts', $alert->getId(), new Document($updates)); + $project = $dbForPlatform->getDocument('projects', $alert->getAttribute('projectId')); + + if ($project->isEmpty()) { + $project = new Document([ + '$id' => 'console', + '$sequence' => 'console', + ]); + } + + return [ + 'alert' => $updated, + 'project' => $project, + 'seenAt' => $seenAt, + ]; + } + + private function logView(Document $alert, Document $project, string $seenAt, Request $request, AuditPublisher $publisherForAudits): void + { + $publisherForAudits->enqueue(new AuditMessage( + event: 'alert.view', + payload: [ + 'alertId' => $alert->getId(), + 'messageId' => $alert->getAttribute('messageId', ''), + 'channel' => $alert->getAttribute('channel', ''), + 'projectId' => $alert->getAttribute('projectId', ''), + 'firstSeen' => $alert->getAttribute('firstSeen'), + 'lastSeen' => $alert->getAttribute('lastSeen'), + 'seenAt' => $seenAt, + ], + project: $project, + user: new Document([ + '$id' => $alert->getAttribute('resourceId', ''), + '$sequence' => $alert->getAttribute('resourceInternalId', ''), + 'name' => '', + 'email' => '', + 'type' => ACTIVITY_TYPE_HIDDEN, + ]), + resource: 'alert/' . $alert->getId(), + mode: APP_MODE_DEFAULT, + ip: $request->getIP(), + userAgent: $request->getUserAgent(''), + hostname: $request->getHostname(), + )); + } + + private function renderLogo(string $theme): string + { + $style = match ($theme) { + 'light' => '.wordmark { fill: #19191C; }', + 'dark' => '.wordmark { fill: #F5F5F7; }', + default => '.wordmark { fill: #19191C; } @media (prefers-color-scheme: dark) { .wordmark { fill: #F5F5F7; } }', + }; + + return << + + + + Appwrite + +SVG; + } +} diff --git a/src/Appwrite/Platform/Modules/Notifications/Module.php b/src/Appwrite/Platform/Modules/Notifications/Module.php new file mode 100644 index 0000000000..f4e1c574cc --- /dev/null +++ b/src/Appwrite/Platform/Modules/Notifications/Module.php @@ -0,0 +1,14 @@ +addService('http', new Http()); + } +} diff --git a/src/Appwrite/Platform/Modules/Notifications/Services/Http.php b/src/Appwrite/Platform/Modules/Notifications/Services/Http.php new file mode 100644 index 0000000000..b274c92fa1 --- /dev/null +++ b/src/Appwrite/Platform/Modules/Notifications/Services/Http.php @@ -0,0 +1,16 @@ +type = Service::TYPE_HTTP; + + $this->addAction(GetAppwriteLogo::getName(), new GetAppwriteLogo()); + } +} diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 3a82a405d9..00949aa068 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -274,14 +274,10 @@ class Notifications extends Action } $subject = \strip_tags($subjectTemplate->render()); - $deterministicAlertId = $messageId !== '' - ? self::buildAlertId($messageId, $recipient) - : null; - $recipientHash = self::buildRecipientHash($recipient); $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); - if ($deterministicAlertId !== null && !empty($opensslKey)) { - $body = $this->injectTrackingPixel($body, $deterministicAlertId, $recipientHash, $project, $opensslKey); + if ($messageId !== '' && !empty($opensslKey)) { + $body = $this->injectTrackingLogo($body, $messageId, $recipient['channel'], $recipientHash, $project, $opensslKey); } /** @var EmailAdapter $adapter */ @@ -459,8 +455,7 @@ class Notifications extends Action } /** - * Persist an alert row. Returns the alertId so callers can build a - * tracking-pixel URL or otherwise reference the row. + * Persist an alert row. Returns the alertId so callers can reference the row. * * Idempotent: on a duplicate composite-key violation the existing * row's id is returned. @@ -518,9 +513,7 @@ class Notifications extends Action /** * Build the deterministic alertId composed of the messageId plus a - * recipient hash. Single source of truth used - * by both `persistAlert()` and `dispatchEmail()` so the tracking pixel - * URL matches the eventually-persisted row exactly. + * recipient hash. * * @param array{address: string, channel: string, signatureKey?: string, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient */ @@ -637,17 +630,18 @@ class Notifications extends Action } /** - * Splice a 1x1 tracking pixel before the last `` tag (or - * append at the end if the body has no closing tag). The pixel + * Splice a visible tracking logo before the last `` tag (or + * append at the end if the body has no closing tag). The logo URL * carries a signed JWT identifying the alert recipient, which the - * `/v1/account/alerts/:alertId/track` endpoint verifies before - * marking the alert as read. + * `/v1/notifications/logos/appwrite` endpoint verifies before marking + * the alert as read and recording view timestamps. */ - private function injectTrackingPixel(string $body, string $alertId, string $recipientHash, Document $project, string $opensslKey): string + private function injectTrackingLogo(string $body, string $messageId, string $channel, string $recipientHash, Document $project, string $opensslKey): string { $jwt = (new JWT($opensslKey, 'HS256', ALERT_TRACKING_JWT_TTL, 0)) ->encode([ - 'alertId' => $alertId, + 'messageId' => $messageId, + 'channel' => $channel, 'recipientHash' => $recipientHash, 'projectId' => $project->getId(), 'projectInternalId' => $project->getSequence(), @@ -657,14 +651,14 @@ class Notifications extends Action $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_DOMAIN', 'localhost'); - $pixelUrl = $protocol . '://' . $hostname . '/v1/account/alerts/' . $alertId . '/track?jwt=' . \urlencode($jwt); - $pixel = ''; + $logoUrl = $protocol . '://' . $hostname . '/v1/notifications/logos/appwrite?jwt=' . \urlencode($jwt); + $logo = 'Appwrite logo'; // Case-insensitive splice before the LAST . if (\preg_match('/<\/body\s*>(?!.*<\/body\s*>)/is', $body)) { - return \preg_replace('/<\/body\s*>(?!.*<\/body\s*>)/is', $pixel . '$0', $body, 1) ?? ($body . $pixel); + return \preg_replace('/<\/body\s*>(?!.*<\/body\s*>)/is', $logo . '$0', $body, 1) ?? ($body . $logo); } - return $body . $pixel; + return $body . $logo; } } diff --git a/src/Appwrite/SDK/ContentType.php b/src/Appwrite/SDK/ContentType.php index 174c889815..7a1671df7a 100644 --- a/src/Appwrite/SDK/ContentType.php +++ b/src/Appwrite/SDK/ContentType.php @@ -8,6 +8,7 @@ enum ContentType: string case JSON = 'application/json'; case IMAGE = 'image/*'; case IMAGE_PNG = 'image/png'; + case IMAGE_SVG = 'image/svg+xml'; case MULTIPART = 'multipart/form-data'; case HTML = 'text/html'; case TEXT = 'text/plain'; diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 68ab7a0986..08ca654fae 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -313,6 +313,7 @@ class OpenAPI3 extends Format 'image/jpeg', 'image/gif', 'image/png', + 'image/svg+xml', 'image/webp', 'image/svg-x', 'image/x-icon', diff --git a/src/Appwrite/SDK/Specification/Format/Swagger2.php b/src/Appwrite/SDK/Specification/Format/Swagger2.php index fb1ef66eca..07843643c8 100644 --- a/src/Appwrite/SDK/Specification/Format/Swagger2.php +++ b/src/Appwrite/SDK/Specification/Format/Swagger2.php @@ -316,6 +316,7 @@ class Swagger2 extends Format 'image/jpeg', 'image/gif', 'image/png', + 'image/svg+xml', 'image/webp', 'image/svg-x', 'image/x-icon', diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php index d0c7490cd7..7045364cb7 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php @@ -14,6 +14,8 @@ class Alerts extends Base 'resourceId', 'parentResourceType', 'parentResourceId', + 'firstSeen', + 'lastSeen', ]; /** diff --git a/src/Appwrite/Utopia/Response/Model/Alert.php b/src/Appwrite/Utopia/Response/Model/Alert.php index 84f66ecb4c..cd554801d5 100644 --- a/src/Appwrite/Utopia/Response/Model/Alert.php +++ b/src/Appwrite/Utopia/Response/Model/Alert.php @@ -97,6 +97,20 @@ class Alert extends Model 'example' => false, 'required' => false, ]) + ->addRule('firstSeen', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'First time the alert was viewed from a notification logo.', + 'default' => null, + 'example' => self::TYPE_DATETIME_EXAMPLE, + 'required' => false, + ]) + ->addRule('lastSeen', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'Most recent time the alert was viewed from a notification logo.', + 'default' => null, + 'example' => self::TYPE_DATETIME_EXAMPLE, + 'required' => false, + ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/Log.php b/src/Appwrite/Utopia/Response/Model/Log.php index 1e48a6bc89..038eb9eea7 100644 --- a/src/Appwrite/Utopia/Response/Model/Log.php +++ b/src/Appwrite/Utopia/Response/Model/Log.php @@ -42,7 +42,7 @@ class Log extends Model ]) ->addRule('userType', [ 'type' => self::TYPE_STRING, - 'description' => 'User type who triggered the audit log. Possible values: user, admin, guest, keyProject, keyAccount, keyOrganization.', + 'description' => 'User type who triggered the audit log. Possible values: user, admin, guest, hidden, keyProject, keyAccount, keyOrganization.', 'default' => '', 'example' => 'user', ]) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index a610d89e15..cc120b96ec 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -24,7 +24,7 @@ use Utopia\System\System; * * - GET /v1/account/alerts (empty + populated) * - PATCH /v1/account/alerts/:alertId (happy + unauthorized) - * - GET /v1/account/alerts/:alertId/track (valid JWT + invalid JWT) + * - GET /v1/notifications/logos/appwrite (valid JWT + invalid JWT) * * Dedup, per-channel dispatch, and webhook signing are covered by: * - tests/unit/Platform/Workers/NotificationsTest.php @@ -232,51 +232,64 @@ trait NotificationsBase self::$seededAlertId = $alertId; } - public function testTrackingPixelTogglesRead(): void + public function testNotificationLogoTracksViews(): void { $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); + $alert = $this->findConsoleAlert($alertId); + $this->assertFalse($alert['read']); + $this->assertEmpty($alert['firstSeen'] ?? null); + $this->assertEmpty($alert['lastSeen'] ?? null); $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); - $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for tracking pixel test'); + $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for notification logo test'); $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); - // Track endpoint requires `purpose: 'alert_track'` — see C/M7 in - // PR #12195 review. Other claim purposes are silently ignored (which - // testTrackingPixelRejectsJwtWithoutPurposeClaim covers). + // Logo endpoint requires `purpose: 'alert_track'`. Other claim + // purposes are silently ignored, which the purpose-claim test covers. $jwt = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ - 'alertId' => $alertId, + 'messageId' => $alert['messageId'], + 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, - 'projectId' => $this->getProject()['$id'], + 'projectId' => $alert['projectId'], 'purpose' => 'alert_track', ]); $response = $this->client->call( Client::METHOD_GET, - '/account/alerts/' . $alertId . '/track', + '/notifications/logos/appwrite', + ['x-appwrite-project' => 'console'], + [ + 'jwt' => $jwt, + 'theme' => 'dark', + ] + ); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); + $this->assertNotEmpty($response['body']); + $this->assertStringStartsWith('assertStringContainsString('Appwrite', $response['body']); + + $tracked = $this->findConsoleAlert($alertId); + $this->assertTrue($tracked['read']); + $this->assertNotEmpty($tracked['firstSeen']); + $this->assertNotEmpty($tracked['lastSeen']); + + \sleep(1); + + $secondResponse = $this->client->call( + Client::METHOD_GET, + '/notifications/logos/appwrite', ['x-appwrite-project' => 'console'], ['jwt' => $jwt] ); - $this->assertSame(200, $response['headers']['status-code']); - $this->assertStringContainsString('image/png', $response['headers']['content-type']); - $this->assertNotEmpty($response['body']); - $this->assertSame("\x89PNG\r\n\x1a\n", \substr($response['body'], 0, 8), 'Response body must be a PNG.'); + $this->assertSame(200, $secondResponse['headers']['status-code']); - // Subsequent listing should report alert as read. - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - $found = null; - foreach ($list['body']['alerts'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - - $this->assertNotNull($found); - $this->assertTrue($found['read']); + $viewedAgain = $this->findConsoleAlert($alertId); + $this->assertSame($tracked['firstSeen'], $viewedAgain['firstSeen']); + $this->assertNotSame($tracked['lastSeen'], $viewedAgain['lastSeen']); self::$seededAlertId = null; } @@ -287,109 +300,91 @@ trait NotificationsBase * with the same secret (sessions, password reset, etc.) could be * replayed against this endpoint to mark arbitrary alerts as read. * - * The endpoint always returns the 1x1 PNG (200 image/png) — the only + * The endpoint always returns the SVG logo (200 image/svg+xml) — the only * observable difference is whether the alert flips to `read: true`. */ - public function testTrackingPixelRejectsJwtWithoutPurposeClaim(): void + public function testNotificationLogoRejectsJwtWithoutPurposeClaim(): void { $alertId = $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); + $alert = $this->findConsoleAlert($alertId); $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for the JWT purpose-claim test'); $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); - // Mint a JWT with valid alertId/recipientHash but NO purpose claim. + // Mint a JWT with valid message/recipient claims but NO purpose claim. $jwtNoPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ - 'alertId' => $alertId, + 'messageId' => $alert['messageId'], + 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, - 'projectId' => $this->getProject()['$id'], + 'projectId' => $alert['projectId'], ]); $response = $this->client->call( Client::METHOD_GET, - '/account/alerts/' . $alertId . '/track', + '/notifications/logos/appwrite', ['x-appwrite-project' => 'console'], ['jwt' => $jwtNoPurpose] ); $this->assertSame(200, $response['headers']['status-code'], 'endpoint must always return 200'); - $this->assertStringContainsString('image/png', $response['headers']['content-type']); + $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); - $found = null; - foreach ($list['body']['alerts'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - $this->assertNotNull($found); + $found = $this->findConsoleAlert($alertId); $this->assertFalse($found['read'], 'JWT without purpose claim must not flip the read flag'); + $this->assertEmpty($found['firstSeen'] ?? null); + $this->assertEmpty($found['lastSeen'] ?? null); // Mint a JWT with a wrong purpose value: same expectation, silently rejected. $jwtWrongPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ - 'alertId' => $alertId, + 'messageId' => $alert['messageId'], + 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, - 'projectId' => $this->getProject()['$id'], + 'projectId' => $alert['projectId'], 'purpose' => 'something_else', ]); $response = $this->client->call( Client::METHOD_GET, - '/account/alerts/' . $alertId . '/track', + '/notifications/logos/appwrite', ['x-appwrite-project' => 'console'], ['jwt' => $jwtWrongPurpose] ); $this->assertSame(200, $response['headers']['status-code']); - $this->assertStringContainsString('image/png', $response['headers']['content-type']); + $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); - $found = null; - foreach ($list['body']['alerts'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - $this->assertNotNull($found); + $found = $this->findConsoleAlert($alertId); $this->assertFalse($found['read'], 'JWT with wrong purpose value must not flip the read flag'); + $this->assertEmpty($found['firstSeen'] ?? null); + $this->assertEmpty($found['lastSeen'] ?? null); self::$seededAlertId = $alertId; } - public function testTrackingPixelInvalidTokenReturnsPng(): void + public function testNotificationLogoInvalidTokenReturnsSvg(): void { $alertId = $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); $response = $this->client->call( Client::METHOD_GET, - '/account/alerts/' . $alertId . '/track', + '/notifications/logos/appwrite', ['x-appwrite-project' => 'console'], ['jwt' => 'tampered-or-empty'] ); $this->assertSame(200, $response['headers']['status-code']); - $this->assertStringContainsString('image/png', $response['headers']['content-type']); + $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); $this->assertNotEmpty($response['body']); - $this->assertSame("\x89PNG\r\n\x1a\n", \substr($response['body'], 0, 8), 'Response body must be a PNG.'); + $this->assertStringStartsWith('client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - $found = null; - foreach ($list['body']['alerts'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - - $this->assertNotNull($found); + $found = $this->findConsoleAlert($alertId); $this->assertFalse($found['read']); + $this->assertEmpty($found['firstSeen'] ?? null); + $this->assertEmpty($found['lastSeen'] ?? null); self::$seededAlertId = $alertId; } @@ -418,6 +413,24 @@ trait NotificationsBase ]; } + /** + * @return array + */ + protected function findConsoleAlert(string $alertId): array + { + $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $this->assertSame(200, $list['headers']['status-code']); + + foreach ($list['body']['alerts'] as $alert) { + if ($alert['$id'] === $alertId) { + return $alert; + } + } + + $this->fail('Alert not present in /account/alerts response: ' . $alertId); + return []; + } + /** * Drive a webhook past the failure threshold so the * Webhooks worker emits a console+email alert fanout to the project diff --git a/tests/unit/Migration/MigrationVersionsTest.php b/tests/unit/Migration/MigrationVersionsTest.php index 499c2fdeb3..7958d08f05 100644 --- a/tests/unit/Migration/MigrationVersionsTest.php +++ b/tests/unit/Migration/MigrationVersionsTest.php @@ -70,6 +70,8 @@ class MigrationVersionsTest extends TestCase } $this->assertArrayHasKey('resourceInternalId', $attributes); $this->assertArrayHasKey('parentResourceInternalId', $attributes); + $this->assertArrayHasKey('firstSeen', $attributes); + $this->assertArrayHasKey('lastSeen', $attributes); $indexes = []; foreach ($collection->getAttribute('indexes', []) as $index) { @@ -88,4 +90,44 @@ class MigrationVersionsTest extends TestCase $this->assertSame(['projectId', 'projectInternalId', 'resourceType', 'resourceId', 'resourceInternalId'], $indexes['_key_project_resource']); $this->assertSame(['projectId', 'projectInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'], $indexes['_key_project_parent_resource']); } + + public function testV24AddsSeenAttributesToExistingAlertsCollection(): void + { + require_once __DIR__ . '/../../../app/init.php'; + + $authorization = new Authorization(); + $database = new Database(new Memory(), new Cache(new NoCache())); + $database + ->setAuthorization($authorization) + ->setDatabase('migrationV24ExistingAlerts') + ->setNamespace('migration_existing_alerts_' . \uniqid()); + $database->create(); + $database->createCollection('alerts'); + + $migration = new V24(); + $migration->setProject( + new Document(['$id' => 'console', '$sequence' => 'console']), + $database, + $database, + $authorization, + ); + + $migrateCollections = new \ReflectionMethod($migration, 'migrateCollections'); + \ob_start(); + try { + $migrateCollections->invoke($migration); + } finally { + \ob_end_clean(); + } + + $collection = $database->getCollection('alerts'); + $attributes = []; + foreach ($collection->getAttribute('attributes', []) as $attribute) { + $id = $attribute instanceof Document ? $attribute->getAttribute('$id') : ($attribute['$id'] ?? ''); + $attributes[$id] = $attribute; + } + + $this->assertArrayHasKey('firstSeen', $attributes); + $this->assertArrayHasKey('lastSeen', $attributes); + } } diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 62166071f2..26e34bd660 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -191,6 +191,8 @@ class NotificationsTest extends TestCase $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); + $this->database->createAttribute('alerts', 'firstSeen', Database::VAR_DATETIME, 0, false); + $this->database->createAttribute('alerts', 'lastSeen', Database::VAR_DATETIME, 0, false); // Mirror the production `_key_recipient` UNIQUE composite index so the // duplicate-handling branch in persistAlert (catch DuplicateException -> @@ -683,13 +685,13 @@ class NotificationsTest extends TestCase $this->assertSame('project-internal-x', $rows[0]->getAttribute('parentResourceInternalId')); } - public function testTrackingPixelInjectedIntoEmailHtml(): void + public function testTrackingLogoInjectedIntoEmailHtml(): void { $spy = new SpyEmailAdapter(); $this->registry->set('smtp', static fn () => $spy); // Force the cloud SMTP branch (project has no smtp config) and - // provide an OpenSSL key so injectTrackingPixel actually runs. + // provide an OpenSSL key so injectTrackingLogo actually runs. $previousSmtpHost = \getenv('_APP_SMTP_HOST'); $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); $previousDomain = \getenv('_APP_DOMAIN'); @@ -709,7 +711,7 @@ class NotificationsTest extends TestCase ], 'subject' => 'Heads up', 'body' => 'plain body', - 'deduplicationKey' => 'pixel-key', + 'deduplicationKey' => 'logo-key', ]; $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); @@ -723,26 +725,40 @@ class NotificationsTest extends TestCase $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); $body = $spy->captured->getContent(); - $this->assertStringContainsString(' must be present'); - $this->assertStringContainsString('/v1/account/alerts/', $body); - $this->assertStringContainsString('/track?jwt=', $body); - $this->assertStringContainsString('http://api.example.test/v1/account/alerts/', \html_entity_decode($body)); - $this->assertStringNotContainsString('console.example.test/v1/account/alerts/', \html_entity_decode($body)); + $this->assertStringContainsString(' must be present'); + $this->assertStringContainsString('/v1/notifications/logos/appwrite?jwt=', $body); + $this->assertStringContainsString('alt="Appwrite logo"', $body); + $this->assertStringContainsString('width="120"', $body); + $this->assertStringContainsString('height="28"', $body); + $this->assertStringNotContainsString('display:none', $body); + $this->assertStringContainsString('http://api.example.test/v1/notifications/logos/appwrite?jwt=', \html_entity_decode($body)); + $this->assertStringNotContainsString('console.example.test/v1/notifications/logos/appwrite', \html_entity_decode($body)); - \preg_match('/track\?jwt=([^"&]+)/', \html_entity_decode($body), $matches); + \preg_match('/logos\/appwrite\?jwt=([^"&]+)/', \html_entity_decode($body), $matches); $this->assertNotEmpty($matches[1] ?? ''); $claims = (new JWT('test-key-32bytes-min-aaaaaaaaaaaaaa', 'HS256', ALERT_TRACKING_JWT_TTL, 0)) ->decode(\urldecode($matches[1])); + $this->assertSame(\md5('logo-key'), $claims['messageId'] ?? null); + $this->assertSame(NOTIFICATION_TYPE_EMAIL, $claims['channel'] ?? null); + $this->assertSame($this->recipientHash( + NOTIFICATION_TYPE_EMAIL, + 'user@example.test', + RESOURCE_TYPE_USERS, + 'user-7', + 'user-7-internal', + ), $claims['recipientHash'] ?? null); $this->assertSame('project-x', $claims['projectId'] ?? null); $this->assertSame('project-internal-x', $claims['projectInternalId'] ?? null); + $this->assertSame('alert_track', $claims['purpose'] ?? null); + $this->assertArrayNotHasKey('alertId', $claims); - // The pixel must sit BEFORE the last . + // The logo must sit BEFORE the last . $lastBodyClose = \strripos($body, ''); - $pixelPosition = \strripos($body, ''); - $this->assertNotFalse($pixelPosition); - $this->assertLessThan($lastBodyClose, $pixelPosition, 'pixel must be spliced before the final '); + $this->assertNotFalse($logoPosition); + $this->assertLessThan($lastBodyClose, $logoPosition, 'logo must be spliced before the final '); } public function testPersistAlertReturnsExistingAlertIdOnDuplicate(): void @@ -1015,7 +1031,7 @@ class NotificationsTest extends TestCase * Worker happy-path: email channel. * * Asserts the SMTP adapter is invoked once with the expected - * to/subject/body, the rendered body carries the tracking pixel before + * to/subject/body, the rendered body carries the tracking logo before * ``, and an alert row is persisted AFTER the send returns * successfully (see C1: persist-after-send invariant). */ @@ -1056,15 +1072,14 @@ class NotificationsTest extends TestCase $this->assertSame('Welcome aboard', $message->getSubject()); $body = $message->getContent(); - $this->assertStringContainsString('assertStringContainsString('/v1/account/alerts/', $body); - $this->assertStringContainsString('/track?jwt=', $body); + $this->assertStringContainsString('assertStringContainsString('/v1/notifications/logos/appwrite?jwt=', $body); $closing = \strripos($body, ''); - $pixel = \strripos($body, ''); + $this->assertNotFalse($logo); + $this->assertLessThan($closing, $logo, 'logo must precede the closing '); $this->assertSame(1, $worker->persistAlertCalls, 'email channel must persist exactly once after a successful send'); $messageId = \md5('happy-email'); @@ -1077,8 +1092,7 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_TYPE_EMAIL, $row->getAttribute('channel')); $this->assertFalse($row->getAttribute('read')); - // dispatchEmail's returned alertId must match the row $id (used by the - // tracking pixel URL). + // dispatchEmail's returned alertId must match the row $id. $this->assertSame($worker->persistedIds[0], $row->getId()); $this->assertLessThanOrEqual(36, \strlen($row->getId()), 'alert ids must pass the UID route validator'); } diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index dc84baddf3..3b87e8854e 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -48,6 +48,9 @@ class ConsoleTest extends TestCase $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); + $this->database->createAttribute('alerts', 'firstSeen', Database::VAR_DATETIME, 0, false); + $this->database->createAttribute('alerts', 'lastSeen', Database::VAR_DATETIME, 0, false); } protected function tearDown(): void @@ -118,6 +121,9 @@ class ConsoleTest extends TestCase $this->assertSame('Hello', $stored->getAttribute('title')); $this->assertSame('World', $stored->getAttribute('body')); $this->assertSame('info', $stored->getAttribute('type')); + $this->assertFalse($stored->getAttribute('read')); + $this->assertNull($stored->getAttribute('firstSeen')); + $this->assertNull($stored->getAttribute('lastSeen')); } public function testUserPermissionsScopedToRecipient(): void From 5b9e2eaca713f70b941af7d20846494cc23ea537 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 15:26:57 +1200 Subject: [PATCH 57/80] fix: remove unreachable alert helper return --- tests/e2e/Services/Notifications/NotificationsBase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index cc120b96ec..77deeb3039 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -428,7 +428,6 @@ trait NotificationsBase } $this->fail('Alert not present in /account/alerts response: ' . $alertId); - return []; } /** From d69844c08de1c18cb99fea502abea071a3696465 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 16:12:39 +1200 Subject: [PATCH 58/80] fix: address notification review feedback --- .env | 1 + app/config/variables.php | 9 ++ app/init/constants.php | 2 +- app/init/models.php | 6 +- app/init/resources.php | 2 +- app/views/install/compose.phtml | 3 + docker-compose.yml | 3 + docs/references/account/list-alerts.md | 1 - docs/references/account/update-alert.md | 1 - docs/references/notifications/get-logo.md | 2 +- .../notifications/list-notifications.md | 1 + .../notifications/update-notification.md | 1 + src/Appwrite/Docker/Compose.php | 4 +- .../Modules/Account/Services/Http.php | 6 +- .../Http/Notifications/Logos/Appwrite/Get.php | 50 +++++------ .../Http/Notifications}/Update.php | 36 ++++---- .../Http/Notifications}/XList.php | 37 ++++---- .../Modules/Notifications/Services/Http.php | 7 +- .../Platform/Workers/Notifications.php | 23 +++-- .../Queries/{Alerts.php => Notifications.php} | 6 +- src/Appwrite/Utopia/Response.php | 6 +- .../Model/{Alert.php => Notification.php} | 44 ++++------ .../Notifications/NotificationsBase.php | 88 +++++++++---------- tests/resources/docker/docker-compose.yml | 3 + .../Platform/Workers/NotificationsTest.php | 65 +++++++++++--- 25 files changed, 226 insertions(+), 181 deletions(-) delete mode 100644 docs/references/account/list-alerts.md delete mode 100644 docs/references/account/update-alert.md create mode 100644 docs/references/notifications/list-notifications.md create mode 100644 docs/references/notifications/update-notification.md rename src/Appwrite/Platform/Modules/{Account/Http/Alerts => Notifications/Http/Notifications}/Update.php (63%) rename src/Appwrite/Platform/Modules/{Account/Http/Alerts => Notifications/Http/Notifications}/XList.php (70%) rename src/Appwrite/Utopia/Database/Validator/Queries/{Alerts.php => Notifications.php} (85%) rename src/Appwrite/Utopia/Response/Model/{Alert.php => Notification.php} (74%) diff --git a/.env b/.env index 4a6a3ac344..f6e3f1d8ca 100644 --- a/.env +++ b/.env @@ -24,6 +24,7 @@ _APP_OPTIONS_ROUTER_PROTECTION=disabled _APP_OPTIONS_FORCE_HTTPS=disabled _APP_OPTIONS_ROUTER_FORCE_HTTPS=disabled _APP_OPENSSL_KEY_V1=your-secret-key +_APP_NOTIFICATIONS_TRACKING_SECRET=your-secret-key _APP_DNS=172.16.238.100 # CoreDNS _APP_DOMAIN=appwrite.test _APP_CONSOLE_DOMAIN=localhost diff --git a/app/config/variables.php b/app/config/variables.php index 90df9b4518..e33c149889 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -79,6 +79,15 @@ return [ 'question' => 'Choose a secret API key, make sure to make a backup of your key in a secure location', 'filter' => 'token' ], + [ + 'name' => '_APP_NOTIFICATIONS_TRACKING_SECRET', + 'description' => 'Secret key used to sign notification logo tracking tokens. This must be the same value for API containers and notification workers. Change it from the default value before running in production.', + 'introduction' => '1.9.0', + 'default' => 'your-secret-key', + 'required' => true, + 'question' => '', + 'filter' => 'token' + ], [ 'name' => '_APP_DOMAIN', 'description' => 'Your Appwrite domain address. When setting a public suffix domain, Appwrite will attempt to issue a valid SSL certificate automatically. When used with a dev domain, Appwrite will assign a self-signed SSL certificate. The default value is \'localhost\'. Multiple domains can be separated by commas.', diff --git a/app/init/constants.php b/app/init/constants.php index 187c0b1d3a..8f064c6efc 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -269,7 +269,7 @@ const NOTIFICATION_TYPE_SMS = MESSAGE_TYPE_SMS; const NOTIFICATION_TYPE_PUSH = MESSAGE_TYPE_PUSH; const NOTIFICATION_TYPE_CONSOLE = 'console'; const NOTIFICATION_TYPE_WEBHOOK = 'webhook'; -const ALERT_TRACKING_JWT_TTL = 60 * 60 * 24 * 7; +const NOTIFICATION_TRACKING_JWT_TTL = 60 * 60 * 24 * 7; // API key types const API_KEY_STANDARD = 'standard'; const API_KEY_EPHEMERAL = 'ephemeral'; diff --git a/app/init/models.php b/app/init/models.php index 112344df57..6c73bcf7dd 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -2,7 +2,6 @@ use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Account; -use Appwrite\Utopia\Response\Model\Alert; use Appwrite\Utopia\Response\Model\AlgoArgon2; use Appwrite\Utopia\Response\Model\AlgoBcrypt; use Appwrite\Utopia\Response\Model\AlgoMd5; @@ -116,6 +115,7 @@ use Appwrite\Utopia\Response\Model\MigrationReport; use Appwrite\Utopia\Response\Model\Mock; use Appwrite\Utopia\Response\Model\MockNumber; use Appwrite\Utopia\Response\Model\None; +use Appwrite\Utopia\Response\Model\Notification; use Appwrite\Utopia\Response\Model\OAuth2Amazon; use Appwrite\Utopia\Response\Model\OAuth2Apple; use Appwrite\Utopia\Response\Model\OAuth2Auth0; @@ -249,7 +249,7 @@ Response::setModel(new BaseList('Column Indexes List', Response::MODEL_COLUMN_IN Response::setModel(new BaseList('Users List', Response::MODEL_USER_LIST, 'users', Response::MODEL_USER)); Response::setModel(new BaseList('Sessions List', Response::MODEL_SESSION_LIST, 'sessions', Response::MODEL_SESSION)); Response::setModel(new BaseList('Identities List', Response::MODEL_IDENTITY_LIST, 'identities', Response::MODEL_IDENTITY)); -Response::setModel(new BaseList('Alerts List', Response::MODEL_ALERT_LIST, 'alerts', Response::MODEL_ALERT)); +Response::setModel(new BaseList('Notifications List', Response::MODEL_NOTIFICATION_LIST, 'notifications', Response::MODEL_NOTIFICATION)); Response::setModel(new BaseList('Logs List', Response::MODEL_LOG_LIST, 'logs', Response::MODEL_LOG)); Response::setModel(new BaseList('Files List', Response::MODEL_FILE_LIST, 'files', Response::MODEL_FILE)); Response::setModel(new BaseList('Buckets List', Response::MODEL_BUCKET_LIST, 'buckets', Response::MODEL_BUCKET)); @@ -380,7 +380,7 @@ Response::setModel(new Account()); Response::setModel(new Preferences()); Response::setModel(new Session()); Response::setModel(new Identity()); -Response::setModel(new Alert()); +Response::setModel(new Notification()); Response::setModel(new Token()); Response::setModel(new JWT()); Response::setModel(new Locale()); diff --git a/app/init/resources.php b/app/init/resources.php index 5237803815..90551d83bc 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -133,7 +133,7 @@ $container->set('publisherForDeletes', fn (Publisher $publisher) => new DeletePu $container->set('publisherForMails', fn (Publisher $publisher) => new MailPublisher( $publisher, - new Queue(System::getEnv('_APP_NOTIFICATIONS_QUEUE_NAME', Event::NOTIFICATIONS_QUEUE_NAME)) + new Queue(System::getEnv('_APP_MAILS_QUEUE_NAME', Event::MAILS_QUEUE_NAME)) ), ['publisher']); $container->set('publisherForMessaging', fn (Publisher $publisher) => new MessagingPublisher( diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index a7c69bf0c7..5188ee7588 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -103,6 +103,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_OPTIONS_FORCE_HTTPS - _APP_OPTIONS_ROUTER_FORCE_HTTPS - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_DOMAIN - _APP_DOMAIN_TARGET_CNAME - _APP_DOMAIN_TARGET_AAAA @@ -613,6 +614,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_ENV - _APP_WORKER_PER_CORE - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_SYSTEM_EMAIL_NAME - _APP_SYSTEM_EMAIL_ADDRESS - _APP_DB_ADAPTER @@ -651,6 +653,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_ENV - _APP_WORKER_PER_CORE - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_SYSTEM_EMAIL_NAME - _APP_SYSTEM_EMAIL_ADDRESS - _APP_DB_ADAPTER diff --git a/docker-compose.yml b/docker-compose.yml index ca18dd64cd..112342959e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -142,6 +142,7 @@ services: - _APP_OPTIONS_FORCE_HTTPS - _APP_OPTIONS_ROUTER_FORCE_HTTPS - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_DOMAIN - _APP_CONSOLE_DOMAIN - _APP_CONSOLE_TRUSTED_PROJECTS @@ -784,6 +785,7 @@ services: - _APP_WORKER_PER_CORE - _APP_POOL_ADAPTER - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_SYSTEM_EMAIL_NAME - _APP_SYSTEM_EMAIL_ADDRESS - _APP_REDIS_HOST @@ -825,6 +827,7 @@ services: - _APP_WORKER_PER_CORE - _APP_POOL_ADAPTER - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_SYSTEM_EMAIL_NAME - _APP_SYSTEM_EMAIL_ADDRESS - _APP_REDIS_HOST diff --git a/docs/references/account/list-alerts.md b/docs/references/account/list-alerts.md deleted file mode 100644 index ff20fcda76..0000000000 --- a/docs/references/account/list-alerts.md +++ /dev/null @@ -1 +0,0 @@ -Get the list of alerts for the currently logged in user. Use queries to filter the results by attributes such as read status, view timestamps, or creation date. diff --git a/docs/references/account/update-alert.md b/docs/references/account/update-alert.md deleted file mode 100644 index 288a3d1b1a..0000000000 --- a/docs/references/account/update-alert.md +++ /dev/null @@ -1 +0,0 @@ -Update an alert for the currently logged in user. Use the read parameter to mark the alert as read or unread. diff --git a/docs/references/notifications/get-logo.md b/docs/references/notifications/get-logo.md index c58b48cb52..eadc755c69 100644 --- a/docs/references/notifications/get-logo.md +++ b/docs/references/notifications/get-logo.md @@ -1 +1 @@ -Get the Appwrite logo SVG used in notification emails. When loaded with a valid tracking token, the related alert is marked as read and its view timestamps are updated. +Get the Appwrite logo SVG used in notification emails. When loaded with a valid tracking token, the related notification is marked as read and its view timestamps are updated. diff --git a/docs/references/notifications/list-notifications.md b/docs/references/notifications/list-notifications.md new file mode 100644 index 0000000000..3d9f93789c --- /dev/null +++ b/docs/references/notifications/list-notifications.md @@ -0,0 +1 @@ +Get the list of notifications for the currently logged in console user. Use queries to filter the results by attributes such as read status, view timestamps, or creation date. diff --git a/docs/references/notifications/update-notification.md b/docs/references/notifications/update-notification.md new file mode 100644 index 0000000000..220c79b70e --- /dev/null +++ b/docs/references/notifications/update-notification.md @@ -0,0 +1 @@ +Update a notification by its unique ID. Use the `read` parameter to mark the notification as read or unread. diff --git a/src/Appwrite/Docker/Compose.php b/src/Appwrite/Docker/Compose.php index 9ea6420d2d..f879ad7aec 100644 --- a/src/Appwrite/Docker/Compose.php +++ b/src/Appwrite/Docker/Compose.php @@ -4,6 +4,7 @@ namespace Appwrite\Docker; use Appwrite\Docker\Compose\Service; use Exception; +use Spyc; class Compose { @@ -14,7 +15,8 @@ class Compose public function __construct(string $data) { - $this->compose = yaml_parse($data); + $compose = \function_exists('yaml_parse') ? \yaml_parse($data) : Spyc::YAMLLoadString($data); + $this->compose = \is_array($compose) ? $compose : []; $this->compose['services'] = (isset($this->compose['services']) && is_array($this->compose['services'])) ? $this->compose['services'] : []; diff --git a/src/Appwrite/Platform/Modules/Account/Services/Http.php b/src/Appwrite/Platform/Modules/Account/Services/Http.php index 6297cbd487..ae2e841636 100644 --- a/src/Appwrite/Platform/Modules/Account/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Account/Services/Http.php @@ -12,8 +12,6 @@ use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Create as C use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Get as GetRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\RecoveryCodes\Update as UpdateRecoveryCodes; use Appwrite\Platform\Modules\Account\Http\Account\MFA\Update as UpdateMfa; -use Appwrite\Platform\Modules\Account\Http\Alerts\Update as UpdateAlert; -use Appwrite\Platform\Modules\Account\Http\Alerts\XList as ListAlerts; use Utopia\Platform\Service; class Http extends Service @@ -31,8 +29,6 @@ class Http extends Service ->addAction(UpdateRecoveryCodes::getName(), new UpdateRecoveryCodes()) ->addAction(GetRecoveryCodes::getName(), new GetRecoveryCodes()) ->addAction(CreateChallenge::getName(), new CreateChallenge()) - ->addAction(UpdateChallenge::getName(), new UpdateChallenge()) - ->addAction(ListAlerts::getName(), new ListAlerts()) - ->addAction(UpdateAlert::getName(), new UpdateAlert()); + ->addAction(UpdateChallenge::getName(), new UpdateChallenge()); } } diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php index c9571dd078..cace972628 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php @@ -73,18 +73,18 @@ class Get extends Action Authorization $authorization, AuditPublisher $publisherForAudits, ): void { - $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); + $secret = System::getEnv('_APP_NOTIFICATIONS_TRACKING_SECRET'); if (!empty($secret) && $jwt !== '') { try { - $decoder = new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0); + $decoder = new JWT($secret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0); $decoded = $decoder->decode($jwt); $result = $authorization->skip(fn () => $this->trackView($decoded, $dbForPlatform)); if ($result !== null) { $this->logView( - $result['alert'], + $result['notification'], $result['project'], $result['seenAt'], $request, @@ -103,32 +103,32 @@ class Get extends Action } /** - * @return array{alert: Document, project: Document, seenAt: string}|null + * @return array{notification: Document, project: Document, seenAt: string}|null */ private function trackView(array $decoded, Database $dbForPlatform): ?array { if ( !isset($decoded['messageId'], $decoded['recipientHash'], $decoded['channel'], $decoded['projectId'], $decoded['purpose']) - || $decoded['purpose'] !== 'alert_track' + || $decoded['purpose'] !== 'notification_track' ) { return null; } - $alerts = $dbForPlatform->find('alerts', [ + $notifications = $dbForPlatform->find('alerts', [ Query::equal('messageId', [(string) $decoded['messageId']]), Query::equal('channel', [(string) $decoded['channel']]), Query::equal('recipientHash', [(string) $decoded['recipientHash']]), Query::limit(1), ]); - if (empty($alerts)) { + if (empty($notifications)) { return null; } - $alert = $alerts[0]; + $notification = $notifications[0]; if ( - $alert->getAttribute('projectId') !== (string) $decoded['projectId'] - || (isset($decoded['projectInternalId']) && $alert->getAttribute('projectInternalId') !== (string) $decoded['projectInternalId']) + $notification->getAttribute('projectId') !== (string) $decoded['projectId'] + || (isset($decoded['projectInternalId']) && $notification->getAttribute('projectInternalId') !== (string) $decoded['projectInternalId']) ) { return null; } @@ -139,12 +139,12 @@ class Get extends Action 'lastSeen' => $seenAt, ]; - if (empty($alert->getAttribute('firstSeen'))) { + if (empty($notification->getAttribute('firstSeen'))) { $updates['firstSeen'] = $seenAt; } - $updated = $dbForPlatform->updateDocument('alerts', $alert->getId(), new Document($updates)); - $project = $dbForPlatform->getDocument('projects', $alert->getAttribute('projectId')); + $updated = $dbForPlatform->updateDocument('alerts', $notification->getId(), new Document($updates)); + $project = $dbForPlatform->getDocument('projects', $notification->getAttribute('projectId')); if ($project->isEmpty()) { $project = new Document([ @@ -154,34 +154,34 @@ class Get extends Action } return [ - 'alert' => $updated, + 'notification' => $updated, 'project' => $project, 'seenAt' => $seenAt, ]; } - private function logView(Document $alert, Document $project, string $seenAt, Request $request, AuditPublisher $publisherForAudits): void + private function logView(Document $notification, Document $project, string $seenAt, Request $request, AuditPublisher $publisherForAudits): void { $publisherForAudits->enqueue(new AuditMessage( - event: 'alert.view', + event: 'notification.view', payload: [ - 'alertId' => $alert->getId(), - 'messageId' => $alert->getAttribute('messageId', ''), - 'channel' => $alert->getAttribute('channel', ''), - 'projectId' => $alert->getAttribute('projectId', ''), - 'firstSeen' => $alert->getAttribute('firstSeen'), - 'lastSeen' => $alert->getAttribute('lastSeen'), + 'notificationId' => $notification->getId(), + 'messageId' => $notification->getAttribute('messageId', ''), + 'channel' => $notification->getAttribute('channel', ''), + 'projectId' => $notification->getAttribute('projectId', ''), + 'firstSeen' => $notification->getAttribute('firstSeen'), + 'lastSeen' => $notification->getAttribute('lastSeen'), 'seenAt' => $seenAt, ], project: $project, user: new Document([ - '$id' => $alert->getAttribute('resourceId', ''), - '$sequence' => $alert->getAttribute('resourceInternalId', ''), + '$id' => $notification->getAttribute('resourceId', ''), + '$sequence' => $notification->getAttribute('resourceInternalId', ''), 'name' => '', 'email' => '', 'type' => ACTOR_TYPE_HIDDEN, ]), - resource: 'alert/' . $alert->getId(), + resource: 'notification/' . $notification->getId(), mode: APP_MODE_DEFAULT, ip: $request->getIP(), userAgent: $request->getUserAgent(''), diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php similarity index 63% rename from src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php rename to src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php index 30555e247e..d7769c6732 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/Update.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/account/alerts/:alertId') - ->desc('Update alert') - ->groups(['api', 'account']) + ->setHttpPath('/v1/notifications/:notificationId') + ->desc('Update notification') + ->groups(['api', 'notifications']) ->label('scope', 'account') ->label('sdk', new Method( - namespace: 'account', - group: 'alerts', - name: 'updateAlert', - description: '/docs/references/account/update-alert.md', + namespace: 'notifications', + group: null, + name: 'update', + description: '/docs/references/notifications/update-notification.md', auth: [AuthType::SESSION, AuthType::JWT], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, - model: Response::MODEL_ALERT, + model: Response::MODEL_NOTIFICATION, ) ] )) - ->param('alertId', '', new UID(), 'Alert ID.') - ->param('read', null, new Boolean(), 'Alert read status.') + ->param('notificationId', '', new UID(), 'Notification ID.') + ->param('read', null, new Boolean(), 'Notification read status.') ->inject('response') ->inject('dbForPlatform') ->inject('authorization') @@ -56,7 +56,7 @@ class Update extends Action } public function action( - string $alertId, + string $notificationId, bool $read, Response $response, Database $dbForPlatform, @@ -68,20 +68,20 @@ class Update extends Action throw new Exception(Exception::USER_UNAUTHORIZED); } - $alert = $dbForPlatform->getDocument('alerts', $alertId); + $notification = $dbForPlatform->getDocument('alerts', $notificationId); - if ($alert->isEmpty()) { - $exists = $authorization->skip(fn () => !$dbForPlatform->getDocument('alerts', $alertId)->isEmpty()); + if ($notification->isEmpty()) { + $exists = $authorization->skip(fn () => !$dbForPlatform->getDocument('alerts', $notificationId)->isEmpty()); if ($exists) { throw new Exception(Exception::USER_UNAUTHORIZED); } throw new Exception(Exception::DOCUMENT_NOT_FOUND); } - $updated = $dbForPlatform->updateDocument('alerts', $alertId, new Document([ + $updated = $dbForPlatform->updateDocument('alerts', $notificationId, new Document([ 'read' => $read, ])); - $response->dynamic($updated, Response::MODEL_ALERT); + $response->dynamic($updated, Response::MODEL_NOTIFICATION); } } diff --git a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php similarity index 70% rename from src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php rename to src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php index 015ae32d9e..461a210016 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Alerts/XList.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php @@ -1,12 +1,12 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/account/alerts') - ->desc('List alerts') - ->groups(['api', 'account']) + ->setHttpPath('/v1/notifications') + ->desc('List notifications') + ->groups(['api', 'notifications']) ->label('scope', 'account') ->label('sdk', new Method( - namespace: 'account', - group: 'alerts', - name: 'listAlerts', - description: '/docs/references/account/list-alerts.md', + namespace: 'notifications', + group: null, + name: 'list', + description: '/docs/references/notifications/list-notifications.md', auth: [AuthType::SESSION, AuthType::JWT], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, - model: Response::MODEL_ALERT_LIST, + model: Response::MODEL_NOTIFICATION_LIST, ) ] )) - ->param('queries', [], new Alerts(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Alerts::ALLOWED_ATTRIBUTES), true) + ->param('queries', [], new NotificationQueries(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', NotificationQueries::ALLOWED_ATTRIBUTES), true) ->inject('response') ->inject('dbForPlatform') ->inject('project') @@ -75,9 +75,6 @@ class XList extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); } - /** - * Get cursor document if there was a cursor query, we use array_filter and reset for reference $cursor to $queries - */ $cursor = \array_filter($queries, function ($query) { return \in_array($query->getMethod(), [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]); }); @@ -90,11 +87,11 @@ class XList extends Action throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription()); } - $alertId = $cursor->getValue(); - $cursorDocument = $dbForPlatform->getDocument('alerts', $alertId); + $notificationId = $cursor->getValue(); + $cursorDocument = $dbForPlatform->getDocument('alerts', $notificationId); if ($cursorDocument->isEmpty()) { - throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Alert '{$alertId}' for the 'cursor' value not found."); + throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Notification '{$notificationId}' for the 'cursor' value not found."); } $cursor->setValue($cursorDocument); @@ -110,8 +107,8 @@ class XList extends Action } $response->dynamic(new Document([ - 'alerts' => $results, + 'notifications' => $results, 'total' => $total, - ]), Response::MODEL_ALERT_LIST); + ]), Response::MODEL_NOTIFICATION_LIST); } } diff --git a/src/Appwrite/Platform/Modules/Notifications/Services/Http.php b/src/Appwrite/Platform/Modules/Notifications/Services/Http.php index b274c92fa1..91700ca125 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Notifications/Services/Http.php @@ -3,6 +3,8 @@ namespace Appwrite\Platform\Modules\Notifications\Services; use Appwrite\Platform\Modules\Notifications\Http\Notifications\Logos\Appwrite\Get as GetAppwriteLogo; +use Appwrite\Platform\Modules\Notifications\Http\Notifications\Update as UpdateNotification; +use Appwrite\Platform\Modules\Notifications\Http\Notifications\XList as ListNotifications; use Utopia\Platform\Service; class Http extends Service @@ -11,6 +13,9 @@ class Http extends Service { $this->type = Service::TYPE_HTTP; - $this->addAction(GetAppwriteLogo::getName(), new GetAppwriteLogo()); + $this + ->addAction(ListNotifications::getName(), new ListNotifications()) + ->addAction(UpdateNotification::getName(), new UpdateNotification()) + ->addAction(GetAppwriteLogo::getName(), new GetAppwriteLogo()); } } diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 00949aa068..0b65f54f48 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -206,11 +206,8 @@ class Notifications extends Action $smtp = $this->resolveSmtpConfig($project, $payload); if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { - if (empty($payload['recipients']) && !empty($payload['recipient'] ?? '')) { - throw new Exception('Skipped mail processing. No SMTP configuration has been set.'); - } $log->addTag('email_skipped', 'no_smtp'); - return null; + throw new Exception('Skipped mail processing. No SMTP configuration has been set.'); } $type = empty($smtp) ? 'cloud' : 'smtp'; @@ -275,9 +272,9 @@ class Notifications extends Action $subject = \strip_tags($subjectTemplate->render()); $recipientHash = self::buildRecipientHash($recipient); - $opensslKey = System::getEnv('_APP_OPENSSL_KEY_V1'); - if ($messageId !== '' && !empty($opensslKey)) { - $body = $this->injectTrackingLogo($body, $messageId, $recipient['channel'], $recipientHash, $project, $opensslKey); + $trackingSecret = System::getEnv('_APP_NOTIFICATIONS_TRACKING_SECRET'); + if ($messageId !== '' && !empty($trackingSecret)) { + $body = $this->injectTrackingLogo($body, $messageId, $recipient['channel'], $recipientHash, $project, $trackingSecret); } /** @var EmailAdapter $adapter */ @@ -611,7 +608,7 @@ class Notifications extends Action { foreach (['resourceType', 'resourceId', 'resourceInternalId', 'parentResourceType', 'parentResourceId', 'parentResourceInternalId'] as $key) { if ($recipient[$key] === '') { - throw new Exception('Missing notification alert ' . $key); + throw new Exception('Missing notification ' . $key); } } } @@ -632,20 +629,20 @@ class Notifications extends Action /** * Splice a visible tracking logo before the last `` tag (or * append at the end if the body has no closing tag). The logo URL - * carries a signed JWT identifying the alert recipient, which the + * carries a signed JWT identifying the notification recipient, which the * `/v1/notifications/logos/appwrite` endpoint verifies before marking - * the alert as read and recording view timestamps. + * the notification as read and recording view timestamps. */ - private function injectTrackingLogo(string $body, string $messageId, string $channel, string $recipientHash, Document $project, string $opensslKey): string + private function injectTrackingLogo(string $body, string $messageId, string $channel, string $recipientHash, Document $project, string $trackingSecret): string { - $jwt = (new JWT($opensslKey, 'HS256', ALERT_TRACKING_JWT_TTL, 0)) + $jwt = (new JWT($trackingSecret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0)) ->encode([ 'messageId' => $messageId, 'channel' => $channel, 'recipientHash' => $recipientHash, 'projectId' => $project->getId(), 'projectInternalId' => $project->getSequence(), - 'purpose' => 'alert_track', + 'purpose' => 'notification_track', ]); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php b/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php similarity index 85% rename from src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php rename to src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php index 7045364cb7..b1cde8ed82 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Alerts.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php @@ -2,7 +2,7 @@ namespace Appwrite\Utopia\Database\Validator\Queries; -class Alerts extends Base +class Notifications extends Base { public const ALLOWED_ATTRIBUTES = [ 'read', @@ -18,10 +18,6 @@ class Alerts extends Base 'lastSeen', ]; - /** - * Expression constructor - * - */ public function __construct() { parent::__construct('alerts', self::ALLOWED_ATTRIBUTES); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index f8fc9cc148..36f35526b0 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -70,9 +70,9 @@ class Response extends SwooleResponse public const MODEL_ROW = 'row'; public const MODEL_ROW_LIST = 'rowList'; - // Alerts - public const MODEL_ALERT = 'alert'; - public const MODEL_ALERT_LIST = 'alertList'; + // Notifications + public const MODEL_NOTIFICATION = 'notification'; + public const MODEL_NOTIFICATION_LIST = 'notificationList'; // Database Attributes public const MODEL_ATTRIBUTE = 'attribute'; diff --git a/src/Appwrite/Utopia/Response/Model/Alert.php b/src/Appwrite/Utopia/Response/Model/Notification.php similarity index 74% rename from src/Appwrite/Utopia/Response/Model/Alert.php rename to src/Appwrite/Utopia/Response/Model/Notification.php index cd554801d5..601871c620 100644 --- a/src/Appwrite/Utopia/Response/Model/Alert.php +++ b/src/Appwrite/Utopia/Response/Model/Notification.php @@ -5,26 +5,26 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; -class Alert extends Model +class Notification extends Model { public function __construct() { $this ->addRule('$id', [ 'type' => self::TYPE_STRING, - 'description' => 'Alert ID.', + 'description' => 'Notification ID.', 'default' => '', 'example' => '5e5ea5c16897e', ]) ->addRule('$createdAt', [ 'type' => self::TYPE_DATETIME, - 'description' => 'Alert creation date in ISO 8601 format.', + 'description' => 'Notification creation date in ISO 8601 format.', 'default' => '', 'example' => self::TYPE_DATETIME_EXAMPLE, ]) ->addRule('$updatedAt', [ 'type' => self::TYPE_DATETIME, - 'description' => 'Alert update date in ISO 8601 format.', + 'description' => 'Notification update date in ISO 8601 format.', 'default' => '', 'example' => self::TYPE_DATETIME_EXAMPLE, ]) @@ -37,7 +37,7 @@ class Alert extends Model ]) ->addRule('type', [ 'type' => self::TYPE_STRING, - 'description' => 'Alert type: info, warning, error.', + 'description' => 'Notification type: info, warning, error.', 'default' => 'info', 'example' => 'info', ]) @@ -49,64 +49,64 @@ class Alert extends Model ]) ->addRule('resourceType', [ 'type' => self::TYPE_STRING, - 'description' => 'Resource type this alert is addressed to.', + 'description' => 'Resource type this notification is addressed to.', 'default' => '', 'example' => 'users', ]) ->addRule('resourceId', [ 'type' => self::TYPE_STRING, - 'description' => 'Resource ID this alert is addressed to.', + 'description' => 'Resource ID this notification is addressed to.', 'default' => '', 'example' => '5e5bb8c16897e', ]) ->addRule('parentResourceType', [ 'type' => self::TYPE_STRING, - 'description' => 'Parent resource type for the alert.', + 'description' => 'Parent resource type for the notification.', 'default' => '', 'example' => 'projects', ]) ->addRule('parentResourceId', [ 'type' => self::TYPE_STRING, - 'description' => 'Parent resource ID for the alert.', + 'description' => 'Parent resource ID for the notification.', 'default' => '', 'example' => '5e5bb8c16897e', ]) ->addRule('projectId', [ 'type' => self::TYPE_STRING, - 'description' => 'Project the alert pertains to.', + 'description' => 'Project the notification pertains to.', 'default' => '', 'example' => '5e5bb8c16897e', 'required' => false, ]) ->addRule('title', [ 'type' => self::TYPE_STRING, - 'description' => 'Alert title.', + 'description' => 'Notification title.', 'default' => '', 'example' => 'New sign-in detected', ]) ->addRule('body', [ 'type' => self::TYPE_STRING, - 'description' => 'Alert body.', + 'description' => 'Notification body.', 'default' => '', 'example' => 'A new device signed in to your account.', ]) ->addRule('read', [ 'type' => self::TYPE_BOOLEAN, - 'description' => 'Whether the alert has been read.', + 'description' => 'Whether the notification has been read.', 'default' => false, 'example' => false, 'required' => false, ]) ->addRule('firstSeen', [ 'type' => self::TYPE_DATETIME, - 'description' => 'First time the alert was viewed from a notification logo.', + 'description' => 'First time the notification was viewed from a notification logo.', 'default' => null, 'example' => self::TYPE_DATETIME_EXAMPLE, 'required' => false, ]) ->addRule('lastSeen', [ 'type' => self::TYPE_DATETIME, - 'description' => 'Most recent time the alert was viewed from a notification logo.', + 'description' => 'Most recent time the notification was viewed from a notification logo.', 'default' => null, 'example' => self::TYPE_DATETIME_EXAMPLE, 'required' => false, @@ -114,23 +114,13 @@ class Alert extends Model ; } - /** - * Get Name - * - * @return string - */ public function getName(): string { - return 'Alert'; + return 'Notification'; } - /** - * Get Type - * - * @return string - */ public function getType(): string { - return Response::MODEL_ALERT; + return Response::MODEL_NOTIFICATION; } } diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index 77deeb3039..b2adf99d01 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -9,7 +9,7 @@ use Utopia\System\System; /** * End-to-end coverage for the notifications queue health surface and the - * account-alerts user-facing API. + * notifications user-facing API. * * The notification worker itself is exercised in unit tests with a pinned * queue payload; the server side cannot deterministically inject a @@ -20,10 +20,10 @@ use Utopia\System\System; * - the threshold guard returns 503 when the depth exceeds the budget * - the failed-jobs surface accepts the notifications queue name * - * The alerts portion exercises the full webhook-paused fanout end-to-end: + * The notifications portion exercises the full webhook-paused fanout end-to-end: * - * - GET /v1/account/alerts (empty + populated) - * - PATCH /v1/account/alerts/:alertId (happy + unauthorized) + * - GET /v1/notifications (empty + populated) + * - PATCH /v1/notifications/:notificationId (happy + unauthorized) * - GET /v1/notifications/logos/appwrite (valid JWT + invalid JWT) * * Dedup, per-channel dispatch, and webhook signing are covered by: @@ -66,23 +66,23 @@ trait NotificationsBase $this->assertIsInt($response['body']['size']); } - public function testListAccountAlertsEmpty(): void + public function testListNotificationsEmpty(): void { - // Always read alerts as the console-authenticated owner of the team. - // The /v1/account/alerts endpoint is platform-scoped (dbForPlatform) and + // Always read notifications as the console-authenticated owner of the team. + // The /v1/notifications endpoint is platform-scoped (dbForPlatform) and // requires a session; server-mode API keys do not satisfy it. - $response = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $response = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $response['headers']['status-code']); - $this->assertArrayHasKey('alerts', $response['body']); + $this->assertArrayHasKey('notifications', $response['body']); $this->assertArrayHasKey('total', $response['body']); - $this->assertIsArray($response['body']['alerts']); + $this->assertIsArray($response['body']['notifications']); $this->assertIsInt($response['body']['total']); - // The shared root console user may carry alerts from prior tests in the + // The shared root console user may carry notifications from prior tests in the // same suite — assert only that the response shape is correct and that // counts agree. - $this->assertSame(\count($response['body']['alerts']), \min(\count($response['body']['alerts']), $response['body']['total'])); + $this->assertSame(\count($response['body']['notifications']), \min(\count($response['body']['notifications']), $response['body']['total'])); } public function testWebhookFailureCreatesConsoleAlert(): void @@ -91,18 +91,18 @@ trait NotificationsBase $this->assertNotEmpty($alertId); - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); $found = null; - foreach ($list['body']['alerts'] as $alert) { + foreach ($list['body']['notifications'] as $alert) { if ($alert['$id'] === $alertId) { $found = $alert; break; } } - $this->assertNotNull($found, 'Seeded alert not present in /account/alerts response.'); + $this->assertNotNull($found, 'Seeded alert not present in /notifications response.'); $this->assertSame('console', $found['channel']); $this->assertStringContainsStringIgnoringCase('webhook', $found['title']); @@ -110,14 +110,14 @@ trait NotificationsBase self::$seededAlertId = $alertId; } - public function testMarkAlertReadTogglesFlag(): void + public function testMarkNotificationReadTogglesFlag(): void { $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); $patch = $this->client->call( Client::METHOD_PATCH, - '/account/alerts/' . $alertId, + '/notifications/' . $alertId, $this->getConsoleAlertHeaders(), ['read' => true] ); @@ -126,11 +126,11 @@ trait NotificationsBase $this->assertSame($alertId, $patch['body']['$id']); $this->assertTrue($patch['body']['read']); - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); $found = null; - foreach ($list['body']['alerts'] as $alert) { + foreach ($list['body']['notifications'] as $alert) { if ($alert['$id'] === $alertId) { $found = $alert; break; @@ -140,10 +140,10 @@ trait NotificationsBase $this->assertNotNull($found); $this->assertTrue($found['read']); - self::$seededAlertId = null; // alert is read — downstream tests will seed fresh + self::$seededAlertId = null; // notification is read, downstream tests will seed fresh } - public function testMarkAlertReadUnauthorized(): void + public function testMarkNotificationReadUnauthorized(): void { $alertId = $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); @@ -153,7 +153,7 @@ trait NotificationsBase $unauthorized = $this->client->call( Client::METHOD_PATCH, - '/account/alerts/' . $alertId, + '/notifications/' . $alertId, [ 'origin' => 'http://localhost', 'content-type' => 'application/json', @@ -167,11 +167,11 @@ trait NotificationsBase $this->assertSame('user_unauthorized', $unauthorized['body']['type'] ?? ''); // Owner re-fetches — alert must still be unread. - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); $found = null; - foreach ($list['body']['alerts'] as $alert) { + foreach ($list['body']['notifications'] as $alert) { if ($alert['$id'] === $alertId) { $found = $alert; break; @@ -184,7 +184,7 @@ trait NotificationsBase self::$seededAlertId = $alertId; } - public function testProjectAccountCannotListPlatformAlertsWithCollidingUserId(): void + public function testProjectAccountCannotListPlatformNotificationsWithCollidingUserId(): void { $alertId = $this->seedWebhookFailureAlert(); $this->assertNotEmpty($alertId); @@ -219,7 +219,7 @@ trait NotificationsBase $this->assertSame(201, $session['headers']['status-code']); $this->assertNotEmpty($session['cookies']['a_session_' . $projectId] ?? ''); - $response = $this->client->call(Client::METHOD_GET, '/account/alerts', [ + $response = $this->client->call(Client::METHOD_GET, '/notifications', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', 'cookie' => 'a_session_' . $projectId . '=' . $session['cookies']['a_session_' . $projectId], @@ -241,18 +241,18 @@ trait NotificationsBase $this->assertEmpty($alert['firstSeen'] ?? null); $this->assertEmpty($alert['lastSeen'] ?? null); - $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); - $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for notification logo test'); + $secret = System::getEnv('_APP_NOTIFICATIONS_TRACKING_SECRET'); + $this->assertNotEmpty($secret, '_APP_NOTIFICATIONS_TRACKING_SECRET must be set for notification logo test'); $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); - // Logo endpoint requires `purpose: 'alert_track'`. Other claim + // Logo endpoint requires `purpose: 'notification_track'`. Other claim // purposes are silently ignored, which the purpose-claim test covers. - $jwt = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ + $jwt = (new JWT($secret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ 'messageId' => $alert['messageId'], 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, 'projectId' => $alert['projectId'], - 'purpose' => 'alert_track', + 'purpose' => 'notification_track', ]); $response = $this->client->call( @@ -295,10 +295,10 @@ trait NotificationsBase } /** - * Reviewer M7: a tracking JWT without a `purpose: 'alert_track'` claim + * Reviewer M7: a tracking JWT without a `purpose: 'notification_track'` claim * must be silently rejected. Without the purpose check, any JWT minted * with the same secret (sessions, password reset, etc.) could be - * replayed against this endpoint to mark arbitrary alerts as read. + * replayed against this endpoint to mark arbitrary notifications as read. * * The endpoint always returns the SVG logo (200 image/svg+xml) — the only * observable difference is whether the alert flips to `read: true`. @@ -309,12 +309,12 @@ trait NotificationsBase $this->assertNotEmpty($alertId); $alert = $this->findConsoleAlert($alertId); - $secret = System::getEnv('_APP_OPENSSL_KEY_V1'); - $this->assertNotEmpty($secret, '_APP_OPENSSL_KEY_V1 must be set for the JWT purpose-claim test'); + $secret = System::getEnv('_APP_NOTIFICATIONS_TRACKING_SECRET'); + $this->assertNotEmpty($secret, '_APP_NOTIFICATIONS_TRACKING_SECRET must be set for the JWT purpose-claim test'); $recipientHash = \substr($alertId, \strrpos($alertId, '_') + 1); // Mint a JWT with valid message/recipient claims but NO purpose claim. - $jwtNoPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ + $jwtNoPurpose = (new JWT($secret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ 'messageId' => $alert['messageId'], 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, @@ -337,7 +337,7 @@ trait NotificationsBase $this->assertEmpty($found['lastSeen'] ?? null); // Mint a JWT with a wrong purpose value: same expectation, silently rejected. - $jwtWrongPurpose = (new JWT($secret, 'HS256', ALERT_TRACKING_JWT_TTL, 0))->encode([ + $jwtWrongPurpose = (new JWT($secret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ 'messageId' => $alert['messageId'], 'channel' => $alert['channel'], 'recipientHash' => $recipientHash, @@ -380,7 +380,7 @@ trait NotificationsBase $this->assertNotEmpty($response['body']); $this->assertStringStartsWith('findConsoleAlert($alertId); $this->assertFalse($found['read']); $this->assertEmpty($found['firstSeen'] ?? null); @@ -397,7 +397,7 @@ trait NotificationsBase /** * Build the auth header set used to talk to the platform-scoped - * /v1/account/alerts endpoints. Always console session, regardless of + * /v1/notifications endpoints. Always console session, regardless of * the trait's host (server vs console) — the endpoint requires a session * and the root user owns the project team. * @@ -418,16 +418,16 @@ trait NotificationsBase */ protected function findConsoleAlert(string $alertId): array { - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); - foreach ($list['body']['alerts'] as $alert) { + foreach ($list['body']['notifications'] as $alert) { if ($alert['$id'] === $alertId) { return $alert; } } - $this->fail('Alert not present in /account/alerts response: ' . $alertId); + $this->fail('Alert not present in /notifications response: ' . $alertId); } /** @@ -494,10 +494,10 @@ trait NotificationsBase // and webhook deliveries also queue up — give them generous time. $alertId = null; $this->assertEventually(function () use (&$alertId, $projectId, $webhookId, $webhookName) { - $list = $this->client->call(Client::METHOD_GET, '/account/alerts', $this->getConsoleAlertHeaders()); + $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); $this->assertSame(200, $list['headers']['status-code']); - foreach ($list['body']['alerts'] as $alert) { + foreach ($list['body']['notifications'] as $alert) { if ( ($alert['channel'] ?? '') === 'console' && ($alert['projectId'] ?? '') === $projectId diff --git a/tests/resources/docker/docker-compose.yml b/tests/resources/docker/docker-compose.yml index 5d59d3ae77..1ae466c451 100644 --- a/tests/resources/docker/docker-compose.yml +++ b/tests/resources/docker/docker-compose.yml @@ -66,6 +66,7 @@ services: - _APP_OPTIONS_FORCE_HTTPS - _APP_OPTIONS_ROUTER_FORCE_HTTPS - _APP_OPENSSL_KEY_V1 + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_DOMAIN - _APP_DOMAIN_FUNCTIONS - _APP_DOMAIN_TARGET @@ -281,6 +282,7 @@ services: # - smtp environment: - _APP_ENV + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_REDIS_HOST - _APP_REDIS_PORT - _APP_DB_HOST @@ -302,6 +304,7 @@ services: - maildev environment: - _APP_ENV + - _APP_NOTIFICATIONS_TRACKING_SECRET - _APP_REDIS_HOST - _APP_REDIS_PORT - _APP_DB_HOST diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 26e34bd660..737e1594cc 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -691,13 +691,13 @@ class NotificationsTest extends TestCase $this->registry->set('smtp', static fn () => $spy); // Force the cloud SMTP branch (project has no smtp config) and - // provide an OpenSSL key so injectTrackingLogo actually runs. + // provide the tracking secret so injectTrackingLogo actually runs. $previousSmtpHost = \getenv('_APP_SMTP_HOST'); - $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); + $previousTrackingSecret = \getenv('_APP_NOTIFICATIONS_TRACKING_SECRET'); $previousDomain = \getenv('_APP_DOMAIN'); $previousConsoleDomain = \getenv('_APP_CONSOLE_DOMAIN'); \putenv('_APP_SMTP_HOST=spy.smtp.test'); - \putenv('_APP_OPENSSL_KEY_V1=test-key-32bytes-min-aaaaaaaaaaaaaa'); + \putenv('_APP_NOTIFICATIONS_TRACKING_SECRET=test-key-32bytes-min-aaaaaaaaaaaaaa'); \putenv('_APP_DOMAIN=api.example.test'); \putenv('_APP_CONSOLE_DOMAIN=console.example.test'); @@ -717,7 +717,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); - \putenv($previousOpensslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpensslKey); + \putenv($previousTrackingSecret === false ? '_APP_NOTIFICATIONS_TRACKING_SECRET' : '_APP_NOTIFICATIONS_TRACKING_SECRET=' . $previousTrackingSecret); \putenv($previousDomain === false ? '_APP_DOMAIN' : '_APP_DOMAIN=' . $previousDomain); \putenv($previousConsoleDomain === false ? '_APP_CONSOLE_DOMAIN' : '_APP_CONSOLE_DOMAIN=' . $previousConsoleDomain); } @@ -737,7 +737,7 @@ class NotificationsTest extends TestCase \preg_match('/logos\/appwrite\?jwt=([^"&]+)/', \html_entity_decode($body), $matches); $this->assertNotEmpty($matches[1] ?? ''); - $claims = (new JWT('test-key-32bytes-min-aaaaaaaaaaaaaa', 'HS256', ALERT_TRACKING_JWT_TTL, 0)) + $claims = (new JWT('test-key-32bytes-min-aaaaaaaaaaaaaa', 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0)) ->decode(\urldecode($matches[1])); $this->assertSame(\md5('logo-key'), $claims['messageId'] ?? null); $this->assertSame(NOTIFICATION_TYPE_EMAIL, $claims['channel'] ?? null); @@ -750,7 +750,7 @@ class NotificationsTest extends TestCase ), $claims['recipientHash'] ?? null); $this->assertSame('project-x', $claims['projectId'] ?? null); $this->assertSame('project-internal-x', $claims['projectInternalId'] ?? null); - $this->assertSame('alert_track', $claims['purpose'] ?? null); + $this->assertSame('notification_track', $claims['purpose'] ?? null); $this->assertArrayNotHasKey('alertId', $claims); // The logo must sit BEFORE the last . @@ -761,6 +761,42 @@ class NotificationsTest extends TestCase $this->assertLessThan($lastBodyClose, $logoPosition, 'logo must be spliced before the final '); } + public function testTrackingLogoDoesNotUseOpenSslKeyFallback(): void + { + $spy = new SpyEmailAdapter(); + $this->registry->set('smtp', static fn () => $spy); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + $previousTrackingSecret = \getenv('_APP_NOTIFICATIONS_TRACKING_SECRET'); + $previousOpenSslKey = \getenv('_APP_OPENSSL_KEY_V1'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + \putenv('_APP_NOTIFICATIONS_TRACKING_SECRET='); + \putenv('_APP_OPENSSL_KEY_V1=openssl-key-must-not-sign-tracking'); + + try { + $worker = new Notifications(); + + $payload = [ + 'project' => ['$id' => 'project-x'], + 'recipients' => [ + $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-8'), + ], + 'subject' => 'Heads up', + 'body' => 'plain body', + 'deduplicationKey' => 'logo-key-no-tracking-secret', + ]; + + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + \putenv($previousTrackingSecret === false ? '_APP_NOTIFICATIONS_TRACKING_SECRET' : '_APP_NOTIFICATIONS_TRACKING_SECRET=' . $previousTrackingSecret); + \putenv($previousOpenSslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpenSslKey); + } + + $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); + $this->assertStringNotContainsString('/v1/notifications/logos/appwrite?jwt=', $spy->captured->getContent()); + } + public function testPersistAlertReturnsExistingAlertIdOnDuplicate(): void { $spy = new SpyEmailAdapter(); @@ -1041,9 +1077,9 @@ class NotificationsTest extends TestCase $this->registry->set('smtp', static fn () => $spy); $previousSmtpHost = \getenv('_APP_SMTP_HOST'); - $previousOpensslKey = \getenv('_APP_OPENSSL_KEY_V1'); + $previousTrackingSecret = \getenv('_APP_NOTIFICATIONS_TRACKING_SECRET'); \putenv('_APP_SMTP_HOST=spy.smtp.test'); - \putenv('_APP_OPENSSL_KEY_V1=test-key-32bytes-min-aaaaaaaaaaaaaa'); + \putenv('_APP_NOTIFICATIONS_TRACKING_SECRET=test-key-32bytes-min-aaaaaaaaaaaaaa'); try { $worker = new CountingPersistAlertNotifications(); @@ -1061,7 +1097,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); - \putenv($previousOpensslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpensslKey); + \putenv($previousTrackingSecret === false ? '_APP_NOTIFICATIONS_TRACKING_SECRET' : '_APP_NOTIFICATIONS_TRACKING_SECRET=' . $previousTrackingSecret); } $this->assertSame(1, $spy->sendCount, 'SMTP send must be invoked exactly once'); @@ -1179,10 +1215,11 @@ class NotificationsTest extends TestCase $this->assertStringNotContainsString('', $rows[0]->getAttribute('body')); } - public function testEmailChannelSkipsWhenSmtpIsNotConfigured(): void + public function testEmailChannelThrowsWhenSmtpIsNotConfigured(): void { $previousSmtpHost = \getenv('_APP_SMTP_HOST'); \putenv('_APP_SMTP_HOST='); + $threw = false; try { $worker = new CountingPersistAlertNotifications(); @@ -1196,11 +1233,17 @@ class NotificationsTest extends TestCase 'deduplicationKey' => 'missing-smtp', ]; - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + try { + $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); + } catch (\Exception $error) { + $this->assertStringContainsString('No SMTP configuration has been set', $error->getMessage()); + $threw = true; + } } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); } + $this->assertTrue($threw, 'missing SMTP must fail email delivery so the queue can retry'); $this->assertSame(0, $worker->persistAlertCalls); $this->assertCount(0, $this->database->find('alerts')); $this->assertSame('no_smtp', $this->log->getTags()['email_skipped'] ?? null); From afb956a18c10a2fa2c4937f2e4508a436ca3fa6c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 19:46:13 +1200 Subject: [PATCH 59/80] fix: purge project cache after updates --- .../Modules/Project/Http/Project/AuthMethods/Update.php | 1 + .../Platform/Modules/Project/Http/Project/Labels/Update.php | 1 + .../Modules/Project/Http/Project/MockPhone/Create.php | 1 + .../Modules/Project/Http/Project/MockPhone/Delete.php | 1 + .../Modules/Project/Http/Project/MockPhone/Update.php | 1 + .../Platform/Modules/Project/Http/Project/OAuth2/Base.php | 5 ++++- .../Http/Project/Policies/MembershipPrivacy/Update.php | 1 + .../Http/Project/Policies/PasswordDictionary/Update.php | 1 + .../Project/Http/Project/Policies/PasswordHistory/Update.php | 1 + .../Http/Project/Policies/PasswordPersonalData/Update.php | 1 + .../Project/Http/Project/Policies/SessionAlert/Update.php | 1 + .../Project/Http/Project/Policies/SessionDuration/Update.php | 1 + .../Http/Project/Policies/SessionInvalidation/Update.php | 1 + .../Project/Http/Project/Policies/SessionLimit/Update.php | 1 + .../Project/Http/Project/Policies/UserLimit/Update.php | 1 + .../Modules/Project/Http/Project/Protocols/Update.php | 1 + .../Platform/Modules/Project/Http/Project/SMTP/Update.php | 1 + .../Modules/Project/Http/Project/Services/Update.php | 1 + .../Modules/Project/Http/Project/Templates/Email/Update.php | 1 + .../Platform/Modules/Projects/Http/Projects/Team/Update.php | 1 + .../Platform/Modules/Projects/Http/Projects/Update.php | 1 + 21 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index e98d3141f6..f5f0ae0a32 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -82,6 +82,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php index 8a3506eb13..76c47a32f8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php @@ -71,6 +71,7 @@ class Update extends Action $labels = (array) \array_values(\array_unique($labels)); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document(['labels' => $labels]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index f4002c60ef..759dd931c0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -101,6 +101,7 @@ class Create extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index 0fb23e1764..f65a955b3d 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -95,6 +95,7 @@ class Delete extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index 48b90a1b97..f2b618c39e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -97,6 +97,7 @@ class Update extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php index b5b8cacb73..b439e7fdf7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -425,7 +425,10 @@ abstract class Base extends Action 'oAuthProviders' => $oAuthProviders ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); + + return $project; } public function action( diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index 41a6168b07..92e1eb1dd1 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -98,6 +98,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index d7ee99fbfe..d0f8882e99 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index 84861a19e1..be611e299f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -83,6 +83,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index 435f00fc39..ff7819f192 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -76,6 +76,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index 79653d46ad..d930fd89e7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index 0a7f33218a..10eabb57cd 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index a1feb67346..d4d87edbba 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 936a541249..93ae339b79 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -81,6 +81,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index 2b7e704853..b3f430b779 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -81,6 +81,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index bfc73802c8..e0e4477a3c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -79,6 +79,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'apis' => $protocols, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index c3fe222298..3f5eefed02 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -169,6 +169,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 518eeb4c36..86ef67bb01 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -79,6 +79,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'services' => $services, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('serviceId', $serviceId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index c8ec04bc76..8186931c5f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -129,6 +129,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('templateId', $templateId); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php index ab92d1a15f..d2f7d5a29d 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php @@ -78,6 +78,7 @@ class Update extends Action 'teamInternalId' => $team->getSequence(), '$permissions' => $permissions, ])); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $installations = $dbForPlatform->find('installations', [ Query::equal('projectInternalId', [$project->getSequence()]), diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php index f6df843d07..c633a7adbf 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -72,6 +72,7 @@ class Update extends Action ->setAttribute('legalAddress', $legalAddress) ->setAttribute('legalTaxId', $legalTaxId) ->setAttribute('search', implode(' ', [$projectId, $name]))); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->dynamic($project, Response::MODEL_PROJECT); } From 16bfbce2e946b0bef6983592be108b99e02424f3 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 20:17:59 +1200 Subject: [PATCH 60/80] fix: scope notification listing to user --- .../Http/Notifications/XList.php | 3 + .../Notifications/NotificationsTest.php | 127 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 tests/unit/Platform/Modules/Notifications/NotificationsTest.php diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php index 461a210016..7916ce29ff 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php @@ -97,6 +97,9 @@ class XList extends Action $cursor->setValue($cursorDocument); } + $queries[] = Query::equal('resourceType', [RESOURCE_TYPE_USERS]); + $queries[] = Query::equal('resourceId', [$user->getId()]); + $filterQueries = Query::groupByType($queries)['filters']; try { diff --git a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php new file mode 100644 index 0000000000..d5126a68a5 --- /dev/null +++ b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php @@ -0,0 +1,127 @@ +document = $document; + $this->model = $model; + } +} + +class NotificationsTest extends TestCase +{ + private Authorization $authorization; + private Database $database; + + protected function setUp(): void + { + $this->authorization = new Authorization(); + $this->authorization->addRole(Role::any()->toString()); + + $this->database = new Database(new Memory(), new Cache(new NoCache())); + $this->database + ->setAuthorization($this->authorization) + ->setDatabase('notificationEndpointTests') + ->setNamespace('notifications_' . \uniqid()); + + $permissions = [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ]; + + $this->database->create(); + $this->database->createCollection('alerts', [], [], $permissions, false); + $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); + } + + protected function tearDown(): void + { + $this->authorization->cleanRoles(); + $this->authorization->addRole(Role::any()->toString()); + } + + public function testListReturnsOnlyCurrentUserNotifications(): void + { + $this->createNotification('user-alert', RESOURCE_TYPE_USERS, 'user-a'); + $this->createNotification('team-alert', RESOURCE_TYPE_TEAMS, 'team-a'); + + $response = new CapturingNotificationsResponse(); + + (new XList())->action( + [], + $response, + $this->database, + new Document(['$id' => 'console']), + new Document(['$id' => 'user-a']), + ); + + $notifications = $response->document->getAttribute('notifications'); + + $this->assertCount(1, $notifications); + $this->assertSame('user-alert', $notifications[0]->getId()); + $this->assertSame(1, $response->document->getAttribute('total')); + $this->assertSame(Response::MODEL_NOTIFICATION_LIST, $response->model); + } + + private function createNotification(string $id, string $resourceType, string $resourceId): Document + { + return $this->database->createDocument('alerts', new Document([ + '$id' => $id, + 'messageId' => $id, + 'recipientHash' => \substr(\md5($id), 0, 16), + 'type' => 'info', + 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'projectId' => 'project', + 'projectInternalId' => '10', + 'resourceType' => $resourceType, + 'resourceId' => $resourceId, + 'resourceInternalId' => '20', + 'parentResourceType' => RESOURCE_TYPE_PROJECTS, + 'parentResourceId' => 'project', + 'parentResourceInternalId' => '10', + 'title' => 'Title', + 'body' => 'Body', + 'read' => false, + ])); + } +} From 0880fa23a517007e0a4b3ac93ef16aef4c39190e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 21:15:41 +1200 Subject: [PATCH 61/80] fix: retain mails worker action --- app/worker.php | 3 +- src/Appwrite/Platform/Services/Workers.php | 2 + src/Appwrite/Platform/Workers/Mails.php | 212 ++++++++++++++++++ tests/unit/Platform/Workers/MailsTest.php | 94 ++++++++ .../Platform/Workers/RegistrationTest.php | 28 +++ 5 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Platform/Workers/Mails.php create mode 100644 tests/unit/Platform/Workers/MailsTest.php create mode 100644 tests/unit/Platform/Workers/RegistrationTest.php diff --git a/app/worker.php b/app/worker.php index b9e5cbd2a7..169b8e9770 100644 --- a/app/worker.php +++ b/app/worker.php @@ -73,7 +73,6 @@ if (! isset($args[1])) { \array_shift($args); $workerName = $args[0]; -$actionName = $workerName === 'mails' ? 'notifications' : $workerName; if (\str_starts_with($workerName, 'databases')) { $queueName = System::getEnv('_APP_QUEUE_NAME', 'database_db_main'); @@ -110,7 +109,7 @@ try { $platform->setWorker($worker); $platform->init(Service::TYPE_WORKER, [ - 'workerName' => strtolower($actionName), + 'workerName' => strtolower($workerName), ]); } catch (\Throwable $e) { Console::error($e->getMessage() . ', File: ' . $e->getFile() . ', Line: ' . $e->getLine()); diff --git a/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index 9ab26a41b7..5af3d078de 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -7,6 +7,7 @@ use Appwrite\Platform\Workers\Certificates; use Appwrite\Platform\Workers\Deletes; use Appwrite\Platform\Workers\Executions; use Appwrite\Platform\Workers\Functions; +use Appwrite\Platform\Workers\Mails; use Appwrite\Platform\Workers\Messaging; use Appwrite\Platform\Workers\Migrations; use Appwrite\Platform\Workers\Notifications; @@ -26,6 +27,7 @@ class Workers extends Service ->addAction(Deletes::getName(), new Deletes()) ->addAction(Executions::getName(), new Executions()) ->addAction(Functions::getName(), new Functions()) + ->addAction(Mails::getName(), new Mails()) ->addAction(Messaging::getName(), new Messaging()) ->addAction(Notifications::getName(), new Notifications()) ->addAction(Webhooks::getName(), new Webhooks()) diff --git a/src/Appwrite/Platform/Workers/Mails.php b/src/Appwrite/Platform/Workers/Mails.php new file mode 100644 index 0000000000..74a007720b --- /dev/null +++ b/src/Appwrite/Platform/Workers/Mails.php @@ -0,0 +1,212 @@ + + */ + protected array $richTextParams = [ + 'b' => '', + '/b' => '', + ]; + + public static function getName(): string + { + return 'mails'; + } + + public function __construct() + { + $this + ->desc('Mails worker') + ->inject('message') + ->inject('project') + ->inject('register') + ->inject('log') + ->callback($this->action(...)); + } + + public function action(Message $message, Document $project, Registry $register, Log $log): void + { + if (\class_exists(Runtime::class)) { + Runtime::setHookFlags(SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP); + } + + $payload = $message->getPayload(); + if (empty($payload)) { + throw new Exception('Missing payload'); + } + + $smtp = $payload['smtp'] ?? []; + if (!\is_array($smtp)) { + $smtp = []; + } + + if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { + throw new Exception('Skipped mail processing. No SMTP configuration has been set.'); + } + + $type = empty($smtp) ? 'cloud' : 'smtp'; + $log->addTag('type', $type); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; + $hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', 'localhost')); + + $recipient = (string) ($payload['recipient'] ?? ''); + $subject = (string) ($payload['subject'] ?? ''); + $variables = $payload['variables'] ?? []; + if (!\is_array($variables)) { + $variables = []; + } + $variables['host'] = $protocol . '://' . $hostname; + $name = (string) ($payload['name'] ?? ''); + $body = (string) ($payload['body'] ?? ''); + $preview = (string) ($payload['preview'] ?? ''); + + $variables['subject'] = $subject; + $variables['heading'] = $variables['heading'] ?? $subject; + $variables['year'] = \date('Y'); + + $attachment = $payload['attachment'] ?? []; + if (!\is_array($attachment)) { + $attachment = []; + } + + $bodyTemplate = (string) ($payload['bodyTemplate'] ?? ''); + if (empty($bodyTemplate)) { + $bodyTemplate = __DIR__ . '/../../../../app/config/locale/templates/email-base.tpl'; + } + + $bodyTemplate = Template::fromFile($bodyTemplate); + $bodyTemplate->setParam('{{body}}', $body, escapeHtml: false); + foreach ($variables as $key => $value) { + $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: $key !== 'redirect'); + } + foreach ($this->richTextParams as $key => $value) { + $bodyTemplate->setParam('{{' . $key . '}}', $value, escapeHtml: false); + } + + $previewWhitespace = ''; + if (!empty($preview)) { + $previewTemplate = Template::fromString($preview); + foreach ($variables as $key => $value) { + $previewTemplate->setParam('{{' . $key . '}}', $value); + } + $preview = \strip_tags($previewTemplate->render()); + + $previewLen = \strlen($preview); + if ($previewLen < $this->previewMaxLen) { + $previewWhitespace = \str_repeat($this->whitespaceCodes, $this->previewMaxLen - $previewLen); + } + } + + $bodyTemplate->setParam('{{preview}}', $preview); + $bodyTemplate->setParam('{{previewWhitespace}}', $previewWhitespace, false); + + $body = $bodyTemplate->render(); + + $subjectTemplate = Template::fromString($subject); + foreach ($variables as $key => $value) { + $subjectTemplate->setParam('{{' . $key . '}}', $value); + } + $subject = \strip_tags($subjectTemplate->render()); + + /** @var EmailAdapter $adapter */ + $adapter = empty($smtp) + ? $register->get('smtp') + : new SMTP( + host: $smtp['host'], + port: (int) $smtp['port'], + username: $smtp['username'] ?? '', + password: $smtp['password'] ?? '', + smtpSecure: $smtp['secure'] ?? '', + smtpAutoTLS: false, + xMailer: 'Appwrite Mailer', + timeout: 10, + keepAlive: true, + timelimit: 30, + ); + + $defaultFromEmail = System::getEnv('_APP_SYSTEM_EMAIL_ADDRESS', APP_EMAIL_TEAM); + $defaultFromName = \urldecode(System::getEnv('_APP_SYSTEM_EMAIL_NAME', APP_NAME . ' Server')); + + $fromEmail = !empty($smtp) ? ($smtp['senderEmail'] ?? $defaultFromEmail) : $defaultFromEmail; + $fromName = !empty($smtp) ? ($smtp['senderName'] ?? $defaultFromName) : $defaultFromName; + $replyTo = $defaultFromEmail; + $replyToName = $defaultFromName; + + $customMailOptions = $payload['customMailOptions'] ?? []; + if (!\is_array($customMailOptions)) { + $customMailOptions = []; + } + + if (!empty($customMailOptions['senderEmail'])) { + $fromEmail = (string) $customMailOptions['senderEmail']; + } + if (!empty($customMailOptions['senderName'])) { + $fromName = (string) $customMailOptions['senderName']; + } + + if (!empty($customMailOptions['replyToEmail']) || !empty($customMailOptions['replyToName'])) { + $replyTo = (string) ($customMailOptions['replyToEmail'] ?? $replyTo); + $replyToName = (string) ($customMailOptions['replyToName'] ?? $replyToName); + } elseif (!empty($smtp)) { + $smtpReplyToEmail = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; + $replyTo = !empty($smtpReplyToEmail) ? $smtpReplyToEmail : ($smtp['senderEmail'] ?? $replyTo); + $replyToName = !empty($smtp['replyToName']) ? $smtp['replyToName'] : ($smtp['senderName'] ?? $replyToName); + } + + $attachments = null; + if (!empty($attachment['content'] ?? '')) { + $attachments = [ + new Attachment( + name: $attachment['filename'] ?? 'unknown.file', + path: '', + type: $attachment['type'] ?? 'plain/text', + content: \base64_decode($attachment['content']), + ), + ]; + } + + $emailMessage = new EmailMessage( + to: [['email' => $recipient, 'name' => $name]], + subject: $subject, + content: $body, + fromName: $fromName, + fromEmail: $fromEmail, + replyToName: $replyToName, + replyToEmail: $replyTo, + attachments: $attachments, + html: true, + ); + + try { + $adapter->send($emailMessage); + } catch (Throwable $error) { + if ($type === 'smtp') { + throw new Exception('Error sending mail: ' . $error->getMessage(), 401); + } + throw new Exception('Error sending mail: ' . $error->getMessage(), 500); + } + } +} diff --git a/tests/unit/Platform/Workers/MailsTest.php b/tests/unit/Platform/Workers/MailsTest.php new file mode 100644 index 0000000000..08a695429b --- /dev/null +++ b/tests/unit/Platform/Workers/MailsTest.php @@ -0,0 +1,94 @@ +sendCount++; + $this->captured = $message; + + return [ + 'deliveredTo' => 1, + 'type' => $this->getType(), + 'results' => [['recipient' => $message->getTo()[0]['email'] ?? '', 'status' => 'sent']], + ]; + } +} + +class MailsTest extends TestCase +{ + public function testLegacyMailPayloadIsSentByMailsWorker(): void + { + $adapter = new SpyMailAdapter(); + $registry = new Registry(); + $registry->set('smtp', static fn () => $adapter); + + $previousSmtpHost = \getenv('_APP_SMTP_HOST'); + \putenv('_APP_SMTP_HOST=spy.smtp.test'); + + try { + $worker = new Mails(); + $worker->action( + new Message([ + 'pid' => 'pid', + 'queue' => 'v1-mails', + 'timestamp' => \time(), + 'payload' => [ + 'recipient' => 'legacy@example.test', + 'name' => 'Legacy User', + 'subject' => 'Hello {{name}}', + 'body' => 'Body {{name}}', + 'variables' => ['name' => 'Legacy'], + 'customMailOptions' => [ + 'senderEmail' => 'sender@example.test', + 'senderName' => 'Custom Sender', + 'replyToEmail' => 'reply@example.test', + 'replyToName' => 'Custom Reply', + ], + ], + ]), + new Document(['$id' => 'project-x']), + $registry, + new Log(), + ); + } finally { + \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); + } + + $this->assertSame(1, $adapter->sendCount); + $this->assertNotNull($adapter->captured); + + $message = $adapter->captured; + $this->assertSame('legacy@example.test', $message->getTo()[0]['email'] ?? ''); + $this->assertSame('Legacy User', $message->getTo()[0]['name'] ?? ''); + $this->assertSame('Hello Legacy', $message->getSubject()); + $this->assertSame('sender@example.test', $message->getFromEmail()); + $this->assertSame('Custom Sender', $message->getFromName()); + $this->assertSame('reply@example.test', $message->getReplyToEmail()); + $this->assertSame('Custom Reply', $message->getReplyToName()); + } +} diff --git a/tests/unit/Platform/Workers/RegistrationTest.php b/tests/unit/Platform/Workers/RegistrationTest.php new file mode 100644 index 0000000000..e1d92a3028 --- /dev/null +++ b/tests/unit/Platform/Workers/RegistrationTest.php @@ -0,0 +1,28 @@ +assertInstanceOf(Mails::class, $service->getAction('mails')); + $this->assertInstanceOf(Notifications::class, $service->getAction('notifications')); + } + + public function testEntrypointDoesNotAliasMailsToNotifications(): void + { + $contents = \file_get_contents(__DIR__ . '/../../../../app/worker.php'); + + $this->assertIsString($contents); + $this->assertStringNotContainsString("mails' ? 'notifications'", $contents); + $this->assertStringContainsString('\'workerName\' => strtolower($workerName)', $contents); + } +} From 7736b850156f27596c630c56b99917e9f2014040 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 3 Jun 2026 03:23:38 +0000 Subject: [PATCH 62/80] test(notifications): assert email channel via maildev in webhook-paused e2e The webhook-paused fanout enqueues one Notification with both a console and an email recipient. The console alert was already asserted end-to-end; this adds a maildev poll for the rendered email to the project owner so the email channel is verified through the full Notifications worker -> SMTP -> maildev path, matching the session-alert e2e pattern. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Notifications/NotificationsBase.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php index b2adf99d01..e6c8babe9b 100644 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ b/tests/e2e/Services/Notifications/NotificationsBase.php @@ -26,6 +26,10 @@ use Utopia\System\System; * - PATCH /v1/notifications/:notificationId (happy + unauthorized) * - GET /v1/notifications/logos/appwrite (valid JWT + invalid JWT) * + * The same webhook-paused fanout drives both channels of a single + * Notification, so the email channel is asserted end-to-end through maildev + * (Notifications worker → SMTP → maildev inbox) alongside the console alert. + * * Dedup, per-channel dispatch, and webhook signing are covered by: * - tests/unit/Platform/Workers/NotificationsTest.php * - tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -110,6 +114,33 @@ trait NotificationsBase self::$seededAlertId = $alertId; } + /** + * The webhook-paused fanout enqueues a single Notification with both a + * console and an email recipient. The console side is asserted above; this + * verifies the email channel travels the full path — Notifications worker + * → SMTP → maildev — by polling the maildev inbox for the rendered email + * addressed to the project owner. + */ + public function testWebhookFailureSendsEmailNotification(): void + { + // Reuse the fanout from the console-alert test when it already ran in + // this process; otherwise drive a fresh one. Either way the owner email + // lands in maildev for the root user this suite created. + if (self::$seededAlertId === null) { + self::$seededAlertId = $this->seedWebhookFailureAlert(); + } + + $ownerEmail = $this->getRoot()['email']; + + $this->assertEventually(function () use ($ownerEmail) { + $this->assertGreaterThanOrEqual( + 1, + $this->countNotificationEmails($ownerEmail, 'Webhook deliveries have been paused'), + 'No webhook-paused email observed in maildev for ' . $ownerEmail + ); + }, 30000, 1000); + } + public function testMarkNotificationReadTogglesFlag(): void { $alertId = self::$seededAlertId ?? $this->seedWebhookFailureAlert(); @@ -413,6 +444,35 @@ trait NotificationsBase ]; } + /** + * Count emails sitting in the maildev inbox that are addressed to the given + * recipient and carry the given subject. Mirrors the maildev polling used + * by the session-alert e2e suite. + */ + protected function countNotificationEmails(string $address, string $subject): int + { + $host = System::getEnv('_APP_SMTP_HOST', 'maildev'); + $inbox = \file_get_contents('http://' . $host . ':1080/email'); + if ($inbox === false) { + return 0; + } + + $emails = \json_decode($inbox, true) ?? []; + $count = 0; + foreach ($emails as $email) { + if (($email['subject'] ?? '') !== $subject) { + continue; + } + foreach ($email['to'] ?? [] as $recipient) { + if (($recipient['address'] ?? '') === $address) { + $count++; + } + } + } + + return $count; + } + /** * @return array */ From f2daae986a6ef86a5673dff81f3a16a6f426c51b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 3 Jun 2026 06:18:17 +0000 Subject: [PATCH 63/80] refactor(notifications): rename alerts collection to notifications Renames the platform `alerts` collection to `notifications` and widens `type` (100) and `body` (65535) so the collection can absorb a future data migration without redefining shared columns. Updates the worker, console adapter, notifications HTTP endpoints, queries validator, deletes worker, the V24 migration, and the affected unit fixtures. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/config/collections/platform.php | 10 ++-- src/Appwrite/Migration/Version/V24.php | 10 ++-- .../Http/Notifications/Logos/Appwrite/Get.php | 4 +- .../Http/Notifications/Update.php | 6 +-- .../Http/Notifications/XList.php | 6 +-- src/Appwrite/Platform/Workers/Deletes.php | 6 +-- .../Platform/Workers/Notifications.php | 6 +-- .../Validator/Queries/Notifications.php | 2 +- .../Utopia/Messaging/Adapter/Console.php | 2 +- .../unit/Migration/MigrationVersionsTest.php | 6 +-- tests/unit/Platform/Workers/DeletesTest.php | 4 +- .../Utopia/Messaging/Adapter/ConsoleTest.php | 50 +++++++++---------- 12 files changed, 56 insertions(+), 56 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 3c144a989b..167eb8cead 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1057,10 +1057,10 @@ $platformCollections = [ ], ], - 'alerts' => [ + 'notifications' => [ '$collection' => ID::custom(Database::METADATA), - '$id' => ID::custom('alerts'), - 'name' => 'Alerts', + '$id' => ID::custom('notifications'), + 'name' => 'Notifications', 'attributes' => [ [ '$id' => ID::custom('messageId'), @@ -1088,7 +1088,7 @@ $platformCollections = [ '$id' => ID::custom('type'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 64, + 'size' => 100, 'signed' => true, 'required' => false, 'default' => 'info', @@ -1209,7 +1209,7 @@ $platformCollections = [ '$id' => ID::custom('body'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 16384, + 'size' => 65535, 'signed' => true, 'required' => true, 'default' => null, diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index 7c69c751c1..fe43714315 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -90,11 +90,11 @@ class V24 extends Migration }; $collections = $this->collections[$collectionType]; - $alertsCollectionExists = false; + $notificationsCollectionExists = false; if ($collectionType === 'console') { - $alertsCollectionExists = !$this->dbForProject->getCollection('alerts')->isEmpty(); - $this->createCollection('alerts'); + $notificationsCollectionExists = !$this->dbForProject->getCollection('notifications')->isEmpty(); + $this->createCollection('notifications'); } foreach ($collections as $collection) { @@ -176,8 +176,8 @@ class V24 extends Migration $this->dbForProject->purgeCachedCollection($id); break; - case 'alerts': - if ($collectionType === 'console' && $alertsCollectionExists) { + case 'notifications': + if ($collectionType === 'console' && $notificationsCollectionExists) { foreach (['firstSeen', 'lastSeen'] as $attribute) { try { $this->createAttributeFromCollection($this->dbForProject, $id, $attribute); diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php index cace972628..7bb99c7ceb 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php @@ -114,7 +114,7 @@ class Get extends Action return null; } - $notifications = $dbForPlatform->find('alerts', [ + $notifications = $dbForPlatform->find('notifications', [ Query::equal('messageId', [(string) $decoded['messageId']]), Query::equal('channel', [(string) $decoded['channel']]), Query::equal('recipientHash', [(string) $decoded['recipientHash']]), @@ -143,7 +143,7 @@ class Get extends Action $updates['firstSeen'] = $seenAt; } - $updated = $dbForPlatform->updateDocument('alerts', $notification->getId(), new Document($updates)); + $updated = $dbForPlatform->updateDocument('notifications', $notification->getId(), new Document($updates)); $project = $dbForPlatform->getDocument('projects', $notification->getAttribute('projectId')); if ($project->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php index d7769c6732..9ca7b6b683 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php @@ -68,17 +68,17 @@ class Update extends Action throw new Exception(Exception::USER_UNAUTHORIZED); } - $notification = $dbForPlatform->getDocument('alerts', $notificationId); + $notification = $dbForPlatform->getDocument('notifications', $notificationId); if ($notification->isEmpty()) { - $exists = $authorization->skip(fn () => !$dbForPlatform->getDocument('alerts', $notificationId)->isEmpty()); + $exists = $authorization->skip(fn () => !$dbForPlatform->getDocument('notifications', $notificationId)->isEmpty()); if ($exists) { throw new Exception(Exception::USER_UNAUTHORIZED); } throw new Exception(Exception::DOCUMENT_NOT_FOUND); } - $updated = $dbForPlatform->updateDocument('alerts', $notificationId, new Document([ + $updated = $dbForPlatform->updateDocument('notifications', $notificationId, new Document([ 'read' => $read, ])); diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php index 7916ce29ff..c36e69f4ef 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php @@ -88,7 +88,7 @@ class XList extends Action } $notificationId = $cursor->getValue(); - $cursorDocument = $dbForPlatform->getDocument('alerts', $notificationId); + $cursorDocument = $dbForPlatform->getDocument('notifications', $notificationId); if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Notification '{$notificationId}' for the 'cursor' value not found."); @@ -103,8 +103,8 @@ class XList extends Action $filterQueries = Query::groupByType($queries)['filters']; try { - $results = $dbForPlatform->find('alerts', $queries); - $total = $dbForPlatform->count('alerts', $filterQueries, APP_LIMIT_COUNT); + $results = $dbForPlatform->find('notifications', $queries); + $total = $dbForPlatform->count('notifications', $filterQueries, APP_LIMIT_COUNT); } catch (OrderException $e) { throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null."); } diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index f45d990b0d..ed537d1674 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -738,15 +738,15 @@ class Deletes extends Action Console::error('Failed to delete schedules: ' . $th->getMessage()); } - // Delete Alerts + // Delete Notifications try { - $this->deleteByGroup('alerts', [ + $this->deleteByGroup('notifications', [ Query::equal('projectId', [$projectId]), Query::equal('projectInternalId', [$projectInternalId]), Query::orderAsc() ], $dbForPlatform); } catch (Throwable $th) { - Console::error('Failed to delete alerts: ' . $th->getMessage()); + Console::error('Failed to delete notifications: ' . $th->getMessage()); } // Delete Advisor insights diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php index 0b65f54f48..04f705e2d1 100644 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ b/src/Appwrite/Platform/Workers/Notifications.php @@ -159,7 +159,7 @@ class Notifications extends Action private function alreadyDelivered(Database $dbForPlatform, string $alertId): bool { - return !$dbForPlatform->getDocument('alerts', $alertId)->isEmpty(); + return !$dbForPlatform->getDocument('notifications', $alertId)->isEmpty(); } /** @@ -500,10 +500,10 @@ class Notifications extends Action ]); try { - $dbForPlatform->createDocument('alerts', $document); + $dbForPlatform->createDocument('notifications', $document); return $alertId; } catch (DuplicateException) { - $existing = $dbForPlatform->getDocument('alerts', $alertId); + $existing = $dbForPlatform->getDocument('notifications', $alertId); return $existing->isEmpty() ? $alertId : $existing->getId(); } } diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php b/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php index b1cde8ed82..a85609aaa7 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php @@ -20,6 +20,6 @@ class Notifications extends Base public function __construct() { - parent::__construct('alerts', self::ALLOWED_ATTRIBUTES); + parent::__construct('notifications', self::ALLOWED_ATTRIBUTES); } } diff --git a/src/Appwrite/Utopia/Messaging/Adapter/Console.php b/src/Appwrite/Utopia/Messaging/Adapter/Console.php index 46a17ec675..daf09c692a 100644 --- a/src/Appwrite/Utopia/Messaging/Adapter/Console.php +++ b/src/Appwrite/Utopia/Messaging/Adapter/Console.php @@ -89,7 +89,7 @@ class Console extends Adapter 'body' => $message->getBody(), ]); - $this->database->createDocument('alerts', $document); + $this->database->createDocument('notifications', $document); $delivered++; $response->addResult($key); } catch (DuplicateException) { diff --git a/tests/unit/Migration/MigrationVersionsTest.php b/tests/unit/Migration/MigrationVersionsTest.php index 7958d08f05..86871a5b2c 100644 --- a/tests/unit/Migration/MigrationVersionsTest.php +++ b/tests/unit/Migration/MigrationVersionsTest.php @@ -60,7 +60,7 @@ class MigrationVersionsTest extends TestCase \ob_end_clean(); } - $collection = $database->getCollection('alerts'); + $collection = $database->getCollection('notifications'); $this->assertFalse($collection->isEmpty()); $attributes = []; @@ -102,7 +102,7 @@ class MigrationVersionsTest extends TestCase ->setDatabase('migrationV24ExistingAlerts') ->setNamespace('migration_existing_alerts_' . \uniqid()); $database->create(); - $database->createCollection('alerts'); + $database->createCollection('notifications'); $migration = new V24(); $migration->setProject( @@ -120,7 +120,7 @@ class MigrationVersionsTest extends TestCase \ob_end_clean(); } - $collection = $database->getCollection('alerts'); + $collection = $database->getCollection('notifications'); $attributes = []; foreach ($collection->getAttribute('attributes', []) as $attribute) { $id = $attribute instanceof Document ? $attribute->getAttribute('$id') : ($attribute['$id'] ?? ''); diff --git a/tests/unit/Platform/Workers/DeletesTest.php b/tests/unit/Platform/Workers/DeletesTest.php index 04941b422b..09d028b2ec 100644 --- a/tests/unit/Platform/Workers/DeletesTest.php +++ b/tests/unit/Platform/Workers/DeletesTest.php @@ -75,9 +75,9 @@ class DeletesTest extends TestCase $this->assertSame('stop', $exception->getMessage()); } - $this->assertArrayHasKey('alerts', $worker->groups); + $this->assertArrayHasKey('notifications', $worker->groups); - $queries = $worker->groups['alerts']; + $queries = $worker->groups['notifications']; $this->assertSame(Query::TYPE_EQUAL, $queries[0]->getMethod()); $this->assertSame('projectId', $queries[0]->getAttribute()); $this->assertSame(['project-1'], $queries[0]->getValues()); diff --git a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php index 3b87e8854e..614700c726 100644 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ b/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php @@ -33,24 +33,24 @@ class ConsoleTest extends TestCase ->setNamespace('alerts_' . \uniqid()); $this->database->create(); - $this->database->createCollection('alerts', [], [], [Permission::create(Role::any()), Permission::read(Role::any())], false); - $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); - $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); - $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); - $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); - $this->database->createAttribute('alerts', 'firstSeen', Database::VAR_DATETIME, 0, false); - $this->database->createAttribute('alerts', 'lastSeen', Database::VAR_DATETIME, 0, false); + $this->database->createCollection('notifications', [], [], [Permission::create(Role::any()), Permission::read(Role::any())], false); + $this->database->createAttribute('notifications', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('notifications', 'recipientHash', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('notifications', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'parentResourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('notifications', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('notifications', 'read', Database::VAR_BOOLEAN, 0, false, false); + $this->database->createAttribute('notifications', 'firstSeen', Database::VAR_DATETIME, 0, false); + $this->database->createAttribute('notifications', 'lastSeen', Database::VAR_DATETIME, 0, false); } protected function tearDown(): void @@ -106,7 +106,7 @@ class ConsoleTest extends TestCase $this->assertSame(1, $result['deliveredTo']); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-aaa', 'user-1', RESOURCE_TYPE_USERS, 'user-1')); + $stored = $this->database->getDocument('notifications', $this->alertId('msg-aaa', 'user-1', RESOURCE_TYPE_USERS, 'user-1')); $this->assertFalse($stored->isEmpty()); $this->assertSame('msg-aaa', $stored->getAttribute('messageId')); $this->assertSame('console', $stored->getAttribute('channel')); @@ -139,7 +139,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-perms-user', 'user-2', RESOURCE_TYPE_USERS, 'user-2')); + $stored = $this->database->getDocument('notifications', $this->alertId('msg-perms-user', 'user-2', RESOURCE_TYPE_USERS, 'user-2')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::user('user-2')), $permissions); @@ -160,7 +160,7 @@ class ConsoleTest extends TestCase (new Console($this->database))->send($message); - $stored = $this->database->getDocument('alerts', $this->alertId('msg-team', 'team-9', RESOURCE_TYPE_TEAMS, 'team-9')); + $stored = $this->database->getDocument('notifications', $this->alertId('msg-team', 'team-9', RESOURCE_TYPE_TEAMS, 'team-9')); $permissions = $stored->getPermissions(); $this->assertContains(Permission::read(Role::team('team-9')), $permissions); @@ -195,8 +195,8 @@ class ConsoleTest extends TestCase $this->assertNotSame($idA, $idB, 'recipient suffixes must be distinct'); - $rowA = $this->database->getDocument('alerts', $idA); - $rowB = $this->database->getDocument('alerts', $idB); + $rowA = $this->database->getDocument('notifications', $idA); + $rowB = $this->database->getDocument('notifications', $idB); $this->assertFalse($rowA->isEmpty(), 'first recipient row must exist'); $this->assertFalse($rowB->isEmpty(), 'second recipient row must exist'); @@ -235,7 +235,7 @@ class ConsoleTest extends TestCase // Pre-insert an alert with the SAME id the adapter will compute. The // adapter's createDocument will hit the primary-key DuplicateException // and must treat it as a successful (idempotent) send. - $this->database->createDocument('alerts', new Document([ + $this->database->createDocument('notifications', new Document([ '$id' => $documentId, '$permissions' => [Permission::read(Role::any())], 'messageId' => $messageId, @@ -272,7 +272,7 @@ class ConsoleTest extends TestCase // Still exactly ONE row: the pre-existing one. The adapter must not // overwrite it nor create a sibling. - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(1, $rows); $this->assertSame($documentId, $rows[0]->getId()); $this->assertSame('pre-existing', $rows[0]->getAttribute('title'), 'duplicate path must not overwrite existing row'); From 7bcc091268297580bd47963bb45c447436b60ba0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 4 Jun 2026 04:51:38 +0000 Subject: [PATCH 64/80] fix(notifications): align tests with notifications collection rename and import order - Update NotificationsTest (worker + module) to create/query the `notifications` collection instead of `alerts`; the rename commit renamed the worker target but left the tests on the old collection, causing 21 "Collection not found" errors/failures. - Fix import ordering in app/init/resources.php (Migration before Notification) flagged by the Format check after the 1.9.x merge. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/init/resources.php | 2 +- .../Notifications/NotificationsTest.php | 34 ++++---- .../Platform/Workers/NotificationsTest.php | 80 +++++++++---------- 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 4f790a5dc1..51d0b25f8a 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -11,8 +11,8 @@ use Appwrite\Event\Publisher\Execution as ExecutionPublisher; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Messaging as MessagingPublisher; -use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Migration as MigrationPublisher; +use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; diff --git a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php index d5126a68a5..4ec5f6442f 100644 --- a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php +++ b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php @@ -56,22 +56,22 @@ class NotificationsTest extends TestCase ]; $this->database->create(); - $this->database->createCollection('alerts', [], [], $permissions, false); - $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); - $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); - $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); - $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); + $this->database->createCollection('notifications', [], [], $permissions, false); + $this->database->createAttribute('notifications', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('notifications', 'recipientHash', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('notifications', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'parentResourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('notifications', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('notifications', 'read', Database::VAR_BOOLEAN, 0, false, false); } protected function tearDown(): void @@ -105,7 +105,7 @@ class NotificationsTest extends TestCase private function createNotification(string $id, string $resourceType, string $resourceId): Document { - return $this->database->createDocument('alerts', new Document([ + return $this->database->createDocument('notifications', new Document([ '$id' => $id, 'messageId' => $id, 'recipientHash' => \substr(\md5($id), 0, 16), diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index 737e1594cc..f115c7cab2 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -170,35 +170,35 @@ class NotificationsTest extends TestCase $this->database->create(); $this->database->createCollection( - 'alerts', + 'notifications', [], [], [Permission::create(Role::any()), Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(Role::any())], false, ); - $this->database->createAttribute('alerts', 'messageId', Database::VAR_STRING, 255, false); - $this->database->createAttribute('alerts', 'recipientHash', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'type', Database::VAR_STRING, 64, false, 'info'); - $this->database->createAttribute('alerts', 'channel', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'projectId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'projectInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'resourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'resourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'resourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'parentResourceType', Database::VAR_STRING, 64, true); - $this->database->createAttribute('alerts', 'parentResourceId', Database::VAR_STRING, 255, true); - $this->database->createAttribute('alerts', 'parentResourceInternalId', Database::VAR_ID, 0, true); - $this->database->createAttribute('alerts', 'title', Database::VAR_STRING, 256, true); - $this->database->createAttribute('alerts', 'body', Database::VAR_STRING, 16384, true); - $this->database->createAttribute('alerts', 'read', Database::VAR_BOOLEAN, 0, false, false); - $this->database->createAttribute('alerts', 'firstSeen', Database::VAR_DATETIME, 0, false); - $this->database->createAttribute('alerts', 'lastSeen', Database::VAR_DATETIME, 0, false); + $this->database->createAttribute('notifications', 'messageId', Database::VAR_STRING, 255, false); + $this->database->createAttribute('notifications', 'recipientHash', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'type', Database::VAR_STRING, 64, false, 'info'); + $this->database->createAttribute('notifications', 'channel', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'projectId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'projectInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'resourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'resourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'resourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'parentResourceType', Database::VAR_STRING, 64, true); + $this->database->createAttribute('notifications', 'parentResourceId', Database::VAR_STRING, 255, true); + $this->database->createAttribute('notifications', 'parentResourceInternalId', Database::VAR_ID, 0, true); + $this->database->createAttribute('notifications', 'title', Database::VAR_STRING, 256, true); + $this->database->createAttribute('notifications', 'body', Database::VAR_STRING, 16384, true); + $this->database->createAttribute('notifications', 'read', Database::VAR_BOOLEAN, 0, false, false); + $this->database->createAttribute('notifications', 'firstSeen', Database::VAR_DATETIME, 0, false); + $this->database->createAttribute('notifications', 'lastSeen', Database::VAR_DATETIME, 0, false); // Mirror the production `_key_recipient` UNIQUE composite index so the // duplicate-handling branch in persistAlert (catch DuplicateException -> // return existing alertId) is actually exercised by tests. $this->database->createIndex( - 'alerts', + 'notifications', '_key_recipient', Database::INDEX_UNIQUE, ['messageId', 'channel', 'recipientHash'], @@ -311,7 +311,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(2, $rows); $resourceIds = \array_map(static fn (Document $row) => $row->getAttribute('resourceId'), $rows); \sort($resourceIds); @@ -525,7 +525,7 @@ class NotificationsTest extends TestCase $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $tags['channel'] ?? null); $this->assertSame('boom', $tags['error'] ?? null); - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(0, $rows, 'failed dispatch must not persist alert'); } @@ -553,7 +553,7 @@ class NotificationsTest extends TestCase $this->assertSame('webhook down', $error->getMessage()); } - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'first attempt should persist only the successful console recipient'); @@ -565,7 +565,7 @@ class NotificationsTest extends TestCase $this->assertCount(1, $retry->dispatched, 'retry should dispatch only the previously undelivered webhook'); $this->assertSame(NOTIFICATION_TYPE_WEBHOOK, $retry->dispatched[0]['channel']); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(2, $rows, 'retry must complete the missing recipient without duplicating console'); @@ -590,7 +590,7 @@ class NotificationsTest extends TestCase // The Console adapter wrote exactly one alert; the action loop // must NOT have called persistAlert (otherwise we'd see 2 rows or // a duplicate-key swallow plus a non-zero counter). - $consoleRows = $this->database->find('alerts', [ + $consoleRows = $this->database->find('notifications', [ Query::equal('channel', ['console']), ]); $this->assertCount(1, $consoleRows); @@ -618,7 +618,7 @@ class NotificationsTest extends TestCase $this->assertStringContainsString('Console alert delivery failed', $error->getMessage()); } - $this->assertCount(0, $this->database->find('alerts'), 'failed console dispatch must not persist alert'); + $this->assertCount(0, $this->database->find('notifications'), 'failed console dispatch must not persist alert'); } public function testMultiRecipientFanoutNoCollision(): void @@ -638,7 +638,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(2, $rows, 'two recipients must produce two distinct alert rows'); $messageId = \md5('fanout'); @@ -675,7 +675,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(1, $rows); $this->assertSame(RESOURCE_TYPE_USERS, $rows[0]->getAttribute('resourceType')); $this->assertSame('u1', $rows[0]->getAttribute('resourceId')); @@ -874,13 +874,13 @@ class NotificationsTest extends TestCase $threw = false; try { - $this->database->createDocument('alerts', $sameTupleDoc); + $this->database->createDocument('notifications', $sameTupleDoc); } catch (\Utopia\Database\Exception\Duplicate) { $threw = true; } $this->assertTrue($threw, 'unique-index `_key_recipient` must reject a second row sharing the recipient tuple'); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'unique-index must prevent a second row from being persisted'); @@ -919,7 +919,7 @@ class NotificationsTest extends TestCase $this->assertCount(1, $worker->persistedIds); $alertId = $worker->persistedIds[0]; - $row = $this->database->getDocument('alerts', $alertId); + $row = $this->database->getDocument('notifications', $alertId); $this->assertFalse($row->isEmpty(), 'persistAlert must return an id resolvable via getDocument'); $this->assertSame(RESOURCE_TYPE_USERS, $row->getAttribute('resourceType')); $this->assertSame('user-7', $row->getAttribute('resourceId')); @@ -973,7 +973,7 @@ class NotificationsTest extends TestCase // Critical: no orphan dedup row. If there is one, the retry below // will short-circuit and the user never gets the email. - $orphans = $this->database->find('alerts', [ + $orphans = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(0, $orphans, 'failed SMTP send must not leave a dedup row behind'); @@ -988,7 +988,7 @@ class NotificationsTest extends TestCase $this->assertSame(1, $working->sendCount, 'retry must invoke the working adapter'); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows, 'retry must persist exactly one alert row'); @@ -1033,7 +1033,7 @@ class NotificationsTest extends TestCase } $this->assertTrue($threw, 'SMTP failure must still propagate so the email recipient is retried'); - $consoleRows = $this->database->find('alerts', [ + $consoleRows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), Query::equal('channel', [NOTIFICATION_TYPE_CONSOLE]), ]); @@ -1041,7 +1041,7 @@ class NotificationsTest extends TestCase $this->assertSame('project-x', $consoleRows[0]->getAttribute('projectId')); $this->assertSame('project-internal-x', $consoleRows[0]->getAttribute('projectInternalId')); - $emailRows = $this->database->find('alerts', [ + $emailRows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), Query::equal('channel', [NOTIFICATION_TYPE_EMAIL]), ]); @@ -1054,7 +1054,7 @@ class NotificationsTest extends TestCase $retryWorker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); $this->assertSame(1, $working->sendCount, 'retry must still deliver the email recipient'); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(2, $rows, 'retry must add email without duplicating the already-delivered console alert'); @@ -1119,7 +1119,7 @@ class NotificationsTest extends TestCase $this->assertSame(1, $worker->persistAlertCalls, 'email channel must persist exactly once after a successful send'); $messageId = \md5('happy-email'); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [$messageId]), ]); $this->assertCount(1, $rows); @@ -1165,7 +1165,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('channel', ['console']), ]); $this->assertCount(1, $rows, 'console adapter must write exactly one alert'); @@ -1208,7 +1208,7 @@ class NotificationsTest extends TestCase $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $rows = $this->database->find('alerts'); + $rows = $this->database->find('notifications'); $this->assertCount(1, $rows); $this->assertSame('Webhook orders paused', $rows[0]->getAttribute('title')); $this->assertSame('Plain alert for orders.', $rows[0]->getAttribute('body')); @@ -1245,7 +1245,7 @@ class NotificationsTest extends TestCase $this->assertTrue($threw, 'missing SMTP must fail email delivery so the queue can retry'); $this->assertSame(0, $worker->persistAlertCalls); - $this->assertCount(0, $this->database->find('alerts')); + $this->assertCount(0, $this->database->find('notifications')); $this->assertSame('no_smtp', $this->log->getTags()['email_skipped'] ?? null); } @@ -1371,7 +1371,7 @@ class NotificationsTest extends TestCase // Action loop must persist exactly one webhook alert row AFTER the send. $this->assertSame(1, $worker->persistAlertCalls); - $rows = $this->database->find('alerts', [ + $rows = $this->database->find('notifications', [ Query::equal('messageId', [\md5('happy-webhook')]), ]); $this->assertCount(1, $rows); From 0245a32a99e6e61bc486b0a804b9322f5b3c145d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 4 Jun 2026 04:54:44 +0000 Subject: [PATCH 65/80] fix(notifications): scope listNotifications to console channel The user-facing notification list queried by resourceType + resourceId but not channel, so email and webhook delivery-receipt rows (persisted to the same notifications collection) appeared alongside in-app console alerts. Filter on channel = console. Cover it with a test that seeds a non-console row and asserts it is excluded. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Modules/Notifications/Http/Notifications/XList.php | 1 + .../Platform/Modules/Notifications/NotificationsTest.php | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php index c36e69f4ef..558190d99a 100644 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php +++ b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php @@ -97,6 +97,7 @@ class XList extends Action $cursor->setValue($cursorDocument); } + $queries[] = Query::equal('channel', [NOTIFICATION_TYPE_CONSOLE]); $queries[] = Query::equal('resourceType', [RESOURCE_TYPE_USERS]); $queries[] = Query::equal('resourceId', [$user->getId()]); diff --git a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php index 4ec5f6442f..fd0745f3ff 100644 --- a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php +++ b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php @@ -84,6 +84,7 @@ class NotificationsTest extends TestCase { $this->createNotification('user-alert', RESOURCE_TYPE_USERS, 'user-a'); $this->createNotification('team-alert', RESOURCE_TYPE_TEAMS, 'team-a'); + $this->createNotification('user-email-receipt', RESOURCE_TYPE_USERS, 'user-a', NOTIFICATION_TYPE_EMAIL); $response = new CapturingNotificationsResponse(); @@ -103,14 +104,14 @@ class NotificationsTest extends TestCase $this->assertSame(Response::MODEL_NOTIFICATION_LIST, $response->model); } - private function createNotification(string $id, string $resourceType, string $resourceId): Document + private function createNotification(string $id, string $resourceType, string $resourceId, string $channel = NOTIFICATION_TYPE_CONSOLE): Document { return $this->database->createDocument('notifications', new Document([ '$id' => $id, 'messageId' => $id, 'recipientHash' => \substr(\md5($id), 0, 16), 'type' => 'info', - 'channel' => NOTIFICATION_TYPE_CONSOLE, + 'channel' => $channel, 'projectId' => 'project', 'projectInternalId' => '10', 'resourceType' => $resourceType, From 21a4b2817ea0fe4fb297359b0c974038ee9f0e4b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 4 Jun 2026 05:05:42 +0000 Subject: [PATCH 66/80] fix(notifications): remove dead owner-role branch in Webhooks alert hasOwnerRole checked for a 'project-{id}-owner' role string that team memberships never store (roles are plain values like 'owner'/'member'), so the branch was dead. Simplify to the 'owner' check and drop the now unused projectId parameter. Addresses the remaining Greptile finding. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Appwrite/Platform/Workers/Webhooks.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 695c2b0e5b..22926e27ff 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -229,7 +229,7 @@ class Webhooks extends Action $ownerMemberships = \array_filter( $memberships, - fn (Document $membership) => self::hasOwnerRole($membership, $project->getId()) + fn (Document $membership) => self::hasOwnerRole($membership) ); if (empty($ownerMemberships)) { @@ -326,7 +326,7 @@ class Webhooks extends Action } } - private static function hasOwnerRole(Document $membership, string $projectId): bool + private static function hasOwnerRole(Document $membership): bool { $roles = $membership->getAttribute('roles', []); if (\is_string($roles)) { @@ -341,8 +341,7 @@ class Webhooks extends Action continue; } - $role = \strtolower($role); - if ($role === 'owner' || $role === 'project-' . \strtolower($projectId) . '-owner') { + if (\strtolower($role) === 'owner') { return true; } } From 25f898e5c94f720909af9e4147f93333e6b4ea82 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 16 Jun 2026 04:26:23 +0000 Subject: [PATCH 67/80] test(notifications): align owner-role test with simplified hasOwnerRole hasOwnerRole no longer takes a projectId or matches the project-scoped 'project-{id}-owner' string (that role format is never stored). Update the data provider so those cases assert they are not recognized as owner and drop the removed argument from the reflection call. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/unit/Platform/Workers/WebhooksTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 28568f9476..724654ab3c 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -197,7 +197,7 @@ class WebhooksTest extends TestCase } #[DataProvider('ownerRoleProvider')] - public function testOwnerRoleDetectionAcceptsArrayAndLegacyStringRoles(mixed $roles, bool $expected): void + public function testOwnerRoleDetectionAcceptsArrayAndCommaStringRoles(mixed $roles, bool $expected): void { $method = new \ReflectionMethod(Webhooks::class, 'hasOwnerRole'); $membership = new Document([ @@ -205,7 +205,7 @@ class WebhooksTest extends TestCase 'roles' => $roles, ]); - $this->assertSame($expected, $method->invoke(null, $membership, 'project-1')); + $this->assertSame($expected, $method->invoke(null, $membership)); } public static function ownerRoleProvider(): array @@ -213,10 +213,10 @@ class WebhooksTest extends TestCase return [ 'array owner' => [['owner'], true], 'array mixed case owner' => [['Owner'], true], - 'project owner' => [['project-project-1-owner'], true], - 'mixed case project owner' => [['Project-Project-1-Owner'], true], 'comma string owner' => ['developer, owner', true], - 'comma string project owner' => ['developer, project-project-1-owner', true], + 'project-scoped owner string not recognized' => [['project-project-1-owner'], false], + 'mixed case project-scoped owner string not recognized' => [['Project-Project-1-Owner'], false], + 'comma string project-scoped owner not recognized' => ['developer, project-project-1-owner', false], 'other project owner' => [['project-project-2-owner'], false], 'non owner' => [['developer'], false], 'invalid roles' => [null, false], From 3c31bfbb21c2bd922683b575e90d2c291f59f564 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Tue, 23 Jun 2026 02:25:13 +0000 Subject: [PATCH 68/80] fix(notifications): satisfy messaging 1.1.0 Message interface and align tests The 1.9.x merge bumped utopia-php/messaging to 1.1.0, which added setOrigin/getOrigin to the Message interface; implement them on the Console and Webhook message classes (fatal on class load otherwise). Also pass the new Telemetry argument to Mails::action() in MailsTest and apply the repository Rector rules to the notification test suite so the Checks (lint/rector/phpstan) job is green. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Utopia/Messaging/Messages/Console.php | 14 +++++++ .../Utopia/Messaging/Messages/Webhook.php | 14 +++++++ .../Notifications/NotificationsTest.php | 2 +- .../Modules/Projects/SchedulesTest.php | 2 +- tests/unit/Platform/Workers/MailsTest.php | 6 ++- .../Platform/Workers/NotificationsTest.php | 10 ++--- tests/unit/Platform/Workers/WebhooksTest.php | 38 +++++++++---------- 7 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/Appwrite/Utopia/Messaging/Messages/Console.php b/src/Appwrite/Utopia/Messaging/Messages/Console.php index f56b592af6..20d85f27e0 100644 --- a/src/Appwrite/Utopia/Messaging/Messages/Console.php +++ b/src/Appwrite/Utopia/Messaging/Messages/Console.php @@ -6,6 +6,8 @@ use Utopia\Messaging\Message; class Console implements Message { + private ?string $origin = null; + /** * @param array $recipients */ @@ -65,4 +67,16 @@ class Console implements Message { return $this->projectInternalId; } + + public function setOrigin(?string $origin): self + { + $this->origin = $origin; + + return $this; + } + + public function getOrigin(): ?string + { + return $this->origin; + } } diff --git a/src/Appwrite/Utopia/Messaging/Messages/Webhook.php b/src/Appwrite/Utopia/Messaging/Messages/Webhook.php index 2852dbeac3..e1146f9bd0 100644 --- a/src/Appwrite/Utopia/Messaging/Messages/Webhook.php +++ b/src/Appwrite/Utopia/Messaging/Messages/Webhook.php @@ -6,6 +6,8 @@ use Utopia\Messaging\Message; class Webhook implements Message { + private ?string $origin = null; + /** * @param array $urls * @param array $payload @@ -63,4 +65,16 @@ class Webhook implements Message { return $this->timeout; } + + public function setOrigin(?string $origin): self + { + $this->origin = $origin; + + return $this; + } + + public function getOrigin(): ?string + { + return $this->origin; + } } diff --git a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php index c128fade15..5daed2633e 100644 --- a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php +++ b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php @@ -34,7 +34,7 @@ final class CapturingNotificationsResponse extends Response } } -class NotificationsTest extends TestCase +final class NotificationsTest extends TestCase { private Authorization $authorization; private Database $database; diff --git a/tests/unit/Platform/Modules/Projects/SchedulesTest.php b/tests/unit/Platform/Modules/Projects/SchedulesTest.php index 8b3f063c21..ca3f105f6c 100644 --- a/tests/unit/Platform/Modules/Projects/SchedulesTest.php +++ b/tests/unit/Platform/Modules/Projects/SchedulesTest.php @@ -37,7 +37,7 @@ final class CapturingSchedulesResponse extends Response } } -class SchedulesTest extends TestCase +final class SchedulesTest extends TestCase { private Authorization $authorization; private Database $database; diff --git a/tests/unit/Platform/Workers/MailsTest.php b/tests/unit/Platform/Workers/MailsTest.php index 172480c129..a6b7a123d7 100644 --- a/tests/unit/Platform/Workers/MailsTest.php +++ b/tests/unit/Platform/Workers/MailsTest.php @@ -12,6 +12,7 @@ use Utopia\Messaging\Adapter\Email as EmailAdapter; use Utopia\Messaging\Messages\Email as EmailMessage; use Utopia\Queue\Message; use Utopia\Registry\Registry; +use Utopia\Telemetry\Adapter\None; final class SpyMailAdapter extends EmailAdapter { @@ -41,7 +42,7 @@ final class SpyMailAdapter extends EmailAdapter } } -class MailsTest extends TestCase +final class MailsTest extends TestCase { public function testLegacyMailPayloadIsSentByMailsWorker(): void { @@ -76,13 +77,14 @@ class MailsTest extends TestCase new Document(['$id' => 'project-x']), $registry, new Log(), + new None(), ); } finally { \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); } $this->assertSame(1, $adapter->sendCount); - $this->assertNotNull($adapter->captured); + $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $adapter->captured); $message = $adapter->captured; $this->assertSame('legacy@example.test', $message->getTo()[0]['email'] ?? ''); diff --git a/tests/unit/Platform/Workers/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php index fe3eaeaa09..96c93c42fe 100644 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ b/tests/unit/Platform/Workers/NotificationsTest.php @@ -151,7 +151,7 @@ class SpyEmailAdapter extends EmailAdapter } } -class NotificationsTest extends TestCase +final class NotificationsTest extends TestCase { private Database $database; private Authorization $authorization; @@ -414,7 +414,7 @@ class NotificationsTest extends TestCase } $this->assertSame(1, $spy->sendCount); - $this->assertNotNull($spy->captured); + $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $spy->captured); $message = $spy->captured; $this->assertSame('legacy@example.test', $message->getTo()[0]['email'] ?? ''); @@ -724,7 +724,7 @@ class NotificationsTest extends TestCase \putenv($previousConsoleDomain === false ? '_APP_CONSOLE_DOMAIN' : '_APP_CONSOLE_DOMAIN=' . $previousConsoleDomain); } - $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); + $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); $body = $spy->captured->getContent(); $this->assertStringContainsString(' must be present'); @@ -795,7 +795,7 @@ class NotificationsTest extends TestCase \putenv($previousOpenSslKey === false ? '_APP_OPENSSL_KEY_V1' : '_APP_OPENSSL_KEY_V1=' . $previousOpenSslKey); } - $this->assertNotNull($spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); + $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); $this->assertStringNotContainsString('/v1/notifications/logos/appwrite?jwt=', $spy->captured->getContent()); } @@ -1103,7 +1103,7 @@ class NotificationsTest extends TestCase } $this->assertSame(1, $spy->sendCount, 'SMTP send must be invoked exactly once'); - $this->assertNotNull($spy->captured); + $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $spy->captured); $message = $spy->captured; $this->assertSame('happy@example.test', $message->getTo()[0]['email'] ?? ''); diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php index 9f8ce4c745..356db9f146 100644 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ b/tests/unit/Platform/Workers/WebhooksTest.php @@ -67,10 +67,10 @@ final class WebhooksTest extends TestCase \realpath(__DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl'), \realpath($payload['bodyTemplate']) ); - $this->assertStringContainsString('Payments', $payload['body']); - $this->assertStringContainsString('Ada Lovelace', $payload['body']); - $this->assertStringContainsString('/console/project-fra-project-1/settings/webhooks/webhook-1', $payload['body']); - $this->assertStringNotContainsString('{{', $payload['body']); + $this->assertStringContainsString('Payments', (string) $payload['body']); + $this->assertStringContainsString('Ada Lovelace', (string) $payload['body']); + $this->assertStringContainsString('/console/project-fra-project-1/settings/webhooks/webhook-1', (string) $payload['body']); + $this->assertStringNotContainsString('{{', (string) $payload['body']); $this->assertSame(APP_NAME, $payload['variables']['platform']); $this->assertSame(APP_EMAIL_LOGO_URL, $payload['variables']['logoUrl']); $this->assertCount(2, $payload['recipients']); @@ -153,10 +153,10 @@ final class WebhooksTest extends TestCase $this->assertArrayHasKey('owner@example.test', $bodies); $this->assertArrayHasKey('grace@example.test', $bodies); - $this->assertStringContainsString('Ada Lovelace', $bodies['owner@example.test']); - $this->assertStringNotContainsString('Grace Hopper', $bodies['owner@example.test']); - $this->assertStringContainsString('Grace Hopper', $bodies['grace@example.test']); - $this->assertStringNotContainsString('Ada Lovelace', $bodies['grace@example.test']); + $this->assertStringContainsString('Ada Lovelace', (string) $bodies['owner@example.test']); + $this->assertStringNotContainsString('Grace Hopper', (string) $bodies['owner@example.test']); + $this->assertStringContainsString('Grace Hopper', (string) $bodies['grace@example.test']); + $this->assertStringNotContainsString('Ada Lovelace', (string) $bodies['grace@example.test']); } public function testSendAlertDeduplicationKeyChangesPerPauseCycle(): void @@ -210,19 +210,17 @@ final class WebhooksTest extends TestCase $this->assertSame($expected, $method->invoke(null, $membership)); } - public static function ownerRoleProvider(): array + public static function ownerRoleProvider(): \Iterator { - return [ - 'array owner' => [['owner'], true], - 'array mixed case owner' => [['Owner'], true], - 'comma string owner' => ['developer, owner', true], - 'project-scoped owner string not recognized' => [['project-project-1-owner'], false], - 'mixed case project-scoped owner string not recognized' => [['Project-Project-1-Owner'], false], - 'comma string project-scoped owner not recognized' => ['developer, project-project-1-owner', false], - 'other project owner' => [['project-project-2-owner'], false], - 'non owner' => [['developer'], false], - 'invalid roles' => [null, false], - ]; + yield 'array owner' => [['owner'], true]; + yield 'array mixed case owner' => [['Owner'], true]; + yield 'comma string owner' => ['developer, owner', true]; + yield 'project-scoped owner string not recognized' => [['project-project-1-owner'], false]; + yield 'mixed case project-scoped owner string not recognized' => [['Project-Project-1-Owner'], false]; + yield 'comma string project-scoped owner not recognized' => ['developer, project-project-1-owner', false]; + yield 'other project owner' => [['project-project-2-owner'], false]; + yield 'non owner' => [['developer'], false]; + yield 'invalid roles' => [null, false]; } private function createPlatformDatabase(): Database From bb21c18ad319f2226d8804ceab764b55acbb5490 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:02:32 +0530 Subject: [PATCH 69/80] Prepare 1.9.5 release updates --- .agents/skills/self-hosted-release/SKILL.md | 139 +++++++++++++++++ .../self-hosted-release/agents/openai.yaml | 4 + .env | 140 +++++++++--------- app/views/install/compose.phtml | 2 +- docker-compose.override.yml | 3 +- docker-compose.yml | 24 +-- 6 files changed, 230 insertions(+), 82 deletions(-) create mode 100644 .agents/skills/self-hosted-release/SKILL.md create mode 100644 .agents/skills/self-hosted-release/agents/openai.yaml diff --git a/.agents/skills/self-hosted-release/SKILL.md b/.agents/skills/self-hosted-release/SKILL.md new file mode 100644 index 0000000000..989b8f4c1a --- /dev/null +++ b/.agents/skills/self-hosted-release/SKILL.md @@ -0,0 +1,139 @@ +--- +name: self-hosted-release +description: Enforce Appwrite self-hosted release readiness checks. Use when validating a self-hosted RC or final release, running fresh Docker Compose install tests, testing realistic e2e workflows, discovering the previous stable release baseline, upgrading seeded data from the previous self-hosted release, verifying migrations, compatibility filters, release metadata, installer behavior, or deciding whether a self-hosted release is safe to publish. +compatibility: Requires an Appwrite server repository checkout, Docker Compose, network access to fetch tags/images, and the normal Appwrite test environment. +--- + +# Self-Hosted Release + +## Purpose + +Use this skill as a release gate for Appwrite self-hosted RCs and final releases. Do not treat container health or metadata review as enough. A release is not ready until a fresh self-hosted stack works with realistic workflows and an upgrade from the previous stable self-hosted release preserves seeded data through migrations. + +## Release Context + +1. Confirm the current branch and target version: + - Check the branch name, `app/init/constants.php`, release notes, Docker image tags, and the user's stated target. + - If those disagree, stop and resolve the target version before validating. +2. Determine the previous stable self-hosted baseline automatically: + - Prefer remote tags so stale local tags do not mislead the gate. + - Use stable semver tags lower than the target version; ignore RC, beta, alpha, dev, and other prerelease tags. + - Pick the highest stable version lower than the target as the upgrade and compatibility baseline. + - Only ask the user for a baseline if no stable lower tag can be discovered or the target version is ambiguous. +3. Record the target version, target commit SHA, previous stable baseline, database adapter(s), Compose profile(s), and any release-specific risks before starting validation. + +Useful commands: + +```sh +git rev-parse --abbrev-ref HEAD +git rev-parse HEAD +rg -n "APP_VERSION_STABLE|APP_CACHE_BUSTER" app/init/constants.php +git ls-remote --tags origin "refs/tags/*" +``` + +## Fresh Install Gate + +Build and run the target release with the local Docker Compose stack, then test it like a real self-hosted install. + +1. Start from a clean stack and build the target: + +```sh +docker compose down -v +docker compose up -d --force-recreate --build --wait +docker compose exec -T appwrite vars +curl -fsS http://localhost/v1/health/version +``` + +2. Inspect runtime health before running tests: + - Check `docker compose ps` and `docker compose logs` for crash loops, missing env vars, failed mounts, worker failures, executor/runtime failures, migration errors, queue errors, and warnings that indicate broken self-hosted wiring. + - Verify required services are reachable through the public HTTP surface, not only from inside containers. + - If a route is advertised by specs, docs, install templates, or release notes, verify the route actually exists and returns the expected auth/validation behavior. + +3. Run the Appwrite test suite inside Docker: + +```sh +docker compose exec -T appwrite test /usr/src/code/tests/unit +docker compose exec -T appwrite test /usr/src/code/tests/e2e/General +docker compose exec -T appwrite sh -lc 'vendor/bin/paratest --processes "$(nproc)" /usr/src/code/tests/e2e/Services --exclude-group abuseEnabled --exclude-group screenshots' +``` + +When validating a risky release, broaden the matrix with the same database adapters and modes CI uses. At minimum, cover the default self-hosted adapter and every adapter touched by migrations, storage, query behavior, relationships, or installer changes. + +4. Exercise realistic workflows with real-ish data: + - Create console users, projects, API keys, platforms, webhooks, teams, memberships, and sessions. + - Create databases/tables/columns/indexes/relationships and rows with varied permissions, sizes, types, pagination, filters, and updates. + - Create storage buckets and files, then read, update, preview/download, and delete them through public APIs. + - Create and execute functions/sites when compute, runtimes, builds, domains, or executor behavior changed. + - Exercise messaging, realtime, domains, auth providers, abuse/rate-limit behavior, and background workers when they are in scope. + - Use public APIs and SDK-like calls wherever practical; direct database inspection is only supporting evidence. + +Block the release for any missing env var, broken container dependency, failing public route, unexpected 5xx, queue/worker failure, test failure, or workflow that only works with unrealistic empty data. + +## Upgrade Gate + +Validate upgrade from the previous stable self-hosted release to the target release using the same persisted volumes and configuration. + +1. Install the previous stable baseline: + - Use the discovered baseline tag/image, not a hardcoded version. + - Start with the documented self-hosted install path and a clean volume set. + - Confirm `/v1/health/version` reports the baseline version. + +2. Seed broad, realistic data on the baseline before upgrading: + - Include multiple projects, users, teams, memberships, API keys, sessions/tokens, databases/tables/rows, attributes, indexes, relationships, permissions, buckets, files, functions/sites, executions, webhooks, messaging/realtime resources, domains, and project settings as applicable. + - Include edge-shaped data that migrations commonly break: empty values, long strings, arrays, enums, relationships, large files, many rows, deleted/disabled resources, and mixed permissions. + - Capture enough IDs and API calls to verify the same resources after upgrade. + +3. Upgrade the baseline stack to the target release: + - Reuse the same volumes and configuration. + - Replace the Appwrite image/tag with the target release build. + - Run the documented upgrade path and required migration command(s). + - Capture logs for appwrite, workers, databases, redis, executor, and proxy services during the first boot. + +4. Verify migration behavior: + - Migration completes without fatal errors, partial failures, or skipped projects. + - Running the migration again is safe and idempotent. + - A failed or interrupted migration can be retried without data loss when that scenario is relevant to the changed migration. + - Existing volumes can be reused after restart. + +5. Verify seeded data after upgrade through public APIs: + - Fetch and mutate seeded projects, users, teams, permissions, rows, files, functions/sites, executions, domains, webhooks, messaging/realtime resources, and settings. + - Confirm indexes, relationships, file contents, execution history, auth/session behavior, and background worker side effects still work. + - Run focused e2e tests against the upgraded stack for every service touched by migration, compatibility, installer, or public API changes. + +Block the release for migration failure, non-idempotency, data loss, changed permissions, broken reads/writes on upgraded data, failed workers/queues after upgrade, or any behavior that only works on fresh installs. + +## Metadata And Compatibility Gate + +Review release metadata after the runtime gates, not instead of them: + +- `app/init/constants.php`: `APP_VERSION_STABLE` is the target version and `APP_CACHE_BUSTER` is incremented. +- `docker-compose.yml` and `app/views/install/compose.phtml`: Appwrite and console image tags are correct for the target release. +- `README.md` and `README-CN.md`: self-hosted install snippets use the target release. +- `src/Appwrite/Migration/Migration.php`: the target version maps to the correct migration class. +- `CHANGES.md`: the target version has user-facing notes for notable changes, fixes, migrations, compatibility notes, and self-hosted operator impact. +- Generated/public outputs are refreshed when API specs, SDKs, install templates, Docker Compose output, or metadata-driven docs changed. + +For public API changes, compare against the previous stable baseline: + +- Look for renamed/removed fields, new required inputs, enum changes, response model changes, status-code changes, route behavior changes, permission changes, and SDK-visible serialization changes. +- Request filters live in `src/Appwrite/Utopia/Request/Filters/V*.php`. +- Response filters live in `src/Appwrite/Utopia/Response/Filters/V*.php`. +- Filters must be registered in `app/controllers/general.php` for the correct `x-appwrite-response-format` boundary. +- Every compatibility filter needs focused unit coverage under `tests/unit/Utopia/Request/Filters` or `tests/unit/Utopia/Response/Filters`. +- Add service e2e coverage with `x-appwrite-response-format` when behavior depends on routing, auth, persistence, validation, or serialization. + +Block the release when a public breaking change lacks a compatibility filter and tests, unless the release owner explicitly accepts the break and documents it in release notes. + +## Release Decision + +Do not approve an RC or final self-hosted release until all gates pass: + +- Fresh install stack builds, starts, and survives realistic workflows. +- Full relevant tests pass inside Docker. +- No missing env vars, broken mounts, failed workers, broken routes, or unexplained runtime errors remain. +- Previous stable release is installed, seeded with broad data, upgraded to the target, migrated, restarted, and verified through public APIs. +- Migrations are complete, idempotent, retryable where relevant, and preserve seeded data. +- Compatibility filters and tests cover unintended public breaking changes. +- Version constants, image tags, install snippets, migration mapping, generated outputs, and changelog are current. + +For every RC/final validation, produce a short release evidence summary with: target version, commit SHA, previous stable baseline, tested database adapters and modes, fresh install scenarios, upgrade scenarios, seeded data categories, test commands and results, blockers found, fixes applied, and remaining owner-approved risks. diff --git a/.agents/skills/self-hosted-release/agents/openai.yaml b/.agents/skills/self-hosted-release/agents/openai.yaml new file mode 100644 index 0000000000..821102bce0 --- /dev/null +++ b/.agents/skills/self-hosted-release/agents/openai.yaml @@ -0,0 +1,4 @@ +interface: + display_name: "Self-Hosted Release" + short_description: "Validate Appwrite self-hosted release gates" + default_prompt: "Use $self-hosted-release to validate a fresh install and upgrade path for an Appwrite self-hosted release." diff --git a/.env b/.env index 57deb8cc0a..3c7c2a21ed 100644 --- a/.env +++ b/.env @@ -1,45 +1,39 @@ _APP_ENV=development _APP_EDITION=self-hosted _APP_LOCALE=en +_APP_OPENSSL_KEY_V1=your-secret-key +_APP_RULES_FORMAT=md5 +_APP_SYSTEM_RESPONSE_FORMAT= +_APP_TRUSTED_HEADERS=x-forwarded-for +_APP_POOL_ADAPTER=stack _APP_WORKER_PER_CORE=6 +_APP_WORKERS_NUM=1 +_APP_WORKER_MAX_COROUTINES=1 +_APP_WORKER_SCREENSHOTS_ROUTER=http://appwrite _APP_COMPRESSION_ENABLED=enabled _APP_COMPRESSION_MIN_SIZE_BYTES=1024 -_APP_CONSOLE_WHITELIST_ROOT=disabled -_APP_CONSOLE_WHITELIST_EMAILS= -_APP_CONSOLE_SESSION_ALERTS=enabled -_APP_CONSOLE_WHITELIST_IPS= -_APP_CONSOLE_COUNTRIES_DENYLIST=AQ -_APP_CONSOLE_HOSTNAMES=localhost,appwrite.io,*.appwrite.io -_APP_CONSOLE_SCHEMA=appwriteio -_APP_MIGRATION_HOST=appwrite -_APP_SYSTEM_EMAIL_NAME=Appwrite -_APP_SYSTEM_EMAIL_ADDRESS=noreply@appwrite.io -_APP_SYSTEM_TEAM_EMAIL=team@appwrite.io -_APP_EMAIL_SECURITY=security@appwrite.io -_APP_EMAIL_CERTIFICATES=certificates@appwrite.io -_APP_SYSTEM_RESPONSE_FORMAT= -_APP_CUSTOM_DOMAIN_DENY_LIST= -_APP_OPTIONS_ABUSE=disabled -_APP_OPTIONS_ROUTER_PROTECTION=disabled -_APP_OPTIONS_FORCE_HTTPS=disabled -_APP_OPTIONS_ROUTER_FORCE_HTTPS=disabled -_APP_OPENSSL_KEY_V1=your-secret-key -_APP_DNS=172.16.238.100 # CoreDNS _APP_DOMAIN=appwrite.test -_APP_CONSOLE_DOMAIN=localhost -_APP_CONSOLE_TRUSTED_PROJECTS=trusted-project,another-trusted-project _APP_DOMAIN_FUNCTIONS=functions.localhost _APP_DOMAIN_SITES=sites.localhost,rebranded.localhost -_APP_DOMAIN_TARGET_CNAME=cname.localhost _APP_DOMAIN_TARGET_A=203.0.0.1 _APP_DOMAIN_TARGET_AAAA=::1 _APP_DOMAIN_TARGET_CAA=digicert.com -_APP_RULES_FORMAT=md5 -_APP_REDIS_HOST=redis -_APP_REDIS_PORT=6379 -_APP_REDIS_PASS= -_APP_REDIS_USER= -COMPOSE_PROFILES=mariadb,mongodb,postgresql +_APP_DOMAIN_TARGET_CNAME=cname.localhost +_APP_CUSTOM_DOMAIN_DENY_LIST= +_APP_DNS=172.16.238.100 +_APP_OPTIONS_FORCE_HTTPS=disabled +_APP_OPTIONS_ROUTER_FORCE_HTTPS=disabled +_APP_OPTIONS_ROUTER_PROTECTION=disabled +_APP_OPTIONS_ABUSE=disabled +_APP_CONSOLE_DOMAIN=localhost +_APP_CONSOLE_HOSTNAMES=localhost,appwrite.io,*.appwrite.io +_APP_CONSOLE_SCHEMA=appwriteio +_APP_CONSOLE_SESSION_ALERTS=enabled +_APP_CONSOLE_TRUSTED_PROJECTS=trusted-project,another-trusted-project +_APP_CONSOLE_COUNTRIES_DENYLIST=AQ +_APP_CONSOLE_WHITELIST_EMAILS= +_APP_CONSOLE_WHITELIST_IPS= +_APP_CONSOLE_WHITELIST_ROOT=disabled _APP_DB_ADAPTER=mongodb _APP_DB_HOST=mongodb _APP_DB_PORT=27017 @@ -47,18 +41,27 @@ _APP_DB_SCHEMA=appwrite _APP_DB_USER=user _APP_DB_PASS=password _APP_DB_ROOT_PASS=rootsecretpassword -_APP_DATABASE_SHARED_TABLES= _APP_DATABASE_SHARED_NAMESPACE= +_APP_DATABASE_SHARED_TABLES= _APP_DB_ADAPTER_DOCUMENTSDB=mongodb _APP_DB_HOST_DOCUMENTSDB=mongodb _APP_DB_PORT_DOCUMENTSDB=27017 _APP_DB_ADAPTER_VECTORSDB=postgresql _APP_DB_HOST_VECTORSDB=postgresql _APP_DB_PORT_VECTORSDB=5432 -_APP_EMBEDDING_MODELS=nomic-embed-text +_APP_REDIS_HOST=redis +_APP_REDIS_PORT=6379 +_APP_REDIS_USER= +_APP_REDIS_PASS= _APP_EMBEDDING_ENDPOINT='http://appwrite-embedding:3000/embed' +_APP_EMBEDDING_MODELS=nomic-embed-text _APP_EMBEDDING_TIMEOUT=30000 _APP_STORAGE_DEVICE=Local +_APP_STORAGE_LIMIT=30000000 +_APP_STORAGE_PREVIEW_LIMIT=20000000 +_APP_STORAGE_ANTIVIRUS=disabled +_APP_STORAGE_ANTIVIRUS_HOST=clamav +_APP_STORAGE_ANTIVIRUS_PORT=3310 _APP_STORAGE_S3_ACCESS_KEY= _APP_STORAGE_S3_SECRET= _APP_STORAGE_S3_REGION=us-east-1 @@ -80,9 +83,11 @@ _APP_STORAGE_WASABI_ACCESS_KEY= _APP_STORAGE_WASABI_SECRET= _APP_STORAGE_WASABI_REGION=eu-central-1 _APP_STORAGE_WASABI_BUCKET= -_APP_STORAGE_ANTIVIRUS=disabled -_APP_STORAGE_ANTIVIRUS_HOST=clamav -_APP_STORAGE_ANTIVIRUS_PORT=3310 +_APP_SYSTEM_EMAIL_NAME=Appwrite +_APP_SYSTEM_EMAIL_ADDRESS=noreply@appwrite.io +_APP_SYSTEM_TEAM_EMAIL=team@appwrite.io +_APP_EMAIL_SECURITY=security@appwrite.io +_APP_EMAIL_CERTIFICATES=certificates@appwrite.io _APP_SMTP_HOST=maildev _APP_SMTP_PORT=1025 _APP_SMTP_SECURE= @@ -91,65 +96,58 @@ _APP_SMTP_PASSWORD=password _APP_SMS_PROVIDER=sms://username:password@mock _APP_SMS_FROM=+123456789 _APP_SMS_PROJECTS_DENY_LIST= -_APP_STORAGE_LIMIT=30000000 -_APP_STORAGE_PREVIEW_LIMIT=20000000 +_APP_MESSAGE_EMAIL_TEST_DSN= +_APP_MESSAGE_PUSH_TEST_DSN= +_APP_MESSAGE_SMS_TEST_DSN= _APP_COMPUTE_SIZE_LIMIT=30000000 -_APP_FUNCTIONS_TIMEOUT=900 -_APP_SITES_TIMEOUT=30 _APP_COMPUTE_BUILD_TIMEOUT=900 _APP_COMPUTE_CPUS=8 _APP_COMPUTE_MEMORY=8192 _APP_COMPUTE_INACTIVE_THRESHOLD=600 _APP_COMPUTE_MAINTENANCE_INTERVAL=600 _APP_COMPUTE_RUNTIMES_NETWORK=runtimes -_APP_EXECUTOR_SECRET=your-secret-key -_APP_EXECUTOR_HOST=http://exc1/v1 -_APP_BROWSER_HOST=http://appwrite-browser:3000/v1 _APP_FUNCTIONS_RUNTIMES=node-22 +_APP_FUNCTIONS_TIMEOUT=900 +_APP_FUNCTIONS_CREATION_ABUSE_LIMIT=5000 _APP_SITES_RUNTIMES=static-1,node-22 +_APP_SITES_TIMEOUT=30 +_APP_EXECUTOR_HOST=http://exc1/v1 +_APP_EXECUTOR_SECRET=your-secret-key +_APP_EXECUTOR_IMAGES=openruntimes/node:v5-22,openruntimes/static:v5-1 +_APP_BROWSER_HOST=http://appwrite-browser:3000/v1 +_APP_DOCKER_HUB_USERNAME= +_APP_DOCKER_HUB_PASSWORD= _APP_MAINTENANCE_INTERVAL=86400 _APP_MAINTENANCE_START_TIME=12:00 -_APP_MAINTENANCE_RETENTION_CACHE=2592000 -_APP_MAINTENANCE_RETENTION_EXECUTION=1209600 _APP_MAINTENANCE_RETENTION_ABUSE=86400 _APP_MAINTENANCE_RETENTION_AUDIT=1209600 _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE=15778800 -_APP_USAGE_AGGREGATION_INTERVAL=30 -_APP_STATS_RESOURCES_INTERVAL=30 -_APP_MAINTENANCE_RETENTION_USAGE_HOURLY=8640000 +_APP_MAINTENANCE_RETENTION_CACHE=2592000 +_APP_MAINTENANCE_RETENTION_EXECUTION=1209600 _APP_MAINTENANCE_RETENTION_SCHEDULES=86400 -_APP_INTERVAL_DOMAIN_VERIFICATION=120 +_APP_MAINTENANCE_RETENTION_USAGE_HOURLY=8640000 _APP_INTERVAL_CLEANUP_STALE_EXECUTIONS=300 +_APP_INTERVAL_DOMAIN_VERIFICATION=120 +_APP_USAGE_AGGREGATION_INTERVAL=30 _APP_USAGE_STATS=enabled -_APP_LOGGING_CONFIG= -_APP_LOGGING_CONFIG_REALTIME= +_APP_STATS_RESOURCES_INTERVAL=30 _APP_GRAPHQL_INTROSPECTION=enabled _APP_GRAPHQL_MAX_BATCH_SIZE=10 _APP_GRAPHQL_MAX_COMPLEXITY=250 _APP_GRAPHQL_MAX_DEPTH=4 -_APP_DOCKER_HUB_USERNAME= -_APP_DOCKER_HUB_PASSWORD= -_APP_VCS_GITHUB_APP_NAME= -_APP_VCS_GITHUB_PRIVATE_KEY=disabled -_APP_VCS_GITHUB_APP_ID= -_APP_VCS_GITHUB_CLIENT_ID= -_APP_VCS_GITHUB_CLIENT_SECRET= -_APP_VCS_GITHUB_WEBHOOK_SECRET= +_APP_ASSISTANT_OPENAI_API_KEY= +_APP_LOGGING_CONFIG= +_APP_LOGGING_CONFIG_REALTIME= +_APP_MIGRATION_HOST=appwrite _APP_MIGRATIONS_FIREBASE_CLIENT_ID= _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET= -_APP_ASSISTANT_OPENAI_API_KEY= -_APP_MESSAGE_SMS_TEST_DSN= -_APP_MESSAGE_EMAIL_TEST_DSN= -_APP_MESSAGE_PUSH_TEST_DSN= -_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 _APP_PROJECT_REGIONS=default -_APP_FUNCTIONS_CREATION_ABUSE_LIMIT=5000 -_APP_STATS_USAGE_DUAL_WRITING_DBS=database_db_main -_APP_TRUSTED_HEADERS=x-forwarded-for -_APP_POOL_ADAPTER=stack -_APP_WORKER_SCREENSHOTS_ROUTER=http://appwrite +_APP_VCS_GITHUB_APP_ID= +_APP_VCS_GITHUB_APP_NAME= +_APP_VCS_GITHUB_CLIENT_ID= +_APP_VCS_GITHUB_CLIENT_SECRET= +_APP_VCS_GITHUB_PRIVATE_KEY=disabled +_APP_VCS_GITHUB_WEBHOOK_SECRET= +_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 _TESTS_OAUTH2_GITHUB_CLIENT_ID= _TESTS_OAUTH2_GITHUB_CLIENT_SECRET= - -_APP_WORKERS_NUM=1 -_APP_WORKER_MAX_COROUTINES=1 diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index d04baa5e11..a7274d6e2b 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -1126,7 +1126,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); exec mongod --replSet rs0 --bind_ip_all --auth --keyFile "$$KEYFILE_PATH" healthcheck: test: | - mongosh -u root -p "$$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin --quiet --eval "rs.status().ok" | grep -q 1 + mongosh -u root -p "$$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin --quiet --eval "const hello = db.adminCommand({hello: 1}); if (!hello.isWritablePrimary) { quit(1); }" interval: 10s timeout: 10s retries: 10 diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 29182f01d2..05e31ece94 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -16,7 +16,8 @@ services: ME_CONFIG_BASICAUTH_USERNAME: ${_APP_DB_USER} ME_CONFIG_BASICAUTH_PASSWORD: ${_APP_DB_PASS} depends_on: - - mongodb + mongodb: + condition: service_healthy adminer: image: adminer diff --git a/docker-compose.yml b/docker-compose.yml index 4341698653..e5b27fb5b1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1253,7 +1253,6 @@ services: restart: unless-stopped networks: - appwrite - - runtimes volumes: - /var/run/docker.sock:/var/run/docker.sock - appwrite-builds:/storage/builds:rw @@ -1265,7 +1264,7 @@ services: environment: - OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost - OPR_EXECUTOR_CONNECTION_BUILD_CACHE_STORAGE= - - OPR_EXECUTOR_IMAGE_PULL=enabled + - OPR_EXECUTOR_IMAGES=$_APP_EXECUTOR_IMAGES - OPR_EXECUTOR_INACTIVE_TRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL=$_APP_COMPUTE_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK=$_APP_COMPUTE_RUNTIMES_NETWORK @@ -1352,13 +1351,20 @@ services: healthcheck: test: | bash -c " - if mongosh -u root -p ${_APP_DB_ROOT_PASS} --authenticationDatabase admin --quiet --eval 'rs.status().ok' 2>/dev/null | grep -q 1; then - exit 0 - else - mongosh -u root -p ${_APP_DB_ROOT_PASS} --authenticationDatabase admin --quiet --eval \" - rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'appwrite-mongodb:27017'}]}) - \" 2>/dev/null || exit 1 - fi + mongosh -u root -p ${_APP_DB_ROOT_PASS} --authenticationDatabase admin --quiet --eval \" + try { + const hello = db.adminCommand({hello: 1}); + if (hello.isWritablePrimary) { + quit(0); + } + } catch (e) {} + + try { + rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'appwrite-mongodb:27017'}]}); + } catch (e) {} + + quit(1); + \" 2>/dev/null " interval: 10s timeout: 10s From 98c4845b69f3f41ba2be834436ce3b42fa8a9d7b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:48:11 +0530 Subject: [PATCH 70/80] Fix installer compose release wiring --- app/views/install/compose.phtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index a7274d6e2b..b2061745b7 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -194,7 +194,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); appwrite-console: <<: *x-logging container_name: appwrite-console - image: /console:7.8.26 + image: /console:8 restart: unless-stopped networks: - appwrite @@ -1003,7 +1003,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); environment: - OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost - OPR_EXECUTOR_CONNECTION_BUILD_CACHE_STORAGE= - - OPR_EXECUTOR_IMAGES= + - OPR_EXECUTOR_IMAGES= - OPR_EXECUTOR_INACTIVE_TRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL=$_APP_COMPUTE_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK=$_APP_COMPUTE_RUNTIMES_NETWORK From 83ba5ee61eda363f82fc49c454142e41cae94627 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:49:56 +0530 Subject: [PATCH 71/80] Update console image to 8.7.5 --- app/views/install/compose.phtml | 2 +- docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index b2061745b7..220dac8172 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -194,7 +194,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); appwrite-console: <<: *x-logging container_name: appwrite-console - image: /console:8 + image: /console:8.7.5 restart: unless-stopped networks: - appwrite diff --git a/docker-compose.yml b/docker-compose.yml index e5b27fb5b1..7c8ca3a7c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -255,7 +255,7 @@ services: appwrite-console: <<: *x-logging container_name: appwrite-console - image: appwrite/console:8 + image: appwrite/console:8.7.5 restart: unless-stopped networks: - appwrite From bafa5380038b01bb75f3c38c2de289efa39995c0 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:52:35 +0530 Subject: [PATCH 72/80] Update README SDK lists --- README-CN.md | 22 +++++++++++++--------- README.md | 17 ++++++++++------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README-CN.md b/README-CN.md index 212b5bb08d..8920e47aec 100644 --- a/README-CN.md +++ b/README-CN.md @@ -161,6 +161,8 @@ docker run -it --rm ` | | [PHP 快速开始](https://appwrite.io/docs/quick-starts/php) | | | [Kotlin 快速开始](https://appwrite.io/docs/quick-starts/kotlin) | | | [Swift 快速开始](https://appwrite.io/docs/quick-starts/swift) | +| | [Go 快速开始](https://appwrite.io/docs/quick-starts/go) | +| | [Rust 快速开始](https://appwrite.io/docs/quick-starts/rust) | ### 软件服务 @@ -185,20 +187,22 @@ docker run -it --rm ` - :white_check_mark:   [Web](https://github.com/appwrite/sdk-for-web) (由 Appwrite 团队维护) - :white_check_mark:   [Flutter](https://github.com/appwrite/sdk-for-flutter) (由 Appwrite 团队维护) -- :white_check_mark:   [Apple](https://github.com/appwrite/sdk-for-apple) - **公测** (由 Appwrite 团队维护) +- :white_check_mark:   [Apple](https://github.com/appwrite/sdk-for-apple) (由 Appwrite 团队维护) - :white_check_mark:   [Android](https://github.com/appwrite/sdk-for-android) (由 Appwrite 团队维护) +- :white_check_mark:   [React Native](https://github.com/appwrite/sdk-for-react-native) - **公测** (由 Appwrite 团队维护) #### 服务器 -- :white_check_mark:   [NodeJS](https://github.com/appwrite/sdk-for-node) (由 Appwrite 团队维护) -- :white_check_mark:   [PHP](https://github.com/appwrite/sdk-for-php) (由 Appwrite 团队维护) -- :white_check_mark:   [Dart](https://github.com/appwrite/sdk-for-dart) - (由 Appwrite 团队维护) -- :white_check_mark:   [Deno](https://github.com/appwrite/sdk-for-deno) - **公测** (由 Appwrite 团队维护) -- :white_check_mark:   [Ruby](https://github.com/appwrite/sdk-for-ruby) (由 Appwrite 团队维护) +- :white_check_mark:   [Node.js](https://github.com/appwrite/sdk-for-node) (由 Appwrite 团队维护) - :white_check_mark:   [Python](https://github.com/appwrite/sdk-for-python) (由 Appwrite 团队维护) -- :white_check_mark:   [Kotlin](https://github.com/appwrite/sdk-for-kotlin) - **公测** (由 Appwrite 团队维护) -- :white_check_mark:   [Apple](https://github.com/appwrite/sdk-for-apple) - **公测** (由 Appwrite 团队维护) -- :white_check_mark:   [.NET](https://github.com/appwrite/sdk-for-dotnet) - **公测** (由 Appwrite 团队维护) +- :white_check_mark:   [Dart](https://github.com/appwrite/sdk-for-dart) (由 Appwrite 团队维护) +- :white_check_mark:   [PHP](https://github.com/appwrite/sdk-for-php) (由 Appwrite 团队维护) +- :white_check_mark:   [Ruby](https://github.com/appwrite/sdk-for-ruby) (由 Appwrite 团队维护) +- :white_check_mark:   [.NET](https://github.com/appwrite/sdk-for-dotnet) (由 Appwrite 团队维护) +- :white_check_mark:   [Go](https://github.com/appwrite/sdk-for-go) (由 Appwrite 团队维护) +- :white_check_mark:   [Swift](https://github.com/appwrite/sdk-for-swift) (由 Appwrite 团队维护) +- :white_check_mark:   [Kotlin](https://github.com/appwrite/sdk-for-kotlin) (由 Appwrite 团队维护) +- :white_check_mark:   [Rust](https://github.com/appwrite/sdk-for-rust) (由 Appwrite 团队维护) #### 开发者社区 diff --git a/README.md b/README.md index 88d527f060..8bcad82564 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,8 @@ Getting started with Appwrite is as easy as creating a new project, choosing you | | [Quick start for PHP](https://appwrite.io/docs/quick-starts/php) | | | [Quick start for Kotlin](https://appwrite.io/docs/quick-starts/kotlin) | | | [Quick start for Swift](https://appwrite.io/docs/quick-starts/swift) | +| | [Quick start for Go](https://appwrite.io/docs/quick-starts/go) | +| | [Quick start for Rust](https://appwrite.io/docs/quick-starts/rust) | ### SDKs @@ -181,15 +183,16 @@ Below is a list of currently supported platforms and languages. If you would lik #### Server -- :white_check_mark:   [NodeJS](https://github.com/appwrite/sdk-for-node) -- :white_check_mark:   [PHP](https://github.com/appwrite/sdk-for-php) -- :white_check_mark:   [Dart](https://github.com/appwrite/sdk-for-dart) -- :white_check_mark:   [Deno](https://github.com/appwrite/sdk-for-deno) -- :white_check_mark:   [Ruby](https://github.com/appwrite/sdk-for-ruby) +- :white_check_mark:   [Node.js](https://github.com/appwrite/sdk-for-node) - :white_check_mark:   [Python](https://github.com/appwrite/sdk-for-python) -- :white_check_mark:   [Kotlin](https://github.com/appwrite/sdk-for-kotlin) -- :white_check_mark:   [Swift](https://github.com/appwrite/sdk-for-swift) +- :white_check_mark:   [Dart](https://github.com/appwrite/sdk-for-dart) +- :white_check_mark:   [PHP](https://github.com/appwrite/sdk-for-php) +- :white_check_mark:   [Ruby](https://github.com/appwrite/sdk-for-ruby) - :white_check_mark:   [.NET](https://github.com/appwrite/sdk-for-dotnet) +- :white_check_mark:   [Go](https://github.com/appwrite/sdk-for-go) +- :white_check_mark:   [Swift](https://github.com/appwrite/sdk-for-swift) +- :white_check_mark:   [Kotlin](https://github.com/appwrite/sdk-for-kotlin) +- :white_check_mark:   [Rust](https://github.com/appwrite/sdk-for-rust) Looking for more SDKs? - Help us by contributing a pull request to our [SDK Generator](https://github.com/appwrite/sdk-generator)! From fb3858c8c099fce205ad96e3e27f7f1795f4a7e1 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:58:50 +0530 Subject: [PATCH 73/80] Fix installer database and secret key UI --- app/views/install/compose.phtml | 2 +- app/views/install/installer.phtml | 2 +- app/views/install/installer/js/modules/context.js | 4 ++-- app/views/install/installer/js/steps.js | 4 ++-- app/views/install/installer/templates/steps/step-1.phtml | 2 +- app/views/install/installer/templates/steps/step-2.phtml | 2 +- app/views/install/installer/templates/steps/step-4.phtml | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 220dac8172..7170439c1e 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -14,7 +14,7 @@ $image = $this->getParam('image', ''); $enableAssistant = $this->getParam('enableAssistant', false); $executorImages = $this->getParam('executorImages', ''); $dbService = $this->getParam('database', 'mongodb'); -$allowedDbServices = ['mariadb', 'mongodb']; +$allowedDbServices = ['mariadb', 'mongodb', 'postgresql']; if (!\in_array($dbService, $allowedDbServices, true)) { $dbService = 'mongodb'; } diff --git a/app/views/install/installer.phtml b/app/views/install/installer.phtml index d33838c8c6..7130a16f30 100644 --- a/app/views/install/installer.phtml +++ b/app/views/install/installer.phtml @@ -9,7 +9,7 @@ $defaultAppDomain = ($defaultAppDomain === 'traefik') ? 'localhost' : $defaultAp $defaultEmailCertificates = $defaultEmailCertificates ?? ''; $defaultDatabase = $vars['_APP_DB_ADAPTER']['default'] ?? 'mongodb'; $lockedDatabase = $isUpgrade && empty($lockedDatabase) ? $defaultDatabase : $lockedDatabase; -$enabledDatabases = $enabledDatabases ?? ['mongodb', 'mariadb', 'postgresql']; +$enabledDatabases = $enabledDatabases ?? ['mongodb', 'mariadb']; $isLocalInstall = $isLocalInstall ?? false; diff --git a/app/views/install/installer/js/modules/context.js b/app/views/install/installer/js/modules/context.js index 6f215da899..36b85a3843 100644 --- a/app/views/install/installer/js/modules/context.js +++ b/app/views/install/installer/js/modules/context.js @@ -4,8 +4,8 @@ const getLockedDatabase = () => getBodyDataset().lockedDatabase || ''; const getEnabledDatabases = () => { const raw = getBodyDataset().enabledDatabases; - if (!raw) return ['mongodb', 'mariadb', 'postgresql']; - try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb', 'postgresql']; } + if (!raw) return ['mongodb', 'mariadb']; + try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb']; } }; const STEP_IDS = Object.freeze({ diff --git a/app/views/install/installer/js/steps.js b/app/views/install/installer/js/steps.js index b34389b561..d5ee3dcb60 100644 --- a/app/views/install/installer/js/steps.js +++ b/app/views/install/installer/js/steps.js @@ -432,11 +432,11 @@ const secretKey = root?.querySelector('#secret-key'); const secretValue = secretKey?.value.trim() || ''; if (!secretKey || !secretValue) { - setFieldError?.(secretKey, 'Please enter or generate a secret API key'); + setFieldError?.(secretKey, 'Please enter or generate a secret key'); return false; } if (secretValue.length > 64) { - setFieldError?.(secretKey, 'Secret API key must be 1-64 characters'); + setFieldError?.(secretKey, 'Secret key must be 1-64 characters'); return false; } } diff --git a/app/views/install/installer/templates/steps/step-1.phtml b/app/views/install/installer/templates/steps/step-1.phtml index 8f4a726158..c02a669c63 100644 --- a/app/views/install/installer/templates/steps/step-1.phtml +++ b/app/views/install/installer/templates/steps/step-1.phtml @@ -7,7 +7,7 @@ $defaultHttpsPort = $defaultHttpsPort ?? '443'; $defaultEmailCertificates = $defaultEmailCertificates ?? ''; $defaultAssistantOpenAIKey = $defaultAssistantOpenAIKey ?? ''; $defaultDatabase = $defaultDatabase ?? 'mongodb'; -$enabledDatabases = $enabledDatabases ?? ['mongodb', 'mariadb', 'postgresql']; +$enabledDatabases = $enabledDatabases ?? ['mongodb', 'mariadb']; $selectedDatabase = $lockedDatabase ?: $defaultDatabase; $isDatabaseLocked = !empty($lockedDatabase); $mongoEnabled = in_array('mongodb', $enabledDatabases, true); diff --git a/app/views/install/installer/templates/steps/step-2.phtml b/app/views/install/installer/templates/steps/step-2.phtml index 9b7eed11b1..f0610e6295 100644 --- a/app/views/install/installer/templates/steps/step-2.phtml +++ b/app/views/install/installer/templates/steps/step-2.phtml @@ -27,7 +27,7 @@ $secretKeyValue = htmlspecialchars((string) $defaultSecretKey, ENT_QUOTES, 'UTF-

- +
-
Secret API key
+
Secret key
From f477f4e22109b4128b46dfb4c301a7e946b39ebd Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:59:24 +0530 Subject: [PATCH 74/80] Simplify installer template defaults --- app/views/install/installer.phtml | 16 ++++++++-------- .../installer/templates/steps/step-1.phtml | 18 +++++++++--------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/views/install/installer.phtml b/app/views/install/installer.phtml index 7130a16f30..d5e4383f48 100644 --- a/app/views/install/installer.phtml +++ b/app/views/install/installer.phtml @@ -1,16 +1,16 @@ Date: Wed, 24 Jun 2026 13:08:30 +0530 Subject: [PATCH 75/80] Restore PostgreSQL installer defaults --- app/views/install/installer.phtml | 2 +- app/views/install/installer/js/modules/context.js | 4 ++-- app/views/install/installer/templates/steps/step-1.phtml | 2 +- src/Appwrite/Platform/Installer/Runtime/Config.php | 2 +- src/Appwrite/Platform/Tasks/Install.php | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/install/installer.phtml b/app/views/install/installer.phtml index d5e4383f48..2de92fd3f3 100644 --- a/app/views/install/installer.phtml +++ b/app/views/install/installer.phtml @@ -9,7 +9,7 @@ $defaultAppDomain = ($defaultAppDomain === 'traefik') ? 'localhost' : $defaultAp $defaultEmailCertificates ??= ''; $defaultDatabase = $vars['_APP_DB_ADAPTER']['default'] ?? 'mongodb'; $lockedDatabase = $isUpgrade && empty($lockedDatabase) ? $defaultDatabase : $lockedDatabase; -$enabledDatabases ??= ['mongodb', 'mariadb']; +$enabledDatabases ??= ['mongodb', 'mariadb', 'postgresql']; $isLocalInstall ??= false; diff --git a/app/views/install/installer/js/modules/context.js b/app/views/install/installer/js/modules/context.js index 36b85a3843..6f215da899 100644 --- a/app/views/install/installer/js/modules/context.js +++ b/app/views/install/installer/js/modules/context.js @@ -4,8 +4,8 @@ const getLockedDatabase = () => getBodyDataset().lockedDatabase || ''; const getEnabledDatabases = () => { const raw = getBodyDataset().enabledDatabases; - if (!raw) return ['mongodb', 'mariadb']; - try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb']; } + if (!raw) return ['mongodb', 'mariadb', 'postgresql']; + try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb', 'postgresql']; } }; const STEP_IDS = Object.freeze({ diff --git a/app/views/install/installer/templates/steps/step-1.phtml b/app/views/install/installer/templates/steps/step-1.phtml index e7d356745b..cfd02819bb 100644 --- a/app/views/install/installer/templates/steps/step-1.phtml +++ b/app/views/install/installer/templates/steps/step-1.phtml @@ -7,7 +7,7 @@ $defaultHttpsPort ??= '443'; $defaultEmailCertificates ??= ''; $defaultAssistantOpenAIKey ??= ''; $defaultDatabase ??= 'mongodb'; -$enabledDatabases ??= ['mongodb', 'mariadb']; +$enabledDatabases ??= ['mongodb', 'mariadb', 'postgresql']; $selectedDatabase = $lockedDatabase ?: $defaultDatabase; $isDatabaseLocked = !empty($lockedDatabase); $mongoEnabled = in_array('mongodb', $enabledDatabases, true); diff --git a/src/Appwrite/Platform/Installer/Runtime/Config.php b/src/Appwrite/Platform/Installer/Runtime/Config.php index 6142e47152..53479a00f0 100644 --- a/src/Appwrite/Platform/Installer/Runtime/Config.php +++ b/src/Appwrite/Platform/Installer/Runtime/Config.php @@ -27,7 +27,7 @@ final class Config private bool $isLocal = false; private ?string $hostPath = null; private ?string $lockedDatabase = null; - private array $enabledDatabases = ['mongodb', 'mariadb']; + private array $enabledDatabases = ['mongodb', 'mariadb', 'postgresql']; private array $vars = []; public function __construct(array $values = []) diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index 75f3c5bf18..70a6135901 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -199,7 +199,7 @@ class Install extends Action } $installerConfig = $this->readInstallerConfig(); - $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb']; + $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb', 'postgresql']; if (!in_array($database, $enabledDatabases, true)) { Console::error("Database '{$database}' is not available. Available options: " . implode(', ', $enabledDatabases)); Console::exit(1); @@ -331,7 +331,7 @@ class Install extends Action $state->clearStaleLock(); $installerConfig = $this->readInstallerConfig(); - $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb']; + $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb', 'postgresql']; $this->setInstallerConfig([ 'defaultHttpPort' => $defaultHttpPort, From 67af7528199f4f96ca6df6f10beb32db3e11f243 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 13:16:45 +0530 Subject: [PATCH 76/80] Update installer config default database tests --- .../Platform/Modules/Installer/Runtime/ConfigTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php b/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php index 7f0faafcbc..a539ae9b12 100644 --- a/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php +++ b/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php @@ -22,7 +22,7 @@ final class ConfigTest extends TestCase $this->assertFalse($config->isLocal()); $this->assertNull($config->getHostPath()); $this->assertNull($config->getLockedDatabase()); - $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); $this->assertEmpty($config->getVars()); } @@ -439,7 +439,7 @@ final class ConfigTest extends TestCase $this->assertFalse($array['isLocal']); $this->assertNull($array['hostPath']); $this->assertNull($array['lockedDatabase']); - $this->assertEquals(['mongodb', 'mariadb'], $array['enabledDatabases']); + $this->assertEquals(['mongodb', 'mariadb', 'postgresql'], $array['enabledDatabases']); } public function testMultipleApplyCallsAccumulate(): void @@ -494,10 +494,10 @@ final class ConfigTest extends TestCase public function testDefaultEnabledDatabases(): void { $config = new Config(); - $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); $this->assertTrue($config->isDatabaseEnabled('mongodb')); $this->assertTrue($config->isDatabaseEnabled('mariadb')); - $this->assertFalse($config->isDatabaseEnabled('postgresql')); + $this->assertTrue($config->isDatabaseEnabled('postgresql')); } public function testSetEnabledDatabases(): void @@ -535,7 +535,7 @@ final class ConfigTest extends TestCase { $config = new Config(); $config->apply(['enabledDatabases' => 'mongodb']); - $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); } public function testEnabledDatabasesInToArray(): void From 6a6741137c6dd7804207f9505e9a7dfb52dc7c9c Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 13:22:45 +0530 Subject: [PATCH 77/80] Keep PostgreSQL disabled by default in installer --- app/views/install/installer.phtml | 2 +- app/views/install/installer/js/modules/context.js | 4 ++-- .../install/installer/templates/steps/step-1.phtml | 2 +- src/Appwrite/Platform/Installer/Runtime/Config.php | 2 +- src/Appwrite/Platform/Tasks/Install.php | 13 +++++++++---- .../Modules/Installer/Runtime/ConfigTest.php | 10 +++++----- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/views/install/installer.phtml b/app/views/install/installer.phtml index 2de92fd3f3..d5e4383f48 100644 --- a/app/views/install/installer.phtml +++ b/app/views/install/installer.phtml @@ -9,7 +9,7 @@ $defaultAppDomain = ($defaultAppDomain === 'traefik') ? 'localhost' : $defaultAp $defaultEmailCertificates ??= ''; $defaultDatabase = $vars['_APP_DB_ADAPTER']['default'] ?? 'mongodb'; $lockedDatabase = $isUpgrade && empty($lockedDatabase) ? $defaultDatabase : $lockedDatabase; -$enabledDatabases ??= ['mongodb', 'mariadb', 'postgresql']; +$enabledDatabases ??= ['mongodb', 'mariadb']; $isLocalInstall ??= false; diff --git a/app/views/install/installer/js/modules/context.js b/app/views/install/installer/js/modules/context.js index 6f215da899..36b85a3843 100644 --- a/app/views/install/installer/js/modules/context.js +++ b/app/views/install/installer/js/modules/context.js @@ -4,8 +4,8 @@ const getLockedDatabase = () => getBodyDataset().lockedDatabase || ''; const getEnabledDatabases = () => { const raw = getBodyDataset().enabledDatabases; - if (!raw) return ['mongodb', 'mariadb', 'postgresql']; - try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb', 'postgresql']; } + if (!raw) return ['mongodb', 'mariadb']; + try { return JSON.parse(raw); } catch (e) { return ['mongodb', 'mariadb']; } }; const STEP_IDS = Object.freeze({ diff --git a/app/views/install/installer/templates/steps/step-1.phtml b/app/views/install/installer/templates/steps/step-1.phtml index cfd02819bb..e7d356745b 100644 --- a/app/views/install/installer/templates/steps/step-1.phtml +++ b/app/views/install/installer/templates/steps/step-1.phtml @@ -7,7 +7,7 @@ $defaultHttpsPort ??= '443'; $defaultEmailCertificates ??= ''; $defaultAssistantOpenAIKey ??= ''; $defaultDatabase ??= 'mongodb'; -$enabledDatabases ??= ['mongodb', 'mariadb', 'postgresql']; +$enabledDatabases ??= ['mongodb', 'mariadb']; $selectedDatabase = $lockedDatabase ?: $defaultDatabase; $isDatabaseLocked = !empty($lockedDatabase); $mongoEnabled = in_array('mongodb', $enabledDatabases, true); diff --git a/src/Appwrite/Platform/Installer/Runtime/Config.php b/src/Appwrite/Platform/Installer/Runtime/Config.php index 53479a00f0..6142e47152 100644 --- a/src/Appwrite/Platform/Installer/Runtime/Config.php +++ b/src/Appwrite/Platform/Installer/Runtime/Config.php @@ -27,7 +27,7 @@ final class Config private bool $isLocal = false; private ?string $hostPath = null; private ?string $lockedDatabase = null; - private array $enabledDatabases = ['mongodb', 'mariadb', 'postgresql']; + private array $enabledDatabases = ['mongodb', 'mariadb']; private array $vars = []; public function __construct(array $values = []) diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index 70a6135901..adaedd9427 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -102,6 +102,8 @@ class Install extends Action $envFileExists = file_exists($this->path . '/' . $this->getEnvFileName()); $existingInstallation = $data !== '' || $envFileExists; + $existingDatabase = null; + if ($existingInstallation) { $time = \time(); $composeFileName = $this->getComposeFileName(); @@ -171,7 +173,6 @@ class Install extends Action // Detect database type from existing installation. // 1.9.0+ installs have _APP_DB_ADAPTER; pre-1.9.0 installs // can be detected by the DB service name or _APP_DB_HOST. - $existingDatabase = null; foreach ($compose->getServices() as $service) { $svcEnv = $service->getEnvironment()->list(); if (isset($svcEnv['_APP_DB_ADAPTER'])) { @@ -199,8 +200,9 @@ class Install extends Action } $installerConfig = $this->readInstallerConfig(); - $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb', 'postgresql']; - if (!in_array($database, $enabledDatabases, true)) { + $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb']; + $isExistingDatabase = $isUpgrade && $existingDatabase !== null && $database === $existingDatabase; + if (!in_array($database, $enabledDatabases, true) && !$isExistingDatabase) { Console::error("Database '{$database}' is not available. Available options: " . implode(', ', $enabledDatabases)); Console::exit(1); } @@ -331,7 +333,10 @@ class Install extends Action $state->clearStaleLock(); $installerConfig = $this->readInstallerConfig(); - $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb', 'postgresql']; + $enabledDatabases = $installerConfig['enabledDatabases'] ?? ['mongodb', 'mariadb']; + if ($isUpgrade && $lockedDatabase !== null && !in_array($lockedDatabase, $enabledDatabases, true)) { + $enabledDatabases[] = $lockedDatabase; + } $this->setInstallerConfig([ 'defaultHttpPort' => $defaultHttpPort, diff --git a/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php b/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php index a539ae9b12..7f0faafcbc 100644 --- a/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php +++ b/tests/unit/Platform/Modules/Installer/Runtime/ConfigTest.php @@ -22,7 +22,7 @@ final class ConfigTest extends TestCase $this->assertFalse($config->isLocal()); $this->assertNull($config->getHostPath()); $this->assertNull($config->getLockedDatabase()); - $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); $this->assertEmpty($config->getVars()); } @@ -439,7 +439,7 @@ final class ConfigTest extends TestCase $this->assertFalse($array['isLocal']); $this->assertNull($array['hostPath']); $this->assertNull($array['lockedDatabase']); - $this->assertEquals(['mongodb', 'mariadb', 'postgresql'], $array['enabledDatabases']); + $this->assertEquals(['mongodb', 'mariadb'], $array['enabledDatabases']); } public function testMultipleApplyCallsAccumulate(): void @@ -494,10 +494,10 @@ final class ConfigTest extends TestCase public function testDefaultEnabledDatabases(): void { $config = new Config(); - $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); $this->assertTrue($config->isDatabaseEnabled('mongodb')); $this->assertTrue($config->isDatabaseEnabled('mariadb')); - $this->assertTrue($config->isDatabaseEnabled('postgresql')); + $this->assertFalse($config->isDatabaseEnabled('postgresql')); } public function testSetEnabledDatabases(): void @@ -535,7 +535,7 @@ final class ConfigTest extends TestCase { $config = new Config(); $config->apply(['enabledDatabases' => 'mongodb']); - $this->assertSame(['mongodb', 'mariadb', 'postgresql'], $config->getEnabledDatabases()); + $this->assertSame(['mongodb', 'mariadb'], $config->getEnabledDatabases()); } public function testEnabledDatabasesInToArray(): void From 9df27901d640ea47144cc606591244c26b207437 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 14:03:23 +0530 Subject: [PATCH 78/80] Disable PostgreSQL for fresh CLI installs --- src/Appwrite/Platform/Tasks/Install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index adaedd9427..8c9b86dedb 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -57,7 +57,7 @@ class Install extends Action ->param('image', 'appwrite', new Text(0), 'Main appwrite docker image', true) ->param('interactive', 'Y', new Text(1), 'Run an interactive session', true) ->param('no-start', false, new Boolean(true), 'Run an interactive session', true) - ->param('database', 'mongodb', new WhiteList(['mongodb', 'mariadb', 'postgresql']), 'Database to use (mongodb|mariadb|postgresql)', true) + ->param('database', 'mongodb', new WhiteList(['mongodb', 'mariadb']), 'Database to use (mongodb|mariadb)', true) ->callback($this->action(...)); } From 8d711e1db6e789da7d6e8bba08a1973df5545949 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:05:36 +0530 Subject: [PATCH 79/80] Enable Open Runtimes NFT pruning config --- .env | 1 + app/config/variables.php | 9 +++++++++ app/views/install/compose.phtml | 4 +++- docker-compose.yml | 4 +++- .../Platform/Modules/Functions/Workers/Builds.php | 2 ++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 3c7c2a21ed..35f5f135bc 100644 --- a/.env +++ b/.env @@ -106,6 +106,7 @@ _APP_COMPUTE_MEMORY=8192 _APP_COMPUTE_INACTIVE_THRESHOLD=600 _APP_COMPUTE_MAINTENANCE_INTERVAL=600 _APP_COMPUTE_RUNTIMES_NETWORK=runtimes +_APP_OPEN_RUNTIMES_NFT=enabled _APP_FUNCTIONS_RUNTIMES=node-22 _APP_FUNCTIONS_TIMEOUT=900 _APP_FUNCTIONS_CREATION_ABUSE_LIMIT=5000 diff --git a/app/config/variables.php b/app/config/variables.php index 90df9b4518..b77d841aaf 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1070,6 +1070,15 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_OPEN_RUNTIMES_NFT', + 'description' => 'Enables dependency tracing for Open Runtimes build artifacts. When enabled, supported SSR site builds prune unused node_modules files before packaging the build output.', + 'introduction' => '1.9.0', + 'default' => 'enabled', + 'required' => false, + 'question' => '', + 'filter' => '' + ], [ 'name' => '_APP_DOCKER_HUB_USERNAME', 'description' => 'The username for hub.docker.com. This variable is used to pull images from hub.docker.com.', diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml index 7170439c1e..4f910be9e1 100644 --- a/app/views/install/compose.phtml +++ b/app/views/install/compose.phtml @@ -457,6 +457,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - _APP_COMPUTE_BUILD_TIMEOUT - _APP_COMPUTE_CPUS - _APP_COMPUTE_MEMORY + - _APP_OPEN_RUNTIMES_NFT - _APP_COMPUTE_SIZE_LIMIT - _APP_OPTIONS_FORCE_HTTPS - _APP_OPTIONS_ROUTER_FORCE_HTTPS @@ -1004,7 +1005,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost - OPR_EXECUTOR_CONNECTION_BUILD_CACHE_STORAGE= - OPR_EXECUTOR_IMAGES= - - OPR_EXECUTOR_INACTIVE_TRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD + - OPR_EXECUTOR_INACTIVE_THRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL=$_APP_COMPUTE_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK=$_APP_COMPUTE_RUNTIMES_NETWORK - OPR_EXECUTOR_DOCKER_HUB_USERNAME=$_APP_DOCKER_HUB_USERNAME @@ -1013,6 +1014,7 @@ $hostPath = rtrim($this->getParam('hostPath', ''), '/'); - OPR_EXECUTOR_RUNTIMES=$_APP_FUNCTIONS_RUNTIMES,$_APP_SITES_RUNTIMES - OPR_EXECUTOR_SECRET=$_APP_EXECUTOR_SECRET - OPR_EXECUTOR_RUNTIME_VERSIONS=v5 + - OPEN_RUNTIMES_NFT=$_APP_OPEN_RUNTIMES_NFT - OPR_EXECUTOR_LOGGING_CONFIG=$_APP_LOGGING_CONFIG - OPR_EXECUTOR_STORAGE_DEVICE=$_APP_STORAGE_DEVICE - OPR_EXECUTOR_STORAGE_S3_ACCESS_KEY=$_APP_STORAGE_S3_ACCESS_KEY diff --git a/docker-compose.yml b/docker-compose.yml index 7c8ca3a7c9..5793af4a60 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -560,6 +560,7 @@ services: - _APP_COMPUTE_BUILD_TIMEOUT - _APP_COMPUTE_CPUS - _APP_COMPUTE_MEMORY + - _APP_OPEN_RUNTIMES_NFT - _APP_COMPUTE_SIZE_LIMIT - _APP_OPTIONS_FORCE_HTTPS - _APP_OPTIONS_ROUTER_FORCE_HTTPS @@ -1265,7 +1266,7 @@ services: - OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost - OPR_EXECUTOR_CONNECTION_BUILD_CACHE_STORAGE= - OPR_EXECUTOR_IMAGES=$_APP_EXECUTOR_IMAGES - - OPR_EXECUTOR_INACTIVE_TRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD + - OPR_EXECUTOR_INACTIVE_THRESHOLD=$_APP_COMPUTE_INACTIVE_THRESHOLD - OPR_EXECUTOR_MAINTENANCE_INTERVAL=$_APP_COMPUTE_MAINTENANCE_INTERVAL - OPR_EXECUTOR_NETWORK=$_APP_COMPUTE_RUNTIMES_NETWORK - OPR_EXECUTOR_DOCKER_HUB_USERNAME=$_APP_DOCKER_HUB_USERNAME @@ -1274,6 +1275,7 @@ services: - OPR_EXECUTOR_RUNTIMES=$_APP_FUNCTIONS_RUNTIMES,$_APP_SITES_RUNTIMES - OPR_EXECUTOR_SECRET=$_APP_EXECUTOR_SECRET - OPR_EXECUTOR_RUNTIME_VERSIONS=v5 + - OPEN_RUNTIMES_NFT=$_APP_OPEN_RUNTIMES_NFT - OPR_EXECUTOR_LOGGING_CONFIG=$_APP_LOGGING_CONFIG - OPR_EXECUTOR_STORAGE_DEVICE=$_APP_STORAGE_DEVICE - OPR_EXECUTOR_STORAGE_S3_ACCESS_KEY=$_APP_STORAGE_S3_ACCESS_KEY diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 2550db36e2..eb23453b1e 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -662,6 +662,7 @@ class Builds extends Action 'APPWRITE_FUNCTION_RUNTIME_VERSION' => $runtime['version'] ?? '', 'APPWRITE_FUNCTION_CPUS' => $cpus, 'APPWRITE_FUNCTION_MEMORY' => $memory, + 'OPEN_RUNTIMES_NFT' => System::getEnv('_APP_OPEN_RUNTIMES_NFT', 'enabled'), ]; break; case 'sites': @@ -677,6 +678,7 @@ class Builds extends Action 'APPWRITE_SITE_RUNTIME_VERSION' => $runtime['version'] ?? '', 'APPWRITE_SITE_CPUS' => $cpus, 'APPWRITE_SITE_MEMORY' => $memory, + 'OPEN_RUNTIMES_NFT' => System::getEnv('_APP_OPEN_RUNTIMES_NFT', 'enabled'), ]; break; } From 2dc2358c998fd6428634a78eb02ee2fae7347974 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:13:51 +0530 Subject: [PATCH 80/80] chore: remove sunset SDK entries --- app/config/sdks.php | 18 -------- docs/sdks/deno/CHANGELOG.md | 29 ------------ docs/sdks/deno/GETTING_STARTED.md | 68 ---------------------------- docs/sdks/flutter-dev/CHANGELOG.md | 66 --------------------------- docs/sdks/flutter-dev/EXAMPLES.md | 63 -------------------------- docs/sdks/objective-c/CHANGELOG.md | 1 - src/Appwrite/Platform/Tasks/SDKs.php | 8 ---- 7 files changed, 253 deletions(-) delete mode 100644 docs/sdks/deno/CHANGELOG.md delete mode 100644 docs/sdks/deno/GETTING_STARTED.md delete mode 100644 docs/sdks/flutter-dev/CHANGELOG.md delete mode 100644 docs/sdks/flutter-dev/EXAMPLES.md delete mode 100644 docs/sdks/objective-c/CHANGELOG.md diff --git a/app/config/sdks.php b/app/config/sdks.php index f3d89db3c2..8d128cfe0c 100644 --- a/app/config/sdks.php +++ b/app/config/sdks.php @@ -95,24 +95,6 @@ return [ 'gitBranch' => 'dev', 'changelog' => \realpath(__DIR__ . '/../../docs/sdks/apple/CHANGELOG.md'), ], - [ - 'key' => 'objective-c', - 'name' => 'Objective C', - 'url' => '', - 'package' => '', - 'enabled' => false, - 'beta' => false, - 'dev' => false, - 'hidden' => false, - 'family' => APP_SDK_PLATFORM_CLIENT, - 'prism' => '', - 'source' => false, - 'gitUrl' => 'git@github.com:appwrite/sdk-for-objective-c.git', - 'gitRepoName' => 'sdk-for-objective-c', - 'gitUserName' => 'appwrite', - 'gitBranch' => 'dev', - 'changelog' => \realpath(__DIR__ . '/../../docs/sdks/objective-c/CHANGELOG.md'), - ], [ 'key' => 'android', 'name' => 'Android', diff --git a/docs/sdks/deno/CHANGELOG.md b/docs/sdks/deno/CHANGELOG.md deleted file mode 100644 index 4319fb6d59..0000000000 --- a/docs/sdks/deno/CHANGELOG.md +++ /dev/null @@ -1,29 +0,0 @@ -# Change Log - -## 16.0.0 - -* Add array-based enum parameters (e.g., `permissions: BrowserPermission[]`). -* Breaking change: `Output` enum has been removed; use `ImageFormat` instead. -* Add `getQueueAudits` support to `Health` service. -* Add longtext/mediumtext/text/varchar attribute and column helpers to `Databases` and `TablesDB` services. - -## 15.1.0 - -* Add `incrementDocumentAttribute` and `decrementDocumentAttribute` support to `Databases` service -* Add `gif` support to `ImageFormat` enum -* Add `dart38` and `flutter332` support to runtime models -* Add `encrypt` support to `StringAttribute` model -* Add `sequence` support to `Document` model -* Add `upsertDocument` support to `Databases` service - -## 15.0.0 - -* Add `` to doc examples due to the new multi region endpoints -* Add doc examples and methods for bulk api transactions: `createDocuments`, `deleteDocuments` etc. -* Add doc examples, class and methods for new `Sites` service -* Add doc examples, class and methods for new `Tokens` service -* Add enums for `BuildRuntime `, `Adapter`, `Framework`, `DeploymentDownloadType` and `VCSDeploymentType` -* Updates enum for `runtimes` with Pythonml312, Dart219, Flutter327 and Flutter329 -* Add `token` param to `getFilePreview` and `getFileView` for File tokens usage -* Add `queries` and `search` params to `listMemberships` method -* Removes `search` param from `listExecutions` method \ No newline at end of file diff --git a/docs/sdks/deno/GETTING_STARTED.md b/docs/sdks/deno/GETTING_STARTED.md deleted file mode 100644 index 5046d61c74..0000000000 --- a/docs/sdks/deno/GETTING_STARTED.md +++ /dev/null @@ -1,68 +0,0 @@ -## Getting Started - -### Init your SDK - -Initialize your SDK with your Appwrite server API endpoint and project ID which can be found in your project settings page and your new API secret Key from project's API keys section. - -```typescript -let client = new sdk.Client(); - -client - .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint - .setProject('5df5acd0d48c2') // Your project ID - .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key - .setSelfSigned() // Use only on dev mode with a self-signed SSL cert -; -``` - -### Make your first request - -Once your SDK object is set, create any of the Appwrite service objects and choose any request to send. Full documentation for any service method you would like to use can be found in your SDK documentation or in the [API References](https://appwrite.io/docs) section. - -```typescript -let users = new sdk.Users(client); - -let user = await users.create(ID.unique(), "email@example.com", "+123456789", "password", "Walter O'Brien"); -console.log(user); -``` - -### Full Example - -```typescript -import * as sdk from "https://deno.land/x/appwrite/mod.ts"; - -let client = new sdk.Client(); -let users = new sdk.Users(client); - -client - .setEndpoint('https://[HOSTNAME_OR_IP]/v1') // Your API Endpoint - .setProject('5df5acd0d48c2') // Your project ID - .setKey('919c2d18fb5d4...a2ae413da83346ad2') // Your secret API key - .setSelfSigned() // Use only on dev mode with a self-signed SSL cert -; - -let user = await users.create(ID.unique(), "email@example.com", "+123456789", "password", "Walter O'Brien"); -console.log(user); -``` - -### Error Handling - -The Appwrite Deno SDK raises `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example. - -```typescript -let users = new sdk.Users(client); - -try { - let user = await users.create(ID.unique(), "email@example.com", "+123456789", "password", "Walter O'Brien"); -} catch(e) { - console.log(e.message); -} -``` - -### Learn more - -You can use the following resources to learn more and get help -- 🚀 [Getting Started Tutorial](https://appwrite.io/docs/getting-started-for-server) -- 📜 [Appwrite Docs](https://appwrite.io/docs) -- 💬 [Discord Community](https://appwrite.io/discord) -- 🚂 [Appwrite Deno Playground](https://github.com/appwrite/playground-for-deno) diff --git a/docs/sdks/flutter-dev/CHANGELOG.md b/docs/sdks/flutter-dev/CHANGELOG.md deleted file mode 100644 index 9b6bb58c41..0000000000 --- a/docs/sdks/flutter-dev/CHANGELOG.md +++ /dev/null @@ -1,66 +0,0 @@ -## 0.3.2 - -- Fixed package namespaces -- Breaking change: `Output` enum has been removed; use `ImageFormat` instead. -- Add `Channel` helpers for Realtime. - -## 0.3.1 - -- Downgraded meta package version to 1.1.8 - -## 0.3.0 - -- Updated package dependencies (@lohanidamodar) -- Added Flutter for Web compatibility (@lohanidamodar) - -## 0.2.3 - -- Fixed OAuth2 cookie bug, where a new session cookie couldn't overwrite an old cookie - -## 0.2.2 - -- Fixed an error that happened when the OAuth session creation request was sent before any other API call -- Fixed a bug in the Avatars service where location URL generation had syntax error - -## 0.2.1 - -- Fixed callback scheme - -## 0.2.0 - -- Updated flutter_web_auth plugin to version 0.2.4 -- Added per project unique callback for OAuth2 redirects to avoid conflicts between multiple Appwrite projects - -## 0.1.1 - -- Updated flutter_web_auth version - -## 0.1.0 - -- Added examples file -- Some minor style fixes - -## 0.0.14 - -- Using MultipartFile for file uploads - -## 0.0.13 - -- Fix for file upload method - -## 0.0.12 - -- Added file upload support for storage service - -## 0.0.11 - -- Added integration with web auth plugin to support Appwrite OAuth API - -## 0.0.9 - -- Updated default params - -## 0.0.8 - -- Fixed compilation error in Client class -- Shorter description for package diff --git a/docs/sdks/flutter-dev/EXAMPLES.md b/docs/sdks/flutter-dev/EXAMPLES.md deleted file mode 100644 index d0cb0c2b2a..0000000000 --- a/docs/sdks/flutter-dev/EXAMPLES.md +++ /dev/null @@ -1,63 +0,0 @@ -# Examples - -Init your Appwrite client: - -```dart -Client client = Client(); - -client - .setEndpoint('https://localhost/v1') // Your Appwrite Endpoint - .setProject('5e8cf4f46b5e8') // Your project ID - .setSelfSigned() // Remove in production -; -``` - -Create a new user and session: - -```dart -Account account = Account(client); - -final user = await account.create(userId: ID.unique(), email: "email@example.com", password: "password", name: "Walter O'Brien"); - -final session = await account.createEmailSession(email: 'me@appwrite.io', password: 'password'); - -``` - -Fetch user profile: - -```dart -Account account = Account(client); - -final profile = await account.get(); -``` - -Upload File: - -```dart -Storage storage = Storage(client); - -late InputFile file; - -if(kIsWeb) { - file = InputFile(bytes: pickedFile.bytes, filename: 'image.jpg'); -} else { - file = InputFile(path: './path-to-file/image.jpg', filename: 'image.jpg'); -} - -storage.createFile( - bucketId: '[BUCKET_ID]', - fileId: '[FILE_ID]', // use 'unique()' to automatically generate a unique ID - file: file, - permissions: [ - Permission.read(Role.any()), - ], -) -.then((response) { - print(response); // File uploaded! -}) -.catchError((error) { - print(error.response); -}); -``` - -All examples and API features are available at the [official Appwrite docs](https://appwrite.io/docs) diff --git a/docs/sdks/objective-c/CHANGELOG.md b/docs/sdks/objective-c/CHANGELOG.md deleted file mode 100644 index 420e6f23d0..0000000000 --- a/docs/sdks/objective-c/CHANGELOG.md +++ /dev/null @@ -1 +0,0 @@ -# Change Log diff --git a/src/Appwrite/Platform/Tasks/SDKs.php b/src/Appwrite/Platform/Tasks/SDKs.php index b3802b8eac..30b355d8ae 100644 --- a/src/Appwrite/Platform/Tasks/SDKs.php +++ b/src/Appwrite/Platform/Tasks/SDKs.php @@ -10,7 +10,6 @@ use Appwrite\SDK\Language\CLI; use Appwrite\SDK\Language\CodexPlugin; use Appwrite\SDK\Language\CursorPlugin; use Appwrite\SDK\Language\Dart; -use Appwrite\SDK\Language\Deno; use Appwrite\SDK\Language\DotNet; use Appwrite\SDK\Language\Flutter; use Appwrite\SDK\Language\Go; @@ -389,9 +388,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND $warning = $warning . "\n\n > This is the Node.js SDK for integrating with Appwrite from your Node.js server-side code. If you're looking to integrate from the browser, you should check [appwrite/sdk-for-web](https://github.com/appwrite/sdk-for-web)"; break; - case 'deno': - $config = new Deno(); - break; case 'python': $config = new Python(); $config->setPipPackage('appwrite'); @@ -409,10 +405,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND $config = new ReactNative(); $config->setNPMPackage('react-native-appwrite'); break; - case 'flutter-dev': - $config = new Flutter(); - $config->setPackageName('appwrite_dev'); - break; case 'dart': $config = new Dart(); $config->setPackageName('dart_appwrite');