Optimize follower visibility queries#448
Merged
dahlia merged 1 commit intofedify-dev:mainfrom Apr 26, 2026
Merged
Conversation
Preload approved follow relationships before status and timeline queries, then reuse those results with array-backed visibility predicates. This removes the follower visibility subquery from status reads while avoiding unbounded bind parameter growth for large follow sets. Apply the same approved-follow semantics to timeline inbox construction so pending follows cannot receive follower-only posts through inbox-backed home or list timelines. Fixes fedify-dev#173 Assisted-by: Codex:gpt-5.5
Member
Author
|
@codex review |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes follower-only (“private”) post visibility checks by preloading approved follow relationships once and reusing them across API queries (statuses, context, quotes, timelines) and federation timeline inbox logic, while also adding a supporting partial index.
Changes:
- Added shared visibility helpers (
getApprovedFollowingAccountIds,buildPostVisibilityConditions, and an efficientuuid[]-parameterized author filter). - Updated API status/timeline queries and federation timeline inclusion logic to consistently apply “approved follow” semantics (pending follows don’t grant access).
- Added an index + migrations and regression tests covering approved vs. pending follows, plus changelog entry.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/schema.ts | Adds a partial index to speed up approved follower lookups by (follower_id, following_id) |
| src/federation/timeline.ts | Ensures private visibility in inbox-backed timelines only considers approved follows |
| src/federation/timeline.test.ts | Adds regression tests for excluding private posts / reply handling with pending follows |
| src/api/visibility.ts | Introduces reusable visibility scope + SQL condition builders using a single uuid[] parameter |
| src/api/visibility.test.ts | Verifies approved-follow filtering and that generated SQL no longer embeds a follows subquery |
| src/api/v1/timelines.ts | Replaces per-query follow subselects with preloaded approved-follow IDs and array-based filtering |
| src/api/v1/timelines.test.ts | Adds home timeline regression test ensuring only approved follows surface private posts |
| src/api/v1/statuses.ts | Centralizes status visibility filtering via the new helper and reuses preloaded scope in context/quotes |
| src/api/v1/statuses.test.ts | Adds regression tests ensuring pending follows cannot access private statuses (incl. context ancestors) |
| drizzle/meta/_journal.json | Registers migration 0085_optimize_follower_visibility |
| drizzle/meta/0085_snapshot.json | Updates Drizzle snapshot for the new index |
| drizzle/0085_optimize_follower_visibility.sql | Creates the new partial index in SQL migration form |
| CHANGES.md | Documents the visibility-query optimization and links the issue/PR |
|
Codex Review: Didn't find any major issues. 🚀 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR optimizes follower-only status visibility checks by preloading approved follow relationships and reusing them across status, conversation context, quote, and timeline queries.
The main visibility logic now lives in src/api/visibility.ts. Status reads no longer embed a follower lookup inside each post visibility condition, and large follow sets are passed to PostgreSQL as a single
uuid[]array parameter rather than expanding into one bind parameter per followed account.This also applies approved-follow semantics to timeline inbox construction in src/federation/timeline.ts, so pending follows cannot receive follower-only posts through inbox-backed home or list timelines.
Fixes #173.
Changes
Testing
pnpm run typecheckpnpm run lintpnpm run fmt:checkpnpm test:ci src/api/visibility.test.ts src/api/v1/statuses.test.ts src/api/v1/timelines.test.ts src/federation/timeline.test.ts --runpnpm test -- --run