From 37fb34ae1207275892cf7116d0a18f840c75d4cf Mon Sep 17 00:00:00 2001 From: Darshan Date: Mon, 5 May 2025 14:33:27 +0530 Subject: [PATCH 1/3] wip: events compat based on version. --- app/controllers/shared/api.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 8c15f27acc..ba695acff4 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -483,10 +483,26 @@ App::init() /* * Background Jobs */ + $events = $route->getLabel('event', ''); $queueForEvents - ->setEvent($route->getLabel('event', '')) - ->setProject($project) - ->setUser($user); + ->setUser($user) + ->setEvent($events) + ->setProject($project); + + if (str_contains($events, '.tables.')) { + $requestFormat = $request->getHeader('x-appwrite-response-format', System::getEnv('_APP_SYSTEM_RESPONSE_FORMAT', '')); + if ($requestFormat && version_compare($requestFormat, '1.7.0', '<')) { + $map = [ + 'rows' => 'documents', + 'tables' => 'collections', + 'columns' => 'attributes', + ]; + + $compatibleEvents = str_replace(array_keys($map), array_values($map), $events); + // override the events + $queueForEvents->setEvent($compatibleEvents); + } + } $queueForAudits ->setMode($mode) From d70d001f5ab14a9d4b4437ea67d29d0797ef797a Mon Sep 17 00:00:00 2001 From: Darshan Date: Tue, 6 May 2025 12:52:05 +0530 Subject: [PATCH 2/3] add: backported channels. --- src/Appwrite/Messaging/Adapter/Realtime.php | 6 ++++++ .../Services/Realtime/RealtimeCustomClientTest.php | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index 2657ecccaa..b775078c61 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -311,10 +311,16 @@ class Realtime extends Adapter throw new \Exception('Table needs to be passed to Realtime for Row events in the Database.'); } + // 1.7.x $channels[] = 'rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows.' . $payload->getId(); + // 1.6.x + $channels[] = 'documents'; + $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$tableId') . '.documents'; + $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$tableId') . '.documents.' . $payload->getId(); + $roles = $table->getAttribute('documentSecurity', false) ? \array_merge($table->getRead(), $payload->getRead()) : $table->getRead(); diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index 671bc491f4..eef7978a0e 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -769,7 +769,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains('databases.' . $databaseId . '.tables.' . $actorsId . '.rows.' . $documentId, $response['data']['channels']); $this->assertContains('databases.' . $databaseId . '.tables.' . $actorsId . '.rows', $response['data']['channels']); @@ -813,7 +813,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows.{$documentId}", $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows", $response['data']['channels']); @@ -867,7 +867,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows.{$documentId}", $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows", $response['data']['channels']); @@ -987,7 +987,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows.{$documentId}", $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows", $response['data']['channels']); @@ -1026,7 +1026,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows.{$documentId}", $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows", $response['data']['channels']); @@ -1076,7 +1076,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('event', $response['type']); $this->assertNotEmpty($response['data']); $this->assertArrayHasKey('timestamp', $response['data']); - $this->assertCount(3, $response['data']['channels']); + $this->assertCount(6, $response['data']['channels']); // includes old and new channels $this->assertContains('rows', $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows.{$documentId}", $response['data']['channels']); $this->assertContains("databases.{$databaseId}.tables.{$actorsId}.rows", $response['data']['channels']); From ee117b816a7ecc4c2bbf4a2f7245825dc7907c7c Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 7 May 2025 10:33:54 +0530 Subject: [PATCH 3/3] manage: events and channels. --- app/controllers/shared/api.php | 22 +++------------------ src/Appwrite/Event/Realtime.php | 6 ++++-- src/Appwrite/Messaging/Adapter/Realtime.php | 14 ++++++------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index ba695acff4..8c15f27acc 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -483,26 +483,10 @@ App::init() /* * Background Jobs */ - $events = $route->getLabel('event', ''); $queueForEvents - ->setUser($user) - ->setEvent($events) - ->setProject($project); - - if (str_contains($events, '.tables.')) { - $requestFormat = $request->getHeader('x-appwrite-response-format', System::getEnv('_APP_SYSTEM_RESPONSE_FORMAT', '')); - if ($requestFormat && version_compare($requestFormat, '1.7.0', '<')) { - $map = [ - 'rows' => 'documents', - 'tables' => 'collections', - 'columns' => 'attributes', - ]; - - $compatibleEvents = str_replace(array_keys($map), array_values($map), $events); - // override the events - $queueForEvents->setEvent($compatibleEvents); - } - } + ->setEvent($route->getLabel('event', '')) + ->setProject($project) + ->setUser($user); $queueForAudits ->setMode($mode) diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index e1ec891e67..67d51e5c52 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -76,16 +76,18 @@ class Realtime extends Event $payload = new Document($this->getPayload()); $db = $this->getContext('database'); - $table = $this->getContext('table'); $bucket = $this->getContext('bucket'); + // can be Tables API or Collections API, generated channels include both! + $tableOrCollection = $this->getContext('table') ?? $this->getContext('collection'); + $target = RealtimeAdapter::fromPayload( // Pass first, most verbose event pattern event: $allEvents[0], payload: $payload, project: $this->getProject(), database: $db, - table: $table, + table: $tableOrCollection, bucket: $bucket, ); diff --git a/src/Appwrite/Messaging/Adapter/Realtime.php b/src/Appwrite/Messaging/Adapter/Realtime.php index b775078c61..5cb908464f 100644 --- a/src/Appwrite/Messaging/Adapter/Realtime.php +++ b/src/Appwrite/Messaging/Adapter/Realtime.php @@ -303,23 +303,23 @@ class Realtime extends Adapter $channels[] = 'projects.' . $project->getId(); $projectId = 'console'; $roles = [Role::team($project->getAttribute('teamId'))->toString()]; - } elseif (($parts[4] ?? '') === 'rows') { + } elseif (($parts[4] ?? '') === 'rows' || ($parts[4] ?? '') === 'documents') { if ($database->isEmpty()) { - throw new \Exception('Database needs to be passed to Realtime for Row events in the Database.'); + throw new \Exception('Database needs to be passed to Realtime for Document/Row events in the Database.'); } if ($table->isEmpty()) { - throw new \Exception('Table needs to be passed to Realtime for Row events in the Database.'); + throw new \Exception('Collection or the Table needs to be passed to Realtime for Document/Row events in the Database.'); } - // 1.7.x + // 1.7.x - Tables API $channels[] = 'rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows'; $channels[] = 'databases.' . $database->getId() . '.tables.' . $payload->getAttribute('$tableId') . '.rows.' . $payload->getId(); - // 1.6.x + // 1.6.x - Collections API $channels[] = 'documents'; - $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$tableId') . '.documents'; - $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$tableId') . '.documents.' . $payload->getId(); + $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$collectionId') . '.documents'; + $channels[] = 'databases.' . $database->getId() . '.collections.' . $payload->getAttribute('$collectionId') . '.documents.' . $payload->getId(); $roles = $table->getAttribute('documentSecurity', false) ? \array_merge($table->getRead(), $payload->getRead())