Repository navigation
feat(messaging): allow filtering messages by user and target - #13958
Conversation
The messages `users` and `targets` attributes are string arrays, so listing them in ALLOWED_ATTRIBUTES makes Query.contains usable on both and keeps the endpoint's query description in sync.
🔵 Tier A · Mergeable after minor fixes
This change allows message-list queries to filter on the stored
📂 Walkthrough · 2
Reviewed |
| 'users', | ||
| 'targets', |
There was a problem hiding this comment.
Index recipient lookups before exposing them on message lists
The messages collection only indexes search, so filtering either array scans the project's message history. A list request also runs a matching count() by default (Messages/XList.php:94-95), making the intended scheduled-message lookup increasingly expensive for projects with many messages; this needs an indexable recipient lookup or a suitable index and migration.
🟡 Minor · performance · Reply if this doesn't apply.
|
| $this->assertEquals(true, $validator->isValid([Query::contains('users', ['someUserId'])]), $validator->getDescription()); | ||
| $this->assertEquals(true, $validator->isValid([Query::contains('targets', ['someTargetId'])]), $validator->getDescription()); | ||
|
|
||
| /** | ||
| * Test for Failure | ||
| */ | ||
| $this->assertEquals(false, $validator->isValid([Query::contains('topics', ['someTopicId'])]), $validator->getDescription()); |
There was a problem hiding this comment.
Tests mirror the allowlist The three assertions check only whether the validator accepts
users and targets and rejects topics. They do not check whether the API returns the right messages for a recipient, so the new filtering feature could fail while this test stays green. This violates the repository requirement to test observable behavior rather than mirror source configuration. Add a filtered-list API test with persisted messages before merging.
Context Used: Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. (source)
Knowledge Base Used: Messaging delivery API
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/unit/Utopia/Database/Validator/Queries/MessagesTest.php
Line: 20-26
Comment:
**Tests mirror the allowlist** The three assertions check only whether the validator accepts `users` and `targets` and rejects `topics`. They do not check whether the API returns the right messages for a recipient, so the new filtering feature could fail while this test stays green. This violates the repository requirement to test observable behavior rather than mirror source configuration. Add a filtered-list API test with persisted messages before merging.
**Context Used:** Call out and harshly judge implementation-coupled tests. We don't mirror source code, configuration, or version pins in assertions. We test observable behavior; use linters for syntax and schema checks. ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))
**Knowledge Base Used:** [Messaging delivery API](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/messaging-api.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| 'users', | ||
| 'targets', |
There was a problem hiding this comment.
Recipient lookups lack indexes This makes the
users and targets arrays available for filtering, but the messages collection has no index on either field. The list endpoint also counts matches by default. On a large message collection, repeated recipient lookups can scan the collection for both the page and its total, increasing response time and database load. Consider indexing these lookups or otherwise bounding their cost.
Knowledge Base Used: Messaging delivery API
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Utopia/Database/Validator/Queries/Messages.php
Line: 14-15
Comment:
**Recipient lookups lack indexes** This makes the `users` and `targets` arrays available for filtering, but the messages collection has no index on either field. The list endpoint also counts matches by default. On a large message collection, repeated recipient lookups can scan the collection for both the page and its total, increasing response time and database load. Consider indexing these lookups or otherwise bounding their cost.
**Knowledge Base Used:** [Messaging delivery API](https://app.greptile.com/appwrite/-/custom-context/knowledge-base/appwrite/appwrite/-/docs/messaging-api.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
a106894 to
dc55ba7
Compare
What does this PR do?
GET /v1/messaging/messagescould not be filtered by recipient, so finding a scheduled message for a given user meant the client trackingmessageIditself. AddsusersandtargetstoMessages::ALLOWED_ATTRIBUTES.That constant is the whole change.
Queries/Base.phpwalks the messages collection config and only hands an attribute to the Filter validator when its$idis in the allowed list, and bothusersandtargetsare alreadyVAR_STRINGwitharray => true. This is the settled pattern for exposing an array attribute:Users::ALLOWED_ATTRIBUTESlistslabelsandMemberships::ALLOWED_ATTRIBUTESlistsroles, both array attributes on their collections.Scoped to the two the issue names.
topicsis left out, and the pre-existing danglingdescriptionentry in that constant is left alone deliberately.Test Plan
composer linton both changed files →{"tool":"pint","result":"passed"}eachtests/unit/Utopia/Database/Validator/Queries/MessagesTest.php, mirroring the siblingUsersTest.php. Run and watched: OK, 1 test, 3 assertions. Whole directory also green: 9 tests, 34 assertions.Messages.phpalone and re-ran — the test fails (Failed asserting that false matches expected true), then restored. So it genuinely pins the change.utopia-php/database'sFiltervalidator that an array attribute permitscontains/containsAny/containsAll/notContains/isNull/isNotNull/exists/notExistsand rejectsequal, soQuery.contains('users', [userId])is what becomes available.Not run: no e2e. The natural home,
tests/e2e/Services/Messaging/MessagingBase.php, has uncommitted work on another branch right now, so it was left untouched. The HTTP surface wants a case there once that settles.Related PRs and Issues
Two things a reviewer should know rather than discover:
Queries/Base.phpfeeds the same list to the Order validator, which does not reject array attributes, soQuery.orderAsc('users')now passes validation and orders by a raw JSON column. Identical exposure already exists forlabelson Users androleson Memberships, so this matches precedent rather than introducing something new._key_searchfulltext index, socontains()on these is a scan. Same aslabelson users, but messaging tables grow faster.Checklist
XList.phpinterpolates this constant into itsqueriesparam description, so regenerated specs and SDK docstrings will differ at release time; there is no repo-tracked spec directory to update here