Display name only: UI, docs, API title, dashboards, hashtag and User-Agent. Identifiers (package, metrics, env vars, SCROBBLER_CONFIG) are unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
11 KiB
ActivityPub phase 4: now playing
Status: done on feature/activitypub (7d86bad … 9883111, plus a docker-compose.yml fix from the manual pass below). The GoToSocial interop test now covers now playing too: turning it on updates the "Now playing" field GoToSocial has cached for the profile, via the periodic Update(Person) push - not just a fresh fetch showing the live truth, which was already covered by the actor route's own tests.
The manual pass with a real Mastodon account through a tunnel (the last standing gap after every phase) is also done now, covering phases 1-4 together: WebFinger, actor and follow/unfollow/approve/block against mastodon.social and activitypub.academy over a cloudflared tunnel, a forced weekly summary posting and deleting correctly, and the now-playing profile field appearing, clearing, and respecting the 5-minute push throttle.
Where this differed from the plan:
- The now-playing key had to be hashed, not raw.
artist\x1ftrack\x1fstarted_atwas going straight intofederation_posts.key(VARCHAR(64)), which real track and artist names routinely overflow. Caught by the first Postgres-backed test that used a real track name, not by anything in the plan itself. now_playing_field()needed its own privacy gate. It read core'snow_playingtable (populated for every user regardless of federation settings) without checkingnow_playing_modeitself, so a user who'd never turned the feature on at all would still leak what they were listening to if a caller forgot to gate it externally. Found while designing the periodic push task, not by a test written against the plan - the existing tests happened to never combine "something playing" with "mode off". Fixed by moving the gate insidenow_playing_field()itself, so there's exactly one place that can get this wrong.docker-compose.ymlhardcoded the API'sCORS_ORIGINStoUI_BASE_URL. Found during the manual pass: pointingUI_BASE_URLat the tunnel host (needed for federation's own identity) silently changed which browser origins the API accepted, blocking the operator's ownlocalhost:8080UI from calling it at all - an.envCORS_ORIGINSsetting had no effect, since the compose file never referenced it. Fixed by makingCORS_ORIGINSits own variable, defaulting the same as before.TestConfigwasn't isolatingUI_BASE_URL/CORS_ORIGINSeither. The same local.envchange broke six tests that assert onhttp://localhost:8080URLs, the same way an earlier session foundFEDERATION_ENABLED/LASTFM_API_KEYleaking from a developer's.envinto the test run. Pinned alongside them.
This implements phase 4 of activitypub.md: now playing, the last of the "still open" decisions there. It builds on phase 3: publishing, delivery and the web UI for posts.
Chosen (of the doc's three options): profile field, plus opt-in posts — the profile field is what a user gets as soon as they turn now playing on at all; posts are a further, separate opt-in on top of that, matching the doc's own "profile field mode, then optional posts" framing.
Outcome:
- A sharing user can turn now playing on. While something is genuinely playing, their fediverse profile shows a "Now playing: Track by Artist" field, refreshed automatically and cleared a few minutes after playback stops.
- If they also opt into posts, each track they play for at least 30 seconds gets a post to their followers (at most one every 30 minutes), with the previous now-playing post deleted when a new one goes out — so at most one exists at a time.
- Nothing here is retroactive or history-based: it only ever reflects what's playing right now, so there's no baseline/import concern like milestones has.
Everything is in scrobbler.federation, using the core's existing services.scrobbles.get_now_playing() (no core changes needed this time), and the boundary tests keep holding.
1. Settings and data (migration on the federation branch)
-
federation_settingsgainsnow_playing_mode:off(default),profile, orposts. Validated the same wayvisibilityis. -
federation_now_playing, one row per user, the state of what Scrobbled has last told the fediverse (as opposed tonow_playingin core, which is what's actually playing):profile_key(nullable) andprofile_updated_at(nullable): what the lastUpdate(Person)said, and when it was sentpost_key(nullable) andpost_posted_at(nullable): the key of the last now-playing post, and when
A "key" is
artist\x1ftrack\x1fstarted_at— includingstarted_atmeans playing the same track again later is a genuinely new now-playing moment, not a stale repeat, and it's whatfederation_posts.keyuses too (so posting is idempotent through the same unique-constraint mechanism phase 3 already has, no new machinery needed).
2. Vocabulary (pure: protocol/vocab.py)
update(activity_id, actor_id, actor_object): wraps a document as anUpdate, addressed the same wayundo()already does (mirrors its shape).person(...)already takesfields(phase 1): a "Now playing" field is just one more(name, value)pair,"Track by Artist".- Now-playing posts reuse the existing
note()/create()builders from phase 3, tagged#NowPlayingin addition to#Scrobbled(already anticipated in the phase 3 plan's vocabulary notes).
3. Live actor vs. pushed updates
Two different questions, answered two different ways:
GET /users/<u>(a pull) always reflects the truth right now: it readsget_now_playing()directly and adds the field only while something unexpired is playing. No throttling — a fresh fetch should never lie.Update(Person)(a push), telling already-following servers proactively so they don't have to poll, is throttled to once every 5 minutes (federation_now_playing.profile_updated_at). A server that fetches in between sees the live truth anyway via the pull path above; the push is purely so it doesn't have to.
This split means a fast-changing "now playing" never spams followers' inboxes, while the profile itself is never stale to a server that asks.
4. publishing/now_playing.py
A periodic worker task (federation.now_playing, every 30 seconds — well under both throttle windows, so they stay the binding constraint) checks every sharing user with now_playing_mode != "off":
- Read
get_now_playing(user)(core service; federation reaches it throughscrobbler.services.scrobbles, already inside the public surface) and computecurrent_key(Noneif nothing's playing or it's expired). - Profile (mode
profileorposts): ifcurrent_key != federation_now_playing.profile_keyand at least 5 minutes have passed sinceprofile_updated_at(or it's never been sent), build the actor document with (or without) the now-playing field and sendUpdate(Person)to accepted followers' inboxes. Update the stored key and timestamp either way the check runs, so a change that arrives mid-throttle isn't retried every 30 seconds — only reconsidered once the throttle clears. - Posts (mode
postsonly): ifcurrent_keyis set, has been playing for at least 30 seconds, differs fromfederation_now_playing.post_key, and at least 30 minutes have passed sincepost_posted_at(or never posted): publish anow_playingpost (through the samepublishing.publish()as weekly/milestones — idempotent onnow_playing:<key>), then delete the previous now-playing post if one exists, and update the stored key and timestamp.
No event receiver, no pending-checks queue: unlike milestones, now playing doesn't need to react within a scrobble request, and the throttle windows (5 and 30 minutes) are so much longer than a 30-second poll that a periodic scan (the same shape as weekly.check_all) is simplest and cheapest.
5. Endpoints
GET /users/<u>: gains the live now-playing field (§3). No new endpoint.GET /users/<u>/outbox,GET /users/<u>/posts/<uuid>,/api/v1/federation/posts,DELETE /posts/{id}: unchanged —now_playingposts are just anotherkindinfederation_posts, so they already show up everywhere phase 3's post endpoints look./api/v1/federation/settings: gainsnow_playing_mode.
6. Web UI
- Sharing page, a new Now playing section: off / profile field / profile field + posts, as a single choice (matching the settings column), with a one-line explanation of the 5-minute and 30-minute throttles.
- Posts page:
now_playingposts show there like any other kind (already handled generically). - Profile page: shows the "Now playing" field when the profile response includes it (the public profile API already returns arbitrary settings-derived fields; this adds one, read from the same live path as the actor document).
7. Metrics, dashboard
scrobbler_federation_profile_updates_total{result}:sentorthrottled. (Posts already getscrobbler_federation_posts_total{kind="now_playing",visibility}for free, phase 3's existing metric.)- Federation dashboard: a panel on the existing "Posts" row for profile updates/min, and the
kind="now_playing"series already shows up in the existing posts-by-kind panel without changes.
8. Tests
- Vocabulary:
update()shape. - Actor document: the live now-playing field appears while playing, is absent once expired, absent when off.
now_playing.py:- profile update sent on a genuine change, throttled within 5 minutes, cleared when playback stops
- post sent only after 30 seconds played, throttled within 30 minutes, previous post deleted when a new one goes out
profilemode never posts;offmode does nothing at all- idempotence (the same key never posted twice, matching phase 3's pattern)
- Settings API:
now_playing_modevalidation and round-trip. - Interop (GoToSocial): extends the existing driver — now playing is turned on, a track is set as playing, and the test confirms GoToSocial's resolved account shows the "Now playing" field after the next
Update(Person); posts aren't re-tested here since they go through the same publish/delivery path phase 3 already covers end to end.
9. Docs
- README: the now-playing section, its modes and throttles.
- Federation README:
now_playing.pyadded to the publishing plug-ins list. - Regenerate the OpenAPI export.
- Mark this plan done.
Order of work (a commit per step once its tests pass)
- Settings column and
federation_now_playingtable - Vocabulary (
update()) and the live actor now-playing field publishing/now_playing.pyand its worker task- Settings API and schema
- Web UI (Sharing page section, profile page field)
- Metrics and dashboard
- Interop check
- Docs
Defaults I've chosen (easy to change)
| Choice | Default |
|---|---|
| Now playing | Off by default, per user |
| Profile push throttle | 5 minutes |
| Post throttle | 30 minutes |
| Minimum played before a post | 30 seconds |
| Deleting the previous now-playing post | Always, when a new one goes out |
| Worker poll interval | 30 seconds |
| Hashtags | #Scrobbled and #NowPlaying |