From abef6d790420e7caf8831a97f0be9cf0975a676f Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Thu, 28 May 2026 15:06:28 +0530 Subject: [PATCH 1/3] Add console presence upsert and update test with real-time broadcast support --- app/controllers/shared/api.php | 3 +- src/Appwrite/Event/Realtime.php | 2 + .../Presences/PresenceConsoleClientTest.php | 133 ++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) 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/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..acfc0861ce 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,132 @@ 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); + + 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']); + + $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(); + } + } + + 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' => 2, + ] + ); + + $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) + ); + } } From 75cae418a1dc01d99c1ed9a8d73e6ed2c0d12516 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Thu, 28 May 2026 15:17:22 +0530 Subject: [PATCH 2/3] Add cleanup logic for console presence upsert test --- .../Presences/PresenceConsoleClientTest.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php index acfc0861ce..63cb509a37 100644 --- a/tests/e2e/Services/Presences/PresenceConsoleClientTest.php +++ b/tests/e2e/Services/Presences/PresenceConsoleClientTest.php @@ -71,6 +71,7 @@ class PresenceConsoleClientTest extends Scope $presenceId = ID::unique(); $client = $this->openConsolePresenceSocket($user, $presenceId); + $needsCleanup = false; try { $upsertMetadata = ['testRunId' => ID::unique(), 'case' => 'console-upsert']; @@ -92,6 +93,7 @@ class PresenceConsoleClientTest extends Scope ] ); $this->assertSame(200, $upsert['headers']['status-code']); + $needsCleanup = true; $upsertEvent = $this->receivePresenceFrame($client, $presenceId, 'upsert'); $this->assertSame('online', $upsertEvent['data']['payload']['status'] ?? null); @@ -123,6 +125,18 @@ class PresenceConsoleClientTest extends Scope $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()) + ); + } } } @@ -140,7 +154,7 @@ class PresenceConsoleClientTest extends Scope 'origin' => 'http://localhost', 'cookie' => 'a_session_console=' . $user['session'], ], - 'timeout' => 2, + 'timeout' => 1, ] ); From 747f4558ecc5502754d50bc19da3bf6dcd1f58b4 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Thu, 28 May 2026 15:28:25 +0530 Subject: [PATCH 3/3] updated sdk generator and twig --- composer.lock | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/composer.lock b/composer.lock index 8f3ab9fd93..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", @@ -5638,16 +5638,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "1.31.1", + "version": "1.31.8", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "5699f6da951aef9378fabdcf12f40a9a54fb3128" + "reference": "3bc6c697e60807dd036b5c02c66d03183afad79f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/5699f6da951aef9378fabdcf12f40a9a54fb3128", - "reference": "5699f6da951aef9378fabdcf12f40a9a54fb3128", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/3bc6c697e60807dd036b5c02c66d03183afad79f", + "reference": "3bc6c697e60807dd036b5c02c66d03183afad79f", "shasum": "" }, "require": { @@ -5656,7 +5656,7 @@ "ext-mbstring": "*", "matthiasmullie/minify": "1.3.*", "php": ">=8.3", - "twig/twig": "3.26.*" + "twig/twig": "3.27.*" }, "require-dev": { "brianium/paratest": "7.*", @@ -5683,9 +5683,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/1.31.1" + "source": "https://github.com/appwrite/sdk-generator/tree/1.31.8" }, - "time": "2026-05-20T22:22:59+00:00" + "time": "2026-05-28T05:01:17+00:00" }, { "name": "brianium/paratest", @@ -8469,16 +8469,16 @@ }, { "name": "twig/twig", - "version": "v3.26.0", + "version": "v3.27.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "1fcae487b180d78e6351f4e0afa91f9eab96a2bc" + "reference": "04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/1fcae487b180d78e6351f4e0afa91f9eab96a2bc", - "reference": "1fcae487b180d78e6351f4e0afa91f9eab96a2bc", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed", + "reference": "04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed", "shasum": "" }, "require": { @@ -8533,7 +8533,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.26.0" + "source": "https://github.com/twigphp/Twig/tree/v3.27.0" }, "funding": [ { @@ -8545,7 +8545,7 @@ "type": "tidelift" } ], - "time": "2026-05-20T07:31:59+00:00" + "time": "2026-05-27T13:05:51+00:00" } ], "aliases": [],