From f04e53a972fcce3c51619eed02066d7df7b9281b Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Sat, 26 Sep 2026 17:22:02 +0100 Subject: [PATCH] Web UI for now playing (phase 4, step 5) Sharing page gets a "Now playing" section: off / show on profile / show on profile and post, as a single choice matching the settings column - generalised sharing.js's visibility-only radio-group handling to a small RADIO_GROUPS list so now_playing_mode reuses the same fill/ save logic rather than adding a second special case. Public profile API and profile.html gain a live "now playing" field, sourced from the same actors.now_playing_text() the actor document uses and gated the same way (now_playing_mode off means never showing it, whatever's actually playing). Refactored actors.py along the way: now_playing_field() takes settings directly and does its own off-mode gating, rather than build_document() doing it externally - one place decides whether the field is shown, reused by the actor document, the push Update, and now the public profile API. Verified by hand in a real browser: settings round-trip through the Sharing page's new radio group, the public profile page shows "Now playing: Reckoner by Radiohead" once now_playing_mode is on, and the raw actor document (curl, Accept: application/activity+json) carries the same PropertyValue attachment. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- docs/openapi.json | 7 +++++++ frontend/css/style.css | 8 ++++++++ frontend/js/profile.js | 3 +++ frontend/js/sharing.js | 11 ++++++++--- frontend/sharing.html | 16 ++++++++++++++++ src/scrobbler/federation/actors.py | 29 +++++++++++++++++------------ src/scrobbler/federation/api.py | 5 ++++- src/scrobbler/federation/schemas.py | 3 +++ tests/test_federation_actors.py | 18 ++++++++++++++---- tests/test_federation_api.py | 18 ++++++++++++++++++ 10 files changed, 98 insertions(+), 20 deletions(-) diff --git a/docs/openapi.json b/docs/openapi.json index d863c4f..84633dd 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -4575,6 +4575,13 @@ "null" ] }, + "now_playing": { + "type": [ + "string", + "null" + ], + "description": "\"Track by Artist\", or null" + }, "handle": { "type": "string", "example": "@alice@scrobble.example" diff --git a/frontend/css/style.css b/frontend/css/style.css index 9f4c983..1d80019 100644 --- a/frontend/css/style.css +++ b/frontend/css/style.css @@ -954,6 +954,14 @@ input { white-space: pre-line; } +.now-playing { + padding: 0.6rem 1rem; + border-radius: var(--radius-control); + border-left: 4px solid var(--glow); + background: var(--paper-raised); + margin: 0.75rem 0; +} + .profile-follow { margin: 2rem 0 0.4rem; } diff --git a/frontend/js/profile.js b/frontend/js/profile.js index bbf1031..31a4456 100644 --- a/frontend/js/profile.js +++ b/frontend/js/profile.js @@ -25,6 +25,9 @@ try { h("h1", {}, profile.display_name), h("div", { class: "approve-app handle" }, profile.handle), profile.bio ? h("p", { class: "lede profile-bio" }, profile.bio) : null, + profile.now_playing + ? h("p", { class: "now-playing" }, "Now playing: ", h("strong", {}, profile.now_playing)) + : null, h("h2", { class: "profile-follow" }, "Follow from the fediverse"), h( "p", diff --git a/frontend/js/sharing.js b/frontend/js/sharing.js index 3fe7fb8..fd3f1cf 100644 --- a/frontend/js/sharing.js +++ b/frontend/js/sharing.js @@ -5,6 +5,7 @@ import { signedInPage } from "./layout.js"; const form = document.getElementById("sharing"); const messages = document.getElementById("messages"); const handlePanel = document.getElementById("handle-panel"); +const RADIO_GROUPS = ["visibility", "now_playing_mode"]; const FIELDS = [ "enabled", "visibility", @@ -14,6 +15,7 @@ const FIELDS = [ "post_weekly_summary", "post_milestones", "timezone", + "now_playing_mode", "display_name", "bio", ]; @@ -70,10 +72,12 @@ async function previewWeekly() { function fill(values) { for (const name of FIELDS) { + if (RADIO_GROUPS.includes(name)) { + form.querySelector(`input[name=${name}][value="${values[name]}"]`).checked = true; + continue; + } const field = form.elements[name]; - if (name === "visibility") { - form.querySelector(`input[name=visibility][value="${values.visibility}"]`).checked = true; - } else if (field.type === "checkbox") { + if (field.type === "checkbox") { field.checked = Boolean(values[name]); } else { field.value = values[name] ?? ""; @@ -150,6 +154,7 @@ async function save(event) { post_weekly_summary: form.elements.post_weekly_summary.checked, post_milestones: form.elements.post_milestones.checked, timezone: form.elements.timezone.value.trim() || "UTC", + now_playing_mode: form.querySelector("input[name=now_playing_mode]:checked").value, display_name: form.elements.display_name.value.trim() || null, bio: form.elements.bio.value.trim() || null, }; diff --git a/frontend/sharing.html b/frontend/sharing.html index 96ac78e..65383f8 100644 --- a/frontend/sharing.html +++ b/frontend/sharing.html @@ -83,6 +83,22 @@

