feat: reply contexts that show the post (v2.19.0)

Replies to Mastodon posts showed only the server's domain: the page has no
h-entry, so nothing was found and the host stood in for the author.

- Context sources in order: Bluesky AppView API, microformats, the
  origin's Mastodon API or this site's GoToSocial instance, OpenGraph
- Migration 024: author_handle, photo, photo_alt, source; rows without a
  source are fetched again once by the webmention worker
- Note page: a card with avatar, name, handle, text, image and date;
  home/tag/likes: "In reply to <author>" with a short quote
- Avatars and images saved locally (CSP allows only this site's images);
  avatar upkeep now covers reply contexts so their avatars aren't removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
PhilandClaude Opus 5.5 committed 2026-11-13 15:10:46 -07:00
1 parent 22e588377f
commit 62e257e0e0
14 files changed
+1094 -309

No files matched your search

@@ -0,0 +1,108 @@
# v2.19.0: Reply contexts that show the post
## Why
A reply to `https://defcon.social/@deviantollam/117611186265622833` showed
"In reply to: defcon.social" and nothing else. Reply contexts were read only
from microformats. Mastodon's status page is a JavaScript app shell with no
h-entry, so `fetch_reply_context` stored `fetch_status='fetched'` with no
content, and `extract_author` fell back to the URL's host for the author name.
GoToSocial pages have an h-card but no h-entry; Bluesky pages have neither.
The home page didn't use the context at all: it printed the raw URL.
The data was available elsewhere:
- Mastodon's public API (`/api/v1/statuses/<id>`) returned the author, text,
attachments (with alt text) and date without authentication.
- GoToSocial's API needs a token, but this site's own GoToSocial instance can
resolve any fediverse URL with signed requests
(`/api/v2/search?resolve=true`). The crossposter already does this for
native likes and boosts (`MastodonClient.find_status`).
- `public.api.bsky.app` answers `app.bsky.feed.getPostThread` for
`at://<handle>/app.bsky.feed.post/<rkey>` without logging in. (`getPosts`
rejects handles in the URI; `getPostThread` resolves them.)
- Most pages carry OpenGraph tags: `og:title`, `og:description`, `og:image`
(with `og:image:alt`), `og:site_name`, a published time. Fediverse servers
put "Name (@user@host)" in `og:title` and the handle in
`profile:username`.
## Sources, in order
| # | When | Source | `source` |
|---|---|---|---|
| 1 | `bsky.app/profile/<actor>/post/<rkey>` | Bluesky AppView API | `bluesky` |
| 2 | The page has an h-entry | Microformats (as before), image from `u-photo` or `og:image` | `mf2` |
| 3 | Fediverse path (`/@user/<id>`, `/@user/statuses/<id>`, `/users/…/statuses/…`, `/notes/…`, `/notice/…`, `/objects/…`) or a `profile:username` tag | The origin's Mastodon API (numeric Mastodon ids only), else this site's GoToSocial instance | `fediverse` |
| 4 | Any other HTML page | OpenGraph tags; author from the page's h-card or `<meta name="author">` | `opengraph` |
All requests other than the instance lookup go through `fetch_source`, so
they get the SSRF checks, size limit and timeouts. The instance lookup uses
the crossposting client (`MastodonClient.resolve_status`, which
`find_status` now calls too) and is only tried when GoToSocial crossposting
is configured.
Mastodon statuses keep their content warnings: when `spoiler_text` is set,
the snippet is "CW: …" and a `sensitive` post's image is left out. Content
HTML is sanitized with the webmention sanitizer (nh3; `p br strong em b i a
code pre blockquote`, `a[href]`) and cut to about 500 characters. Plain text
(Bluesky, OpenGraph) is escaped and split into paragraphs.
An `mf2` or `opengraph` title that is only the start of the content
(an mf2 implied name, a description that repeats the title) is dropped.
The URL's host is no longer used as the author's name.
## Storage (migration 024)
`reply_contexts` gains `author_handle`, `photo`, `photo_alt` and `source`.
Error rows get `source='error'`.
A row with no `source` was stored before v2.19.0. `fetch_reply_context`
fetches such rows again instead of returning them, and the webmention
worker calls `refresh_stale()` on every pass, which fetches up to five rows
that a note still uses. Each row is fetched once: success stores a source,
and a failure keeps the old data and marks it `legacy`.
## Images
The CSP only allows images from this site (`img-src 'self' data:`), so
remote avatars and images can't be shown directly. As with response avatars
(ADR-073 Decision 6), they are saved locally:
- Author avatars go into the existing `DATA_PATH/avatars/` (96 px squares).
- Post images go into `DATA_PATH/previews/`, at most 640 px wide, as WebP,
served from `/previews/<sha256>.webp`. Downloads are capped at 8 MB.
The images are saved right after the fetch. `avatars.fill_missing` also
covers reply contexts that notes use, so the worker retries what failed, and
`remove_unused` keeps those files (before this release it would have deleted
any avatar that wasn't a webmention's). Until there is a copy, the card shows
the coloured initial and leaves the image out.
## Display
`templates/partials/reply_context.html`:
- `reply_card(url, ctx)` on the note page: "↩ In reply to", then a card with
the avatar, name (linked to the author) and handle, the title if there is
one, the content (sanitized HTML), the image with its alt text, and a footer
linking to the original with its date and site. Without a fetched context
it is the bare link, as before.
- `reply_line(url, ctx)` on the home, tag and likes pages: "↩ In reply to
<author>" (or the title, "by <author>"), then up to 140 characters of the
post as a quote. Without a context it is the raw URL, as before.
`attach_contexts` now gives replies their context as well as likes and
reposts, and the note page uses that when webmentions are disabled.
Microformats are unchanged for consumers: the card is still the note's
`u-in-reply-to h-cite`. Its author h-card and `e-content` are nested inside
the h-cite, so they don't become the note's properties. Post Type Discovery
still reads the note as a reply (`tests/test_reply_context_sources.py`).
## Tests
`tests/test_reply_context_sources.py`, through the mocked network: the
Mastodon API, content warnings, resolving through the instance, the
OpenGraph fallback for fediverse and other pages, Bluesky, the once-only
refresh of old rows (including keeping data when the refresh fails), and the
note and home pages (content, local image and avatar, mf2 reply).