diff --git a/app/init/worker/message.php b/app/init/worker/message.php index 6f6fc39eef..571bb2bbac 100644 --- a/app/init/worker/message.php +++ b/app/init/worker/message.php @@ -3,6 +3,7 @@ use Appwrite\Database\Factory as DatabaseFactory; use Appwrite\Deployment\Deployments; use Appwrite\Event\Event; +use Appwrite\Event\Message\ProjectContext; use Appwrite\Event\Publisher\Func as FunctionPublisher; use Appwrite\Event\Publisher\Notification as NotificationPublisher; use Appwrite\Event\Realtime; @@ -53,6 +54,13 @@ return function (Container $container): void { $container->set('dbForPlatform', fn (DatabaseFactory $databaseFactory) => $databaseFactory->platform(), ['databaseFactory']); + $container->set('projectContext', function ($message) { + $payload = $message->getPayload() ?? []; + $project = $payload['project'] ?? []; + + return ProjectContext::fromArray(\is_array($project) ? $project : []); + }, ['message']); + $container->set('project', function ($message, Database $dbForPlatform) { $payload = $message->getPayload() ?? []; $project = new Document($payload['project'] ?? []); diff --git a/src/Appwrite/Event/Message/Audit.php b/src/Appwrite/Event/Message/Audit.php index 1305b87e92..e719d89b22 100644 --- a/src/Appwrite/Event/Message/Audit.php +++ b/src/Appwrite/Event/Message/Audit.php @@ -26,13 +26,7 @@ final class Audit extends Base public function toArray(): array { return [ - 'project' => [ - '$id' => $this->project->getId(), - '$sequence' => $this->project->getSequence(), - 'database' => $this->project->getAttribute('database', ''), - 'teamId' => $this->project->getAttribute('teamId', ''), - 'teamInternalId' => $this->project->getAttribute('teamInternalId', ''), - ], + 'project' => ProjectContext::fromDocument($this->project)->toArray(), 'user' => $this->user->getArrayCopy(), 'impersonatorUser' => $this->impersonatorUser->getArrayCopy(), 'payload' => $this->payload, diff --git a/src/Appwrite/Event/Message/ProjectContext.php b/src/Appwrite/Event/Message/ProjectContext.php new file mode 100644 index 0000000000..fa2103fb06 --- /dev/null +++ b/src/Appwrite/Event/Message/ProjectContext.php @@ -0,0 +1,64 @@ +getId(), + sequence: (string) $project->getSequence(), + teamId: (string) $project->getAttribute('teamId', ''), + teamInternalId: (string) $project->getAttribute('teamInternalId', ''), + createdAt: (string) $project->getCreatedAt(), + region: (string) $project->getAttribute('region', ''), + ); + } + + public static function fromArray(array $project): self + { + return new self( + id: (string) ($project['$id'] ?? ''), + sequence: (string) ($project['$sequence'] ?? ''), + teamId: (string) ($project['teamId'] ?? ''), + teamInternalId: (string) ($project['teamInternalId'] ?? ''), + createdAt: (string) ($project['$createdAt'] ?? ''), + region: (string) ($project['region'] ?? ''), + ); + } + + public function isEmpty(): bool + { + return $this->id === ''; + } + + public function isConsole(): bool + { + return $this->id === 'console'; + } + + public function toArray(): array + { + return [ + '$id' => $this->id, + '$sequence' => $this->sequence, + 'teamId' => $this->teamId, + 'teamInternalId' => $this->teamInternalId, + '$createdAt' => $this->createdAt, + 'region' => $this->region, + ]; + } +} diff --git a/tests/unit/Event/Message/ProjectContextTest.php b/tests/unit/Event/Message/ProjectContextTest.php new file mode 100644 index 0000000000..de28df0bc5 --- /dev/null +++ b/tests/unit/Event/Message/ProjectContextTest.php @@ -0,0 +1,50 @@ + 'project-id', + '$sequence' => '42', + '$createdAt' => '2026-09-03T10:00:00.000+00:00', + 'teamId' => 'team-id', + 'teamInternalId' => '84', + 'region' => 'fra', + 'database' => 'mysql://user:secret@database/project', + 'webhooks' => [['url' => 'https://example.test']], + ])); + + $this->assertSame([ + '$id' => 'project-id', + '$sequence' => '42', + 'teamId' => 'team-id', + 'teamInternalId' => '84', + '$createdAt' => '2026-09-03T10:00:00.000+00:00', + 'region' => 'fra', + ], $context->toArray()); + } + + public function testParsesLegacyPartialContext(): void + { + $context = ProjectContext::fromArray([ + '$id' => 'project-id', + '$sequence' => 42, + ]); + + $this->assertSame('project-id', $context->id); + $this->assertSame('42', $context->sequence); + $this->assertSame('', $context->teamId); + $this->assertSame('', $context->teamInternalId); + $this->assertSame('', $context->createdAt); + $this->assertSame('', $context->region); + } +}