diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 494bd3da28..2b39208fcd 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -824,7 +824,8 @@ Http::shutdown() $queueForEvents->getParams() ); - if ($project->getId() !== 'console') { + $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); + if ($project->getId() !== 'console' || $allowedOnConsole) { $queueForRealtime ->from($queueForEvents) ->trigger(); diff --git a/composer.lock b/composer.lock index ee0276156e..6787411985 100644 --- a/composer.lock +++ b/composer.lock @@ -2895,16 +2895,16 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.37.0", + "version": "v1.38.1", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "6a21eb99c6973357967f6ce3708cd55a6bec6315" + "reference": "14c5439eec4ccff081ac14eca2dc57feb2a66d92" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6a21eb99c6973357967f6ce3708cd55a6bec6315", - "reference": "6a21eb99c6973357967f6ce3708cd55a6bec6315", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/14c5439eec4ccff081ac14eca2dc57feb2a66d92", + "reference": "14c5439eec4ccff081ac14eca2dc57feb2a66d92", "shasum": "" }, "require": { @@ -2956,7 +2956,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.37.0" + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.38.1" }, "funding": [ { @@ -2976,7 +2976,7 @@ "type": "tidelift" } ], - "time": "2026-04-10T17:25:58+00:00" + "time": "2026-05-26T12:51:13+00:00" }, { "name": "symfony/polyfill-php82", diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index f040d91468..1b21ef1e44 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -10,6 +10,8 @@ use Utopia\Database\Exception; class Realtime extends Event { + public const CONSOLE_ALLOWLIST = ['presences']; + protected array $subscribers = []; private Adapter $realtime; diff --git a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php index 1f1c52a234..63cb509a37 100644 --- a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php +++ b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php @@ -6,6 +6,11 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; +use Utopia\Database\Helpers\ID; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; +use WebSocket\Client as WebSocketClient; +use WebSocket\TimeoutException; class PresenceConsoleClientTest extends Scope { @@ -59,4 +64,146 @@ class PresenceConsoleClientTest extends Scope $this->assertIsNumeric($response['body']['usersOnlineTotal']); $this->assertIsArray($response['body']['presences']); } + + public function testConsolePresenceUpsertAndUpdateBroadcastRealtime(): void + { + $user = $this->getUser(); + $presenceId = ID::unique(); + + $client = $this->openConsolePresenceSocket($user, $presenceId); + $needsCleanup = false; + + try { + $upsertMetadata = ['testRunId' => ID::unique(), 'case' => 'console-upsert']; + $upsert = $this->client->call( + Client::METHOD_PUT, + '/presences/' . $presenceId, + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders()), + [ + 'status' => 'online', + 'metadata' => $upsertMetadata, + 'permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ] + ); + $this->assertSame(200, $upsert['headers']['status-code']); + $needsCleanup = true; + + $upsertEvent = $this->receivePresenceFrame($client, $presenceId, 'upsert'); + $this->assertSame('online', $upsertEvent['data']['payload']['status'] ?? null); + $this->assertSame($upsertMetadata, $upsertEvent['data']['payload']['metadata'] ?? null); + $this->assertSame($user['$id'], $upsertEvent['data']['payload']['userId'] ?? null); + // Internal fields must not leak through the realtime broadcast. + $this->assertArrayNotHasKey('userInternalId', $upsertEvent['data']['payload']); + $this->assertArrayNotHasKey('permissionsHash', $upsertEvent['data']['payload']); + $this->assertArrayNotHasKey('hostname', $upsertEvent['data']['payload']); + + $updateMetadata = ['testRunId' => $upsertMetadata['testRunId'], 'case' => 'console-update']; + $update = $this->client->call( + Client::METHOD_PATCH, + '/presences/' . $presenceId, + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders()), + [ + 'status' => 'away', + 'metadata' => $updateMetadata, + ] + ); + $this->assertSame(200, $update['headers']['status-code']); + + $updateEvent = $this->receivePresenceFrame($client, $presenceId, 'update'); + $this->assertSame('away', $updateEvent['data']['payload']['status'] ?? null); + $this->assertSame($updateMetadata, $updateEvent['data']['payload']['metadata'] ?? null); + $this->assertSame($user['$id'], $updateEvent['data']['payload']['userId'] ?? null); + } finally { + $client->close(); + + if ($needsCleanup) { + // Drop the row so reruns / parallel users don't accumulate orphaned presences. + $this->client->call( + Client::METHOD_DELETE, + '/presences/' . $presenceId, + \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], $this->getHeaders()) + ); + } + } + } + + private function openConsolePresenceSocket(array $user, string $presenceId): WebSocketClient + { + $queryString = \http_build_query([ + 'project' => 'console', + 'channels' => ['presences', 'presences.' . $presenceId], + ]); + + $client = new WebSocketClient( + 'ws://appwrite.test/v1/realtime?' . $queryString, + [ + 'headers' => [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $user['session'], + ], + 'timeout' => 1, + ] + ); + + $connected = \json_decode($client->receive(), true); + $this->assertSame('connected', $connected['type'] ?? null); + + return $client; + } + + private function receivePresenceFrame( + WebSocketClient $client, + string $presenceId, + string $action, + int $timeoutMs = 3000 + ): array { + $deadline = \microtime(true) + ($timeoutMs / 1000); + $lastFrame = []; + + while (\microtime(true) < $deadline) { + try { + $raw = $client->receive(); + } catch (TimeoutException) { + continue; + } + + $frame = \json_decode($raw, true); + if (!\is_array($frame)) { + continue; + } + + $lastFrame = $frame; + if ( + ($frame['type'] ?? null) === 'event' + && ($frame['data']['payload']['$id'] ?? null) === $presenceId + && \in_array( + 'presences.' . $presenceId . '.' . $action, + $frame['data']['events'] ?? [], + true + ) + ) { + $this->assertContains('presences', $frame['data']['channels'] ?? []); + $this->assertContains('presences.' . $presenceId, $frame['data']['channels'] ?? []); + return $frame; + } + } + + $this->fail( + 'Timed out waiting for presences.' . $presenceId . '.' . $action + . ' frame on console. Last frame: ' . \json_encode($lastFrame) + ); + } }