Web UI for now playing (phase 4, step 5)
CI / validate-monitoring (push) Successful in 14s
CI / lint (push) Successful in 21s
CI / federation-interop (push) Successful in 1m21s
CI / test (push) Successful in 4m2s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Sonnet 5 committed 2026-09-26 17:22:02 +01:00
1 parent 515acae233
commit f04e53a972
10 files changed
+98 -20

No files matched your search

+7
View File
@@ -4575,6 +4575,13 @@
"null"
]
},
"now_playing": {
"type": [
"string",
"null"
],
"description": "\"Track by Artist\", or null"
},
"handle": {
"type": "string",
"example": "@alice@scrobble.example"
+8
View File
@@ -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;
}
+3
View File
@@ -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",
+8 -3
View File
@@ -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,
};
+16
View File
@@ -83,6 +83,22 @@
<p class="muted"><a href="posts.html">See your posts</a></p>
</fieldset>
<fieldset class="choices">
<legend>Now playing</legend>
<div class="choice">
<input type="radio" id="np-off" name="now_playing_mode" value="off">
<label for="np-off"><strong>Off</strong><span class="muted">Nothing shown or posted.</span></label>
</div>
<div class="choice">
<input type="radio" id="np-profile" name="now_playing_mode" value="profile">
<label for="np-profile"><strong>Show on my profile</strong><span class="muted">A "Now playing" field, refreshed every few minutes and cleared when you stop.</span></label>
</div>
<div class="choice">
<input type="radio" id="np-posts" name="now_playing_mode" value="posts">
<label for="np-posts"><strong>Show on my profile and post</strong><span class="muted">The profile field, plus a post for each track played 30+ seconds, replacing the last one, at most every 30 minutes.</span></label>
</div>
</fieldset>
<fieldset class="choices">
<legend>Profile</legend>
<div class="field">
+17 -12
View File
@@ -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),
)
+4 -1
View File
@@ -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"},
}
+3
View File
@@ -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()
+14 -4
View File
@@ -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):
+18
View File
@@ -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