# ActivityPub phase 3: posts **Status: done** on `feature/activitypub` (`bc717c2` … `860d4d0`). The GoToSocial interop test now covers posts too: a weekly summary reaches a follower's home timeline as `private` with the expected content, and disappears from it when deleted. The manual pass with a real Mastodon account through a tunnel (done alongside phase 4's, see its notes) confirmed a forced weekly summary posted and deleted correctly there too. Where this differed from the plan: - **A top-10 entry needs a real top 10 first.** With fewer than `TOP_N` distinct artists, every first play of a new artist would trivially "enter the top 10" — caught by a test that used the real (unpatched) thresholds instead of always overriding them down for speed. Nothing is posted until a user actually has that many distinct artists. - **`post_now()` is a separate path from the normal check.** `flask federation post-weekly --now` and the Sharing page's preview both need the week still *in progress*, not the last one whose Monday-09:00 gate has passed; they share the posting logic (`_post_for_window`) with the normal `check_and_post`, but compute a different window. - **The Note's `url` needed the username, not just the post's id.** `post.html` (the UI page a browser lands on) has no other way to know whose post to look up; caught while wiring up the UI, not by a test written against the plan. This implements phase 3 of [activitypub.md](activitypub.md): **weekly summaries** and **milestones**. Now playing is phase 4. It builds on [phase 2](activitypub-phase2.md): followers, signed delivery and retries. **Outcome:** - A user who shares gets two kinds of post, delivered to their followers: a summary of the past week, every Monday morning in their own time zone, and occasional milestone posts. - Posts are **followers only by default**, following the user's visibility setting. - Each kind can be switched off, and any post can be deleted. Turning sharing off deletes the posts from followers' servers. - Existing history never produces posts retroactively. Everything is in `scrobbler.federation` except one small addition to the core stats service (§1), and the boundary tests keep holding. ## 1. Core: stats over a date range - `services.stats` gains top artists, top tracks and play counts for an explicit window (`start`, `end`). Today it only knows the rolling periods (`7day` and so on). - The existing functions keep working, re-expressed on top of the window version. This is a core change and is tested like one. Federation uses it through the public service, as the boundary rules require. ## 2. Settings and data (migration on the `federation` branch) - **`federation_settings` gains:** - `timezone`: an IANA name such as `Europe/London`, validated with `zoneinfo`. The Sharing page fills it in from the browser the first time. - `post_weekly_summary`: default on. - `post_milestones`: default on. - **`federation_posts`**, one row per post: - user, `kind` (`weekly` or `milestone`) - `key`, unique per user, e.g. `weekly:2026-W39` or `milestone:scrobbles:10000` - the Note's UUID, the visibility it was sent with, plain-text and HTML content - `created_at`, and `deleted_at` (a tombstone) The unique key makes posting **idempotent**: a worker restart or a repeated check can never post twice. - **`federation_milestone_marks`**: user plus key, with `posted` true or false. It records thresholds already passed, including ones deliberately *not* posted (see §5). - **`federation_pending_checks`**: user, `since`, `reason`. It's a small queue, filled by event receivers (§5), so receivers never do real work inside a scrobble request. ## 3. Vocabulary (pure: `protocol/vocab.py`) - **`note(...)`**: a `Note` with: - `id`, `attributedTo`, `content` (HTML) plus `contentMap` (`{"en": …}`) - `published`, `url` (the UI post page) - `to` and `cc` from the visibility setting - `tag` (`Hashtag` objects: `#Scrobbled`, and `#NowPlaying` in phase 4) - `sensitive: false` - **`create(...)`**: `Create` wrapping the Note, with the same addressing. - **`delete(...)`**: `Delete` whose object is a `Tombstone` of the Note (the same shape Mastodon sends). - **`ordered_collection_page(...)`**, used for outbox paging. - **Addressing** comes from one function (`protocol/addressing.py`), unit-tested for each visibility: - followers only: `to` followers, no `cc` - unlisted: `to` followers, `cc` Public - public: `to` Public, `cc` followers - The HTML uses only tags Mastodon keeps (`p`, `br`, `strong`, `em`, `ol`, `li`), and every artist and track name is escaped. A formatting test covers awkward names (`