Forum: the link module, with YouTube previews

- services/forum/src/links: recognises the links in a post and shows a
  preview for the ones a provider knows; for now YouTube (watch, shorts,
  live, embed and youtu.be links, with the start time). Title and channel
  come from YouTube's oEmbed answer, the thumbnail is cut to 16:9 and kept
  in our own storage, so reading a thread loads nothing from YouTube. Made
  when a post is written or edited; without it the link stays text.
- The card: on a computer a click plays the video in place
  (youtube-nocookie.com); on a phone it opens the YouTube app (Android:
  an intent with the web page as fallback; iOS: the link the app takes
  over), and "Lejátszás itt" plays it in place.
- The privacy notice (HU, EN) says what YouTube sees when one plays it.
- "Pörgős most" is now "Élénk beszélgetések"; the new thread form lost its
  note about text and emoji.
- docs: the Turnstile captcha is on the list before going live.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-26 16:26:37 +00:00
co-authored by Claude Opus 5.5
parent 493539bf56
commit 1d4f18b5d6
21 changed files with 653 additions and 26 deletions
+52 -1
View File
@@ -30,12 +30,18 @@ struct User {
impl TestApp {
fn new(pool: PgPool) -> Self {
Self::with_links(pool, crate::links::LinkConfig::new("http://127.0.0.1:9/oembed", "http://127.0.0.1:9"))
}
/// With the link providers at these (fake) addresses.
fn with_links(pool: PgPool, links: crate::links::LinkConfig) -> Self {
let state = AppState {
db: pool.clone(),
flood_secs: 0,
base_url: "https://sorsfonalak.test".into(),
hub: crate::live::Hub::default(),
storage: None,
links,
};
Self { router: routes::router(Arc::new(state)), pool }
}
@@ -272,7 +278,7 @@ async fn threads_replies_and_what_is_new(pool: PgPool) {
assert_eq!(followed["threads"].as_array().unwrap().len(), 2);
assert_eq!(followed["threads"][0]["reply_count"], 3);
// Busy today: three replies make it "pörgős".
// Busy today: three replies make it lively ("Élénk beszélgetések").
assert_eq!(app.get("/v1/forum/home", Some(&bea)).await["hot"][0]["id"], heart);
// Editing one's own post only.
@@ -869,6 +875,7 @@ async fn live_doorbell_over_websocket(pool: PgPool) {
base_url: "https://sorsfonalak.test".into(),
hub: crate::live::Hub::default(),
storage: None,
links: crate::links::LinkConfig::new("http://127.0.0.1:9/oembed", "http://127.0.0.1:9"),
});
tokio::spawn(crate::live::listen(pool.clone(), state.hub.clone()));
let router = routes::router(state.clone());
@@ -1350,3 +1357,47 @@ async fn the_first_fifteen_are_founders(pool: PgPool) {
let shown = app.get(&format!("/v1/forum/threads/{thread}"), None).await;
assert_eq!(shown["posts"][2]["author"]["founder"], true);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn youtube_links_get_a_preview(pool: PgPool) {
// A fake YouTube: the oEmbed answer for one video, 404 for the rest.
let fake = Router::new().route(
"/oembed",
axum::routing::get(|q: axum::extract::Query<std::collections::HashMap<String, String>>| async move {
if q.get("url").is_some_and(|u| u.ends_with("v=dQw4w9WgXcQ")) {
Ok(axum::Json(json!({ "title": "Telihold meditáció", "author_name": "Csendes Csatorna" })))
} else {
Err(StatusCode::NOT_FOUND)
}
}),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move { axum::serve(listener, fake).await.unwrap() });
let app = TestApp::with_links(
pool,
crate::links::LinkConfig::new(&format!("http://{addr}/oembed"), &format!("http://{addr}")),
);
let anna = app.member("anna@example.com", "Csendesvíz", &["user"]).await;
let thread = app.thread(&anna, "belso-utak", "Esti meditáció").await;
let post = app
.reply(
&anna,
thread,
"Ezt hallgatom esténként: https://youtu.be/dQw4w9WgXcQ?t=95 – és ez nincs: https://youtu.be/aaaaaaaaaaa",
None,
)
.await;
assert_eq!(post["links"].as_array().unwrap().len(), 1, "{post}");
let link = &post["links"][0];
assert_eq!(link["provider"], "youtube");
assert_eq!(link["id"], "dQw4w9WgXcQ");
assert_eq!(link["start"], 95);
assert_eq!(link["title"], "Telihold meditáció");
assert_eq!(link["author"], "Csendes Csatorna");
assert!(link["thumbnail"].is_null(), "no storage in the tests");
// Shown again with the thread, from the stored preview; a deleted post shows none.
let shown = app.get(&format!("/v1/forum/threads/{thread}"), None).await;
assert_eq!(shown["posts"][1]["links"][0]["title"], "Telihold meditáció");
assert!(shown["posts"][0]["links"].is_null());
}