refactor: improve channel handling for Realtime events and update tes…#11430
refactor: improve channel handling for Realtime events and update tes…#11430
Conversation
…ts for TablesDB compatibility
📝 WalkthroughWalkthroughEvent::mirrorCollectionEvents now returns a deduplicated, reindexed numeric array via array_values(array_unique(...)). The Realtime messaging adapter consolidates channel generation by merging legacy and tablesdb channel sources, adds a 'tablesdb' case in getDatabaseChannels, and removes prior per-prefix branching. Tests were expanded heavily: new/updated end-to-end realtime tests validate dual-prefix channel exposure (databases.* and tablesdb.*), require channels to be lists, and cover collection/table and document/row events, concurrency, and transactions. No public API signatures were changed. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Security Scan Results for PRDocker Image Scan Results
Source Code Scan Results🎉 No vulnerabilities found! |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/Appwrite/Messaging/Adapter/Realtime.php`:
- Around line 514-519: The merged channel list in Realtime.php is creating
duplicate entries (e.g., 'rows') when combining results from getDatabaseChannels
into $channels; after merging the three arrays assigned to $channels,
deduplicate the merged array (preserving a clean list for emission) before it’s
used/returned—update the code around the $channels assignment that calls
getDatabaseChannels to filter out duplicate channel names (e.g., via a
unique/filter step) so each channel appears only once.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/Appwrite/Event/Event.phpsrc/Appwrite/Messaging/Adapter/Realtime.phptests/e2e/Services/Realtime/RealtimeCustomClientTest.php
…ing channel merging in Realtime adapter
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/Appwrite/Messaging/Adapter/Realtime.php (1)
514-519:⚠️ Potential issue | 🟠 MajorDeduplicate merged channels before returning payload.
This merge still produces duplicates (notably
rows) because it reindexes witharray_values(...)but does not unique-filter the merged list.🛠️ Proposed fix
- $channels = array_values(array_merge( + $channels = array_values(array_unique(array_merge( self::getDatabaseChannels('legacy', $database->getId(), $resourceId, $payload->getId(), 'databases'), self::getDatabaseChannels('tablesdb', $database->getId(), $resourceId, $payload->getId(), 'databases'), self::getDatabaseChannels('tablesdb', $database->getId(), $resourceId, $payload->getId()) - )); + )));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Appwrite/Messaging/Adapter/Realtime.php` around lines 514 - 519, The merged channel list assigned to $channels from multiple calls to self::getDatabaseChannels(...) can contain duplicate entries (e.g., "rows"); after merging, filter duplicates before reindexing by applying a uniqueness filter (e.g., array_unique) to the combined array, then call array_values to reindex so $channels contains only distinct channel names; update the $channels assignment where the three getDatabaseChannels calls are merged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/Appwrite/Messaging/Adapter/Realtime.php`:
- Around line 514-519: The merged channel list assigned to $channels from
multiple calls to self::getDatabaseChannels(...) can contain duplicate entries
(e.g., "rows"); after merging, filter duplicates before reindexing by applying a
uniqueness filter (e.g., array_unique) to the combined array, then call
array_values to reindex so $channels contains only distinct channel names;
update the $channels assignment where the three getDatabaseChannels calls are
merged.
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/e2e/Services/Realtime/RealtimeCustomClientTest.php (2)
126-127: Remove redundant list assertion in the same block.
assertIsList()already validates this; keeping both checks adds noise.♻️ Proposed cleanup
- $this->assertIsList($response['data']['channels']); - $this->assertTrue(array_is_list($response['data']['channels'])); + $this->assertIsList($response['data']['channels']);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/e2e/Services/Realtime/RealtimeCustomClientTest.php` around lines 126 - 127, The test contains a redundant assertion: keep the built-in PHPUnit assertion $this->assertIsList($response['data']['channels']) and remove the duplicate check that calls array_is_list($response['data']['channels']) inside $this->assertTrue(...); update the RealtimeCustomClientTest test method to delete the line with $this->assertTrue(array_is_list(...)) so only $this->assertIsList(...) remains.
823-824: Consider replacing repeated8with a named test constant.The mirrored-channel count is asserted many times; a constant will make updates safer and easier.
♻️ Proposed refactor
class RealtimeCustomClientTest extends Scope { + private const MIRRORED_DB_CHANNEL_COUNT = 8;- $this->assertCount(8, $response['data']['channels']); + $this->assertCount(self::MIRRORED_DB_CHANNEL_COUNT, $response['data']['channels']);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/e2e/Services/Realtime/RealtimeCustomClientTest.php` around lines 823 - 824, The test uses the magic number 8 repeatedly when asserting the mirrored channel count; add a named test constant (e.g., EXPECTED_MIRRORED_CHANNEL_COUNT) to the test class and replace literal usages of 8 in assertions like $this->assertCount(8, $response['data']['channels']) and any other identical assertions with self::EXPECTED_MIRRORED_CHANNEL_COUNT to centralize the value and make future updates safer.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/e2e/Services/Realtime/RealtimeCustomClientTest.php`:
- Around line 126-127: The test contains a redundant assertion: keep the
built-in PHPUnit assertion $this->assertIsList($response['data']['channels'])
and remove the duplicate check that calls
array_is_list($response['data']['channels']) inside $this->assertTrue(...);
update the RealtimeCustomClientTest test method to delete the line with
$this->assertTrue(array_is_list(...)) so only $this->assertIsList(...) remains.
- Around line 823-824: The test uses the magic number 8 repeatedly when
asserting the mirrored channel count; add a named test constant (e.g.,
EXPECTED_MIRRORED_CHANNEL_COUNT) to the test class and replace literal usages of
8 in assertions like $this->assertCount(8, $response['data']['channels']) and
any other identical assertions with self::EXPECTED_MIRRORED_CHANNEL_COUNT to
centralize the value and make future updates safer.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/Appwrite/Messaging/Adapter/Realtime.phptests/e2e/Services/Realtime/RealtimeCustomClientTest.php
✨ Benchmark results
⚡ Benchmark Comparison
|
🔄 PHP-Retry SummaryFlaky tests detected across commits: |
…ts for TablesDB compatibility
What does this PR do?
(Provide a description of what this PR does and why it's needed.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Screenshots may also be helpful.)
Related PRs and Issues
Checklist