From e18e848a68671fdfd2dd2cb81ab769d8cbbc4b62 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Sat, 11 Apr 2026 18:50:26 +0200 Subject: [PATCH 001/153] feat(projects): add onboarding stages tracking and stages API Add configurable onboarding stages keyed by SDK method, persist completion in project onboarding JSON on successful API responses, and expose listStages and updateStage routes with stages.read/write scopes and stage/stageList models. Made-with: Cursor --- app/config/collections/platform.php | 11 ++ app/config/onboarding.php | 30 ++++ app/config/roles.php | 2 + app/config/scopes/project.php | 6 + app/controllers/shared/api.php | 47 +++++- app/init/configs.php | 1 + app/init/constants.php | 6 + app/init/models.php | 3 + .../Modules/Projects/Http/Stages/Update.php | 150 ++++++++++++++++++ .../Modules/Projects/Http/Stages/XList.php | 97 +++++++++++ .../Modules/Projects/Services/Http.php | 5 + src/Appwrite/Utopia/Response.php | 2 + .../Utopia/Response/Model/Project.php | 6 + src/Appwrite/Utopia/Response/Model/Stage.php | 61 +++++++ 14 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 app/config/onboarding.php create mode 100644 src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php create mode 100644 src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php create mode 100644 src/Appwrite/Utopia/Response/Model/Stage.php diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 6195c11724..4775fd174f 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -342,6 +342,17 @@ $platformCollections = [ 'array' => true, 'filters' => [], ], + [ + '$id' => ID::custom('onboarding'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 65536, + 'signed' => true, + 'required' => false, + 'default' => [], + 'array' => false, + 'filters' => ['json'], + ], [ '$id' => 'status', 'type' => Database::VAR_STRING, diff --git a/app/config/onboarding.php b/app/config/onboarding.php new file mode 100644 index 0000000000..626ddd91ca --- /dev/null +++ b/app/config/onboarding.php @@ -0,0 +1,30 @@ + 'create_database', + 'sdk' => 'databases.create', + ], + [ + 'id' => 'create_bucket', + 'sdk' => 'storage.createBucket', + ], + [ + 'id' => 'create_function', + 'sdk' => 'functions.create', + ], +]; + +$sdkIndex = []; +foreach ($stages as $stage) { + $sdkIndex[$stage['sdk']] = $stage['id']; +} + +return [ + 'stages' => $stages, + 'sdkIndex' => $sdkIndex, +]; diff --git a/app/config/roles.php b/app/config/roles.php index 116e8ac932..0e9d391418 100644 --- a/app/config/roles.php +++ b/app/config/roles.php @@ -95,6 +95,8 @@ $admins = [ 'tokens.write', 'schedules.read', 'schedules.write', + 'stages.read', + 'stages.write', ]; return [ diff --git a/app/config/scopes/project.php b/app/config/scopes/project.php index 6c7f75c08e..c195f1e9cb 100644 --- a/app/config/scopes/project.php +++ b/app/config/scopes/project.php @@ -151,6 +151,12 @@ return [ // List of publicly visible scopes 'schedules.write' => [ 'description' => 'Access to create, update, and delete your project\'s schedules', ], + 'stages.read' => [ + 'description' => 'Access to read your project\'s stages', + ], + 'stages.write' => [ + 'description' => 'Access to update your project\'s stages', + ], 'migrations.read' => [ 'description' => 'Access to read your project\'s migrations', ], diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index fa96d2ae80..bb8b118c46 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -800,16 +800,61 @@ Http::shutdown() ->inject('queueForWebhooks') ->inject('queueForRealtime') ->inject('dbForProject') + ->inject('dbForPlatform') ->inject('authorization') ->inject('timelimit') ->inject('eventProcessor') ->inject('bus') ->inject('apiKey') ->inject('mode') - ->action(function (Http $utopia, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, Audit $queueForAudits, Context $usage, UsagePublisher $publisherForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Func $queueForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Http $utopia, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, Audit $queueForAudits, Context $usage, UsagePublisher $publisherForUsage, Delete $queueForDeletes, EventDatabase $queueForDatabase, Build $queueForBuilds, Messaging $queueForMessaging, Func $queueForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Database $dbForPlatform, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); + /** + * Persist completed stage when the route matches a configured SDK method (stored on project as `onboarding`: stageId → row). + */ + if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300 && $project->getId() !== 'console') { + $sdkLabel = $utopia->getRoute()?->getLabel('sdk', false); + $stageId = null; + if ($sdkLabel !== false && $sdkLabel !== null) { + $sdkIndex = Config::getParam('onboarding', [])['sdkIndex'] ?? []; + foreach ($sdkLabel instanceof Method ? [$sdkLabel] : (\is_array($sdkLabel) ? $sdkLabel : []) as $sdkMethod) { + if ($sdkMethod instanceof Method && isset($sdkIndex[$k = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName()])) { + $stageId = $sdkIndex[$k]; + break; + } + } + } + if ($stageId !== null) { + $byStageId = $project->getAttribute('onboarding', []); + if (! \is_array($byStageId)) { + $byStageId = []; + } + $done = \is_array($byStageId[$stageId] ?? null) ? ($byStageId[$stageId]['status'] ?? '') : ''; + if ($done !== ONBOARDING_STATUS_COMPLETED && $done !== ONBOARDING_STATUS_SKIPPED) { + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) + ? match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTIVITY_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTIVITY_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_DYNAMIC => ACTIVITY_TYPE_KEY_PROJECT, + default => ACTIVITY_TYPE_KEY_PROJECT, + } + : (! $user->isEmpty() + ? ($mode === APP_MODE_ADMIN ? ACTIVITY_TYPE_ADMIN : ACTIVITY_TYPE_USER) + : ACTIVITY_TYPE_GUEST); + $byStageId[$stageId] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byStageId, + ]))); + } + } + } + if (! empty($queueForEvents->getEvent())) { if (empty($queueForEvents->getPayload())) { $queueForEvents->setPayload($responsePayload); diff --git a/app/init/configs.php b/app/init/configs.php index 35c8e3899d..1eba1d5dcd 100644 --- a/app/init/configs.php +++ b/app/init/configs.php @@ -26,6 +26,7 @@ Config::load('projectScopes', __DIR__ . '/../config/scopes/project.php', $config Config::load('organizationScopes', __DIR__ . '/../config/scopes/organization.php', $configAdapter); Config::load('accountScopes', __DIR__ . '/../config/scopes/account.php', $configAdapter); Config::load('services', __DIR__ . '/../config/services.php', $configAdapter); // List of services +Config::load('onboarding', __DIR__ . '/../config/onboarding.php', $configAdapter); // Project onboarding stages → routes Config::load('variables', __DIR__ . '/../config/variables.php', $configAdapter); // List of env variables Config::load('regions', __DIR__ . '/../config/regions.php', $configAdapter); // List of available regions Config::load('avatar-browsers', __DIR__ . '/../config/avatars/browsers.php', $configAdapter); diff --git a/app/init/constants.php b/app/init/constants.php index 3b907572ab..6e842a74a0 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -163,6 +163,12 @@ const ACTIVITY_TYPE_KEY_PROJECT = 'keyProject'; const ACTIVITY_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTIVITY_TYPE_KEY_ORGANIZATION = 'keyOrganization'; +/** + * Project onboarding stage status (stored per stage id under project.onboarding JSON). + */ +const ONBOARDING_STATUS_COMPLETED = 'completed'; +const ONBOARDING_STATUS_SKIPPED = 'skipped'; + /** * MFA */ diff --git a/app/init/models.php b/app/init/models.php index dd97b03652..646ff45af9 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -126,6 +126,7 @@ use Appwrite\Utopia\Response\Model\Schedule; use Appwrite\Utopia\Response\Model\Session; use Appwrite\Utopia\Response\Model\Site; use Appwrite\Utopia\Response\Model\Specification; +use Appwrite\Utopia\Response\Model\Stage; use Appwrite\Utopia\Response\Model\Subscriber; use Appwrite\Utopia\Response\Model\Table; use Appwrite\Utopia\Response\Model\Target; @@ -212,6 +213,7 @@ Response::setModel(new BaseList('Variables List', Response::MODEL_VARIABLE_LIST, Response::setModel(new BaseList('Status List', Response::MODEL_HEALTH_STATUS_LIST, 'statuses', Response::MODEL_HEALTH_STATUS)); Response::setModel(new BaseList('Rule List', Response::MODEL_PROXY_RULE_LIST, 'rules', Response::MODEL_PROXY_RULE)); Response::setModel(new BaseList('Schedules List', Response::MODEL_SCHEDULE_LIST, 'schedules', Response::MODEL_SCHEDULE)); +Response::setModel(new BaseList('Stages List', Response::MODEL_STAGE_LIST, 'stages', Response::MODEL_STAGE, false, false)); Response::setModel(new BaseList('Locale codes list', Response::MODEL_LOCALE_CODE_LIST, 'localeCodes', Response::MODEL_LOCALE_CODE)); Response::setModel(new BaseList('Provider list', Response::MODEL_PROVIDER_LIST, 'providers', Response::MODEL_PROVIDER)); Response::setModel(new BaseList('Message list', Response::MODEL_MESSAGE_LIST, 'messages', Response::MODEL_MESSAGE)); @@ -373,6 +375,7 @@ Response::setModel(new Headers()); Response::setModel(new Specification()); Response::setModel(new Rule()); Response::setModel(new Schedule()); +Response::setModel(new Stage()); Response::setModel(new TemplateSMS()); Response::setModel(new TemplateEmail()); Response::setModel(new ConsoleVariables()); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php new file mode 100644 index 0000000000..a489dd2b3f --- /dev/null +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php @@ -0,0 +1,150 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) + ->setHttpPath('/v1/projects/:projectId/stages/:stageId') + ->desc('Update stage') + ->groups(['api', 'projects']) + ->label('scope', 'stages.write') + ->label('audits.event', 'stages.update') + ->label('audits.resource', 'project/{request.projectId}') + ->label('sdk', new Method( + namespace: 'projects', + group: 'stages', + name: 'updateStage', + description: '/docs/references/projects/update-stage.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_STAGE, + ) + ], + )) + ->param('projectId', '', new UID(), 'Project unique ID.') + ->param('stageId', '', new Text(64), 'Stage ID.') + ->param('skip', true, new Boolean(), 'Mark the stage as skipped.', true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('apiKey') + ->inject('user') + ->inject('mode') + ->callback($this->action(...)); + } + + public function action(string $projectId, string $stageId, bool $skip, Response $response, Database $dbForPlatform, ?Key $apiKey, User $user, string $mode): void + { + $project = $dbForPlatform->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } + + $definition = $this->getStageDefinition($stageId); + + $byStageId = $project->getAttribute('onboarding', []); + if (! \is_array($byStageId)) { + $byStageId = []; + } + + $row = \is_array($byStageId[$stageId] ?? null) ? $byStageId[$stageId] : null; + + if ($skip) { + $prev = \is_array($row) ? ($row['status'] ?? '') : ''; + if ($prev !== ONBOARDING_STATUS_COMPLETED) { + $byStageId[$stageId] = [ + 'status' => ONBOARDING_STATUS_SKIPPED, + 'at' => DateTime::now(), + 'actorType' => $this->resolveActorType($apiKey, $user, $mode), + ]; + $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('onboarding', $byStageId)); + $byStageId = $project->getAttribute('onboarding', []); + $row = \is_array($byStageId[$stageId] ?? null) ? $byStageId[$stageId] : null; + } + } + + $response->dynamic(new Document($this->formatStageRow($definition, $row)), Response::MODEL_STAGE); + } + + /** + * @return array{id: string, sdk: string} + */ + private function getStageDefinition(string $stageId): array + { + foreach (Config::getParam('onboarding', [])['stages'] ?? [] as $definition) { + if (($definition['id'] ?? '') === $stageId) { + return $definition; + } + } + + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unknown stage ID: ' . $stageId); + } + + /** + * @param array|null $row + * @return array + */ + private function formatStageRow(array $definition, ?array $row): array + { + $stageId = $definition['id']; + $status = \is_array($row) ? ($row['status'] ?? null) : null; + $at = \is_array($row) ? ($row['at'] ?? '') : ''; + $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; + + return [ + 'id' => $stageId, + 'sdk' => $definition['sdk'] ?? '', + 'status' => $status ?? 'pending', + 'at' => $at, + 'actorType' => $actorType, + ]; + } + + private function resolveActorType(?Key $apiKey, User $user, string $mode): string + { + if ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) { + return match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTIVITY_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTIVITY_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_DYNAMIC => ACTIVITY_TYPE_KEY_PROJECT, + default => ACTIVITY_TYPE_KEY_PROJECT, + }; + } + + if (! $user->isEmpty()) { + return $mode === APP_MODE_ADMIN ? ACTIVITY_TYPE_ADMIN : ACTIVITY_TYPE_USER; + } + + return ACTIVITY_TYPE_GUEST; + } +} diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php new file mode 100644 index 0000000000..e0f4f4074b --- /dev/null +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php @@ -0,0 +1,97 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/projects/:projectId/stages') + ->desc('List stages') + ->groups(['api', 'projects']) + ->label('scope', 'stages.read') + ->label('sdk', new Method( + namespace: 'projects', + group: 'stages', + name: 'listStages', + description: '/docs/references/projects/list-stages.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_OK, + model: Response::MODEL_STAGE_LIST, + ) + ], + )) + ->param('projectId', '', new UID(), 'Project unique ID.') + ->inject('response') + ->inject('dbForPlatform') + ->callback($this->action(...)); + } + + public function action(string $projectId, Response $response, Database $dbForPlatform): void + { + $project = $dbForPlatform->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } + + $response->dynamic(new Document([ + 'stages' => $this->buildStagesList($project), + ]), Response::MODEL_STAGE_LIST); + } + + /** + * @return array> + */ + private function buildStagesList(Document $project): array + { + $definitions = Config::getParam('onboarding', [])['stages'] ?? []; + $byStageId = $project->getAttribute('onboarding', []); + if (! \is_array($byStageId)) { + $byStageId = []; + } + + $out = []; + foreach ($definitions as $definition) { + $stageId = $definition['id']; + $row = $byStageId[$stageId] ?? null; + $status = \is_array($row) ? ($row['status'] ?? null) : null; + + $at = \is_array($row) ? ($row['at'] ?? '') : ''; + $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; + + $out[] = [ + 'id' => $stageId, + 'sdk' => $definition['sdk'] ?? '', + 'status' => $status ?? 'pending', + 'at' => $at, + 'actorType' => $actorType, + ]; + } + + return $out; + } +} diff --git a/src/Appwrite/Platform/Modules/Projects/Services/Http.php b/src/Appwrite/Platform/Modules/Projects/Services/Http.php index 8275e664d5..56e8c26d73 100644 --- a/src/Appwrite/Platform/Modules/Projects/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Projects/Services/Http.php @@ -14,6 +14,8 @@ use Appwrite\Platform\Modules\Projects\Http\Projects\XList as ListProjects; use Appwrite\Platform\Modules\Projects\Http\Schedules\Create as CreateSchedule; use Appwrite\Platform\Modules\Projects\Http\Schedules\Get as GetSchedule; use Appwrite\Platform\Modules\Projects\Http\Schedules\XList as ListSchedules; +use Appwrite\Platform\Modules\Projects\Http\Stages\Update as UpdateStages; +use Appwrite\Platform\Modules\Projects\Http\Stages\XList as ListStages; use Utopia\Platform\Service; class Http extends Service @@ -35,5 +37,8 @@ class Http extends Service $this->addAction(CreateSchedule::getName(), new CreateSchedule()); $this->addAction(GetSchedule::getName(), new GetSchedule()); $this->addAction(ListSchedules::getName(), new ListSchedules()); + + $this->addAction(ListStages::getName(), new ListStages()); + $this->addAction(UpdateStages::getName(), new UpdateStages()); } } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 295348c665..c4a272f5bf 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -247,6 +247,8 @@ class Response extends SwooleResponse // Project public const MODEL_PROJECT = 'project'; public const MODEL_PROJECT_LIST = 'projectList'; + public const MODEL_STAGE = 'stage'; + public const MODEL_STAGE_LIST = 'stageList'; public const MODEL_WEBHOOK = 'webhook'; public const MODEL_WEBHOOK_LIST = 'webhookList'; public const MODEL_KEY = 'key'; diff --git a/src/Appwrite/Utopia/Response/Model/Project.php b/src/Appwrite/Utopia/Response/Model/Project.php index 1ef73aa769..2c46d51243 100644 --- a/src/Appwrite/Utopia/Response/Model/Project.php +++ b/src/Appwrite/Utopia/Response/Model/Project.php @@ -308,6 +308,12 @@ class Project extends Model 'default' => 'active', 'example' => 'active', ]) + ->addRule('onboarding', [ + 'type' => self::TYPE_JSON, + 'description' => 'Stage progress (completed or skipped) with timestamps and actor types, keyed by stage id.', + 'default' => [], + 'example' => [], + ]) ; $services = Config::getParam('services', []); diff --git a/src/Appwrite/Utopia/Response/Model/Stage.php b/src/Appwrite/Utopia/Response/Model/Stage.php new file mode 100644 index 0000000000..8adaaaae47 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/Stage.php @@ -0,0 +1,61 @@ +addRule('id', [ + 'type' => self::TYPE_STRING, + 'description' => 'Stage ID.', + 'default' => '', + 'example' => 'create_database', + ]) + ->addRule('sdk', [ + 'type' => self::TYPE_STRING, + 'description' => 'SDK method key (namespace.name) for this stage.', + 'default' => '', + 'example' => 'databases.create', + ]) + ->addRule('status', [ + 'type' => self::TYPE_STRING, + 'description' => 'Stage status.', + 'default' => 'pending', + 'example' => 'completed', + ]) + ->addRule('at', [ + 'type' => self::TYPE_DATETIME, + 'description' => 'When the stage was completed or skipped, in ISO 8601 format.', + 'default' => '', + 'example' => self::TYPE_DATETIME_EXAMPLE, + ]) + ->addRule('actorType', [ + 'type' => self::TYPE_STRING, + 'description' => 'Actor type when the stage was recorded.', + 'default' => '', + 'example' => 'user', + ]) + ; + } + + public function getName(): string + { + return 'Stage'; + } + + public function getType(): string + { + return Response::MODEL_STAGE; + } + + public function filter(Document $document): Document + { + return $document; + } +} From f1c24a0b24ba37811cd85980df99e2d9eb821c38 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Sat, 11 Apr 2026 19:14:57 +0200 Subject: [PATCH 002/153] feat(projects): realtime on stage completion; add stages E2E tests Emit console realtime when onboarding stages complete in API shutdown. Add Project/StagesBase + StagesConsoleClientTest following Platforms pattern. Made-with: Cursor --- app/controllers/shared/api.php | 15 ++ tests/e2e/Services/Project/StagesBase.php | 179 ++++++++++++++++++ .../Project/StagesConsoleClientTest.php | 14 ++ 3 files changed, 208 insertions(+) create mode 100644 tests/e2e/Services/Project/StagesBase.php create mode 100644 tests/e2e/Services/Project/StagesConsoleClientTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index bb8b118c46..6f13250405 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -851,6 +851,21 @@ Http::shutdown() $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'onboarding' => $byStageId, ]))); + + $queueForRealtime->reset(); + $queueForRealtime + ->setProject($project) + ->setSubscribers(['console']) + ->setEvent('projects.[projectId].stages.[stageId].complete') + ->setParam('projectId', $project->getId()) + ->setParam('stageId', $stageId) + ->setPayload([ + 'stageId' => $stageId, + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => $byStageId[$stageId]['at'], + 'actorType' => $actorType, + ]) + ->trigger(); } } } diff --git a/tests/e2e/Services/Project/StagesBase.php b/tests/e2e/Services/Project/StagesBase.php new file mode 100644 index 0000000000..9bf3f21f4b --- /dev/null +++ b/tests/e2e/Services/Project/StagesBase.php @@ -0,0 +1,179 @@ +createTeamAndProject(); + + $response = $this->listStages($projectId); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertIsArray($response['body']['stages']); + $this->assertCount(3, $response['body']['stages']); + + $ids = array_column($response['body']['stages'], 'id'); + $this->assertSame(['create_database', 'create_bucket', 'create_function'], $ids); + + foreach ($response['body']['stages'] as $stage) { + $this->assertArrayHasKey('sdk', $stage); + $this->assertArrayHasKey('status', $stage); + $this->assertArrayHasKey('at', $stage); + $this->assertArrayHasKey('actorType', $stage); + $this->assertSame('pending', $stage['status']); + } + + // Verify via GET for unknown project + $response = $this->listStages(ID::unique()); + $this->assertSame(404, $response['headers']['status-code']); + $this->assertSame(Exception::PROJECT_NOT_FOUND, $response['body']['type']); + } + + public function testListProjectStagesWithoutAuthentication(): void + { + $projectId = $this->createTeamAndProject(); + + $response = $this->listStages($projectId, false); + + $this->assertSame(401, $response['headers']['status-code']); + } + + // ========================================================================= + // Update stage (skip) tests + // ========================================================================= + + public function testUpdateProjectStageSkip(): void + { + $projectId = $this->createTeamAndProject(); + + $response = $this->updateStage($projectId, 'create_database', true); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame('create_database', $response['body']['id']); + $this->assertSame('databases.create', $response['body']['sdk']); + $this->assertSame(ONBOARDING_STATUS_SKIPPED, $response['body']['status']); + $this->assertNotEmpty($response['body']['at']); + $this->assertSame(ACTIVITY_TYPE_USER, $response['body']['actorType']); + + $dateValidator = new DatetimeValidator(); + $this->assertSame(true, $dateValidator->isValid($response['body']['at'])); + + $list = $this->listStages($projectId); + $this->assertSame(200, $list['headers']['status-code']); + $first = $list['body']['stages'][0]; + $this->assertSame('create_database', $first['id']); + $this->assertSame(ONBOARDING_STATUS_SKIPPED, $first['status']); + $this->assertSame(ACTIVITY_TYPE_USER, $first['actorType']); + + $response = $this->updateStage($projectId, 'unknown_stage_xyz', true); + $this->assertSame(400, $response['headers']['status-code']); + $this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); + + $response = $this->updateStage(ID::unique(), 'create_bucket', true); + $this->assertSame(404, $response['headers']['status-code']); + $this->assertSame(Exception::PROJECT_NOT_FOUND, $response['body']['type']); + } + + public function testUpdateProjectStageSkipFalseLeavesPending(): void + { + $projectId = $this->createTeamAndProject(); + + $response = $this->updateStage($projectId, 'create_bucket', false); + + $this->assertSame(200, $response['headers']['status-code']); + $this->assertSame('create_bucket', $response['body']['id']); + $this->assertSame('pending', $response['body']['status']); + } + + public function testUpdateProjectStageWithoutAuthentication(): void + { + $projectId = $this->createTeamAndProject(); + + $response = $this->updateStage($projectId, 'create_database', true, false); + + $this->assertSame(401, $response['headers']['status-code']); + } + + // ========================================================================= + // Helpers + // ========================================================================= + + protected function listStages(string $projectId, bool $authenticated = true): mixed + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]; + + if ($authenticated) { + $headers = array_merge($headers, $this->getHeaders()); + } + + return $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/stages', $headers, []); + } + + protected function updateStage(string $projectId, string $stageId, bool $skip, bool $authenticated = true): mixed + { + $headers = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]; + + if ($authenticated) { + $headers = array_merge($headers, $this->getHeaders()); + } + + return $this->client->call(Client::METHOD_PATCH, '/projects/' . $projectId . '/stages/' . $stageId, $headers, [ + 'skip' => $skip, + ]); + } + + /** + * Creates a new team and child project under the console session (isolated from cached getProject()). + */ + protected function createTeamAndProject(): string + { + $teamId = ID::unique(); + $team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders()), [ + 'teamId' => $teamId, + 'name' => 'Stages E2E Team', + ]); + + $this->assertContains($team['headers']['status-code'], [201, 409]); + if ($team['headers']['status-code'] === 201) { + $teamId = $team['body']['$id']; + } + + $project = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders()), [ + 'projectId' => ID::unique(), + 'name' => 'Stages E2E Project', + 'teamId' => $teamId, + 'region' => System::getEnv('_APP_REGION', 'default'), + ]); + + $this->assertSame(201, $project['headers']['status-code']); + + return $project['body']['$id']; + } +} diff --git a/tests/e2e/Services/Project/StagesConsoleClientTest.php b/tests/e2e/Services/Project/StagesConsoleClientTest.php new file mode 100644 index 0000000000..cde4f4ab79 --- /dev/null +++ b/tests/e2e/Services/Project/StagesConsoleClientTest.php @@ -0,0 +1,14 @@ + Date: Sat, 11 Apr 2026 21:44:14 +0100 Subject: [PATCH 003/153] docs: add projects stages API reference markdown for spec generation Made-with: Cursor --- docs/references/projects/list-stages.md | 1 + docs/references/projects/update-stage.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 docs/references/projects/list-stages.md create mode 100644 docs/references/projects/update-stage.md diff --git a/docs/references/projects/list-stages.md b/docs/references/projects/list-stages.md new file mode 100644 index 0000000000..3a2f6cc401 --- /dev/null +++ b/docs/references/projects/list-stages.md @@ -0,0 +1 @@ +Get the onboarding stages for the current project, including each stage’s SDK method key and status (for example pending, completed, or skipped). diff --git a/docs/references/projects/update-stage.md b/docs/references/projects/update-stage.md new file mode 100644 index 0000000000..9e6f43919e --- /dev/null +++ b/docs/references/projects/update-stage.md @@ -0,0 +1 @@ +Update an onboarding stage for the current project. Use this endpoint to skip a stage or leave it unchanged without performing the related API action. From 34935b8eeddc418bf884b166dcd196bb37e8120f Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 1 May 2026 12:07:32 +1200 Subject: [PATCH 004/153] 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 005/153] 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 006/153] 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 007/153] 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 008/153] 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 009/153] 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 010/153] 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 011/153] 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 012/153] 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 013/153] 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 014/153] 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 015/153] 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 016/153] 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 017/153] 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 018/153] 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 019/153] 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 020/153] 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 021/153] 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 022/153] 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 023/153] 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 024/153] 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 025/153] 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 026/153] 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 027/153] 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 028/153] 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 029/153] 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 030/153] 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 031/153] 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 032/153] 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 033/153] 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 034/153] 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 035/153] 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 036/153] 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 037/153] 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 038/153] 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 039/153] 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 040/153] 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 041/153] 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 042/153] 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 043/153] 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 044/153] 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 045/153] 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 046/153] 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 047/153] 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 048/153] 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 049/153] 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 050/153] 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 051/153] 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 052/153] 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 053/153] 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 054/153] 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 055/153] 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 056/153] 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 057/153] 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 058/153] 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 4b239a4ebb7345e19b35ed34ef185a9fb6bd0b8a Mon Sep 17 00:00:00 2001 From: deepshekhardas Date: Mon, 25 May 2026 17:17:55 +0530 Subject: [PATCH 059/153] fix(graphql): add missing CORS headers to successful responses The CORS init hook was registered with groups(['api', 'web']) but GraphQL routes use groups(['graphql']), so the hook never fired for GraphQL requests. Responses via GET /v1/graphql and POST /v1/graphql were missing Access-Control-Allow-Origin and other CORS headers, breaking cross-origin browser usage. Add 'graphql' to the CORS init hook groups so CORS headers are set on all successful GraphQL responses. --- app/controllers/general.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 219c14774f..49a74ad470 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -982,7 +982,7 @@ Http::init() * @see https://www.owasp.org/index.php/List_of_useful_HTTP_headers */ Http::init() - ->groups(['api', 'web']) + ->groups(['api', 'web', 'graphql']) ->inject('request') ->inject('response') ->inject('cors') From a4b719a666b5e76818689e1c7ecd54829291cf3a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Tue, 26 May 2026 15:16:08 +1200 Subject: [PATCH 060/153] 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 061/153] 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 062/153] 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 063/153] 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 064/153] 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 065/153] 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 066/153] 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 067/153] 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 068/153] 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 069/153] 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 070/153] 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 071/153] 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 072/153] 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 073/153] 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 452ff7ff75c4f0f45e2269d64f83256eee87add5 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:17:27 +0530 Subject: [PATCH 074/153] fix: disconnect VCS when GitHub installation is revoked (404) --- .../Modules/Functions/Workers/Builds.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index c2b8837c13..ead686b86f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -295,7 +295,23 @@ class Builds extends Action $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + try { + $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + } catch (\Exception $e) { + if (\str_contains($e->getMessage(), 'Status: 404')) { + $collection = $resource->getCollection(); + $dbForProject->updateDocument($collection, $resource->getId(), new Document([ + 'providerRepositoryId' => '', + 'installationId' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + 'providerBranch' => '', + 'providerRootDirectory' => '', + 'providerSilentMode' => true, + ])); + } + throw $e; + } } Span::add('timings.setup', \round(\microtime(true) - $phaseStart, 3)); From b6bc787554dc65553b5393a075e32daab0ae6f0e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:23:34 +0530 Subject: [PATCH 075/153] fix: use correct disconnect pattern matching Deletes worker --- .../Modules/Functions/Workers/Builds.php | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index ead686b86f..f129a94172 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -299,16 +299,19 @@ class Builds extends Action $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { if (\str_contains($e->getMessage(), 'Status: 404')) { - $collection = $resource->getCollection(); - $dbForProject->updateDocument($collection, $resource->getId(), new Document([ - 'providerRepositoryId' => '', - 'installationId' => '', - 'repositoryId' => '', - 'repositoryInternalId' => '', - 'providerBranch' => '', - 'providerRootDirectory' => '', - 'providerSilentMode' => true, - ])); + $repositoryId = $resource->getAttribute('repositoryId', ''); + if (!empty($repositoryId)) { + $dbForPlatform->deleteDocument('repositories', $repositoryId); + } + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); } throw $e; } From 4e793764583f0ec2ef32fda886aaebc1dbe4794d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:27:33 +0530 Subject: [PATCH 076/153] test: add unit tests for disconnectVcs method --- .../Modules/Functions/Workers/Builds.php | 31 +++++----- tests/unit/Functions/BuildsTest.php | 56 +++++++++++++++++++ 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f129a94172..748128ca8b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -299,19 +299,7 @@ class Builds extends Action $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { if (\str_contains($e->getMessage(), 'Status: 404')) { - $repositoryId = $resource->getAttribute('repositoryId', ''); - if (!empty($repositoryId)) { - $dbForPlatform->deleteDocument('repositories', $repositoryId); - } - $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); + $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } throw $e; } @@ -1574,6 +1562,23 @@ class Builds extends Action } } + protected function disconnectVcs(Document $resource, Database $dbForProject, Database $dbForPlatform): void + { + $repositoryId = $resource->getAttribute('repositoryId', ''); + if (!empty($repositoryId)) { + $dbForPlatform->deleteDocument('repositories', $repositoryId); + } + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); + } + private function updateLatestDeployment(Database $dbForProject, Document $resource): Document { $latestDeployment = $dbForProject->findOne('deployments', [ diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index b757856c83..d4021b5d7e 100644 --- a/tests/unit/Functions/BuildsTest.php +++ b/tests/unit/Functions/BuildsTest.php @@ -8,6 +8,7 @@ use Appwrite\Platform\Modules\Functions\Workers\Builds; use PHPUnit\Framework\TestCase; use ReflectionMethod; use Utopia\Config\Config; +use Utopia\Database\Database; use Utopia\Database\Document; final class BuildsTest extends TestCase @@ -87,6 +88,61 @@ final class BuildsTest extends TestCase $this->assertNull($detection->getFallbackFile()); } + public function testDisconnectVcsClearsAllFieldsAndDeletesRepository(): void + { + $resource = new Document([ + '$id' => 'func123', + '$collection' => 'functions', + 'repositoryId' => 'repo456', + 'installationId' => 'install789', + 'installationInternalId' => 'install789internal', + 'providerRepositoryId' => 'gh123', + 'providerBranch' => 'main', + 'providerRootDirectory' => 'functions/my-func', + 'providerSilentMode' => true, + 'repositoryInternalId' => 'repo456internal', + ]); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('repositories', 'repo456'); + + $dbForProject->expects($this->once()) + ->method('updateDocument') + ->with('functions', 'func123', $this->callback(function (Document $doc) { + return $doc->getAttribute('installationId') === '' + && $doc->getAttribute('installationInternalId') === '' + && $doc->getAttribute('providerRepositoryId') === '' + && $doc->getAttribute('providerBranch') === '' + && $doc->getAttribute('providerRootDirectory') === '' + && $doc->getAttribute('providerSilentMode') === false + && $doc->getAttribute('repositoryId') === '' + && $doc->getAttribute('repositoryInternalId') === ''; + })); + + $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); + } + + public function testDisconnectVcsSkipsRepositoryDeletionWhenRepositoryIdEmpty(): void + { + $resource = new Document([ + '$id' => 'func123', + '$collection' => 'functions', + 'repositoryId' => '', + ]); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + + $dbForPlatform->expects($this->never())->method('deleteDocument'); + $dbForProject->expects($this->once())->method('updateDocument'); + + $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); + } + private function callBuilds(string $method, mixed ...$arguments): mixed { return (new ReflectionMethod($this->builds, $method))->invoke($this->builds, ...$arguments); From 48c78ee1cc897c97d9134caf03822865f73462a0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:34:24 +0530 Subject: [PATCH 077/153] fix: disconnect functions and sites on GitHub installation deleted webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index e40e447ed5..e75099b3f3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -78,7 +78,7 @@ class Create extends Action $parsedPayload = $github->getEvent($event, $payload); match ($event) { - $github::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization), + $github::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), $github::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $githubAppId, $privateKey, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), $github::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, @@ -96,12 +96,12 @@ class Create extends Action array $parsedPayload, Database $dbForPlatform, Authorization $authorization, + callable $getProjectDB, ) { if ($parsedPayload["action"] !== "deleted") { return; } - // TODO: Use worker for this job instead (update function/site as well) $providerInstallationId = $parsedPayload["installationId"]; $installations = $dbForPlatform->find('installations', [ @@ -110,6 +110,32 @@ class Create extends Action ]); foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $dbForPlatform->getDocument('projects', $projectId); + + if (!$project->isEmpty()) { + $dbForProject = $getProjectDB($project); + + foreach (['functions', 'sites'] as $collection) { + $resources = $dbForProject->find($collection, [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]); + + foreach ($resources as $resource) { + $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); + } + } + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ Query::equal('installationInternalId', [$installation->getSequence()]), Query::limit(1000) From d6c4ce4fecf510c6142d932d6d7162485d4bcb15 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:47:39 +0530 Subject: [PATCH 078/153] test: add unit tests for GitHub installation deleted webhook handler --- .../unit/Vcs/GitHubInstallationEventTest.php | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/unit/Vcs/GitHubInstallationEventTest.php diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php new file mode 100644 index 0000000000..ac313df596 --- /dev/null +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -0,0 +1,156 @@ +handler = new Create(); + } + + public function testHandleInstallationEventIgnoresNonDeletedActions(): void + { + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createStub(Authorization::class); + + $dbForPlatform->expects($this->never())->method('find'); + $dbForPlatform->expects($this->never())->method('deleteDocument'); + + $result = $this->callHandler('handleInstallationEvent', [ + 'action' => 'created', + 'installationId' => '123', + ], $dbForPlatform, $authorization, fn () => null); + + $this->assertNull($result); + } + + public function testHandleInstallationEventDisconnectsFunctionsAndSites(): void + { + $installation = new Document([ + '$id' => 'install1', + '$sequence' => 'install1seq', + 'projectId' => 'project1', + 'providerInstallationId' => 'gh-install-123', + ]); + + $function = new Document([ + '$id' => 'func1', + '$collection' => 'functions', + 'installationId' => 'install1', + 'installationInternalId' => 'install1seq', + 'providerRepositoryId' => 'repo123', + 'providerBranch' => 'main', + 'providerRootDirectory' => 'functions/my-func', + 'providerSilentMode' => true, + 'repositoryId' => 'repo1', + 'repositoryInternalId' => 'repo1seq', + ]); + + $project = new Document(['$id' => 'project1']); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createMock(Authorization::class); + + $dbForPlatform->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => match ($collection) { + 'installations' => [$installation], + 'repositories' => [], + default => [], + }); + + $dbForPlatform->expects($this->once()) + ->method('getDocument') + ->with('projects', 'project1') + ->willReturn($project); + + $dbForProject->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => $collection === 'functions' ? [$function] : []); + + $dbForProject->expects($this->once()) + ->method('updateDocument') + ->with('functions', 'func1', $this->callback(function (Document $doc) { + return $doc->getAttribute('installationId') === '' + && $doc->getAttribute('installationInternalId') === '' + && $doc->getAttribute('providerRepositoryId') === '' + && $doc->getAttribute('providerBranch') === '' + && $doc->getAttribute('providerRootDirectory') === '' + && $doc->getAttribute('providerSilentMode') === false + && $doc->getAttribute('repositoryId') === '' + && $doc->getAttribute('repositoryInternalId') === ''; + })); + + $authorization->expects($this->exactly(2)) + ->method('skip') + ->willReturnCallback(fn ($fn) => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('installations', 'install1'); + + $getProjectDB = fn () => $dbForProject; + + $this->callHandler('handleInstallationEvent', [ + 'action' => 'deleted', + 'installationId' => 'gh-install-123', + ], $dbForPlatform, $authorization, $getProjectDB); + } + + public function testHandleInstallationEventSkipsDisconnectWhenProjectNotFound(): void + { + $installation = new Document([ + '$id' => 'install1', + '$sequence' => 'install1seq', + 'projectId' => 'project1', + 'providerInstallationId' => 'gh-install-123', + ]); + + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createMock(Authorization::class); + + $dbForPlatform->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => match ($collection) { + 'installations' => [$installation], + 'repositories' => [], + default => [], + }); + + $dbForPlatform->expects($this->once()) + ->method('getDocument') + ->with('projects', 'project1') + ->willReturn(new Document()); + + $authorization->expects($this->exactly(2)) + ->method('skip') + ->willReturnCallback(fn ($fn) => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('installations', 'install1'); + + $this->callHandler('handleInstallationEvent', [ + 'action' => 'deleted', + 'installationId' => 'gh-install-123', + ], $dbForPlatform, $authorization, fn () => null); + } + + private function callHandler(string $method, mixed ...$arguments): mixed + { + return (new ReflectionMethod($this->handler, $method))->invoke($this->handler, ...$arguments); + } +} From 98c4845b69f3f41ba2be834436ce3b42fa8a9d7b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 12:48:11 +0530 Subject: [PATCH 079/153] 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 080/153] 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 081/153] 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 082/153] 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 083/153] 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:00:50 +0530 Subject: [PATCH 084/153] fix: address Greptile P1s - auth skip and pagination in installation webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 66 ++++++++++++------- tests/unit/Functions/BuildsTest.php | 17 ++--- .../unit/Vcs/GitHubInstallationEventTest.php | 34 +++++----- 3 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index e75099b3f3..15b2a4f7b7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -92,7 +92,7 @@ class Create extends Action return; } - private function handleInstallationEvent( + protected function handleInstallationEvent( array $parsedPayload, Database $dbForPlatform, Authorization $authorization, @@ -111,39 +111,57 @@ class Create extends Action foreach ($installations as $installation) { $projectId = $installation->getAttribute('projectId', ''); - $project = $dbForPlatform->getDocument('projects', $projectId); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); if (!$project->isEmpty()) { $dbForProject = $getProjectDB($project); foreach (['functions', 'sites'] as $collection) { - $resources = $dbForProject->find($collection, [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]); + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); - foreach ($resources as $resource) { - $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); - } + foreach ($resources as $resource) { + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', ''))); + } + + $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; + } while ($cursor !== null); } } - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000) - ])); + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); - foreach ($repositories as $repository) { - $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); - } + foreach ($repositories as $repository) { + $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + } + + $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; + } while ($cursor !== null); $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); } diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index d4021b5d7e..36515d2f5b 100644 --- a/tests/unit/Functions/BuildsTest.php +++ b/tests/unit/Functions/BuildsTest.php @@ -113,14 +113,15 @@ final class BuildsTest extends TestCase $dbForProject->expects($this->once()) ->method('updateDocument') ->with('functions', 'func123', $this->callback(function (Document $doc) { - return $doc->getAttribute('installationId') === '' - && $doc->getAttribute('installationInternalId') === '' - && $doc->getAttribute('providerRepositoryId') === '' - && $doc->getAttribute('providerBranch') === '' - && $doc->getAttribute('providerRootDirectory') === '' - && $doc->getAttribute('providerSilentMode') === false - && $doc->getAttribute('repositoryId') === '' - && $doc->getAttribute('repositoryInternalId') === ''; + $this->assertSame('', $doc->getAttribute('installationId')); + $this->assertSame('', $doc->getAttribute('installationInternalId')); + $this->assertSame('', $doc->getAttribute('providerRepositoryId')); + $this->assertSame('', $doc->getAttribute('providerBranch')); + $this->assertSame('', $doc->getAttribute('providerRootDirectory')); + $this->assertFalse($doc->getAttribute('providerSilentMode')); + $this->assertSame('', $doc->getAttribute('repositoryId')); + $this->assertSame('', $doc->getAttribute('repositoryInternalId')); + return true; })); $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php index ac313df596..46582ffd61 100644 --- a/tests/unit/Vcs/GitHubInstallationEventTest.php +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -66,7 +66,7 @@ final class GitHubInstallationEventTest extends TestCase $dbForPlatform->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => match ($collection) { + ->willReturnCallback(fn (string $collection): array => match ($collection) { 'installations' => [$installation], 'repositories' => [], default => [], @@ -79,24 +79,27 @@ final class GitHubInstallationEventTest extends TestCase $dbForProject->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => $collection === 'functions' ? [$function] : []); + ->willReturnCallback(fn (string $collection): array => $collection === 'functions' ? [$function] : []); $dbForProject->expects($this->once()) ->method('updateDocument') ->with('functions', 'func1', $this->callback(function (Document $doc) { - return $doc->getAttribute('installationId') === '' - && $doc->getAttribute('installationInternalId') === '' - && $doc->getAttribute('providerRepositoryId') === '' - && $doc->getAttribute('providerBranch') === '' - && $doc->getAttribute('providerRootDirectory') === '' - && $doc->getAttribute('providerSilentMode') === false - && $doc->getAttribute('repositoryId') === '' - && $doc->getAttribute('repositoryInternalId') === ''; + $this->assertSame('', $doc->getAttribute('installationId')); + $this->assertSame('', $doc->getAttribute('installationInternalId')); + $this->assertSame('', $doc->getAttribute('providerRepositoryId')); + $this->assertSame('', $doc->getAttribute('providerBranch')); + $this->assertSame('', $doc->getAttribute('providerRootDirectory')); + $this->assertFalse($doc->getAttribute('providerSilentMode')); + $this->assertSame('', $doc->getAttribute('repositoryId')); + $this->assertSame('', $doc->getAttribute('repositoryInternalId')); + return true; })); - $authorization->expects($this->exactly(2)) + // 1: getDocument(projects), 2: find(functions), 3: updateDocument(func1), + // 4: find(sites), 5: find(repositories), 6: deleteDocument(installations) + $authorization->expects($this->exactly(6)) ->method('skip') - ->willReturnCallback(fn ($fn) => $fn()); + ->willReturnCallback(fn (callable $fn): mixed => $fn()); $dbForPlatform->expects($this->once()) ->method('deleteDocument') @@ -124,7 +127,7 @@ final class GitHubInstallationEventTest extends TestCase $dbForPlatform->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => match ($collection) { + ->willReturnCallback(fn (string $collection): array => match ($collection) { 'installations' => [$installation], 'repositories' => [], default => [], @@ -135,9 +138,10 @@ final class GitHubInstallationEventTest extends TestCase ->with('projects', 'project1') ->willReturn(new Document()); - $authorization->expects($this->exactly(2)) + // 1: getDocument(projects), 2: find(repositories), 3: deleteDocument(installations) + $authorization->expects($this->exactly(3)) ->method('skip') - ->willReturnCallback(fn ($fn) => $fn()); + ->willReturnCallback(fn (callable $fn): mixed => $fn()); $dbForPlatform->expects($this->once()) ->method('deleteDocument') From 1db6a43b346a92dbf61a2e35c6cb3b27d38c4ebc Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 13:08:30 +0530 Subject: [PATCH 085/153] 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 086/153] 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 cd8266af3bbcf78cc6c976b4409a26c877e528f8 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 13:18:54 +0530 Subject: [PATCH 087/153] fix: paginate outer installation lookup in installation deleted webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 115 ++++++++++-------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 15b2a4f7b7..789c01ef47 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -104,67 +104,76 @@ class Create extends Action $providerInstallationId = $parsedPayload["installationId"]; - $installations = $dbForPlatform->find('installations', [ - Query::equal('providerInstallationId', [$providerInstallationId]), - Query::limit(1000) - ]); + $installationCursor = null; + do { + $installationQueries = [ + Query::equal('providerInstallationId', [$providerInstallationId]), + Query::limit(1000), + ]; + if ($installationCursor !== null) { + $installationQueries[] = Query::cursorAfter($installationCursor); + } + $installations = $dbForPlatform->find('installations', $installationQueries); - foreach ($installations as $installation) { - $projectId = $installation->getAttribute('projectId', ''); - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); + foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); - if (!$project->isEmpty()) { - $dbForProject = $getProjectDB($project); + if (!$project->isEmpty()) { + $dbForProject = $getProjectDB($project); - foreach (['functions', 'sites'] as $collection) { - $cursor = null; - do { - $queries = [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]; - if ($cursor !== null) { - $queries[] = Query::cursorAfter($cursor); - } - $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); + foreach (['functions', 'sites'] as $collection) { + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); - foreach ($resources as $resource) { - $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', ''))); - } + foreach ($resources as $resource) { + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', ''))); + } - $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; - } while ($cursor !== null); + $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; + } while ($cursor !== null); + } } + + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); + + foreach ($repositories as $repository) { + $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + } + + $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; + } while ($cursor !== null); + + $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); } - $cursor = null; - do { - $queries = [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]; - if ($cursor !== null) { - $queries[] = Query::cursorAfter($cursor); - } - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); - - foreach ($repositories as $repository) { - $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); - } - - $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; - } while ($cursor !== null); - - $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); - } + $installationCursor = count($installations) === 1000 ? $installations[array_key_last($installations)] : null; + } while ($installationCursor !== null); } private function handlePushEvent( From 6a6741137c6dd7804207f9505e9a7dfb52dc7c9c Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Wed, 24 Jun 2026 13:22:45 +0530 Subject: [PATCH 088/153] 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 089/153] 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 317f7515bb971bfd8dd4b3b3aab54de07774f760 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 17:39:51 +0530 Subject: [PATCH 090/153] fix: wrap outer installations lookup in authorization skip --- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 2 +- tests/unit/Vcs/GitHubInstallationEventTest.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 789c01ef47..725779a969 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -113,7 +113,7 @@ class Create extends Action if ($installationCursor !== null) { $installationQueries[] = Query::cursorAfter($installationCursor); } - $installations = $dbForPlatform->find('installations', $installationQueries); + $installations = $authorization->skip(fn () => $dbForPlatform->find('installations', $installationQueries)); foreach ($installations as $installation) { $projectId = $installation->getAttribute('projectId', ''); diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php index 46582ffd61..045ac5d9d6 100644 --- a/tests/unit/Vcs/GitHubInstallationEventTest.php +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -95,9 +95,9 @@ final class GitHubInstallationEventTest extends TestCase return true; })); - // 1: getDocument(projects), 2: find(functions), 3: updateDocument(func1), - // 4: find(sites), 5: find(repositories), 6: deleteDocument(installations) - $authorization->expects($this->exactly(6)) + // 1: find(installations), 2: getDocument(projects), 3: find(functions), 4: updateDocument(func1), + // 5: find(sites), 6: find(repositories), 7: deleteDocument(installations) + $authorization->expects($this->exactly(7)) ->method('skip') ->willReturnCallback(fn (callable $fn): mixed => $fn()); @@ -138,8 +138,8 @@ final class GitHubInstallationEventTest extends TestCase ->with('projects', 'project1') ->willReturn(new Document()); - // 1: getDocument(projects), 2: find(repositories), 3: deleteDocument(installations) - $authorization->expects($this->exactly(3)) + // 1: find(installations), 2: getDocument(projects), 3: find(repositories), 4: deleteDocument(installations) + $authorization->expects($this->exactly(4)) ->method('skip') ->willReturnCallback(fn (callable $fn): mixed => $fn()); From f5bb65f50ecd9394551f2010e8fdece0de7830fb Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 17:46:58 +0530 Subject: [PATCH 091/153] fix: guard disconnectVcs against stale deployment overwriting reconnected installation --- src/Appwrite/Platform/Modules/Functions/Workers/Builds.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 748128ca8b..cceb2630b1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -298,7 +298,8 @@ class Builds extends Action try { $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { - if (\str_contains($e->getMessage(), 'Status: 404')) { + if (\str_contains($e->getMessage(), 'Status: 404') + && $resource->getAttribute('installationId', '') === $installationId) { $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } throw $e; From 0d3038ffe93f41d4af3ea47da411a89369432339 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 19:05:13 +0530 Subject: [PATCH 092/153] fix: use partial document for VCS disconnect updates --- .../Modules/Functions/Workers/Builds.php | 21 ++++++++++--------- .../Modules/VCS/Http/GitHub/Events/Create.php | 19 +++++++++-------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index cceb2630b1..d0806ccadf 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -298,7 +298,7 @@ class Builds extends Action try { $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { - if (\str_contains($e->getMessage(), 'Status: 404') + if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } @@ -1569,15 +1569,16 @@ class Builds extends Action if (!empty($repositoryId)) { $dbForPlatform->deleteDocument('repositories', $repositoryId); } - $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ])); } private function updateLatestDeployment(Database $dbForProject, Document $resource): Document diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 725779a969..53f67f34b0 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -135,15 +135,16 @@ class Create extends Action $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); foreach ($resources as $resource) { - $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', ''))); + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ]))); } $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; From 89848c21d16bc17463df74eba81dc4bb44bb1f83 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 02:19:35 +0000 Subject: [PATCH 093/153] feat: extract Resource Parser and cap usage labels to resource scope - Add Appwrite\Resource\Parser with render(), parse() and a usage cap helper. Both audit logging (api.php) and the usage label resolver in cloud now share the same template renderer. - Drop usage.resource from routes outside the in-scope resource paths (databases, storage, functions, sites). Audit-style sub-resource hierarchies are unchanged; only usage tagging is narrowed. - Add usage.resource to the resource-scoped read endpoints for databases, tablesdb, documentsdb, vectorsdb, storage, functions and sites that were missing it. --- app/controllers/shared/api.php | 46 +------ .../Account/MFA/Authenticators/Create.php | 1 - .../Account/MFA/Authenticators/Delete.php | 1 - .../Account/MFA/Authenticators/Update.php | 1 - .../Http/Account/MFA/Challenges/Create.php | 1 - .../Http/Account/MFA/Challenges/Update.php | 1 - .../Http/Account/MFA/RecoveryCodes/Create.php | 1 - .../Http/Account/MFA/RecoveryCodes/Update.php | 1 - .../Account/Http/Account/MFA/Update.php | 1 - .../Modules/Advisor/Http/Reports/Delete.php | 1 - .../Databases/Collections/Attributes/Get.php | 1 + .../Collections/Attributes/XList.php | 1 + .../Collections/Documents/Logs/XList.php | 1 + .../Http/Databases/Collections/Get.php | 1 + .../Databases/Collections/Indexes/Get.php | 1 + .../Databases/Collections/Indexes/XList.php | 1 + .../Http/Databases/Collections/Logs/XList.php | 1 + .../Http/Databases/Collections/Usage/Get.php | 1 + .../Http/Databases/Collections/XList.php | 1 + .../Modules/Databases/Http/Databases/Get.php | 1 + .../Databases/Http/Databases/Logs/XList.php | 1 + .../Http/Databases/Transactions/Get.php | 1 + .../Databases/Http/Databases/Usage/Get.php | 1 + .../DocumentsDB/Collections/Documents/Get.php | 1 + .../Collections/Documents/XList.php | 1 + .../Http/DocumentsDB/Collections/Get.php | 1 + .../DocumentsDB/Collections/Indexes/Get.php | 1 + .../DocumentsDB/Collections/Indexes/XList.php | 1 + .../DocumentsDB/Collections/Usage/Get.php | 1 + .../Http/DocumentsDB/Collections/XList.php | 1 + .../Databases/Http/DocumentsDB/Get.php | 1 + .../Databases/Http/DocumentsDB/Logs/XList.php | 1 + .../Http/DocumentsDB/Transactions/Get.php | 1 + .../Databases/Http/DocumentsDB/Usage/Get.php | 1 + .../Modules/Databases/Http/TablesDB/Get.php | 1 + .../Databases/Http/TablesDB/Logs/XList.php | 1 + .../Http/TablesDB/Tables/Columns/Get.php | 1 + .../Http/TablesDB/Tables/Columns/XList.php | 1 + .../Databases/Http/TablesDB/Tables/Get.php | 1 + .../Http/TablesDB/Tables/Indexes/Get.php | 1 + .../Http/TablesDB/Tables/Indexes/XList.php | 1 + .../Http/TablesDB/Tables/Logs/XList.php | 1 + .../Http/TablesDB/Tables/Rows/Get.php | 1 + .../Http/TablesDB/Tables/Rows/Logs/XList.php | 1 + .../Http/TablesDB/Tables/Rows/XList.php | 1 + .../Http/TablesDB/Tables/Usage/Get.php | 1 + .../Databases/Http/TablesDB/Tables/XList.php | 1 + .../Http/TablesDB/Transactions/Get.php | 1 + .../Databases/Http/TablesDB/Usage/Get.php | 1 + .../VectorsDB/Collections/Documents/Get.php | 1 + .../VectorsDB/Collections/Documents/XList.php | 1 + .../Http/VectorsDB/Collections/Get.php | 1 + .../VectorsDB/Collections/Indexes/Get.php | 1 + .../VectorsDB/Collections/Indexes/XList.php | 1 + .../Http/VectorsDB/Collections/Usage/Get.php | 1 + .../Http/VectorsDB/Collections/XList.php | 1 + .../Http/VectorsDB/Embeddings/Text/Create.php | 2 +- .../Modules/Databases/Http/VectorsDB/Get.php | 1 + .../Databases/Http/VectorsDB/Logs/XList.php | 1 + .../Http/VectorsDB/Transactions/Get.php | 1 + .../Databases/Http/VectorsDB/Usage/Get.php | 1 + .../Http/Deployments/Download/Get.php | 1 + .../Functions/Http/Deployments/Get.php | 1 + .../Functions/Http/Deployments/XList.php | 1 + .../Modules/Functions/Http/Executions/Get.php | 1 + .../Functions/Http/Executions/XList.php | 1 + .../Modules/Functions/Http/Functions/Get.php | 1 + .../Modules/Functions/Http/Templates/Get.php | 1 + .../Modules/Functions/Http/Usage/Get.php | 1 + .../Modules/Functions/Http/Variables/Get.php | 1 + .../Functions/Http/Variables/XList.php | 1 + .../Migrations/Http/Migrations/Delete.php | 1 - .../Migrations/Http/Migrations/Update.php | 1 - .../Organization/Http/Projects/Create.php | 1 - .../Organization/Http/Projects/Delete.php | 1 - .../Organization/Http/Projects/Update.php | 1 - .../Modules/Presences/HTTP/Delete.php | 1 - .../Modules/Presences/HTTP/Update.php | 1 - .../Modules/Presences/HTTP/Upsert.php | 1 - .../Http/Project/AuthMethods/Update.php | 1 - .../Modules/Project/Http/Project/Delete.php | 1 - .../Project/Http/Project/Keys/Create.php | 1 - .../Project/Http/Project/Keys/Delete.php | 1 - .../Http/Project/Keys/Ephemeral/Create.php | 1 - .../Project/Http/Project/Keys/Update.php | 1 - .../Project/Http/Project/Labels/Update.php | 1 - .../Project/Http/Project/MockPhone/Create.php | 1 - .../Project/Http/Project/MockPhone/Delete.php | 1 - .../Project/Http/Project/MockPhone/Update.php | 1 - .../Http/Project/OAuth2/Apple/Update.php | 1 - .../Http/Project/OAuth2/Auth0/Update.php | 1 - .../Http/Project/OAuth2/Authentik/Update.php | 1 - .../Project/Http/Project/OAuth2/Base.php | 1 - .../Http/Project/OAuth2/FusionAuth/Update.php | 1 - .../Http/Project/OAuth2/Gitlab/Update.php | 1 - .../Http/Project/OAuth2/Google/Update.php | 1 - .../Http/Project/OAuth2/Keycloak/Update.php | 1 - .../Http/Project/OAuth2/Microsoft/Update.php | 1 - .../Http/Project/OAuth2/Oidc/Update.php | 1 - .../Http/Project/OAuth2/Okta/Update.php | 1 - .../Http/Project/Platforms/Android/Create.php | 1 - .../Http/Project/Platforms/Android/Update.php | 1 - .../Http/Project/Platforms/Apple/Create.php | 1 - .../Http/Project/Platforms/Apple/Update.php | 1 - .../Project/Http/Project/Platforms/Delete.php | 1 - .../Http/Project/Platforms/Linux/Create.php | 1 - .../Http/Project/Platforms/Linux/Update.php | 1 - .../Http/Project/Platforms/Web/Create.php | 1 - .../Http/Project/Platforms/Web/Update.php | 1 - .../Http/Project/Platforms/Windows/Create.php | 1 - .../Http/Project/Platforms/Windows/Update.php | 1 - .../Policies/MembershipPrivacy/Update.php | 1 - .../Policies/PasswordDictionary/Update.php | 1 - .../Policies/PasswordHistory/Update.php | 1 - .../Policies/PasswordPersonalData/Update.php | 1 - .../Policies/PasswordStrength/Update.php | 1 - .../Project/Policies/SessionAlert/Update.php | 1 - .../Policies/SessionDuration/Update.php | 1 - .../Policies/SessionInvalidation/Update.php | 1 - .../Project/Policies/SessionLimit/Update.php | 1 - .../Project/Policies/UserLimit/Update.php | 1 - .../Project/Http/Project/Protocols/Update.php | 1 - .../Project/Http/Project/SMTP/Update.php | 1 - .../Project/Http/Project/Services/Update.php | 1 - .../Http/Project/Templates/Email/Update.php | 1 - .../Project/Http/Project/Variables/Create.php | 1 - .../Project/Http/Project/Variables/Delete.php | 1 - .../Project/Http/Project/Variables/Update.php | 1 - .../Modules/Projects/Http/Projects/Create.php | 1 - .../Modules/Projects/Http/Projects/Update.php | 1 - .../Projects/Http/Schedules/Create.php | 1 - .../Modules/Proxy/Http/Rules/API/Create.php | 1 - .../Modules/Proxy/Http/Rules/Delete.php | 1 - .../Proxy/Http/Rules/Function/Create.php | 1 - .../Proxy/Http/Rules/Redirect/Create.php | 1 - .../Modules/Proxy/Http/Rules/Site/Create.php | 1 - .../Proxy/Http/Rules/Status/Update.php | 1 - .../Sites/Http/Deployments/Download/Get.php | 1 + .../Modules/Sites/Http/Deployments/Get.php | 1 + .../Modules/Sites/Http/Deployments/XList.php | 1 + .../Platform/Modules/Sites/Http/Logs/Get.php | 1 + .../Modules/Sites/Http/Logs/XList.php | 1 + .../Platform/Modules/Sites/Http/Sites/Get.php | 1 + .../Modules/Sites/Http/Templates/Get.php | 1 + .../Platform/Modules/Sites/Http/Usage/Get.php | 1 + .../Modules/Sites/Http/Variables/Get.php | 1 + .../Modules/Sites/Http/Variables/XList.php | 1 + .../Http/Buckets/Files/Download/Get.php | 1 + .../Storage/Http/Buckets/Files/Get.php | 1 + .../Http/Buckets/Files/Preview/Get.php | 1 + .../Storage/Http/Buckets/Files/Push/Get.php | 1 + .../Storage/Http/Buckets/Files/View/Get.php | 1 + .../Storage/Http/Buckets/Files/XList.php | 1 + .../Modules/Storage/Http/Buckets/Get.php | 1 + .../Modules/Storage/Http/Usage/Get.php | 1 + .../Modules/Teams/Http/Memberships/Create.php | 1 - .../Modules/Teams/Http/Memberships/Delete.php | 1 - .../Teams/Http/Memberships/Status/Update.php | 1 - .../Modules/Teams/Http/Memberships/Update.php | 1 - .../Modules/Teams/Http/Preferences/Update.php | 1 - .../Modules/Teams/Http/Teams/Create.php | 1 - .../Modules/Teams/Http/Teams/Delete.php | 1 - .../Modules/Teams/Http/Teams/Name/Update.php | 1 - .../Http/Tokens/Buckets/Files/Create.php | 1 - .../Modules/Tokens/Http/Tokens/Delete.php | 1 - .../Modules/Tokens/Http/Tokens/Update.php | 1 - .../Modules/Webhooks/Http/Webhooks/Create.php | 1 - .../Modules/Webhooks/Http/Webhooks/Delete.php | 1 - .../Webhooks/Http/Webhooks/Secret/Update.php | 1 - .../Modules/Webhooks/Http/Webhooks/Update.php | 1 - src/Appwrite/Resource/Parser.php | 123 ++++++++++++++++++ 171 files changed, 209 insertions(+), 130 deletions(-) create mode 100644 src/Appwrite/Resource/Parser.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 30377334d0..12929d7ee6 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -43,45 +43,13 @@ use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; -$parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project) { - preg_match_all('/{(.*?)}/', $label, $matches); - foreach ($matches[1] as $pos => $match) { - $find = $matches[0][$pos]; - $parts = explode('.', $match); - - if (count($parts) !== 2) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "The server encountered an error while parsing the label: $label. Please create an issue on GitHub to allow us to investigate further https://github.com/appwrite/appwrite/issues/new/choose"); - } - - $namespace = $parts[0]; - $replace = $parts[1]; - - $params = match ($namespace) { - 'user' => (array) $user, - 'project' => $project->getArrayCopy(), - 'request' => $requestParams, - default => $responsePayload, - }; - - if (array_key_exists($replace, $params)) { - $replacement = $params[$replace]; - // Convert to string if it's not already a string - if (! is_string($replacement)) { - if (is_array($replacement)) { - $replacement = json_encode($replacement); - } elseif (is_object($replacement) && method_exists($replacement, '__toString')) { - $replacement = (string) $replacement; - } elseif (is_scalar($replacement)) { - $replacement = (string) $replacement; - } else { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "The server encountered an error while parsing the label: $label. Please create an issue on GitHub to allow us to investigate further https://github.com/appwrite/appwrite/issues/new/choose"); - } - } - $label = \str_replace($find, $replacement, $label); - } - } - - return $label; +$parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project): string { + return \Appwrite\Resource\Parser::render($label, [ + 'user' => (array) $user, + 'project' => $project, + 'request' => $requestParams, + 'response' => $responsePayload, + ]); }; Http::init() diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php index cdf8828ab3..6aa13e4cd0 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Create.php @@ -42,7 +42,6 @@ class Create extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{response.$id}') - ->label('usage.resource', 'user/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Delete.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Delete.php index 44bbdfd702..ce0e8d266e 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Delete.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Delete.php @@ -39,7 +39,6 @@ class Delete extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{user.$id}') - ->label('usage.resource', 'user/{user.$id}') ->label('audits.userId', '{user.$id}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php index 0bcfcfb7ba..11f044b507 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Authenticators/Update.php @@ -41,7 +41,6 @@ class Update extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{response.$id}') - ->label('usage.resource', 'user/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php index aae9bcfcd0..da6055fffd 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Create.php @@ -57,7 +57,6 @@ class Create extends Action ->label('event', 'users.[userId].challenges.[challengeId].create') ->label('audits.event', 'challenge.create') ->label('audits.resource', 'user/{response.userId}') - ->label('usage.resource', 'user/{response.userId}') ->label('audits.userId', '{response.userId}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php index 3e16aab2ab..5f6b7a1186 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Challenges/Update.php @@ -40,7 +40,6 @@ class Update extends Action ->label('event', 'users.[userId].sessions.[sessionId].create') ->label('audits.event', 'challenges.update') ->label('audits.resource', 'user/{response.userId}') - ->label('usage.resource', 'user/{response.userId}') ->label('audits.userId', '{response.userId}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Create.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Create.php index 7d7c1d8e75..0d1191aaea 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Create.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Create.php @@ -36,7 +36,6 @@ class Create extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{response.$id}') - ->label('usage.resource', 'user/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Update.php index 5d7570e61e..40051cfebc 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/RecoveryCodes/Update.php @@ -36,7 +36,6 @@ class Update extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{response.$id}') - ->label('usage.resource', 'user/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php index 6842e99d4e..a578149654 100644 --- a/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php +++ b/src/Appwrite/Platform/Modules/Account/Http/Account/MFA/Update.php @@ -36,7 +36,6 @@ class Update extends Action ->label('scope', 'account') ->label('audits.event', 'user.update') ->label('audits.resource', 'user/{response.$id}') - ->label('usage.resource', 'user/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', new Method( namespace: 'account', diff --git a/src/Appwrite/Platform/Modules/Advisor/Http/Reports/Delete.php b/src/Appwrite/Platform/Modules/Advisor/Http/Reports/Delete.php index baa53aa7ca..1efc029c17 100644 --- a/src/Appwrite/Platform/Modules/Advisor/Http/Reports/Delete.php +++ b/src/Appwrite/Platform/Modules/Advisor/Http/Reports/Delete.php @@ -38,7 +38,6 @@ class Delete extends Action ->label('resourceType', RESOURCE_TYPE_REPORTS) ->label('audits.event', 'report.delete') ->label('audits.resource', 'report/{request.reportId}') - ->label('usage.resource', 'report/{request.reportId}') ->label('abuse-key', 'projectId:{projectId},userId:{userId}') ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Get.php index 4fc251fe51..61c7760eed 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Get.php @@ -45,6 +45,7 @@ class Get extends Action ->desc('Get attribute') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/XList.php index 430117188f..6390797407 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/XList.php @@ -40,6 +40,7 @@ class XList extends Action ->desc('List attributes') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php index 976a76bba7..b9e7f846f2 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Logs/XList.php @@ -45,6 +45,7 @@ class XList extends Action ->desc('List document logs') ->groups(['api', 'database']) ->label('scope', 'documents.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Get.php index 43364413db..64667f52c0 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Get.php @@ -34,6 +34,7 @@ class Get extends Action ->desc('Get collection') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Get.php index 763fd86f7b..39e951e100 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Get.php @@ -35,6 +35,7 @@ class Get extends Action ->desc('Get index') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/XList.php index c4d70c5fb6..506b45a890 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/XList.php @@ -41,6 +41,7 @@ class XList extends Action ->desc('List indexes') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php index f2b4e1b4fa..5dfe31f3d8 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php @@ -45,6 +45,7 @@ class XList extends Action ->desc('List collection logs') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php index 2fce1a3906..fb1ced0a40 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php @@ -45,6 +45,7 @@ class Get extends Action ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/XList.php index 5d1ccbd45f..5731fd9aff 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/XList.php @@ -42,6 +42,7 @@ class XList extends Action ->desc('List collections') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Get.php index 5ee8e2f5e3..d5e2a3eabf 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Get.php @@ -28,6 +28,7 @@ class Get extends Action ->desc('Get database') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php index 1474e602e3..135f70d290 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php @@ -40,6 +40,7 @@ class XList extends Action ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Get.php index dc20349e7c..d31c2fb48b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Get.php @@ -33,6 +33,7 @@ class Get extends Action ->desc('Get transaction') ->groups(['api', 'database', 'transactions']) ->label('scope', 'rows.read') + ->label('usage.resource', 'transaction/{request.transactionId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php index ee65cbd7ff..d68e5b800b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php @@ -74,6 +74,7 @@ class Get extends DatabasesAction ->desc('Get database usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php index 47d352bf98..ec02abe2aa 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Get.php @@ -33,6 +33,7 @@ class Get extends DocumentGet ->desc('Get document') ->groups(['api', 'database']) ->label('scope', 'documents.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php index 51c0d67e8a..f2da8d262b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/XList.php @@ -35,6 +35,7 @@ class XList extends DocumentXList ->desc('List documents') ->groups(['api', 'database']) ->label('scope', 'documents.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php index 53120dd636..d112f919e2 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Get.php @@ -31,6 +31,7 @@ class Get extends CollectionGet ->desc('Get collection') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php index 7fa75b6ed9..567abaefed 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/Get.php @@ -32,6 +32,7 @@ class Get extends IndexGet ->desc('Get index') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php index 1e16155f76..93ffb8fb55 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Indexes/XList.php @@ -33,6 +33,7 @@ class XList extends IndexXList ->desc('List indexes') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php index 907ba22ae4..017f5feb11 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php @@ -39,6 +39,7 @@ class Get extends CollectionUsageGet ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php index 638244145b..2fde8aa50b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/XList.php @@ -34,6 +34,7 @@ class XList extends CollectionXList ->desc('List collections') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Get.php index 309a3b867e..d2a1f65779 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Get.php @@ -27,6 +27,7 @@ class Get extends DatabaseGet ->desc('Get database') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php index 8afb0fd1ef..1327c86474 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php @@ -30,6 +30,7 @@ class XList extends DatabaseLogs ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Transactions/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Transactions/Get.php index 7def4f0b9a..c2f4b85881 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Transactions/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Transactions/Get.php @@ -31,6 +31,7 @@ class Get extends TransactionsGet ->desc('Get transaction') ->groups(['api', 'database', 'transactions']) ->label('scope', 'documents.read') + ->label('usage.resource', 'transaction/{request.transactionId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php index e94106e31e..1ab9aeab6b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php @@ -34,6 +34,7 @@ class Get extends DatabaseUsageGet ->desc('Get DocumentsDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Get.php index 5fad045dda..a7171c9691 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Get.php @@ -27,6 +27,7 @@ class Get extends DatabaseGet ->desc('Get database') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'tablesDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php index 0df38fbf4b..68718cf1fd 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php @@ -39,6 +39,7 @@ class XList extends Action ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/Get.php index b0ef9e8a85..9885f44ca4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/Get.php @@ -43,6 +43,7 @@ class Get extends AttributesGet ->desc('Get column') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read', 'columns.read', 'attributes.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/XList.php index 56c436a13e..643ff94db4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Columns/XList.php @@ -34,6 +34,7 @@ class XList extends AttributesXList ->desc('List columns') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read', 'columns.read', 'attributes.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Get.php index ca104f05c4..f7d61f0ebe 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Get.php @@ -32,6 +32,7 @@ class Get extends CollectionGet ->desc('Get table') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/Get.php index 9918bcb2b8..96e185e01a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/Get.php @@ -33,6 +33,7 @@ class Get extends IndexGet ->desc('Get index') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read', 'indexes.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/XList.php index 5fe3be4c05..b67ea60f44 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/XList.php @@ -34,6 +34,7 @@ class XList extends IndexXList ->desc('List indexes') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read', 'indexes.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php index 62cd634f19..fd7e6d4e21 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php @@ -29,6 +29,7 @@ class XList extends CollectionLogXList ->desc('List table logs') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Get.php index 65ae238a1a..4b5b427f88 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Get.php @@ -35,6 +35,7 @@ class Get extends DocumentGet ->desc('Get row') ->groups(['api', 'database']) ->label('scope', ['rows.read', 'documents.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php index e1d821130f..9959c912c5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php @@ -29,6 +29,7 @@ class XList extends DocumentLogXList ->desc('List row logs') ->groups(['api', 'database']) ->label('scope', ['rows.read', 'documents.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/XList.php index 87e276719e..b951fec8f8 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/XList.php @@ -37,6 +37,7 @@ class XList extends DocumentXList ->desc('List rows') ->groups(['api', 'database']) ->label('scope', ['rows.read', 'documents.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php index 293db004ee..1aeae427e5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php @@ -34,6 +34,7 @@ class Get extends CollectionUsageGet ->desc('Get table usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', ['tables.read', 'collections.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/XList.php index 30ebbab409..fd8e51b467 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/XList.php @@ -35,6 +35,7 @@ class XList extends CollectionXList ->desc('List tables') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Transactions/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Transactions/Get.php index d81d3e579d..cfb621d28d 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Transactions/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Transactions/Get.php @@ -32,6 +32,7 @@ class Get extends TransactionsGet ->desc('Get transaction') ->groups(['api', 'database', 'transactions']) ->label('scope', ['documents.read', 'rows.read']) + ->label('usage.resource', 'transaction/{request.transactionId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'tablesDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php index 3f3e58dc89..2c56bf597e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php @@ -29,6 +29,7 @@ class Get extends DatabaseUsageGet ->desc('Get TablesDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', ['tables.read', 'collections.read']) + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Get.php index 73f7a55026..35dc3f178a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Get.php @@ -33,6 +33,7 @@ class Get extends DocumentGet ->desc('Get document') ->groups(['api', 'database']) ->label('scope', 'documents.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/XList.php index c9ed05ac02..1b11b282fd 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/XList.php @@ -35,6 +35,7 @@ class XList extends DocumentXList ->desc('List documents') ->groups(['api', 'database']) ->label('scope', 'documents.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Get.php index 9619bb5048..73c0a9d3dc 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Get.php @@ -31,6 +31,7 @@ class Get extends CollectionGet ->desc('Get collection') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/Get.php index 4cf646acba..3d1b492ff9 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/Get.php @@ -32,6 +32,7 @@ class Get extends IndexGet ->desc('Get index') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/XList.php index acc46fb570..8475349979 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Indexes/XList.php @@ -33,6 +33,7 @@ class XList extends IndexXList ->desc('List indexes') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php index fa28028cc1..a181b0f78d 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php @@ -38,6 +38,7 @@ class Get extends CollectionUsageGet ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/XList.php index 7ba26b8b6a..ac6a20c457 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/XList.php @@ -34,6 +34,7 @@ class XList extends CollectionXList ->desc('List collections') ->groups(['api', 'database']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Embeddings/Text/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Embeddings/Text/Create.php index 4ab3edeb70..a77cc51f1b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Embeddings/Text/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Embeddings/Text/Create.php @@ -50,7 +50,7 @@ class Create extends CreateDocumentAction ->label('resourceType', RESOURCE_TYPE_EMBEDDINGS_TEXT) ->label('audits.event', 'embedding.create') ->label('audits.resource', 'vectorsdb/embeddings/text') - ->label('usage.resource', 'vectorsdb/embeddings/text') + ->label('usage.resource', 'database/embeddings/text') ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT * 2) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Get.php index a79632b105..606fb7150a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Get.php @@ -26,6 +26,7 @@ class Get extends DatabaseGet ->desc('Get database') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php index d8c1df5f04..ef71a3f6a5 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php @@ -29,6 +29,7 @@ class XList extends DatabaseLogs ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Transactions/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Transactions/Get.php index fa4cc86cdd..5b1a227844 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Transactions/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Transactions/Get.php @@ -31,6 +31,7 @@ class Get extends TransactionsGet ->desc('Get transaction') ->groups(['api', 'database', 'transactions']) ->label('scope', 'documents.read') + ->label('usage.resource', 'transaction/{request.transactionId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php index 725e11e48b..187b093f72 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php @@ -33,6 +33,7 @@ class Get extends DatabaseUsageGet ->desc('Get VectorsDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') + ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php index 2d57ec5525..70d20fc0d9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php @@ -36,6 +36,7 @@ class Get extends Action ->groups(['api', 'functions']) ->desc('Get deployment download') ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php index 57c4421928..31a395341b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php @@ -29,6 +29,7 @@ class Get extends Action ->desc('Get deployment') ->groups(['api', 'functions']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/XList.php index e8e9ea9a18..581dea3d12 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/XList.php @@ -40,6 +40,7 @@ class XList extends Base ->desc('List deployments') ->groups(['api', 'functions']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php index 9908669f84..027ff51166 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Get.php @@ -32,6 +32,7 @@ class Get extends Base ->desc('Get execution') ->groups(['api', 'functions']) ->label('scope', ['executions.read', 'execution.read']) + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php index bf31fa0ced..29bed70cd0 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/XList.php @@ -40,6 +40,7 @@ class XList extends Base ->desc('List executions') ->groups(['api', 'functions']) ->label('scope', ['executions.read', 'execution.read']) + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Get.php index 9fab8afef8..449413cb10 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Get.php @@ -30,6 +30,7 @@ class Get extends Base ->desc('Get function') ->groups(['api', 'functions']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php index f66322839b..ab8f068521 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php @@ -31,6 +31,7 @@ class Get extends Base ->desc('Get function template') ->groups(['api', 'functions']) ->label('scope', 'public') + ->label('usage.resource', 'template/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php index def354aa2d..a3e0a003bf 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php @@ -36,6 +36,7 @@ class Get extends Base ->desc('Get function usage') ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Variables/Get.php index 13ce73e751..41decb1a48 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Variables/Get.php @@ -30,6 +30,7 @@ class Get extends Base ->desc('Get variable') ->groups(['api', 'functions']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label( 'sdk', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Variables/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Variables/XList.php index b330812b96..bf6ba67190 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Variables/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Variables/XList.php @@ -37,6 +37,7 @@ class XList extends Base ->desc('List variables') ->groups(['api', 'functions']) ->label('scope', 'functions.read') + ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label( 'sdk', diff --git a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Delete.php b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Delete.php index daad2bf634..f9c989b5bf 100644 --- a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Delete.php +++ b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Delete.php @@ -34,7 +34,6 @@ class Delete extends Action ->label('event', 'migrations.[migrationId].delete') ->label('audits.event', 'migrationId.delete') ->label('audits.resource', 'migrations/{request.migrationId}') - ->label('usage.resource', 'migrations/{request.migrationId}') ->label('sdk', new Method( namespace: 'migrations', group: null, diff --git a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Update.php b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Update.php index b900b6586b..8ecc53c2a3 100644 --- a/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Update.php +++ b/src/Appwrite/Platform/Modules/Migrations/Http/Migrations/Update.php @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'migrations.[migrationId].retry') ->label('audits.event', 'migration.retry') ->label('audits.resource', 'migrations/{request.migrationId}') - ->label('usage.resource', 'migrations/{request.migrationId}') ->label('sdk', new Method( namespace: 'migrations', group: null, diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php index 191c09e455..90c3781701 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Create.php @@ -46,7 +46,6 @@ class Create extends Action ->groups(['api', 'organization']) ->label('audits.event', 'projects.create') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('scope', 'projects.write') ->label('sdk', new Method( namespace: 'organization', diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Delete.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Delete.php index 56af8a6b8b..fc8d5cccfc 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Delete.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Delete.php @@ -35,7 +35,6 @@ class Delete extends Action ->label('scope', 'projects.write') ->label('audits.event', 'projects.delete') ->label('audits.resource', 'project/{request.projectId}') - ->label('usage.resource', 'project/{request.projectId}') ->label('sdk', new Method( namespace: 'organization', group: 'projects', diff --git a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Update.php index b178a0c549..c364a5d6df 100644 --- a/src/Appwrite/Platform/Modules/Organization/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Organization/Http/Projects/Update.php @@ -33,7 +33,6 @@ class Update extends Action ->label('scope', 'projects.write') ->label('audits.event', 'projects.update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'organization', group: 'projects', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php index 7431efd411..66ab00c3a7 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Delete.php @@ -36,7 +36,6 @@ class Delete extends PlatformAction ->label('event', 'presences.[presenceId].delete') ->label('audits.event', 'presence.delete') ->label('audits.resource', 'presence/{request.presenceId}') - ->label('usage.resource', 'presence/{request.presenceId}') ->label('sdk', new Method( namespace: 'presences', group: 'presences', diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php index 2d37818830..cd2f0d5636 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Update.php @@ -45,7 +45,6 @@ class Update extends PlatformAction ->label('event', 'presences.[presenceId].update') ->label('audits.event', 'presence.update') ->label('audits.resource', 'presence/{response.$id}') - ->label('usage.resource', 'presence/{response.$id}') ->label('sdk', [ // Client-side SDK: `userId` is not accepted (session callers can only update their own presence). new Method( diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php index 16bf5458f5..9e691e4647 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php @@ -46,7 +46,6 @@ class Upsert extends PlatformAction ->label('event', 'presences.[presenceId].upsert') ->label('audits.event', 'presence.upsert') ->label('audits.resource', 'presence/{response.$id}') - ->label('usage.resource', 'presence/{response.$id}') ->label('sdk', [ // Client-side SDK: `userId` is not accepted (session callers should just upsert their own presence). new Method( 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 c6bf5a3c7c..f5f0ae0a32 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -38,7 +38,6 @@ class Update extends Action ->label('event', 'authMethod.[methodId].update') ->label('audits.event', 'project.authMethods.[methodId].update') ->label('audits.resource', 'project.authMethods/{response.$id}') - ->label('usage.resource', 'project.authMethods/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: null, diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Delete.php index d84af6b995..201061dd62 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Delete.php @@ -36,7 +36,6 @@ class Delete extends Action ->label('scope', 'project.write') ->label('audits.event', 'project.delete') ->label('audits.resource', 'project/{project.$id}') - ->label('usage.resource', 'project/{project.$id}') ->label('sdk', new Method( namespace: 'project', group: null, diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php index 3b23bb9039..992b3532c4 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Create.php @@ -46,7 +46,6 @@ class Create extends Base ->label('event', 'keys.[keyId].create') ->label('audits.event', 'project.key.create') ->label('audits.resource', 'project.key/{response.$id}') - ->label('usage.resource', 'project.key/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'keys', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Delete.php index 2c5e3bc2ed..c5da673e22 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Delete.php @@ -38,7 +38,6 @@ class Delete extends Base ->label('event', 'keys.[keyId].delete') ->label('audits.event', 'project.key.delete') ->label('audits.resource', 'project.key/{request.keyId}') - ->label('usage.resource', 'project.key/{request.keyId}') ->label('sdk', new Method( namespace: 'project', group: 'keys', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php index 314188645c..aadf76d74f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Ephemeral/Create.php @@ -42,7 +42,6 @@ class Create extends Base ->label('event', 'keys.[keyId].create') ->label('audits.event', 'project.key.create') ->label('audits.resource', 'project.key/{response.$id}') - ->label('usage.resource', 'project.key/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'keys', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Update.php index 09d3e639d5..0d96bcf315 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Keys/Update.php @@ -44,7 +44,6 @@ class Update extends Base ->label('event', 'keys.[keyId].update') ->label('audits.event', 'project.key.update') ->label('audits.resource', 'project.key/{response.$id}') - ->label('usage.resource', 'project.key/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'keys', 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 1647f9febe..8a3506eb13 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php @@ -35,7 +35,6 @@ class Update extends Action // ->label('event', 'project.labels.update') ->label('audits.event', 'project.labels.update') ->label('audits.resource', 'project.labels/{response.$id}') - ->label('usage.resource', 'project.labels/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: null, 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 4300795d01..f4002c60ef 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -37,7 +37,6 @@ class Create extends Action ->label('event', 'mock-phones.[number].create') ->label('audits.event', 'project.mock-phone.create') ->label('audits.resource', 'project.mock-phone/{response.number}') - ->label('usage.resource', 'project.mock-phone/{response.number}') ->label('sdk', new Method( namespace: 'project', group: 'mocks', 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 14b5f662c1..0fb23e1764 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -36,7 +36,6 @@ class Delete extends Action ->label('event', 'mock-phones.[number].delete') ->label('audits.event', 'project.mock-phone.delete') ->label('audits.resource', 'project.mock-phone/{request.number}') - ->label('usage.resource', 'project.mock-phone/{request.number}') ->label('sdk', new Method( namespace: 'project', group: 'mocks', 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 99f27a2fc1..48b90a1b97 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'mock-phones.[number].update') ->label('audits.event', 'project.mock-phone.update') ->label('audits.resource', 'project.mock-phone/{response.number}') - ->label('usage.resource', 'project.mock-phone/{response.number}') ->label('sdk', new Method( namespace: 'project', group: 'mocks', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Apple/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Apple/Update.php index 25144643b9..dc9eede2b4 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Apple/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Apple/Update.php @@ -116,7 +116,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Auth0/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Auth0/Update.php index 51cd34a27d..aa5f39b213 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Auth0/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Auth0/Update.php @@ -90,7 +90,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Authentik/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Authentik/Update.php index f02dd540e9..af6b12618a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Authentik/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Authentik/Update.php @@ -90,7 +90,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', 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 8cbb2549d3..b439e7fdf7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -242,7 +242,6 @@ abstract class Base extends Action ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/FusionAuth/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/FusionAuth/Update.php index be2038d0c9..3cdf0eeb89 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/FusionAuth/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/FusionAuth/Update.php @@ -90,7 +90,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Gitlab/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Gitlab/Update.php index 3ef25071dd..804f6354ae 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Gitlab/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Gitlab/Update.php @@ -101,7 +101,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Google/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Google/Update.php index 9839350e54..5623f2bdf9 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Google/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Google/Update.php @@ -94,7 +94,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Keycloak/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Keycloak/Update.php index c7dd7ae572..aa41e8a5e9 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Keycloak/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Keycloak/Update.php @@ -96,7 +96,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Microsoft/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Microsoft/Update.php index 67be3a8be0..811819a05c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Microsoft/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Microsoft/Update.php @@ -100,7 +100,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php index 6cc170c086..697b306be8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Oidc/Update.php @@ -110,7 +110,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Okta/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Okta/Update.php index f51311ec23..0344b6a14a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Okta/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Okta/Update.php @@ -98,7 +98,6 @@ class Update extends Base ->label('event', 'oauth2.[providerId].update') ->label('audits.event', 'project.oauth2.[providerId].update') ->label('audits.resource', 'project.oauth2/{response.$id}') - ->label('usage.resource', 'project.oauth2/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'oauth2', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Create.php index 94078072e1..accc6d5b35 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Create.php @@ -39,7 +39,6 @@ class Create extends Action ->label('event', 'platforms.[platformId].create') ->label('audits.event', 'project.platform.create') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Update.php index 9b368fd849..3ff958e814 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Android/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'platforms.[platformId].update') ->label('audits.event', 'project.platform.update') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Create.php index b66687bf0c..0843bf9a0c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Create.php @@ -39,7 +39,6 @@ class Create extends Action ->label('event', 'platforms.[platformId].create') ->label('audits.event', 'project.platform.create') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Update.php index 02c5c226f8..0295075f19 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Apple/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'platforms.[platformId].update') ->label('audits.event', 'project.platform.update') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Delete.php index d580aa7c8e..24669b02b2 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Delete.php @@ -37,7 +37,6 @@ class Delete extends Action ->label('event', 'platforms.[platformId].delete') ->label('audits.event', 'project.platform.delete') ->label('audits.resource', 'project.platform/{request.platformId}') - ->label('usage.resource', 'project.platform/{request.platformId}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Create.php index b4edf8bcd7..472b41cace 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Create.php @@ -39,7 +39,6 @@ class Create extends Action ->label('event', 'platforms.[platformId].create') ->label('audits.event', 'project.platform.create') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Update.php index 47ff114562..9c1f715c33 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Linux/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'platforms.[platformId].update') ->label('audits.event', 'project.platform.update') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Create.php index ce9ff52443..6c07727150 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Create.php @@ -47,7 +47,6 @@ class Create extends Action ->label('event', 'platforms.[platformId].create') ->label('audits.event', 'project.platform.create') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Update.php index 79dc5e8184..62d209ea25 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Web/Update.php @@ -39,7 +39,6 @@ class Update extends Action ->label('event', 'platforms.[platformId].update') ->label('audits.event', 'project.platform.update') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Create.php index 3530c6ed17..58be45d03b 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Create.php @@ -39,7 +39,6 @@ class Create extends Action ->label('event', 'platforms.[platformId].create') ->label('audits.event', 'project.platform.create') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Update.php index 4d4a371f5e..5cfb6ee7ea 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Platforms/Windows/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'platforms.[platformId].update') ->label('audits.event', 'project.platform.update') ->label('audits.resource', 'project.platform/{response.$id}') - ->label('usage.resource', 'project.platform/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'platforms', 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 7ca0586e36..b167aba794 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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 bd21431793..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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 e1d040de3a..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 @@ -36,7 +36,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 f522368882..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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php index f76da811ef..0f8c566511 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 e79859e9f8..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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 f32aa82f3b..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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 68ecb22057..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 @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 541e18f636..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 @@ -36,7 +36,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 726ca237d2..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 @@ -36,7 +36,6 @@ class Update extends Action ->label('event', 'projects.[projectId].policies.[policy].update') ->label('audits.event', 'projects.[projectId].policies.[policy].update') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'policies', 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 cc7e9e5c7f..e0e4477a3c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -39,7 +39,6 @@ class Update extends Action ->label('event', 'protocols.[protocolId].update') ->label('audits.event', 'project.protocols.[protocolId].update') ->label('audits.resource', 'project.protocols/{response.$id}') - ->label('usage.resource', 'project.protocols/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: null, 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 8879facae4..dd8a469d26 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -45,7 +45,6 @@ class Update extends Action // ->label('event', 'project.smtp.update') ->label('audits.event', 'project.smtp.update') ->label('audits.resource', 'project.smtp/{response.$id}') - ->label('usage.resource', 'project.smtp/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'smtp', 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 e1e6efd768..86ef67bb01 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -39,7 +39,6 @@ class Update extends Action ->label('event', 'services.[serviceId].update') ->label('audits.event', 'project.services.[serviceId].update') ->label('audits.resource', 'project.services/{response.$id}') - ->label('usage.resource', 'project.services/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: null, 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 8e86d7d1e6..c8ec04bc76 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 @@ -42,7 +42,6 @@ class Update extends Action ->label('event', 'templates.[templateId].update') ->label('audits.event', 'project.template.update') ->label('audits.resource', 'project.template/{response.templateId}') - ->label('usage.resource', 'project.template/{response.templateId}') ->label('sdk', new Method( namespace: 'project', group: 'templates', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Create.php index 6a4bd033cf..8c76ed2a8e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Create.php @@ -38,7 +38,6 @@ class Create extends Action ->label('event', 'variables.[variableId].create') ->label('audits.event', 'project.variable.create') ->label('audits.resource', 'project.variable/{response.$id}') - ->label('usage.resource', 'project.variable/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'variables', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Delete.php index 5183bdc68e..553fb09e54 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Delete.php @@ -35,7 +35,6 @@ class Delete extends Action ->label('event', 'variables.[variableId].delete') ->label('audits.event', 'project.variable.delete') ->label('audits.resource', 'project.variable/{request.variableId}') - ->label('usage.resource', 'project.variable/{request.variableId}') ->label('sdk', new Method( namespace: 'project', group: 'variables', diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Update.php index cad912d97c..6b05e19a78 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Variables/Update.php @@ -37,7 +37,6 @@ class Update extends Action ->label('event', 'variables.[variableId].update') ->label('audits.event', 'project.variable.update') ->label('audits.resource', 'project.variable/{response.$id}') - ->label('usage.resource', 'project.variable/{response.$id}') ->label('sdk', new Method( namespace: 'project', group: 'variables', diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php index b45d0a0aae..28405fe1da 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Create.php @@ -51,7 +51,6 @@ class Create extends Action ->groups(['api', 'projects']) ->label('audits.event', 'projects.create') ->label('audits.resource', 'project/{response.$id}') - ->label('usage.resource', 'project/{response.$id}') ->label('scope', 'projects.write') ->param('projectId', '', new ProjectId(), 'Unique Id. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, and hyphen. Can\'t start with a special char. Max length is 36 chars.') ->param('name', null, new Text(128), 'Project name. Max length: 128 chars.') diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php index 0778c51661..f6df843d07 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -36,7 +36,6 @@ class Update extends Action ->label('scope', 'projects.write') ->label('audits.event', 'projects.update') ->label('audits.resource', 'project/{request.projectId}') - ->label('usage.resource', 'project/{request.projectId}') ->param('projectId', '', new UID(), 'Project unique ID.') ->param('name', null, new Text(128), 'Project name. Max length: 128 chars.') ->param('description', '', new Text(256), 'Project description. Max length: 256 chars.', true) diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php index 3ef3a6bd24..7cf13c5ca2 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php @@ -72,7 +72,6 @@ class Create extends Action ->label('event', 'schedules.[scheduleId].create') ->label('audits.event', 'schedule.create') ->label('audits.resource', 'schedule/{response.$id}') - ->label('usage.resource', 'schedule/{response.$id}') ->label('sdk', new Method( namespace: 'projects', group: 'schedules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index 56e5ad4f7c..9431d24cde 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -41,7 +41,6 @@ class Create extends Action ->label('event', 'rules.[ruleId].create') ->label('audits.event', 'rule.create') ->label('audits.resource', 'rule/{response.$id}') - ->label('usage.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php index 3d7fd02fa8..991b8eb006 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Delete.php @@ -38,7 +38,6 @@ class Delete extends Action ->label('event', 'rules.[ruleId].delete') ->label('audits.event', 'rules.delete') ->label('audits.resource', 'rule/{request.ruleId}') - ->label('usage.resource', 'rule/{request.ruleId}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php index 24cf52bc0d..7cc8b5e59e 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -43,7 +43,6 @@ class Create extends Action ->label('event', 'rules.[ruleId].create') ->label('audits.event', 'rule.create') ->label('audits.resource', 'rule/{response.$id}') - ->label('usage.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php index cfd165d5bd..c15bc3b1d9 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -45,7 +45,6 @@ class Create extends Action ->label('event', 'rules.[ruleId].create') ->label('audits.event', 'rule.create') ->label('audits.resource', 'rule/{response.$id}') - ->label('usage.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index 43de6b5699..ca45d73e13 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -43,7 +43,6 @@ class Create extends Action ->label('event', 'rules.[ruleId].create') ->label('audits.event', 'rule.create') ->label('audits.resource', 'rule/{response.$id}') - ->label('usage.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php index 711681fc67..1ad6f730b3 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Status/Update.php @@ -41,7 +41,6 @@ class Update extends Action ->label('event', 'rules.[ruleId].update') ->label('audits.event', 'rule.update') ->label('audits.resource', 'rule/{response.$id}') - ->label('usage.resource', 'rule/{response.$id}') ->label('sdk', new Method( namespace: 'proxy', group: 'rules', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php index fa84b2f22f..79ef3d2b1d 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php @@ -35,6 +35,7 @@ class Get extends Action ->desc('Get deployment download') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php index 56aaa3640b..6c6384e2a0 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php @@ -29,6 +29,7 @@ class Get extends Action ->desc('Get deployment') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/XList.php index 3dccd687ea..8dd25fdd55 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/XList.php @@ -40,6 +40,7 @@ class XList extends Base ->desc('List deployments') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php index c482add2da..3582eb1bbb 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php @@ -30,6 +30,7 @@ class Get extends Base ->desc('Get log') ->groups(['api', 'sites']) ->label('scope', 'log.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php index 89fb3ee2e6..d8c0aa79c2 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php @@ -39,6 +39,7 @@ class XList extends Base ->desc('List logs') ->groups(['api', 'sites']) ->label('scope', 'log.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Get.php index d685f2082d..508bcf0120 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Get.php @@ -30,6 +30,7 @@ class Get extends Base ->desc('Get site') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php index 633ef31e68..e204f7a543 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php @@ -31,6 +31,7 @@ class Get extends Base ->desc('Get site template') ->groups(['api']) ->label('scope', 'public') + ->label('usage.resource', 'template/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php index 73400977d1..c1b8c56a26 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php @@ -36,6 +36,7 @@ class Get extends Base ->desc('Get site usage') ->groups(['api', 'sites', 'usage']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Variables/Get.php index 2fcb051996..50e10fe4c9 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Variables/Get.php @@ -30,6 +30,7 @@ class Get extends Base ->desc('Get variable') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label( 'sdk', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Variables/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Variables/XList.php index 1270fe4925..9637c0e8e0 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Variables/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Variables/XList.php @@ -37,6 +37,7 @@ class XList extends Base ->desc('List variables') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label( 'sdk', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php index ed02a295c8..eab1366e34 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php @@ -43,6 +43,7 @@ class Get extends Action ->desc('Get file for download') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php index d997fe3cc0..67a6e81186 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php @@ -32,6 +32,7 @@ class Get extends Action ->desc('Get file') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index 2a2686b544..6a41c55f1e 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -53,6 +53,7 @@ class Get extends Action ->desc('Get file preview') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('cache', true) ->label('cache.resourceType', 'bucket/{request.bucketId}') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php index 2b2d23c1fa..76756d37b8 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php @@ -40,6 +40,7 @@ class Get extends Action ->desc('Get file for push notification') ->groups(['api', 'storage']) ->label('scope', 'public') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->param('bucketId', '', new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).') ->param('fileId', '', new UID(), 'File ID.') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php index 01e8b56fbe..0030639ab1 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php @@ -44,6 +44,7 @@ class Get extends Action ->desc('Get file for view') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/XList.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/XList.php index 28dfa87c5d..de61ea5910 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/XList.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/XList.php @@ -41,6 +41,7 @@ class XList extends Action ->desc('List files') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php index 4e75de27c8..0418ef8b45 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php @@ -32,6 +32,7 @@ class Get extends Action ->desc('Get bucket') ->groups(['api', 'storage']) ->label('scope', 'buckets.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php index 41ae0dd9ab..9d526d42dd 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php @@ -35,6 +35,7 @@ class Get extends Action ->desc('Get bucket usage stats') ->groups(['api', 'storage']) ->label('scope', 'files.read') + ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php index 9412db8268..92db118c46 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Create.php @@ -61,7 +61,6 @@ class Create extends Action ->label('auth.type', 'invites') ->label('audits.event', 'membership.create') ->label('audits.resource', 'team/{request.teamId}') - ->label('usage.resource', 'team/{request.teamId}') ->label('audits.userId', '{request.userId}') ->label('sdk', new Method( namespace: 'teams', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php index 94a7a5a047..d055ecb23f 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Delete.php @@ -38,7 +38,6 @@ class Delete extends Action ->label('scope', 'teams.write') ->label('audits.event', 'membership.delete') ->label('audits.resource', 'team/{request.teamId}') - ->label('usage.resource', 'team/{request.teamId}') ->label('sdk', new Method( namespace: 'teams', group: 'memberships', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php index 45706ad2a1..35297710af 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Status/Update.php @@ -46,7 +46,6 @@ class Update extends Action ->label('scope', 'public') ->label('audits.event', 'membership.update') ->label('audits.resource', 'team/{request.teamId}') - ->label('usage.resource', 'team/{request.teamId}') ->label('audits.userId', '{request.userId}') ->label('sdk', new Method( namespace: 'teams', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php index bb4b50c699..0d2f1e40b4 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php @@ -40,7 +40,6 @@ class Update extends Action ->label('scope', 'teams.write') ->label('audits.event', 'membership.update') ->label('audits.resource', 'team/{request.teamId}') - ->label('usage.resource', 'team/{request.teamId}') ->label('sdk', new Method( namespace: 'teams', group: 'memberships', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Preferences/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Preferences/Update.php index 22d9b7d8cf..2ec5dc9c74 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Preferences/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Preferences/Update.php @@ -36,7 +36,6 @@ class Update extends Action ->label('scope', 'teams.write') ->label('audits.event', 'team.update') ->label('audits.resource', 'team/{response.$id}') - ->label('usage.resource', 'team/{response.$id}') ->label('audits.userId', '{response.$id}') ->label('sdk', new Method( namespace: 'teams', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Create.php b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Create.php index 7b335ac48f..222b0968be 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Create.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Create.php @@ -44,7 +44,6 @@ class Create extends Action ->label('scope', 'teams.write') ->label('audits.event', 'team.create') ->label('audits.resource', 'team/{response.$id}') - ->label('usage.resource', 'team/{response.$id}') ->label('sdk', new Method( namespace: 'teams', group: 'teams', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Delete.php b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Delete.php index c7c125393e..3bae031e06 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Delete.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Delete.php @@ -38,7 +38,6 @@ class Delete extends Action ->label('scope', 'teams.write') ->label('audits.event', 'team.delete') ->label('audits.resource', 'team/{request.teamId}') - ->label('usage.resource', 'team/{request.teamId}') ->label('sdk', new Method( namespace: 'teams', group: 'teams', diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Name/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Name/Update.php index cd43f87e4c..ebe751ee1c 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Teams/Name/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Teams/Name/Update.php @@ -35,7 +35,6 @@ class Update extends Action ->label('scope', 'teams.write') ->label('audits.event', 'team.update') ->label('audits.resource', 'team/{response.$id}') - ->label('usage.resource', 'team/{response.$id}') ->label('sdk', new Method( namespace: 'teams', group: 'teams', diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php index d0f2c7bcda..1c2bd692ef 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php @@ -40,7 +40,6 @@ class Create extends Action ->label('event', 'tokens.[tokenId].create') ->label('audits.event', 'token.create') ->label('audits.resource', 'token/{response.$id}') - ->label('usage.resource', 'token/{response.$id}') ->label('usage.metric', 'tokens.{scope}.requests.create') ->label('usage.params', ['resourceId:{request.resourceId}', 'resourceType:{request.resourceType}']) ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php index 4188170367..a71f2ec726 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php @@ -33,7 +33,6 @@ class Delete extends Action ->label('event', 'tokens.[tokenId].delete') ->label('audits.event', 'tokens.delete') ->label('audits.resource', 'token/{request.tokenId}') - ->label('usage.resource', 'token/{request.tokenId}') ->label('usage.metric', 'tokens.{scope}.requests.delete') ->label('usage.params', ['tokenId:{request.tokenId}']) ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php index 924d73dfe8..444f49de62 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php @@ -35,7 +35,6 @@ class Update extends Action ->label('event', 'tokens.[tokenId].update') ->label('audits.event', 'tokens.update') ->label('audits.resource', 'tokens/{response.$id}') - ->label('usage.resource', 'tokens/{response.$id}') ->label('usage.metric', 'tokens.{scope}.requests.update') ->label('usage.params', ['tokenId:{request.tokenId}']) ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') diff --git a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Create.php b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Create.php index 5c9520442b..38196420be 100644 --- a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Create.php +++ b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Create.php @@ -47,7 +47,6 @@ class Create extends Action ->label('event', 'webhooks.[webhookId].create') ->label('audits.event', 'webhook.create') ->label('audits.resource', 'webhook/{response.$id}') - ->label('usage.resource', 'webhook/{response.$id}') ->label('sdk', new Method( namespace: 'webhooks', group: null, diff --git a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Delete.php b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Delete.php index 1d0ffe9303..cd05b6210c 100644 --- a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Delete.php +++ b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Delete.php @@ -38,7 +38,6 @@ class Delete extends Action ->label('event', 'webhooks.[webhookId].delete') ->label('audits.event', 'webhook.delete') ->label('audits.resource', 'webhook/{request.webhookId}') - ->label('usage.resource', 'webhook/{request.webhookId}') ->label('sdk', new Method( namespace: 'webhooks', group: null, diff --git a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Secret/Update.php b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Secret/Update.php index 396c147542..fbff94735e 100644 --- a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Secret/Update.php +++ b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Secret/Update.php @@ -38,7 +38,6 @@ class Update extends Action ->label('event', 'webhooks.[webhookId].update') ->label('audits.event', 'webhooks.update') ->label('audits.resource', 'webhook/{response.$id}') - ->label('usage.resource', 'webhook/{response.$id}') ->label('sdk', new Method( namespace: 'webhooks', group: null, diff --git a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Update.php b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Update.php index 0f89d90d4e..17b93812f3 100644 --- a/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Update.php +++ b/src/Appwrite/Platform/Modules/Webhooks/Http/Webhooks/Update.php @@ -44,7 +44,6 @@ class Update extends Action ->label('event', 'webhooks.[webhookId].update') ->label('audits.event', 'webhooks.update') ->label('audits.resource', 'webhook/{response.$id}') - ->label('usage.resource', 'webhook/{response.$id}') ->label('sdk', new Method( namespace: 'webhooks', group: null, diff --git a/src/Appwrite/Resource/Parser.php b/src/Appwrite/Resource/Parser.php new file mode 100644 index 0000000000..3a34e3d919 --- /dev/null +++ b/src/Appwrite/Resource/Parser.php @@ -0,0 +1,123 @@ +|Document|object> $context + */ + public static function render(string $template, array $context): string + { + if ($template === '' || !str_contains($template, '{')) { + return $template; + } + + $fallback = self::asArray($context['response'] ?? []); + + preg_match_all('/{([^}]+)}/', $template, $matches); + foreach ($matches[1] as $pos => $match) { + $find = $matches[0][$pos]; + $parts = explode('.', $match, 2); + if (count($parts) !== 2) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid resource label template: {$template}"); + } + + [$namespace, $key] = $parts; + $bag = array_key_exists($namespace, $context) ? self::asArray($context[$namespace]) : $fallback; + if (!array_key_exists($key, $bag)) { + continue; + } + + $value = $bag[$key]; + if (!is_string($value)) { + if (is_array($value)) { + $value = json_encode($value); + } elseif (is_object($value) && method_exists($value, '__toString')) { + $value = (string) $value; + } elseif (is_scalar($value)) { + $value = (string) $value; + } else { + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Cannot stringify label value: {$template}"); + } + } + + $template = str_replace($find, $value, $template); + } + + return $template; + } + + /** + * Split a rendered resource string into ordered {type, id} segments. + * Trailing odd segment (e.g. `bucket/{id}/files`) is treated as a + * collection name with no id and dropped. + * + * @return array + */ + public static function parse(string $rendered): array + { + if ($rendered === '') { + return []; + } + + $parts = explode('/', trim($rendered, '/')); + $segments = []; + for ($i = 0, $n = count($parts); $i + 1 < $n; $i += 2) { + $segments[] = ['type' => $parts[$i], 'id' => $parts[$i + 1]]; + } + return $segments; + } + + /** + * Apply the usage-warehouse granularity rule on top of {@see parse()}: + * - databases stop at `database/{databaseId}/table` + tableId + * - storage stops at `bucket` + bucketId + * - functions/sites are scalar (`function`/`site` + id) + * - everything else returns the head segment unchanged + * + * @param array $segments + * @return array{resource: string, resourceId: string} + */ + public static function capForUsage(array $segments): array + { + if ($segments === []) { + return ['resource' => '', 'resourceId' => '']; + } + + $head = $segments[0]; + if ($head['type'] === 'database' && isset($segments[1]) && $segments[1]['type'] === 'collection') { + return [ + 'resource' => 'database/' . $head['id'] . '/table', + 'resourceId' => $segments[1]['id'], + ]; + } + + return ['resource' => $head['type'], 'resourceId' => $head['id']]; + } + + /** + * @return array + */ + private static function asArray(mixed $value): array + { + if (is_array($value)) { + return $value; + } + if ($value instanceof Document) { + return $value->getArrayCopy(); + } + if (is_object($value)) { + return (array) $value; + } + return []; + } +} From 2be61b99d69ee3a880c5b331bcf9a307d6006e6b Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 02:28:16 +0000 Subject: [PATCH 094/153] fixup: relocate Parser to Event\\Resource, drop deprecated logs/usage endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move Appwrite\\Resource\\Parser → Appwrite\\Event\\Resource\\Parser so it lives alongside Event\\Context, Event\\Message, Event\\Publisher families that consume the parsed resource field. - Use ResourceParser via use-import in api.php instead of an inline FQCN. - Skip the deprecated logs/ and usage/ read endpoints across databases, tablesdb, documentsdb, vectorsdb, functions, sites and storage — these are being replaced by /v1/activities and /v1/usage in cloud. --- app/controllers/shared/api.php | 3 ++- src/Appwrite/{ => Event}/Resource/Parser.php | 2 +- .../Http/Databases/Collections/Documents/Logs/XList.php | 1 - .../Databases/Http/Databases/Collections/Logs/XList.php | 1 - .../Modules/Databases/Http/Databases/Collections/Usage/Get.php | 1 - .../Platform/Modules/Databases/Http/Databases/Logs/XList.php | 1 - .../Platform/Modules/Databases/Http/Databases/Usage/Get.php | 1 - .../Databases/Http/DocumentsDB/Collections/Usage/Get.php | 1 - .../Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php | 1 - .../Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php | 1 - .../Platform/Modules/Databases/Http/TablesDB/Logs/XList.php | 1 - .../Modules/Databases/Http/TablesDB/Tables/Logs/XList.php | 1 - .../Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php | 1 - .../Modules/Databases/Http/TablesDB/Tables/Usage/Get.php | 1 - .../Platform/Modules/Databases/Http/TablesDB/Usage/Get.php | 1 - .../Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php | 1 - .../Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php | 1 - .../Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php | 1 - src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php | 1 - src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php | 1 - 23 files changed, 3 insertions(+), 23 deletions(-) rename src/Appwrite/{ => Event}/Resource/Parser.php (99%) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 12929d7ee6..b16074cc15 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -9,6 +9,7 @@ use Appwrite\Event\Message\Audit as AuditMessage; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Usage as UsageMessage; use Appwrite\Event\Publisher\Audit; +use Appwrite\Event\Resource\Parser as ResourceParser; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; @@ -44,7 +45,7 @@ use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; $parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project): string { - return \Appwrite\Resource\Parser::render($label, [ + return ResourceParser::render($label, [ 'user' => (array) $user, 'project' => $project, 'request' => $requestParams, diff --git a/src/Appwrite/Resource/Parser.php b/src/Appwrite/Event/Resource/Parser.php similarity index 99% rename from src/Appwrite/Resource/Parser.php rename to src/Appwrite/Event/Resource/Parser.php index 3a34e3d919..6c1ba6cdd0 100644 --- a/src/Appwrite/Resource/Parser.php +++ b/src/Appwrite/Event/Resource/Parser.php @@ -1,6 +1,6 @@ desc('List document logs') ->groups(['api', 'database']) ->label('scope', 'documents.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php index 5dfe31f3d8..f2b4e1b4fa 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Logs/XList.php @@ -45,7 +45,6 @@ class XList extends Action ->desc('List collection logs') ->groups(['api', 'database']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php index fb1ced0a40..2fce1a3906 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Usage/Get.php @@ -45,7 +45,6 @@ class Get extends Action ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'databases', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php index 135f70d290..1474e602e3 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Logs/XList.php @@ -40,7 +40,6 @@ class XList extends Action ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php index d68e5b800b..ee65cbd7ff 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Usage/Get.php @@ -74,7 +74,6 @@ class Get extends DatabasesAction ->desc('Get database usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php index 017f5feb11..907ba22ae4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Usage/Get.php @@ -39,7 +39,6 @@ class Get extends CollectionUsageGet ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'documentsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php index 1327c86474..8afb0fd1ef 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Logs/XList.php @@ -30,7 +30,6 @@ class XList extends DatabaseLogs ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php index 1ab9aeab6b..e94106e31e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Usage/Get.php @@ -34,7 +34,6 @@ class Get extends DatabaseUsageGet ->desc('Get DocumentsDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php index 68718cf1fd..0df38fbf4b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Logs/XList.php @@ -39,7 +39,6 @@ class XList extends Action ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php index fd7e6d4e21..62cd634f19 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Logs/XList.php @@ -29,7 +29,6 @@ class XList extends CollectionLogXList ->desc('List table logs') ->groups(['api', 'database']) ->label('scope', ['tables.read', 'collections.read']) - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php index 9959c912c5..e1d821130f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Rows/Logs/XList.php @@ -29,7 +29,6 @@ class XList extends DocumentLogXList ->desc('List row logs') ->groups(['api', 'database']) ->label('scope', ['rows.read', 'documents.read']) - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php index 1aeae427e5..293db004ee 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Usage/Get.php @@ -34,7 +34,6 @@ class Get extends CollectionUsageGet ->desc('Get table usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', ['tables.read', 'collections.read']) - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: $this->getSDKNamespace(), diff --git a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php index 2c56bf597e..3f3e58dc89 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Usage/Get.php @@ -29,7 +29,6 @@ class Get extends DatabaseUsageGet ->desc('Get TablesDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', ['tables.read', 'collections.read']) - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php index a181b0f78d..fa28028cc1 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Usage/Get.php @@ -38,7 +38,6 @@ class Get extends CollectionUsageGet ->desc('Get collection usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', new Method( namespace: 'vectorsDB', diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php index ef71a3f6a5..d8c1df5f04 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Logs/XList.php @@ -29,7 +29,6 @@ class XList extends DatabaseLogs ->desc('List database logs') ->groups(['api', 'database']) ->label('scope', 'databases.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php index 187b093f72..725e11e48b 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Usage/Get.php @@ -33,7 +33,6 @@ class Get extends DatabaseUsageGet ->desc('Get VectorsDB usage stats') ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') - ->label('usage.resource', 'database/{request.databaseId}') ->label('resourceType', RESOURCE_TYPE_DATABASES) ->label('sdk', [ new Method( diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php index a3e0a003bf..def354aa2d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Usage/Get.php @@ -36,7 +36,6 @@ class Get extends Base ->desc('Get function usage') ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') - ->label('usage.resource', 'function/{request.functionId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php index 3582eb1bbb..c482add2da 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/Get.php @@ -30,7 +30,6 @@ class Get extends Base ->desc('Get log') ->groups(['api', 'sites']) ->label('scope', 'log.read') - ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php index d8c0aa79c2..89fb3ee2e6 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Logs/XList.php @@ -39,7 +39,6 @@ class XList extends Base ->desc('List logs') ->groups(['api', 'sites']) ->label('scope', 'log.read') - ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php index c1b8c56a26..73400977d1 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Usage/Get.php @@ -36,7 +36,6 @@ class Get extends Base ->desc('Get site usage') ->groups(['api', 'sites', 'usage']) ->label('scope', 'sites.read') - ->label('usage.resource', 'site/{request.siteId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php index 9d526d42dd..41ae0dd9ab 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Usage/Get.php @@ -35,7 +35,6 @@ class Get extends Action ->desc('Get bucket usage stats') ->groups(['api', 'storage']) ->label('scope', 'files.read') - ->label('usage.resource', 'bucket/{request.bucketId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', From 33b5f9be676bc7a7581ab2c25a4f434583715bbc Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 02:32:50 +0000 Subject: [PATCH 095/153] test(usage): cover Event\\Resource\\Parser rendering, parsing and usage cap Adds unit coverage for template substitution across request/response/user/ project namespaces, segment splitting, and the usage-warehouse cap rules that produce database/{id}/table / bucket / function / site resources. --- tests/unit/Event/Resource/ParserTest.php | 120 +++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/unit/Event/Resource/ParserTest.php diff --git a/tests/unit/Event/Resource/ParserTest.php b/tests/unit/Event/Resource/ParserTest.php new file mode 100644 index 0000000000..615095b286 --- /dev/null +++ b/tests/unit/Event/Resource/ParserTest.php @@ -0,0 +1,120 @@ + ['databaseId' => 'db1'], + 'response' => ['$id' => 'col9'], + ]); + + $this->assertSame('database/db1/collection/col9', $rendered); + } + + public function testRenderFallsBackToResponseForUnknownNamespace(): void + { + $rendered = Parser::render('thing/{unknown.id}', [ + 'response' => ['id' => 'r1'], + ]); + + $this->assertSame('thing/r1', $rendered); + } + + public function testRenderSupportsUserAndProjectNamespaces(): void + { + $rendered = Parser::render('user/{user.$id}/project/{project.$id}', [ + 'user' => ['$id' => 'usr1'], + 'project' => new Document(['$id' => 'prj1']), + ]); + + $this->assertSame('user/usr1/project/prj1', $rendered); + } + + public function testRenderLeavesUnresolvedTokensIntact(): void + { + $rendered = Parser::render('database/{request.databaseId}', [ + 'request' => [], + ]); + + $this->assertSame('database/{request.databaseId}', $rendered); + } + + public function testRenderShortCircuitsWhenNoTemplate(): void + { + $this->assertSame('database/db1', Parser::render('database/db1', [])); + $this->assertSame('', Parser::render('', [])); + } + + public function testParseSplitsIntoTypeIdSegmentsAndDropsTrailingCollectionName(): void + { + $this->assertSame( + [ + ['type' => 'database', 'id' => 'db1'], + ['type' => 'collection', 'id' => 'col1'], + ], + Parser::parse('database/db1/collection/col1/documents'), + ); + + $this->assertSame( + [['type' => 'bucket', 'id' => 'b1']], + Parser::parse('bucket/b1'), + ); + + $this->assertSame([], Parser::parse('')); + } + + public function testCapForUsageStopsAtCollectionForDatabaseResources(): void + { + $segments = Parser::parse('database/db1/collection/col1/document/doc1'); + + $this->assertSame( + ['resource' => 'database/db1/table', 'resourceId' => 'col1'], + Parser::capForUsage($segments), + ); + } + + public function testCapForUsageCollapsesStorageToBucket(): void + { + $segments = Parser::parse('bucket/b1/file/f1'); + + $this->assertSame( + ['resource' => 'bucket', 'resourceId' => 'b1'], + Parser::capForUsage($segments), + ); + } + + public function testCapForUsageTreatsFunctionsAndSitesAsScalar(): void + { + $this->assertSame( + ['resource' => 'function', 'resourceId' => 'fn1'], + Parser::capForUsage(Parser::parse('function/fn1/execution/exec1')), + ); + + $this->assertSame( + ['resource' => 'site', 'resourceId' => 's1'], + Parser::capForUsage(Parser::parse('site/s1/deployment/d1')), + ); + } + + public function testCapForUsageHandlesDatabaseWithoutCollection(): void + { + $this->assertSame( + ['resource' => 'database', 'resourceId' => 'db1'], + Parser::capForUsage(Parser::parse('database/db1')), + ); + + $this->assertSame( + ['resource' => '', 'resourceId' => ''], + Parser::capForUsage([]), + ); + } +} From 96215bf242c27e293b9549499a6bc6a220b33602 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 02:39:42 +0000 Subject: [PATCH 096/153] refactor(events): split label rendering from resource parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two responsibilities, two classes: - Appwrite\\Event\\Label\\Renderer::render() substitutes {namespace.key} placeholders inside a route label and returns the rendered string. Used by audit logging (api.php) and the cloud usage resolver to turn route labels into concrete resource paths. - Appwrite\\Event\\Resource\\Parser parses that rendered path string into ordered {type,id} segments and exposes consumer-specific projections — auditTarget() for the granular audit-log shape (deepest segment + joined parent), and capForUsage() for the container-level usage cap. Split the original combined Parser test into RendererTest and ParserTest. --- app/controllers/shared/api.php | 4 +- src/Appwrite/Event/Label/Renderer.php | 75 ++++++++++++++++ src/Appwrite/Event/Resource/Parser.php | 108 +++++++---------------- tests/unit/Event/Label/RendererTest.php | 56 ++++++++++++ tests/unit/Event/Resource/ParserTest.php | 68 +++++--------- 5 files changed, 190 insertions(+), 121 deletions(-) create mode 100644 src/Appwrite/Event/Label/Renderer.php create mode 100644 tests/unit/Event/Label/RendererTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index b16074cc15..c5306f1160 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -9,7 +9,7 @@ use Appwrite\Event\Message\Audit as AuditMessage; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Usage as UsageMessage; use Appwrite\Event\Publisher\Audit; -use Appwrite\Event\Resource\Parser as ResourceParser; +use Appwrite\Event\Label\Renderer as LabelRenderer; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; @@ -45,7 +45,7 @@ use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; $parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project): string { - return ResourceParser::render($label, [ + return LabelRenderer::render($label, [ 'user' => (array) $user, 'project' => $project, 'request' => $requestParams, diff --git a/src/Appwrite/Event/Label/Renderer.php b/src/Appwrite/Event/Label/Renderer.php new file mode 100644 index 0000000000..42c38b9614 --- /dev/null +++ b/src/Appwrite/Event/Label/Renderer.php @@ -0,0 +1,75 @@ +|Document|object> $context + */ + public static function render(string $template, array $context): string + { + if ($template === '' || !str_contains($template, '{')) { + return $template; + } + + $fallback = self::asArray($context['response'] ?? []); + + preg_match_all('/{([^}]+)}/', $template, $matches); + foreach ($matches[1] as $pos => $match) { + $find = $matches[0][$pos]; + $parts = explode('.', $match, 2); + if (count($parts) !== 2) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid resource label template: {$template}"); + } + + [$namespace, $key] = $parts; + $bag = array_key_exists($namespace, $context) ? self::asArray($context[$namespace]) : $fallback; + if (!array_key_exists($key, $bag)) { + continue; + } + + $value = $bag[$key]; + if (!is_string($value)) { + if (is_array($value)) { + $value = json_encode($value); + } elseif (is_object($value) && method_exists($value, '__toString')) { + $value = (string) $value; + } elseif (is_scalar($value)) { + $value = (string) $value; + } else { + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Cannot stringify label value: {$template}"); + } + } + + $template = str_replace($find, $value, $template); + } + + return $template; + } + + /** + * @return array + */ + private static function asArray(mixed $value): array + { + if (is_array($value)) { + return $value; + } + if ($value instanceof Document) { + return $value->getArrayCopy(); + } + if (is_object($value)) { + return (array) $value; + } + return []; + } +} diff --git a/src/Appwrite/Event/Resource/Parser.php b/src/Appwrite/Event/Resource/Parser.php index 6c1ba6cdd0..73aa4b19a5 100644 --- a/src/Appwrite/Event/Resource/Parser.php +++ b/src/Appwrite/Event/Resource/Parser.php @@ -2,64 +2,12 @@ namespace Appwrite\Event\Resource; -use Appwrite\Extend\Exception; -use Utopia\Database\Document; - class Parser { /** - * Substitute `{namespace.key}` placeholders inside a label using the - * supplied context. Supported namespaces map to the keys of `$context`; - * an unknown namespace falls back to the `response` payload to preserve - * the historical behaviour of `audits.resource` rendering. - * - * @param array|Document|object> $context - */ - public static function render(string $template, array $context): string - { - if ($template === '' || !str_contains($template, '{')) { - return $template; - } - - $fallback = self::asArray($context['response'] ?? []); - - preg_match_all('/{([^}]+)}/', $template, $matches); - foreach ($matches[1] as $pos => $match) { - $find = $matches[0][$pos]; - $parts = explode('.', $match, 2); - if (count($parts) !== 2) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid resource label template: {$template}"); - } - - [$namespace, $key] = $parts; - $bag = array_key_exists($namespace, $context) ? self::asArray($context[$namespace]) : $fallback; - if (!array_key_exists($key, $bag)) { - continue; - } - - $value = $bag[$key]; - if (!is_string($value)) { - if (is_array($value)) { - $value = json_encode($value); - } elseif (is_object($value) && method_exists($value, '__toString')) { - $value = (string) $value; - } elseif (is_scalar($value)) { - $value = (string) $value; - } else { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "Cannot stringify label value: {$template}"); - } - } - - $template = str_replace($find, $value, $template); - } - - return $template; - } - - /** - * Split a rendered resource string into ordered {type, id} segments. - * Trailing odd segment (e.g. `bucket/{id}/files`) is treated as a - * collection name with no id and dropped. + * Split a rendered resource path (e.g. `database/db1/collection/col1/document/doc1`) + * into ordered {type, id} segments. Trailing odd segment (e.g. + * `bucket/b1/files`) is treated as a collection name with no id and dropped. * * @return array */ @@ -78,11 +26,40 @@ class Parser } /** - * Apply the usage-warehouse granularity rule on top of {@see parse()}: + * Pick the audit-log target — the deepest known segment, plus the joined + * parent chain. Matches the granular shape the audit worker writes (e.g. + * type=document, id=doc1, parent=database/db1/collection/col1). + * + * @param array $segments + * @return array{type: string, id: string, parent: string} + */ + public static function auditTarget(array $segments): array + { + if ($segments === []) { + return ['type' => '', 'id' => '', 'parent' => '']; + } + + $count = count($segments); + $leaf = $segments[$count - 1]; + $parent = ''; + if ($count > 1) { + $parts = []; + for ($i = 0; $i < $count - 1; $i++) { + $parts[] = $segments[$i]['type']; + $parts[] = $segments[$i]['id']; + } + $parent = implode('/', $parts); + } + + return ['type' => $leaf['type'], 'id' => $leaf['id'], 'parent' => $parent]; + } + + /** + * Apply the usage-warehouse granularity rule: * - databases stop at `database/{databaseId}/table` + tableId * - storage stops at `bucket` + bucketId * - functions/sites are scalar (`function`/`site` + id) - * - everything else returns the head segment unchanged + * - anything else returns the head segment unchanged * * @param array $segments * @return array{resource: string, resourceId: string} @@ -103,21 +80,4 @@ class Parser return ['resource' => $head['type'], 'resourceId' => $head['id']]; } - - /** - * @return array - */ - private static function asArray(mixed $value): array - { - if (is_array($value)) { - return $value; - } - if ($value instanceof Document) { - return $value->getArrayCopy(); - } - if (is_object($value)) { - return (array) $value; - } - return []; - } } diff --git a/tests/unit/Event/Label/RendererTest.php b/tests/unit/Event/Label/RendererTest.php new file mode 100644 index 0000000000..f0bd199c63 --- /dev/null +++ b/tests/unit/Event/Label/RendererTest.php @@ -0,0 +1,56 @@ + ['databaseId' => 'db1'], + 'response' => ['$id' => 'col9'], + ]); + + $this->assertSame('database/db1/collection/col9', $rendered); + } + + public function testFallsBackToResponseForUnknownNamespace(): void + { + $rendered = Renderer::render('thing/{unknown.id}', [ + 'response' => ['id' => 'r1'], + ]); + + $this->assertSame('thing/r1', $rendered); + } + + public function testSupportsUserAndProjectNamespaces(): void + { + $rendered = Renderer::render('user/{user.$id}/project/{project.$id}', [ + 'user' => ['$id' => 'usr1'], + 'project' => new Document(['$id' => 'prj1']), + ]); + + $this->assertSame('user/usr1/project/prj1', $rendered); + } + + public function testLeavesUnresolvedTokensIntact(): void + { + $rendered = Renderer::render('database/{request.databaseId}', [ + 'request' => [], + ]); + + $this->assertSame('database/{request.databaseId}', $rendered); + } + + public function testShortCircuitsWhenNoTemplate(): void + { + $this->assertSame('database/db1', Renderer::render('database/db1', [])); + $this->assertSame('', Renderer::render('', [])); + } +} diff --git a/tests/unit/Event/Resource/ParserTest.php b/tests/unit/Event/Resource/ParserTest.php index 615095b286..cda111a941 100644 --- a/tests/unit/Event/Resource/ParserTest.php +++ b/tests/unit/Event/Resource/ParserTest.php @@ -6,54 +6,9 @@ namespace Tests\Unit\Event\Resource; use Appwrite\Event\Resource\Parser; use PHPUnit\Framework\TestCase; -use Utopia\Database\Document; final class ParserTest extends TestCase { - public function testRenderSubstitutesRequestAndResponseNamespaces(): void - { - $rendered = Parser::render('database/{request.databaseId}/collection/{response.$id}', [ - 'request' => ['databaseId' => 'db1'], - 'response' => ['$id' => 'col9'], - ]); - - $this->assertSame('database/db1/collection/col9', $rendered); - } - - public function testRenderFallsBackToResponseForUnknownNamespace(): void - { - $rendered = Parser::render('thing/{unknown.id}', [ - 'response' => ['id' => 'r1'], - ]); - - $this->assertSame('thing/r1', $rendered); - } - - public function testRenderSupportsUserAndProjectNamespaces(): void - { - $rendered = Parser::render('user/{user.$id}/project/{project.$id}', [ - 'user' => ['$id' => 'usr1'], - 'project' => new Document(['$id' => 'prj1']), - ]); - - $this->assertSame('user/usr1/project/prj1', $rendered); - } - - public function testRenderLeavesUnresolvedTokensIntact(): void - { - $rendered = Parser::render('database/{request.databaseId}', [ - 'request' => [], - ]); - - $this->assertSame('database/{request.databaseId}', $rendered); - } - - public function testRenderShortCircuitsWhenNoTemplate(): void - { - $this->assertSame('database/db1', Parser::render('database/db1', [])); - $this->assertSame('', Parser::render('', [])); - } - public function testParseSplitsIntoTypeIdSegmentsAndDropsTrailingCollectionName(): void { $this->assertSame( @@ -72,6 +27,29 @@ final class ParserTest extends TestCase $this->assertSame([], Parser::parse('')); } + public function testAuditTargetReturnsLeafAndParentChain(): void + { + $segments = Parser::parse('database/db1/collection/col1/document/doc1'); + + $this->assertSame( + ['type' => 'document', 'id' => 'doc1', 'parent' => 'database/db1/collection/col1'], + Parser::auditTarget($segments), + ); + } + + public function testAuditTargetForTopLevelResourceHasEmptyParent(): void + { + $this->assertSame( + ['type' => 'database', 'id' => 'db1', 'parent' => ''], + Parser::auditTarget(Parser::parse('database/db1')), + ); + + $this->assertSame( + ['type' => '', 'id' => '', 'parent' => ''], + Parser::auditTarget([]), + ); + } + public function testCapForUsageStopsAtCollectionForDatabaseResources(): void { $segments = Parser::parse('database/db1/collection/col1/document/doc1'); From 2697492f1b595bd3c66eb4d519490645b2ecadb0 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 02:49:15 +0000 Subject: [PATCH 097/153] fixup: drop Resource\\Parser from CE; only Label\\Renderer is shared The resource path parser is only used in cloud (StatsUsage worker / future Activity worker). Keep it co-located with the other Cloud Event classes rather than carrying it through CE just for one downstream consumer. --- src/Appwrite/Event/Resource/Parser.php | 83 -------------------- tests/unit/Event/Resource/ParserTest.php | 98 ------------------------ 2 files changed, 181 deletions(-) delete mode 100644 src/Appwrite/Event/Resource/Parser.php delete mode 100644 tests/unit/Event/Resource/ParserTest.php diff --git a/src/Appwrite/Event/Resource/Parser.php b/src/Appwrite/Event/Resource/Parser.php deleted file mode 100644 index 73aa4b19a5..0000000000 --- a/src/Appwrite/Event/Resource/Parser.php +++ /dev/null @@ -1,83 +0,0 @@ - - */ - public static function parse(string $rendered): array - { - if ($rendered === '') { - return []; - } - - $parts = explode('/', trim($rendered, '/')); - $segments = []; - for ($i = 0, $n = count($parts); $i + 1 < $n; $i += 2) { - $segments[] = ['type' => $parts[$i], 'id' => $parts[$i + 1]]; - } - return $segments; - } - - /** - * Pick the audit-log target — the deepest known segment, plus the joined - * parent chain. Matches the granular shape the audit worker writes (e.g. - * type=document, id=doc1, parent=database/db1/collection/col1). - * - * @param array $segments - * @return array{type: string, id: string, parent: string} - */ - public static function auditTarget(array $segments): array - { - if ($segments === []) { - return ['type' => '', 'id' => '', 'parent' => '']; - } - - $count = count($segments); - $leaf = $segments[$count - 1]; - $parent = ''; - if ($count > 1) { - $parts = []; - for ($i = 0; $i < $count - 1; $i++) { - $parts[] = $segments[$i]['type']; - $parts[] = $segments[$i]['id']; - } - $parent = implode('/', $parts); - } - - return ['type' => $leaf['type'], 'id' => $leaf['id'], 'parent' => $parent]; - } - - /** - * Apply the usage-warehouse granularity rule: - * - databases stop at `database/{databaseId}/table` + tableId - * - storage stops at `bucket` + bucketId - * - functions/sites are scalar (`function`/`site` + id) - * - anything else returns the head segment unchanged - * - * @param array $segments - * @return array{resource: string, resourceId: string} - */ - public static function capForUsage(array $segments): array - { - if ($segments === []) { - return ['resource' => '', 'resourceId' => '']; - } - - $head = $segments[0]; - if ($head['type'] === 'database' && isset($segments[1]) && $segments[1]['type'] === 'collection') { - return [ - 'resource' => 'database/' . $head['id'] . '/table', - 'resourceId' => $segments[1]['id'], - ]; - } - - return ['resource' => $head['type'], 'resourceId' => $head['id']]; - } -} diff --git a/tests/unit/Event/Resource/ParserTest.php b/tests/unit/Event/Resource/ParserTest.php deleted file mode 100644 index cda111a941..0000000000 --- a/tests/unit/Event/Resource/ParserTest.php +++ /dev/null @@ -1,98 +0,0 @@ -assertSame( - [ - ['type' => 'database', 'id' => 'db1'], - ['type' => 'collection', 'id' => 'col1'], - ], - Parser::parse('database/db1/collection/col1/documents'), - ); - - $this->assertSame( - [['type' => 'bucket', 'id' => 'b1']], - Parser::parse('bucket/b1'), - ); - - $this->assertSame([], Parser::parse('')); - } - - public function testAuditTargetReturnsLeafAndParentChain(): void - { - $segments = Parser::parse('database/db1/collection/col1/document/doc1'); - - $this->assertSame( - ['type' => 'document', 'id' => 'doc1', 'parent' => 'database/db1/collection/col1'], - Parser::auditTarget($segments), - ); - } - - public function testAuditTargetForTopLevelResourceHasEmptyParent(): void - { - $this->assertSame( - ['type' => 'database', 'id' => 'db1', 'parent' => ''], - Parser::auditTarget(Parser::parse('database/db1')), - ); - - $this->assertSame( - ['type' => '', 'id' => '', 'parent' => ''], - Parser::auditTarget([]), - ); - } - - public function testCapForUsageStopsAtCollectionForDatabaseResources(): void - { - $segments = Parser::parse('database/db1/collection/col1/document/doc1'); - - $this->assertSame( - ['resource' => 'database/db1/table', 'resourceId' => 'col1'], - Parser::capForUsage($segments), - ); - } - - public function testCapForUsageCollapsesStorageToBucket(): void - { - $segments = Parser::parse('bucket/b1/file/f1'); - - $this->assertSame( - ['resource' => 'bucket', 'resourceId' => 'b1'], - Parser::capForUsage($segments), - ); - } - - public function testCapForUsageTreatsFunctionsAndSitesAsScalar(): void - { - $this->assertSame( - ['resource' => 'function', 'resourceId' => 'fn1'], - Parser::capForUsage(Parser::parse('function/fn1/execution/exec1')), - ); - - $this->assertSame( - ['resource' => 'site', 'resourceId' => 's1'], - Parser::capForUsage(Parser::parse('site/s1/deployment/d1')), - ); - } - - public function testCapForUsageHandlesDatabaseWithoutCollection(): void - { - $this->assertSame( - ['resource' => 'database', 'resourceId' => 'db1'], - Parser::capForUsage(Parser::parse('database/db1')), - ); - - $this->assertSame( - ['resource' => '', 'resourceId' => ''], - Parser::capForUsage([]), - ); - } -} From 190120fd92e87794449f6f34fc9d592272db5206 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 04:12:48 +0000 Subject: [PATCH 098/153] refactor(reference): rename Event\Label\Renderer to Reference\Renderer; drop parseLabel closure The templated label is a resource reference; live the type in Appwrite\Reference. Renderer is now instance-bound to a per-request context, so api.php constructs one inside each shutdown closure that needs it and the global $parseLabel adapter goes away. --- app/controllers/shared/api.php | 34 +++++----- .../{Event/Label => Reference}/Renderer.php | 34 +++++++--- tests/unit/Event/Label/RendererTest.php | 56 ---------------- tests/unit/Reference/RendererTest.php | 67 +++++++++++++++++++ 4 files changed, 109 insertions(+), 82 deletions(-) rename src/Appwrite/{Event/Label => Reference}/Renderer.php (65%) delete mode 100644 tests/unit/Event/Label/RendererTest.php create mode 100644 tests/unit/Reference/RendererTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index c5306f1160..13a72af6a3 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -9,7 +9,6 @@ use Appwrite\Event\Message\Audit as AuditMessage; use Appwrite\Event\Message\Func as FunctionMessage; use Appwrite\Event\Message\Usage as UsageMessage; use Appwrite\Event\Publisher\Audit; -use Appwrite\Event\Label\Renderer as LabelRenderer; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Event\Realtime; @@ -19,6 +18,7 @@ use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Functions\EventProcessor; use Appwrite\Platform\Modules\Storage\Config\CacheControl; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; +use Appwrite\Reference\Renderer; use Appwrite\SDK\Method; use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; @@ -44,15 +44,6 @@ use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; -$parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project): string { - return LabelRenderer::render($label, [ - 'user' => (array) $user, - 'project' => $project, - 'request' => $requestParams, - 'response' => $responsePayload, - ]); -}; - Http::init() ->groups(['api']) ->inject('route') @@ -891,12 +882,18 @@ Http::shutdown() ->inject('auditContext') ->inject('publisherForAudits') ->inject('mode') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, User $targetUser, AuditContext $auditContext, Audit $publisherForAudits, string $mode) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, User $targetUser, AuditContext $auditContext, Audit $publisherForAudits, string $mode) { $responsePayload = $response->getPayload(); $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $params, $targetUser, $project); + $renderer = new Renderer([ + 'user' => (array) $targetUser, + 'project' => $project, + 'request' => $params, + 'response' => $responsePayload, + ]); + $resource = $renderer->render($pattern); if (! empty($resource) && $resource !== $pattern) { $auditContext->resource = $resource; } @@ -955,7 +952,7 @@ Http::shutdown() ->inject('user') ->inject('dbForProject') ->inject('authorization') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Database $dbForProject, Authorization $authorization) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Database $dbForProject, Authorization $authorization) { if (! $route->getLabel('cache', false)) { return; } @@ -985,11 +982,18 @@ Http::shutdown() $resourcePattern = $route->getLabel('cache.resource', null); $resourceTypePattern = $route->getLabel('cache.resourceType', null); + $renderer = new Renderer([ + 'user' => (array) $user, + 'project' => $project, + 'request' => $requestParams, + 'response' => $data, + ]); + try { $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, - 'resource' => empty($resourcePattern) ? null : $parseLabel($resourcePattern, $data, $requestParams, $user, $project), - 'resourceType' => empty($resourceTypePattern) ? null : $parseLabel($resourceTypePattern, $data, $requestParams, $user, $project), + 'resource' => empty($resourcePattern) ? null : $renderer->render($resourcePattern), + 'resourceType' => empty($resourceTypePattern) ? null : $renderer->render($resourceTypePattern), 'mimeType' => $response->getContentType(), 'accessedAt' => $now, 'signature' => $signature, diff --git a/src/Appwrite/Event/Label/Renderer.php b/src/Appwrite/Reference/Renderer.php similarity index 65% rename from src/Appwrite/Event/Label/Renderer.php rename to src/Appwrite/Reference/Renderer.php index 42c38b9614..e69716b407 100644 --- a/src/Appwrite/Event/Label/Renderer.php +++ b/src/Appwrite/Reference/Renderer.php @@ -1,38 +1,50 @@ > */ + private array $context; + /** - * Substitute `{namespace.key}` placeholders inside a route label using the - * supplied context. Supported namespaces map to the keys of `$context`; - * an unknown namespace falls back to the `response` payload to preserve - * the historical behaviour of `audits.resource` rendering. - * * @param array|Document|object> $context */ - public static function render(string $template, array $context): string + public function __construct(array $context) + { + $this->context = []; + foreach ($context as $namespace => $bag) { + $this->context[$namespace] = self::asArray($bag); + } + } + + public function render(string $template): string { if ($template === '' || !str_contains($template, '{')) { return $template; } - $fallback = self::asArray($context['response'] ?? []); + $fallback = $this->context['response'] ?? []; preg_match_all('/{([^}]+)}/', $template, $matches); foreach ($matches[1] as $pos => $match) { $find = $matches[0][$pos]; $parts = explode('.', $match, 2); if (count($parts) !== 2) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid resource label template: {$template}"); + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Invalid resource reference template: {$template}"); } [$namespace, $key] = $parts; - $bag = array_key_exists($namespace, $context) ? self::asArray($context[$namespace]) : $fallback; + $bag = $this->context[$namespace] ?? $fallback; if (!array_key_exists($key, $bag)) { continue; } @@ -46,7 +58,7 @@ class Renderer } elseif (is_scalar($value)) { $value = (string) $value; } else { - throw new Exception(Exception::GENERAL_SERVER_ERROR, "Cannot stringify label value: {$template}"); + throw new Exception(Exception::GENERAL_SERVER_ERROR, "Cannot stringify reference value: {$template}"); } } diff --git a/tests/unit/Event/Label/RendererTest.php b/tests/unit/Event/Label/RendererTest.php deleted file mode 100644 index f0bd199c63..0000000000 --- a/tests/unit/Event/Label/RendererTest.php +++ /dev/null @@ -1,56 +0,0 @@ - ['databaseId' => 'db1'], - 'response' => ['$id' => 'col9'], - ]); - - $this->assertSame('database/db1/collection/col9', $rendered); - } - - public function testFallsBackToResponseForUnknownNamespace(): void - { - $rendered = Renderer::render('thing/{unknown.id}', [ - 'response' => ['id' => 'r1'], - ]); - - $this->assertSame('thing/r1', $rendered); - } - - public function testSupportsUserAndProjectNamespaces(): void - { - $rendered = Renderer::render('user/{user.$id}/project/{project.$id}', [ - 'user' => ['$id' => 'usr1'], - 'project' => new Document(['$id' => 'prj1']), - ]); - - $this->assertSame('user/usr1/project/prj1', $rendered); - } - - public function testLeavesUnresolvedTokensIntact(): void - { - $rendered = Renderer::render('database/{request.databaseId}', [ - 'request' => [], - ]); - - $this->assertSame('database/{request.databaseId}', $rendered); - } - - public function testShortCircuitsWhenNoTemplate(): void - { - $this->assertSame('database/db1', Renderer::render('database/db1', [])); - $this->assertSame('', Renderer::render('', [])); - } -} diff --git a/tests/unit/Reference/RendererTest.php b/tests/unit/Reference/RendererTest.php new file mode 100644 index 0000000000..b019d0aa42 --- /dev/null +++ b/tests/unit/Reference/RendererTest.php @@ -0,0 +1,67 @@ + ['databaseId' => 'db1'], + 'response' => ['$id' => 'col9'], + ]); + + $this->assertSame( + 'database/db1/collection/col9', + $renderer->render('database/{request.databaseId}/collection/{response.$id}'), + ); + } + + public function testFallsBackToResponseForUnknownNamespace(): void + { + $renderer = new Renderer([ + 'response' => ['id' => 'r1'], + ]); + + $this->assertSame('thing/r1', $renderer->render('thing/{unknown.id}')); + } + + public function testSupportsUserAndProjectNamespaces(): void + { + $renderer = new Renderer([ + 'user' => ['$id' => 'usr1'], + 'project' => new Document(['$id' => 'prj1']), + ]); + + $this->assertSame( + 'user/usr1/project/prj1', + $renderer->render('user/{user.$id}/project/{project.$id}'), + ); + } + + public function testLeavesUnresolvedTokensIntact(): void + { + $renderer = new Renderer([ + 'request' => [], + ]); + + $this->assertSame( + 'database/{request.databaseId}', + $renderer->render('database/{request.databaseId}'), + ); + } + + public function testShortCircuitsWhenNoTemplate(): void + { + $renderer = new Renderer([]); + + $this->assertSame('database/db1', $renderer->render('database/db1')); + $this->assertSame('', $renderer->render('')); + } +} From b31bbefbb4022e1018ac3692b9a4467702cdc2c9 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 04:38:07 +0000 Subject: [PATCH 099/153] fix(usage.resource): include fileId on file-specific storage endpoints --- .../Platform/Modules/Storage/Http/Buckets/Files/Create.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/Delete.php | 2 +- .../Modules/Storage/Http/Buckets/Files/Download/Get.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/Get.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/Update.php | 2 +- .../Platform/Modules/Storage/Http/Buckets/Files/View/Get.php | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php index 680bdcff63..cf934575c0 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Create.php @@ -62,7 +62,7 @@ class Create extends Action ->label('audits.event', 'file.create') ->label('event', 'buckets.[bucketId].files.[fileId].create') ->label('audits.resource', 'file/{response.$id}') - ->label('usage.resource', 'file/{response.$id}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{response.$id}') ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId},chunkId:{chunkId}') ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Delete.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Delete.php index e2d5122305..6b631230c7 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Delete.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Delete.php @@ -42,7 +42,7 @@ class Delete extends Action ->label('event', 'buckets.[bucketId].files.[fileId].delete') ->label('audits.event', 'file.delete') ->label('audits.resource', 'file/{request.fileId}') - ->label('usage.resource', 'file/{request.fileId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php index eab1366e34..e5d82c7aa1 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php @@ -43,7 +43,7 @@ class Get extends Action ->desc('Get file for download') ->groups(['api', 'storage']) ->label('scope', 'files.read') - ->label('usage.resource', 'bucket/{request.bucketId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php index 67a6e81186..53ce6fad4f 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Get.php @@ -32,7 +32,7 @@ class Get extends Action ->desc('Get file') ->groups(['api', 'storage']) ->label('scope', 'files.read') - ->label('usage.resource', 'bucket/{request.bucketId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index 6a41c55f1e..d856c0456d 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -53,7 +53,7 @@ class Get extends Action ->desc('Get file preview') ->groups(['api', 'storage']) ->label('scope', 'files.read') - ->label('usage.resource', 'bucket/{request.bucketId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('cache', true) ->label('cache.resourceType', 'bucket/{request.bucketId}') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php index 76756d37b8..637d1b22e6 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Push/Get.php @@ -40,7 +40,7 @@ class Get extends Action ->desc('Get file for push notification') ->groups(['api', 'storage']) ->label('scope', 'public') - ->label('usage.resource', 'bucket/{request.bucketId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->param('bucketId', '', new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).') ->param('fileId', '', new UID(), 'File ID.') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Update.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Update.php index 7758493b7b..c917e21ce8 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Update.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Update.php @@ -43,7 +43,7 @@ class Update extends Action ->label('event', 'buckets.[bucketId].files.[fileId].update') ->label('audits.event', 'file.update') ->label('audits.resource', 'file/{response.$id}') - ->label('usage.resource', 'file/{response.$id}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{response.$id}') ->label('sdk', new Method( namespace: 'storage', group: 'files', diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php index 0030639ab1..8214cdbfa1 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php @@ -44,7 +44,7 @@ class Get extends Action ->desc('Get file for view') ->groups(['api', 'storage']) ->label('scope', 'files.read') - ->label('usage.resource', 'bucket/{request.bucketId}') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('resourceType', RESOURCE_TYPE_BUCKETS) ->label('sdk', new Method( namespace: 'storage', From 13fd2b946d7e637f8885853db8b1a7d3b3d6fe16 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 05:04:10 +0000 Subject: [PATCH 100/153] feat(usage.resource): tag function/site catalog routes under function/site Templates, runtimes, specifications etc. don't have a functionId or siteId in the URL but should still attribute usage to the function or site service category instead of falling back to project-level. --- .../Platform/Modules/Functions/Http/Functions/XList.php | 1 + src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php | 1 + .../Platform/Modules/Functions/Http/Specifications/XList.php | 1 + src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php | 2 +- .../Platform/Modules/Functions/Http/Templates/XList.php | 1 + src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php | 1 + src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php | 1 + .../Platform/Modules/Sites/Http/Specifications/XList.php | 1 + src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php | 2 +- src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php | 1 + 10 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php index 8e71258501..32f3775107 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php @@ -37,6 +37,7 @@ class XList extends Base ->groups(['api', 'functions']) ->desc('List functions') ->label('scope', 'functions.read') + ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php index 73700e122d..8ae46bd7cc 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php @@ -30,6 +30,7 @@ class XList extends Base ->groups(['api']) ->desc('List runtimes') ->label('scope', 'public') + ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php index 8cbff906fa..63442d0b8d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php @@ -31,6 +31,7 @@ class XList extends Base ->groups(['api', 'functions']) ->desc('List specifications') ->label('scope', 'functions.read') + ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php index ab8f068521..357926a0ea 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php @@ -31,7 +31,7 @@ class Get extends Base ->desc('Get function template') ->groups(['api', 'functions']) ->label('scope', 'public') - ->label('usage.resource', 'template/{request.templateId}') + ->label('usage.resource', 'function/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php index f334351fef..4ef79271b0 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php @@ -35,6 +35,7 @@ class XList extends Base ->desc('List templates') ->groups(['api']) ->label('scope', 'public') + ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php index b1f0a5bc1f..9a5e3b0623 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php @@ -29,6 +29,7 @@ class XList extends Base ->desc('List frameworks') ->groups(['api']) ->label('scope', 'public') + ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php index 2571043363..8d70720a70 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php @@ -37,6 +37,7 @@ class XList extends Base ->desc('List sites') ->groups(['api', 'sites']) ->label('scope', 'sites.read') + ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php index a4f865d859..abbdfbfcd0 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php @@ -31,6 +31,7 @@ class XList extends Base ->groups(['api', 'sites']) ->desc('List specifications') ->label('scope', 'sites.read') + ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php index e204f7a543..b923ab146d 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php @@ -31,7 +31,7 @@ class Get extends Base ->desc('Get site template') ->groups(['api']) ->label('scope', 'public') - ->label('usage.resource', 'template/{request.templateId}') + ->label('usage.resource', 'site/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php index 722cf986f8..335c750d13 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php @@ -34,6 +34,7 @@ class XList extends Base ->desc('List templates') ->groups(['api']) ->label('scope', 'public') + ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', From e4296c2dc713f617b1e7df1e898fa608f32b4395 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 05:22:06 +0000 Subject: [PATCH 101/153] fix(usage.resource): label file-token routes as bucket/{id}/file/{id} /v1/tokens/buckets/:bucketId/files/:fileId is semantically a storage file operation even though its URL prefix is /v1/tokens. The scope strip keyed on URL prefix and dropped the label; restore it with the file-level resource so the network usage rolls up under the file the token targets. --- .../Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php | 1 + .../Platform/Modules/Tokens/Http/Tokens/Buckets/Files/XList.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php index 1c2bd692ef..ceb73f0079 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/Create.php @@ -42,6 +42,7 @@ class Create extends Action ->label('audits.resource', 'token/{response.$id}') ->label('usage.metric', 'tokens.{scope}.requests.create') ->label('usage.params', ['resourceId:{request.resourceId}', 'resourceType:{request.resourceType}']) + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}') ->label('abuse-limit', APP_LIMIT_WRITE_RATE_DEFAULT) ->label('abuse-time', APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT) diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/XList.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/XList.php index 5a93a06f26..65ab108e65 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/XList.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Buckets/Files/XList.php @@ -37,6 +37,7 @@ class XList extends Action ->groups(['api', 'tokens']) ->label('scope', 'tokens.read') ->label('usage.metric', 'tokens.requests.read') + ->label('usage.resource', 'bucket/{request.bucketId}/file/{request.fileId}') ->label('sdk', new Method( namespace: 'tokens', group: 'files', From ba7a7113fe1e35e45c292d948293159295f02aed Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 25 Jun 2026 06:47:36 +0000 Subject: [PATCH 102/153] feat(usage): tag /v1/tokens/:tokenId by the file the token targets The Token response model only carries resourceId as a colon-joined "bucketId:fileId" string -- no separate fields to template a label off, and Delete returns 204 with no body anyway. So inject the usage context into Get/Update/Delete, split the resource id after loading the token, and set resourcePath = bucket/{bid}/file/{fid} directly. The cloud Context's setResourcePath then routes the value through the worker exactly like the labelled file-token Create/XList endpoints. CE base Context gets a no-op setResourcePath so the call compiles in CE-only deployments. --- .../Platform/Modules/Tokens/Http/Tokens/Delete.php | 12 +++++++++++- .../Platform/Modules/Tokens/Http/Tokens/Get.php | 12 +++++++++++- .../Platform/Modules/Tokens/Http/Tokens/Update.php | 12 +++++++++++- src/Appwrite/Usage/Context.php | 10 ++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php index a71f2ec726..4fca85e2a2 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Delete.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Usage\Context; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Validator\UID; @@ -58,16 +59,25 @@ class Delete extends Action ->inject('response') ->inject('dbForProject') ->inject('queueForEvents') + ->inject('usage') ->callback($this->action(...)); } - public function action(string $tokenId, Response $response, Database $dbForProject, Event $queueForEvents) + public function action(string $tokenId, Response $response, Database $dbForProject, Event $queueForEvents, Context $usage) { $token = $dbForProject->getDocument('resourceTokens', $tokenId); if ($token->isEmpty()) { throw new Exception(Exception::TOKEN_NOT_FOUND); } + if ($token->getAttribute('resourceType') === TOKENS_RESOURCE_TYPE_FILES) { + $resourceId = (string) $token->getAttribute('resourceId', ''); + [$bucketId, $fileId] = explode(':', $resourceId, 2) + ['', '']; + if ($bucketId !== '' && $fileId !== '') { + $usage->setResourcePath('bucket/' . $bucketId . '/file/' . $fileId); + } + } + $dbForProject->deleteDocument('resourceTokens', $tokenId); $queueForEvents diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Get.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Get.php index 0a364683ca..a3a540552c 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Get.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Get.php @@ -7,6 +7,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Usage\Context; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Validator\UID; @@ -50,10 +51,11 @@ class Get extends Action ->param('tokenId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Token ID.', false, ['dbForProject']) ->inject('response') ->inject('dbForProject') + ->inject('usage') ->callback($this->action(...)); } - public function action(string $tokenId, Response $response, Database $dbForProject) + public function action(string $tokenId, Response $response, Database $dbForProject, Context $usage) { $token = $dbForProject->getDocument('resourceTokens', $tokenId); @@ -61,6 +63,14 @@ class Get extends Action throw new Exception(Exception::TOKEN_NOT_FOUND); } + if ($token->getAttribute('resourceType') === TOKENS_RESOURCE_TYPE_FILES) { + $resourceId = (string) $token->getAttribute('resourceId', ''); + [$bucketId, $fileId] = explode(':', $resourceId, 2) + ['', '']; + if ($bucketId !== '' && $fileId !== '') { + $usage->setResourcePath('bucket/' . $bucketId . '/file/' . $fileId); + } + } + $response->dynamic($token, Response::MODEL_RESOURCE_TOKEN); } } diff --git a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php index 444f49de62..63b2fb098b 100644 --- a/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php +++ b/src/Appwrite/Platform/Modules/Tokens/Http/Tokens/Update.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; +use Appwrite\Usage\Context; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Validator\Datetime as DatetimeValidator; @@ -61,10 +62,11 @@ class Update extends Action ->inject('response') ->inject('dbForProject') ->inject('queueForEvents') + ->inject('usage') ->callback($this->action(...)); } - public function action(string $tokenId, ?string $expire, Response $response, Database $dbForProject, Event $queueForEvents) + public function action(string $tokenId, ?string $expire, Response $response, Database $dbForProject, Event $queueForEvents, Context $usage) { $token = $dbForProject->getDocument('resourceTokens', $tokenId); @@ -72,6 +74,14 @@ class Update extends Action throw new Exception(Exception::TOKEN_NOT_FOUND); } + if ($token->getAttribute('resourceType') === TOKENS_RESOURCE_TYPE_FILES) { + $resourceId = (string) $token->getAttribute('resourceId', ''); + [$bucketId, $fileId] = explode(':', $resourceId, 2) + ['', '']; + if ($bucketId !== '' && $fileId !== '') { + $usage->setResourcePath('bucket/' . $bucketId . '/file/' . $fileId); + } + } + $token->setAttribute('expire', $expire); $token = $dbForProject->updateDocument('resourceTokens', $tokenId, $token); diff --git a/src/Appwrite/Usage/Context.php b/src/Appwrite/Usage/Context.php index 7283cff836..f095eb4981 100644 --- a/src/Appwrite/Usage/Context.php +++ b/src/Appwrite/Usage/Context.php @@ -61,6 +61,16 @@ class Context return empty($this->metrics) && empty($this->reduce); } + public function setResourcePath(string $path): self + { + return $this; + } + + public function getResourcePath(): string + { + return ''; + } + /** * Reset the context */ From 420c0b6d49ca58d921881d3a300f6ea5ef919612 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 25 Jun 2026 19:00:14 +0530 Subject: [PATCH 103/153] fix: bump utopia-php/vcs to 4.2.4 --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 4e0de16a28..3072718613 100644 --- a/composer.lock +++ b/composer.lock @@ -5124,16 +5124,16 @@ }, { "name": "utopia-php/vcs", - "version": "4.2.3", + "version": "4.2.4", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "598cbd2793a5a6394493f1046743e75ec6544a57" + "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/598cbd2793a5a6394493f1046743e75ec6544a57", - "reference": "598cbd2793a5a6394493f1046743e75ec6544a57", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f19c61daa1e9594734d85ae25b3e90ba2b350d33", + "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33", "shasum": "" }, "require": { @@ -5167,9 +5167,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/4.2.3" + "source": "https://github.com/utopia-php/vcs/tree/4.2.4" }, - "time": "2026-06-16T12:14:08+00:00" + "time": "2026-06-25T13:27:19+00:00" }, { "name": "utopia-php/websocket", From a1308ad398bb09be7c29bab960e64acb4fbb9b45 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 14:29:54 -0400 Subject: [PATCH 104/153] Update development ports and documentation to reflect new port mappings - Changed default development port from 8080 to 9520 in .gitpod.yml and docker-compose files. - Updated various documentation files to reference the new endpoint URLs (http://localhost:9520/v1) for local development. - Adjusted port mappings for services in docker-compose.override.yml and docker-compose.yml to align with the new configuration. - Ensured all references to the Appwrite endpoint in tests and examples are updated accordingly. --- .agents/skills/self-hosted-release/SKILL.md | 2 +- .github/workflows/ci.yml | 10 +++---- .gitpod.yml | 2 +- CONTRIBUTING.md | 27 +++++++++++++++++-- docker-compose.override.yml | 6 ++--- docker-compose.yml | 27 ++++++++++++------- docs/sdks/cli/GETTING_STARTED.md | 4 +-- docs/sdks/flutter-dev/EXAMPLES.md | 2 +- docs/sdks/flutter/EXAMPLES.md | 2 +- docs/sdks/react-native/GETTING_STARTED.md | 4 +-- docs/sdks/web/GETTING_STARTED.md | 4 +-- docs/tutorials/add-translations.md | 4 +-- .../bulk-operations/LargeDocumentCreation.js | 12 ++++----- .../bulk-operations/documentCreation.js | 8 +++--- .../bulk-operations/noisyNeighbor.js | 14 +++++----- tests/benchmarks/bulk-operations/utils.js | 7 +++-- tests/benchmarks/config.js | 2 ++ .../graphql/account/graphql-full-selection.js | 3 ++- .../graphql/account/graphql-selection-set.js | 3 ++- tests/benchmarks/graphql/account/rest.js | 3 ++- .../graphql/locale/graphql-batched.js | 3 ++- .../graphql/locale/graphql-full-selection.js | 3 ++- .../graphql/locale/graphql-selection-set.js | 3 ++- .../benchmarks/graphql/locale/rest-serial.js | 13 ++++----- tests/benchmarks/graphql/locale/rest.js | 3 ++- tests/benchmarks/http-local.sh | 2 +- tests/benchmarks/http.js | 3 ++- tests/benchmarks/ws.js | 3 ++- 28 files changed, 113 insertions(+), 66 deletions(-) create mode 100644 tests/benchmarks/config.js diff --git a/.agents/skills/self-hosted-release/SKILL.md b/.agents/skills/self-hosted-release/SKILL.md index 989b8f4c1a..b4d955c104 100644 --- a/.agents/skills/self-hosted-release/SKILL.md +++ b/.agents/skills/self-hosted-release/SKILL.md @@ -41,7 +41,7 @@ Build and run the target release with the local Docker Compose stack, then test 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 +curl -fsS http://localhost:9520/v1/health/version ``` 2. Inspect runtime health before running tests: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c734df8e9..79978b9a8d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -667,7 +667,7 @@ jobs: docker tag ${{ env.IMAGE }}:before ${{ env.IMAGE }} docker compose up -d --wait --no-build for attempt in $(seq 1 10); do - if curl -fsS http://localhost/v1/health/version; then + if curl -fsS http://localhost:9520/v1/health/version; then break fi if [ "$attempt" -eq 10 ]; then @@ -684,7 +684,7 @@ jobs: continue-on-error: true uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: - APPWRITE_ENDPOINT: 'http://localhost/v1' + APPWRITE_ENDPOINT: 'http://localhost:9520/v1' APPWRITE_BENCHMARK_ITERATIONS: '5' APPWRITE_BENCHMARK_VUS: '1' APPWRITE_WORKER_TIMEOUT_MS: '120000' @@ -706,7 +706,7 @@ jobs: - name: Wait for benchmark ports if: always() run: | - for port in 80 443 8080 9503; do + for port in 9520 9521 9522 9503; do for attempt in $(seq 1 30); do if ! ss -ltn | awk '{print $4}' | grep -Eq "[:.]${port}$"; then break @@ -731,7 +731,7 @@ jobs: docker tag ${{ env.IMAGE }}:after ${{ env.IMAGE }} docker compose up -d --wait --no-build for attempt in $(seq 1 10); do - if curl -fsS http://localhost/v1/health/version; then + if curl -fsS http://localhost:9520/v1/health/version; then break fi if [ "$attempt" -eq 10 ]; then @@ -745,7 +745,7 @@ jobs: continue-on-error: true uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: - APPWRITE_ENDPOINT: 'http://localhost/v1' + APPWRITE_ENDPOINT: 'http://localhost:9520/v1' APPWRITE_BENCHMARK_ITERATIONS: '5' APPWRITE_BENCHMARK_VUS: '1' APPWRITE_WORKER_TIMEOUT_MS: '120000' diff --git a/.gitpod.yml b/.gitpod.yml index 78375bb1f0..82130fa32d 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -17,7 +17,7 @@ tasks: --prefer-dist ports: - - port: 8080 + - port: 9520 visibility: public vscode: diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5d7ab96a4f..9dd25edc27 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -281,11 +281,34 @@ Although the Appwrite API is a monolithic app, it has a very clear separation of Each container in Appwrite is a microservice on its own. Each service is an independent process that can scale without regard to any of the other services. -Currently, all the Appwrite microservices are intended to communicate using the TCP protocol over a private network. With the exception of the public-facing port 80 and 443, which by default are used to expose the Appwrite HTTP API, you should **avoid exposing any other services' ports**. +Currently, all the Appwrite microservices are intended to communicate using the TCP protocol over a private network. You should **avoid exposing services' ports** beyond what is needed for local development and debugging. ## Ports -Appwrite dev version uses ports 80 and 443 as an entry point to the Appwrite API and console. We also expose multiple ports in the range of 9500-9504 for debugging some of the Appwrite containers on dev mode. If you have any conflicts with the ports running on your system, you can easily replace them by editing Appwrite's docker-compose.yml file and executing `docker compose up -d` command. +Appwrite server dev publishes all host ports in the **9500–9522** range so the stack can run alongside other local services (including Appwrite Cloud on **9600–9699**). Access the API and console at `http://localhost:9520` (or `https://localhost:9521`). + +| Host port | Service | +|-----------|---------| +| 9520 | Traefik HTTP (API, console, dev hostnames) | +| 9521 | Traefik HTTPS | +| 9522 | Traefik HTTP (alt) | +| 9500 | Traefik dashboard | +| 9501 | Appwrite API only (direct; no console) | +| 9502 | Realtime only (direct; no console) | +| 9503 | Mail catcher | +| 9504 | Request catcher (webhook) | +| 9505 | Embedding | +| 9506 | Adminer | +| 9507 | Request catcher (SMS) | +| 9508 | Mongo Express | +| 9509 | GraphQL explorer | +| 9510 | MariaDB | +| 9511 | MongoDB | +| 9512 | PostgreSQL | +| 9513 | Redis | +| 9514 | Redis Insight | + +Containers still talk to each other on the internal Docker network using standard service ports (for example, `mariadb:3306`). The table above is only for connections from your host machine. If you still have a conflict, change the host side in `docker-compose.yml` or `docker-compose.override.yml` and run `docker compose up -d`. ## Technology Stack diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 05e31ece94..91625df148 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -10,7 +10,7 @@ services: networks: - appwrite ports: - - "8082:8081" + - "9508:8081" environment: ME_CONFIG_MONGODB_URL: "mongodb://root:${_APP_DB_ROOT_PASS}@appwrite-mongodb:27017/?replicaSet=rs0&directConnection=true" ME_CONFIG_BASICAUTH_USERNAME: ${_APP_DB_USER} @@ -76,7 +76,7 @@ services: - "traefik.http.routers.appwrite_redisinsight_https.service=appwrite_redisinsight" - "traefik.http.routers.appwrite_redisinsight_https.tls=true" ports: - - "8081:5540" + - "9514:5540" graphql-explorer: container_name: appwrite-graphql-explorer @@ -87,7 +87,7 @@ services: ports: - "9509:3000" environment: - - SERVER_URL=http://localhost/v1/graphql + - SERVER_URL=http://localhost:9520/v1/graphql configs: redisinsight-connections.json: diff --git a/docker-compose.yml b/docker-compose.yml index edeb936faa..dc328f0dc8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,6 +2,15 @@ # This is a development version of THE Appwrite docker-compose.yml file. # Avoid using this file in your production environment. # We're exposing here sensitive ports and mounting code volumes for rapid development and debugging of the server stack. +# +# Development port map (host:container) +# ----------------------------------- +# Entry points: 9520:80 (HTTP), 9521:443 (HTTPS), 9522:80 (HTTP alt) +# Reserve 9600-9699 for Appwrite Cloud dev when running both stacks locally. +# 9500 Traefik dashboard, 9501 Appwrite API, 9502 Realtime, 9503 Mail catcher +# 9504 Request catcher (webhook), 9505 Embedding, 9507 Request catcher (SMS) +# 9510 MariaDB, 9511 MongoDB, 9512 PostgreSQL, 9513 Redis +# Dev tools in docker-compose.override.yml: 9506 Adminer, 9508 Mongo Express, 9509 GraphQL, 9514 Redis Insight x-logging: &x-logging logging: @@ -36,9 +45,9 @@ services: - --entrypoints.appwrite_websecure.address=:443 - --accesslog=true ports: - - 80:80 - - 8080:80 - - 443:443 + - 9520:80 + - 9522:80 + - 9521:443 - 9500:8080 volumes: - /var/run/docker.sock:/var/run/docker.sock @@ -281,7 +290,7 @@ services: image: appwrite-dev restart: unless-stopped ports: - - 9505:80 + - 9502:80 labels: - "traefik.enable=true" - "traefik.constraint-label-stack=appwrite" @@ -1314,7 +1323,7 @@ services: volumes: - appwrite-mariadb:/var/lib/mysql:rw ports: - - "3306:3306" + - "9510:3306" environment: - MYSQL_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - MYSQL_DATABASE=${_APP_DB_SCHEMA} @@ -1341,7 +1350,7 @@ services: - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro - ./mongo-entrypoint.sh:/mongo-entrypoint.sh:ro ports: - - "27017:27017" + - "9511:27017" environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} @@ -1381,7 +1390,7 @@ services: volumes: - appwrite-postgresql:/var/lib/postgresql/18/data:rw ports: - - "5432:5432" + - "9512:5432" environment: - POSTGRES_DB=${_APP_DB_SCHEMA} - POSTGRES_USER=${_APP_DB_USER} @@ -1404,7 +1413,7 @@ services: EMBEDDING_MODELS: ${_APP_EMBEDDING_MODELS:-nomic-embed-text} container_name: appwrite-embedding ports: - - "3000:3000" + - "9505:3000" restart: unless-stopped # persistent for caching models across restarts and preloading volumes: @@ -1422,7 +1431,7 @@ services: --maxmemory-policy allkeys-lru --maxmemory-samples 5 ports: - - "6379:6379" + - "9513:6379" networks: - appwrite volumes: diff --git a/docs/sdks/cli/GETTING_STARTED.md b/docs/sdks/cli/GETTING_STARTED.md index 1cadb1bbda..8b907d298a 100644 --- a/docs/sdks/cli/GETTING_STARTED.md +++ b/docs/sdks/cli/GETTING_STARTED.md @@ -9,7 +9,7 @@ $ appwrite login ? Enter your password ******** ✓ Success ``` -This will also prompt you to enter your Appwrite endpoint ( default: http://localhost/v1 ) +This will also prompt you to enter your Appwrite endpoint ( default: http://localhost:9520/v1 ) * ### Initialising your project Once logged in, the CLI needs to be initialised before you can use it with your Appwrite project. You can do this with the `appwrite init project` command. @@ -139,5 +139,5 @@ $ appwrite client --reset The Appwrite CLI can also work in a CI environment. The initialisation of the CLI works a bit differently in CI. In CI, you set your `endpoint`, `projectId` and `API Key` using ```sh -appwrite client --endpoint http://localhost/v1 --projectId --key +appwrite client --endpoint http://localhost:9520/v1 --projectId --key ``` diff --git a/docs/sdks/flutter-dev/EXAMPLES.md b/docs/sdks/flutter-dev/EXAMPLES.md index d0cb0c2b2a..36537cbf18 100644 --- a/docs/sdks/flutter-dev/EXAMPLES.md +++ b/docs/sdks/flutter-dev/EXAMPLES.md @@ -6,7 +6,7 @@ Init your Appwrite client: Client client = Client(); client - .setEndpoint('https://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('https://localhost:9521/v1') // Your Appwrite Endpoint .setProject('5e8cf4f46b5e8') // Your project ID .setSelfSigned() // Remove in production ; diff --git a/docs/sdks/flutter/EXAMPLES.md b/docs/sdks/flutter/EXAMPLES.md index d0cb0c2b2a..36537cbf18 100644 --- a/docs/sdks/flutter/EXAMPLES.md +++ b/docs/sdks/flutter/EXAMPLES.md @@ -6,7 +6,7 @@ Init your Appwrite client: Client client = Client(); client - .setEndpoint('https://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('https://localhost:9521/v1') // Your Appwrite Endpoint .setProject('5e8cf4f46b5e8') // Your project ID .setSelfSigned() // Remove in production ; diff --git a/docs/sdks/react-native/GETTING_STARTED.md b/docs/sdks/react-native/GETTING_STARTED.md index a5c6f57c46..08b5c2f266 100644 --- a/docs/sdks/react-native/GETTING_STARTED.md +++ b/docs/sdks/react-native/GETTING_STARTED.md @@ -34,7 +34,7 @@ import { Client } from 'react-native-appwrite'; const client = new Client(); client - .setEndpoint('http://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('http://localhost:9520/v1') // Your Appwrite Endpoint .setProject('455x34dfkj') // Your project ID .setPlatform('com.example.myappwriteapp') // Your application ID or bundle ID. ; @@ -65,7 +65,7 @@ import { Client, Account } from 'react-native-appwrite'; const client = new Client(); client - .setEndpoint('http://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('http://localhost:9520/v1') // Your Appwrite Endpoint .setProject('455x34dfkj') .setPlatform('com.example.myappwriteapp') // YOUR application ID ; diff --git a/docs/sdks/web/GETTING_STARTED.md b/docs/sdks/web/GETTING_STARTED.md index cc75a3d3e5..db02a8428d 100644 --- a/docs/sdks/web/GETTING_STARTED.md +++ b/docs/sdks/web/GETTING_STARTED.md @@ -15,7 +15,7 @@ Initialize your SDK with your Appwrite server API endpoint and project ID which const client = new Client(); client - .setEndpoint('http://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('http://localhost:9520/v1') // Your Appwrite Endpoint .setProject('455x34dfkj') // Your project ID ; ``` @@ -44,7 +44,7 @@ account.create(ID.unique(), "email@example.com", "password", "Walter O'Brien") const client = new Client(); client - .setEndpoint('http://localhost/v1') // Your Appwrite Endpoint + .setEndpoint('http://localhost:9520/v1') // Your Appwrite Endpoint .setProject('455x34dfkj') ; diff --git a/docs/tutorials/add-translations.md b/docs/tutorials/add-translations.md index 68023fcf8b..ef56336799 100644 --- a/docs/tutorials/add-translations.md +++ b/docs/tutorials/add-translations.md @@ -149,13 +149,13 @@ First of all, we need to start a local instance of Appwrite. Thanks to Docker, t docker compose up -d ``` -If this is your first time running Appwrite, it may take up to a few minutes to download all images and start all containers. Once everything is started, you should see Appwrite running on `http://localhost:80`. When you visit this URL, it will ask you to sign up. After that, it will show you your empty dashboard where you need to create a new project - give it any name you want. Then you need to go to `Settings` of the project and copy `Project-ID`. +If this is your first time running Appwrite, it may take up to a few minutes to download all images and start all containers. Once everything is started, you should see Appwrite running on `http://localhost:9520`. When you visit this URL, it will ask you to sign up. After that, it will show you your empty dashboard where you need to create a new project - give it any name you want. Then you need to go to `Settings` of the project and copy `Project-ID`. If you are lost in the Appwrite dashboard, check out our [Article about Appwrite's dashboard](https://dev.to/appwrite/30daysofappwrite-appwrite-dashboard-15cc). Now, let's send the request. We will be editing headers of the request, so you will need a tool to do that, such as Postman or Insomnia. -First, let's see English translations. Let's set request type to `GET`, URL to `https://localhost/v1/locale/countries/eu` and add `X-Appwrite-Project` header. +First, let's see English translations. Let's set request type to `GET`, URL to `https://localhost:9521/v1/locale/countries/eu` and add `X-Appwrite-Project` header. ![English test](images/test-en.png) diff --git a/tests/benchmarks/bulk-operations/LargeDocumentCreation.js b/tests/benchmarks/bulk-operations/LargeDocumentCreation.js index c903ed2dba..138c68c4d1 100644 --- a/tests/benchmarks/bulk-operations/LargeDocumentCreation.js +++ b/tests/benchmarks/bulk-operations/LargeDocumentCreation.js @@ -1,6 +1,6 @@ import { check, sleep } from "k6"; import http from "k6/http"; -import { provisionProject, provisionDatabase, cleanup, unique } from "./utils.js"; +import { provisionProject, provisionDatabase, cleanup, unique, ENDPOINT } from "./utils.js"; const millionRecords = 1_000_000; const batchSize = 10_000; @@ -8,7 +8,7 @@ const numBatches = millionRecords / batchSize; export function setup() { const resources = provisionProject({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, email: 'test@test.com', password: 'password123', name: 'Test User', @@ -16,7 +16,7 @@ export function setup() { }); const { databaseId, collectionId } = provisionDatabase({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, apiHeaders: resources.apiHeaders }); @@ -31,7 +31,7 @@ export function setup() { attributes: ["name", "email"] }; - const indexRes = http.post(`http://localhost/v1/databases/${databaseId}/collections/${collectionId}/indexes`, + const indexRes = http.post(`${ENDPOINT}/databases/${databaseId}/collections/${collectionId}/indexes`, JSON.stringify(index), { headers: resources.apiHeaders }); @@ -57,7 +57,7 @@ export function setup() { })); requests.push({ method: "POST", - url: `http://localhost/v1/databases/${databaseId}/collections/${collectionId}/documents`, + url: `${ENDPOINT}/databases/${databaseId}/collections/${collectionId}/documents`, body: JSON.stringify({ documents: docs }), params: { headers: resources.apiHeaders, @@ -96,7 +96,7 @@ export default function (data) { const payload = JSON.stringify({ documents: docs }); const res = http.post( - `http://localhost/v1/databases/${data.databaseId}/collections/${data.collectionId}/documents`, + `${ENDPOINT}/databases/${data.databaseId}/collections/${data.collectionId}/documents`, payload, { headers: data.apiHeaders, diff --git a/tests/benchmarks/bulk-operations/documentCreation.js b/tests/benchmarks/bulk-operations/documentCreation.js index 730b6bb541..94ba690c0f 100644 --- a/tests/benchmarks/bulk-operations/documentCreation.js +++ b/tests/benchmarks/bulk-operations/documentCreation.js @@ -1,12 +1,12 @@ import { check, sleep } from "k6"; import http from "k6/http"; -import { provisionProject, provisionDatabase, cleanup, unique } from "./utils.js"; +import { provisionProject, provisionDatabase, cleanup, unique, ENDPOINT } from "./utils.js"; const amount = 10_000; export function setup() { const resources = provisionProject({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, email: 'test@test.com', password: 'password123', name: 'Test User', @@ -14,7 +14,7 @@ export function setup() { }); const { databaseId, collectionId } = provisionDatabase({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, apiHeaders: resources.apiHeaders }); @@ -54,7 +54,7 @@ export default function (data) { }); const res = http.post( - `http://localhost/v1/databases/${data.databaseId}/collections/${data.collectionId}/documents`, + `${ENDPOINT}/databases/${data.databaseId}/collections/${data.collectionId}/documents`, payload, { headers: data.apiHeaders diff --git a/tests/benchmarks/bulk-operations/noisyNeighbor.js b/tests/benchmarks/bulk-operations/noisyNeighbor.js index 4bfec4e564..3cb4c62510 100644 --- a/tests/benchmarks/bulk-operations/noisyNeighbor.js +++ b/tests/benchmarks/bulk-operations/noisyNeighbor.js @@ -1,7 +1,7 @@ import { check, sleep } from "k6"; import http from "k6/http"; import { Trend } from "k6/metrics"; -import { provisionProject, provisionDatabase, cleanup, unique } from "./utils.js"; +import { provisionProject, provisionDatabase, cleanup, unique, ENDPOINT } from "./utils.js"; // Custom Trend metric for light response time tracking export const lightResponseTime = new Trend("light_response_time", true); @@ -12,7 +12,7 @@ const LIGHT_AMOUNT = 10; // Light operation amount export function setup() { // Set up two separate projects - one for bulk operations (noisy neighbor) and one for light operations const heavyResources = provisionProject({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, email: 'heavy@test.com', password: 'password123', name: 'Heavy User', @@ -20,7 +20,7 @@ export function setup() { }); const lightResources = provisionProject({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, email: 'light@test.com', password: 'password123', name: 'Light User', @@ -29,12 +29,12 @@ export function setup() { // Set up databases for both projects const heavy = provisionDatabase({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, apiHeaders: heavyResources.apiHeaders }); const light = provisionDatabase({ - endpoint: 'http://localhost/v1', + endpoint: ENDPOINT, apiHeaders: lightResources.apiHeaders }); @@ -84,7 +84,7 @@ export function heavy(data) { const payload = JSON.stringify({ documents }); const res = http.post( - `http://localhost/v1/databases/${data.heavy.databaseId}/collections/${data.heavy.collectionId}/documents`, + `${ENDPOINT}/databases/${data.heavy.databaseId}/collections/${data.heavy.collectionId}/documents`, payload, { headers: data.heavy.apiHeaders @@ -103,7 +103,7 @@ export function light(data) { const startTime = new Date(); const res = http.post( - `http://localhost/v1/databases/${data.light.databaseId}/collections/${data.light.collectionId}/documents`, + `${ENDPOINT}/databases/${data.light.databaseId}/collections/${data.light.collectionId}/documents`, payload, { headers: data.light.apiHeaders diff --git a/tests/benchmarks/bulk-operations/utils.js b/tests/benchmarks/bulk-operations/utils.js index 5b8bbc6c67..56771e7acc 100644 --- a/tests/benchmarks/bulk-operations/utils.js +++ b/tests/benchmarks/bulk-operations/utils.js @@ -1,5 +1,8 @@ import http from 'k6/http'; import { check } from 'k6'; +import { ENDPOINT } from '../config.js'; + +export { ENDPOINT }; /** * @typedef {Object} AuthHeaders @@ -47,7 +50,7 @@ function assert(response, checkName, condition) { * - API Key creation * * @param {Object} config Configuration object - * @param {string} config.endpoint Base endpoint URL (e.g., 'http://localhost:80/v1') + * @param {string} config.endpoint Base endpoint URL (e.g., 'http://localhost:9520/v1') * @param {string} config.email Email for account creation * @param {string} config.password Password for account creation * @param {string} config.name Name for account creation @@ -165,7 +168,7 @@ export function provisionProject(config) { * Example usage: * * const config = { - * endpoint: 'http://localhost:80/v1', + * endpoint: 'http://localhost:9520/v1', * email: 'test@example.com', * password: 'complex-password', * name: 'Test User', diff --git a/tests/benchmarks/config.js b/tests/benchmarks/config.js new file mode 100644 index 0000000000..cb7b22677a --- /dev/null +++ b/tests/benchmarks/config.js @@ -0,0 +1,2 @@ +export const ENDPOINT = (__ENV.APPWRITE_ENDPOINT || 'http://localhost:9520/v1').replace(/\/+$/, ''); +export const REALTIME_URL = (__ENV.APPWRITE_REALTIME_URL || `${ENDPOINT.replace(/^http/, 'ws')}/realtime`).replace(/\/+$/, ''); diff --git a/tests/benchmarks/graphql/account/graphql-full-selection.js b/tests/benchmarks/graphql/account/graphql-full-selection.js index 84d41328d6..85521547c1 100644 --- a/tests/benchmarks/graphql/account/graphql-full-selection.js +++ b/tests/benchmarks/graphql/account/graphql-full-selection.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.post('http://localhost/v1/graphql', JSON.stringify({ + http.post(`${ENDPOINT}/graphql`, JSON.stringify({ query: `query { accountGet { _id diff --git a/tests/benchmarks/graphql/account/graphql-selection-set.js b/tests/benchmarks/graphql/account/graphql-selection-set.js index fd605121e9..befc161fbc 100644 --- a/tests/benchmarks/graphql/account/graphql-selection-set.js +++ b/tests/benchmarks/graphql/account/graphql-selection-set.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.post('http://localhost/v1/graphql', JSON.stringify({ + http.post(`${ENDPOINT}/graphql`, JSON.stringify({ query: `query { accountGet { _id email } }` }), { headers: { diff --git a/tests/benchmarks/graphql/account/rest.js b/tests/benchmarks/graphql/account/rest.js index 04037c28f1..2f60892b09 100644 --- a/tests/benchmarks/graphql/account/rest.js +++ b/tests/benchmarks/graphql/account/rest.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.get('http://localhost/v1/account', { + http.get(`${ENDPOINT}/account`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', diff --git a/tests/benchmarks/graphql/locale/graphql-batched.js b/tests/benchmarks/graphql/locale/graphql-batched.js index b2c633d49a..0dfd933f6d 100644 --- a/tests/benchmarks/graphql/locale/graphql-batched.js +++ b/tests/benchmarks/graphql/locale/graphql-batched.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.post('http://localhost/v1/graphql', JSON.stringify([{ + http.post(`${ENDPOINT}/graphql`, JSON.stringify([{ query: 'query { localeGetCountries { total countries { code } } }', }, { query: 'query { localeGetContinents { total continents { code } } }', diff --git a/tests/benchmarks/graphql/locale/graphql-full-selection.js b/tests/benchmarks/graphql/locale/graphql-full-selection.js index d66d6c44a7..219a78d0e9 100644 --- a/tests/benchmarks/graphql/locale/graphql-full-selection.js +++ b/tests/benchmarks/graphql/locale/graphql-full-selection.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.post('http://localhost/v1/graphql', JSON.stringify({ + http.post(`${ENDPOINT}/graphql`, JSON.stringify({ query: 'query { localeGetCountries { total countries { code name } } }', }), { headers: { diff --git a/tests/benchmarks/graphql/locale/graphql-selection-set.js b/tests/benchmarks/graphql/locale/graphql-selection-set.js index 46c6dcaa53..59f00e47e3 100644 --- a/tests/benchmarks/graphql/locale/graphql-selection-set.js +++ b/tests/benchmarks/graphql/locale/graphql-selection-set.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.post('http://localhost/v1/graphql', JSON.stringify({ + http.post(`${ENDPOINT}/graphql`, JSON.stringify({ query: 'query { localeGetCountries { total } }', }), { headers: { diff --git a/tests/benchmarks/graphql/locale/rest-serial.js b/tests/benchmarks/graphql/locale/rest-serial.js index 22f0c48ab0..acad4bcbc9 100644 --- a/tests/benchmarks/graphql/locale/rest-serial.js +++ b/tests/benchmarks/graphql/locale/rest-serial.js @@ -1,37 +1,38 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.get('http://localhost/v1/locale/countries', { + http.get(`${ENDPOINT}/locale/countries`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', } }) - http.get('http://localhost/v1/locale/continents', { + http.get(`${ENDPOINT}/locale/continents`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', } }) - http.get('http://localhost/v1/locale/countries/eu', { + http.get(`${ENDPOINT}/locale/countries/eu`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', } }) - http.get('http://localhost/v1/locale/countries/phones', { + http.get(`${ENDPOINT}/locale/countries/phones`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', } }) - http.get('http://localhost/v1/locale/languages', { + http.get(`${ENDPOINT}/locale/languages`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', } }) -} \ No newline at end of file +} diff --git a/tests/benchmarks/graphql/locale/rest.js b/tests/benchmarks/graphql/locale/rest.js index 0c9c57355f..d830fe58dc 100644 --- a/tests/benchmarks/graphql/locale/rest.js +++ b/tests/benchmarks/graphql/locale/rest.js @@ -1,10 +1,11 @@ import http from 'k6/http'; +import { ENDPOINT } from '../../config.js'; export const options = { vus: 20, duration: '60s', }; export default function () { - http.get('http://localhost/v1/locale/countries', { + http.get(`${ENDPOINT}/locale/countries`, { headers: { 'Content-Type': 'application/json', 'X-Appwrite-Project': 'test', diff --git a/tests/benchmarks/http-local.sh b/tests/benchmarks/http-local.sh index 734c825fda..15c4b64cbe 100755 --- a/tests/benchmarks/http-local.sh +++ b/tests/benchmarks/http-local.sh @@ -5,7 +5,7 @@ export K6_WEB_DASHBOARD="${K6_WEB_DASHBOARD:-true}" export K6_WEB_DASHBOARD_HOST="${K6_WEB_DASHBOARD_HOST:-127.0.0.1}" export K6_WEB_DASHBOARD_PORT="${K6_WEB_DASHBOARD_PORT:-5665}" export K6_WEB_DASHBOARD_EXPORT="${K6_WEB_DASHBOARD_EXPORT:-/tmp/appwrite-k6-report.html}" -export APPWRITE_ENDPOINT="${APPWRITE_ENDPOINT:-http://localhost/v1}" +export APPWRITE_ENDPOINT="${APPWRITE_ENDPOINT:-http://localhost:9520/v1}" export APPWRITE_WORKER_TIMEOUT_MS="${APPWRITE_WORKER_TIMEOUT_MS:-120000}" export APPWRITE_BENCHMARK_SUMMARY_PATH="${APPWRITE_BENCHMARK_SUMMARY_PATH:-/tmp/appwrite-k6-summary.json}" diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index ef42e99663..1e357f7dc9 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -1,3 +1,5 @@ +import { ENDPOINT } from './config.js'; + /* * Run locally: * Requires k6 and a running Appwrite instance. @@ -11,7 +13,6 @@ import { check, group, sleep } from 'k6'; import encoding from 'k6/encoding'; import { Counter, Trend } from 'k6/metrics'; -const ENDPOINT = (__ENV.APPWRITE_ENDPOINT || 'http://localhost/v1').replace(/\/+$/, ''); const CONSOLE_PROJECT = __ENV.APPWRITE_CONSOLE_PROJECT || 'console'; const REGION = __ENV.APPWRITE_REGION || 'default'; const REDIRECT_URL = __ENV.APPWRITE_BENCHMARK_REDIRECT_URL || 'http://localhost'; diff --git a/tests/benchmarks/ws.js b/tests/benchmarks/ws.js index 916458856f..0b657f6e4e 100644 --- a/tests/benchmarks/ws.js +++ b/tests/benchmarks/ws.js @@ -3,6 +3,7 @@ import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'; import ws from 'k6/ws'; import { check } from 'k6'; +import { REALTIME_URL } from './config.js'; export let options = { stages: [ @@ -20,7 +21,7 @@ export let options = { export default function () { // const url = new URL('wss://appwrite-realtime.monitor-api.com/v1/realtime'); // url.searchParams.append('project', '604249e6b1a9f'); - const url = new URL('ws://localhost/v1/realtime'); + const url = new URL(REALTIME_URL); url.searchParams.append('project', 'console'); url.searchParams.append('channels[]', 'files'); From 4bb5dfb6ffd4e66b6bb721cc9b7ef236f93953db Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 15:37:07 -0400 Subject: [PATCH 105/153] Refactor onboarding stages to use SDK method keys instead of stage IDs. Update related logic in API handling, constants, and response models to reflect this change. Adjust tests to validate new onboarding structure and ensure proper functionality. --- app/config/onboarding.php | 84 +++++++---- app/controllers/shared/api.php | 132 +++++++++++------- app/init/constants.php | 2 +- .../Modules/Projects/Http/Stages/Update.php | 56 ++++---- .../Modules/Projects/Http/Stages/XList.php | 17 ++- src/Appwrite/Utopia/Response/Model/Stage.php | 4 +- tests/e2e/Services/Project/StagesBase.php | 43 +++--- 7 files changed, 203 insertions(+), 135 deletions(-) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 626ddd91ca..72f2861206 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -1,30 +1,66 @@ 'create_database', - 'sdk' => 'databases.create', - ], - [ - 'id' => 'create_bucket', - 'sdk' => 'storage.createBucket', - ], - [ - 'id' => 'create_function', - 'sdk' => 'functions.create', - ], -]; - -$sdkIndex = []; -foreach ($stages as $stage) { - $sdkIndex[$stage['sdk']] = $stage['id']; -} - return [ - 'stages' => $stages, - 'sdkIndex' => $sdkIndex, + // Connect — register platform + 'project.createWebPlatform' => true, + 'project.createAndroidPlatform' => true, + 'project.createApplePlatform' => true, + 'project.createWindowsPlatform' => true, + 'project.createLinuxPlatform' => true, + // Connect — API key + 'project.createKey' => true, + + // Auth + 'users.create' => true, + 'teams.create' => true, + + // Databases — tablesDB + 'tablesDB.create' => true, + 'tablesDB.createTable' => true, + 'tablesDB.createRow' => true, + // Databases — documentsDB + 'documentsDB.create' => true, + 'documentsDB.createCollection' => true, + 'documentsDB.createDocument' => true, + // Databases — legacy + 'databases.create' => true, + 'databases.createCollection' => true, + 'databases.createDocument' => true, + + // Storage + 'storage.createBucket' => true, + 'storage.createFile' => true, + + // Functions + 'functions.create' => true, + 'functions.createDeployment' => true, + 'functions.updateFunctionDeployment' => true, + + // Messaging + 'messaging.createTopic' => true, + 'messaging.createMailgunProvider' => true, + 'messaging.createSendgridProvider' => true, + 'messaging.createSesProvider' => true, + 'messaging.createResendProvider' => true, + 'messaging.createSmtpProvider' => true, + 'messaging.createSMTPProvider' => true, + 'messaging.createMsg91Provider' => true, + 'messaging.createTelesignProvider' => true, + 'messaging.createTextmagicProvider' => true, + 'messaging.createTwilioProvider' => true, + 'messaging.createVonageProvider' => true, + 'messaging.createFcmProvider' => true, + 'messaging.createFCMProvider' => true, + 'messaging.createApnsProvider' => true, + 'messaging.createAPNSProvider' => true, + + // Sites + 'sites.create' => true, + 'sites.createDeployment' => true, + 'sites.updateSiteDeployment' => true, ]; diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index dae41d4f4b..8123be05e1 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -891,63 +891,91 @@ Http::shutdown() ->inject('mode') ->action(function (Route $route, Response $response, Document $project, User $user, Realtime $queueForRealtime, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { /** - * Persist completed stage when the route matches a configured SDK method (stored on project as `onboarding`: stageId → row). + * Persist completed stage when the route matches a configured SDK method (stored on project as `onboarding`: method → row). + * Hot path: fail fast on status/console, O(1) onboarding key lookup, O(1) project state check — no work on unrelated routes. */ - if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300 && $project->getId() !== 'console') { - $sdkLabel = $route->getLabel('sdk', false); - $stageId = null; - if ($sdkLabel !== false && $sdkLabel !== null) { - $sdkIndex = Config::getParam('onboarding', [])['sdkIndex'] ?? []; - foreach ($sdkLabel instanceof Method ? [$sdkLabel] : (\is_array($sdkLabel) ? $sdkLabel : []) as $sdkMethod) { - if ($sdkMethod instanceof Method && isset($sdkIndex[$k = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName()])) { - $stageId = $sdkIndex[$k]; - break; - } - } - } - if ($stageId !== null) { - $byStageId = $project->getAttribute('onboarding', []); - if (! \is_array($byStageId)) { - $byStageId = []; - } - $done = \is_array($byStageId[$stageId] ?? null) ? ($byStageId[$stageId]['status'] ?? '') : ''; - if ($done !== ONBOARDING_STATUS_COMPLETED && $done !== ONBOARDING_STATUS_SKIPPED) { - $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) - ? match ($apiKey->getType()) { - API_KEY_ACCOUNT => ACTIVITY_TYPE_KEY_ACCOUNT, - API_KEY_ORGANIZATION => ACTIVITY_TYPE_KEY_ORGANIZATION, - API_KEY_STANDARD, API_KEY_DYNAMIC => ACTIVITY_TYPE_KEY_PROJECT, - default => ACTIVITY_TYPE_KEY_PROJECT, - } - : (! $user->isEmpty() - ? ($mode === APP_MODE_ADMIN ? ACTIVITY_TYPE_ADMIN : ACTIVITY_TYPE_USER) - : ACTIVITY_TYPE_GUEST); - $byStageId[$stageId] = [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => DateTime::now(), - 'actorType' => $actorType, - ]; - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $byStageId, - ]))); + $statusCode = $response->getStatusCode(); + if ($statusCode < 200 || $statusCode >= 300 || $project->getId() === 'console') { + return; + } - $queueForRealtime->reset(); - $queueForRealtime - ->setProject($project) - ->setSubscribers(['console']) - ->setEvent('projects.[projectId].stages.[stageId].complete') - ->setParam('projectId', $project->getId()) - ->setParam('stageId', $stageId) - ->setPayload([ - 'stageId' => $stageId, - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => $byStageId[$stageId]['at'], - 'actorType' => $actorType, - ]) - ->trigger(); + $sdkLabel = $route->getLabel('sdk', false); + if ($sdkLabel === false || $sdkLabel === null) { + return; + } + + /** @var array $onboarding */ + $onboarding = Config::getParam('onboarding', []); + if ($onboarding === []) { + return; + } + + $method = null; + if ($sdkLabel instanceof Method) { + $key = $sdkLabel->getNamespace() . '.' . $sdkLabel->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + } + } elseif (\is_array($sdkLabel)) { + foreach ($sdkLabel as $sdkMethod) { + if (! $sdkMethod instanceof Method) { + continue; + } + $key = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + break; } } } + + if ($method === null) { + return; + } + + $byMethod = $project->getAttribute('onboarding', []); + $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; + if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { + return; + } + + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) + ? match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, + default => ACTOR_TYPE_KEY_PROJECT, + } + : (! $user->isEmpty() + ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) + : ACTOR_TYPE_GUEST); + $byMethod[$method] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ]))); + + $queueForRealtime->reset(); + $queueForRealtime + ->setProject($project) + ->setSubscribers(['console']) + ->setEvent('projects.[projectId].stages.[stageId].complete') + ->setParam('projectId', $project->getId()) + ->setParam('stageId', $method) + ->setPayload([ + 'stageId' => $method, + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => $byMethod[$method]['at'], + 'actorType' => $actorType, + ]) + ->trigger(); }); Http::shutdown() diff --git a/app/init/constants.php b/app/init/constants.php index 44d3b5110b..9efb6e3222 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -173,7 +173,7 @@ const ACTOR_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTOR_TYPE_KEY_ORGANIZATION = 'keyOrganization'; /** - * Project onboarding stage status (stored per stage id under project.onboarding JSON). + * Project onboarding stage status (stored per SDK method key under project.onboarding JSON). */ const ONBOARDING_STATUS_COMPLETED = 'completed'; const ONBOARDING_STATUS_SKIPPED = 'skipped'; diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php index a489dd2b3f..910b2dcf63 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php @@ -52,7 +52,7 @@ class Update extends Action ], )) ->param('projectId', '', new UID(), 'Project unique ID.') - ->param('stageId', '', new Text(64), 'Stage ID.') + ->param('stageId', '', new Text(128), 'SDK method key (namespace.method).') ->param('skip', true, new Boolean(), 'Mark the stage as skipped.', true) ->inject('response') ->inject('dbForPlatform') @@ -70,60 +70,54 @@ class Update extends Action throw new Exception(Exception::PROJECT_NOT_FOUND); } - $definition = $this->getStageDefinition($stageId); + $this->assertOnboardingMethod($stageId); - $byStageId = $project->getAttribute('onboarding', []); - if (! \is_array($byStageId)) { - $byStageId = []; + $byMethod = $project->getAttribute('onboarding', []); + if (! \is_array($byMethod)) { + $byMethod = []; } - $row = \is_array($byStageId[$stageId] ?? null) ? $byStageId[$stageId] : null; + $row = \is_array($byMethod[$stageId] ?? null) ? $byMethod[$stageId] : null; if ($skip) { $prev = \is_array($row) ? ($row['status'] ?? '') : ''; if ($prev !== ONBOARDING_STATUS_COMPLETED) { - $byStageId[$stageId] = [ + $byMethod[$stageId] = [ 'status' => ONBOARDING_STATUS_SKIPPED, 'at' => DateTime::now(), 'actorType' => $this->resolveActorType($apiKey, $user, $mode), ]; - $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('onboarding', $byStageId)); - $byStageId = $project->getAttribute('onboarding', []); - $row = \is_array($byStageId[$stageId] ?? null) ? $byStageId[$stageId] : null; + $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ])); + $byMethod = $project->getAttribute('onboarding', []); + $row = \is_array($byMethod[$stageId] ?? null) ? $byMethod[$stageId] : null; } } - $response->dynamic(new Document($this->formatStageRow($definition, $row)), Response::MODEL_STAGE); + $response->dynamic(new Document($this->formatStageRow($stageId, $row)), Response::MODEL_STAGE); } - /** - * @return array{id: string, sdk: string} - */ - private function getStageDefinition(string $stageId): array + private function assertOnboardingMethod(string $method): void { - foreach (Config::getParam('onboarding', [])['stages'] ?? [] as $definition) { - if (($definition['id'] ?? '') === $stageId) { - return $definition; - } + if (! \array_key_exists($method, Config::getParam('onboarding', []))) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unknown SDK method: ' . $method); } - - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unknown stage ID: ' . $stageId); } /** * @param array|null $row * @return array */ - private function formatStageRow(array $definition, ?array $row): array + private function formatStageRow(string $method, ?array $row): array { - $stageId = $definition['id']; $status = \is_array($row) ? ($row['status'] ?? null) : null; $at = \is_array($row) ? ($row['at'] ?? '') : ''; $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; return [ - 'id' => $stageId, - 'sdk' => $definition['sdk'] ?? '', + 'id' => $method, + 'sdk' => $method, 'status' => $status ?? 'pending', 'at' => $at, 'actorType' => $actorType, @@ -134,17 +128,17 @@ class Update extends Action { if ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) { return match ($apiKey->getType()) { - API_KEY_ACCOUNT => ACTIVITY_TYPE_KEY_ACCOUNT, - API_KEY_ORGANIZATION => ACTIVITY_TYPE_KEY_ORGANIZATION, - API_KEY_STANDARD, API_KEY_DYNAMIC => ACTIVITY_TYPE_KEY_PROJECT, - default => ACTIVITY_TYPE_KEY_PROJECT, + API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, + default => ACTOR_TYPE_KEY_PROJECT, }; } if (! $user->isEmpty()) { - return $mode === APP_MODE_ADMIN ? ACTIVITY_TYPE_ADMIN : ACTIVITY_TYPE_USER; + return $mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER; } - return ACTIVITY_TYPE_GUEST; + return ACTOR_TYPE_GUEST; } } diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php index e0f4f4074b..7fa6537152 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/XList.php @@ -68,24 +68,23 @@ class XList extends Action */ private function buildStagesList(Document $project): array { - $definitions = Config::getParam('onboarding', [])['stages'] ?? []; - $byStageId = $project->getAttribute('onboarding', []); - if (! \is_array($byStageId)) { - $byStageId = []; + $methods = \array_keys(Config::getParam('onboarding', [])); + $byMethod = $project->getAttribute('onboarding', []); + if (! \is_array($byMethod)) { + $byMethod = []; } $out = []; - foreach ($definitions as $definition) { - $stageId = $definition['id']; - $row = $byStageId[$stageId] ?? null; + foreach ($methods as $method) { + $row = $byMethod[$method] ?? null; $status = \is_array($row) ? ($row['status'] ?? null) : null; $at = \is_array($row) ? ($row['at'] ?? '') : ''; $actorType = \is_array($row) ? ($row['actorType'] ?? '') : ''; $out[] = [ - 'id' => $stageId, - 'sdk' => $definition['sdk'] ?? '', + 'id' => $method, + 'sdk' => $method, 'status' => $status ?? 'pending', 'at' => $at, 'actorType' => $actorType, diff --git a/src/Appwrite/Utopia/Response/Model/Stage.php b/src/Appwrite/Utopia/Response/Model/Stage.php index 8adaaaae47..2b39499f8f 100644 --- a/src/Appwrite/Utopia/Response/Model/Stage.php +++ b/src/Appwrite/Utopia/Response/Model/Stage.php @@ -15,13 +15,13 @@ class Stage extends Model 'type' => self::TYPE_STRING, 'description' => 'Stage ID.', 'default' => '', - 'example' => 'create_database', + 'example' => 'tablesDB.create', ]) ->addRule('sdk', [ 'type' => self::TYPE_STRING, 'description' => 'SDK method key (namespace.name) for this stage.', 'default' => '', - 'example' => 'databases.create', + 'example' => 'tablesDB.create', ]) ->addRule('status', [ 'type' => self::TYPE_STRING, diff --git a/tests/e2e/Services/Project/StagesBase.php b/tests/e2e/Services/Project/StagesBase.php index 9bf3f21f4b..ff7a56359d 100644 --- a/tests/e2e/Services/Project/StagesBase.php +++ b/tests/e2e/Services/Project/StagesBase.php @@ -5,6 +5,7 @@ namespace Tests\E2E\Services\Project; use Appwrite\Extend\Exception; use Appwrite\Tests\Async; use Tests\E2E\Client; +use Utopia\Config\Config; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\System\System; @@ -25,13 +26,15 @@ trait StagesBase $this->assertSame(200, $response['headers']['status-code']); $this->assertIsArray($response['body']['stages']); - $this->assertCount(3, $response['body']['stages']); + $this->assertCount(\count(Config::getParam('onboarding', [])), $response['body']['stages']); $ids = array_column($response['body']['stages'], 'id'); - $this->assertSame(['create_database', 'create_bucket', 'create_function'], $ids); + $this->assertContains('tablesDB.create', $ids); + $this->assertContains('storage.createBucket', $ids); + $this->assertContains('functions.create', $ids); foreach ($response['body']['stages'] as $stage) { - $this->assertArrayHasKey('sdk', $stage); + $this->assertSame($stage['id'], $stage['sdk']); $this->assertArrayHasKey('status', $stage); $this->assertArrayHasKey('at', $stage); $this->assertArrayHasKey('actorType', $stage); @@ -60,31 +63,38 @@ trait StagesBase public function testUpdateProjectStageSkip(): void { $projectId = $this->createTeamAndProject(); + $method = 'tablesDB.create'; - $response = $this->updateStage($projectId, 'create_database', true); + $response = $this->updateStage($projectId, $method, true); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame('create_database', $response['body']['id']); - $this->assertSame('databases.create', $response['body']['sdk']); + $this->assertSame($method, $response['body']['id']); + $this->assertSame($method, $response['body']['sdk']); $this->assertSame(ONBOARDING_STATUS_SKIPPED, $response['body']['status']); $this->assertNotEmpty($response['body']['at']); - $this->assertSame(ACTIVITY_TYPE_USER, $response['body']['actorType']); + $this->assertSame(ACTOR_TYPE_USER, $response['body']['actorType']); $dateValidator = new DatetimeValidator(); $this->assertSame(true, $dateValidator->isValid($response['body']['at'])); $list = $this->listStages($projectId); $this->assertSame(200, $list['headers']['status-code']); - $first = $list['body']['stages'][0]; - $this->assertSame('create_database', $first['id']); - $this->assertSame(ONBOARDING_STATUS_SKIPPED, $first['status']); - $this->assertSame(ACTIVITY_TYPE_USER, $first['actorType']); + $databaseStage = null; + foreach ($list['body']['stages'] as $stage) { + if ($stage['id'] === $method) { + $databaseStage = $stage; + break; + } + } + $this->assertNotNull($databaseStage); + $this->assertSame(ONBOARDING_STATUS_SKIPPED, $databaseStage['status']); + $this->assertSame(ACTOR_TYPE_USER, $databaseStage['actorType']); - $response = $this->updateStage($projectId, 'unknown_stage_xyz', true); + $response = $this->updateStage($projectId, 'unknown.method', true); $this->assertSame(400, $response['headers']['status-code']); $this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); - $response = $this->updateStage(ID::unique(), 'create_bucket', true); + $response = $this->updateStage(ID::unique(), 'storage.createBucket', true); $this->assertSame(404, $response['headers']['status-code']); $this->assertSame(Exception::PROJECT_NOT_FOUND, $response['body']['type']); } @@ -92,11 +102,12 @@ trait StagesBase public function testUpdateProjectStageSkipFalseLeavesPending(): void { $projectId = $this->createTeamAndProject(); + $method = 'storage.createBucket'; - $response = $this->updateStage($projectId, 'create_bucket', false); + $response = $this->updateStage($projectId, $method, false); $this->assertSame(200, $response['headers']['status-code']); - $this->assertSame('create_bucket', $response['body']['id']); + $this->assertSame($method, $response['body']['id']); $this->assertSame('pending', $response['body']['status']); } @@ -104,7 +115,7 @@ trait StagesBase { $projectId = $this->createTeamAndProject(); - $response = $this->updateStage($projectId, 'create_database', true, false); + $response = $this->updateStage($projectId, 'tablesDB.create', true, false); $this->assertSame(401, $response['headers']['status-code']); } From 7b24acb5b6d477a0515bdd2709e6d851d572d74a Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 15:53:58 -0400 Subject: [PATCH 106/153] Enhance API shutdown logic to persist onboarding stage completion after usage metrics are recorded. Introduce error handling for database updates to ensure reliability in onboarding status updates. --- app/controllers/shared/api.php | 206 +++++++++++++++++---------------- 1 file changed, 106 insertions(+), 100 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 8123be05e1..c16e590f4a 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -42,6 +42,7 @@ use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; +use Throwable; $parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project) { preg_match_all('/{(.*?)}/', $label, $matches); @@ -878,106 +879,6 @@ Http::shutdown() } }); -Http::shutdown() - ->groups(['api']) - ->inject('route') - ->inject('response') - ->inject('project') - ->inject('user') - ->inject('queueForRealtime') - ->inject('dbForPlatform') - ->inject('authorization') - ->inject('apiKey') - ->inject('mode') - ->action(function (Route $route, Response $response, Document $project, User $user, Realtime $queueForRealtime, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { - /** - * Persist completed stage when the route matches a configured SDK method (stored on project as `onboarding`: method → row). - * Hot path: fail fast on status/console, O(1) onboarding key lookup, O(1) project state check — no work on unrelated routes. - */ - $statusCode = $response->getStatusCode(); - if ($statusCode < 200 || $statusCode >= 300 || $project->getId() === 'console') { - return; - } - - $sdkLabel = $route->getLabel('sdk', false); - if ($sdkLabel === false || $sdkLabel === null) { - return; - } - - /** @var array $onboarding */ - $onboarding = Config::getParam('onboarding', []); - if ($onboarding === []) { - return; - } - - $method = null; - if ($sdkLabel instanceof Method) { - $key = $sdkLabel->getNamespace() . '.' . $sdkLabel->getMethodName(); - if (isset($onboarding[$key])) { - $method = $key; - } - } elseif (\is_array($sdkLabel)) { - foreach ($sdkLabel as $sdkMethod) { - if (! $sdkMethod instanceof Method) { - continue; - } - $key = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName(); - if (isset($onboarding[$key])) { - $method = $key; - break; - } - } - } - - if ($method === null) { - return; - } - - $byMethod = $project->getAttribute('onboarding', []); - $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; - if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { - return; - } - - if (! \is_array($byMethod)) { - $byMethod = []; - } - - $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) - ? match ($apiKey->getType()) { - API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, - API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, - API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, - default => ACTOR_TYPE_KEY_PROJECT, - } - : (! $user->isEmpty() - ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) - : ACTOR_TYPE_GUEST); - $byMethod[$method] = [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => DateTime::now(), - 'actorType' => $actorType, - ]; - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $byMethod, - ]))); - - $queueForRealtime->reset(); - $queueForRealtime - ->setProject($project) - ->setSubscribers(['console']) - ->setEvent('projects.[projectId].stages.[stageId].complete') - ->setParam('projectId', $project->getId()) - ->setParam('stageId', $method) - ->setPayload([ - 'stageId' => $method, - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => $byMethod[$method]['at'], - 'actorType' => $actorType, - ]) - ->trigger(); - }); - Http::shutdown() ->groups(['api']) ->inject('route') @@ -1212,6 +1113,111 @@ Http::shutdown() } }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('queueForRealtime') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('apiKey') + ->inject('mode') + ->action(function (Route $route, Response $response, Document $project, User $user, Realtime $queueForRealtime, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { + /** + * Persist completed onboarding stage after usage shutdown so a schema/write failure here + * cannot suppress RequestCompleted or usage metrics on the same request. + */ + $statusCode = $response->getStatusCode(); + if ($statusCode < 200 || $statusCode >= 300 || $project->getId() === 'console') { + return; + } + + $sdkLabel = $route->getLabel('sdk', false); + if ($sdkLabel === false || $sdkLabel === null) { + return; + } + + /** @var array $onboarding */ + $onboarding = Config::getParam('onboarding', []); + if ($onboarding === []) { + return; + } + + $method = null; + if ($sdkLabel instanceof Method) { + $key = $sdkLabel->getNamespace() . '.' . $sdkLabel->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + } + } elseif (\is_array($sdkLabel)) { + foreach ($sdkLabel as $sdkMethod) { + if (! $sdkMethod instanceof Method) { + continue; + } + $key = $sdkMethod->getNamespace() . '.' . $sdkMethod->getMethodName(); + if (isset($onboarding[$key])) { + $method = $key; + break; + } + } + } + + if ($method === null) { + return; + } + + $byMethod = $project->getAttribute('onboarding', []); + $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; + if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { + return; + } + + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) + ? match ($apiKey->getType()) { + API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, + API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, + API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, + default => ACTOR_TYPE_KEY_PROJECT, + } + : (! $user->isEmpty() + ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) + : ACTOR_TYPE_GUEST); + $byMethod[$method] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; + + try { + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ]))); + + $queueForRealtime->reset(); + $queueForRealtime + ->setProject($project) + ->setSubscribers(['console']) + ->setEvent('projects.[projectId].stages.[stageId].complete') + ->setParam('projectId', $project->getId()) + ->setParam('stageId', $method) + ->setPayload([ + 'stageId' => $method, + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => $byMethod[$method]['at'], + 'actorType' => $actorType, + ]) + ->trigger(); + } catch (Throwable) { + // Missing `onboarding` attribute on upgraded installs must not break the request lifecycle. + } + }); + Http::init() ->groups(['usage']) ->action(function () { From c1a03613928052606fbe97a22cbadd53695ac901 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 16:31:24 -0400 Subject: [PATCH 107/153] Update onboarding configuration to include account creation permissions and adjust API role checks. Modify migration versioning for consistency and refactor project stage tests to streamline project ID retrieval. --- app/config/onboarding.php | 2 + app/controllers/shared/api.php | 2 +- src/Appwrite/Migration/Migration.php | 1 + .../Modules/Projects/Http/Stages/Update.php | 2 +- tests/e2e/Services/Project/StagesBase.php | 76 ++++--------------- .../Project/StagesConsoleClientTest.php | 4 +- 6 files changed, 21 insertions(+), 66 deletions(-) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 72f2861206..38f2bb8ec5 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -17,6 +17,8 @@ return [ // Auth 'users.create' => true, + 'account.create' => true, + 'account.createAnonymousSession' => true, 'teams.create' => true, // Databases — tablesDB diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index c16e590f4a..9a894c4587 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -1178,7 +1178,7 @@ Http::shutdown() $byMethod = []; } - $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) ? match ($apiKey->getType()) { API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 77c62bce96..93d8994d7c 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -98,6 +98,7 @@ abstract class Migration '1.9.3' => 'V24', '1.9.4' => 'V24', '1.9.5' => 'V24', + '1.9.6' => 'V25', ]; /** diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php index 910b2dcf63..c345712bc8 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Stages/Update.php @@ -126,7 +126,7 @@ class Update extends Action private function resolveActorType(?Key $apiKey, User $user, string $mode): string { - if ($apiKey !== null && $apiKey->getRole() === User::ROLE_APPS) { + if ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) { return match ($apiKey->getType()) { API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, API_KEY_ORGANIZATION => ACTOR_TYPE_KEY_ORGANIZATION, diff --git a/tests/e2e/Services/Project/StagesBase.php b/tests/e2e/Services/Project/StagesBase.php index ff7a56359d..471fdd9acb 100644 --- a/tests/e2e/Services/Project/StagesBase.php +++ b/tests/e2e/Services/Project/StagesBase.php @@ -3,24 +3,19 @@ namespace Tests\E2E\Services\Project; use Appwrite\Extend\Exception; -use Appwrite\Tests\Async; use Tests\E2E\Client; use Utopia\Config\Config; -use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\Datetime as DatetimeValidator; -use Utopia\System\System; trait StagesBase { - use Async; - // ========================================================================= // List stages tests // ========================================================================= public function testListProjectStages(): void { - $projectId = $this->createTeamAndProject(); + $projectId = $this->getProject()['$id']; $response = $this->listStages($projectId); @@ -38,20 +33,12 @@ trait StagesBase $this->assertArrayHasKey('status', $stage); $this->assertArrayHasKey('at', $stage); $this->assertArrayHasKey('actorType', $stage); - $this->assertSame('pending', $stage['status']); } - - // Verify via GET for unknown project - $response = $this->listStages(ID::unique()); - $this->assertSame(404, $response['headers']['status-code']); - $this->assertSame(Exception::PROJECT_NOT_FOUND, $response['body']['type']); } public function testListProjectStagesWithoutAuthentication(): void { - $projectId = $this->createTeamAndProject(); - - $response = $this->listStages($projectId, false); + $response = $this->listStages($this->getProject()['$id'], false); $this->assertSame(401, $response['headers']['status-code']); } @@ -62,7 +49,7 @@ trait StagesBase public function testUpdateProjectStageSkip(): void { - $projectId = $this->createTeamAndProject(); + $projectId = $this->getProject()['$id']; $method = 'tablesDB.create'; $response = $this->updateStage($projectId, $method, true); @@ -93,15 +80,11 @@ trait StagesBase $response = $this->updateStage($projectId, 'unknown.method', true); $this->assertSame(400, $response['headers']['status-code']); $this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $response['body']['type']); - - $response = $this->updateStage(ID::unique(), 'storage.createBucket', true); - $this->assertSame(404, $response['headers']['status-code']); - $this->assertSame(Exception::PROJECT_NOT_FOUND, $response['body']['type']); } public function testUpdateProjectStageSkipFalseLeavesPending(): void { - $projectId = $this->createTeamAndProject(); + $projectId = $this->getProject()['$id']; $method = 'storage.createBucket'; $response = $this->updateStage($projectId, $method, false); @@ -113,9 +96,7 @@ trait StagesBase public function testUpdateProjectStageWithoutAuthentication(): void { - $projectId = $this->createTeamAndProject(); - - $response = $this->updateStage($projectId, 'tablesDB.create', true, false); + $response = $this->updateStage($this->getProject()['$id'], 'tablesDB.create', true, false); $this->assertSame(401, $response['headers']['status-code']); } @@ -126,13 +107,15 @@ trait StagesBase protected function listStages(string $projectId, bool $authenticated = true): mixed { + // Stages live under /projects (console scope); use the root console session. $headers = [ 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-project' => 'console', ]; if ($authenticated) { - $headers = array_merge($headers, $this->getHeaders()); + $headers['origin'] = 'http://localhost'; + $headers['cookie'] = 'a_session_console=' . $this->getRoot()['session']; } return $this->client->call(Client::METHOD_GET, '/projects/' . $projectId . '/stages', $headers, []); @@ -142,49 +125,16 @@ trait StagesBase { $headers = [ 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-project' => 'console', ]; if ($authenticated) { - $headers = array_merge($headers, $this->getHeaders()); + $headers['origin'] = 'http://localhost'; + $headers['cookie'] = 'a_session_console=' . $this->getRoot()['session']; } - return $this->client->call(Client::METHOD_PATCH, '/projects/' . $projectId . '/stages/' . $stageId, $headers, [ + return $this->client->call(Client::METHOD_PATCH, '/projects/' . $projectId . '/stages/' . rawurlencode($stageId), $headers, [ 'skip' => $skip, ]); } - - /** - * Creates a new team and child project under the console session (isolated from cached getProject()). - */ - protected function createTeamAndProject(): string - { - $teamId = ID::unique(); - $team = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - ], $this->getHeaders()), [ - 'teamId' => $teamId, - 'name' => 'Stages E2E Team', - ]); - - $this->assertContains($team['headers']['status-code'], [201, 409]); - if ($team['headers']['status-code'] === 201) { - $teamId = $team['body']['$id']; - } - - $project = $this->client->call(Client::METHOD_POST, '/projects', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - ], $this->getHeaders()), [ - 'projectId' => ID::unique(), - 'name' => 'Stages E2E Project', - 'teamId' => $teamId, - 'region' => System::getEnv('_APP_REGION', 'default'), - ]); - - $this->assertSame(201, $project['headers']['status-code']); - - return $project['body']['$id']; - } } diff --git a/tests/e2e/Services/Project/StagesConsoleClientTest.php b/tests/e2e/Services/Project/StagesConsoleClientTest.php index cde4f4ab79..51ba5fa3e4 100644 --- a/tests/e2e/Services/Project/StagesConsoleClientTest.php +++ b/tests/e2e/Services/Project/StagesConsoleClientTest.php @@ -1,12 +1,14 @@ Date: Thu, 25 Jun 2026 16:40:14 -0400 Subject: [PATCH 108/153] Remove migration version mapping for '1.9.6' to maintain consistency in versioning. --- src/Appwrite/Migration/Migration.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Appwrite/Migration/Migration.php b/src/Appwrite/Migration/Migration.php index 93d8994d7c..77c62bce96 100644 --- a/src/Appwrite/Migration/Migration.php +++ b/src/Appwrite/Migration/Migration.php @@ -98,7 +98,6 @@ abstract class Migration '1.9.3' => 'V24', '1.9.4' => 'V24', '1.9.5' => 'V24', - '1.9.6' => 'V25', ]; /** From 67fd6226a90dd5d2448c45d202d75988f7b79161 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 16:41:14 -0400 Subject: [PATCH 109/153] Refactor API role determination logic for improved readability and maintainability. Remove unused Throwable import and adjust formatting for consistency. --- app/controllers/shared/api.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 9a894c4587..150a2bcd6b 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -23,6 +23,7 @@ use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Throwable; use Utopia\Abuse\Abuse; use Utopia\Bus\Bus; use Utopia\Cache\Adapter\Filesystem; @@ -42,7 +43,6 @@ use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Validator\WhiteList; -use Throwable; $parseLabel = function (string $label, array $responsePayload, array $requestParams, User $user, Document $project) { preg_match_all('/{(.*?)}/', $label, $matches); @@ -1185,9 +1185,9 @@ Http::shutdown() API_KEY_STANDARD, API_KEY_EPHEMERAL => ACTOR_TYPE_KEY_PROJECT, default => ACTOR_TYPE_KEY_PROJECT, } - : (! $user->isEmpty() - ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) - : ACTOR_TYPE_GUEST); + : (! $user->isEmpty() + ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) + : ACTOR_TYPE_GUEST); $byMethod[$method] = [ 'status' => ONBOARDING_STATUS_COMPLETED, 'at' => DateTime::now(), From 5ee82afc8c3eeb99306bbc3397a399513acf7730 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Thu, 25 Jun 2026 17:41:30 -0400 Subject: [PATCH 110/153] Remove unused queueForRealtime injection from API shutdown logic to streamline onboarding process. Adjust related action parameters for improved clarity and maintainability. --- app/controllers/shared/api.php | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 150a2bcd6b..e30305e55b 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -1119,12 +1119,11 @@ Http::shutdown() ->inject('response') ->inject('project') ->inject('user') - ->inject('queueForRealtime') ->inject('dbForPlatform') ->inject('authorization') ->inject('apiKey') ->inject('mode') - ->action(function (Route $route, Response $response, Document $project, User $user, Realtime $queueForRealtime, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { + ->action(function (Route $route, Response $response, Document $project, User $user, Database $dbForPlatform, Authorization $authorization, ?Key $apiKey, string $mode) { /** * Persist completed onboarding stage after usage shutdown so a schema/write failure here * cannot suppress RequestCompleted or usage metrics on the same request. @@ -1198,21 +1197,6 @@ Http::shutdown() $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'onboarding' => $byMethod, ]))); - - $queueForRealtime->reset(); - $queueForRealtime - ->setProject($project) - ->setSubscribers(['console']) - ->setEvent('projects.[projectId].stages.[stageId].complete') - ->setParam('projectId', $project->getId()) - ->setParam('stageId', $method) - ->setPayload([ - 'stageId' => $method, - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => $byMethod[$method]['at'], - 'actorType' => $actorType, - ]) - ->trigger(); } catch (Throwable) { // Missing `onboarding` attribute on upgraded installs must not break the request lifecycle. } From 8d711e1db6e789da7d6e8bba08a1973df5545949 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:05:36 +0530 Subject: [PATCH 111/153] 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 d45587d5cd1e269f1091456c385de00377b82632 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:05:36 +0530 Subject: [PATCH 112/153] 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 900c0e78c5..dc2bc60763 100644 --- a/.env +++ b/.env @@ -107,6 +107,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 dc328f0dc8..03948e2e76 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -570,6 +570,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 @@ -1275,7 +1276,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 @@ -1284,6 +1285,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 d0806ccadf..fb4e691926 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -677,6 +677,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': @@ -692,6 +693,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 2c3e5bc2b2a8f0b2be36c4ce4aa32fa22233030d Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:13:51 +0530 Subject: [PATCH 113/153] 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 36537cbf18..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:9521/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'); From 2dc2358c998fd6428634a78eb02ee2fae7347974 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 08:13:51 +0530 Subject: [PATCH 114/153] 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'); From 70a7d91c7fcf57d19b871226063e66669869df2e Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 14:55:50 +0530 Subject: [PATCH 115/153] fix: purge project cache after legacy oauth2 update --- app/controllers/api/projects.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index a39ce3c4bd..a9eec75f8b 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -64,6 +64,8 @@ Http::patch('/v1/projects/:projectId/oauth2') ])); }); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); + $response->dynamic($project, Response::MODEL_PROJECT); }); From 52927ac0ec9d1a3c55fd84811a1d85132817d666 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 13:10:43 +0100 Subject: [PATCH 116/153] ci: run CI on pushes to main Add main to the push.branches trigger so merge commits landing on main run the test suite, not just PRs and 1.9.x pushes. Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c734df8e9..0ddacbfd84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,7 @@ on: pull_request: push: branches: + - main - 1.9.x workflow_dispatch: inputs: From 5ad7d70e1bdcfe24eeea9d2493249ce3a7b44b78 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 18:08:13 +0530 Subject: [PATCH 117/153] Fix email template update race --- .../Http/Project/Templates/Email/Update.php | 82 +++++++++++-------- 1 file changed, 50 insertions(+), 32 deletions(-) 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 8e86d7d1e6..4ae859dfeb 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 @@ -91,45 +91,63 @@ class Update extends Action ) { $locale = $locale ?: System::getEnv('_APP_LOCALE', 'en'); - // Prevent template update if custom SMTP is not configured - $smtp = $project->getAttribute('smtp', []); - if (($smtp['enabled'] ?? false) !== true) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); - } + $templateKey = "email.{$templateId}-{$locale}"; + $changes = [ + 'senderName' => $senderName, + 'senderEmail' => $senderEmail, + 'replyToEmail' => $replyToEmail, + 'replyToName' => $replyToName, + 'message' => $message, + 'subject' => $subject, + ]; - // Fetch current configuration - $templates = $project->getAttribute('templates', []); - $template = $templates['email.' . $templateId . '-' . $locale] ?? []; + $project = $dbForPlatform->withTransaction(function () use ($dbForPlatform, $authorization, $project, $templateKey, $changes) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId(), forUpdate: true)); - // Apply changes — null means "not provided, keep existing". - // Empty string explicitly clears a previously-set value. - $keys = ['senderName', 'senderEmail', 'replyToEmail', 'replyToName', 'message', 'subject']; - foreach ($keys as $key) { - if (!\is_null(${$key})) { - $template[$key] = ${$key}; + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); } - } - // Backwards compatibility - if (!\is_null($template['replyTo'] ?? null)) { - $template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? ''; - } - - // Ensure required fields are set - $requiredKeys = ['subject', 'message']; - foreach ($requiredKeys as $key) { - if (empty($template[$key])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + // Prevent template update if custom SMTP is not configured + $smtp = $project->getAttribute('smtp', []); + if (($smtp['enabled'] ?? false) !== true) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); } - } - // Save configuration - $templates['email.' . $templateId . '-' . $locale] = $template; - $updates = new Document([ - 'templates' => $templates, - ]); + // Fetch current configuration from the locked row so concurrent updates + // to other locale/template entries are preserved. + $templates = $project->getAttribute('templates', []); + $template = $templates[$templateKey] ?? []; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + // Apply changes: null means "not provided, keep existing". + // Empty string explicitly clears a previously-set value. + foreach ($changes as $key => $value) { + if ($value !== null) { + $template[$key] = $value; + } + } + + // Backwards compatibility + if (($template['replyTo'] ?? null) !== null) { + $template['replyToEmail'] ??= $template['replyTo'] ?? ''; + } + + // Ensure required fields are set + $requiredKeys = ['subject', 'message']; + foreach ($requiredKeys as $key) { + if (empty($template[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + } + } + + $templates[$templateKey] = $template; + + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'templates' => $templates, + ]))); + }); + + $template = $project->getAttribute('templates', [])[$templateKey] ?? []; $queueForEvents->setParam('templateId', $templateId); From cbb2446c93e012396706ff5b363fe95d99ddb6cb Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Fri, 26 Jun 2026 18:30:56 +0530 Subject: [PATCH 118/153] Stabilize sites usage stats test --- tests/e2e/General/UsageTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e/General/UsageTest.php b/tests/e2e/General/UsageTest.php index 217991c58f..42321395cc 100644 --- a/tests/e2e/General/UsageTest.php +++ b/tests/e2e/General/UsageTest.php @@ -1869,7 +1869,7 @@ final class UsageTest extends Scope $this->validateDates($response['body']['executions']); $this->assertEquals($executionTime, $response['body']['executionsTime'][array_key_last($response['body']['executionsTime'])]['value']); $this->validateDates($response['body']['executionsTime']); - }); + }, 60000, 500); $this->assertEventually(function () use ($executions, $executionTime) { $response = $this->client->call( @@ -1901,7 +1901,7 @@ final class UsageTest extends Scope $this->validateDates($response['body']['executionsTime']); $this->assertGreaterThan(0, $response['body']['buildsTime'][array_key_last($response['body']['buildsTime'])]['value']); $this->validateDates($response['body']['buildsTime']); - }); + }, 60000, 500); } public function testCustomDomainsFunctionStats(): void From 6b35a4ad89b6d97b3967c2785b8bafd181348205 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:12:31 +0100 Subject: [PATCH 119/153] Run benchmark at 50 VUs and clarify the PR comment Bump the CI benchmark to 50 VUs (with 250 shared iterations so every VU does work) for a meaningful concurrency/RPS measurement. Rework the PR comment around a headline table (RPS, P50, P95 with direction-aware percent change) and move the per-scenario breakdown and top API waits into a collapsed details section. Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/benchmark-comment.js | 91 ++++++++++++++++---------- .github/workflows/ci.yml | 8 +-- 2 files changed, 60 insertions(+), 39 deletions(-) diff --git a/.github/workflows/benchmark-comment.js b/.github/workflows/benchmark-comment.js index f25116c4f2..0de20aac13 100644 --- a/.github/workflows/benchmark-comment.js +++ b/.github/workflows/benchmark-comment.js @@ -51,42 +51,37 @@ function buildComment(core) { const baseRef = markdownText(process.env.BENCHMARK_BASE_REF || 'base'); const headRef = markdownText(process.env.BENCHMARK_HEAD_REF || 'head'); const rows = benchmarkRows(before, after, beforeSamples, afterSamples); - const topWaits = topSamples(afterSamples, 'appwrite_api_waiting', 3); + const total = rows[0]; + const topWaits = topSamples(afterSamples, 'appwrite_api_waiting', 5); const lines = [ marker, '## :sparkles: Benchmark results', '', - `Comparing ${baseRef} (before) to ${headRef} (after).`, + `Comparing \`${baseRef}\` (before) → \`${headRef}\` (after).`, '', ]; if (before === null) { - lines.push('> Before benchmark did not complete; showing current branch metrics only.', ''); + lines.push('> :warning: Before benchmark did not complete — change column unavailable.', ''); } if (after === null) { - lines.push('> Current branch benchmark did not complete; showing available metrics only.', ''); + lines.push('> :warning: Current branch benchmark did not complete — showing available metrics only.', ''); } lines.push( - '**Before**', - '', - metricTable(rows, 'before'), - '', - '**After**', - '', - metricTable(rows, 'after'), - '', - '**Delta**', - '', - '| Scenario | P95 delta (ms) |', - '| --- | ---: |', - ...rows.map(deltaRow), + ...headlineTable(total), '', '
', - 'Top API waits', + 'Per-scenario breakdown & investigation details', '', '
', '', + '_Metrics below reflect the current branch (after). Δ P95 compares against the base._', + '', + detailTable(rows), + '', + '**Top API waits (after)**', + '', '| API request | Max wait (ms) |', '| --- | ---: |', ...topWaitRows(topWaits), @@ -97,6 +92,44 @@ function buildComment(core) { return `${lines.join('\n')}\n`; } +function headlineTable(total) { + const { before, after } = total; + return [ + '| Metric | Before | After | Change |', + '| --- | ---: | ---: | :--- |', + `| :rocket: Requests/sec | ${formatRate(before?.rps)} | ${formatRate(after?.rps)} | ${changeCell(before?.rps, after?.rps, false)} |`, + `| :stopwatch: Latency P50 | ${formatMsUnit(before?.p50)} | ${formatMsUnit(after?.p50)} | ${changeCell(before?.p50, after?.p50, true)} |`, + `| :stopwatch: Latency P95 | ${formatMsUnit(before?.p95)} | ${formatMsUnit(after?.p95)} | ${changeCell(before?.p95, after?.p95, true)} |`, + ]; +} + +function changeCell(before, after, lowerIsBetter) { + if (before === null || before === undefined || after === null || after === undefined || Number.isNaN(before) || Number.isNaN(after) || before === 0) { + return 'n/a'; + } + + const percent = Number((((after - before) / before) * 100).toFixed(1)); + if (percent === 0) { + return ':white_circle: 0%'; + } + + const improved = lowerIsBetter ? percent < 0 : percent > 0; + return `${improved ? ':green_circle:' : ':red_circle:'} ${percent > 0 ? '+' : ''}${trimNumber(percent)}%`; +} + +function detailTable(rows) { + return [ + '| Scenario | P50 (ms) | P95 (ms) | Requests | RPS | Δ P95 (ms) |', + '| --- | ---: | ---: | ---: | ---: | ---: |', + ...rows.map(detailRow), + ].join('\n'); +} + +function detailRow(row) { + const values = row.after; + return `| ${row.label} | ${formatMs(values?.p50)} | ${formatMs(values?.p95)} | ${formatCount(values?.iterations)} | ${formatRate(values?.rps)} | ${formatDelta(row.before?.p95, row.after?.p95)} |`; +} + function readSummary(path, core) { if (!fs.existsSync(path)) { return null; @@ -256,23 +289,6 @@ function metricValue(data, metric, stat) { return metricValues(data, metric)?.[stat] ?? null; } -function metricTable(rows, side) { - return [ - '| Scenario | P50 (ms) | P95 (ms) | Requests | RPS |', - '| --- | ---: | ---: | ---: | ---: |', - ...rows.map((row) => metricRow(row, side)), - ].join('\n'); -} - -function metricRow(row, side) { - const values = row[side]; - return `| ${row.label} | ${formatMs(values?.p50)} | ${formatMs(values?.p95)} | ${formatCount(values?.iterations)} | ${formatRate(values?.rps)} |`; -} - -function deltaRow(row) { - return `| ${row.label} | ${formatDelta(row.before?.p95, row.after?.p95)} |`; -} - function topSamples(samples, metric, limit) { const byName = samples.reduce((result, sample) => { if (sample.metric !== metric || typeof sample.data?.value !== 'number') { @@ -313,6 +329,11 @@ function formatMs(value) { return formatNumber(value, 2); } +function formatMsUnit(value) { + const formatted = formatNumber(value, 2); + return formatted === 'n/a' ? formatted : `${formatted} ms`; +} + function formatRate(value) { return formatNumber(value, 2); } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7bc5c91e5..13eb44a6f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -686,8 +686,8 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_ITERATIONS: '5' - APPWRITE_BENCHMARK_VUS: '1' + APPWRITE_BENCHMARK_ITERATIONS: '250' + APPWRITE_BENCHMARK_VUS: '50' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-before-summary.json' with: @@ -747,8 +747,8 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_ITERATIONS: '5' - APPWRITE_BENCHMARK_VUS: '1' + APPWRITE_BENCHMARK_ITERATIONS: '250' + APPWRITE_BENCHMARK_VUS: '50' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH: '../../benchmark-before-summary.json' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-after-summary.json' From 180bb589318d074bd6f747ecbf077c560a07d0f2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:17:29 +0100 Subject: [PATCH 120/153] Use constant-vus executor: 50 VUs for 2 minutes Switch the benchmark scenario from shared-iterations to constant-vus so it runs a fixed 50 VUs for a fixed 2m window (configurable via APPWRITE_BENCHMARK_DURATION) instead of draining a shared iteration pool. Drops the now-unused iterations/maxDuration knobs. Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/ci.yml | 4 ++-- tests/benchmarks/http.js | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 13eb44a6f5..d753b1cc0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -686,7 +686,7 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_ITERATIONS: '250' + APPWRITE_BENCHMARK_DURATION: '2m' APPWRITE_BENCHMARK_VUS: '50' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-before-summary.json' @@ -747,7 +747,7 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_ITERATIONS: '250' + APPWRITE_BENCHMARK_DURATION: '2m' APPWRITE_BENCHMARK_VUS: '50' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH: '../../benchmark-before-summary.json' diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index 1e357f7dc9..5930b6efe2 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -18,8 +18,8 @@ const REGION = __ENV.APPWRITE_REGION || 'default'; const REDIRECT_URL = __ENV.APPWRITE_BENCHMARK_REDIRECT_URL || 'http://localhost'; const PASSWORD = __ENV.APPWRITE_BENCHMARK_PASSWORD || 'Password123!'; const WORKER_TIMEOUT_MS = Number(__ENV.APPWRITE_WORKER_TIMEOUT_MS || 120000); -const ITERATIONS = Number(__ENV.APPWRITE_BENCHMARK_ITERATIONS || 1); const VUS = Number(__ENV.APPWRITE_BENCHMARK_VUS || 1); +const DURATION = __ENV.APPWRITE_BENCHMARK_DURATION || '2m'; const SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_SUMMARY_PATH || '/tmp/appwrite-k6-summary.json'; const PREVIOUS_SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH || ''; const PREVIOUS_SUMMARY = PREVIOUS_SUMMARY_PATH ? loadPreviousSummary(PREVIOUS_SUMMARY_PATH) : null; @@ -32,11 +32,10 @@ export const flowFailures = new Counter('appwrite_benchmark_flow_failures'); export const options = { scenarios: { curated_flows: { - executor: 'shared-iterations', + executor: 'constant-vus', exec: 'curatedFlows', vus: VUS, - iterations: ITERATIONS, - maxDuration: __ENV.APPWRITE_BENCHMARK_MAX_DURATION || '30m', + duration: DURATION, }, }, thresholds: { From 2face3c2922c8ca853885c55c1648a15cc3bcb10 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:36:08 +0100 Subject: [PATCH 121/153] Format benchmark numbers as inline code with thousands separators Wrap every numeric cell in inline code and group thousands (e.g. `3,891.22 ms`, `+1,680.87`) so the comment is easier to scan; n/a stays plain. Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/benchmark-comment.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/.github/workflows/benchmark-comment.js b/.github/workflows/benchmark-comment.js index 0de20aac13..75f0658439 100644 --- a/.github/workflows/benchmark-comment.js +++ b/.github/workflows/benchmark-comment.js @@ -110,11 +110,11 @@ function changeCell(before, after, lowerIsBetter) { const percent = Number((((after - before) / before) * 100).toFixed(1)); if (percent === 0) { - return ':white_circle: 0%'; + return `:white_circle: ${code('0%')}`; } const improved = lowerIsBetter ? percent < 0 : percent > 0; - return `${improved ? ':green_circle:' : ':red_circle:'} ${percent > 0 ? '+' : ''}${trimNumber(percent)}%`; + return `${improved ? ':green_circle:' : ':red_circle:'} ${code(`${percent > 0 ? '+' : ''}${trimNumber(percent)}%`)}`; } function detailTable(rows) { @@ -326,16 +326,16 @@ function markdownText(value) { } function formatMs(value) { - return formatNumber(value, 2); + return code(formatNumber(value, 2)); } function formatMsUnit(value) { const formatted = formatNumber(value, 2); - return formatted === 'n/a' ? formatted : `${formatted} ms`; + return formatted === 'n/a' ? formatted : code(`${formatted} ms`); } function formatRate(value) { - return formatNumber(value, 2); + return code(formatNumber(value, 2)); } function formatCount(value) { @@ -343,7 +343,7 @@ function formatCount(value) { return 'n/a'; } - return `${Math.round(value)}`; + return code(Math.round(value).toLocaleString('en-US')); } function formatDelta(before, after) { @@ -352,7 +352,8 @@ function formatDelta(before, after) { } const difference = Number((after - before).toFixed(2)); - return `${difference > 0 ? '+' : ''}${trimNumber(difference)}`; + const sign = difference > 0 ? '+' : difference < 0 ? '-' : ''; + return code(`${sign}${formatNumber(Math.abs(difference), 2)}`); } function formatNumber(value, decimals) { @@ -360,7 +361,14 @@ function formatNumber(value, decimals) { return 'n/a'; } - return trimNumber(Number(value).toFixed(decimals)); + return trimNumber(Number(value).toLocaleString('en-US', { + minimumFractionDigits: decimals, + maximumFractionDigits: decimals, + })); +} + +function code(text) { + return text === 'n/a' ? text : `\`${text}\``; } function trimNumber(value) { From 11397dcba023828e669d68a97dffbebd90692404 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:44:11 +0100 Subject: [PATCH 122/153] Drop absolute p95 latency threshold from benchmark The p(95)<2000ms gate was calibrated for a single VU. At 50 concurrent VUs the CI runner saturates and P95 always exceeds 2s, so the gate failed every run. Keep http_req_failed and flow_failures as the correctness gates; latency is now judged via the before/after comparison in the PR comment. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/benchmarks/http.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index 5930b6efe2..80218225fd 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -40,7 +40,6 @@ export const options = { }, thresholds: { http_req_failed: ['rate<0.05'], - appwrite_api_duration: ['p(95)<2000'], appwrite_benchmark_flow_failures: ['count<1'], }, }; From 101ffff8e2c7adf234b348dbbc2e6865dbeefce2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:46:48 +0100 Subject: [PATCH 123/153] Only color benchmark change when delta exceeds 5% MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Treat changes within ±5% as noise and show a neutral white circle; only swings beyond 5% get a green/red indicator. The percent value is still always shown. Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/benchmark-comment.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/benchmark-comment.js b/.github/workflows/benchmark-comment.js index 75f0658439..bdb0055c60 100644 --- a/.github/workflows/benchmark-comment.js +++ b/.github/workflows/benchmark-comment.js @@ -109,12 +109,14 @@ function changeCell(before, after, lowerIsBetter) { } const percent = Number((((after - before) / before) * 100).toFixed(1)); - if (percent === 0) { - return `:white_circle: ${code('0%')}`; + const label = code(`${percent > 0 ? '+' : ''}${trimNumber(percent)}%`); + + if (Math.abs(percent) <= 5) { + return `:white_circle: ${label}`; } const improved = lowerIsBetter ? percent < 0 : percent > 0; - return `${improved ? ':green_circle:' : ':red_circle:'} ${code(`${percent > 0 ? '+' : ''}${trimNumber(percent)}%`)}`; + return `${improved ? ':green_circle:' : ':red_circle:'} ${label}`; } function detailTable(rows) { From 0050bf47b2d6ac9100c8f284cd874b0860cb5766 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:47:17 +0100 Subject: [PATCH 124/153] Run benchmark at 20 VUs Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d753b1cc0b..832d89aaf8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -687,7 +687,7 @@ jobs: env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' APPWRITE_BENCHMARK_DURATION: '2m' - APPWRITE_BENCHMARK_VUS: '50' + APPWRITE_BENCHMARK_VUS: '20' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-before-summary.json' with: @@ -748,7 +748,7 @@ jobs: env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' APPWRITE_BENCHMARK_DURATION: '2m' - APPWRITE_BENCHMARK_VUS: '50' + APPWRITE_BENCHMARK_VUS: '20' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH: '../../benchmark-before-summary.json' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-after-summary.json' From 5782f4046a4e81e0bbbdf87c3934d6628264aa69 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 15:55:05 +0100 Subject: [PATCH 125/153] Run benchmark for 1 minute instead of 2 Co-Authored-By: Claude Opus 4.8 (1M context) --- .github/workflows/ci.yml | 4 ++-- tests/benchmarks/http.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 832d89aaf8..19493e8443 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -686,7 +686,7 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_DURATION: '2m' + APPWRITE_BENCHMARK_DURATION: '1m' APPWRITE_BENCHMARK_VUS: '20' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_SUMMARY_PATH: 'benchmark-before-summary.json' @@ -747,7 +747,7 @@ jobs: uses: grafana/run-k6-action@de51a7390bdf0ac85a3bef493691bd71d4c7c158 # v1.4.0 env: APPWRITE_ENDPOINT: 'http://localhost:9520/v1' - APPWRITE_BENCHMARK_DURATION: '2m' + APPWRITE_BENCHMARK_DURATION: '1m' APPWRITE_BENCHMARK_VUS: '20' APPWRITE_WORKER_TIMEOUT_MS: '120000' APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH: '../../benchmark-before-summary.json' diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index 80218225fd..e809372a4f 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -19,7 +19,7 @@ const REDIRECT_URL = __ENV.APPWRITE_BENCHMARK_REDIRECT_URL || 'http://localhost' const PASSWORD = __ENV.APPWRITE_BENCHMARK_PASSWORD || 'Password123!'; const WORKER_TIMEOUT_MS = Number(__ENV.APPWRITE_WORKER_TIMEOUT_MS || 120000); const VUS = Number(__ENV.APPWRITE_BENCHMARK_VUS || 1); -const DURATION = __ENV.APPWRITE_BENCHMARK_DURATION || '2m'; +const DURATION = __ENV.APPWRITE_BENCHMARK_DURATION || '1m'; const SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_SUMMARY_PATH || '/tmp/appwrite-k6-summary.json'; const PREVIOUS_SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH || ''; const PREVIOUS_SUMMARY = PREVIOUS_SUMMARY_PATH ? loadPreviousSummary(PREVIOUS_SUMMARY_PATH) : null; From ea2e3b76fbb78dba484337b7bf3d609de9fe3f3e Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 16:17:17 +0100 Subject: [PATCH 126/153] Refocus benchmark on database load, not password hashing The per-iteration account flow ran three CPU-bound password hashing operations (account create, session create, password update) that saturated CPU and masked database performance. Move authentication into setup(): pre-create one signed-in user per VU once (via setupApi, excluded from metrics) and reuse the session across iterations. The account flow now only exercises DB-bound account reads/writes, and the rows flow creates a configurable batch (APPWRITE_BENCHMARK_ROWS, default 5) so database requests dominate the measured mix. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/benchmarks/http.js | 110 +++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index e809372a4f..1b340b1887 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -20,6 +20,7 @@ const PASSWORD = __ENV.APPWRITE_BENCHMARK_PASSWORD || 'Password123!'; const WORKER_TIMEOUT_MS = Number(__ENV.APPWRITE_WORKER_TIMEOUT_MS || 120000); const VUS = Number(__ENV.APPWRITE_BENCHMARK_VUS || 1); const DURATION = __ENV.APPWRITE_BENCHMARK_DURATION || '1m'; +const ROWS_PER_FLOW = Number(__ENV.APPWRITE_BENCHMARK_ROWS || 5); const SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_SUMMARY_PATH || '/tmp/appwrite-k6-summary.json'; const PREVIOUS_SUMMARY_PATH = __ENV.APPWRITE_BENCHMARK_PREVIOUS_SUMMARY_PATH || ''; const PREVIOUS_SUMMARY = PREVIOUS_SUMMARY_PATH ? loadPreviousSummary(PREVIOUS_SUMMARY_PATH) : null; @@ -172,6 +173,7 @@ export function setup() { }, apiHeaders, [201, 409], 'setup.project.platforms.web.create'); const tablesDb = setupTablesDb(apiHeaders); + const users = setupUsers(projectId, VUS); return { runId, @@ -181,10 +183,37 @@ export function setup() { tableId: tablesDb.tableId, consoleSessionHeaders, apiHeaders, + users, platformStatus: platform.status, }; } +// Pre-create one signed-in user per VU so password hashing happens once +// during setup (excluded from metrics) instead of in every iteration. +function setupUsers(projectId, count) { + const headers = projectHeaders(projectId); + const users = []; + + for (let index = 0; index < count; index++) { + const email = `bench-user-${unique('mail')}@example.com`; + setupApi('POST', '/account', { + userId: unique('user'), + email, + password: PASSWORD, + name: 'Benchmark User', + }, headers, [201], 'setup.account.create'); + + const session = setupApi('POST', '/account/sessions/email', { + email, + password: PASSWORD, + }, headers, [201], 'setup.account.session'); + + users.push({ sessionHeaders: { ...headers, Cookie: cookieHeader(session) } }); + } + + return users; +} + function setupTablesDb(apiHeaders) { const databaseId = unique('tdb'); const tableId = unique('tbl'); @@ -218,7 +247,8 @@ function setupTablesDb(apiHeaders) { } export function curatedFlows(data) { - const ctx = { ...data }; + const user = data.users[(__VU - 1) % data.users.length]; + const ctx = { ...data, sessionHeaders: user.sessionHeaders }; try { group('account flow', () => accountFlow(ctx)); @@ -242,62 +272,42 @@ export function teardown(data) { } function accountFlow(ctx) { - const userId = unique('user'); - const email = `bench-user-${unique('mail')}@example.com`; - const headers = projectHeaders(ctx.projectId); + requireSession(ctx, 'accountFlow'); - api('POST', '/account', { - userId, - email, - password: PASSWORD, - name: 'Benchmark User', - }, headers, [201], 'account.create'); - - const session = api('POST', '/account/sessions/email', { - email, - password: PASSWORD, - }, headers, [201], 'account.sessions.email.create'); - - const sessionHeaders = { - ...headers, - Cookie: cookieHeader(session), - }; - - ctx.userId = userId; - ctx.userEmail = email; - ctx.sessionHeaders = sessionHeaders; - - api('GET', '/account', null, sessionHeaders, [200], 'account.get'); - api('GET', '/account/logs', null, sessionHeaders, [200], 'account.logs.list'); - api('PATCH', '/account/prefs', { prefs: { benchmark: true, runId: ctx.runId } }, sessionHeaders, [200], 'account.prefs.update'); - api('PATCH', '/account/name', { name: 'Benchmark User Updated' }, sessionHeaders, [200], 'account.name.update'); - api('PATCH', '/account/password', { password: `${PASSWORD}2`, oldPassword: PASSWORD }, sessionHeaders, [200], 'account.password.update'); + api('GET', '/account', null, ctx.sessionHeaders, [200], 'account.get'); + api('GET', '/account/logs', null, ctx.sessionHeaders, [200], 'account.logs.list'); + api('PATCH', '/account/prefs', { prefs: { benchmark: true, runId: ctx.runId } }, ctx.sessionHeaders, [200], 'account.prefs.update'); + api('PATCH', '/account/name', { name: 'Benchmark User Updated' }, ctx.sessionHeaders, [200], 'account.name.update'); } function tablesDbFlow(ctx) { requireSession(ctx, 'tablesDbFlow'); - const databaseId = ctx.databaseId; - const tableId = ctx.tableId; - const rowId = unique('row'); + const { databaseId, tableId, sessionHeaders } = ctx; + const base = `/tablesdb/${databaseId}/tables/${tableId}/rows`; + const rowIds = []; - api('POST', `/tablesdb/${databaseId}/tables/${tableId}/rows`, { - rowId, - data: tablePayload(), - permissions: ITEM_PERMISSIONS, - }, ctx.sessionHeaders, [201], 'tablesdb.rows.create'); - api('GET', `/tablesdb/${databaseId}/tables/${tableId}/rows`, null, ctx.sessionHeaders, [200], 'tablesdb.rows.list'); - api('GET', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, null, ctx.sessionHeaders, [200], 'tablesdb.rows.get'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, { - data: { title: 'Benchmark Row Updated' }, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.update'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}/quantity/increment`, { - value: 1, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.increment'); - api('PATCH', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}/quantity/decrement`, { - value: 1, - }, ctx.sessionHeaders, [200], 'tablesdb.rows.decrement'); - api('DELETE', `/tablesdb/${databaseId}/tables/${tableId}/rows/${rowId}`, null, ctx.sessionHeaders, [204], 'tablesdb.rows.delete'); + for (let index = 0; index < ROWS_PER_FLOW; index++) { + const rowId = unique('row'); + rowIds.push(rowId); + api('POST', base, { + rowId, + data: tablePayload(), + permissions: ITEM_PERMISSIONS, + }, sessionHeaders, [201], 'tablesdb.rows.create'); + } + + api('GET', base, null, sessionHeaders, [200], 'tablesdb.rows.list'); + + for (const rowId of rowIds) { + api('GET', `${base}/${rowId}`, null, sessionHeaders, [200], 'tablesdb.rows.get'); + api('PATCH', `${base}/${rowId}`, { + data: { title: 'Benchmark Row Updated' }, + }, sessionHeaders, [200], 'tablesdb.rows.update'); + api('PATCH', `${base}/${rowId}/quantity/increment`, { value: 1 }, sessionHeaders, [200], 'tablesdb.rows.increment'); + api('PATCH', `${base}/${rowId}/quantity/decrement`, { value: 1 }, sessionHeaders, [200], 'tablesdb.rows.decrement'); + api('DELETE', `${base}/${rowId}`, null, sessionHeaders, [204], 'tablesdb.rows.delete'); + } } function storageFlow(ctx) { From e29146f9f66cf66ed70d5c610cd374121afd933b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 26 Jun 2026 16:31:40 +0100 Subject: [PATCH 127/153] Clear cookie jar between benchmark user setups setup() runs in a single iteration, so k6 does not reset the per-VU cookie jar between the users we create. The second user's session request carried the first user's session cookie, so the server rejected it with "session already active" (401). Clear the jar after capturing each user's cookie; flows authenticate via explicit Cookie headers, so clearing the jar is safe. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/benchmarks/http.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/benchmarks/http.js b/tests/benchmarks/http.js index 1b340b1887..a41b4778df 100644 --- a/tests/benchmarks/http.js +++ b/tests/benchmarks/http.js @@ -209,6 +209,10 @@ function setupUsers(projectId, count) { }, headers, [201], 'setup.account.session'); users.push({ sessionHeaders: { ...headers, Cookie: cookieHeader(session) } }); + + // Drop the auto-stored session cookie so the next user can sign in; + // setup() runs in a single iteration, so the jar is not reset for us. + http.cookieJar().clear(ENDPOINT); } return users; From dbd45b50b7349a50ae8542a3194c38782d875f46 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 07:16:28 +0530 Subject: [PATCH 128/153] Fix V24 provider trigger attributes --- src/Appwrite/Migration/Version/V24.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index 65da1bd20d..e22f43217a 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -239,6 +239,8 @@ class V24 extends Migration 'startCommand', 'buildSpecification', 'runtimeSpecification', + 'providerBranches', + 'providerPaths', ]; try { $this->createAttributesFromCollection($this->dbForProject, $id, $attributes); @@ -254,6 +256,8 @@ class V24 extends Migration 'deploymentRetention', 'buildSpecification', 'runtimeSpecification', + 'providerBranches', + 'providerPaths', ]; try { $this->createAttributesFromCollection($this->dbForProject, $id, $attributes); From 0477282e213f23714b821dac1dee1ebbfb030c28 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 08:19:12 +0530 Subject: [PATCH 129/153] Fix V24 schema migration gaps --- src/Appwrite/Migration/Version/V24.php | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Appwrite/Migration/Version/V24.php b/src/Appwrite/Migration/Version/V24.php index e22f43217a..82889f8d79 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -115,6 +115,12 @@ class V24 extends Migration } catch (Throwable $th) { Console::warning('Failed to create attributes "' . \implode(', ', $attributes) . "\" in collection {$id}: {$th->getMessage()}"); } + + try { + $this->createIndexFromCollection($this->dbForProject, $id, '_key_teamInternalId'); + } catch (Throwable $th) { + Console::warning("Failed to create index \"_key_teamInternalId\" from {$id}: {$th->getMessage()}"); + } } $this->dbForProject->purgeCachedCollection($id); break; @@ -213,6 +219,15 @@ class V24 extends Migration $this->dbForProject->purgeCachedCollection($id); break; + case 'memberships': + try { + $this->createIndexFromCollection($this->dbForProject, $id, '_key_team_confirm'); + } catch (Throwable $th) { + Console::warning("Failed to create index \"_key_team_confirm\" from {$id}: {$th->getMessage()}"); + } + $this->dbForProject->purgeCachedCollection($id); + break; + case 'tokens': try { $this->createIndexFromCollection($this->dbForProject, $id, '_key_type_expire'); @@ -299,6 +314,32 @@ class V24 extends Migration $this->dbForProject->purgeCachedCollection($id); break; + case 'platforms': + case 'webhooks': + if ($collectionType === 'console') { + try { + $this->createIndexFromCollection($this->dbForProject, $id, '_key_project_id'); + } catch (Throwable $th) { + Console::warning("Failed to create index \"_key_project_id\" from {$id}: {$th->getMessage()}"); + } + } + $this->dbForProject->purgeCachedCollection($id); + break; + + case 'reports': + case 'insights': + if ($collectionType === 'console') { + try { + $this->createCollection($id); + } catch (Throwable $th) { + Console::warning("Failed to create collection \"{$id}\": {$th->getMessage()}"); + + throw $th; + } + } + $this->dbForProject->purgeCachedCollection($id); + break; + case 'providers': try { $this->dbForProject->deleteIndex($id, '_key_name'); From 119cc018311df2e7271f653f01f857004dad029b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 19:49:26 +0530 Subject: [PATCH 130/153] Fix unit test warnings and deprecations --- src/Appwrite/Functions/Validator/Headers.php | 10 +++++++--- src/Appwrite/Task/Validator/Cron.php | 9 +++++++++ tests/extensions/SwooleCleanupSubscriber.php | 17 +++++++++++++++++ tests/extensions/TestHook.php | 1 + 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/extensions/SwooleCleanupSubscriber.php diff --git a/src/Appwrite/Functions/Validator/Headers.php b/src/Appwrite/Functions/Validator/Headers.php index 04003d535b..a18660fa2d 100644 --- a/src/Appwrite/Functions/Validator/Headers.php +++ b/src/Appwrite/Functions/Validator/Headers.php @@ -50,13 +50,17 @@ class Headers extends Validator $size = 0; foreach ($value as $key => $val) { - $length = \strlen($key); // Reject non-string keys - if (!\is_string($key) || $length === 0) { + if (!\is_string($key)) { return false; } - $size += $length + \strlen($val); + $length = \strlen($key); + if ($length === 0) { + return false; + } + + $size += $length + \strlen($val ?? ''); if ($size >= $this->maxSize) { return false; } diff --git a/src/Appwrite/Task/Validator/Cron.php b/src/Appwrite/Task/Validator/Cron.php index 18f3333a57..9e6fcd92a1 100644 --- a/src/Appwrite/Task/Validator/Cron.php +++ b/src/Appwrite/Task/Validator/Cron.php @@ -39,10 +39,19 @@ class Cron extends Validator } try { + \set_error_handler(static function (int $severity, string $message): bool { + if (($severity & E_WARNING) === E_WARNING) { + throw new \RuntimeException($message); + } + + return false; + }); (new CronExpression($value))->getNextRunDate(); return true; } catch (\RuntimeException) { return false; + } finally { + \restore_error_handler(); } } diff --git a/tests/extensions/SwooleCleanupSubscriber.php b/tests/extensions/SwooleCleanupSubscriber.php new file mode 100644 index 0000000000..8f14290211 --- /dev/null +++ b/tests/extensions/SwooleCleanupSubscriber.php @@ -0,0 +1,17 @@ +registerSubscriber(new TestPreparationStartedSubscriber()); $facade->registerSubscriber(new TestFinishedSubscriber(self::MAX_SECONDS_ALLOWED)); $facade->registerSubscriber(new RetrySubscriber()); + $facade->registerSubscriber(new SwooleCleanupSubscriber()); } } From 129f60dc621e2b9609cdbe5a703956ecfe57f15a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 01:33:51 +0000 Subject: [PATCH 131/153] refactor(reference): Renderer takes a Document instead of an array Per review: "Passing an array to construct with no real instructions can get messy. Document param seems sensible." Construction now wraps the request/response/user/project context bag in a Document so the constructor signature is self-describing and consistent with the rest of the codebase. --- app/controllers/shared/api.php | 8 ++++---- src/Appwrite/Reference/Renderer.php | 16 +++------------- tests/unit/Reference/RendererTest.php | 18 +++++++++--------- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index a6b2a769c3..0eec29b7fa 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -899,12 +899,12 @@ Http::shutdown() $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'user' => (array) $targetUser, 'project' => $project, 'request' => $params, 'response' => $responsePayload, - ]); + ])); $resource = $renderer->render($pattern); if (! empty($resource) && $resource !== $pattern) { $auditContext->resource = $resource; @@ -994,12 +994,12 @@ Http::shutdown() $resourcePattern = $route->getLabel('cache.resource', null); $resourceTypePattern = $route->getLabel('cache.resourceType', null); - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'user' => (array) $user, 'project' => $project, 'request' => $requestParams, 'response' => $data, - ]); + ])); try { $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ diff --git a/src/Appwrite/Reference/Renderer.php b/src/Appwrite/Reference/Renderer.php index e69716b407..841560dd1a 100644 --- a/src/Appwrite/Reference/Renderer.php +++ b/src/Appwrite/Reference/Renderer.php @@ -13,18 +13,8 @@ use Utopia\Database\Document; */ class Renderer { - /** @var array> */ - private array $context; - - /** - * @param array|Document|object> $context - */ - public function __construct(array $context) + public function __construct(private readonly Document $context) { - $this->context = []; - foreach ($context as $namespace => $bag) { - $this->context[$namespace] = self::asArray($bag); - } } public function render(string $template): string @@ -33,7 +23,7 @@ class Renderer return $template; } - $fallback = $this->context['response'] ?? []; + $fallback = self::asArray($this->context->getAttribute('response', [])); preg_match_all('/{([^}]+)}/', $template, $matches); foreach ($matches[1] as $pos => $match) { @@ -44,7 +34,7 @@ class Renderer } [$namespace, $key] = $parts; - $bag = $this->context[$namespace] ?? $fallback; + $bag = self::asArray($this->context->getAttribute($namespace, $fallback)); if (!array_key_exists($key, $bag)) { continue; } diff --git a/tests/unit/Reference/RendererTest.php b/tests/unit/Reference/RendererTest.php index b019d0aa42..ac284103cc 100644 --- a/tests/unit/Reference/RendererTest.php +++ b/tests/unit/Reference/RendererTest.php @@ -12,10 +12,10 @@ final class RendererTest extends TestCase { public function testSubstitutesRequestAndResponseNamespaces(): void { - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'request' => ['databaseId' => 'db1'], 'response' => ['$id' => 'col9'], - ]); + ])); $this->assertSame( 'database/db1/collection/col9', @@ -25,19 +25,19 @@ final class RendererTest extends TestCase public function testFallsBackToResponseForUnknownNamespace(): void { - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'response' => ['id' => 'r1'], - ]); + ])); $this->assertSame('thing/r1', $renderer->render('thing/{unknown.id}')); } public function testSupportsUserAndProjectNamespaces(): void { - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'user' => ['$id' => 'usr1'], 'project' => new Document(['$id' => 'prj1']), - ]); + ])); $this->assertSame( 'user/usr1/project/prj1', @@ -47,9 +47,9 @@ final class RendererTest extends TestCase public function testLeavesUnresolvedTokensIntact(): void { - $renderer = new Renderer([ + $renderer = new Renderer(new Document([ 'request' => [], - ]); + ])); $this->assertSame( 'database/{request.databaseId}', @@ -59,7 +59,7 @@ final class RendererTest extends TestCase public function testShortCircuitsWhenNoTemplate(): void { - $renderer = new Renderer([]); + $renderer = new Renderer(new Document([])); $this->assertSame('database/db1', $renderer->render('database/db1')); $this->assertSame('', $renderer->render('')); From 2944259ad3ec9a067d57a9d0ef95c9162039c636 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 01:37:10 +0000 Subject: [PATCH 132/153] revert(usage.resource): drop catalog-route labels per review Function/site templates, runtimes, specifications, frameworks and the top-level Functions/Sites XList don't point at a specific resource - the existing `service` dimension already groups them. Letting the labels through would forge a "function" / "site" resource id that doesn't map to a real entity. --- src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php | 1 - src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php | 1 - .../Platform/Modules/Functions/Http/Specifications/XList.php | 1 - src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php | 1 - src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php | 1 - .../Platform/Modules/Sites/Http/Specifications/XList.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php | 1 - src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php | 1 - 10 files changed, 10 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php index 32f3775107..8e71258501 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/XList.php @@ -37,7 +37,6 @@ class XList extends Base ->groups(['api', 'functions']) ->desc('List functions') ->label('scope', 'functions.read') - ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php index 8ae46bd7cc..73700e122d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Runtimes/XList.php @@ -30,7 +30,6 @@ class XList extends Base ->groups(['api']) ->desc('List runtimes') ->label('scope', 'public') - ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php index 63442d0b8d..8cbff906fa 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Specifications/XList.php @@ -31,7 +31,6 @@ class XList extends Base ->groups(['api', 'functions']) ->desc('List specifications') ->label('scope', 'functions.read') - ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php index 357926a0ea..f66322839b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Templates/Get.php @@ -31,7 +31,6 @@ class Get extends Base ->desc('Get function template') ->groups(['api', 'functions']) ->label('scope', 'public') - ->label('usage.resource', 'function/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php b/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php index 4ef79271b0..f334351fef 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Templates/XList.php @@ -35,7 +35,6 @@ class XList extends Base ->desc('List templates') ->groups(['api']) ->label('scope', 'public') - ->label('usage.resource', 'function') ->label('resourceType', RESOURCE_TYPE_FUNCTIONS) ->label('sdk', new Method( namespace: 'functions', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php index 9a5e3b0623..b1f0a5bc1f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Frameworks/XList.php @@ -29,7 +29,6 @@ class XList extends Base ->desc('List frameworks') ->groups(['api']) ->label('scope', 'public') - ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php index 8d70720a70..2571043363 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/XList.php @@ -37,7 +37,6 @@ class XList extends Base ->desc('List sites') ->groups(['api', 'sites']) ->label('scope', 'sites.read') - ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php index abbdfbfcd0..a4f865d859 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Specifications/XList.php @@ -31,7 +31,6 @@ class XList extends Base ->groups(['api', 'sites']) ->desc('List specifications') ->label('scope', 'sites.read') - ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php index b923ab146d..633ef31e68 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Templates/Get.php @@ -31,7 +31,6 @@ class Get extends Base ->desc('Get site template') ->groups(['api']) ->label('scope', 'public') - ->label('usage.resource', 'site/{request.templateId}') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php b/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php index 335c750d13..722cf986f8 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Templates/XList.php @@ -34,7 +34,6 @@ class XList extends Base ->desc('List templates') ->groups(['api']) ->label('scope', 'public') - ->label('usage.resource', 'site') ->label('resourceType', RESOURCE_TYPE_SITES) ->label('sdk', new Method( namespace: 'sites', From 696b266fc50e394a9d2d26b15a910787238ae6ec Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 04:31:31 +0000 Subject: [PATCH 133/153] fix(projects): mark deployment onboarding stages from the build lifecycle The deployment onboarding stages (functions.createDeployment, functions.updateFunctionDeployment, sites.createDeployment, sites.updateSiteDeployment) never marked on a normal deploy because the shutdown hook keys onboarding off the request's SDK method: - deployments are created via several paths (manual upload, template, duplicate, and VCS webhooks) whose SDK methods differ from createDeployment, and VCS creates the deployment server-side with no labelled request; - activation is performed by the builds worker via the activate flag, not the updateFunctionDeployment/updateSiteDeployment routes, so those stages were effectively unreachable. Mark these stages at the build-lifecycle choke point instead, where every creation path and the worker-driven activation converge. Extract the read-modify-write into a reusable Appwrite\Onboarding\Onboarding helper that persists multiple stages in a single update (so create + activate cannot clobber each other), and have the API shutdown hook reuse it. The builds worker writes onboarding via dbForPlatform - the same platform (console) database it already updates for the rules collection during activation - so the write reaches the central console store. --- app/controllers/shared/api.php | 25 +----- app/init/constants.php | 1 + src/Appwrite/Onboarding/Onboarding.php | 70 +++++++++++++++ .../Modules/Functions/Workers/Builds.php | 19 ++++ tests/unit/Onboarding/OnboardingTest.php | 89 +++++++++++++++++++ 5 files changed, 181 insertions(+), 23 deletions(-) create mode 100644 src/Appwrite/Onboarding/Onboarding.php create mode 100644 tests/unit/Onboarding/OnboardingTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index e30305e55b..6f040fc4b5 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -16,6 +16,7 @@ use Appwrite\Event\Webhook; use Appwrite\Extend\Exception; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Functions\EventProcessor; +use Appwrite\Onboarding\Onboarding; use Appwrite\Platform\Modules\Storage\Config\CacheControl; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; use Appwrite\SDK\Method; @@ -23,7 +24,6 @@ use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Throwable; use Utopia\Abuse\Abuse; use Utopia\Bus\Bus; use Utopia\Cache\Adapter\Filesystem; @@ -1167,16 +1167,6 @@ Http::shutdown() return; } - $byMethod = $project->getAttribute('onboarding', []); - $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; - if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { - return; - } - - if (! \is_array($byMethod)) { - $byMethod = []; - } - $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) ? match ($apiKey->getType()) { API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, @@ -1187,19 +1177,8 @@ Http::shutdown() : (! $user->isEmpty() ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) : ACTOR_TYPE_GUEST); - $byMethod[$method] = [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => DateTime::now(), - 'actorType' => $actorType, - ]; - try { - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $byMethod, - ]))); - } catch (Throwable) { - // Missing `onboarding` attribute on upgraded installs must not break the request lifecycle. - } + $authorization->skip(fn () => Onboarding::complete($dbForPlatform, $project, [$method], $actorType)); }); Http::init() diff --git a/app/init/constants.php b/app/init/constants.php index 9efb6e3222..62819d601b 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -171,6 +171,7 @@ const ACTOR_TYPE_GUEST = 'guest'; const ACTOR_TYPE_KEY_PROJECT = 'keyProject'; const ACTOR_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTOR_TYPE_KEY_ORGANIZATION = 'keyOrganization'; +const ACTOR_TYPE_SYSTEM = 'system'; /** * Project onboarding stage status (stored per SDK method key under project.onboarding JSON). diff --git a/src/Appwrite/Onboarding/Onboarding.php b/src/Appwrite/Onboarding/Onboarding.php new file mode 100644 index 0000000000..555c3a10be --- /dev/null +++ b/src/Appwrite/Onboarding/Onboarding.php @@ -0,0 +1,70 @@ + $methods + */ + public static function complete(Database $dbForPlatform, Document $project, array $methods, string $actorType): void + { + if ($project->getId() === 'console' || $methods === []) { + return; + } + + $onboarding = Config::getParam('onboarding', []); + + $byMethod = $project->getAttribute('onboarding', []); + if (! \is_array($byMethod)) { + $byMethod = []; + } + + $changed = false; + foreach ($methods as $method) { + if (! isset($onboarding[$method])) { + continue; + } + + $status = $byMethod[$method]['status'] ?? null; + if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { + continue; + } + + $byMethod[$method] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; + $changed = true; + } + + if (! $changed) { + return; + } + + try { + $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ])); + } catch (Throwable) { + // Missing `onboarding` attribute on upgraded installs must not break the caller. + } + } +} diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index d0806ccadf..2128dd610a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -13,6 +13,7 @@ use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Filter\BranchDomain as BranchDomainFilter; +use Appwrite\Onboarding\Onboarding; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; @@ -1039,6 +1040,24 @@ class Builds extends Action Span::add('build.activated', true); } + /** + * Mark deployment onboarding stages. Done here (not via the HTTP sdk + * label) so every creation path - manual upload, template, duplicate + * and VCS - and the worker-driven activation are all covered. + */ + $onboardingMethods = match ($resource->getCollection()) { + 'functions' => ['create' => 'functions.createDeployment', 'activate' => 'functions.updateFunctionDeployment'], + 'sites' => ['create' => 'sites.createDeployment', 'activate' => 'sites.updateSiteDeployment'], + default => null, + }; + if ($onboardingMethods !== null) { + $stages = [$onboardingMethods['create']]; + if ($activateBuild) { + $stages[] = $onboardingMethods['activate']; + } + Onboarding::complete($dbForPlatform, $project, $stages, ACTOR_TYPE_SYSTEM); + } + $resource = $this->updateLatestDeployment($dbForProject, $resource); $this->afterDeploymentSuccess( diff --git a/tests/unit/Onboarding/OnboardingTest.php b/tests/unit/Onboarding/OnboardingTest.php new file mode 100644 index 0000000000..97ac84e9e0 --- /dev/null +++ b/tests/unit/Onboarding/OnboardingTest.php @@ -0,0 +1,89 @@ + true, + 'functions.updateFunctionDeployment' => true, + 'sites.createDeployment' => true, + ]); + } + + public function testCompletesMultipleStagesInASingleWrite(): void + { + $project = new Document(['$id' => 'project1', 'onboarding' => []]); + + $dbForPlatform = $this->createMock(Database::class); + $dbForPlatform->expects($this->once()) + ->method('updateDocument') + ->with( + 'projects', + 'project1', + $this->callback(function (Document $document): bool { + $onboarding = $document->getAttribute('onboarding'); + + return isset($onboarding['functions.createDeployment'], $onboarding['functions.updateFunctionDeployment']) + && $onboarding['functions.createDeployment']['status'] === ONBOARDING_STATUS_COMPLETED + && $onboarding['functions.createDeployment']['actorType'] === ACTOR_TYPE_SYSTEM + && ! empty($onboarding['functions.createDeployment']['at']); + }) + ); + + Onboarding::complete($dbForPlatform, $project, [ + 'functions.createDeployment', + 'functions.updateFunctionDeployment', + ], ACTOR_TYPE_SYSTEM); + } + + public function testIgnoresConsoleProject(): void + { + $project = new Document(['$id' => 'console', 'onboarding' => []]); + + $dbForPlatform = $this->createMock(Database::class); + $dbForPlatform->expects($this->never())->method('updateDocument'); + + Onboarding::complete($dbForPlatform, $project, ['functions.createDeployment'], ACTOR_TYPE_SYSTEM); + } + + public function testIgnoresUnknownAndAlreadyCompletedMethods(): void + { + $project = new Document(['$id' => 'project1', 'onboarding' => [ + 'functions.createDeployment' => [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => '2026-01-01 00:00:00.000', + 'actorType' => ACTOR_TYPE_USER, + ], + ]]); + + $dbForPlatform = $this->createMock(Database::class); + $dbForPlatform->expects($this->never())->method('updateDocument'); + + Onboarding::complete($dbForPlatform, $project, ['functions.createDeployment', 'unknown.method'], ACTOR_TYPE_SYSTEM); + } + + public function testDoesNotOverwriteSkippedStage(): void + { + $project = new Document(['$id' => 'project1', 'onboarding' => [ + 'sites.createDeployment' => [ + 'status' => ONBOARDING_STATUS_SKIPPED, + 'at' => '2026-01-01 00:00:00.000', + 'actorType' => ACTOR_TYPE_USER, + ], + ]]); + + $dbForPlatform = $this->createMock(Database::class); + $dbForPlatform->expects($this->never())->method('updateDocument'); + + Onboarding::complete($dbForPlatform, $project, ['sites.createDeployment'], ACTOR_TYPE_SYSTEM); + } +} From 6d52a695a22db66f742d4e4f52a039e104860b69 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 04:44:23 +0000 Subject: [PATCH 134/153] feat(projects): per-type deployment onboarding stages Replace the single createDeployment stage with one key per deployment type (manual, cli, vcs) for functions and sites, keyed off the deployment `type` attribute. Every creation path maps to a type - manual upload -> manual, CLI -> cli, template + VCS/GitHub -> vcs - and the build worker marks the matching stage. Activation still marks updateFunctionDeployment/updateSiteDeployment. The UI can OR the per-type stages for a single "created a deployment" milestone while still tracking which deployment methods a project has used. --- app/config/onboarding.php | 12 +++++- .../Modules/Functions/Workers/Builds.php | 42 ++++++++++++++----- tests/unit/Onboarding/OnboardingTest.php | 26 ++++++------ 3 files changed, 56 insertions(+), 24 deletions(-) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 38f2bb8ec5..5b73328e25 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -40,7 +40,11 @@ return [ // Functions 'functions.create' => true, - 'functions.createDeployment' => true, + // One key per deployment creation type (deployment `type` attribute); + // OR them in the UI for a single "created a deployment" milestone. + 'functions.createManualDeployment' => true, + 'functions.createCliDeployment' => true, + 'functions.createVcsDeployment' => true, 'functions.updateFunctionDeployment' => true, // Messaging @@ -63,6 +67,10 @@ return [ // Sites 'sites.create' => true, - 'sites.createDeployment' => true, + // One key per deployment creation type (deployment `type` attribute); + // OR them in the UI for a single "created a deployment" milestone. + 'sites.createManualDeployment' => true, + 'sites.createCliDeployment' => true, + 'sites.createVcsDeployment' => true, 'sites.updateSiteDeployment' => true, ]; diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 2128dd610a..35ae9948d8 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -1042,19 +1042,41 @@ class Builds extends Action /** * Mark deployment onboarding stages. Done here (not via the HTTP sdk - * label) so every creation path - manual upload, template, duplicate - * and VCS - and the worker-driven activation are all covered. + * label) so every creation path - manual upload, CLI, template, + * duplicate and VCS - and the worker-driven activation are covered. + * The create stage is keyed by the deployment `type` so each type + * has its own stage (OR them in the UI for a single milestone). */ - $onboardingMethods = match ($resource->getCollection()) { - 'functions' => ['create' => 'functions.createDeployment', 'activate' => 'functions.updateFunctionDeployment'], - 'sites' => ['create' => 'sites.createDeployment', 'activate' => 'sites.updateSiteDeployment'], + $collection = $resource->getCollection(); + $createStage = match ($collection) { + 'functions' => match ($deployment->getAttribute('type')) { + 'manual' => 'functions.createManualDeployment', + 'cli' => 'functions.createCliDeployment', + 'vcs' => 'functions.createVcsDeployment', + default => null, + }, + 'sites' => match ($deployment->getAttribute('type')) { + 'manual' => 'sites.createManualDeployment', + 'cli' => 'sites.createCliDeployment', + 'vcs' => 'sites.createVcsDeployment', + default => null, + }, default => null, }; - if ($onboardingMethods !== null) { - $stages = [$onboardingMethods['create']]; - if ($activateBuild) { - $stages[] = $onboardingMethods['activate']; - } + $activateStage = match ($collection) { + 'functions' => 'functions.updateFunctionDeployment', + 'sites' => 'sites.updateSiteDeployment', + default => null, + }; + + $stages = []; + if ($createStage !== null) { + $stages[] = $createStage; + } + if ($activateBuild && $activateStage !== null) { + $stages[] = $activateStage; + } + if ($stages !== []) { Onboarding::complete($dbForPlatform, $project, $stages, ACTOR_TYPE_SYSTEM); } diff --git a/tests/unit/Onboarding/OnboardingTest.php b/tests/unit/Onboarding/OnboardingTest.php index 97ac84e9e0..2ce7d2635d 100644 --- a/tests/unit/Onboarding/OnboardingTest.php +++ b/tests/unit/Onboarding/OnboardingTest.php @@ -13,9 +13,11 @@ final class OnboardingTest extends TestCase protected function setUp(): void { Config::setParam('onboarding', [ - 'functions.createDeployment' => true, + 'functions.createManualDeployment' => true, + 'functions.createCliDeployment' => true, + 'functions.createVcsDeployment' => true, 'functions.updateFunctionDeployment' => true, - 'sites.createDeployment' => true, + 'sites.createVcsDeployment' => true, ]); } @@ -32,15 +34,15 @@ final class OnboardingTest extends TestCase $this->callback(function (Document $document): bool { $onboarding = $document->getAttribute('onboarding'); - return isset($onboarding['functions.createDeployment'], $onboarding['functions.updateFunctionDeployment']) - && $onboarding['functions.createDeployment']['status'] === ONBOARDING_STATUS_COMPLETED - && $onboarding['functions.createDeployment']['actorType'] === ACTOR_TYPE_SYSTEM - && ! empty($onboarding['functions.createDeployment']['at']); + return isset($onboarding['functions.createCliDeployment'], $onboarding['functions.updateFunctionDeployment']) + && $onboarding['functions.createCliDeployment']['status'] === ONBOARDING_STATUS_COMPLETED + && $onboarding['functions.createCliDeployment']['actorType'] === ACTOR_TYPE_SYSTEM + && ! empty($onboarding['functions.createCliDeployment']['at']); }) ); Onboarding::complete($dbForPlatform, $project, [ - 'functions.createDeployment', + 'functions.createCliDeployment', 'functions.updateFunctionDeployment', ], ACTOR_TYPE_SYSTEM); } @@ -52,13 +54,13 @@ final class OnboardingTest extends TestCase $dbForPlatform = $this->createMock(Database::class); $dbForPlatform->expects($this->never())->method('updateDocument'); - Onboarding::complete($dbForPlatform, $project, ['functions.createDeployment'], ACTOR_TYPE_SYSTEM); + Onboarding::complete($dbForPlatform, $project, ['functions.createCliDeployment'], ACTOR_TYPE_SYSTEM); } public function testIgnoresUnknownAndAlreadyCompletedMethods(): void { $project = new Document(['$id' => 'project1', 'onboarding' => [ - 'functions.createDeployment' => [ + 'functions.createCliDeployment' => [ 'status' => ONBOARDING_STATUS_COMPLETED, 'at' => '2026-01-01 00:00:00.000', 'actorType' => ACTOR_TYPE_USER, @@ -68,13 +70,13 @@ final class OnboardingTest extends TestCase $dbForPlatform = $this->createMock(Database::class); $dbForPlatform->expects($this->never())->method('updateDocument'); - Onboarding::complete($dbForPlatform, $project, ['functions.createDeployment', 'unknown.method'], ACTOR_TYPE_SYSTEM); + Onboarding::complete($dbForPlatform, $project, ['functions.createCliDeployment', 'unknown.method'], ACTOR_TYPE_SYSTEM); } public function testDoesNotOverwriteSkippedStage(): void { $project = new Document(['$id' => 'project1', 'onboarding' => [ - 'sites.createDeployment' => [ + 'sites.createVcsDeployment' => [ 'status' => ONBOARDING_STATUS_SKIPPED, 'at' => '2026-01-01 00:00:00.000', 'actorType' => ACTOR_TYPE_USER, @@ -84,6 +86,6 @@ final class OnboardingTest extends TestCase $dbForPlatform = $this->createMock(Database::class); $dbForPlatform->expects($this->never())->method('updateDocument'); - Onboarding::complete($dbForPlatform, $project, ['sites.createDeployment'], ACTOR_TYPE_SYSTEM); + Onboarding::complete($dbForPlatform, $project, ['sites.createVcsDeployment'], ACTOR_TYPE_SYSTEM); } } From d67b9c9326bd1ecf97bc6dd5abece02a88bf3820 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 05:15:07 +0000 Subject: [PATCH 135/153] test: add declare(strict_types=1) to OnboardingTest (Rector) --- tests/unit/Onboarding/OnboardingTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/Onboarding/OnboardingTest.php b/tests/unit/Onboarding/OnboardingTest.php index 2ce7d2635d..e138e8e4ed 100644 --- a/tests/unit/Onboarding/OnboardingTest.php +++ b/tests/unit/Onboarding/OnboardingTest.php @@ -1,5 +1,7 @@ Date: Sun, 28 Jun 2026 05:30:27 +0000 Subject: [PATCH 136/153] refactor: inline deployment onboarding write, drop Onboarding helper Keep the existing inline read-modify-write pattern instead of a shared helper class. The builds worker writes the per-type deployment stages (and activation) directly via dbForPlatform; the API shutdown hook is unchanged from the base branch. Removes the Onboarding helper and its unit test. --- app/controllers/shared/api.php | 25 ++++- src/Appwrite/Onboarding/Onboarding.php | 70 -------------- .../Modules/Functions/Workers/Builds.php | 31 ++++++- tests/unit/Onboarding/OnboardingTest.php | 93 ------------------- 4 files changed, 52 insertions(+), 167 deletions(-) delete mode 100644 src/Appwrite/Onboarding/Onboarding.php delete mode 100644 tests/unit/Onboarding/OnboardingTest.php diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 6f040fc4b5..e30305e55b 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -16,7 +16,6 @@ use Appwrite\Event\Webhook; use Appwrite\Extend\Exception; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Functions\EventProcessor; -use Appwrite\Onboarding\Onboarding; use Appwrite\Platform\Modules\Storage\Config\CacheControl; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; use Appwrite\SDK\Method; @@ -24,6 +23,7 @@ use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Throwable; use Utopia\Abuse\Abuse; use Utopia\Bus\Bus; use Utopia\Cache\Adapter\Filesystem; @@ -1167,6 +1167,16 @@ Http::shutdown() return; } + $byMethod = $project->getAttribute('onboarding', []); + $status = \is_array($byMethod) ? ($byMethod[$method]['status'] ?? null) : null; + if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { + return; + } + + if (! \is_array($byMethod)) { + $byMethod = []; + } + $actorType = ($apiKey !== null && $apiKey->getRole() === User::ROLE_KEYS) ? match ($apiKey->getType()) { API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, @@ -1177,8 +1187,19 @@ Http::shutdown() : (! $user->isEmpty() ? ($mode === APP_MODE_ADMIN ? ACTOR_TYPE_ADMIN : ACTOR_TYPE_USER) : ACTOR_TYPE_GUEST); + $byMethod[$method] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => $actorType, + ]; - $authorization->skip(fn () => Onboarding::complete($dbForPlatform, $project, [$method], $actorType)); + try { + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $byMethod, + ]))); + } catch (Throwable) { + // Missing `onboarding` attribute on upgraded installs must not break the request lifecycle. + } }); Http::init() diff --git a/src/Appwrite/Onboarding/Onboarding.php b/src/Appwrite/Onboarding/Onboarding.php deleted file mode 100644 index 555c3a10be..0000000000 --- a/src/Appwrite/Onboarding/Onboarding.php +++ /dev/null @@ -1,70 +0,0 @@ - $methods - */ - public static function complete(Database $dbForPlatform, Document $project, array $methods, string $actorType): void - { - if ($project->getId() === 'console' || $methods === []) { - return; - } - - $onboarding = Config::getParam('onboarding', []); - - $byMethod = $project->getAttribute('onboarding', []); - if (! \is_array($byMethod)) { - $byMethod = []; - } - - $changed = false; - foreach ($methods as $method) { - if (! isset($onboarding[$method])) { - continue; - } - - $status = $byMethod[$method]['status'] ?? null; - if ($status === ONBOARDING_STATUS_COMPLETED || $status === ONBOARDING_STATUS_SKIPPED) { - continue; - } - - $byMethod[$method] = [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => DateTime::now(), - 'actorType' => $actorType, - ]; - $changed = true; - } - - if (! $changed) { - return; - } - - try { - $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $byMethod, - ])); - } catch (Throwable) { - // Missing `onboarding` attribute on upgraded installs must not break the caller. - } - } -} diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 35ae9948d8..1a64ce235e 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -13,7 +13,6 @@ use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Filter\BranchDomain as BranchDomainFilter; -use Appwrite\Onboarding\Onboarding; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; @@ -1076,8 +1075,36 @@ class Builds extends Action if ($activateBuild && $activateStage !== null) { $stages[] = $activateStage; } + if ($stages !== []) { - Onboarding::complete($dbForPlatform, $project, $stages, ACTOR_TYPE_SYSTEM); + $onboarding = $project->getAttribute('onboarding', []); + if (! \is_array($onboarding)) { + $onboarding = []; + } + + $onboardingChanged = false; + foreach ($stages as $stage) { + $stageStatus = $onboarding[$stage]['status'] ?? null; + if ($stageStatus === ONBOARDING_STATUS_COMPLETED || $stageStatus === ONBOARDING_STATUS_SKIPPED) { + continue; + } + $onboarding[$stage] = [ + 'status' => ONBOARDING_STATUS_COMPLETED, + 'at' => DateTime::now(), + 'actorType' => ACTOR_TYPE_SYSTEM, + ]; + $onboardingChanged = true; + } + + if ($onboardingChanged) { + try { + $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $onboarding, + ])); + } catch (\Throwable) { + // Missing `onboarding` attribute on upgraded installs must not break the build lifecycle. + } + } } $resource = $this->updateLatestDeployment($dbForProject, $resource); diff --git a/tests/unit/Onboarding/OnboardingTest.php b/tests/unit/Onboarding/OnboardingTest.php deleted file mode 100644 index e138e8e4ed..0000000000 --- a/tests/unit/Onboarding/OnboardingTest.php +++ /dev/null @@ -1,93 +0,0 @@ - true, - 'functions.createCliDeployment' => true, - 'functions.createVcsDeployment' => true, - 'functions.updateFunctionDeployment' => true, - 'sites.createVcsDeployment' => true, - ]); - } - - public function testCompletesMultipleStagesInASingleWrite(): void - { - $project = new Document(['$id' => 'project1', 'onboarding' => []]); - - $dbForPlatform = $this->createMock(Database::class); - $dbForPlatform->expects($this->once()) - ->method('updateDocument') - ->with( - 'projects', - 'project1', - $this->callback(function (Document $document): bool { - $onboarding = $document->getAttribute('onboarding'); - - return isset($onboarding['functions.createCliDeployment'], $onboarding['functions.updateFunctionDeployment']) - && $onboarding['functions.createCliDeployment']['status'] === ONBOARDING_STATUS_COMPLETED - && $onboarding['functions.createCliDeployment']['actorType'] === ACTOR_TYPE_SYSTEM - && ! empty($onboarding['functions.createCliDeployment']['at']); - }) - ); - - Onboarding::complete($dbForPlatform, $project, [ - 'functions.createCliDeployment', - 'functions.updateFunctionDeployment', - ], ACTOR_TYPE_SYSTEM); - } - - public function testIgnoresConsoleProject(): void - { - $project = new Document(['$id' => 'console', 'onboarding' => []]); - - $dbForPlatform = $this->createMock(Database::class); - $dbForPlatform->expects($this->never())->method('updateDocument'); - - Onboarding::complete($dbForPlatform, $project, ['functions.createCliDeployment'], ACTOR_TYPE_SYSTEM); - } - - public function testIgnoresUnknownAndAlreadyCompletedMethods(): void - { - $project = new Document(['$id' => 'project1', 'onboarding' => [ - 'functions.createCliDeployment' => [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => '2026-01-01 00:00:00.000', - 'actorType' => ACTOR_TYPE_USER, - ], - ]]); - - $dbForPlatform = $this->createMock(Database::class); - $dbForPlatform->expects($this->never())->method('updateDocument'); - - Onboarding::complete($dbForPlatform, $project, ['functions.createCliDeployment', 'unknown.method'], ACTOR_TYPE_SYSTEM); - } - - public function testDoesNotOverwriteSkippedStage(): void - { - $project = new Document(['$id' => 'project1', 'onboarding' => [ - 'sites.createVcsDeployment' => [ - 'status' => ONBOARDING_STATUS_SKIPPED, - 'at' => '2026-01-01 00:00:00.000', - 'actorType' => ACTOR_TYPE_USER, - ], - ]]); - - $dbForPlatform = $this->createMock(Database::class); - $dbForPlatform->expects($this->never())->method('updateDocument'); - - Onboarding::complete($dbForPlatform, $project, ['sites.createVcsDeployment'], ACTOR_TYPE_SYSTEM); - } -} From 4fc1f737b6a527545761187cd8691bdf91893131 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 05:37:20 +0000 Subject: [PATCH 137/153] fix(builds): re-read project before marking deployment onboarding The injected $project is a job-dispatch-time snapshot and a build can run for minutes. Read the current onboarding map from dbForPlatform before writing so a stage skipped or completed via the API during the build is not overwritten. --- .../Platform/Modules/Functions/Workers/Builds.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 1a64ce235e..7de85fbb91 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -1077,7 +1077,12 @@ class Builds extends Action } if ($stages !== []) { - $onboarding = $project->getAttribute('onboarding', []); + // Re-read the project: the injected $project is a snapshot from + // job-dispatch time, and a build can run for minutes. Reading the + // current onboarding map avoids overwriting a stage that was + // skipped (or completed) via the API while the build was running. + $onboardingProject = $dbForPlatform->getDocument('projects', $project->getId()); + $onboarding = $onboardingProject->getAttribute('onboarding', []); if (! \is_array($onboarding)) { $onboarding = []; } @@ -1096,7 +1101,7 @@ class Builds extends Action $onboardingChanged = true; } - if ($onboardingChanged) { + if ($onboardingChanged && ! $onboardingProject->isEmpty()) { try { $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'onboarding' => $onboarding, From 0fe49223b773556d82b2380bd33f10f19442f664 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 05:48:26 +0000 Subject: [PATCH 138/153] refactor(builds): simplify onboarding write to fetch + set + update Use null-coalescing assignment so only stages not already recorded are set, which inherently leaves a skipped/completed stage untouched. Drops the explicit status guard, changed flag, and isEmpty check. --- .../Modules/Functions/Workers/Builds.php | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 7de85fbb91..0ddb14aef1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -1077,38 +1077,27 @@ class Builds extends Action } if ($stages !== []) { - // Re-read the project: the injected $project is a snapshot from - // job-dispatch time, and a build can run for minutes. Reading the - // current onboarding map avoids overwriting a stage that was - // skipped (or completed) via the API while the build was running. - $onboardingProject = $dbForPlatform->getDocument('projects', $project->getId()); - $onboarding = $onboardingProject->getAttribute('onboarding', []); + // Fetch the project fresh and only set stages that aren't already + // recorded, so a stage skipped or completed via the API while the + // build was running is left untouched (??= respects it). + $onboarding = $dbForPlatform->getDocument('projects', $project->getId())->getAttribute('onboarding', []); if (! \is_array($onboarding)) { $onboarding = []; } - $onboardingChanged = false; foreach ($stages as $stage) { - $stageStatus = $onboarding[$stage]['status'] ?? null; - if ($stageStatus === ONBOARDING_STATUS_COMPLETED || $stageStatus === ONBOARDING_STATUS_SKIPPED) { - continue; - } - $onboarding[$stage] = [ + $onboarding[$stage] ??= [ 'status' => ONBOARDING_STATUS_COMPLETED, 'at' => DateTime::now(), 'actorType' => ACTOR_TYPE_SYSTEM, ]; - $onboardingChanged = true; } - if ($onboardingChanged && ! $onboardingProject->isEmpty()) { - try { - $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $onboarding, - ])); - } catch (\Throwable) { - // Missing `onboarding` attribute on upgraded installs must not break the build lifecycle. - } + try { + $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'onboarding' => $onboarding, + ])); + } catch (\Throwable) { } } From aacab8fcf107e72c4491be14f66502f943984ced Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 06:27:02 +0000 Subject: [PATCH 139/153] feat(projects): track template & duplicate deployment onboarding via the API hook Add functions/sites createTemplateDeployment and createDuplicateDeployment to the onboarding config so the existing api.php shutdown hook marks them in the API request context (which reaches the central console DB), instead of the regional build worker. Reverts the build-worker write and the Onboarding helper. --- app/config/onboarding.php | 20 +++--- app/init/constants.php | 1 - .../Modules/Functions/Workers/Builds.php | 62 ------------------- 3 files changed, 10 insertions(+), 73 deletions(-) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 5b73328e25..6ac29b25ee 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -40,11 +40,11 @@ return [ // Functions 'functions.create' => true, - // One key per deployment creation type (deployment `type` attribute); - // OR them in the UI for a single "created a deployment" milestone. - 'functions.createManualDeployment' => true, - 'functions.createCliDeployment' => true, - 'functions.createVcsDeployment' => true, + // One key per deployment-creation SDK method; OR them in the UI for a + // single "created a deployment" milestone. + 'functions.createDeployment' => true, + 'functions.createTemplateDeployment' => true, + 'functions.createDuplicateDeployment' => true, 'functions.updateFunctionDeployment' => true, // Messaging @@ -67,10 +67,10 @@ return [ // Sites 'sites.create' => true, - // One key per deployment creation type (deployment `type` attribute); - // OR them in the UI for a single "created a deployment" milestone. - 'sites.createManualDeployment' => true, - 'sites.createCliDeployment' => true, - 'sites.createVcsDeployment' => true, + // One key per deployment-creation SDK method; OR them in the UI for a + // single "created a deployment" milestone. + 'sites.createDeployment' => true, + 'sites.createTemplateDeployment' => true, + 'sites.createDuplicateDeployment' => true, 'sites.updateSiteDeployment' => true, ]; diff --git a/app/init/constants.php b/app/init/constants.php index 62819d601b..9efb6e3222 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -171,7 +171,6 @@ const ACTOR_TYPE_GUEST = 'guest'; const ACTOR_TYPE_KEY_PROJECT = 'keyProject'; const ACTOR_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTOR_TYPE_KEY_ORGANIZATION = 'keyOrganization'; -const ACTOR_TYPE_SYSTEM = 'system'; /** * Project onboarding stage status (stored per SDK method key under project.onboarding JSON). diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 0ddb14aef1..d0806ccadf 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -1039,68 +1039,6 @@ class Builds extends Action Span::add('build.activated', true); } - /** - * Mark deployment onboarding stages. Done here (not via the HTTP sdk - * label) so every creation path - manual upload, CLI, template, - * duplicate and VCS - and the worker-driven activation are covered. - * The create stage is keyed by the deployment `type` so each type - * has its own stage (OR them in the UI for a single milestone). - */ - $collection = $resource->getCollection(); - $createStage = match ($collection) { - 'functions' => match ($deployment->getAttribute('type')) { - 'manual' => 'functions.createManualDeployment', - 'cli' => 'functions.createCliDeployment', - 'vcs' => 'functions.createVcsDeployment', - default => null, - }, - 'sites' => match ($deployment->getAttribute('type')) { - 'manual' => 'sites.createManualDeployment', - 'cli' => 'sites.createCliDeployment', - 'vcs' => 'sites.createVcsDeployment', - default => null, - }, - default => null, - }; - $activateStage = match ($collection) { - 'functions' => 'functions.updateFunctionDeployment', - 'sites' => 'sites.updateSiteDeployment', - default => null, - }; - - $stages = []; - if ($createStage !== null) { - $stages[] = $createStage; - } - if ($activateBuild && $activateStage !== null) { - $stages[] = $activateStage; - } - - if ($stages !== []) { - // Fetch the project fresh and only set stages that aren't already - // recorded, so a stage skipped or completed via the API while the - // build was running is left untouched (??= respects it). - $onboarding = $dbForPlatform->getDocument('projects', $project->getId())->getAttribute('onboarding', []); - if (! \is_array($onboarding)) { - $onboarding = []; - } - - foreach ($stages as $stage) { - $onboarding[$stage] ??= [ - 'status' => ONBOARDING_STATUS_COMPLETED, - 'at' => DateTime::now(), - 'actorType' => ACTOR_TYPE_SYSTEM, - ]; - } - - try { - $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'onboarding' => $onboarding, - ])); - } catch (\Throwable) { - } - } - $resource = $this->updateLatestDeployment($dbForProject, $resource); $this->afterDeploymentSuccess( From 808a5f53dfb5a8b834331c2c31565f9bc6351f02 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 06:30:36 +0000 Subject: [PATCH 140/153] feat(projects): drop createDuplicateDeployment from onboarding Duplicating an existing deployment is a redeploy/rollback action that presupposes a prior deployment, so it is not an onboarding milestone. Keeps createDeployment + createTemplateDeployment for functions and sites. --- app/config/onboarding.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 6ac29b25ee..4f51d95e02 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -40,11 +40,10 @@ return [ // Functions 'functions.create' => true, - // One key per deployment-creation SDK method; OR them in the UI for a - // single "created a deployment" milestone. + // Deployment-creation SDK methods; OR them in the UI for a single + // "created a deployment" milestone. 'functions.createDeployment' => true, 'functions.createTemplateDeployment' => true, - 'functions.createDuplicateDeployment' => true, 'functions.updateFunctionDeployment' => true, // Messaging @@ -67,10 +66,9 @@ return [ // Sites 'sites.create' => true, - // One key per deployment-creation SDK method; OR them in the UI for a - // single "created a deployment" milestone. + // Deployment-creation SDK methods; OR them in the UI for a single + // "created a deployment" milestone. 'sites.createDeployment' => true, 'sites.createTemplateDeployment' => true, - 'sites.createDuplicateDeployment' => true, 'sites.updateSiteDeployment' => true, ]; From 971c74bddec6b32efa83252a6cf2dbeace74d41e Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 28 Jun 2026 06:32:53 +0000 Subject: [PATCH 141/153] feat(projects): track VCS deployment onboarding (repo-connected deploy) Add functions/sites createVcsDeployment - the explicit 'deploy from connected repo' endpoint - so connecting a repo to ship a first function/site marks an onboarding stage. Caught by the api hook (API context), like the other methods. --- app/config/onboarding.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/config/onboarding.php b/app/config/onboarding.php index 4f51d95e02..014301a72e 100644 --- a/app/config/onboarding.php +++ b/app/config/onboarding.php @@ -44,6 +44,7 @@ return [ // "created a deployment" milestone. 'functions.createDeployment' => true, 'functions.createTemplateDeployment' => true, + 'functions.createVcsDeployment' => true, 'functions.updateFunctionDeployment' => true, // Messaging @@ -70,5 +71,6 @@ return [ // "created a deployment" milestone. 'sites.createDeployment' => true, 'sites.createTemplateDeployment' => true, + 'sites.createVcsDeployment' => true, 'sites.updateSiteDeployment' => true, ]; From ac44835f6c57d972dff8e124cf6f1ecf1892f6d1 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 29 Jun 2026 04:02:36 +0000 Subject: [PATCH 142/153] fix(audit): record API key internal id as audit actor Requests authenticated with a standard API key produced audit log entries with an empty actor id and internal id. The key role builds a synthetic user with no id, and that user was used as the audit actor, so the actor identity was lost. Enrich the audit actor for standard API keys with the key document's id and sequence so audit entries correctly attribute the acting key. --- app/controllers/shared/api.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 791ab0fe0e..d3b2af0925 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -225,6 +225,13 @@ Http::init() API_KEY_ACCOUNT => ACTOR_TYPE_KEY_ACCOUNT, default => ACTOR_TYPE_KEY_ORGANIZATION, }); + + if ($apiKey->getType() === API_KEY_STANDARD) { + $userClone + ->setAttribute('$id', $dbKey->getId()) + ->setAttribute('$sequence', $dbKey->getSequence()); + } + $auditContext->user = $userClone; } From ed6d4c1e1f7a04a05db0bd25169b735e21b80bc6 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 17:59:32 +0530 Subject: [PATCH 143/153] Generate installer compose from base compose --- .github/workflows/ci.yml | 13 + app/views/install/compose.phtml | 1258 ----------------- docker-compose.override.yml | 477 ++++++- docker-compose.yml | 623 ++++---- docs/tutorials/add-environment-variable.md | 6 +- package-lock.json | 21 +- package.json | 9 +- src/Appwrite/Docker/Compose/Generator.php | 274 ++++ src/Appwrite/Platform/Installer/Server.php | 25 +- src/Appwrite/Platform/Tasks/Install.php | 66 +- tests/unit/Docker/Compose/GeneratorTest.php | 123 ++ .../Platform/Modules/Installer/ServerTest.php | 45 + 12 files changed, 1239 insertions(+), 1701 deletions(-) delete mode 100644 app/views/install/compose.phtml create mode 100644 src/Appwrite/Docker/Compose/Generator.php create mode 100644 tests/unit/Docker/Compose/GeneratorTest.php create mode 100644 tests/unit/Platform/Modules/Installer/ServerTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7ea66ab452..53cf914b97 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -107,6 +107,11 @@ jobs: tools: composer:v2 coverage: none + - name: Setup Node + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 + with: + node-version: '24' + - name: Validate composer.json run: composer validate @@ -114,6 +119,10 @@ jobs: id: install run: composer install --prefer-dist --no-progress --ignore-platform-reqs + - name: Install Node dependencies + if: ${{ !cancelled() && steps.install.outcome == 'success' }} + run: npm ci + - name: Cache PHPStan result cache if: ${{ !cancelled() && steps.install.outcome == 'success' }} uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 @@ -133,6 +142,10 @@ jobs: if: ${{ !cancelled() && steps.install.outcome == 'success' }} run: composer lint + - name: Check Docker Compose formatting + if: ${{ !cancelled() && steps.install.outcome == 'success' }} + run: npm run lint:compose + - name: Run Rector if: ${{ !cancelled() && steps.install.outcome == 'success' }} run: composer refactor:check diff --git a/app/views/install/compose.phtml b/app/views/install/compose.phtml deleted file mode 100644 index f55f9568ab..0000000000 --- a/app/views/install/compose.phtml +++ /dev/null @@ -1,1258 +0,0 @@ -x-logging: &x-logging - logging: - driver: 'json-file' - options: - max-file: '5' - max-size: '10m' -getParam('httpPort', ''); -$httpsPort = $this->getParam('httpsPort', ''); -$version = $this->getParam('version', ''); -$organization = $this->getParam('organization', ''); -$image = $this->getParam('image', ''); -$enableAssistant = $this->getParam('enableAssistant', false); -$executorImages = $this->getParam('executorImages', ''); -$dbService = $this->getParam('database', 'mongodb'); -$allowedDbServices = ['mariadb', 'mongodb', 'postgresql']; -if (!\in_array($dbService, $allowedDbServices, true)) { - $dbService = 'mongodb'; -} -$hostPath = rtrim($this->getParam('hostPath', ''), '/'); -?>services: - traefik: - image: traefik:3.6 - container_name: appwrite-traefik - <<: *x-logging - command: - - --providers.file.directory=/storage/config - - --providers.file.watch=true - - --providers.docker=true - - --providers.docker.exposedByDefault=false - - --providers.docker.constraints=Label(`traefik.constraint-label-stack`,`appwrite`) - - --entrypoints.appwrite_web.address=:80 - - --entrypoints.appwrite_websecure.address=:443 - restart: unless-stopped - ports: - - :80 - - :443 - volumes: - - /var/run/docker.sock:/var/run/docker.sock - - appwrite-config:/storage/config:ro - - appwrite-certificates:/storage/certificates:ro - depends_on: - - appwrite - networks: - - gateway - - appwrite - - appwrite: - image: /: - container_name: appwrite - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - labels: - - traefik.enable=true - - traefik.constraint-label-stack=appwrite - - traefik.docker.network=appwrite - - traefik.http.services.appwrite_api.loadbalancer.server.port=80 - #http - - traefik.http.routers.appwrite_api_http.entrypoints=appwrite_web - - traefik.http.routers.appwrite_api_http.rule=PathPrefix(`/`) - - traefik.http.routers.appwrite_api_http.service=appwrite_api - # https - - traefik.http.routers.appwrite_api_https.entrypoints=appwrite_websecure - - traefik.http.routers.appwrite_api_https.rule=PathPrefix(`/`) - - traefik.http.routers.appwrite_api_https.service=appwrite_api - - traefik.http.routers.appwrite_api_https.tls=true - volumes: - - - ":/usr/src/code:rw" - - - appwrite-uploads:/storage/uploads:rw - - appwrite-imports:/storage/imports:rw - - appwrite-cache:/storage/cache:rw - - appwrite-config:/storage/config:rw - - appwrite-certificates:/storage/certificates:rw - - appwrite-functions:/storage/functions:rw - - appwrite-sites:/storage/sites:rw - - appwrite-builds:/storage/builds:rw - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy -# - clamav - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_LOCALE - - _APP_COMPRESSION_MIN_SIZE_BYTES - - _APP_CONSOLE_WHITELIST_ROOT - - _APP_CONSOLE_WHITELIST_EMAILS - - _APP_CONSOLE_SESSION_ALERTS - - _APP_CONSOLE_WHITELIST_IPS - - _APP_CONSOLE_HOSTNAMES - - _APP_SYSTEM_EMAIL_NAME - - _APP_SYSTEM_EMAIL_ADDRESS - - _APP_EMAIL_SECURITY - - _APP_SYSTEM_RESPONSE_FORMAT - - _APP_OPTIONS_ABUSE - - _APP_OPTIONS_ROUTER_PROTECTION - - _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 - - _APP_DOMAIN_TARGET_A - - _APP_DOMAIN_TARGET_CAA - - _APP_DNS - - _APP_DOMAIN_FUNCTIONS - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_SMTP_HOST - - _APP_SMTP_PORT - - _APP_SMTP_SECURE - - _APP_SMTP_USERNAME - - _APP_SMTP_PASSWORD - - _APP_USAGE_STATS - - _APP_STORAGE_LIMIT - - _APP_STORAGE_PREVIEW_LIMIT - - _APP_STORAGE_ANTIVIRUS - - _APP_STORAGE_ANTIVIRUS_HOST - - _APP_STORAGE_ANTIVIRUS_PORT - - _APP_STORAGE_DEVICE - - _APP_STORAGE_S3_ACCESS_KEY - - _APP_STORAGE_S3_SECRET - - _APP_STORAGE_S3_REGION - - _APP_STORAGE_S3_BUCKET - - _APP_STORAGE_S3_ENDPOINT - - _APP_STORAGE_DO_SPACES_ACCESS_KEY - - _APP_STORAGE_DO_SPACES_SECRET - - _APP_STORAGE_DO_SPACES_REGION - - _APP_STORAGE_DO_SPACES_BUCKET - - _APP_STORAGE_BACKBLAZE_ACCESS_KEY - - _APP_STORAGE_BACKBLAZE_SECRET - - _APP_STORAGE_BACKBLAZE_REGION - - _APP_STORAGE_BACKBLAZE_BUCKET - - _APP_STORAGE_LINODE_ACCESS_KEY - - _APP_STORAGE_LINODE_SECRET - - _APP_STORAGE_LINODE_REGION - - _APP_STORAGE_LINODE_BUCKET - - _APP_STORAGE_WASABI_ACCESS_KEY - - _APP_STORAGE_WASABI_SECRET - - _APP_STORAGE_WASABI_REGION - - _APP_STORAGE_WASABI_BUCKET - - _APP_COMPUTE_SIZE_LIMIT - - _APP_FUNCTIONS_TIMEOUT - - _APP_SITES_TIMEOUT - - _APP_COMPUTE_BUILD_TIMEOUT - - _APP_COMPUTE_CPUS - - _APP_COMPUTE_MEMORY - - _APP_FUNCTIONS_RUNTIMES - - _APP_SITES_RUNTIMES - - _APP_DOMAIN_SITES - - _APP_EXECUTOR_SECRET - - _APP_EXECUTOR_HOST - - _APP_LOGGING_CONFIG - - _APP_MAINTENANCE_INTERVAL - - _APP_MAINTENANCE_DELAY - - _APP_MAINTENANCE_START_TIME - - _APP_MAINTENANCE_RETENTION_EXECUTION - - _APP_MAINTENANCE_RETENTION_CACHE - - _APP_MAINTENANCE_RETENTION_ABUSE - - _APP_MAINTENANCE_RETENTION_AUDIT - - _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE - - _APP_MAINTENANCE_RETENTION_USAGE_HOURLY - - _APP_MAINTENANCE_RETENTION_SCHEDULES - - _APP_SMS_PROVIDER - - _APP_SMS_FROM - - _APP_GRAPHQL_INTROSPECTION - - _APP_GRAPHQL_MAX_BATCH_SIZE - - _APP_GRAPHQL_MAX_COMPLEXITY - - _APP_GRAPHQL_MAX_DEPTH - - _APP_VCS_GITHUB_APP_NAME - - _APP_VCS_GITHUB_PRIVATE_KEY - - _APP_VCS_GITHUB_APP_ID - - _APP_VCS_GITHUB_WEBHOOK_SECRET - - _APP_VCS_GITHUB_CLIENT_SECRET - - _APP_VCS_GITHUB_CLIENT_ID - - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET - - _APP_ASSISTANT_OPENAI_API_KEY - appwrite-console: - <<: *x-logging - container_name: appwrite-console - image: /console:8.7.5 - restart: unless-stopped - networks: - - appwrite - labels: - - "traefik.enable=true" - - "traefik.constraint-label-stack=appwrite" - - "traefik.docker.network=appwrite" - - "traefik.http.services.appwrite_console.loadbalancer.server.port=80" - #ws - - traefik.http.routers.appwrite_console_http.entrypoints=appwrite_web - - traefik.http.routers.appwrite_console_http.rule=PathPrefix(`/console`) - - traefik.http.routers.appwrite_console_http.service=appwrite_console - # wss - - traefik.http.routers.appwrite_console_https.entrypoints=appwrite_websecure - - traefik.http.routers.appwrite_console_https.rule=PathPrefix(`/console`) - - traefik.http.routers.appwrite_console_https.service=appwrite_console - - traefik.http.routers.appwrite_console_https.tls=true - - appwrite-realtime: - image: /: - entrypoint: realtime - container_name: appwrite-realtime - <<: *x-logging - restart: unless-stopped - labels: - - "traefik.enable=true" - - "traefik.constraint-label-stack=appwrite" - - "traefik.docker.network=appwrite" - - "traefik.http.services.appwrite_realtime.loadbalancer.server.port=80" - #ws - - traefik.http.routers.appwrite_realtime_ws.entrypoints=appwrite_web - - traefik.http.routers.appwrite_realtime_ws.rule=PathPrefix(`/v1/realtime`) - - traefik.http.routers.appwrite_realtime_ws.service=appwrite_realtime - # wss - - traefik.http.routers.appwrite_realtime_wss.entrypoints=appwrite_websecure - - traefik.http.routers.appwrite_realtime_wss.rule=PathPrefix(`/v1/realtime`) - - traefik.http.routers.appwrite_realtime_wss.service=appwrite_realtime - - traefik.http.routers.appwrite_realtime_wss.tls=true - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPTIONS_ABUSE - - _APP_OPTIONS_ROUTER_PROTECTION - - _APP_OPENSSL_KEY_V1 - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_USAGE_STATS - - _APP_LOGGING_CONFIG - - appwrite-worker-audits: - image: /: - entrypoint: worker-audits - <<: *x-logging - container_name: appwrite-worker-audits - 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_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - appwrite-worker-webhooks: - image: /: - entrypoint: worker-webhooks - <<: *x-logging - container_name: appwrite-worker-webhooks - 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_EMAIL_SECURITY - - _APP_SYSTEM_SECURITY_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_LOGGING_CONFIG - - appwrite-worker-deletes: - image: /: - entrypoint: worker-deletes - <<: *x-logging - container_name: appwrite-worker-deletes - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - volumes: - - appwrite-uploads:/storage/uploads:rw - - appwrite-cache:/storage/cache:rw - - appwrite-functions:/storage/functions:rw - - appwrite-sites:/storage/sites:rw - - appwrite-builds:/storage/builds:rw - - appwrite-certificates:/storage/certificates:rw - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_STORAGE_DEVICE - - _APP_STORAGE_S3_ACCESS_KEY - - _APP_STORAGE_S3_SECRET - - _APP_STORAGE_S3_REGION - - _APP_STORAGE_S3_BUCKET - - _APP_STORAGE_S3_ENDPOINT - - _APP_STORAGE_DO_SPACES_ACCESS_KEY - - _APP_STORAGE_DO_SPACES_SECRET - - _APP_STORAGE_DO_SPACES_REGION - - _APP_STORAGE_DO_SPACES_BUCKET - - _APP_STORAGE_BACKBLAZE_ACCESS_KEY - - _APP_STORAGE_BACKBLAZE_SECRET - - _APP_STORAGE_BACKBLAZE_REGION - - _APP_STORAGE_BACKBLAZE_BUCKET - - _APP_STORAGE_LINODE_ACCESS_KEY - - _APP_STORAGE_LINODE_SECRET - - _APP_STORAGE_LINODE_REGION - - _APP_STORAGE_LINODE_BUCKET - - _APP_STORAGE_WASABI_ACCESS_KEY - - _APP_STORAGE_WASABI_SECRET - - _APP_STORAGE_WASABI_REGION - - _APP_STORAGE_WASABI_BUCKET - - _APP_LOGGING_CONFIG - - _APP_EXECUTOR_SECRET - - _APP_EXECUTOR_HOST - - _APP_MAINTENANCE_RETENTION_ABUSE - - _APP_MAINTENANCE_RETENTION_AUDIT - - _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE - - _APP_MAINTENANCE_RETENTION_EXECUTION - - _APP_SYSTEM_SECURITY_EMAIL_ADDRESS - - _APP_EMAIL_CERTIFICATES - - appwrite-worker-databases: - image: /: - entrypoint: worker-databases - <<: *x-logging - container_name: appwrite-worker-databases - 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_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - appwrite-worker-builds: - image: /: - entrypoint: worker-builds - <<: *x-logging - container_name: appwrite-worker-builds - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - volumes: - - appwrite-functions:/storage/functions:rw - - appwrite-sites:/storage/sites:rw - - appwrite-builds:/storage/builds:rw - - appwrite-uploads:/storage/uploads:rw - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_EXECUTOR_SECRET - - _APP_EXECUTOR_HOST - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - _APP_VCS_GITHUB_APP_NAME - - _APP_VCS_GITHUB_PRIVATE_KEY - - _APP_VCS_GITHUB_APP_ID - - _APP_FUNCTIONS_TIMEOUT - - _APP_SITES_TIMEOUT - - _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 - - _APP_DOMAIN - - _APP_STORAGE_DEVICE - - _APP_STORAGE_S3_ACCESS_KEY - - _APP_STORAGE_S3_SECRET - - _APP_STORAGE_S3_REGION - - _APP_STORAGE_S3_BUCKET - - _APP_STORAGE_S3_ENDPOINT - - _APP_STORAGE_DO_SPACES_ACCESS_KEY - - _APP_STORAGE_DO_SPACES_SECRET - - _APP_STORAGE_DO_SPACES_REGION - - _APP_STORAGE_DO_SPACES_BUCKET - - _APP_STORAGE_BACKBLAZE_ACCESS_KEY - - _APP_STORAGE_BACKBLAZE_SECRET - - _APP_STORAGE_BACKBLAZE_REGION - - _APP_STORAGE_BACKBLAZE_BUCKET - - _APP_STORAGE_LINODE_ACCESS_KEY - - _APP_STORAGE_LINODE_SECRET - - _APP_STORAGE_LINODE_REGION - - _APP_STORAGE_LINODE_BUCKET - - _APP_STORAGE_WASABI_ACCESS_KEY - - _APP_STORAGE_WASABI_SECRET - - _APP_STORAGE_WASABI_REGION - - _APP_STORAGE_WASABI_BUCKET - - _APP_DOMAIN_SITES - - appwrite-worker-certificates: - image: /: - entrypoint: worker-certificates - <<: *x-logging - container_name: appwrite-worker-certificates - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - volumes: - - appwrite-config:/storage/config:rw - - appwrite-certificates:/storage/certificates:rw - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_DOMAIN - - _APP_DOMAIN_TARGET_CNAME - - _APP_DOMAIN_TARGET_AAAA - - _APP_DOMAIN_TARGET_A - - _APP_DOMAIN_TARGET_CAA - - _APP_DNS - - _APP_DOMAIN_FUNCTIONS - - _APP_EMAIL_CERTIFICATES - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - appwrite-worker-executions: - image: /: - entrypoint: worker-executions - <<: *x-logging - container_name: appwrite-worker-executions - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_ENV - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - appwrite-worker-functions: - image: /: - entrypoint: worker-functions - <<: *x-logging - container_name: appwrite-worker-functions - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - openruntimes-executor: - condition: service_started - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_DOMAIN - - _APP_OPTIONS_FORCE_HTTPS - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_FUNCTIONS_TIMEOUT - - _APP_SITES_TIMEOUT - - _APP_COMPUTE_BUILD_TIMEOUT - - _APP_COMPUTE_CPUS - - _APP_COMPUTE_MEMORY - - _APP_EXECUTOR_SECRET - - _APP_EXECUTOR_HOST - - _APP_USAGE_STATS - - _APP_DOCKER_HUB_USERNAME - - _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_NOTIFICATIONS_TRACKING_SECRET - - _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 - <<: *x-logging - container_name: appwrite-worker-notifications - 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_NOTIFICATIONS_TRACKING_SECRET - - _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-messaging: - image: /: - entrypoint: worker-messaging - <<: *x-logging - container_name: appwrite-worker-messaging - restart: unless-stopped - networks: - - appwrite - volumes: - - appwrite-uploads:/storage/uploads:rw - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - _APP_SMS_FROM - - _APP_SMS_PROVIDER - - _APP_STORAGE_DEVICE - - _APP_STORAGE_S3_ACCESS_KEY - - _APP_STORAGE_S3_SECRET - - _APP_STORAGE_S3_REGION - - _APP_STORAGE_S3_BUCKET - - _APP_STORAGE_S3_ENDPOINT - - _APP_STORAGE_DO_SPACES_ACCESS_KEY - - _APP_STORAGE_DO_SPACES_SECRET - - _APP_STORAGE_DO_SPACES_REGION - - _APP_STORAGE_DO_SPACES_BUCKET - - _APP_STORAGE_BACKBLAZE_ACCESS_KEY - - _APP_STORAGE_BACKBLAZE_SECRET - - _APP_STORAGE_BACKBLAZE_REGION - - _APP_STORAGE_BACKBLAZE_BUCKET - - _APP_STORAGE_LINODE_ACCESS_KEY - - _APP_STORAGE_LINODE_SECRET - - _APP_STORAGE_LINODE_REGION - - _APP_STORAGE_LINODE_BUCKET - - _APP_STORAGE_WASABI_ACCESS_KEY - - _APP_STORAGE_WASABI_SECRET - - _APP_STORAGE_WASABI_REGION - - _APP_STORAGE_WASABI_BUCKET - - appwrite-worker-migrations: - image: /: - entrypoint: worker-migrations - <<: *x-logging - container_name: appwrite-worker-migrations - restart: unless-stopped - networks: - - appwrite - volumes: - - appwrite-imports:/storage/imports:rw - depends_on: - : - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_DOMAIN - - _APP_DOMAIN_TARGET_CNAME - - _APP_DOMAIN_TARGET_AAAA - - _APP_DOMAIN_TARGET_A - - _APP_DOMAIN_TARGET_CAA - - _APP_DNS - - _APP_EMAIL_SECURITY - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_LOGGING_CONFIG - - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET - - appwrite-task-maintenance: - image: /: - entrypoint: maintenance - <<: *x-logging - container_name: appwrite-task-maintenance - restart: unless-stopped - networks: - - appwrite - depends_on: - redis: - condition: service_healthy - : - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_DOMAIN - - _APP_DOMAIN_TARGET_CNAME - - _APP_DOMAIN_TARGET_AAAA - - _APP_DOMAIN_TARGET_A - - _APP_DOMAIN_TARGET_CAA - - _APP_DNS - - _APP_DOMAIN_FUNCTIONS - - _APP_OPENSSL_KEY_V1 - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - _APP_MAINTENANCE_INTERVAL - - _APP_MAINTENANCE_RETENTION_EXECUTION - - _APP_MAINTENANCE_RETENTION_CACHE - - _APP_MAINTENANCE_RETENTION_ABUSE - - _APP_MAINTENANCE_RETENTION_AUDIT - - _APP_MAINTENANCE_RETENTION_AUDIT_CONSOLE - - _APP_MAINTENANCE_RETENTION_USAGE_HOURLY - - _APP_MAINTENANCE_RETENTION_SCHEDULES - - appwrite-task-stats-resources: - image: /: - container_name: appwrite-task-stats-resources - entrypoint: stats-resources - <<: *x-logging - 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_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_USAGE_STATS - - _APP_LOGGING_CONFIG - - _APP_DATABASE_SHARED_TABLES - - _APP_STATS_RESOURCES_INTERVAL - - appwrite-worker-stats-resources: - image: /: - entrypoint: worker-stats-resources - container_name: appwrite-worker-stats-resources - <<: *x-logging - 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_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_USAGE_STATS - - _APP_LOGGING_CONFIG - - _APP_STATS_RESOURCES_INTERVAL - - appwrite-worker-stats-usage: - image: /: - entrypoint: worker-stats-usage - container_name: appwrite-worker-stats-usage - <<: *x-logging - 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_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_USAGE_STATS - - _APP_LOGGING_CONFIG - - _APP_USAGE_AGGREGATION_INTERVAL - - appwrite-task-scheduler-functions: - image: /: - entrypoint: schedule-functions - container_name: appwrite-task-scheduler-functions - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - depends_on: - : - condition: service_healthy - redis: - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_OPTIONS_FORCE_HTTPS - - _APP_DOMAIN - - _APP_CONSOLE_DOMAIN - - _APP_DOMAIN_FUNCTIONS - - _APP_DOMAIN_SITES - - _APP_MIGRATION_HOST - - _APP_CONSOLE_SCHEMA - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - appwrite-task-scheduler-executions: - image: /: - entrypoint: schedule-executions - container_name: appwrite-task-scheduler-executions - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - depends_on: - : - condition: service_healthy - redis: - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_OPTIONS_FORCE_HTTPS - - _APP_DOMAIN - - _APP_CONSOLE_DOMAIN - - _APP_DOMAIN_FUNCTIONS - - _APP_DOMAIN_SITES - - _APP_MIGRATION_HOST - - _APP_CONSOLE_SCHEMA - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - appwrite-task-scheduler-messages: - image: /: - entrypoint: schedule-messages - container_name: appwrite-task-scheduler-messages - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - depends_on: - : - condition: service_healthy - redis: - condition: service_healthy - environment: - - _APP_ENV - - _APP_WORKER_PER_CORE - - _APP_OPENSSL_KEY_V1 - - _APP_REDIS_HOST - - _APP_REDIS_PORT - - _APP_REDIS_USER - - _APP_REDIS_PASS - - _APP_DB_ADAPTER - - _APP_DB_HOST - - _APP_DB_PORT - - _APP_DB_SCHEMA - - _APP_DB_USER - - _APP_DB_PASS - - - appwrite-assistant: - image: appwrite/assistant:0.8.4 - container_name: appwrite-assistant - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - environment: - - _APP_ASSISTANT_OPENAI_API_KEY - - - appwrite-browser: - image: appwrite/browser:0.3.2 - container_name: appwrite-browser - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - - openruntimes-executor: - container_name: exc1 - hostname: exc1 - <<: *x-logging - restart: unless-stopped - stop_signal: SIGINT - image: openruntimes/executor:0.25.1 - networks: - - appwrite - volumes: - - /var/run/docker.sock:/var/run/docker.sock - - appwrite-builds:/storage/builds:rw - - appwrite-functions:/storage/functions:rw - - appwrite-sites:/storage/sites:rw - # Host mount nessessary to share files between executor and runtimes. - # It's not possible to share mount file between 2 containers without host mount (copying is too slow) - - /tmp:/tmp:rw - environment: - - OPR_EXECUTOR_CONNECTION_STORAGE=local://localhost - - OPR_EXECUTOR_CONNECTION_BUILD_CACHE_STORAGE= - - OPR_EXECUTOR_IMAGES= - - 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 - - OPR_EXECUTOR_DOCKER_HUB_PASSWORD=$_APP_DOCKER_HUB_PASSWORD - - OPR_EXECUTOR_ENV=$_APP_ENV - - 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 - - OPR_EXECUTOR_STORAGE_S3_SECRET=$_APP_STORAGE_S3_SECRET - - OPR_EXECUTOR_STORAGE_S3_REGION=$_APP_STORAGE_S3_REGION - - OPR_EXECUTOR_STORAGE_S3_BUCKET=$_APP_STORAGE_S3_BUCKET - - OPR_EXECUTOR_STORAGE_S3_ENDPOINT=$_APP_STORAGE_S3_ENDPOINT - - OPR_EXECUTOR_STORAGE_DO_SPACES_ACCESS_KEY=$_APP_STORAGE_DO_SPACES_ACCESS_KEY - - OPR_EXECUTOR_STORAGE_DO_SPACES_SECRET=$_APP_STORAGE_DO_SPACES_SECRET - - OPR_EXECUTOR_STORAGE_DO_SPACES_REGION=$_APP_STORAGE_DO_SPACES_REGION - - OPR_EXECUTOR_STORAGE_DO_SPACES_BUCKET=$_APP_STORAGE_DO_SPACES_BUCKET - - OPR_EXECUTOR_STORAGE_BACKBLAZE_ACCESS_KEY=$_APP_STORAGE_BACKBLAZE_ACCESS_KEY - - OPR_EXECUTOR_STORAGE_BACKBLAZE_SECRET=$_APP_STORAGE_BACKBLAZE_SECRET - - OPR_EXECUTOR_STORAGE_BACKBLAZE_REGION=$_APP_STORAGE_BACKBLAZE_REGION - - OPR_EXECUTOR_STORAGE_BACKBLAZE_BUCKET=$_APP_STORAGE_BACKBLAZE_BUCKET - - OPR_EXECUTOR_STORAGE_LINODE_ACCESS_KEY=$_APP_STORAGE_LINODE_ACCESS_KEY - - OPR_EXECUTOR_STORAGE_LINODE_SECRET=$_APP_STORAGE_LINODE_SECRET - - OPR_EXECUTOR_STORAGE_LINODE_REGION=$_APP_STORAGE_LINODE_REGION - - OPR_EXECUTOR_STORAGE_LINODE_BUCKET=$_APP_STORAGE_LINODE_BUCKET - - OPR_EXECUTOR_STORAGE_WASABI_ACCESS_KEY=$_APP_STORAGE_WASABI_ACCESS_KEY - - OPR_EXECUTOR_STORAGE_WASABI_SECRET=$_APP_STORAGE_WASABI_SECRET - - OPR_EXECUTOR_STORAGE_WASABI_REGION=$_APP_STORAGE_WASABI_REGION - - OPR_EXECUTOR_STORAGE_WASABI_BUCKET=$_APP_STORAGE_WASABI_BUCKET - - - mariadb: - image: mariadb:10.11 - container_name: appwrite-mariadb - restart: unless-stopped - networks: - - appwrite - volumes: - - appwrite-mariadb:/var/lib/mysql:rw - environment: - - MYSQL_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - - MYSQL_DATABASE=${_APP_DB_SCHEMA} - - MYSQL_USER=${_APP_DB_USER} - - MYSQL_PASSWORD=${_APP_DB_PASS} - - MARIADB_AUTO_UPGRADE=1 - command: 'mysqld --innodb-flush-method=fsync' - healthcheck: - test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] - interval: 10s - timeout: 10s - retries: 10 - start_period: 30s - - - - mongodb: - image: mongo:8.2.5 - container_name: appwrite-mongodb - <<: *x-logging - restart: unless-stopped - networks: - - appwrite - volumes: - - appwrite-mongodb:/data/db - - appwrite-mongodb-keyfile:/data/keyfile - environment: - - MONGO_INITDB_ROOT_USERNAME=root - - MONGO_INITDB_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - - MONGO_INITDB_DATABASE=${_APP_DB_SCHEMA} - - MONGO_INITDB_USERNAME=${_APP_DB_USER} - - MONGO_INITDB_PASSWORD=${_APP_DB_PASS} - entrypoint: - - /bin/bash - - -c - - | - set -e - KEYFILE_PATH="/data/keyfile/mongo-keyfile" - INIT_FLAG="/data/db/.mongodb_initialized" - - # Generate keyfile if it doesn't exist - if [ ! -f "$$KEYFILE_PATH" ]; then - echo "Generating random MongoDB keyfile..." - mkdir -p /data/keyfile - openssl rand -base64 756 > "$$KEYFILE_PATH" - fi - chmod 400 "$$KEYFILE_PATH" - chown mongodb:mongodb "$$KEYFILE_PATH" 2>/dev/null || chown 999:999 "$$KEYFILE_PATH" - - # If not initialized, start without auth first to set up replica set and users - if [ ! -f "$$INIT_FLAG" ]; then - echo "First-time initialization: starting MongoDB without auth..." - mongod --replSet rs0 --bind_ip_all --fork --logpath /var/log/mongodb/mongod.log --dbpath /data/db - - echo "Waiting for MongoDB to start..." - sleep 5 - - echo "Initializing replica set..." - mongosh --eval "rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'mongodb:27017'}]})" - - echo "Waiting for replica set to initialize..." - sleep 5 - - echo "Creating root user..." - mongosh admin --eval "db.createUser({user: '$$MONGO_INITDB_ROOT_USERNAME', pwd: '$$MONGO_INITDB_ROOT_PASSWORD', roles: ['root']})" - - echo "Creating application user..." - mongosh admin --eval "db.createUser({user: '$$MONGO_INITDB_USERNAME', pwd: '$$MONGO_INITDB_PASSWORD', roles: [{role: 'readWrite', db: '$$MONGO_INITDB_DATABASE'}]})" - - echo "Shutting down MongoDB..." - mongod --dbpath /data/db --shutdown - - touch "$$INIT_FLAG" - echo "Initialization complete." - fi - - echo "Starting MongoDB with authentication..." - 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 "const hello = db.adminCommand({hello: 1}); if (!hello.isWritablePrimary) { quit(1); }" - interval: 10s - timeout: 10s - retries: 10 - start_period: 60s - - - - postgresql: - image: appwrite/postgres:0.1.0 - container_name: appwrite-postgresql - restart: unless-stopped - networks: - - appwrite - volumes: - - appwrite-postgresql:/var/lib/postgresql:rw - environment: - - POSTGRES_DB=${_APP_DB_SCHEMA} - - POSTGRES_USER=${_APP_DB_USER} - - POSTGRES_PASSWORD=${_APP_DB_PASS} - command: "postgres" - healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${_APP_DB_USER} -d ${_APP_DB_SCHEMA}"] - interval: 10s - timeout: 10s - retries: 10 - start_period: 30s - - - - redis: - image: redis:7.4.7-alpine - container_name: appwrite-redis - <<: *x-logging - restart: unless-stopped - command: > - redis-server - --maxmemory 512mb - --maxmemory-policy allkeys-lru - --maxmemory-samples 5 - networks: - - appwrite - volumes: - - appwrite-redis:/data:rw - healthcheck: - test: ["CMD", "redis-cli", "ping"] - interval: 10s - timeout: 5s - retries: 5 - start_period: 10s - - # clamav: - # image: appwrite/clamav:1.2.0 - # container_name: appwrite-clamav - # restart: unless-stopped - # networks: - # - appwrite - # volumes: - # - appwrite-uploads:/storage/uploads - -networks: - gateway: - name: gateway - appwrite: - name: appwrite - runtimes: - name: runtimes - -volumes: - - appwrite-mariadb: - - appwrite-postgresql: - - appwrite-mongodb: - appwrite-mongodb-keyfile: - - appwrite-redis: - appwrite-cache: - appwrite-uploads: - appwrite-imports: - appwrite-certificates: - appwrite-functions: - appwrite-sites: - appwrite-builds: - appwrite-config: - openruntimes-node-modules-cache: diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 91625df148..e05067b34d 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,10 +1,373 @@ -# Dev tools for local development only. -# This file is automatically loaded by `docker compose` alongside docker-compose.yml. -# CI sets COMPOSE_FILE=docker-compose.yml explicitly, so these services are excluded from test runs. - services: + appwrite: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - ./docker-compose.yml:/usr/src/code/docker-compose.yml + - ./.env:/usr/src/code/.env + - ./phpunit.xml:/usr/src/code/phpunit.xml + - ./tests:/usr/src/code/tests + - ./app:/usr/src/code/app + - ./docs:/usr/src/code/docs + - ./public:/usr/src/code/public + - ./src:/usr/src/code/src + - ./dev:/usr/src/code/dev + depends_on: + - coredns + ports: + - 9501:80 + dns: + - 127.0.0.11 + - 172.16.238.100 + entrypoint: + - php + - -e + - app/http.php + environment: + - _APP_MESSAGE_SMS_TEST_DSN + - _APP_MESSAGE_EMAIL_TEST_DSN + - _APP_MESSAGE_PUSH_TEST_DSN + - _TESTS_OAUTH2_GITHUB_CLIENT_ID + - _TESTS_OAUTH2_GITHUB_CLIENT_SECRET + appwrite-realtime: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + ports: + - 9502:80 + appwrite-worker-audits: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-webhooks: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + depends_on: + - request-catcher-sms + - request-catcher-webhook + appwrite-worker-deletes: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-databases: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-builds: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-screenshots: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-certificates: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-executions: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-functions: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-mails: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + depends_on: + - maildev + appwrite-worker-messaging: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-migrations: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + - ./tests:/usr/src/code/tests + appwrite-task-maintenance: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-task-interval: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-task-stats-resources: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-stats-resources: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-worker-stats-usage: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-task-scheduler-functions: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-task-scheduler-executions: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + appwrite-task-scheduler-messages: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + coredns: + image: coredns/coredns:1.12.4 + container_name: appwrite-coredns + restart: unless-stopped + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m + command: + - -conf + - /mnt/resources/Corefile + networks: + appwrite: + ipv4_address: 172.16.238.100 + volumes: + - ./tests/resources/coredns:/mnt/resources:ro + maildev: + image: appwrite/mailcatcher:1.1.1 + container_name: appwrite-mailcatcher + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m + ports: + - 9503:1080 + networks: + - appwrite + - gateway + labels: + - traefik.enable=true + - traefik.constraint-label-stack=appwrite + - traefik.docker.network=gateway + - traefik.http.services.appwrite_maildev.loadbalancer.server.port=1080 + - traefik.http.routers.appwrite_maildev_http.entrypoints=appwrite_web + - traefik.http.routers.appwrite_maildev_http.rule=Host(`mail.localhost`) + - traefik.http.routers.appwrite_maildev_http.service=appwrite_maildev + - traefik.http.routers.appwrite_maildev_https.entrypoints=appwrite_websecure + - traefik.http.routers.appwrite_maildev_https.rule=Host(`mail.localhost`) + - traefik.http.routers.appwrite_maildev_https.service=appwrite_maildev + - traefik.http.routers.appwrite_maildev_https.tls=true + environment: + - MAILDEV_INCOMING_USER=${_APP_SMTP_USERNAME} + - MAILDEV_INCOMING_PASS=${_APP_SMTP_PASSWORD} + request-catcher-webhook: + image: appwrite/requestcatcher:1.1.0 + container_name: appwrite-requestcatcher-webhook + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m + ports: + - 9504:5000 + networks: + - appwrite + request-catcher-sms: + image: appwrite/requestcatcher:1.1.0 + container_name: appwrite-requestcatcher-sms + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m + ports: + - 9507:5000 + networks: + - appwrite appwrite-mongo-express: - profiles: ["mongodb"] + profiles: + - mongodb image: mongo-express container_name: appwrite-mongo-express networks: @@ -12,13 +375,12 @@ services: ports: - "9508:8081" environment: - ME_CONFIG_MONGODB_URL: "mongodb://root:${_APP_DB_ROOT_PASS}@appwrite-mongodb:27017/?replicaSet=rs0&directConnection=true" + ME_CONFIG_MONGODB_URL: mongodb://root:${_APP_DB_ROOT_PASS}@appwrite-mongodb:27017/?replicaSet=rs0&directConnection=true ME_CONFIG_BASICAUTH_USERNAME: ${_APP_DB_USER} ME_CONFIG_BASICAUTH_PASSWORD: ${_APP_DB_PASS} depends_on: mongodb: condition: service_healthy - adminer: image: adminer container_name: appwrite-adminer @@ -37,20 +399,19 @@ services: configs: - source: adminer-index.php target: /var/www/html/index.php - mode: 0755 + mode: 493 labels: - - "traefik.enable=true" - - "traefik.constraint-label-stack=appwrite" - - "traefik.docker.network=gateway" - - "traefik.http.services.appwrite_adminer.loadbalancer.server.port=8080" - - "traefik.http.routers.appwrite_adminer_http.entrypoints=appwrite_web" - - "traefik.http.routers.appwrite_adminer_http.rule=Host(`mysql.localhost`)" - - "traefik.http.routers.appwrite_adminer_http.service=appwrite_adminer" - - "traefik.http.routers.appwrite_adminer_https.entrypoints=appwrite_websecure" - - "traefik.http.routers.appwrite_adminer_https.rule=Host(`mysql.localhost`)" - - "traefik.http.routers.appwrite_adminer_https.service=appwrite_adminer" - - "traefik.http.routers.appwrite_adminer_https.tls=true" - + - traefik.enable=true + - traefik.constraint-label-stack=appwrite + - traefik.docker.network=gateway + - traefik.http.services.appwrite_adminer.loadbalancer.server.port=8080 + - traefik.http.routers.appwrite_adminer_http.entrypoints=appwrite_web + - traefik.http.routers.appwrite_adminer_http.rule=Host(`mysql.localhost`) + - traefik.http.routers.appwrite_adminer_http.service=appwrite_adminer + - traefik.http.routers.appwrite_adminer_https.entrypoints=appwrite_websecure + - traefik.http.routers.appwrite_adminer_https.rule=Host(`mysql.localhost`) + - traefik.http.routers.appwrite_adminer_https.service=appwrite_adminer + - traefik.http.routers.appwrite_adminer_https.tls=true redis-insight: image: redis/redisinsight:latest restart: unless-stopped @@ -62,22 +423,21 @@ services: configs: - source: redisinsight-connections.json target: /mnt/connections.json - mode: 0755 + mode: 493 labels: - - "traefik.enable=true" - - "traefik.constraint-label-stack=appwrite" - - "traefik.docker.network=gateway" - - "traefik.http.services.appwrite_redisinsight.loadbalancer.server.port=5540" - - "traefik.http.routers.appwrite_redisinsight_http.entrypoints=appwrite_web" - - "traefik.http.routers.appwrite_redisinsight_http.rule=Host(`redis.localhost`)" - - "traefik.http.routers.appwrite_redisinsight_http.service=appwrite_redisinsight" - - "traefik.http.routers.appwrite_redisinsight_https.entrypoints=appwrite_websecure" - - "traefik.http.routers.appwrite_redisinsight_https.rule=Host(`redis.localhost`)" - - "traefik.http.routers.appwrite_redisinsight_https.service=appwrite_redisinsight" - - "traefik.http.routers.appwrite_redisinsight_https.tls=true" + - traefik.enable=true + - traefik.constraint-label-stack=appwrite + - traefik.docker.network=gateway + - traefik.http.services.appwrite_redisinsight.loadbalancer.server.port=5540 + - traefik.http.routers.appwrite_redisinsight_http.entrypoints=appwrite_web + - traefik.http.routers.appwrite_redisinsight_http.rule=Host(`redis.localhost`) + - traefik.http.routers.appwrite_redisinsight_http.service=appwrite_redisinsight + - traefik.http.routers.appwrite_redisinsight_https.entrypoints=appwrite_websecure + - traefik.http.routers.appwrite_redisinsight_https.rule=Host(`redis.localhost`) + - traefik.http.routers.appwrite_redisinsight_https.service=appwrite_redisinsight + - traefik.http.routers.appwrite_redisinsight_https.tls=true ports: - "9514:5540" - graphql-explorer: container_name: appwrite-graphql-explorer image: appwrite/altair:0.3.0 @@ -85,10 +445,51 @@ services: networks: - appwrite ports: - - "9509:3000" + - 9509:3000 environment: - SERVER_URL=http://localhost:9520/v1/graphql - + traefik: + command: + - --providers.file.directory=/storage/config + - --providers.file.watch=true + - --providers.docker=true + - --providers.docker.exposedByDefault=false + - --providers.docker.constraints=Label(`traefik.constraint-label-stack`,`appwrite`) + - --entrypoints.appwrite_web.address=:80 + - --entrypoints.appwrite_websecure.address=:443 + - --log.level=DEBUG + - --api.insecure=true + - --accesslog=true + ports: + - 9520:80 + - 9522:80 + - 9521:443 + - 9500:8080 + networks: + appwrite: + aliases: + - appwrite.test + gateway: + aliases: + - appwrite.test + runtimes: + aliases: + - appwrite.test + mariadb: + ports: + - "9510:3306" + mongodb: + ports: + - "9511:27017" + postgresql: + ports: + - "9512:5432" + redis: + ports: + - "9513:6379" + appwrite-embedding: + ports: + - "9505:3000" configs: redisinsight-connections.json: content: | @@ -129,7 +530,6 @@ configs: "tags": [] } ] - adminer-index.php: content: | /dev/null"] + test: + - CMD-SHELL + - 'curl -fsS -H "Authorization: Bearer $$OPR_EXECUTOR_SECRET" http://localhost/v1/health + >/dev/null' interval: 5s timeout: 3s retries: 20 start_period: 5s - mariadb: - image: mariadb:10.11 # fix issues when upgrading using: mysql_upgrade -u root -p + image: mariadb:10.11 container_name: appwrite-mariadb - <<: *x-logging + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m networks: - appwrite volumes: - appwrite-mariadb:/var/lib/mysql:rw - ports: - - "9510:3306" environment: - MYSQL_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - MYSQL_DATABASE=${_APP_DB_SCHEMA} - MYSQL_USER=${_APP_DB_USER} - MYSQL_PASSWORD=${_APP_DB_PASS} - MARIADB_AUTO_UPGRADE=1 - command: "mysqld --innodb-flush-method=fsync" + command: + - mysqld + - --innodb-flush-method=fsync healthcheck: - test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + test: + - CMD + - healthcheck.sh + - --connect + - --innodb_initialized interval: 5s timeout: 5s retries: 12 - mongodb: image: mongo:8.2.5 container_name: appwrite-mongodb - <<: *x-logging + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m restart: on-failure:3 networks: - appwrite @@ -1395,15 +1339,15 @@ services: - appwrite-mongodb-keyfile:/data/keyfile - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro - ./mongo-entrypoint.sh:/mongo-entrypoint.sh:ro - ports: - - "9511:27017" environment: - MONGO_INITDB_ROOT_USERNAME=root - MONGO_INITDB_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - MONGO_INITDB_DATABASE=${_APP_DB_SCHEMA} - MONGO_INITDB_USERNAME=${_APP_DB_USER} - MONGO_INITDB_PASSWORD=${_APP_DB_PASS} - entrypoint: ["/bin/bash", "/mongo-entrypoint.sh"] + entrypoint: + - /bin/bash + - /mongo-entrypoint.sh healthcheck: test: | bash -c " @@ -1426,167 +1370,96 @@ services: timeout: 10s retries: 10 start_period: 30s - postgresql: image: appwrite/postgres:0.1.0 container_name: appwrite-postgresql - <<: *x-logging + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m networks: - appwrite volumes: - appwrite-postgresql:/var/lib/postgresql/18/data:rw - ports: - - "9512:5432" environment: - POSTGRES_DB=${_APP_DB_SCHEMA} - POSTGRES_USER=${_APP_DB_USER} - POSTGRES_PASSWORD=${_APP_DB_PASS} healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${_APP_DB_USER} -d ${_APP_DB_SCHEMA}"] + test: + - CMD-SHELL + - pg_isready -U ${_APP_DB_USER} -d ${_APP_DB_SCHEMA} interval: 5s timeout: 5s retries: 10 start_period: 10s - command: > - postgres - -c fsync=off - -c synchronous_commit=off - -c full_page_writes=off - + command: + - postgres + - -c + - fsync=off + - -c + - synchronous_commit=off + - -c + - full_page_writes=off appwrite-embedding: image: appwrite/embedding:0.1.0 environment: EMBEDDING_MODELS: ${_APP_EMBEDDING_MODELS:-nomic-embed-text} container_name: appwrite-embedding - ports: - - "9505:3000" restart: unless-stopped - # persistent for caching models across restarts and preloading volumes: - appwrite-models:/home/embedder/models networks: - appwrite - redis: image: redis:7.4.7-alpine - <<: *x-logging + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m container_name: appwrite-redis - command: > - redis-server - --maxmemory 512mb - --maxmemory-policy allkeys-lru - --maxmemory-samples 5 - ports: - - "9513:6379" + command: + - redis-server + - --maxmemory + - 512mb + - --maxmemory-policy + - allkeys-lru + - --maxmemory-samples + - "5" networks: - appwrite volumes: - appwrite-redis:/data:rw healthcheck: - test: ["CMD", "redis-cli", "ping"] + test: + - CMD + - redis-cli + - ping interval: 5s timeout: 5s retries: 12 - - coredns: # DNS server for testing purposes (Proxy APIs) - image: coredns/coredns:1.12.4 - container_name: appwrite-coredns - restart: unless-stopped - <<: *x-logging - command: ["-conf", "/mnt/resources/Corefile"] - # If you need to debug CoreDNS, do it from "appwrite container", or port forward: - # ports: - # - "53:53" - networks: - appwrite: - ipv4_address: 172.16.238.100 - volumes: - - ./tests/resources/coredns:/mnt/resources:ro - - # Dev Tools Start ------------------------------------------------------------------------------------------ - # - # The Appwrite Team uses the following tools to help debug, monitor and diagnose the Appwrite stack - # - # Here is a description of the different tools and why are we using them: - # - # MailCatcher - An SMTP server. Catches all system emails and displays them in a nice UI. - # RequestCatcher - An HTTP server. Catches all system https calls and displays them using a simple HTTP API. Used to debug & tests webhooks and HTTP tasks - # Redis Insight - A nice UI for exploring Redis data - # Adminer - A nice UI for exploring MariaDB data - # GraphQl Explorer - A nice UI for exploring GraphQL API - - maildev: # used mainly for dev tests - image: appwrite/mailcatcher:1.1.1 - container_name: appwrite-mailcatcher - <<: *x-logging - ports: - - "9503:1080" - networks: - - appwrite - - gateway - labels: - - "traefik.enable=true" - - "traefik.constraint-label-stack=appwrite" - - "traefik.docker.network=gateway" - - "traefik.http.services.appwrite_maildev.loadbalancer.server.port=1080" - - "traefik.http.routers.appwrite_maildev_http.entrypoints=appwrite_web" - - "traefik.http.routers.appwrite_maildev_http.rule=Host(`mail.localhost`)" - - "traefik.http.routers.appwrite_maildev_http.service=appwrite_maildev" - - "traefik.http.routers.appwrite_maildev_https.entrypoints=appwrite_websecure" - - "traefik.http.routers.appwrite_maildev_https.rule=Host(`mail.localhost`)" - - "traefik.http.routers.appwrite_maildev_https.service=appwrite_maildev" - - "traefik.http.routers.appwrite_maildev_https.tls=true" - environment: - - MAILDEV_INCOMING_USER=${_APP_SMTP_USERNAME} - - MAILDEV_INCOMING_PASS=${_APP_SMTP_PASSWORD} - - request-catcher-webhook: # used mainly for dev tests (mock HTTP webhook) - image: appwrite/requestcatcher:1.1.0 - container_name: appwrite-requestcatcher-webhook - <<: *x-logging - ports: - - "9504:5000" - networks: - - appwrite - - request-catcher-sms: # used mainly for dev tests (mock SMS auth secret) - image: appwrite/requestcatcher:1.1.0 - container_name: appwrite-requestcatcher-sms - <<: *x-logging - ports: - - "9507:5000" - networks: - - appwrite - - # Dev tools (adminer, redis-insight, mongo-express, graphql-explorer) - # are defined in docker-compose.override.yml - networks: gateway: name: gateway appwrite: name: appwrite - ipam: - config: - - subnet: 172.16.238.0/24 runtimes: name: runtimes - - - volumes: - appwrite-mariadb: - appwrite-mongodb: - appwrite-mongodb-keyfile: - appwrite-postgresql: - appwrite-redis: - appwrite-cache: - appwrite-uploads: - appwrite-imports: - appwrite-certificates: - appwrite-functions: - appwrite-sites: - appwrite-builds: - appwrite-config: - appwrite-models: - openruntimes-node-modules-cache: + appwrite-mariadb: null + appwrite-mongodb: null + appwrite-mongodb-keyfile: null + appwrite-postgresql: null + appwrite-redis: null + appwrite-cache: null + appwrite-uploads: null + appwrite-imports: null + appwrite-certificates: null + appwrite-functions: null + appwrite-sites: null + appwrite-builds: null + appwrite-config: null + appwrite-models: null + openruntimes-node-modules-cache: null diff --git a/docs/tutorials/add-environment-variable.md b/docs/tutorials/add-environment-variable.md index 5116908580..7fdd416642 100644 --- a/docs/tutorials/add-environment-variable.md +++ b/docs/tutorials/add-environment-variable.md @@ -24,11 +24,11 @@ This information is also used to generate the website documentation at https://a If the newly introduced environment variable has a default value, add it to the `.env` and `Dockerfile` along with other environment variables. `.env` file uses settings for Appwrite development environment. -### Add to Docker Compose file and template +### Add to Docker Compose file -Add the new environment variables to the `docker-compose.yml` and `app/views/install/compose.phtml` for each docker service that requires access to those environment variables. +Add the new environment variables to `docker-compose.yml` for each Docker service that requires access to those environment variables. -The `docker-compose.yml` file is used by the Appwrite maintainers during development, whereas the `app/views/install/compose.phtml` file is used by the Appwrite setup script. +The setup script generates the install compose file from `docker-compose.yml`, so changes should be made in the base compose file instead of a separate template. Run `npm run format:compose` after editing compose files. With these steps, your environment variable is properly added and can be accessed inside Appwrite code and any other containers where it is passed. You can access and use those variables to implement the features you are trying to achieve. diff --git a/package-lock.json b/package-lock.json index 6ab33b14fe..138de0201d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,7 +4,26 @@ "requires": true, "packages": { "": { - "name": "@appwrite.io/repo" + "name": "@appwrite.io/repo", + "devDependencies": { + "prettier": "^3.9.0" + } + }, + "node_modules/prettier": { + "version": "3.9.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.9.0.tgz", + "integrity": "sha512-LjIqSIC5VYLzs9WedVmJ2ljNAGnU+DteIClbahu4L/DBeWjZ6iT/k1lAYyu9JUh+1xINxWadaPw/Pl63y/agAw==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } } } } diff --git a/package.json b/package.json index 1e2b1be430..9dfab20a21 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,15 @@ "type": "git", "url": "git+https://github.com/appwrite/appwrite.git" }, + "scripts": { + "format:compose": "prettier --write docker-compose.yml docker-compose.override.yml", + "lint:compose": "prettier --check docker-compose.yml docker-compose.override.yml" + }, "files": [ "docs/examples", "app/config/specs" - ] + ], + "devDependencies": { + "prettier": "^3.9.0" + } } diff --git a/src/Appwrite/Docker/Compose/Generator.php b/src/Appwrite/Docker/Compose/Generator.php new file mode 100644 index 0000000000..b9e8c1574b --- /dev/null +++ b/src/Appwrite/Docker/Compose/Generator.php @@ -0,0 +1,274 @@ + [ + 'default' => 'mongodb', + 'services' => [ + 'mariadb', + 'mongodb', + 'postgresql', + ], + ], + ]; + + private const array SELECTABLE_VOLUME_GROUPS = [ + 'database' => [ + 'mariadb' => ['appwrite-mariadb'], + 'mongodb' => ['appwrite-mongodb', 'appwrite-mongodb-keyfile'], + 'postgresql' => ['appwrite-postgresql'], + ], + ]; + + private const array OPTIONAL_SERVICES = [ + 'enableAssistant' => 'appwrite-assistant', + ]; + + private const array LOCAL_VOLUME_PREPENDS = [ + 'appwrite' => [ + 'param' => 'hostPath', + 'volume' => '{hostPath}:/usr/src/code:rw', + ], + ]; + + private const array PARAM_DEFAULTS = [ + 'version' => 'latest', + 'database' => 'mongodb', + 'hostPath' => '', + 'enableAssistant' => false, + ]; + + private const array DEPENDENCY_SELECTORS = [ + 'database' => [ + 'param' => 'database', + 'condition' => [ + 'condition' => 'service_healthy', + ], + 'placeholders' => [ + '${_APP_DB_HOST:-mongodb}', + '${_APP_DB_HOST:-mariadb}', + ], + ], + ]; + + private const array YAML_DOCUMENT_MARKER_PATTERNS = [ + '/^---\R/', + '/\R\\.\\.\\.\R?$/', + ]; + + /** + * @var array + */ + private array $compose; + + /** + * @var array + */ + private array $params = []; + + private array $selectableServices = [ + 'mariadb', + 'mongodb', + 'postgresql', + ]; + + public function __construct(string $compose) + { + $parsed = \yaml_parse($compose); + + if (!\is_array($parsed)) { + throw new Exception('Failed to parse Docker Compose file'); + } + + $this->compose = $parsed; + } + + /** + * @param array $params + */ + public function render(array $params): string + { + $compose = $this->compose; + $this->params = $this->normalizeParams($params); + $this->selectableServices = $this->getSelectableServices(); + + $compose['services'] = $this->filterServices($compose['services'] ?? []); + $compose['volumes'] = $this->filterVolumes($compose['volumes'] ?? []); + + foreach ($compose['services'] as $name => &$service) { + if (!\is_array($service)) { + continue; + } + + $this->rewriteDependencies($service); + $this->rewriteLocalVolumes($name, $service); + } + unset($service); + + return $this->emit($compose); + } + + /** + * @param array $params + * @return array + */ + private function normalizeParams(array $params): array + { + $params = \array_merge(self::PARAM_DEFAULTS, $params); + + foreach (self::SELECTABLE_SERVICE_GROUPS as $param => $config) { + if (!\in_array($params[$param], $config['services'], true)) { + $params[$param] = $config['default']; + } + } + + $params['hostPath'] = \rtrim((string)$params['hostPath'], '/'); + + return $params; + } + + /** + * @return string[] + */ + private function getSelectableServices(): array + { + $services = []; + + foreach (self::SELECTABLE_SERVICE_GROUPS as $config) { + $services = \array_merge($services, $config['services']); + } + + return \array_values(\array_unique($services)); + } + + /** + * @param array $services + * @return array + */ + private function filterServices(array $services): array + { + foreach (self::SELECTABLE_SERVICE_GROUPS as $param => $config) { + foreach ($config['services'] as $service) { + if ($service !== $this->params[$param]) { + unset($services[$service]); + } + } + } + + foreach (self::OPTIONAL_SERVICES as $param => $service) { + if (empty($this->params[$param])) { + unset($services[$service]); + } + } + + return $services; + } + + /** + * @param array $volumes + * @return array + */ + private function filterVolumes(array $volumes): array + { + foreach (self::SELECTABLE_VOLUME_GROUPS as $param => $groups) { + foreach ($groups as $service => $names) { + if ($service === $this->params[$param]) { + continue; + } + + foreach ($names as $name) { + unset($volumes[$name]); + } + } + } + + return $volumes; + } + + /** + * @param array $service + */ + private function rewriteDependencies(array &$service): void + { + if (!isset($service['depends_on'])) { + return; + } + + foreach (self::DEPENDENCY_SELECTORS as $selector) { + $selected = $this->params[$selector['param']]; + $removable = [ + ...$this->selectableServices, + ...$selector['placeholders'], + ]; + + if (\array_is_list($service['depends_on'])) { + $dependsOn = \array_values(\array_filter( + $service['depends_on'], + fn (mixed $dependency): bool => !\in_array($dependency, $removable, true) + )); + + if (!\in_array($selected, $dependsOn, true)) { + $dependsOn[] = $selected; + } + + $service['depends_on'] = $dependsOn; + continue; + } + + foreach ($removable as $dependency) { + unset($service['depends_on'][$dependency]); + } + + $service['depends_on'][$selected] = $selector['condition']; + } + } + + /** + * @param array $service + */ + private function rewriteLocalVolumes(string $name, array &$service): void + { + if ($this->params['version'] !== 'local' || !isset(self::LOCAL_VOLUME_PREPENDS[$name])) { + return; + } + + $config = self::LOCAL_VOLUME_PREPENDS[$name]; + if (empty($this->params[$config['param']])) { + return; + } + + $service['volumes'] ??= []; + \array_unshift($service['volumes'], $this->replacePlaceholders($config['volume'])); + } + + /** + * @param array $compose + */ + private function emit(array $compose): string + { + $yaml = \yaml_emit($compose, YAML_UTF8_ENCODING, YAML_LN_BREAK); + + foreach (self::YAML_DOCUMENT_MARKER_PATTERNS as $pattern) { + $yaml = \preg_replace($pattern, '', $yaml) ?? $yaml; + } + + return $yaml; + } + + private function replacePlaceholders(string $value): string + { + foreach ($this->params as $param => $replacement) { + if (\is_scalar($replacement)) { + $value = \str_replace('{' . $param . '}', (string)$replacement, $value); + } + } + + return $value; + } + +} diff --git a/src/Appwrite/Platform/Installer/Server.php b/src/Appwrite/Platform/Installer/Server.php index b86db4d93d..35cd3b01cb 100644 --- a/src/Appwrite/Platform/Installer/Server.php +++ b/src/Appwrite/Platform/Installer/Server.php @@ -52,6 +52,10 @@ class Server private const string DEFAULT_IMAGE = 'appwrite-dev'; public const string DEFAULT_CONTAINER = 'appwrite-installer'; + private const string COMPOSE_FILE = 'docker-compose.yml'; + private const string ENV_FILE = '.env'; + private const string LOCAL_COMPOSE_FILE = 'docker-compose.web-installer.yml'; + private const string LOCAL_ENV_FILE = '.env.web-installer'; private State $state; private array $paths = []; @@ -208,8 +212,7 @@ class Server } $basePath = $config->isLocal() ? '/usr/src/code' : (getcwd() ?: '.'); - $composePath = $basePath . '/docker-compose.yml'; - $envPath = $basePath . '/.env'; + [$composePath, $envPath] = $this->getUpgradeDetectionPaths($config, $basePath); if (!file_exists($composePath) && !file_exists($envPath)) { return; @@ -227,6 +230,24 @@ class Server } } + /** + * @return array{0: string, 1: string} + */ + private function getUpgradeDetectionPaths(Config $config, string $basePath): array + { + if ($config->isLocal()) { + return [ + $basePath . '/' . self::LOCAL_COMPOSE_FILE, + $basePath . '/' . self::LOCAL_ENV_FILE, + ]; + } + + return [ + $basePath . '/' . self::COMPOSE_FILE, + $basePath . '/' . self::ENV_FILE, + ]; + } + private function detectDatabaseFromFiles(string $composePath, string $envPath): ?string { $dbServices = ['mariadb', 'mongodb', 'postgresql']; diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index 8c9b86dedb..6c966ee287 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Docker\Compose; +use Appwrite\Docker\Compose\Generator; use Appwrite\Docker\Env; use Appwrite\Platform\Installer\Runtime\State; use Appwrite\Platform\Installer\Server as InstallerServer; @@ -532,7 +533,7 @@ class Install extends Action } $templateForEnv = new View($this->buildFromProjectPath('/app/views/install/env.phtml')); - $templateForCompose = new View($this->buildFromProjectPath('/app/views/install/compose.phtml')); + $composeGenerator = new Generator((string)\file_get_contents($this->buildFromProjectPath('/docker-compose.yml'))); $database = $input['_APP_DB_ADAPTER'] ?? 'mongodb'; @@ -557,16 +558,18 @@ class Install extends Action } $executorImages = \implode(',', \array_unique($runtimeImages)); - $templateForCompose - ->setParam('httpPort', $httpPort) - ->setParam('httpsPort', $httpsPort) - ->setParam('version', $version) - ->setParam('organization', $organization) - ->setParam('image', $image) - ->setParam('database', $database) - ->setParam('hostPath', $this->hostPath) - ->setParam('enableAssistant', $enableAssistant) - ->setParam('executorImages', $executorImages); + $input['_APP_HTTP_PORT'] = $httpPort; + $input['_APP_HTTPS_PORT'] = $httpsPort; + $input['_APP_IMAGE'] = "{$organization}/{$image}"; + $input['_APP_VERSION'] = $version; + $input['_APP_EXECUTOR_IMAGES'] = $executorImages; + + $composeContent = $composeGenerator->render([ + 'version' => $version, + 'database' => $database, + 'hostPath' => $this->hostPath, + 'enableAssistant' => $enableAssistant, + ]); $templateForEnv->setParam('vars', $input); @@ -604,7 +607,7 @@ class Install extends Action $this->updateProgress($progress, InstallerServer::STEP_DOCKER_COMPOSE, InstallerServer::STATUS_IN_PROGRESS, $messages); if (!$useExistingConfig) { - $this->writeComposeFile($templateForCompose); + $this->writeComposeFile($composeContent); } $this->updateProgress($progress, InstallerServer::STEP_DOCKER_COMPOSE, InstallerServer::STATUS_COMPLETED, $messages); @@ -623,7 +626,7 @@ class Install extends Action } if ($database === 'mongodb' && !$useExistingConfig && $startIndex <= 1) { - $this->copyMongoEntrypointIfNeeded(); + $this->copyMongoFilesIfNeeded(); } if (!$noStart) { @@ -639,7 +642,7 @@ class Install extends Action } } - if (!$isLocalInstall || file_exists('/.dockerenv') || file_exists('/run/.containerenv')) { + if (!$isLocalInstall && (file_exists('/.dockerenv') || file_exists('/run/.containerenv'))) { $this->connectInstallerToAppwriteNetwork(); } @@ -919,10 +922,9 @@ class Install extends Action $healthPath = '/v1/health/version'; if ($isLocalInstall) { - $candidates = [ - self::APPWRITE_API_URL . $healthPath, - 'http://localhost:' . $httpPort . $healthPath, - ]; + $candidates = (file_exists('/.dockerenv') || file_exists('/run/.containerenv')) + ? ['http://host.docker.internal:' . $httpPort . $healthPath] + : ['http://localhost:' . $httpPort . $healthPath]; } else { $candidates = [ self::APPWRITE_API_URL . $healthPath, @@ -1079,11 +1081,10 @@ class Install extends Action ]; } - private function writeComposeFile(View $template): void + private function writeComposeFile(string $renderedContent): void { $composeFileName = $this->getComposeFileName(); $targetPath = $this->path . '/' . $composeFileName; - $renderedContent = $template->render(false); $result = @file_put_contents($targetPath, $renderedContent); if ($result === false) { @@ -1101,13 +1102,18 @@ class Install extends Action } } - private function copyMongoEntrypointIfNeeded(): void + private function copyMongoFilesIfNeeded(): void { - $mongoEntrypoint = $this->buildFromProjectPath('/mongo-entrypoint.sh'); + $files = [ + 'mongo-entrypoint.sh', + 'mongo-init.js', + ]; - if (file_exists($mongoEntrypoint)) { - // Always use container path for file operations - copy($mongoEntrypoint, $this->path . '/mongo-entrypoint.sh'); + foreach ($files as $file) { + $source = $this->buildFromProjectPath('/' . $file); + if (file_exists($source)) { + copy($source, $this->path . '/' . $file); + } } } @@ -1153,6 +1159,16 @@ class Install extends Action $command[] = '--project-directory'; $command[] = $composePath; + + $validateCommand = $command; + $validateCommand[] = 'config'; + $validateCommand[] = '--quiet'; + \exec($env . implode(' ', array_map(escapeshellarg(...), $validateCommand)) . ' 2>&1', $validateOutput, $validateExit); + if ($validateExit !== 0) { + $message = trim(implode("\n", $validateOutput)); + throw new \RuntimeException('Invalid Docker Compose file', 0, $message !== '' ? new \RuntimeException($message) : null); + } + $command[] = 'up'; $command[] = '-d'; $command[] = '--remove-orphans'; diff --git a/tests/unit/Docker/Compose/GeneratorTest.php b/tests/unit/Docker/Compose/GeneratorTest.php new file mode 100644 index 0000000000..03e29c8112 --- /dev/null +++ b/tests/unit/Docker/Compose/GeneratorTest.php @@ -0,0 +1,123 @@ +assertIsString($compose); + + $this->generator = new Generator($compose); + } + + public function testSelectsDatabaseService(): void + { + $compose = $this->render([ + 'database' => 'mariadb', + 'enableAssistant' => false, + ]); + + $this->assertArrayHasKey('mariadb', $compose['services']); + $this->assertArrayNotHasKey('mongodb', $compose['services']); + $this->assertArrayNotHasKey('postgresql', $compose['services']); + $this->assertArrayHasKey('appwrite-mariadb', $compose['volumes']); + $this->assertArrayNotHasKey('appwrite-mongodb', $compose['volumes']); + $this->assertArrayNotHasKey('appwrite-postgresql', $compose['volumes']); + } + + public function testTogglesAssistantService(): void + { + $disabled = $this->render([ + 'enableAssistant' => false, + ]); + $enabled = $this->render([ + 'enableAssistant' => true, + ]); + + $this->assertArrayNotHasKey('appwrite-assistant', $disabled['services']); + $this->assertArrayHasKey('appwrite-assistant', $enabled['services']); + } + + public function testKeepsProductionWorkers(): void + { + $compose = $this->render(); + + $this->assertArrayHasKey('appwrite-worker-screenshots', $compose['services']); + $this->assertArrayHasKey('appwrite-task-interval', $compose['services']); + $this->assertArrayHasKey('appwrite-embedding', $compose['services']); + } + + public function testKeepsMongoInitFiles(): void + { + $compose = $this->render([ + 'database' => 'mongodb', + ]); + + $this->assertContains('./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro', $compose['services']['mongodb']['volumes']); + $this->assertContains('./mongo-entrypoint.sh:/mongo-entrypoint.sh:ro', $compose['services']['mongodb']['volumes']); + } + + public function testAddsLocalHostPathMount(): void + { + $compose = $this->render([ + 'version' => 'local', + 'hostPath' => '/tmp/appwrite', + ]); + + $this->assertSame('/tmp/appwrite:/usr/src/code:rw', $compose['services']['appwrite']['volumes'][0]); + } + + public function testKeepsLongCommandsReadable(): void + { + $mariadb = $this->render([ + 'database' => 'mariadb', + ]); + $postgresql = $this->render([ + 'database' => 'postgresql', + ]); + + $this->assertSame(['mysqld', '--innodb-flush-method=fsync'], $mariadb['services']['mariadb']['command']); + $this->assertSame([ + 'postgres', + '-c', + 'fsync=off', + '-c', + 'synchronous_commit=off', + '-c', + 'full_page_writes=off', + ], $postgresql['services']['postgresql']['command']); + $this->assertSame([ + 'redis-server', + '--maxmemory', + '512mb', + '--maxmemory-policy', + 'allkeys-lru', + '--maxmemory-samples', + '5', + ], $postgresql['services']['redis']['command']); + } + + /** + * @param array $params + * @return array + */ + private function render(array $params = []): array + { + $yaml = $this->generator->render($params); + $compose = \yaml_parse($yaml); + + $this->assertIsArray($compose); + + return $compose; + } +} diff --git a/tests/unit/Platform/Modules/Installer/ServerTest.php b/tests/unit/Platform/Modules/Installer/ServerTest.php new file mode 100644 index 0000000000..1e68e461a7 --- /dev/null +++ b/tests/unit/Platform/Modules/Installer/ServerTest.php @@ -0,0 +1,45 @@ +getUpgradeDetectionPaths(new Config([ + 'isLocal' => true, + ])); + + $this->assertSame([ + '/tmp/appwrite/docker-compose.web-installer.yml', + '/tmp/appwrite/.env.web-installer', + ], $paths); + } + + public function testProductionUpgradeDetectionUsesDefaultFiles(): void + { + $paths = $this->getUpgradeDetectionPaths(new Config()); + + $this->assertSame([ + '/tmp/appwrite/docker-compose.yml', + '/tmp/appwrite/.env', + ], $paths); + } + + /** + * @return array{0: string, 1: string} + */ + private function getUpgradeDetectionPaths(Config $config): array + { + $method = new ReflectionMethod(Server::class, 'getUpgradeDetectionPaths'); + + return $method->invoke(new Server(), $config, '/tmp/appwrite'); + } +} From b470cac5e3a7a6eec4f6b502926ea954631b3fc1 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 18:41:29 +0530 Subject: [PATCH 144/153] Load docker-compose.override.yml in CI docker-compose.yml is now the production base; dev/test services and settings (appwrite-dev build target, test DSNs, request-catcher, maildev, coredns) live in docker-compose.override.yml. Setting COMPOSE_FILE to only docker-compose.yml made Compose skip the override, so CI booted the released production image without the test environment and the appwrite container never became healthy. Include the override in COMPOSE_FILE across the workflows that build and run the stack so the test stack matches a local 'docker compose up'. --- .github/workflows/ci.yml | 2 +- .github/workflows/sdk-preview.yml | 2 +- .github/workflows/specs.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53cf914b97..2a4e8ab5bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,7 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: - COMPOSE_FILE: docker-compose.yml + COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml IMAGE: appwrite-dev K6_VERSION: '0.53.0' diff --git a/.github/workflows/sdk-preview.yml b/.github/workflows/sdk-preview.yml index dacc37a64a..535b3712f6 100644 --- a/.github/workflows/sdk-preview.yml +++ b/.github/workflows/sdk-preview.yml @@ -1,7 +1,7 @@ name: "SDK Preview" env: - COMPOSE_FILE: docker-compose.yml + COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml on: workflow_dispatch: diff --git a/.github/workflows/specs.yml b/.github/workflows/specs.yml index 85c76bacd3..50a70bd4bb 100644 --- a/.github/workflows/specs.yml +++ b/.github/workflows/specs.yml @@ -1,7 +1,7 @@ name: "Generate Specs" env: - COMPOSE_FILE: docker-compose.yml + COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml on: workflow_dispatch: From b43ab099fd1ce9ecf368c285a5fd89217f6b3bc7 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 19:32:21 +0530 Subject: [PATCH 145/153] (refactor): inline getUpgradeDetectionPaths helper --- src/Appwrite/Platform/Installer/Server.php | 26 ++++++---------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/Appwrite/Platform/Installer/Server.php b/src/Appwrite/Platform/Installer/Server.php index 35cd3b01cb..06be8a6ad1 100644 --- a/src/Appwrite/Platform/Installer/Server.php +++ b/src/Appwrite/Platform/Installer/Server.php @@ -212,7 +212,13 @@ class Server } $basePath = $config->isLocal() ? '/usr/src/code' : (getcwd() ?: '.'); - [$composePath, $envPath] = $this->getUpgradeDetectionPaths($config, $basePath); + if ($config->isLocal()) { + $composePath = $basePath . '/' . self::LOCAL_COMPOSE_FILE; + $envPath = $basePath . '/' . self::LOCAL_ENV_FILE; + } else { + $composePath = $basePath . '/' . self::COMPOSE_FILE; + $envPath = $basePath . '/' . self::ENV_FILE; + } if (!file_exists($composePath) && !file_exists($envPath)) { return; @@ -230,24 +236,6 @@ class Server } } - /** - * @return array{0: string, 1: string} - */ - private function getUpgradeDetectionPaths(Config $config, string $basePath): array - { - if ($config->isLocal()) { - return [ - $basePath . '/' . self::LOCAL_COMPOSE_FILE, - $basePath . '/' . self::LOCAL_ENV_FILE, - ]; - } - - return [ - $basePath . '/' . self::COMPOSE_FILE, - $basePath . '/' . self::ENV_FILE, - ]; - } - private function detectDatabaseFromFiles(string $composePath, string $envPath): ?string { $dbServices = ['mariadb', 'mongodb', 'postgresql']; From 5b461fa75790b1bbbc737396fdffcf836634bcff Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sat, 27 Jun 2026 19:37:20 +0530 Subject: [PATCH 146/153] remove file --- .../Platform/Modules/Installer/ServerTest.php | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Installer/ServerTest.php diff --git a/tests/unit/Platform/Modules/Installer/ServerTest.php b/tests/unit/Platform/Modules/Installer/ServerTest.php deleted file mode 100644 index 1e68e461a7..0000000000 --- a/tests/unit/Platform/Modules/Installer/ServerTest.php +++ /dev/null @@ -1,45 +0,0 @@ -getUpgradeDetectionPaths(new Config([ - 'isLocal' => true, - ])); - - $this->assertSame([ - '/tmp/appwrite/docker-compose.web-installer.yml', - '/tmp/appwrite/.env.web-installer', - ], $paths); - } - - public function testProductionUpgradeDetectionUsesDefaultFiles(): void - { - $paths = $this->getUpgradeDetectionPaths(new Config()); - - $this->assertSame([ - '/tmp/appwrite/docker-compose.yml', - '/tmp/appwrite/.env', - ], $paths); - } - - /** - * @return array{0: string, 1: string} - */ - private function getUpgradeDetectionPaths(Config $config): array - { - $method = new ReflectionMethod(Server::class, 'getUpgradeDetectionPaths'); - - return $method->invoke(new Server(), $config, '/tmp/appwrite'); - } -} From 0aeb79a4adcedd9f44dc42ab378405e69b8491ca Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 28 Jun 2026 11:22:37 +0530 Subject: [PATCH 147/153] Fix compose dependency rewriting --- src/Appwrite/Docker/Compose/Generator.php | 22 ++++++++++++++++++--- src/Appwrite/Platform/Tasks/Install.php | 7 ++++++- tests/unit/Docker/Compose/GeneratorTest.php | 11 +++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Docker/Compose/Generator.php b/src/Appwrite/Docker/Compose/Generator.php index b9e8c1574b..f982d80178 100644 --- a/src/Appwrite/Docker/Compose/Generator.php +++ b/src/Appwrite/Docker/Compose/Generator.php @@ -207,12 +207,21 @@ class Generator ]; if (\array_is_list($service['depends_on'])) { + $hasSelectedDependency = false; $dependsOn = \array_values(\array_filter( $service['depends_on'], - fn (mixed $dependency): bool => !\in_array($dependency, $removable, true) + function (mixed $dependency) use ($removable, &$hasSelectedDependency): bool { + if (!\in_array($dependency, $removable, true)) { + return true; + } + + $hasSelectedDependency = true; + + return false; + } )); - if (!\in_array($selected, $dependsOn, true)) { + if ($hasSelectedDependency && !\in_array($selected, $dependsOn, true)) { $dependsOn[] = $selected; } @@ -220,11 +229,18 @@ class Generator continue; } + $hasSelectedDependency = false; foreach ($removable as $dependency) { + if (\array_key_exists($dependency, $service['depends_on'])) { + $hasSelectedDependency = true; + } + unset($service['depends_on'][$dependency]); } - $service['depends_on'][$selected] = $selector['condition']; + if ($hasSelectedDependency) { + $service['depends_on'][$selected] = $selector['condition']; + } } } diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index 6c966ee287..a9433c59e6 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -533,7 +533,12 @@ class Install extends Action } $templateForEnv = new View($this->buildFromProjectPath('/app/views/install/env.phtml')); - $composeGenerator = new Generator((string)\file_get_contents($this->buildFromProjectPath('/docker-compose.yml'))); + $composePath = $this->buildFromProjectPath('/docker-compose.yml'); + $composeYaml = \file_get_contents($composePath); + if ($composeYaml === false) { + throw new \RuntimeException('Failed to read docker-compose.yml from ' . $composePath); + } + $composeGenerator = new Generator($composeYaml); $database = $input['_APP_DB_ADAPTER'] ?? 'mongodb'; diff --git a/tests/unit/Docker/Compose/GeneratorTest.php b/tests/unit/Docker/Compose/GeneratorTest.php index 03e29c8112..3741a6004b 100644 --- a/tests/unit/Docker/Compose/GeneratorTest.php +++ b/tests/unit/Docker/Compose/GeneratorTest.php @@ -77,6 +77,17 @@ final class GeneratorTest extends TestCase $this->assertSame('/tmp/appwrite:/usr/src/code:rw', $compose['services']['appwrite']['volumes'][0]); } + public function testDoesNotAddDatabaseDependencyWithoutPlaceholder(): void + { + $compose = $this->render([ + 'database' => 'postgresql', + ]); + + $this->assertSame(['appwrite'], $compose['services']['traefik']['depends_on']); + $this->assertContains('postgresql', $compose['services']['appwrite']['depends_on']); + $this->assertNotContains('${_APP_DB_HOST:-mongodb}', $compose['services']['appwrite']['depends_on']); + } + public function testKeepsLongCommandsReadable(): void { $mariadb = $this->render([ From 8d08094ccecae62e23c5139b368dcee59926474a Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 28 Jun 2026 11:31:05 +0530 Subject: [PATCH 148/153] Handle Mongo installer copy failures --- src/Appwrite/Platform/Tasks/Install.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Tasks/Install.php b/src/Appwrite/Platform/Tasks/Install.php index a9433c59e6..b2a8843049 100644 --- a/src/Appwrite/Platform/Tasks/Install.php +++ b/src/Appwrite/Platform/Tasks/Install.php @@ -1117,7 +1117,12 @@ class Install extends Action foreach ($files as $file) { $source = $this->buildFromProjectPath('/' . $file); if (file_exists($source)) { - copy($source, $this->path . '/' . $file); + $target = $this->path . '/' . $file; + if (@copy($source, $target) === false) { + $lastError = error_get_last(); + $errorMsg = $lastError ? $lastError['message'] : 'Unknown error'; + throw new \RuntimeException('Failed to copy ' . $file . ' to ' . $target . ': ' . $errorMsg); + } } } } From 83b21090c29d6931126926483e9aad27bc3776a0 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 29 Jun 2026 10:25:01 +0530 Subject: [PATCH 149/153] Adapt compose generator changes for main --- docker-compose.override.yml | 14 ++++++++++++++ docker-compose.yml | 13 +++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/docker-compose.override.yml b/docker-compose.override.yml index e05067b34d..ced607d9ca 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -175,6 +175,20 @@ services: - ./src:/usr/src/code/src depends_on: - maildev + appwrite-worker-notifications: + build: + context: . + target: development + args: + DEBUG: false + TESTING: true + VERSION: dev + image: appwrite-dev + volumes: + - ./app:/usr/src/code/app + - ./src:/usr/src/code/src + depends_on: + - maildev appwrite-worker-messaging: build: context: . diff --git a/docker-compose.yml b/docker-compose.yml index d0957dd30a..9a43dc1dad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -773,17 +773,18 @@ services: appwrite-worker-notifications: entrypoint: worker-notifications - <<: [*x-logging, *x-build] + logging: + driver: json-file + options: + max-file: "5" + max-size: 10m container_name: appwrite-worker-notifications - image: appwrite-dev + restart: unless-stopped + image: ${_APP_IMAGE:-appwrite/appwrite}:${_APP_VERSION:-latest} networks: - appwrite - volumes: - - ./app:/usr/src/code/app - - ./src:/usr/src/code/src depends_on: - redis - - maildev - ${_APP_DB_HOST:-mariadb} environment: - _APP_ENV From 260e43edda111361f934eb3c4ed7a2e34b390866 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 29 Jun 2026 10:42:50 +0530 Subject: [PATCH 150/153] Fix CI compose service selection --- .github/workflows/ci.yml | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a4e8ab5bd..a9227f51b8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -307,8 +307,11 @@ jobs: - name: Load and Start Appwrite timeout-minutes: 5 run: | - docker compose pull --quiet --ignore-buildable - docker compose up -d --quiet-pull --wait + selected_db="${_APP_DB_HOST:-mongodb}" + services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" + + docker compose pull --quiet --ignore-buildable $selected_db $services + docker compose up -d --quiet-pull --wait $selected_db $services - name: Environment Variables run: docker compose exec -T appwrite vars @@ -354,8 +357,11 @@ jobs: - name: Load and Start Appwrite timeout-minutes: 5 run: | - docker compose pull --quiet --ignore-buildable - docker compose up -d --quiet-pull --wait + selected_db="${_APP_DB_HOST:-mongodb}" + services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" + + docker compose pull --quiet --ignore-buildable $selected_db $services + docker compose up -d --quiet-pull --wait $selected_db $services - name: Wait for Open Runtimes timeout-minutes: 3 @@ -444,8 +450,11 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - docker compose pull --quiet --ignore-buildable - docker compose up -d --quiet-pull --wait + selected_db="${_APP_DB_HOST:-mongodb}" + services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" + + docker compose pull --quiet --ignore-buildable $selected_db $services + docker compose up -d --quiet-pull --wait $selected_db $services - name: Wait for Open Runtimes timeout-minutes: 3 @@ -527,8 +536,11 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - docker compose pull --quiet --ignore-buildable - docker compose up -d --quiet-pull --wait + selected_db="${_APP_DB_HOST:-mongodb}" + services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" + + docker compose pull --quiet --ignore-buildable $selected_db $services + docker compose up -d --quiet-pull --wait $selected_db $services - name: Run tests timeout-minutes: 15 @@ -585,8 +597,11 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - docker compose pull --quiet --ignore-buildable - docker compose up -d --quiet-pull --wait + selected_db="${_APP_DB_HOST:-mongodb}" + services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" + + docker compose pull --quiet --ignore-buildable $selected_db $services + docker compose up -d --quiet-pull --wait $selected_db $services - name: Wait for Open Runtimes timeout-minutes: 3 From ff472ab3071fea34e389ff38d740cd0e301ff4ad Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 29 Jun 2026 10:53:19 +0530 Subject: [PATCH 151/153] Revert "Fix CI compose service selection" This reverts commit 260e43edda111361f934eb3c4ed7a2e34b390866. --- .github/workflows/ci.yml | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a9227f51b8..2a4e8ab5bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -307,11 +307,8 @@ jobs: - name: Load and Start Appwrite timeout-minutes: 5 run: | - selected_db="${_APP_DB_HOST:-mongodb}" - services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" - - docker compose pull --quiet --ignore-buildable $selected_db $services - docker compose up -d --quiet-pull --wait $selected_db $services + docker compose pull --quiet --ignore-buildable + docker compose up -d --quiet-pull --wait - name: Environment Variables run: docker compose exec -T appwrite vars @@ -357,11 +354,8 @@ jobs: - name: Load and Start Appwrite timeout-minutes: 5 run: | - selected_db="${_APP_DB_HOST:-mongodb}" - services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" - - docker compose pull --quiet --ignore-buildable $selected_db $services - docker compose up -d --quiet-pull --wait $selected_db $services + docker compose pull --quiet --ignore-buildable + docker compose up -d --quiet-pull --wait - name: Wait for Open Runtimes timeout-minutes: 3 @@ -450,11 +444,8 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - selected_db="${_APP_DB_HOST:-mongodb}" - services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" - - docker compose pull --quiet --ignore-buildable $selected_db $services - docker compose up -d --quiet-pull --wait $selected_db $services + docker compose pull --quiet --ignore-buildable + docker compose up -d --quiet-pull --wait - name: Wait for Open Runtimes timeout-minutes: 3 @@ -536,11 +527,8 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - selected_db="${_APP_DB_HOST:-mongodb}" - services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" - - docker compose pull --quiet --ignore-buildable $selected_db $services - docker compose up -d --quiet-pull --wait $selected_db $services + docker compose pull --quiet --ignore-buildable + docker compose up -d --quiet-pull --wait - name: Run tests timeout-minutes: 15 @@ -597,11 +585,8 @@ jobs: _APP_DATABASE_DOCUMENTSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'documentsdb_db_main' || '' }} _APP_DATABASE_VECTORSDB_SHARED_TABLES: ${{ matrix.mode != 'dedicated' && 'vectorsdb_db_main' || '' }} run: | - selected_db="${_APP_DB_HOST:-mongodb}" - services="$(docker compose config --services | grep -Ev '^(mariadb|mongodb|postgresql|appwrite-mongo-express|adminer|redis-insight|graphql-explorer)$' | tr '\n' ' ')" - - docker compose pull --quiet --ignore-buildable $selected_db $services - docker compose up -d --quiet-pull --wait $selected_db $services + docker compose pull --quiet --ignore-buildable + docker compose up -d --quiet-pull --wait - name: Wait for Open Runtimes timeout-minutes: 3 From 4fe26dc30fa9cb453ce5a64f535d8103fd8f8a89 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 29 Jun 2026 08:06:24 +0000 Subject: [PATCH 152/153] fix(audit): record organization key internal id as audit actor Organization API keys use the same key role as standard keys, so the synthetic user with no id was cloned as the audit actor, leaving an empty actor id and internal id on audit entries. Extend the audit actor enrichment to cover organization keys as well, using the already-loaded key document's id and sequence. --- app/controllers/shared/api.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d3b2af0925..5ef8eb5021 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -226,7 +226,7 @@ Http::init() default => ACTOR_TYPE_KEY_ORGANIZATION, }); - if ($apiKey->getType() === API_KEY_STANDARD) { + if ($apiKey->getType() === API_KEY_STANDARD || $apiKey->getType() === API_KEY_ORGANIZATION) { $userClone ->setAttribute('$id', $dbKey->getId()) ->setAttribute('$sequence', $dbKey->getSequence()); From 66060a5d9a514c3b9b493d47da6daab7d8332afb Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 30 Jun 2026 09:38:24 +0530 Subject: [PATCH 153/153] Revert "Merge pull request #12195 from appwrite/feat-notifications-worker" This reverts commit cb6570124b79088721fb59fff3abe5c11ca2230a, reversing changes made to a6f753e17bec5bf80b8473c3dcbf1b3f9b626bed. --- .env | 1 - .github/workflows/ci.yml | 1 - Dockerfile | 1 - app/config/collections/platform.php | 257 --- app/config/variables.php | 9 - app/controllers/api/messaging.php | 9 - app/init/constants.php | 10 - app/init/models.php | 3 - app/init/resources.php | 6 - app/init/worker/message.php | 13 - bin/worker-mails | 2 +- bin/worker-notifications | 3 - docker-compose.yml | 44 - .../health/get-queue-notifications.md | 1 - docs/references/notifications/get-logo.md | 1 - .../notifications/list-notifications.md | 1 - .../notifications/update-notification.md | 1 - phpunit.xml | 1 - src/Appwrite/Docker/Compose.php | 4 +- src/Appwrite/Docker/Compose/Service.php | 5 - src/Appwrite/Event/Event.php | 3 - src/Appwrite/Event/Message/Notification.php | 116 -- src/Appwrite/Event/Publisher/Notification.php | 27 - src/Appwrite/Migration/Version/V19.php | 1 - src/Appwrite/Migration/Version/V24.php | 53 - src/Appwrite/Platform/Appwrite.php | 2 - .../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 - .../Http/Notifications/Logos/Appwrite/Get.php | 209 --- .../Http/Notifications/Update.php | 87 -- .../Http/Notifications/XList.php | 118 -- .../Platform/Modules/Notifications/Module.php | 14 - .../Modules/Notifications/Services/Http.php | 21 - .../Project/Http/Project/Labels/Update.php | 1 - .../Project/Http/Project/MockPhone/Create.php | 1 - .../Project/Http/Project/MockPhone/Delete.php | 1 - .../Project/Http/Project/MockPhone/Update.php | 1 - .../Projects/Http/Projects/Team/Update.php | 1 - .../Modules/Projects/Http/Projects/Update.php | 1 - .../Projects/Http/Schedules/Create.php | 1 - .../Modules/Projects/Http/Schedules/Get.php | 7 +- .../Modules/Projects/Http/Schedules/XList.php | 19 +- src/Appwrite/Platform/Services/Workers.php | 2 - src/Appwrite/Platform/Workers/Deletes.php | 11 - .../Platform/Workers/Notifications.php | 661 -------- src/Appwrite/Platform/Workers/Webhooks.php | 156 +- src/Appwrite/SDK/ContentType.php | 1 - .../SDK/Specification/Format/OpenAPI3.php | 1 - .../Validator/Queries/Notifications.php | 25 - .../Utopia/Messaging/Adapter/Console.php | 147 -- .../Utopia/Messaging/Adapter/Webhook.php | 124 -- .../Utopia/Messaging/Messages/Console.php | 82 - .../Utopia/Messaging/Messages/Webhook.php | 80 - src/Appwrite/Utopia/Response.php | 4 - src/Appwrite/Utopia/Response/Model/Log.php | 2 +- .../Utopia/Response/Model/Notification.php | 126 -- .../Notifications/NotificationsBase.php | 632 -------- .../NotificationsCustomConsoleTest.php | 16 - .../NotificationsCustomServerTest.php | 16 - tests/resources/docker/docker-compose.yml | 24 - tests/unit/Docker/ComposeTest.php | 14 +- tests/unit/Event/Message/NotificationTest.php | 86 - tests/unit/Event/MockPublisher.php | 2 +- .../unit/Migration/MigrationVersionsTest.php | 106 -- .../Notifications/NotificationsTest.php | 130 -- .../Modules/Projects/SchedulesTest.php | 161 -- tests/unit/Platform/Workers/DeletesTest.php | 93 -- tests/unit/Platform/Workers/MailsTest.php | 98 -- .../Platform/Workers/NotificationsTest.php | 1383 ----------------- .../Platform/Workers/RegistrationTest.php | 30 - tests/unit/Platform/Workers/WebhooksTest.php | 283 ---- .../Messaging/Adapter/CapturingWebhook.php | 34 - .../Utopia/Messaging/Adapter/ConsoleTest.php | 282 ---- .../Utopia/Messaging/Adapter/WebhookTest.php | 180 --- 78 files changed, 53 insertions(+), 6067 deletions(-) delete mode 100755 bin/worker-notifications delete mode 100644 docs/references/health/get-queue-notifications.md delete mode 100644 docs/references/notifications/get-logo.md delete mode 100644 docs/references/notifications/list-notifications.md delete mode 100644 docs/references/notifications/update-notification.md delete mode 100644 src/Appwrite/Event/Message/Notification.php delete mode 100644 src/Appwrite/Event/Publisher/Notification.php delete mode 100644 src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php delete mode 100644 src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php delete mode 100644 src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php delete mode 100644 src/Appwrite/Platform/Modules/Notifications/Module.php delete mode 100644 src/Appwrite/Platform/Modules/Notifications/Services/Http.php delete mode 100644 src/Appwrite/Platform/Workers/Notifications.php delete mode 100644 src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php delete mode 100644 src/Appwrite/Utopia/Messaging/Adapter/Console.php delete mode 100644 src/Appwrite/Utopia/Messaging/Adapter/Webhook.php delete mode 100644 src/Appwrite/Utopia/Messaging/Messages/Console.php delete mode 100644 src/Appwrite/Utopia/Messaging/Messages/Webhook.php delete mode 100644 src/Appwrite/Utopia/Response/Model/Notification.php delete mode 100644 tests/e2e/Services/Notifications/NotificationsBase.php delete mode 100644 tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php delete mode 100644 tests/e2e/Services/Notifications/NotificationsCustomServerTest.php delete mode 100644 tests/unit/Event/Message/NotificationTest.php delete mode 100644 tests/unit/Platform/Modules/Notifications/NotificationsTest.php delete mode 100644 tests/unit/Platform/Modules/Projects/SchedulesTest.php delete mode 100644 tests/unit/Platform/Workers/DeletesTest.php delete mode 100644 tests/unit/Platform/Workers/MailsTest.php delete mode 100644 tests/unit/Platform/Workers/NotificationsTest.php delete mode 100644 tests/unit/Platform/Workers/RegistrationTest.php delete mode 100644 tests/unit/Platform/Workers/WebhooksTest.php delete mode 100644 tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php delete mode 100644 tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php delete mode 100644 tests/unit/Utopia/Messaging/Adapter/WebhookTest.php diff --git a/.env b/.env index edc7bb741a..dc2bc60763 100644 --- a/.env +++ b/.env @@ -2,7 +2,6 @@ _APP_ENV=development _APP_EDITION=self-hosted _APP_LOCALE=en _APP_OPENSSL_KEY_V1=your-secret-key -_APP_NOTIFICATIONS_TRACKING_SECRET=your-secret-key _APP_RULES_FORMAT=md5 _APP_SYSTEM_RESPONSE_FORMAT= _APP_TRUSTED_HEADERS=x-forwarded-for diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a4e8ab5bd..e3883e1838 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -213,7 +213,6 @@ jobs: { name: 'Webhooks', runner: runner4, functional: true, processes: null, timeout: 15 }, { name: 'VCS', runner: runner4, functional: true, processes: null, timeout: 15 }, { name: 'Messaging', runner: runner4, functional: true, processes: null, timeout: 15 }, - { name: 'Notifications', runner: runner4, functional: true, processes: null, timeout: 15 }, { name: 'Migrations', runner: runner4, functional: true, processes: 1, timeout: 15 }, { name: 'Project', runner: runner4, functional: true, processes: null, timeout: 15 }, { name: 'Presences', runner: runner4, functional: true, processes: null, timeout: 15 }, diff --git a/Dockerfile b/Dockerfile index bdda7298b2..7ffd23f683 100755 --- a/Dockerfile +++ b/Dockerfile @@ -93,7 +93,6 @@ 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/app/config/collections/platform.php b/app/config/collections/platform.php index a90170946b..f93d82456e 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -485,17 +485,6 @@ $platformCollections = [ 'array' => false, 'filters' => [], ], - [ - '$id' => ID::custom('projectInternalId'), - 'type' => Database::VAR_ID, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ '$id' => ID::custom('schedule'), 'type' => Database::VAR_STRING, @@ -556,13 +545,6 @@ $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, @@ -570,13 +552,6 @@ $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, @@ -1068,238 +1043,6 @@ $platformCollections = [ ], ], - 'notifications' => [ - '$collection' => ID::custom(Database::METADATA), - '$id' => ID::custom('notifications'), - 'name' => 'Notifications', - '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('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, - 'format' => '', - 'size' => 100, - '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('resourceType'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 64, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('resourceId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('projectId'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => Database::LENGTH_KEY, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('projectInternalId'), - 'type' => Database::VAR_ID, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => true, - 'default' => null, - '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, - 'format' => '', - 'size' => 256, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('body'), - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 65535, - 'signed' => true, - 'required' => true, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => ID::custom('read'), - 'type' => Database::VAR_BOOLEAN, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => false, - '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' => [ - [ - '$id' => ID::custom('_key_messageId'), - 'type' => Database::INDEX_KEY, - 'attributes' => ['messageId'], - 'lengths' => [Database::LENGTH_KEY], - 'orders' => [Database::ORDER_ASC], - ], - [ - '$id' => ID::custom('_key_recipient'), - 'type' => Database::INDEX_UNIQUE, - 'attributes' => ['messageId', 'channel', 'recipientHash'], - 'lengths' => [Database::LENGTH_KEY, 64, 64], - 'orders' => [Database::ORDER_ASC, Database::ORDER_ASC, 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, - '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' => ['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], - ], - ], - ], - 'certificates' => [ '$collection' => ID::custom(Database::METADATA), '$id' => ID::custom('certificates'), diff --git a/app/config/variables.php b/app/config/variables.php index 8b4fa74bf9..b77d841aaf 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -79,15 +79,6 @@ 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/controllers/api/messaging.php b/app/controllers/api/messaging.php index 61a35761ca..13119ac2c6 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -3501,7 +3501,6 @@ Http::post('/v1/messaging/messages/email') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -3648,7 +3647,6 @@ Http::post('/v1/messaging/messages/sms') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -3871,7 +3869,6 @@ Http::post('/v1/messaging/messages/push') 'resourceInternalId' => $message->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), 'schedule' => $scheduledAt, 'active' => true, ])); @@ -4266,7 +4263,6 @@ 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, ])); @@ -4283,7 +4279,6 @@ Http::patch('/v1/messaging/messages/email/:messageId') } $schedule - ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); @@ -4492,7 +4487,6 @@ 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, ])); @@ -4509,7 +4503,6 @@ Http::patch('/v1/messaging/messages/sms/:messageId') } $schedule - ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); @@ -4671,7 +4664,6 @@ 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, ])); @@ -4688,7 +4680,6 @@ Http::patch('/v1/messaging/messages/push/:messageId') } $schedule - ->setAttribute('projectInternalId', $project->getSequence()) ->setAttribute('resourceUpdatedAt', DateTime::now()) ->setAttribute('active', $scheduledStatus); diff --git a/app/init/constants.php b/app/init/constants.php index e905ed56b4..9efb6e3222 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -168,7 +168,6 @@ const SESSION_PROVIDER_SERVER = 'server'; const ACTOR_TYPE_USER = 'user'; const ACTOR_TYPE_ADMIN = 'admin'; const ACTOR_TYPE_GUEST = 'guest'; -const ACTOR_TYPE_HIDDEN = 'hidden'; const ACTOR_TYPE_KEY_PROJECT = 'keyProject'; const ACTOR_TYPE_KEY_ACCOUNT = 'keyAccount'; const ACTOR_TYPE_KEY_ORGANIZATION = 'keyOrganization'; @@ -279,13 +278,6 @@ 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 types -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 NOTIFICATION_TRACKING_JWT_TTL = 60 * 60 * 24 * 7; const MAIL_TEMPLATE_CERTIFICATE_FAILED = 'certificate-failed'; const MAIL_TEMPLATE_DATA_EXPORT = 'data-export'; const MAIL_TEMPLATE_INVITATION = 'invitation'; @@ -471,8 +463,6 @@ 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/app/init/models.php b/app/init/models.php index cdb2a9b9b6..efd2c13215 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -115,7 +115,6 @@ 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; @@ -251,7 +250,6 @@ 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('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)); @@ -383,7 +381,6 @@ Response::setModel(new Account()); Response::setModel(new Preferences()); Response::setModel(new Session()); Response::setModel(new Identity()); -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 51d0b25f8a..34f66d3de7 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -12,7 +12,6 @@ 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; @@ -151,11 +150,6 @@ $container->set('publisherForMessaging', fn (Publisher $publisher) => new Messag 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']); - $container->set('databaseFactory', fn (Group $pools, Cache $cache, Authorization $authorization) => new DatabaseFactory( $pools, $cache, diff --git a/app/init/worker/message.php b/app/init/worker/message.php index a5838e9257..7cbfe19b0f 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -2,8 +2,6 @@ use Appwrite\Database\Factory as DatabaseFactory; use Appwrite\Event\Event; -use Appwrite\Event\Publisher\Func as FunctionPublisher; -use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Realtime; use Appwrite\Event\Webhook; use Appwrite\Usage\Context; @@ -19,7 +17,6 @@ use Utopia\DI\Container; use Utopia\Logger\Log; use Utopia\Pools\Group; use Utopia\Queue\Publisher; -use Utopia\Queue\Queue; use Utopia\Registry\Registry; use Utopia\Span\Span; use Utopia\Storage\Device\Telemetry as TelemetryDevice; @@ -148,16 +145,6 @@ return function (Container $container): void { return new Webhook($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, - new Queue(System::getEnv('_APP_FUNCTIONS_QUEUE_NAME', Event::FUNCTIONS_QUEUE_NAME), 'utopia-queue', Event::FUNCTIONS_QUEUE_TTL) - ), ['publisher']); - $container->set('queueForRealtime', function () { return new Realtime(); }, []); diff --git a/bin/worker-mails b/bin/worker-mails index a28b1ddc67..3b1415f45f 100644 --- a/bin/worker-mails +++ b/bin/worker-mails @@ -1,3 +1,3 @@ #!/bin/sh -exec php /usr/src/code/app/worker.php mails "$@" +exec php /usr/src/code/app/worker.php mails "$@" \ No newline at end of file diff --git a/bin/worker-notifications b/bin/worker-notifications deleted file mode 100755 index 1b7bf28745..0000000000 --- a/bin/worker-notifications +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -exec php /usr/src/code/app/worker.php notifications "$@" diff --git a/docker-compose.yml b/docker-compose.yml index 9a43dc1dad..7ad12e7ddf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -99,7 +99,6 @@ 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 @@ -748,7 +747,6 @@ 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 @@ -771,48 +769,6 @@ services: - _APP_OPTIONS_FORCE_HTTPS - _APP_DATABASE_SHARED_TABLES - appwrite-worker-notifications: - entrypoint: worker-notifications - logging: - driver: json-file - options: - max-file: "5" - max-size: 10m - container_name: appwrite-worker-notifications - restart: unless-stopped - image: ${_APP_IMAGE:-appwrite/appwrite}:${_APP_VERSION:-latest} - networks: - - appwrite - depends_on: - - redis - - ${_APP_DB_HOST:-mariadb} - environment: - - _APP_ENV - - _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 - - _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 logging: diff --git a/docs/references/health/get-queue-notifications.md b/docs/references/health/get-queue-notifications.md deleted file mode 100644 index 529d1056e9..0000000000 --- a/docs/references/health/get-queue-notifications.md +++ /dev/null @@ -1 +0,0 @@ -Get the number of jobs in the notifications queue. diff --git a/docs/references/notifications/get-logo.md b/docs/references/notifications/get-logo.md deleted file mode 100644 index eadc755c69..0000000000 --- a/docs/references/notifications/get-logo.md +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 3d9f93789c..0000000000 --- a/docs/references/notifications/list-notifications.md +++ /dev/null @@ -1 +0,0 @@ -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 deleted file mode 100644 index 220c79b70e..0000000000 --- a/docs/references/notifications/update-notification.md +++ /dev/null @@ -1 +0,0 @@ -Update a notification by its unique ID. Use the `read` parameter to mark the notification as read or unread. diff --git a/phpunit.xml b/phpunit.xml index 56953fdeaf..32e865fe35 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -37,7 +37,6 @@ ./tests/e2e/Services/ProjectWebhooks ./tests/e2e/Services/Messaging ./tests/e2e/Services/Migrations - ./tests/e2e/Services/Notifications ./tests/e2e/Services/Project ./tests/e2e/Services/Advisor ./tests/e2e/Services/Functions/FunctionsBase.php diff --git a/src/Appwrite/Docker/Compose.php b/src/Appwrite/Docker/Compose.php index f879ad7aec..9ea6420d2d 100644 --- a/src/Appwrite/Docker/Compose.php +++ b/src/Appwrite/Docker/Compose.php @@ -4,7 +4,6 @@ namespace Appwrite\Docker; use Appwrite\Docker\Compose\Service; use Exception; -use Spyc; class Compose { @@ -15,8 +14,7 @@ class Compose public function __construct(string $data) { - $compose = \function_exists('yaml_parse') ? \yaml_parse($data) : Spyc::YAMLLoadString($data); - $this->compose = \is_array($compose) ? $compose : []; + $this->compose = yaml_parse($data); $this->compose['services'] = (isset($this->compose['services']) && is_array($this->compose['services'])) ? $this->compose['services'] : []; diff --git a/src/Appwrite/Docker/Compose/Service.php b/src/Appwrite/Docker/Compose/Service.php index 0ea84c12f6..e7993d6927 100644 --- a/src/Appwrite/Docker/Compose/Service.php +++ b/src/Appwrite/Docker/Compose/Service.php @@ -54,11 +54,6 @@ 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/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 54841343e7..357442a07c 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -21,9 +21,6 @@ class Event 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'; public const FUNCTIONS_QUEUE_TTL = 60 * 60 * 24 * 7; // 7 days diff --git a/src/Appwrite/Event/Message/Notification.php b/src/Appwrite/Event/Message/Notification.php deleted file mode 100644 index 644d29d300..0000000000 --- a/src/Appwrite/Event/Message/Notification.php +++ /dev/null @@ -1,116 +0,0 @@ - $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/Publisher/Notification.php b/src/Appwrite/Event/Publisher/Notification.php deleted file mode 100644 index 2fe0353276..0000000000 --- a/src/Appwrite/Event/Publisher/Notification.php +++ /dev/null @@ -1,27 +0,0 @@ -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/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index 6f939b4ce4..2526d03009 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -736,7 +736,6 @@ 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 ea1cd2088e..82889f8d79 100644 --- a/src/Appwrite/Migration/Version/V24.php +++ b/src/Appwrite/Migration/Version/V24.php @@ -90,12 +90,6 @@ class V24 extends Migration }; $collections = $this->collections[$collectionType]; - $notificationsCollectionExists = false; - - if ($collectionType === 'console') { - $notificationsCollectionExists = !$this->dbForProject->getCollection('notifications')->isEmpty(); - $this->createCollection('notifications'); - } foreach ($collections as $collection) { $id = $collection['$id']; @@ -159,42 +153,6 @@ 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 'notifications': - if ($collectionType === 'console' && $notificationsCollectionExists) { - 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 { @@ -495,17 +453,6 @@ 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/Appwrite.php b/src/Appwrite/Platform/Appwrite.php index f402050391..4bf3ed4768 100644 --- a/src/Appwrite/Platform/Appwrite.php +++ b/src/Appwrite/Platform/Appwrite.php @@ -11,7 +11,6 @@ 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\Organization; use Appwrite\Platform\Modules\Presences; use Appwrite\Platform\Modules\Project; @@ -37,7 +36,6 @@ class Appwrite extends Platform $this->addModule(new Presences\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/Functions/Http/Executions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php index 303bf1484b..639b9ae2aa 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php @@ -325,7 +325,6 @@ 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 e674b935c9..5222cba0b0 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -276,7 +276,6 @@ 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 5a6f3f8045..f45a762708 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -325,7 +325,6 @@ class Update extends Base 'resourceInternalId' => $function->getSequence(), 'resourceUpdatedAt' => DateTime::now(), 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), 'schedule' => $function->getAttribute('schedule'), 'active' => false, ])) @@ -338,7 +337,6 @@ 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 2746886bee..f0dff0a811 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 @@ -12,7 +12,6 @@ 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; @@ -73,7 +72,6 @@ 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', enum: new Enum(name: 'HealthQueueName')) ->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') @@ -90,7 +88,6 @@ class Get extends Base ->inject('publisherForMessaging') ->inject('publisherForMigrations') ->inject('publisherForScreenshots') - ->inject('publisherForNotifications') ->callback($this->action(...)); } @@ -111,7 +108,6 @@ class Get extends Base MessagingPublisher $publisherForMessaging, MigrationPublisher $publisherForMigrations, Screenshot $publisherForScreenshots, - NotificationPublisher $publisherForNotifications, ): void { $threshold = (int) $threshold; @@ -129,7 +125,6 @@ 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) => $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 deleted file mode 100644 index 50337cf9d0..0000000000 --- a/src/Appwrite/Platform/Modules/Health/Http/Health/Queue/Notifications/Get.php +++ /dev/null @@ -1,60 +0,0 @@ -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('publisherForNotifications') - ->inject('response') - ->callback($this->action(...)); - } - - public function action(int|string $threshold, NotificationPublisher $publisherForNotifications, Response $response): void - { - $threshold = (int) $threshold; - - $size = $publisherForNotifications->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 a6acbcb4d4..54c6f9ad6d 100644 --- a/src/Appwrite/Platform/Modules/Health/Services/Http.php +++ b/src/Appwrite/Platform/Modules/Health/Services/Http.php @@ -19,7 +19,6 @@ 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; @@ -57,7 +56,6 @@ 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/Notifications/Http/Notifications/Logos/Appwrite/Get.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php deleted file mode 100644 index 7bb99c7ceb..0000000000 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Logos/Appwrite/Get.php +++ /dev/null @@ -1,209 +0,0 @@ -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_NOTIFICATIONS_TRACKING_SECRET'); - - if (!empty($secret) && $jwt !== '') { - try { - $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['notification'], - $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{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'] !== 'notification_track' - ) { - return null; - } - - $notifications = $dbForPlatform->find('notifications', [ - Query::equal('messageId', [(string) $decoded['messageId']]), - Query::equal('channel', [(string) $decoded['channel']]), - Query::equal('recipientHash', [(string) $decoded['recipientHash']]), - Query::limit(1), - ]); - - if (empty($notifications)) { - return null; - } - - $notification = $notifications[0]; - if ( - $notification->getAttribute('projectId') !== (string) $decoded['projectId'] - || (isset($decoded['projectInternalId']) && $notification->getAttribute('projectInternalId') !== (string) $decoded['projectInternalId']) - ) { - return null; - } - - $seenAt = DateTime::now(); - $updates = [ - 'read' => true, - 'lastSeen' => $seenAt, - ]; - - if (empty($notification->getAttribute('firstSeen'))) { - $updates['firstSeen'] = $seenAt; - } - - $updated = $dbForPlatform->updateDocument('notifications', $notification->getId(), new Document($updates)); - $project = $dbForPlatform->getDocument('projects', $notification->getAttribute('projectId')); - - if ($project->isEmpty()) { - $project = new Document([ - '$id' => 'console', - '$sequence' => 'console', - ]); - } - - return [ - 'notification' => $updated, - 'project' => $project, - 'seenAt' => $seenAt, - ]; - } - - private function logView(Document $notification, Document $project, string $seenAt, Request $request, AuditPublisher $publisherForAudits): void - { - $publisherForAudits->enqueue(new AuditMessage( - event: 'notification.view', - payload: [ - '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' => $notification->getAttribute('resourceId', ''), - '$sequence' => $notification->getAttribute('resourceInternalId', ''), - 'name' => '', - 'email' => '', - 'type' => ACTOR_TYPE_HIDDEN, - ]), - resource: 'notification/' . $notification->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/Http/Notifications/Update.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php deleted file mode 100644 index 9ca7b6b683..0000000000 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/Update.php +++ /dev/null @@ -1,87 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_PATCH) - ->setHttpPath('/v1/notifications/:notificationId') - ->desc('Update notification') - ->groups(['api', 'notifications']) - ->label('scope', 'account') - ->label('sdk', new Method( - 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_NOTIFICATION, - ) - ] - )) - ->param('notificationId', '', new UID(), 'Notification ID.') - ->param('read', null, new Boolean(), 'Notification read status.') - ->inject('response') - ->inject('dbForPlatform') - ->inject('authorization') - ->inject('project') - ->inject('user') - ->callback($this->action(...)); - } - - public function action( - string $notificationId, - bool $read, - Response $response, - Database $dbForPlatform, - Authorization $authorization, - Document $project, - Document $user, - ): void { - if ($project->getId() !== 'console') { - throw new Exception(Exception::USER_UNAUTHORIZED); - } - - $notification = $dbForPlatform->getDocument('notifications', $notificationId); - - if ($notification->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('notifications', $notificationId, new Document([ - 'read' => $read, - ])); - - $response->dynamic($updated, Response::MODEL_NOTIFICATION); - } -} diff --git a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php b/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php deleted file mode 100644 index 558190d99a..0000000000 --- a/src/Appwrite/Platform/Modules/Notifications/Http/Notifications/XList.php +++ /dev/null @@ -1,118 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/notifications') - ->desc('List notifications') - ->groups(['api', 'notifications']) - ->label('scope', 'account') - ->label('sdk', new Method( - 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_NOTIFICATION_LIST, - ) - ] - )) - ->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') - ->inject('user') - ->callback($this->action(...)); - } - - /** - * @param array $queries - */ - public function 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) { - throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage()); - } - - $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()); - } - - $notificationId = $cursor->getValue(); - $cursorDocument = $dbForPlatform->getDocument('notifications', $notificationId); - - if ($cursorDocument->isEmpty()) { - throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Notification '{$notificationId}' for the 'cursor' value not found."); - } - - $cursor->setValue($cursorDocument); - } - - $queries[] = Query::equal('channel', [NOTIFICATION_TYPE_CONSOLE]); - $queries[] = Query::equal('resourceType', [RESOURCE_TYPE_USERS]); - $queries[] = Query::equal('resourceId', [$user->getId()]); - - $filterQueries = Query::groupByType($queries)['filters']; - - try { - $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."); - } - - $response->dynamic(new Document([ - 'notifications' => $results, - 'total' => $total, - ]), Response::MODEL_NOTIFICATION_LIST); - } -} diff --git a/src/Appwrite/Platform/Modules/Notifications/Module.php b/src/Appwrite/Platform/Modules/Notifications/Module.php deleted file mode 100644 index f4e1c574cc..0000000000 --- a/src/Appwrite/Platform/Modules/Notifications/Module.php +++ /dev/null @@ -1,14 +0,0 @@ -addService('http', new Http()); - } -} diff --git a/src/Appwrite/Platform/Modules/Notifications/Services/Http.php b/src/Appwrite/Platform/Modules/Notifications/Services/Http.php deleted file mode 100644 index 91700ca125..0000000000 --- a/src/Appwrite/Platform/Modules/Notifications/Services/Http.php +++ /dev/null @@ -1,21 +0,0 @@ -type = Service::TYPE_HTTP; - - $this - ->addAction(ListNotifications::getName(), new ListNotifications()) - ->addAction(UpdateNotification::getName(), new UpdateNotification()) - ->addAction(GetAppwriteLogo::getName(), new GetAppwriteLogo()); - } -} 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 76c47a32f8..8a3506eb13 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php @@ -71,7 +71,6 @@ 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 759dd931c0..f4002c60ef 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -101,7 +101,6 @@ 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 f65a955b3d..0fb23e1764 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -95,7 +95,6 @@ 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 f2b618c39e..48b90a1b97 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -97,7 +97,6 @@ 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/Projects/Http/Projects/Team/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php index d2f7d5a29d..ab92d1a15f 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php @@ -78,7 +78,6 @@ 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 c633a7adbf..f6df843d07 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -72,7 +72,6 @@ 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); } diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php index 0d433ab5d9..7cf13c5ca2 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Create.php @@ -132,7 +132,6 @@ 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 05c4a925db..d14d4c361d 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/Get.php @@ -67,12 +67,7 @@ class Get extends Action throw new Exception(Exception::SCHEDULE_NOT_FOUND); } - $projectInternalId = $schedule->getAttribute('projectInternalId'); - - if ( - $schedule->getAttribute('projectId') !== $project->getId() - || ($projectInternalId !== null && $projectInternalId !== '' && $projectInternalId !== $project->getSequence()) - ) { + if ($schedule->getAttribute('projectId') !== $project->getId()) { 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 7f82162a2c..66879f798f 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Schedules/XList.php @@ -77,10 +77,6 @@ class XList extends Action } $queries[] = Query::equal('projectId', [$project->getId()]); - $queries[] = Query::or([ - Query::equal('projectInternalId', [$project->getSequence()]), - Query::isNull('projectInternalId'), - ]); $cursor = Query::getCursorQueries($queries, false); $cursor = \reset($cursor); @@ -94,11 +90,7 @@ class XList extends Action $scheduleId = $cursor->getValue(); $cursorDocument = $dbForPlatform->getDocument('schedules', $scheduleId); - if ( - $cursorDocument->isEmpty() - || $cursorDocument->getAttribute('projectId') !== $project->getId() - || !$this->matchesProjectInternalId($cursorDocument, $project) - ) { + if ($cursorDocument->isEmpty()) { throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Schedule '{$scheduleId}' for the 'cursor' value not found."); } @@ -119,13 +111,4 @@ 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/src/Appwrite/Platform/Services/Workers.php b/src/Appwrite/Platform/Services/Workers.php index 5af3d078de..fa77725f7a 100644 --- a/src/Appwrite/Platform/Services/Workers.php +++ b/src/Appwrite/Platform/Services/Workers.php @@ -10,7 +10,6 @@ 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; @@ -29,7 +28,6 @@ class Workers extends Service ->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/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 403e695603..1ac808c682 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -788,17 +788,6 @@ class Deletes extends Action Console::error('Failed to delete schedules: ' . $th->getMessage()); } - // Delete Notifications - try { - $this->deleteByGroup('notifications', [ - Query::equal('projectId', [$projectId]), - Query::equal('projectInternalId', [$projectInternalId]), - Query::orderAsc() - ], $dbForPlatform); - } catch (Throwable $th) { - Console::error('Failed to delete notifications: ' . $th->getMessage()); - } - // Delete Advisor insights try { $this->deleteByGroup('insights', [ diff --git a/src/Appwrite/Platform/Workers/Notifications.php b/src/Appwrite/Platform/Workers/Notifications.php deleted file mode 100644 index 04f705e2d1..0000000000 --- a/src/Appwrite/Platform/Workers/Notifications.php +++ /dev/null @@ -1,661 +0,0 @@ - - */ - 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('dbForPlatform') - ->inject('log') - ->callback($this->action(...)); - } - - 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); - } - $payload = $message->getPayload(); - - if (empty($payload)) { - throw new Exception('Missing payload'); - } - - $deduplicationKey = $payload['deduplicationKey'] ?? ''; - $messageId = $deduplicationKey !== '' ? \md5($deduplicationKey) : ''; - - $recipients = $this->resolveRecipients($payload); - if (empty($recipients)) { - throw new Exception('No recipients in payload'); - } - - $failure = null; - foreach ($recipients as $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); - continue; - } - - 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, $project); - } - } catch (Throwable $error) { - $log->addTag('channel', $channel); - $log->addTag('error', $error->getMessage()); - $failure ??= $error; - } - } - - if ($failure !== null) { - throw $failure; - } - } - - /** - * @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_TYPE_EMAIL]]; - } - - /** - * @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, Document $project): array - { - $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['resourceId'] === '' - ) { - $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; - } - - private function alreadyDelivered(Database $dbForPlatform, string $alertId): bool - { - return !$dbForPlatform->getDocument('notifications', $alertId)->isEmpty(); - } - - /** - * 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, resourceType: string, resourceId: string, resourceInternalId: string, parentResourceType: string, parentResourceId: string, parentResourceInternalId: string} $recipient - */ - protected function dispatch( - array $recipient, - string $messageId, - array $payload, - Document $project, - Registry $register, - Database $dbForPlatform, - Log $log, - ): ?string { - $channel = $recipient['channel']; - - return match ($channel) { - NOTIFICATION_TYPE_EMAIL => $this->dispatchEmail($recipient, $messageId, $payload, $project, $register, $dbForPlatform, $log), - 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), - }; - } - - /** - * @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, - string $messageId, - array $payload, - Document $project, - Registry $register, - Database $dbForPlatform, - Log $log, - ): ?string { - $address = $recipient['address']; - $smtp = $this->resolveSmtpConfig($project, $payload); - - if (empty($smtp) && empty(System::getEnv('_APP_SMTP_HOST'))) { - $log->addTag('email_skipped', 'no_smtp'); - 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'; - $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 . '://' . $consoleHostname; - $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()); - - $recipientHash = self::buildRecipientHash($recipient); - $trackingSecret = System::getEnv('_APP_NOTIFICATIONS_TRACKING_SECRET'); - if ($messageId !== '' && !empty($trackingSecret)) { - $body = $this->injectTrackingLogo($body, $messageId, $recipient['channel'], $recipientHash, $project, $trackingSecret); - } - - /** @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' => $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) { - throw new Exception('Error sending notification: ' . $error->getMessage(), $type === 'smtp' ? 401 : 500); - } - - if ($messageId !== '') { - return $this->persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); - } - - return null; - } - - /** - * @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 - { - $this->validateConsoleRecipient($recipient); - - $params = $payload['templateParams'] ?? ($payload['variables'] ?? []); - $title = self::renderText($payload['subject'] ?? '', $params); - $body = self::renderText($payload['preview'] ?? '', $params); - if ($body === '') { - $body = self::renderText($payload['body'] ?? '', $params); - } - - $alertId = $messageId !== '' ? self::buildAlertId($messageId, $recipient) : null; - $recipientHash = $messageId !== '' ? self::buildRecipientHash($recipient) : null; - - $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; - } - - $consoleMessage = new ConsoleMessage( - recipients: [$consoleRecipient], - title: $title, - body: $body, - type: $payload['type'] ?? 'info', - messageId: $messageId !== '' ? $messageId : null, - projectId: $project->getId(), - projectInternalId: $project->getSequence(), - ); - - $adapter = new ConsoleAdapter($dbForPlatform); - $result = $adapter->send($consoleMessage); - - if (($result['deliveredTo'] ?? 0) === 0) { - $error = $result['results'][0]['error'] ?? 'unknown error'; - throw new Exception('Console alert delivery failed: ' . $error); - } - - return $alertId; - } - - /** - * @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 - { - $address = $recipient['address']; - $signatureKey = $recipient['signatureKey'] ?? null; - - $body = [ - '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'] ?? [], - ]; - - if ($signatureKey === null || $signatureKey === '') { - $log->addTag('webhook_signed', 'false'); - } - - $message = new WebhookMessage( - urls: [$address], - payload: $body, - signingSecret: $signatureKey, - ); - - $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); - } - - // Caller persists the alert AFTER successful dispatch. - return null; - } - - /** - * 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. - * - * @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, $project); - - $channel = $recipient['channel']; - - $alertId = self::buildAlertId($messageId, $recipient); - $recipientHash = self::buildRecipientHash($recipient); - - $permissions = $this->buildAlertPermissions($recipient['resourceType'], $recipient['resourceId'], $project->getId()); - 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, - '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, - ]); - - try { - $dbForPlatform->createDocument('notifications', $document); - return $alertId; - } catch (DuplicateException) { - $existing = $dbForPlatform->getDocument('notifications', $alertId); - return $existing->isEmpty() ? $alertId : $existing->getId(); - } - } - - /** - * Build the deterministic alertId composed of the messageId plus a - * recipient hash. - * - * @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 - { - return \substr($messageId, 0, 19) . '_' . self::buildRecipientHash($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']; - - 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 $resourceType, string $resourceId, string $projectId): array - { - $permissions = []; - if ($resourceType === RESOURCE_TYPE_USERS) { - $permissions[] = Permission::read(Role::user($resourceId)); - $permissions[] = Permission::update(Role::user($resourceId)); - $permissions[] = Permission::delete(Role::user($resourceId)); - } - 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($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 $payload - * @return 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 []; - } - - 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'] ?? '', - ]; - } - - private function validateConsoleRecipient(array $recipient): void - { - $validator = new UID(); - - 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 ' . $key); - } - } - } - - 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 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 notification recipient, which the - * `/v1/notifications/logos/appwrite` endpoint verifies before marking - * the notification as read and recording view timestamps. - */ - private function injectTrackingLogo(string $body, string $messageId, string $channel, string $recipientHash, Document $project, string $trackingSecret): string - { - $jwt = (new JWT($trackingSecret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0)) - ->encode([ - 'messageId' => $messageId, - 'channel' => $channel, - 'recipientHash' => $recipientHash, - 'projectId' => $project->getId(), - 'projectInternalId' => $project->getSequence(), - 'purpose' => 'notification_track', - ]); - - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; - $hostname = System::getEnv('_APP_DOMAIN', 'localhost'); - - $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', $logo . '$0', $body, 1) ?? ($body . $logo); - } - - return $body . $logo; - } -} diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 5466ade630..593fd39767 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -2,9 +2,9 @@ namespace Appwrite\Platform\Workers; -use Appwrite\Event\Message\Notification as NotificationMessage; +use Appwrite\Event\Message\Mail as MailMessage; use Appwrite\Event\Message\Usage as UsageMessage; -use Appwrite\Event\Publisher\Notification as NotificationPublisher; +use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Network\Validator\PublicHostname; use Appwrite\Template\Template; @@ -37,7 +37,7 @@ class Webhooks extends Action ->inject('message') ->inject('project') ->inject('dbForPlatform') - ->inject('publisherForNotifications') + ->inject('publisherForMails') ->inject('publisherForUsage') ->inject('log') ->inject('plan') @@ -48,14 +48,14 @@ class Webhooks extends Action * @param Message $message * @param Document $project * @param Database $dbForPlatform - * @param NotificationPublisher $publisherForNotifications + * @param MailPublisher $publisherForMails * @param UsagePublisher $publisherForUsage * @param Log $log * @param array $plan * @return void * @throws Exception */ - public function action(Message $message, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, UsagePublisher $publisherForUsage, Log $log, array $plan): void + public function action(Message $message, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, UsagePublisher $publisherForUsage, Log $log, array $plan): void { $payload = $message->getPayload(); @@ -72,7 +72,7 @@ class Webhooks extends Action $errors = []; foreach ($project->getAttribute('webhooks', []) as $webhook) { if (array_intersect($webhook->getAttribute('events', []), $events)) { - $error = $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $publisherForNotifications, $publisherForUsage, $plan); + $error = $this->execute($events, $webhookPayload, $webhook, $user, $project, $dbForPlatform, $publisherForMails, $publisherForUsage, $plan); if ($error !== null) { $errors[] = $error; } @@ -91,12 +91,11 @@ class Webhooks extends Action * @param Document $user * @param Document $project * @param Database $dbForPlatform - * @param NotificationPublisher $publisherForNotifications - * @param UsagePublisher $publisherForUsage + * @param MailPublisher $publisherForMails * @param array $plan * @return string|null The error log if the delivery failed, otherwise null */ - private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, UsagePublisher $publisherForUsage, array $plan): ?string + private function execute(array $events, string $payload, Document $webhook, Document $user, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, UsagePublisher $publisherForUsage, array $plan): ?string { if ($webhook->getAttribute('enabled') !== true) { return null; @@ -185,7 +184,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, $publisherForNotifications, $plan); + $this->sendEmailAlert($attempts, $statusCode, $webhook, $project, $dbForPlatform, $publisherForMails, $plan); } $dbForPlatform->updateDocument('webhooks', $webhook->getId(), new Document($updatePayload)); @@ -219,136 +218,65 @@ class Webhooks extends Action * @param Document $webhook * @param Document $project * @param Database $dbForPlatform - * @param NotificationPublisher $publisherForNotifications + * @param MailPublisher $publisherForMails * @param array $plan * @return void */ - public function sendAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, NotificationPublisher $publisherForNotifications, array $plan): void + public function sendEmailAlert(int $attempts, mixed $statusCode, Document $webhook, Document $project, Database $dbForPlatform, MailPublisher $publisherForMails, array $plan): void { $memberships = $dbForPlatform->find('memberships', [ Query::equal('teamInternalId', [$project->getAttribute('teamInternalId')]), Query::limit(APP_LIMIT_SUBQUERY) ]); - $ownerMemberships = \array_filter( - $memberships, - fn (Document $membership) => self::hasOwnerRole($membership) - ); - - if (empty($ownerMemberships)) { - return; - } - - $userIds = \array_values(\array_unique(\array_filter(\array_map( - fn (Document $membership) => $membership->getAttribute('userId'), - $ownerMemberships - )))); - - if (empty($userIds)) { - return; - } + $userIds = array_column(\array_map(fn ($membership) => $membership->getArrayCopy(), $memberships), 'userId'); $users = $dbForPlatform->find('users', [ Query::equal('$id', $userIds), - Query::limit(APP_LIMIT_SUBQUERY), ]); - if (empty($users)) { - return; - } - $projectId = $project->getId(); - $projectInternalId = $project->getSequence(); $region = $project->getAttribute('region', 'default'); $webhookId = $webhook->getId(); - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled') === 'disabled' ? 'http' : 'https'; - $consoleHostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', 'localhost')); + $template = Template::fromFile(__DIR__ . '/../../../../app/config/locale/templates/email-webhook-failed.tpl'); + $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('{{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); + + // TODO: Use setbodyTemplate once #7307 is merged $subject = 'Webhook deliveries have been paused'; - $preview = 'Webhook "' . $webhook->getAttribute('name') . '" has been paused after ' . $attempts . ' failed delivery attempts.'; + $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")); foreach ($users as $user) { - $email = $user->getAttribute('email'); - $userId = $user->getId(); - - $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)) { - $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, - ]; - } - - $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( + $publisherForMails->enqueue(new MailMessage( project: $project, - recipients: $recipients, - deduplicationKey: 'webhook:' . $webhook->getId() . ':paused:' . $webhook->getUpdatedAt(), + recipient: $user->getAttribute('email'), + name: $user->getAttribute('name', ''), subject: $subject, - bodyTemplate: __DIR__ . '/../../../../app/config/locale/templates/email-base-styled.tpl', - body: $template->render(), + template: MAIL_TEMPLATE_WEBHOOK_FAILED, + body: $body->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, - ], + variables: ['user' => $user->getAttribute('name', '')], )); } } - - 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)) { - continue; - } - - if (\strtolower($role) === 'owner') { - return true; - } - } - - return false; - } } diff --git a/src/Appwrite/SDK/ContentType.php b/src/Appwrite/SDK/ContentType.php index 7a1671df7a..174c889815 100644 --- a/src/Appwrite/SDK/ContentType.php +++ b/src/Appwrite/SDK/ContentType.php @@ -8,7 +8,6 @@ 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 a98cd6f83f..38f81bca69 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -318,7 +318,6 @@ 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/Utopia/Database/Validator/Queries/Notifications.php b/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php deleted file mode 100644 index a85609aaa7..0000000000 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Notifications.php +++ /dev/null @@ -1,25 +0,0 @@ -process($message); - } - - protected function process(ConsoleMessage $message): array - { - $response = new Response($this->getType()); - $delivered = 0; - - foreach ($message->getRecipients() as $recipient) { - $resourceType = $recipient['resourceType']; - $resourceId = $recipient['resourceId']; - $key = $resourceType . ':' . $resourceId; - - $messageId = $message->getMessageId(); - $recipientKey = self::buildRecipientKey($recipient); - $recipientHash = $recipient['recipientHash'] ?? \substr(\md5($recipientKey), 0, 16); - $documentId = $messageId !== null - ? ($recipient['alertId'] ?? \substr($messageId, 0, 19) . '_' . $recipientHash) - : ID::unique(); - - try { - $document = new Document([ - '$id' => $documentId, - '$permissions' => $this->buildPermissions($resourceType, $resourceId, $message->getProjectId() ?? ''), - 'messageId' => $messageId, - 'recipientHash' => $recipientHash, - 'type' => $message->getType(), - 'channel' => self::TYPE, - '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(), - ]); - - $this->database->createDocument('notifications', $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()); - } - } - - $response->setDeliveredTo($delivered); - return $response->toArray(); - } - - /** - * @return array - */ - private function buildPermissions(string $resourceType, string $resourceId, string $projectId): array - { - $permissions = []; - if ($resourceType === RESOURCE_TYPE_USERS) { - $permissions[] = Permission::read(Role::user($resourceId)); - $permissions[] = Permission::update(Role::user($resourceId)); - $permissions[] = Permission::delete(Role::user($resourceId)); - } - 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($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/Adapter/Webhook.php b/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php deleted file mode 100644 index 76e7234261..0000000000 --- a/src/Appwrite/Utopia/Messaging/Adapter/Webhook.php +++ /dev/null @@ -1,124 +0,0 @@ -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 - { - $client = new FetchClient(); - $client - ->setTimeout($timeout * 1000) - ->setConnectTimeout(\min(10, $timeout) * 1000) - ->setAllowRedirects(false) - ->setUserAgent('Appwrite Webhook'); - - 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' => $response->getStatusCode(), - 'response' => \is_string($output) ? $output : null, - 'error' => null, - ]; - } -} diff --git a/src/Appwrite/Utopia/Messaging/Messages/Console.php b/src/Appwrite/Utopia/Messaging/Messages/Console.php deleted file mode 100644 index 20d85f27e0..0000000000 --- a/src/Appwrite/Utopia/Messaging/Messages/Console.php +++ /dev/null @@ -1,82 +0,0 @@ - $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, - protected ?string $projectInternalId = 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; - } - - public function getProjectInternalId(): ?string - { - 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 deleted file mode 100644 index e1146f9bd0..0000000000 --- a/src/Appwrite/Utopia/Messaging/Messages/Webhook.php +++ /dev/null @@ -1,80 +0,0 @@ - $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; - } - - public function setOrigin(?string $origin): self - { - $this->origin = $origin; - - return $this; - } - - public function getOrigin(): ?string - { - return $this->origin; - } -} diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index e6b713d530..e8701b315e 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -70,10 +70,6 @@ class Response extends SwooleResponse public const MODEL_ROW = 'row'; public const MODEL_ROW_LIST = 'rowList'; - // Notifications - public const MODEL_NOTIFICATION = 'notification'; - public const MODEL_NOTIFICATION_LIST = 'notificationList'; - // Database Attributes public const MODEL_ATTRIBUTE = 'attribute'; public const MODEL_ATTRIBUTE_LIST = 'attributeList'; diff --git a/src/Appwrite/Utopia/Response/Model/Log.php b/src/Appwrite/Utopia/Response/Model/Log.php index 038eb9eea7..1e48a6bc89 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, hidden, keyProject, keyAccount, keyOrganization.', + 'description' => 'User type who triggered the audit log. Possible values: user, admin, guest, keyProject, keyAccount, keyOrganization.', 'default' => '', 'example' => 'user', ]) diff --git a/src/Appwrite/Utopia/Response/Model/Notification.php b/src/Appwrite/Utopia/Response/Model/Notification.php deleted file mode 100644 index 601871c620..0000000000 --- a/src/Appwrite/Utopia/Response/Model/Notification.php +++ /dev/null @@ -1,126 +0,0 @@ -addRule('$id', [ - 'type' => self::TYPE_STRING, - 'description' => 'Notification ID.', - 'default' => '', - 'example' => '5e5ea5c16897e', - ]) - ->addRule('$createdAt', [ - 'type' => self::TYPE_DATETIME, - 'description' => 'Notification creation date in ISO 8601 format.', - 'default' => '', - 'example' => self::TYPE_DATETIME_EXAMPLE, - ]) - ->addRule('$updatedAt', [ - 'type' => self::TYPE_DATETIME, - 'description' => 'Notification 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' => 'Notification 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('resourceType', [ - 'type' => self::TYPE_STRING, - 'description' => 'Resource type this notification is addressed to.', - 'default' => '', - 'example' => 'users', - ]) - ->addRule('resourceId', [ - 'type' => self::TYPE_STRING, - 'description' => 'Resource ID this notification is addressed to.', - 'default' => '', - 'example' => '5e5bb8c16897e', - ]) - ->addRule('parentResourceType', [ - 'type' => self::TYPE_STRING, - 'description' => 'Parent resource type for the notification.', - 'default' => '', - 'example' => 'projects', - ]) - ->addRule('parentResourceId', [ - 'type' => self::TYPE_STRING, - 'description' => 'Parent resource ID for the notification.', - 'default' => '', - 'example' => '5e5bb8c16897e', - ]) - ->addRule('projectId', [ - 'type' => self::TYPE_STRING, - 'description' => 'Project the notification pertains to.', - 'default' => '', - 'example' => '5e5bb8c16897e', - 'required' => false, - ]) - ->addRule('title', [ - 'type' => self::TYPE_STRING, - 'description' => 'Notification title.', - 'default' => '', - 'example' => 'New sign-in detected', - ]) - ->addRule('body', [ - 'type' => self::TYPE_STRING, - 'description' => 'Notification body.', - 'default' => '', - 'example' => 'A new device signed in to your account.', - ]) - ->addRule('read', [ - 'type' => self::TYPE_BOOLEAN, - 'description' => 'Whether the notification has been read.', - 'default' => false, - 'example' => false, - 'required' => false, - ]) - ->addRule('firstSeen', [ - 'type' => self::TYPE_DATETIME, - '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 notification was viewed from a notification logo.', - 'default' => null, - 'example' => self::TYPE_DATETIME_EXAMPLE, - 'required' => false, - ]) - ; - } - - public function getName(): string - { - return 'Notification'; - } - - public function getType(): string - { - return Response::MODEL_NOTIFICATION; - } -} diff --git a/tests/e2e/Services/Notifications/NotificationsBase.php b/tests/e2e/Services/Notifications/NotificationsBase.php deleted file mode 100644 index e6c8babe9b..0000000000 --- a/tests/e2e/Services/Notifications/NotificationsBase.php +++ /dev/null @@ -1,632 +0,0 @@ -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']); - } - - public function testListNotificationsEmpty(): void - { - // 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, '/notifications', $this->getConsoleAlertHeaders()); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertArrayHasKey('notifications', $response['body']); - $this->assertArrayHasKey('total', $response['body']); - $this->assertIsArray($response['body']['notifications']); - $this->assertIsInt($response['body']['total']); - - // 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']['notifications']), \min(\count($response['body']['notifications']), $response['body']['total'])); - } - - public function testWebhookFailureCreatesConsoleAlert(): void - { - $alertId = $this->seedWebhookFailureAlert(); - - $this->assertNotEmpty($alertId); - - $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - $found = null; - foreach ($list['body']['notifications'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - - $this->assertNotNull($found, 'Seeded alert not present in /notifications 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; - } - - /** - * 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(); - $this->assertNotEmpty($alertId); - - $patch = $this->client->call( - Client::METHOD_PATCH, - '/notifications/' . $alertId, - $this->getConsoleAlertHeaders(), - ['read' => true] - ); - - $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, '/notifications', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - $found = null; - foreach ($list['body']['notifications'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - - $this->assertNotNull($found); - $this->assertTrue($found['read']); - - self::$seededAlertId = null; // notification is read, downstream tests will seed fresh - } - - public function testMarkNotificationReadUnauthorized(): 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, - '/notifications/' . $alertId, - [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $stranger['session'], - 'x-appwrite-project' => 'console', - ], - ['read' => true] - ); - - $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, '/notifications', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - $found = null; - foreach ($list['body']['notifications'] as $alert) { - if ($alert['$id'] === $alertId) { - $found = $alert; - break; - } - } - - $this->assertNotNull($found); - $this->assertFalse($found['read']); - - self::$seededAlertId = $alertId; - } - - public function testProjectAccountCannotListPlatformNotificationsWithCollidingUserId(): 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, '/notifications', [ - '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 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_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: 'notification_track'`. Other claim - // purposes are silently ignored, which the purpose-claim test covers. - $jwt = (new JWT($secret, 'HS256', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ - 'messageId' => $alert['messageId'], - 'channel' => $alert['channel'], - 'recipientHash' => $recipientHash, - 'projectId' => $alert['projectId'], - 'purpose' => 'notification_track', - ]); - - $response = $this->client->call( - Client::METHOD_GET, - '/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, $secondResponse['headers']['status-code']); - - $viewedAgain = $this->findConsoleAlert($alertId); - $this->assertSame($tracked['firstSeen'], $viewedAgain['firstSeen']); - $this->assertNotSame($tracked['lastSeen'], $viewedAgain['lastSeen']); - - self::$seededAlertId = null; - } - - /** - * 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 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`. - */ - public function testNotificationLogoRejectsJwtWithoutPurposeClaim(): void - { - $alertId = $this->seedWebhookFailureAlert(); - $this->assertNotEmpty($alertId); - $alert = $this->findConsoleAlert($alertId); - - $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', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ - 'messageId' => $alert['messageId'], - 'channel' => $alert['channel'], - 'recipientHash' => $recipientHash, - 'projectId' => $alert['projectId'], - ]); - - $response = $this->client->call( - Client::METHOD_GET, - '/notifications/logos/appwrite', - ['x-appwrite-project' => 'console'], - ['jwt' => $jwtNoPurpose] - ); - - $this->assertSame(200, $response['headers']['status-code'], 'endpoint must always return 200'); - $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); - - $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', NOTIFICATION_TRACKING_JWT_TTL, 0))->encode([ - 'messageId' => $alert['messageId'], - 'channel' => $alert['channel'], - 'recipientHash' => $recipientHash, - 'projectId' => $alert['projectId'], - 'purpose' => 'something_else', - ]); - - $response = $this->client->call( - Client::METHOD_GET, - '/notifications/logos/appwrite', - ['x-appwrite-project' => 'console'], - ['jwt' => $jwtWrongPurpose] - ); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); - - $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 testNotificationLogoInvalidTokenReturnsSvg(): void - { - $alertId = $this->seedWebhookFailureAlert(); - $this->assertNotEmpty($alertId); - - $response = $this->client->call( - Client::METHOD_GET, - '/notifications/logos/appwrite', - ['x-appwrite-project' => 'console'], - ['jwt' => 'tampered-or-empty'] - ); - - $this->assertSame(200, $response['headers']['status-code']); - $this->assertStringContainsString('image/svg+xml', $response['headers']['content-type']); - $this->assertNotEmpty($response['body']); - $this->assertStringStartsWith('findConsoleAlert($alertId); - $this->assertFalse($found['read']); - $this->assertEmpty($found['firstSeen'] ?? null); - $this->assertEmpty($found['lastSeen'] ?? null); - - 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/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. - * - * @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', - ]; - } - - /** - * 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 - */ - protected function findConsoleAlert(string $alertId): array - { - $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - foreach ($list['body']['notifications'] as $alert) { - if ($alert['$id'] === $alertId) { - return $alert; - } - } - - $this->fail('Alert not present in /notifications response: ' . $alertId); - } - - /** - * 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. - $webhookName = 'Failing Webhook ' . \uniqid(); - $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' => $webhookName, - 'events' => ['users.*.create'], - 'url' => 'http://request-catcher-webhook: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'] ?? '') - ); - } - - // 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, $projectId, $webhookId, $webhookName) { - $list = $this->client->call(Client::METHOD_GET, '/notifications', $this->getConsoleAlertHeaders()); - $this->assertSame(200, $list['headers']['status-code']); - - foreach ($list['body']['notifications'] as $alert) { - if ( - ($alert['channel'] ?? '') === 'console' - && ($alert['projectId'] ?? '') === $projectId - && ($alert['parentResourceId'] ?? '') === $projectId - && ($alert['title'] ?? '') === 'Webhook deliveries have been paused' - && \str_contains($alert['body'] ?? '', $webhookName) - ) { - $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'], - ]; - } -} diff --git a/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php b/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php deleted file mode 100644 index d44a6921fe..0000000000 --- a/tests/e2e/Services/Notifications/NotificationsCustomConsoleTest.php +++ /dev/null @@ -1,16 +0,0 @@ -assertCount(17, $this->object->getServices()); + $this->assertCount(16, $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->assertEquals('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 @@ -44,8 +40,8 @@ final class ComposeTest extends TestCase public function testVolumes(): void { $this->assertCount(7, $this->object->getVolumes()); - $this->assertSame('appwrite-mariadb', $this->object->getVolumes()[0]); - $this->assertSame('appwrite-redis', $this->object->getVolumes()[1]); - $this->assertSame('appwrite-cache', $this->object->getVolumes()[2]); + $this->assertEquals('appwrite-mariadb', $this->object->getVolumes()[0]); + $this->assertEquals('appwrite-redis', $this->object->getVolumes()[1]); + $this->assertEquals('appwrite-cache', $this->object->getVolumes()[2]); } } diff --git a/tests/unit/Event/Message/NotificationTest.php b/tests/unit/Event/Message/NotificationTest.php deleted file mode 100644 index eecf583f34..0000000000 --- a/tests/unit/Event/Message/NotificationTest.php +++ /dev/null @@ -1,86 +0,0 @@ - '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 b990179969..8363ed4e85 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/Migration/MigrationVersionsTest.php b/tests/unit/Migration/MigrationVersionsTest.php index 6274d0820c..a97a582bd9 100644 --- a/tests/unit/Migration/MigrationVersionsTest.php +++ b/tests/unit/Migration/MigrationVersionsTest.php @@ -5,14 +5,7 @@ declare(strict_types=1); 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; final class MigrationVersionsTest extends TestCase { @@ -33,103 +26,4 @@ final 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('notifications'); - $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); - $this->assertArrayHasKey('firstSeen', $attributes); - $this->assertArrayHasKey('lastSeen', $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']); - } - - 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('notifications'); - - $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('notifications'); - $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/Modules/Notifications/NotificationsTest.php b/tests/unit/Platform/Modules/Notifications/NotificationsTest.php deleted file mode 100644 index 5daed2633e..0000000000 --- a/tests/unit/Platform/Modules/Notifications/NotificationsTest.php +++ /dev/null @@ -1,130 +0,0 @@ -document = $document; - $this->model = $model; - } -} - -final 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('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 - { - $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'); - $this->createNotification('user-email-receipt', RESOURCE_TYPE_USERS, 'user-a', NOTIFICATION_TYPE_EMAIL); - - $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, 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' => $channel, - 'projectId' => 'project', - 'projectInternalId' => '10', - 'resourceType' => $resourceType, - 'resourceId' => $resourceId, - 'resourceInternalId' => '20', - 'parentResourceType' => RESOURCE_TYPE_PROJECTS, - 'parentResourceId' => 'project', - 'parentResourceInternalId' => '10', - 'title' => 'Title', - 'body' => 'Body', - 'read' => false, - ])); - } -} diff --git a/tests/unit/Platform/Modules/Projects/SchedulesTest.php b/tests/unit/Platform/Modules/Projects/SchedulesTest.php deleted file mode 100644 index ca3f105f6c..0000000000 --- a/tests/unit/Platform/Modules/Projects/SchedulesTest.php +++ /dev/null @@ -1,161 +0,0 @@ -document = $document; - $this->model = $model; - } -} - -final 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)); - } -} diff --git a/tests/unit/Platform/Workers/DeletesTest.php b/tests/unit/Platform/Workers/DeletesTest.php deleted file mode 100644 index 42dc4ac895..0000000000 --- a/tests/unit/Platform/Workers/DeletesTest.php +++ /dev/null @@ -1,93 +0,0 @@ - '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('notifications', $worker->groups); - - $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()); - - $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()); - } -} diff --git a/tests/unit/Platform/Workers/MailsTest.php b/tests/unit/Platform/Workers/MailsTest.php deleted file mode 100644 index a6b7a123d7..0000000000 --- a/tests/unit/Platform/Workers/MailsTest.php +++ /dev/null @@ -1,98 +0,0 @@ -sendCount++; - $this->captured = $message; - - return [ - 'deliveredTo' => 1, - 'type' => $this->getType(), - 'results' => [['recipient' => $message->getTo()[0]['email'] ?? '', 'status' => 'sent']], - ]; - } -} - -final 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(), - new None(), - ); - } finally { - \putenv($previousSmtpHost === false ? '_APP_SMTP_HOST' : '_APP_SMTP_HOST=' . $previousSmtpHost); - } - - $this->assertSame(1, $adapter->sendCount); - $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $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/NotificationsTest.php b/tests/unit/Platform/Workers/NotificationsTest.php deleted file mode 100644 index 96c93c42fe..0000000000 --- a/tests/unit/Platform/Workers/NotificationsTest.php +++ /dev/null @@ -1,1383 +0,0 @@ -}> */ - public array $dispatched = []; - - /** @var array */ - public array $throwOn = []; - - protected function dispatch( - array $recipient, - string $messageId, - array $payload, - Document $project, - Registry $register, - Database $dbForPlatform, - Log $log, - ): ?string { - $channel = $recipient['channel']; - $this->dispatched[] = [ - 'channel' => $channel, - 'address' => $recipient['address'], - 'signatureKey' => $recipient['signatureKey'] ?? null, - 'payload' => $payload, - ]; - - 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($dbForPlatform, $messageId, $recipient, $payload, $project); - } - - return null; - } -} - -/** - * 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 $dbForPlatform, string $messageId, array $recipient, array $payload, Document $project): string - { - $this->persistAlertCalls++; - $alertId = parent::persistAlert($dbForPlatform, $messageId, $recipient, $payload, $project); - $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, Document $project, Database $dbForPlatform): ?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. - * - * 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 - { - return 'SpySMTP'; - } - - public function getMaxMessagesPerRequest(): int - { - return 1000; - } - - 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(), - 'results' => [['recipient' => $message->getTo()[0]['email'] ?? '', 'status' => 'sent']], - ]; - } -} - -final 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( - 'notifications', - [], - [], - [Permission::create(Role::any()), Permission::read(Role::any()), Permission::update(Role::any()), Permission::delete(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); - - // 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( - 'notifications', - '_key_recipient', - Database::INDEX_UNIQUE, - ['messageId', 'channel', 'recipientHash'], - [Database::LENGTH_KEY, 64, 64], - [Database::ORDER_ASC, Database::ORDER_ASC, Database::ORDER_ASC], - ); - - $this->registry = new Registry(); - $this->project = new Document(['$id' => 'project-x', '$sequence' => 'project-internal-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, - ]); - } - - 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 $resourceType, string $resourceId): string - { - 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 - { - $worker = new SpyNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $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', - 'deduplicationKey' => '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_TYPE_EMAIL, NOTIFICATION_TYPE_CONSOLE, NOTIFICATION_TYPE_WEBHOOK], $channels); - } - - public function testPersistsOneAlertPerRecipientChannel(): void - { - $worker = new SpyNotifications(); - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), - $this->userRecipient('user-2', NOTIFICATION_TYPE_CONSOLE, 'user-2'), - ], - 'subject' => 'Heads up', - 'body' => 'Read me', - 'deduplicationKey' => 'evt-multi', - 'permissions' => [Permission::read(Role::any())], - ]; - - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - - $rows = $this->database->find('notifications'); - $this->assertCount(2, $rows); - $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')); - } - } - - public function testDedupHitShortCircuitsBeforeDispatch(): void - { - $worker = new SpyNotifications(); - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [$this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1')], - 'subject' => 'Sub', - 'body' => 'B', - 'deduplicationKey' => 'dup-key', - ]; - - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - $this->assertCount(1, $worker->dispatched); - - $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_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->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $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(); - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - [ - 'address' => 'https://hooks.example.test/signed', - 'channel' => NOTIFICATION_TYPE_WEBHOOK, - 'signatureKey' => 'tenant-secret', - ], - [ - 'address' => 'https://hooks.example.test/unsigned', - 'channel' => NOTIFICATION_TYPE_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(); - $worker->throwOn[NOTIFICATION_TYPE_WEBHOOK] = new \RuntimeException('boom'); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [$this->userRecipient('https://h.example.test', NOTIFICATION_TYPE_WEBHOOK, 'user-1')], - 'subject' => 's', - 'body' => 'b', - 'deduplicationKey' => '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_TYPE_WEBHOOK, $tags['channel'] ?? null); - $this->assertSame('boom', $tags['error'] ?? null); - - $rows = $this->database->find('notifications'); - $this->assertCount(0, $rows, 'failed dispatch must not persist alert'); - } - - public function testDedupSkipsOnlyDeliveredRecipientAfterPartialFanoutFailure(): void - { - $worker = new SpyNotifications(); - $worker->throwOn[NOTIFICATION_TYPE_WEBHOOK] = new \RuntimeException('webhook down'); - - $messageId = \md5('partial-key'); - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $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', - 'deduplicationKey' => 'partial-key', - ]; - - 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()); - } - - $rows = $this->database->find('notifications', [ - 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('notifications', [ - Query::equal('messageId', [$messageId]), - ]); - $this->assertCount(2, $rows, 'retry must complete the missing recipient without duplicating console'); - } - - public function testConsoleChannelSkipsPersistAlert(): void - { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, '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('notifications', [ - 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' => [ - $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, '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('notifications'), 'failed console dispatch must not persist alert'); - } - - public function testMultiRecipientFanoutNoCollision(): void - { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('user-1', NOTIFICATION_TYPE_CONSOLE, 'user-1'), - $this->userRecipient('user-2', NOTIFICATION_TYPE_CONSOLE, '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('notifications'); - $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 testRecipientStructRoundtripsResourceFields(): void - { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - [ - 'address' => 'console-recipient', - 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'resourceType' => RESOURCE_TYPE_USERS, - 'resourceId' => 'u1', - 'resourceInternalId' => 'u1-internal', - 'parentResourceType' => RESOURCE_TYPE_PROJECTS, - 'parentResourceId' => 'project-x', - 'parentResourceInternalId' => 'project-internal-x', - ], - ], - 'subject' => 'Heads up', - 'body' => 'b', - 'deduplicationKey' => 'roundtrip', - ]; - - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - - $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')); - $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 testTrackingLogoInjectedIntoEmailHtml(): void - { - $spy = new SpyEmailAdapter(); - $this->registry->set('smtp', static fn () => $spy); - - // Force the cloud SMTP branch (project has no smtp config) and - // provide the tracking secret so injectTrackingLogo actually runs. - $previousSmtpHost = \getenv('_APP_SMTP_HOST'); - $previousTrackingSecret = \getenv('_APP_NOTIFICATIONS_TRACKING_SECRET'); - $previousDomain = \getenv('_APP_DOMAIN'); - $previousConsoleDomain = \getenv('_APP_CONSOLE_DOMAIN'); - \putenv('_APP_SMTP_HOST=spy.smtp.test'); - \putenv('_APP_NOTIFICATIONS_TRACKING_SECRET=test-key-32bytes-min-aaaaaaaaaaaaaa'); - \putenv('_APP_DOMAIN=api.example.test'); - \putenv('_APP_CONSOLE_DOMAIN=console.example.test'); - - try { - $worker = new Notifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-7'), - ], - 'subject' => 'Heads up', - 'body' => 'plain body', - 'deduplicationKey' => 'logo-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($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); - } - - $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $spy->captured, 'SpyEmailAdapter must capture exactly one EmailMessage'); - - $body = $spy->captured->getContent(); - $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('/logos\/appwrite\?jwt=([^"&]+)/', \html_entity_decode($body), $matches); - $this->assertNotEmpty($matches[1] ?? ''); - - $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); - $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('notification_track', $claims['purpose'] ?? null); - $this->assertArrayNotHasKey('alertId', $claims); - - // The logo must sit BEFORE the last . - $lastBodyClose = \strripos($body, ''); - $logoPosition = \strripos($body, ''); - $this->assertNotFalse($logoPosition); - $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->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $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(); - $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' => [ - $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-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, - '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 - // 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->project); - - $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, - 'recipientHash' => $this->recipientHash(NOTIFICATION_TYPE_EMAIL, 'user@example.test', RESOURCE_TYPE_USERS, 'user-7', 'user-7-internal'), - 'channel' => NOTIFICATION_TYPE_EMAIL, - '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, - ]); - - $threw = false; - try { - $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('notifications', [ - 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 testPersistAlertReturnsAlertIdAndStoresResource(): 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' => [ - $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, '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('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')); - $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')); - } - - /** - * 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' => [ - $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, '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('notifications', [ - 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('notifications', [ - Query::equal('messageId', [$messageId]), - ]); - $this->assertCount(1, $rows, 'retry must persist exactly one alert row'); - $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); - } - } - - 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' => [ - $this->userRecipient('user@example.test', NOTIFICATION_TYPE_EMAIL, 'user-9'), - $this->userRecipient('user-9', NOTIFICATION_TYPE_CONSOLE, 'user-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('notifications', [ - 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('notifications', [ - 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('notifications', [ - 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); - } - } - - /** - * Worker happy-path: email channel. - * - * Asserts the SMTP adapter is invoked once with the expected - * 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). - */ - public function testEmailChannelHappyPath(): void - { - $spy = new SpyEmailAdapter(); - $this->registry->set('smtp', static fn () => $spy); - - $previousSmtpHost = \getenv('_APP_SMTP_HOST'); - $previousTrackingSecret = \getenv('_APP_NOTIFICATIONS_TRACKING_SECRET'); - \putenv('_APP_SMTP_HOST=spy.smtp.test'); - \putenv('_APP_NOTIFICATIONS_TRACKING_SECRET=test-key-32bytes-min-aaaaaaaaaaaaaa'); - - try { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('happy@example.test', NOTIFICATION_TYPE_EMAIL, '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($previousTrackingSecret === false ? '_APP_NOTIFICATIONS_TRACKING_SECRET' : '_APP_NOTIFICATIONS_TRACKING_SECRET=' . $previousTrackingSecret); - } - - $this->assertSame(1, $spy->sendCount, 'SMTP send must be invoked exactly once'); - $this->assertInstanceOf(\Utopia\Messaging\Messages\Email::class, $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/notifications/logos/appwrite?jwt=', $body); - - $closing = \strripos($body, ''); - $logo = \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('notifications', [ - Query::equal('messageId', [$messageId]), - ]); - $this->assertCount(1, $rows); - $row = $rows[0]; - $this->assertSame('user-happy', $row->getAttribute('resourceId')); - $this->assertSame(NOTIFICATION_TYPE_EMAIL, $row->getAttribute('channel')); - $this->assertFalse($row->getAttribute('read')); - - // 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'); - } - - /** - * 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 resource read/update/delete access. - */ - public function testConsoleChannelHappyPath(): void - { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - [ - 'address' => 'console-recipient', - 'channel' => NOTIFICATION_TYPE_CONSOLE, - 'resourceType' => RESOURCE_TYPE_TEAMS, - 'resourceId' => 't1', - 'resourceInternalId' => 't1-internal', - 'parentResourceType' => RESOURCE_TYPE_PROJECTS, - 'parentResourceId' => 'project-x', - 'parentResourceInternalId' => 'project-internal-x', - ], - ], - '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('notifications', [ - Query::equal('channel', ['console']), - ]); - $this->assertCount(1, $rows, 'console adapter must write exactly one alert'); - - $row = $rows[0]; - $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', 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::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'); - } - - public function testConsoleChannelUsesPreviewBodyInsteadOfRenderedEmailHtml(): void - { - $worker = new CountingPersistAlertNotifications(); - - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('user-preview', NOTIFICATION_TYPE_CONSOLE, '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('notifications'); - $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 testEmailChannelThrowsWhenSmtpIsNotConfigured(): void - { - $previousSmtpHost = \getenv('_APP_SMTP_HOST'); - \putenv('_APP_SMTP_HOST='); - $threw = false; - - try { - $worker = new CountingPersistAlertNotifications(); - $payload = [ - 'project' => ['$id' => 'project-x'], - 'recipients' => [ - $this->userRecipient('missing-smtp@example.test', NOTIFICATION_TYPE_EMAIL, 'user-smtp'), - ], - 'subject' => 'No SMTP', - 'body' => 'Body', - 'deduplicationKey' => 'missing-smtp', - ]; - - 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('notifications')); - $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 resourceId'); - - $worker->action($this->buildMessage($payload), $this->project, $this->registry, $this->database, $this->log); - } - - /** - * 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', - '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', - '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('notifications', [ - 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/Platform/Workers/RegistrationTest.php b/tests/unit/Platform/Workers/RegistrationTest.php deleted file mode 100644 index 81b87199c2..0000000000 --- a/tests/unit/Platform/Workers/RegistrationTest.php +++ /dev/null @@ -1,30 +0,0 @@ -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); - } -} diff --git a/tests/unit/Platform/Workers/WebhooksTest.php b/tests/unit/Platform/Workers/WebhooksTest.php deleted file mode 100644 index 356db9f146..0000000000 --- a/tests/unit/Platform/Workers/WebhooksTest.php +++ /dev/null @@ -1,283 +0,0 @@ -createPlatformDatabase(); - $this->seedOwnerUser($database); - - $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(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 "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'), - \realpath($payload['bodyTemplate']) - ); - $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']); - $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]); - } - - 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', (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 - { - $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 testOwnerRoleDetectionAcceptsArrayAndCommaStringRoles(mixed $roles, bool $expected): void - { - $method = new \ReflectionMethod(Webhooks::class, 'hasOwnerRole'); - $membership = new Document([ - '$id' => 'membership-1', - 'roles' => $roles, - ]); - - $this->assertSame($expected, $method->invoke(null, $membership)); - } - - public static function ownerRoleProvider(): \Iterator - { - 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 - { - $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); - $database->createAttribute('users', 'name', Database::VAR_STRING, 256, false); - - 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', - 'name' => 'Ada Lovelace', - ])); - $database->createDocument('users', new Document([ - '$id' => 'user-2', - '$sequence' => 102, - 'email' => 'developer@example.test', - 'name' => 'Developer User', - ])); - } -} diff --git a/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php b/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php deleted file mode 100644 index 093a3cffb7..0000000000 --- a/tests/unit/Utopia/Messaging/Adapter/CapturingWebhook.php +++ /dev/null @@ -1,34 +0,0 @@ -, 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 deleted file mode 100644 index 9f066c51ab..0000000000 --- a/tests/unit/Utopia/Messaging/Adapter/ConsoleTest.php +++ /dev/null @@ -1,282 +0,0 @@ -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('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 - { - $this->authorization->cleanRoles(); - $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 $address, string $resourceType, string $resourceId): string - { - $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: [$this->recipient('user-1', 'user-1')], - title: 'Hello', - body: 'World', - type: 'info', - messageId: ID::custom('msg-aaa'), - projectId: 'project-1', - projectInternalId: 'project-internal-1', - ); - - $adapter = new Console($this->database); - $result = $adapter->send($message); - - $this->assertSame(1, $result['deliveredTo']); - - $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')); - $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')); - $this->assertFalse($stored->getAttribute('read')); - $this->assertNull($stored->getAttribute('firstSeen')); - $this->assertNull($stored->getAttribute('lastSeen')); - } - - public function testUserPermissionsScopedToRecipient(): void - { - $message = new ConsoleMessage( - recipients: [$this->recipient('user-2', 'user-2')], - title: 'Title', - body: 'Body', - messageId: ID::custom('msg-perms-user'), - projectId: 'project-1', - projectInternalId: 'project-internal-1', - ); - - (new Console($this->database))->send($message); - - $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); - $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: [$this->recipient('team-9', 'team-9', RESOURCE_TYPE_TEAMS)], - title: 'Heads up', - body: '...', - messageId: ID::custom('msg-team'), - projectId: 'project-1', - projectInternalId: 'project-internal-1', - ); - - (new Console($this->database))->send($message); - - $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); - $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 - { - $message = new ConsoleMessage( - recipients: [ - $this->recipient('a', 'a'), - $this->recipient('b', 'b'), - ], - title: 'Heads up', - body: 'multi', - messageId: ID::custom('same-msg'), - projectId: 'project-1', - projectInternalId: 'project-internal-1', - ); - - $adapter = new Console($this->database); - $result = $adapter->send($message); - - $this->assertSame(2, $result['deliveredTo']); - - $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'); - - $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'); - - $this->assertSame('same-msg', $rowA->getAttribute('messageId')); - $this->assertSame('same-msg', $rowB->getAttribute('messageId')); - $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); - } - - 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 WebhookMessage(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, 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 - // and must treat it as a successful (idempotent) send. - $this->database->createDocument('notifications', new Document([ - '$id' => $documentId, - '$permissions' => [Permission::read(Role::any())], - 'messageId' => $messageId, - 'recipientHash' => \substr(\md5($userId . ':' . RESOURCE_TYPE_USERS . ':' . $userId . ':' . $userId . '-internal:projects:project-1:project-internal-1'), 0, 16), - 'channel' => 'console', - '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: [$this->recipient($userId, $userId)], - title: 'Same alert resent', - body: 'b', - messageId: ID::custom($messageId), - projectId: 'project-x', - projectInternalId: 'project-internal-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('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'); - } -} diff --git a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php b/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php deleted file mode 100644 index 9edd6b7d0c..0000000000 --- a/tests/unit/Utopia/Messaging/Adapter/WebhookTest.php +++ /dev/null @@ -1,180 +0,0 @@ - '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\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', - )); - } -}