From dce90400e6fa7179631f0247e5733b969dfef00e Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Sat, 26 Sep 2026 11:39:59 +0100 Subject: [PATCH] Post vocabulary and visibility addressing (phase 3, step 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- .../federation/protocol/addressing.py | 26 +++++ src/scrobbler/federation/protocol/vocab.py | 93 +++++++++++++++++- tests/test_federation_protocol.py | 94 ++++++++++++++++++- 3 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 src/scrobbler/federation/protocol/addressing.py diff --git a/src/scrobbler/federation/protocol/addressing.py b/src/scrobbler/federation/protocol/addressing.py new file mode 100644 index 0000000..b47dd48 --- /dev/null +++ b/src/scrobbler/federation/protocol/addressing.py @@ -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") diff --git a/src/scrobbler/federation/protocol/vocab.py b/src/scrobbler/federation/protocol/vocab.py index 95c4edd..6705804 100644 --- a/src/scrobbler/federation/protocol/vocab.py +++ b/src/scrobbler/federation/protocol/vocab.py @@ -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): diff --git a/tests/test_federation_protocol.py b/tests/test_federation_protocol.py index 35fc76c..8030e31 100644 --- a/tests/test_federation_protocol.py +++ b/tests/test_federation_protocol.py @@ -2,7 +2,7 @@ import xml.etree.ElementTree as ET import pytest -from scrobbler.federation.protocol import hostmeta, media, nodeinfo, vocab, webfinger +from scrobbler.federation.protocol import addressing, hostmeta, media, nodeinfo, vocab, webfinger BASE = "https://scrobble.test" @@ -115,6 +115,98 @@ def test_collections(): "totalItems": 3, } assert vocab.ordered_collection("x", 0, items=[])["orderedItems"] == [] + assert vocab.ordered_collection("x", 5, first="x?page=1")["first"] == "x?page=1" + assert "orderedItems" not in vocab.ordered_collection("x", 5, first="x?page=1") + + +def test_ordered_collection_page(): + page = vocab.ordered_collection_page( + f"{BASE}/users/alice/outbox?page=2", + part_of=f"{BASE}/users/alice/outbox", + items=["a", "b"], + next_page=f"{BASE}/users/alice/outbox?page=3", + ) + assert page["type"] == "OrderedCollectionPage" + assert page["partOf"] == f"{BASE}/users/alice/outbox" + assert page["orderedItems"] == ["a", "b"] + assert page["next"] == f"{BASE}/users/alice/outbox?page=3" + assert "prev" not in page + + with_prev = vocab.ordered_collection_page("x", part_of="y", items=[], prev_page="z") + assert with_prev["prev"] == "z" and "next" not in with_prev + + +@pytest.mark.parametrize( + ("visibility", "to", "cc"), + [ + ("followers", ["FOLLOWERS"], []), + ("unlisted", ["FOLLOWERS"], [vocab.PUBLIC]), + ("public", [vocab.PUBLIC], ["FOLLOWERS"]), + ], +) +def test_addressing(visibility, to, cc): + assert addressing.address(visibility, "FOLLOWERS") == (to, cc) + + +def test_addressing_rejects_unknown_visibility(): + with pytest.raises(ValueError, match="visibility"): + addressing.address("everyone", "FOLLOWERS") + + +def test_is_public_or_unlisted(): + assert addressing.is_public_or_unlisted("public") is True + assert addressing.is_public_or_unlisted("unlisted") is True + assert addressing.is_public_or_unlisted("followers") is False + + +def test_hashtag(): + assert vocab.hashtag("Scrobbler") == {"type": "Hashtag", "name": "#Scrobbler"} + + +def test_note(): + doc = vocab.note( + f"{BASE}/users/alice/posts/1", + actor_id=f"{BASE}/users/alice", + content_html="

Hello

", + published="2026-01-05T09:00:00Z", + url=f"{BASE}/post.html?id=1", + to=["FOLLOWERS"], + cc=[], + tags=["Scrobbler"], + ) + assert doc["type"] == "Note" + assert doc["attributedTo"] == f"{BASE}/users/alice" + assert doc["content"] == "

Hello

" + assert doc["contentMap"] == {"en": "

Hello

"} + assert (doc["to"], doc["cc"]) == (["FOLLOWERS"], []) + assert doc["tag"] == [{"type": "Hashtag", "name": "#Scrobbler"}] + assert doc["sensitive"] is False + assert "@context" not in doc # carried by the wrapping Create instead + + +def test_create_wraps_a_note_with_the_same_addressing(): + inner = vocab.note( + "note-id", + actor_id="actor", + content_html="

x

", + published="2026-01-05T00:00:00Z", + url="url", + to=["FOLLOWERS"], + cc=["PUBLIC"], + ) + doc = vocab.create("activity-id", "actor", inner, to=["FOLLOWERS"], cc=["PUBLIC"]) + assert (doc["type"], doc["id"], doc["actor"]) == ("Create", "activity-id", "actor") + assert doc["object"] == inner + assert (doc["to"], doc["cc"]) == (["FOLLOWERS"], ["PUBLIC"]) + assert doc["published"] == "2026-01-05T00:00:00Z" + assert doc["@context"] == vocab.AS_CONTEXT + + +def test_delete_is_a_tombstone(): + doc = vocab.delete("activity-id", "actor", "note-id", to=["FOLLOWERS"], cc=["PUBLIC"]) + assert doc["type"] == "Delete" + assert doc["object"] == {"id": "note-id", "type": "Tombstone"} + assert (doc["to"], doc["cc"]) == (["FOLLOWERS"], ["PUBLIC"]) def test_nodeinfo():