mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #12412 from appwrite/console-presence-signature-change
Add console presence upsert and update test with real-time broadcast …
This commit is contained in:
4 files changed
+170
-20
No files matched your search
@@ -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();
|
||||
|
||||
Generated
+19
-19
@@ -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": [],
|
||||
|
||||
@@ -10,6 +10,8 @@ use Utopia\Database\Exception;
|
||||
|
||||
class Realtime extends Event
|
||||
{
|
||||
public const CONSOLE_ALLOWLIST = ['presences'];
|
||||
|
||||
protected array $subscribers = [];
|
||||
|
||||
private Adapter $realtime;
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user