Skip to content

Optimize Mastodon API performance#592

Merged
akirk merged 3 commits intomainfrom
optimize-mastodon-api-performance
Mar 4, 2026
Merged

Optimize Mastodon API performance#592
akirk merged 3 commits intomainfrom
optimize-mastodon-api-performance

Conversation

@akirk
Copy link
Copy Markdown
Owner

@akirk akirk commented Mar 2, 2026

Summary

Optimizes the Friends plugin's Mastodon API integration to avoid slow database queries that were causing timeline and account resolution to take seconds per post.

Companion to akirk/enable-mastodon-apps#278.

Changes

  • Reblog handler: try attributedTo metadata first (fast path), only fall back to mastodon_api_account filter when preferredUsername is missing. This avoids a slow get_by_uri() call per reblog.
  • Account handler: use Remote_Actors::get_acct() when ap_actor_id is available, avoiding the slow unindexed guid lookup.
  • Fix external actor resolution: use get_actor_url_from_attributed_to() instead of manually checking $meta['attributedTo']['id'], which failed for posts using the new ap_actor_id format.
  • Add is_known_activitypub_host(): queries all ap_actor guids once per request (post_type is indexed, no JOINs) and builds a hosts map. Used by convert_actor_to_mastodon_handle() to determine if a host is a known fediverse instance.
  • Rewrite convert_actor_to_mastodon_handle(): uses URL pattern matching on known hosts instead of the slow get_by_uri() calls. Handles /@username on any domain, and /users/username or other 2-segment paths on known hosts (.social TLD, mastodon.* domains, or hosts found in ap_actor table).

Test plan

  • Add unit tests for handle conversion patterns, reblog fast path, and account metadata resolution
  • CI passes

akirk added 3 commits March 2, 2026 10:07
Use get_actor_url_from_attributed_to() in mastodon_api_account_external_user
to handle both the new ap_actor_id format and legacy id format. Previously,
posts using the new format would fail the string check and return the
subscription user account ("external") instead of the actual actor.

Also optimize convert_actor_to_mastodon_handle() with a static cache and
URL pattern matching for known ActivityPub URL schemes (Mastodon, GoToSocial,
Pleroma, Akkoma) to avoid expensive unindexed guid queries.
- Reblog handler: try attributedTo metadata first (fast path), only
  fall back to mastodon_api_account filter when preferredUsername is
  missing
- Account handler: use Remote_Actors::get_acct() when ap_actor_id is
  available to avoid slow guid lookup
- Add is_known_activitypub_host() to detect fediverse instances from
  stored ap_actor posts (single query, cached per request)
- Rewrite convert_actor_to_mastodon_handle() to use URL pattern
  matching on known hosts instead of slow get_by_uri() calls
Test handle conversion for various URL patterns (/@username, /users/,
.social TLD, mastodon.* domains, unknown hosts), reblog metadata fast
path, and account metadata resolution from attributedTo.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 2, 2026

Test this PR in WordPress Playground

You can test this pull request directly in WordPress Playground:

Launch WordPress Playground

This will install and activate the plugin with the changes from this PR.

@akirk akirk merged commit 2495a3b into main Mar 4, 2026
25 checks passed
@akirk akirk deleted the optimize-mastodon-api-performance branch March 4, 2026 11:29
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.

1 participant