Files
Scrobbler/src/scrobbler/federation/protocol/media.py
T
mattandClaude Opus 5.5 e890568c54
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 57s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Federation protocol building blocks (phase 1, step 2)
Pure, framework-free code in federation/protocol:
- media: ActivityPub content types and Accept negotiation
- vocab: actor URLs, Person and Application actors (Mastodon's extension
  terms), public keys, collections, plain text to safe HTML
- webfinger: acct:/actor-URL resource parsing (other domains and bad
  usernames rejected) and JRD documents
- nodeinfo: discovery and NodeInfo 2.1; hostmeta: XRD pointing at
  WebFinger

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 20:20:24 +01:00

38 lines
1.4 KiB
Python

"""ActivityPub media types and content negotiation."""
ACTIVITY_JSON = "application/activity+json"
AS_PROFILE = "https://www.w3.org/ns/activitystreams"
LD_JSON = f'application/ld+json; profile="{AS_PROFILE}"'
JRD_JSON = "application/jrd+json"
def _media_ranges(header: str):
for part in (header or "").split(","):
pieces = [p.strip() for p in part.split(";")]
media = pieces[0].lower()
params = {}
for piece in pieces[1:]:
name, _, value = piece.partition("=")
params[name.strip().lower()] = value.strip().strip('"')
try:
quality = float(params.get("q", "1"))
except ValueError:
quality = 0.0
yield media, params, quality
def is_activitypub(media: str, params: dict) -> bool:
if media == ACTIVITY_JSON:
return True
# ld+json counts when it names the ActivityStreams profile (or no profile at all)
return media == "application/ld+json" and params.get("profile", AS_PROFILE) == AS_PROFILE
def wants_activitypub(accept: str | None) -> bool:
"""True if an Accept header asks for ActivityPub JSON (rather than HTML)."""
return any(q > 0 and is_activitypub(m, p) for m, p, q in _media_ranges(accept or ""))
def is_activitypub_content_type(content_type: str | None) -> bool:
return any(is_activitypub(m, p) for m, p, _ in _media_ranges(content_type or ""))