Post vocabulary and visibility addressing (phase 3, step 3)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 12s
CI / federation-interop (push) Successful in 1m1s
CI / test (push) Successful in 2m19s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s

Pure additions to federation/protocol:
- vocab: note() (a Note, no @context of its own — always embedded in
  a Create), create() (wraps an object with the post's addressing),
  delete() (a Tombstone, the shape Mastodon sends), hashtag() (name
  only; we don't serve tag pages), ordered_collection_page() for
  outbox paging, and ordered_collection() gains an optional `first`
  link
- addressing.py: one function turning a visibility (followers/
  unlisted/public) into (to, cc), unit-tested for each value

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 11:39:59 +01:00
1 parent ed74a78625
commit dce90400e6
3 files changed
+210 -3

No files matched your search

@@ -0,0 +1,26 @@
"""to/cc addressing for our posts, by visibility."""
from scrobbler.federation.protocol.vocab import PUBLIC
VISIBILITIES = ("followers", "unlisted", "public")
def address(visibility: str, followers_url: str) -> tuple[list[str], list[str]]:
"""(to, cc) for a Note/Create/Delete with this visibility.
followers: to followers, no cc (never reaches a public timeline or search).
unlisted: to followers, cc Public (visible to anyone who looks, not pushed to feeds).
public: to Public, cc followers (the normal public-post shape).
"""
if visibility not in VISIBILITIES:
raise ValueError(f"unknown visibility: {visibility!r}")
if visibility == "public":
return [PUBLIC], [followers_url]
if visibility == "unlisted":
return [followers_url], [PUBLIC]
return [followers_url], []
def is_public_or_unlisted(visibility: str) -> bool:
"""Whether a post at this visibility may be listed in the public outbox."""
return visibility in ("public", "unlisted")
+91 -2
View File
@@ -138,8 +138,12 @@ def application(
}
def ordered_collection(collection_id: str, total: int, items: list | None = None) -> dict:
"""A collection. With items=None only the count is published (e.g. followers)."""
def ordered_collection(
collection_id: str, total: int, items: list | None = None, first: str | None = None
) -> dict:
"""A collection. With items=None only the count is published (e.g. followers). With
`first`, the collection just points at its first page rather than listing items
itself (e.g. the outbox, paged by ordered_collection_page)."""
doc = {
"@context": AS_CONTEXT,
"id": collection_id,
@@ -148,6 +152,30 @@ def ordered_collection(collection_id: str, total: int, items: list | None = None
}
if items is not None:
doc["orderedItems"] = items
if first:
doc["first"] = first
return doc
def ordered_collection_page(
page_id: str,
*,
part_of: str,
items: list,
next_page: str | None = None,
prev_page: str | None = None,
) -> dict:
doc = {
"@context": AS_CONTEXT,
"id": page_id,
"type": "OrderedCollectionPage",
"partOf": part_of,
"orderedItems": items,
}
if next_page:
doc["next"] = next_page
if prev_page:
doc["prev"] = prev_page
return doc
@@ -193,6 +221,67 @@ def undo(activity_id: str, actor_id: str, undone: dict) -> dict:
}
def hashtag(name: str) -> dict:
"""A Hashtag tag, e.g. #Scrobbler. No href: we don't serve tag pages, and Mastodon
still recognises and indexes the tag from `name` alone."""
return {"type": "Hashtag", "name": f"#{name}"}
def note(
note_id: str,
*,
actor_id: str,
content_html: str,
published: str,
url: str,
to: list[str],
cc: list[str],
tags: list[str] = (),
) -> dict:
"""A post (weekly summary or milestone). No `@context`: it's always embedded in the
Create that carries the same addressing, as Mastodon does."""
return {
"id": note_id,
"type": "Note",
"attributedTo": actor_id,
"content": content_html,
"contentMap": {"en": content_html},
"published": published,
"url": url,
"to": list(to),
"cc": list(cc),
"tag": [hashtag(t) for t in tags],
"sensitive": False,
}
def create(activity_id: str, actor_id: str, obj: dict, *, to: list[str], cc: list[str]) -> dict:
"""Wraps a Note (or other object) for publishing, with the same addressing."""
return {
"@context": AS_CONTEXT,
"id": activity_id,
"type": "Create",
"actor": actor_id,
"object": obj,
"published": obj.get("published"),
"to": list(to),
"cc": list(cc),
}
def delete(activity_id: str, actor_id: str, note_id: str, *, to: list[str], cc: list[str]) -> dict:
"""Withdraws one of our Notes, as a Tombstone (the shape Mastodon sends)."""
return {
"@context": AS_CONTEXT,
"id": activity_id,
"type": "Delete",
"actor": actor_id,
"object": {"id": note_id, "type": "Tombstone"},
"to": list(to),
"cc": list(cc),
}
def object_id(value) -> str | None:
"""An object's id, whether it's given inline (a dict) or as a bare URI."""
if isinstance(value, dict):