Skip to content

feat(messaging): allow filtering messages by user and target - #13958

Merged
HarshMN2345 merged 5 commits into
mainfrom
fix/issue-8974
Sep 29, 2026
Merged

HarshMN2345 merged 5 commits into
mainfrom
fix/issue-8974

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

What does this PR do?

GET /v1/messaging/messages could not be filtered by recipient, so finding a scheduled message for a given user meant the client tracking messageId itself. Adds users and targets to Messages::ALLOWED_ATTRIBUTES.

That constant is the whole change. Queries/Base.php walks the messages collection config and only hands an attribute to the Filter validator when its $id is in the allowed list, and both users and targets are already VAR_STRING with array => true. This is the settled pattern for exposing an array attribute: Users::ALLOWED_ATTRIBUTES lists labels and Memberships::ALLOWED_ATTRIBUTES lists roles, both array attributes on their collections.

Scoped to the two the issue names. topics is left out, and the pre-existing dangling description entry in that constant is left alone deliberately.

Test Plan

  • composer lint on both changed files → {"tool":"pint","result":"passed"} each
  • New tests/unit/Utopia/Database/Validator/Queries/MessagesTest.php, mirroring the sibling UsersTest.php. Run and watched: OK, 1 test, 3 assertions. Whole directory also green: 9 tests, 34 assertions.
  • Negative control: reverted Messages.php alone and re-ran — the test fails (Failed asserting that false matches expected true), then restored. So it genuinely pins the change.
  • Confirmed against utopia-php/database's Filter validator that an array attribute permits contains/containsAny/containsAll/notContains/isNull/isNotNull/exists/notExists and rejects equal, so Query.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:

  1. Queries/Base.php feeds the same list to the Order validator, which does not reject array attributes, so Query.orderAsc('users') now passes validation and orders by a raw JSON column. Identical exposure already exists for labels on Users and roles on Memberships, so this matches precedent rather than introducing something new.
  2. The messages collection has only the _key_search fulltext index, so contains() on these is a scan. Same as labels on users, but messaging tables grow faster.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? — XList.php interpolates this constant into its queries param description, so regenerated specs and SDK docstrings will differ at release time; there is no repo-tracked spec directory to update here

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.
@ghost

ghost commented Sep 28, 2026

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The filters validate as intended, but recipient lookups need a scalable path before use on large message histories.

This change allows message-list queries to filter on the stored users and targets arrays. It also adds unit coverage for those filters and confirms that topics remains disallowed.

Verdict New comments Fixed Still open
✅ Approved 1 0 0
Finding Where
🟡 Index recipient lookups before exposing them on message lists src/Appwrite/Utopia/Database/Validator/Queries/Messages.php:14
📂 Walkthrough · 2
File Change
src/Appwrite/Utopia/Database/Validator/Queries/Messages.php Adds users and targets to the allowed message-query attributes.
tests/unit/Utopia/Database/Validator/Queries/MessagesTest.php Tests validation of the new filters and rejection of topics.

Reviewed 165d9dc · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +14 to +15
'users',
'targets',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds query filtering fields to the messaging system.

The PR appears mergeable, with the previously raised API-test and recipient-lookup performance concerns still open.

Fix All in Claude CodeFindings

  1. P2 Tests mirror the allowlist ▶
  2. P2 Recipient lookups lack indexes ▶
Fix with agent prompt
### Issue 1
tests/unit/Utopia/Database/Validator/Queries/MessagesTest.php:20-26
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.

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!

### Issue 2
src/Appwrite/Utopia/Database/Validator/Queries/Messages.php:14-15
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.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR allows message-list queries to filter by the existing users and targets array attributes.

  • The implementation-coupled validator test was removed; no filtered-list API test replaces it.
  • Recipient lookups still have no supporting index.

Reviews (4) · Last reviewed commit: "Merge branch 'main' into fix/issue-8974"

Comment on lines +20 to +26
$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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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!

Fix in Claude Code Fix in Codex

Comment on lines +14 to +15
'users',
'targets',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Fix in Claude Code Fix in Codex

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/issue-8974 (after).

Metric Before After Change
🚀 Requests/sec 171.38 173.73 ⚪ +1.4%
⏱️ Latency P50 99.62 ms 98.65 ms ⚪ -1%
⏱️ Latency P95 239.23 ms 230.32 ms ⚪ -3.7%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 98.65 230.32 11,115 173.73 -8.91
Account 190.02 367.94 585 9.74 +4.46
TablesDB 94.68 184.51 6,045 97.44 -9.22
Storage 91.38 197.52 2,925 48.49 +2.94
Functions 141.61 288.28 1,560 26.44 -3.85

Top API waits (after)

API request Max wait (ms)
account.name.update 572.72
account.prefs.update 488.01
tablesdb.rows.increment 445.83
functions.variables.update 419.42
functions.create 371.54

@HarshMN2345
HarshMN2345 merged commit 5d13980 into main Sep 29, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀 Enhancement: query messages by user and target

1 participant