neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Link previews: OpenGraph/Twitter cards built as an anonymous visitor
83838e8 · gmorell · 2026-12-03 04:13
Message
{commit_body(@commit)}
Files changed
modified
README.md
+61
−1
| 257 | 257 | | `REGISTRATION_TOKEN_DURATION_DAYS` | `7` | Default invite validity | |
| 258 | 258 | | `AUTO_MIGRATE` | `true` | Run migrations on boot | |
| 259 | 259 | | `REGISTRATION_ENABLED` | `false` | Allow `/register` without an invite link (token still required) | |
| 260 | −| `PUBLIC_URL` | `http://localhost:<BIND port>` | Public origin: base of email-verification links, the default OIDC redirect URI and the API-token curl example. `https://` also marks the session cookie `Secure` | | |
| 260 | +| `PUBLIC_URL` | `http://localhost:<BIND port>` | Public origin: base of email-verification links, the default OIDC redirect URI, the API-token curl example and link-preview URLs. `https://` also marks the session cookie `Secure` | | |
| 261 | 261 | | `MIN_TRUST_TO_CREATE_TAG` | `3` | Trust level needed to mint new tags | |
| 262 | 262 | | `MIN_TRUST_TO_FLAG` | `1` | Trust level needed to flag a post (0–4) | |
| 263 | 263 | | `FLAG_HIDE_RATIO` | `0.1` | Auto-hide a post when distinct counted flaggers ÷ readers of it reaches this (0–1]; see Moderation | |
| 343 | 343 | `src/handlers/focus.rs` (modelled on pin_flip's focus mode and shared pages, |
| 344 | 344 | and cartosnap's shared-trip stepping). |
| 345 | 345 | |
| 346 | +## Link previews | |
| 347 | + | |
| 348 | +Paste a URL into Slack, Discord, iMessage or <https://www.linkpreview.app> and | |
| 349 | +the unfurler fetches it once and reads `<head>`. It doesn't run JavaScript and | |
| 350 | +doesn't follow hx-boost, so everything it needs is in the first response. | |
| 351 | +`root.html` emits OpenGraph and Twitter-card tags (plus `<link rel=canonical>` | |
| 352 | +and `<meta name=description>`) from a `meta` value built by `src/meta.rs`. | |
| 353 | + | |
| 354 | +| Route | og:title | og:description | og:image | | |
| 355 | +|-------|----------|----------------|----------| | |
| 356 | +| `/t/<id>` (Classic and Focus) | the topic title | the opening post as plain text, cut at a word near 200 chars with `…` | the post's first image if it's on our host, else the site mark | | |
| 357 | +| `/c/<slug>` | the category name | its description, else `12 topics: A, B, C and 9 more.` | site mark | | |
| 358 | +| `/` | the site name | `<site>: a discussion forum. Latest: A, B and C.` | site mark | | |
| 359 | +| `/c/` | `Categories on <site>` | `4 categories: A, B, C and D.` | site mark | | |
| 360 | +| `/p/latest`, `/p/popular` | `Latest on <site>` / `Popular on <site>` | the newest / most active titles | site mark | | |
| 361 | +| `/tags`, `/tag/<slug>` | `Tags on <site>` / `#<tag>` | the busiest tags / `3 topics tagged #x: A, B and C.` | site mark | | |
| 362 | + | |
| 363 | +A topic page is `og:type` `article`, with `article:published_time`, | |
| 364 | +`article:modified_time`, `article:section` (the category), an `article:tag` | |
| 365 | +per tag, and `<meta name=author>`. A Focus list opened at a topic | |
| 366 | +(`?view=focus&topic=<id>`, on `/`, the feeds, a category or a tag) carries that | |
| 367 | +topic's card, since that's the URL people copy. `/p/<id>` 303s to its topic, | |
| 368 | +and the card is the topic's: `og:url` is the canonical `/t/<id>` with no | |
| 369 | +`#post` anchor (a fragment never reaches the server, and the card describes the | |
| 370 | +topic anyway). | |
| 371 | + | |
| 372 | +Details worth knowing: | |
| 373 | + | |
| 374 | +- **Built as anonymous, always.** The unfurler has no session, so the card is | |
| 375 | + computed from what an anonymous visitor may read, even when the request is | |
| 376 | + signed in, using the same `Viewer::anonymous()` / `Visible` rules as the | |
| 377 | + pages. A topic in a members-only, trust-gated or staff-only category, a held | |
| 378 | + or deleted topic, or a hidden opening post gets a generic card ("A | |
| 379 | + members-only discussion on <site>", no title or excerpt, `noindex`), so a | |
| 380 | + member who copies a link can't leak it through the preview. A moderated | |
| 381 | + opening post's excerpt is the staff note that replaced it; the original | |
| 382 | + lives only in the revision history and is never read here. List cards only | |
| 383 | + name topics and categories that anonymous visitors can see. (A signed-in | |
| 384 | + reader's `<title>` still names the topic: that's their browser tab, and a | |
| 385 | + scraper never carries their session.) | |
| 386 | +- **Absolute URLs come from `PUBLIC_URL`** (default `http://localhost:<port>`). | |
| 387 | + Unlike movie_monster, `Host` / `X-Forwarded-Host` are never read: they're | |
| 388 | + client-controlled, and recourse trusts no header it doesn't have to. Set | |
| 389 | + `PUBLIC_URL` in production or the cards point at localhost. | |
| 390 | +- **Images.** Only images on our own origin go in a card. A third-party image | |
| 391 | + in the opening post isn't hotlinked: we never vetted it, its host can swap | |
| 392 | + what it serves after the fact, and every unfurl would tell that host who's | |
| 393 | + looking. Everything else gets the site mark, `assets/f/img/site-mark.png`: | |
| 394 | + 512×512, composited from the Android launcher icon's layers (so it declares | |
| 395 | + `og:image:width`/`height`). The card is `twitter:card: summary` because | |
| 396 | + nothing we have is a known wide image; `summary_large_image` would letterbox | |
| 397 | + the square mark. | |
| 398 | +- **Indexing is opt-in.** A page that builds no card (admin, auth, profile, | |
| 399 | + devices, invites, moderation, bookmarks, notifications, reactions, search | |
| 400 | + results) gets the site name, a generic description and `noindex, nofollow`. | |
| 401 | +- **Attribute escaping.** The `<head>` tags go through the `attr` filter | |
| 402 | + instead of minijinja's default autoescape, which turns every `/` in a URL | |
| 403 | + into `/`: legal HTML, but plenty of unfurlers hand it back verbatim. | |
| 404 | + `& < > "` are still escaped, so a title can't break out of its attribute. | |
| 405 | + | |
| 346 | 406 | ## Android app and API |
| 347 | 407 | |
| 348 | 408 | `android/` is the phone client (Kotlin + Compose): browse in Classic or Focus |
modified
?
+0
−0
Binary file — not rendered.
modified
src/handlers/focus.rs
+8
−0
| 581 | 581 | pub search_q: Option<String>, |
| 582 | 582 | pub mark_all_read: bool, |
| 583 | 583 | pub others: Vec<minijinja::Value>, |
| 584 | + /// The link-preview card (public pages only; see `crate::meta`). | |
| 585 | + pub meta: Option<crate::meta::PageMeta>, | |
| 584 | 586 | } |
| 585 | 587 | |
| 586 | 588 | impl ListPage { |
| 600 | 602 | search_q: None, |
| 601 | 603 | mark_all_read: false, |
| 602 | 604 | others: vec![], |
| 605 | + meta: None, | |
| 603 | 606 | } |
| 604 | 607 | } |
| 605 | 608 |
| 643 | 646 | if let Some(q) = self.search_q { |
| 644 | 647 | ctx = context! { search_q => q, ..ctx }; |
| 645 | 648 | } |
| 649 | + if let Some(m) = self.meta { | |
| 650 | + ctx = context! { meta => m, ..ctx }; | |
| 651 | + } | |
| 646 | 652 | super::render(state, "forum/focus_list.html", ctx) |
| 647 | 653 | } |
| 648 | 654 | } |
| 800 | 806 | state, |
| 801 | 807 | "forum/focus.html", |
| 802 | 808 | context! { |
| 809 | + meta => crate::meta::with_reader(state, tree, open_topic, crate::meta::category_page(state, tree, category).await).await, | |
| 803 | 810 | title => category.name, |
| 804 | 811 | app_name => state.config.app_name, |
| 805 | 812 | user => user, |
| 912 | 919 | state, |
| 913 | 920 | "forum/home_visual.html", |
| 914 | 921 | context! { |
| 922 | + meta => crate::meta::categories_index(state, tree), | |
| 915 | 923 | title => "Home", |
| 916 | 924 | app_name => state.config.app_name, |
| 917 | 925 | user => uc.map(|c| c.user.clone()), |
modified
src/handlers/forum.rs
+7
−0
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | let page = render(&state, "forum/home.html", context! { |
| 547 | + meta => crate::meta::categories_index(&state, &tree), | |
| 547 | 548 | title => "Home", |
| 548 | 549 | app_name => state.config.app_name, |
| 549 | 550 | user => user_context.map(|c| c.user), |
| 662 | 663 | |
| 663 | 664 | let (can_create_topic, create_block) = |
| 664 | 665 | crate::actions::create_caps(&state, user_context.as_ref().map(|c| &c.user), &category); |
| 666 | + let meta = crate::meta::category_page(&state, &tree, &category).await; | |
| 665 | 667 | let page = render(&state, "forum/category.html", context! { |
| 668 | + meta => meta, | |
| 666 | 669 | can_create_topic => can_create_topic, |
| 667 | 670 | create_block => create_block, |
| 668 | 671 | rules => posting_badges(&category), |
| 745 | 748 | app_name => state.config.app_name, |
| 746 | 749 | user => user_context.map(|c| c.user), |
| 747 | 750 | category => category, |
| 751 | + meta => crate::meta::topic(&state, &tree, &topic, vec![]).await, | |
| 748 | 752 | })?; |
| 749 | 753 | return Ok(view.respond(page)); |
| 750 | 754 | } |
| 803 | 807 | crate::trust::can_create_tags(&c.user, state.config.min_trust_to_create_tag) |
| 804 | 808 | }); |
| 805 | 809 | |
| 810 | + // The link-preview card, as an anonymous visitor would see the topic. | |
| 811 | + let meta = crate::meta::topic(&state, &tree, &topic, topic_tag_chips.iter().map(|t| t.name.clone()).collect()).await; | |
| 806 | 812 | let ctx = context! { |
| 813 | + meta => meta, | |
| 807 | 814 | title => topic.title, |
| 808 | 815 | app_name => state.config.app_name, |
| 809 | 816 | user => user_context.as_ref().map(|c| c.user.clone()), |
modified
src/handlers/landing.rs
+5
−0
| 299 | 299 | page.empty = "Nothing here yet.".into(); |
| 300 | 300 | page.rail = Some(rail); |
| 301 | 301 | page.rail_label = "Feeds and categories"; |
| 302 | + let card = crate::meta::feed(&state, &tree, feed == Feed::Popular).await; | |
| 303 | + page.meta = Some(crate::meta::with_reader(&state, &tree, q.topic, card).await); | |
| 302 | 304 | let html = page.render(&state, tiles, reader, uc, &headers)?; |
| 303 | 305 | return Ok(view.respond(html)); |
| 304 | 306 | } |
| 307 | 309 | &state, |
| 308 | 310 | "forum/feed.html", |
| 309 | 311 | context! { |
| 312 | + meta => crate::meta::feed(&state, &tree, feed == Feed::Popular).await, | |
| 310 | 313 | title => feed.heading(), |
| 311 | 314 | app_name => state.config.app_name, |
| 312 | 315 | user => uc.map(|c| c.user.clone()), |
| 389 | 392 | &state, |
| 390 | 393 | "forum/landing_focus.html", |
| 391 | 394 | context! { |
| 395 | + meta => crate::meta::with_reader(&state, &tree, q.topic, crate::meta::home(&state, &tree).await).await, | |
| 392 | 396 | title => "Home", |
| 393 | 397 | app_name => state.config.app_name, |
| 394 | 398 | user => user, |
| 409 | 413 | &state, |
| 410 | 414 | "forum/landing.html", |
| 411 | 415 | context! { |
| 416 | + meta => crate::meta::home(&state, &tree).await, | |
| 412 | 417 | title => "Home", |
| 413 | 418 | app_name => state.config.app_name, |
| 414 | 419 | user => user, |
modified
src/handlers/tags.rs
+12
−0
Click to load diff…
modified
src/lib.rs
+1
−0
Click to load diff…
added
src/meta.rs
+485
−0
Click to load diff…
modified
src/web.rs
+30
−0
Click to load diff…
modified
templates/root.html
+33
−0
Click to load diff…
modified
tests/moderation_db.rs
+229
−0
Click to load diff…
Parents: 4ed675a