Skip to content

Optimize follower visibility queries#448

Merged
dahlia merged 1 commit intofedify-dev:mainfrom
dahlia:issue-173
Apr 26, 2026
Merged

Optimize follower visibility queries#448
dahlia merged 1 commit intofedify-dev:mainfrom
dahlia:issue-173

Conversation

@dahlia
Copy link
Copy Markdown
Member

@dahlia dahlia commented Apr 26, 2026

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

  • Added reusable visibility helpers in src/api/visibility.ts.
  • Updated src/api/v1/statuses.ts and src/api/v1/timelines.ts to use approved follow preloading.
  • Updated timeline inbox filtering in src/federation/timeline.ts to ignore pending follows.
  • Added a partial index on approved follower lookups in src/schema.ts and drizzle/0085_optimize_follower_visibility.sql.
  • Added regression coverage for approved vs. pending follows in src/api/visibility.test.ts, src/api/v1/statuses.test.ts, src/api/v1/timelines.test.ts, and src/federation/timeline.test.ts.
  • Documented the change in CHANGES.md.

Testing

  • pnpm run typecheck
  • pnpm run lint
  • pnpm run fmt:check
  • pnpm test:ci src/api/visibility.test.ts src/api/v1/statuses.test.ts src/api/v1/timelines.test.ts src/federation/timeline.test.ts --run
  • pnpm test -- --run

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
@dahlia dahlia added this to the Hollo 0.8 milestone Apr 26, 2026
@dahlia dahlia requested a review from Copilot April 26, 2026 08:44
@dahlia dahlia self-assigned this Apr 26, 2026
@dahlia dahlia added enhancement New feature or request performance labels Apr 26, 2026
@dahlia
Copy link
Copy Markdown
Member Author

dahlia commented Apr 26, 2026

@codex review

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 efficient uuid[]-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

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

@dahlia dahlia merged commit f4992a2 into fedify-dev:main Apr 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize follower visibility queries for better performance

2 participants