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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user