Forum: live notices over WebSocket

A doorbell, not the data: the forum service tells a member's page that
their menu counts, a thread or a conversation changed, and the page
fetches the news over HTTP. Events go through Postgres NOTIFY so every
instance hears them; pings keep the connection (and the online dot)
alive; only the site's own origin may connect. The menu now refreshes
its badges live, on every page change and when the page comes back to
the front, and polls while there is no connection. Caddy sends
/api/v1/forum/ws straight to the forum service; in development
FORUM_WS_URL points at it.

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 11:41:45 +00:00
co-authored by Claude Opus 5.5
parent 88d9a5df3e
commit 2cf7266fc1
18 changed files with 589 additions and 58 deletions
+9
View File
@@ -225,6 +225,11 @@ pub async fn request(
};
tx.commit().await?;
crate::mail::after_message(&state, id, me.user_id, true, Some(&intro)).await;
crate::live::emit(
&state.db,
crate::live::Event { conversation: Some(id), ..Default::default() }.for_users(vec![other]),
)
.await;
Ok((StatusCode::CREATED, Json(json!({ "id": id }))))
}
@@ -398,6 +403,8 @@ pub async fn respond(state: St, viewer: Viewer, path: IdPath, answer: &'static s
.await?;
}
}
let event = crate::live::Event { conversation: Some(id), ..Default::default() }.for_users(vec![c.other(me), me]);
crate::live::emit(&state.db, event).await;
Ok(StatusCode::NO_CONTENT)
}
@@ -463,6 +470,8 @@ pub async fn send(
mark(&mut *tx, id, me, message_id).await?;
tx.commit().await?;
crate::mail::after_message(&state, id, me, false, None).await;
let event = crate::live::Event { conversation: Some(id), ..Default::default() }.for_users(vec![c.other(me)]);
crate::live::emit(&state.db, event).await;
Ok((
StatusCode::CREATED,
Json(json!({ "id": message_id, "mine": true, "body": body, "created_at": rfc3339(created_at) })),
+1
View File
@@ -66,6 +66,7 @@ pub fn router(state: Arc<AppState>) -> Router {
.route("/v1/forum/nickname-check", get(members::nickname_check))
.route("/v1/forum/presence", post(members::presence))
.route("/v1/forum/unsubscribe", post(members::unsubscribe))
.route("/v1/forum/ws", get(crate::live::connect))
.route("/v1/forum/members/{nickname}", get(members::profile))
.route("/v1/forum/members/{nickname}/ban", post(moderation::ban).delete(moderation::unban))
.route("/v1/forum/mod/log", get(moderation::log))
+10
View File
@@ -18,6 +18,11 @@ use crate::error::{ApiError, ApiResult};
use crate::rank::HELPED_POINTS;
use crate::text;
/// Anyone viewing the thread refetches it (reactions, edits, removals).
async fn announce(state: &crate::AppState, thread: i64) {
crate::live::emit(&state.db, crate::live::Event { thread: Some(thread), ..Default::default() }).await;
}
/// A post and its thread, both readable by the viewer.
async fn load(db: &PgPool, viewer: &Viewer, id: i64) -> ApiResult<(PostRow, ThreadRow)> {
let post: PostRow = sqlx::query_as(&format!("SELECT {POST_COLUMNS} FROM forum.posts WHERE id = $1"))
@@ -55,6 +60,7 @@ pub async fn edit(
.bind(&body)
.fetch_one(&state.db)
.await?;
announce(&state, post.thread_id).await;
Ok(Json(posts_json(&state.db, &viewer, std::slice::from_ref(&post)).await?.remove(0)))
}
@@ -120,6 +126,7 @@ pub async fn remove(
}
crate::mail::notify_reporters(&state, &reporters, "removed").await;
}
announce(&state, post.thread_id).await;
Ok(StatusCode::NO_CONTENT)
}
@@ -224,6 +231,7 @@ pub async fn react(
.await?;
add_points(&mut tx, post.author_id, delta).await?;
tx.commit().await?;
announce(&state, post.thread_id).await;
Ok(Json(posts_json(&state.db, &viewer, std::slice::from_ref(&post)).await?.remove(0)))
}
@@ -241,6 +249,7 @@ pub async fn unreact(State(state): St, viewer: Viewer, WithRejection(Path(id), _
add_points(&mut tx, post.author_id, -points_of(&kind)).await?;
}
tx.commit().await?;
announce(&state, post.thread_id).await;
Ok(Json(posts_json(&state.db, &viewer, std::slice::from_ref(&post)).await?.remove(0)))
}
@@ -293,6 +302,7 @@ async fn set_helped(
add_points(&mut tx, post.author_id, if on { HELPED_POINTS } else { -HELPED_POINTS }).await?;
}
tx.commit().await?;
announce(&state, thread.id).await;
let post: PostRow = sqlx::query_as(&format!("SELECT {POST_COLUMNS} FROM forum.posts WHERE id = $1"))
.bind(id)
.fetch_one(&state.db)
+3
View File
@@ -97,6 +97,7 @@ pub async fn report_post(
.execute(&state.db)
.await?;
tracing::info!(post = id, kind = %req.kind, "post reported");
crate::live::emit(&state.db, crate::live::Event { moderators: true, ..Default::default() }).await;
Ok((StatusCode::CREATED, received(&req.kind)))
}
@@ -132,6 +133,7 @@ pub async fn report_message(
.execute(&state.db)
.await?;
tracing::info!(message = id, kind = %req.kind, "message reported");
crate::live::emit(&state.db, crate::live::Event { moderators: true, ..Default::default() }).await;
Ok((StatusCode::CREATED, received(&req.kind)))
}
@@ -322,6 +324,7 @@ pub async fn handle(
tx.commit().await?;
let outcome = if status == "resolved" { "handled" } else { "no_violation" };
crate::mail::notify_reporters(&state, &reporters, outcome).await;
crate::live::emit(&state.db, crate::live::Event { moderators: true, ..Default::default() }).await;
Ok(StatusCode::NO_CONTENT)
}
+3
View File
@@ -421,6 +421,9 @@ pub async fn reply(
after_post(&mut tx, member.user_id, id, post.id).await?;
tx.commit().await?;
crate::mail::after_reply(&state, id, post.id, member.user_id, post.parent_id, &body).await;
let followers = crate::live::followers(&state.db, id, member.user_id).await;
crate::live::emit(&state.db, crate::live::Event { thread: Some(id), ..Default::default() }.for_users(followers))
.await;
let post = posts_json(&state.db, &viewer, std::slice::from_ref(&post)).await?.remove(0);
Ok((StatusCode::CREATED, Json(post)))
}