See your posts

+
+ Now playing +
+ + +
+
+ + +
+
+ + +
+
+
Profile
diff --git a/src/scrobbler/federation/actors.py b/src/scrobbler/federation/actors.py index 219006d..5e5fc07 100644 --- a/src/scrobbler/federation/actors.py +++ b/src/scrobbler/federation/actors.py @@ -13,23 +13,28 @@ from scrobbler.services.scrobbles import get_now_playing NOW_PLAYING_FIELD = "Now playing" -def now_playing_field(user) -> list[tuple[str, str]]: - """The actor's now-playing `PropertyValue` field. Empty when nothing's playing - (`get_now_playing` already excludes expired tracks). - - Doesn't look at the user's now_playing_mode: core's now_playing table is populated - for every user regardless of federation settings, so a caller must gate this itself - (build_document does) rather than leak listening activity for users who never turned - now playing on at all. - """ +def now_playing_text(user) -> str | None: + """ "Track by Artist", or None if nothing unexpired is playing (`get_now_playing` + already excludes expired tracks). Doesn't look at now_playing_mode: core's + now_playing table is populated for every user regardless of federation settings, so + a caller must gate this itself (now_playing_field does) rather than leak listening + activity for users who never turned now playing on at all.""" playing = get_now_playing(user) if playing is None: + return None + return f"{playing.track} by {playing.artist}" + + +def now_playing_field(user, settings) -> list[tuple[str, str]]: + """The actor's now-playing `PropertyValue` field: empty unless now_playing_mode is + on and something's actually playing.""" + if settings.now_playing_mode == "off": return [] - return [(NOW_PLAYING_FIELD, f"{playing.track} by {playing.artist}")] + text = now_playing_text(user) + return [(NOW_PLAYING_FIELD, text)] if text else [] def build_document(urls: vocab.ActorUrls, user, settings) -> dict: - fields = now_playing_field(user) if settings.now_playing_mode != "off" else [] return vocab.person( urls, username=user.username, @@ -40,5 +45,5 @@ def build_document(urls: vocab.ActorUrls, user, settings) -> dict: discoverable=settings.discoverable, indexable=settings.indexable, published=settings.created_at.isoformat().replace("+00:00", "Z"), - fields=fields, + fields=now_playing_field(user, settings), ) diff --git a/src/scrobbler/federation/api.py b/src/scrobbler/federation/api.py index cbf967a..30f7342 100644 --- a/src/scrobbler/federation/api.py +++ b/src/scrobbler/federation/api.py @@ -5,7 +5,7 @@ from flask_smorest import Blueprint, abort from scrobbler.api.decorators import authenticated from scrobbler.extensions import db -from scrobbler.federation import activities, ids, publishing, sharing +from scrobbler.federation import activities, actors, ids, publishing, sharing from scrobbler.federation import followers as follower_state from scrobbler.federation import metrics as fed_metrics from scrobbler.federation.models import FederationBlock, FederationFollower, FederationPost @@ -175,6 +175,9 @@ def public_profile(username): "username": user.username, "display_name": settings.display_name or user.username, "bio": settings.bio, + "now_playing": actors.now_playing_text(user) + if settings.now_playing_mode != "off" + else None, **{k: v for k, v in _identity(user.username).items() if k != "profile_url"}, } diff --git a/src/scrobbler/federation/schemas.py b/src/scrobbler/federation/schemas.py index b6fc785..c4dfba7 100644 --- a/src/scrobbler/federation/schemas.py +++ b/src/scrobbler/federation/schemas.py @@ -68,6 +68,9 @@ class PublicProfileSchema(Schema): username = fields.String() display_name = fields.String() bio = fields.String(allow_none=True) + now_playing = fields.String( + allow_none=True, metadata={"description": '"Track by Artist", or null'} + ) handle = fields.String(metadata={"example": "@alice@scrobble.example"}) actor_url = fields.String() diff --git a/tests/test_federation_actors.py b/tests/test_federation_actors.py index 9da04d8..048368d 100644 --- a/tests/test_federation_actors.py +++ b/tests/test_federation_actors.py @@ -10,13 +10,23 @@ URLS = vocab.actor_urls( ) -def test_now_playing_field_is_empty_when_nothing_is_playing(fed_ctx, user): - assert actors.now_playing_field(user) == [] +def test_now_playing_text_is_none_when_nothing_is_playing(fed_ctx, user): + assert actors.now_playing_text(user) is None -def test_now_playing_field_shows_the_current_track(fed_ctx, user): +def test_now_playing_text_shows_the_current_track(fed_ctx, user): update_now_playing(user, TrackInput(artist="Radiohead", track="Reckoner")) - assert actors.now_playing_field(user) == [("Now playing", "Reckoner by Radiohead")] + assert actors.now_playing_text(user) == "Reckoner by Radiohead" + + +def test_now_playing_field_respects_the_mode(fed_ctx, user): + update_now_playing(user, TrackInput(artist="Radiohead", track="Reckoner")) + off = sharing.settings_for(user.id) + assert actors.now_playing_field(user, off) == [] + + sharing.update(user.id, {"enabled": True, "now_playing_mode": "profile"}) + on = sharing.settings_for(user.id) + assert actors.now_playing_field(user, on) == [("Now playing", "Reckoner by Radiohead")] def test_build_document_includes_the_now_playing_field(fed_ctx, user): diff --git a/tests/test_federation_api.py b/tests/test_federation_api.py index 495faf5..fe7d981 100644 --- a/tests/test_federation_api.py +++ b/tests/test_federation_api.py @@ -126,6 +126,7 @@ def test_public_profile(fed_client, sharing_user, make_user): "username": "alice", "display_name": "Alice", "bio": "Listening.", + "now_playing": None, "handle": "@alice@scrobble.test", "actor_url": "https://scrobble.test/users/alice", } @@ -133,6 +134,23 @@ def test_public_profile(fed_client, sharing_user, make_user): assert fed_client.get("/api/v1/federation/profiles/bob").status_code == 404 +def test_public_profile_shows_now_playing_when_the_mode_is_on(fed_client, fed_ctx, sharing_user): + from scrobbler.services.scrobbles import TrackInput, update_now_playing + + sharing.update(sharing_user.id, {"now_playing_mode": "profile"}) + update_now_playing(sharing_user, TrackInput(artist="Radiohead", track="Reckoner")) + body = fed_client.get("/api/v1/federation/profiles/alice").get_json() + assert body["now_playing"] == "Reckoner by Radiohead" + + +def test_public_profile_hides_now_playing_when_the_mode_is_off(fed_client, fed_ctx, sharing_user): + from scrobbler.services.scrobbles import TrackInput, update_now_playing + + update_now_playing(sharing_user, TrackInput(artist="Radiohead", track="Reckoner")) + body = fed_client.get("/api/v1/federation/profiles/alice").get_json() + assert body["now_playing"] is None + + def test_disabled_server_has_no_federation_api(client, auth): assert client.get("/api/v1/federation/settings", headers=auth).status_code == 404