Attach cover art to ActivityPub "now playing" posts
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 11s
CI / federation-interop (push) Successful in 1m38s
CI / test (push) Successful in 3m31s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 11s
CI / federation-interop (push) Successful in 1m38s
CI / test (push) Successful in 3m31s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
Note objects can now carry an optional attachment (an AS2 Image), which Mastodon and other AP servers render as the post's media. Only "now playing" posts use it for now - it's the only post type tied to a single track, and therefore the only one with one unambiguous album; weekly summaries and milestones aggregate many tracks. The attachment points straight at the Cover Art Archive URL cached in AlbumArt, not our own authenticated /api/v1/art/album proxy - a remote server fetches attachments unauthenticated, and Cover Art Archive is already public. The lookup happens in the periodic now-playing worker task, not a request, so a cache-miss MusicBrainz/ Cover Art Archive call adds no request latency. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Hn2QwS1HRT8tFAzo8ew63
This commit is contained in:
1 parent
f106d68921
commit
44abd70caa
5 files changed
+114
-8
No files matched your search
@@ -8,18 +8,33 @@ from scrobbler.extensions import db
|
||||
from scrobbler.federation import sharing
|
||||
from scrobbler.federation.models import FederationActivity, FederationNowPlaying, FederationPost
|
||||
from scrobbler.federation.publishing import now_playing
|
||||
from scrobbler.models import NowPlaying
|
||||
from scrobbler.models import AlbumArt, NowPlaying
|
||||
from scrobbler.services.scrobbles import TrackInput, update_now_playing
|
||||
|
||||
|
||||
def play(user, artist="Radiohead", track="Reckoner") -> datetime:
|
||||
def play(user, artist="Radiohead", track="Reckoner", album=None) -> datetime:
|
||||
"""Sets the track as playing (started_at = now) and returns that now, so tests
|
||||
always measure elapsed/throttle time relative to when it actually started, not a
|
||||
fixed constant that would drift from real test-execution time."""
|
||||
update_now_playing(user, TrackInput(artist=artist, track=track))
|
||||
update_now_playing(user, TrackInput(artist=artist, track=track, album=album))
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
def cache_art(artist: str, album: str, status: str, image_url: str | None = None) -> None:
|
||||
"""Primes the AlbumArt cache directly, so art.lookup() is a cache hit and never
|
||||
makes a real network call during tests."""
|
||||
db.session.add(
|
||||
AlbumArt(
|
||||
artist=artist,
|
||||
album=album,
|
||||
status=status,
|
||||
image_url=image_url,
|
||||
checked_at=datetime.now(UTC),
|
||||
)
|
||||
)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def stop_playing(user):
|
||||
"""Expire (rather than delete) the row, matching how it naturally goes away."""
|
||||
db.session.get(NowPlaying, user.id).expires_at = datetime.now(UTC) - timedelta(minutes=1)
|
||||
@@ -141,6 +156,44 @@ def test_post_sent_after_30_seconds(fed_ctx, posting_user):
|
||||
]
|
||||
|
||||
|
||||
def test_post_includes_album_art_when_known(fed_ctx, posting_user):
|
||||
cache_art(
|
||||
"Radiohead",
|
||||
"In Rainbows",
|
||||
"found",
|
||||
image_url="https://coverartarchive.org/release-group/test/front-500",
|
||||
)
|
||||
now = play(posting_user, album="In Rainbows")
|
||||
now_playing.check_and_post(posting_user.id, now + timedelta(seconds=31))
|
||||
[post] = posts_for(posting_user.id)
|
||||
activity = db.session.get(FederationActivity, post.activity_id)
|
||||
assert activity.document["object"]["attachment"] == [
|
||||
{
|
||||
"type": "Image",
|
||||
"mediaType": "image/jpeg",
|
||||
"url": "https://coverartarchive.org/release-group/test/front-500",
|
||||
"name": "Cover art for In Rainbows",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_post_has_no_attachment_without_an_album(fed_ctx, posting_user):
|
||||
now = play(posting_user) # no album
|
||||
now_playing.check_and_post(posting_user.id, now + timedelta(seconds=31))
|
||||
[post] = posts_for(posting_user.id)
|
||||
activity = db.session.get(FederationActivity, post.activity_id)
|
||||
assert "attachment" not in activity.document["object"]
|
||||
|
||||
|
||||
def test_post_has_no_attachment_when_art_is_not_found(fed_ctx, posting_user):
|
||||
cache_art("Radiohead", "Some Rare Bootleg", "not_found")
|
||||
now = play(posting_user, album="Some Rare Bootleg")
|
||||
now_playing.check_and_post(posting_user.id, now + timedelta(seconds=31))
|
||||
[post] = posts_for(posting_user.id)
|
||||
activity = db.session.get(FederationActivity, post.activity_id)
|
||||
assert "attachment" not in activity.document["object"]
|
||||
|
||||
|
||||
def test_post_throttled_within_30_minutes(fed_ctx, posting_user):
|
||||
now = play(posting_user, track="Reckoner")
|
||||
now_playing.check_and_post(posting_user.id, now + timedelta(seconds=31))
|
||||
|
||||
@@ -182,6 +182,27 @@ def test_note():
|
||||
assert doc["tag"] == [{"type": "Hashtag", "name": "#Scrobbler"}]
|
||||
assert doc["sensitive"] is False
|
||||
assert "@context" not in doc # carried by the wrapping Create instead
|
||||
assert "attachment" not in doc # omitted entirely, not an empty list, when there's none
|
||||
|
||||
|
||||
def test_note_with_attachment():
|
||||
image = {
|
||||
"type": "Image",
|
||||
"mediaType": "image/jpeg",
|
||||
"url": f"{BASE}/art.jpg",
|
||||
"name": "Cover art",
|
||||
}
|
||||
doc = vocab.note(
|
||||
f"{BASE}/users/alice/posts/1",
|
||||
actor_id=f"{BASE}/users/alice",
|
||||
content_html="<p>Hello</p>",
|
||||
published="2026-01-05T09:00:00Z",
|
||||
url=f"{BASE}/post.html?id=1",
|
||||
to=["FOLLOWERS"],
|
||||
cc=[],
|
||||
attachment=[image],
|
||||
)
|
||||
assert doc["attachment"] == [image]
|
||||
|
||||
|
||||
def test_create_wraps_a_note_with_the_same_addressing():
|
||||
|
||||
Reference in new issue
Block a user