diff --git a/crates/forge-web/src/internal.rs b/crates/forge-web/src/internal.rs index 81aa87a..9f54556 100644 --- a/crates/forge-web/src/internal.rs +++ b/crates/forge-web/src/internal.rs @@ -164,6 +164,7 @@ async fn hook_endpoint( // A wiki push changes neither the code repo's refs nor its CI, pull requests or events. // It still goes to the standby. record_wiki_push(&state.store, &req).await; + crate::pages::wiki_live::bump_wiki(state, &req.repo_id); crate::replication::enqueue_repo(state, &req.repo_id); HookResponse::allow() } diff --git a/crates/forge-web/src/live.rs b/crates/forge-web/src/live.rs index 85fbfc4..5cb06c9 100644 --- a/crates/forge-web/src/live.rs +++ b/crates/forge-web/src/live.rs @@ -70,6 +70,14 @@ pub enum Topic { /// One discussion: its body, comments, reactions, answer and lock state. Keyed by discussion /// id. Bumped by [`crate::pages::discussion_live`]. Discussion(String), + /// A repo's wiki: a page was saved, or a push changed the wiki's main branch. Keyed by repo id. + /// Bumped by [`crate::pages::wiki_live`] and by the wiki push hook. + Wiki(String), + /// The snippet list: a snippet was created, changed or deleted. Global; each viewer's list is + /// fingerprinted, so a bump that changes nothing the viewer sees sends nothing. + Snippets, + /// One snippet: its files, title, visibility and revisions. Keyed by snippet id. + Snippet(String), } /// The open topics, each with the counter its subscribers wait on. diff --git a/crates/forge-web/src/pages/mod.rs b/crates/forge-web/src/pages/mod.rs index 4e40040..2f1872e 100644 --- a/crates/forge-web/src/pages/mod.rs +++ b/crates/forge-web/src/pages/mod.rs @@ -125,7 +125,9 @@ pub mod settings_tokens; pub mod setup; pub mod snippet_raw; pub mod snippets; +pub mod snippets_live; pub mod star; pub mod theme_cookie; pub mod tree; pub mod wiki; +pub mod wiki_live; diff --git a/crates/forge-web/src/pages/snippets.rs b/crates/forge-web/src/pages/snippets.rs index 679c2da..4e008e0 100644 --- a/crates/forge-web/src/pages/snippets.rs +++ b/crates/forge-web/src/pages/snippets.rs @@ -23,6 +23,7 @@ use crate::auth::{current_user, require_csrf, require_user}; use crate::i18n::community::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::snippet_raw::snippet_raw_url; +use crate::pages::snippets_live::{bump_snippet, item_live_url, list_live_url}; use crate::render::highlight::highlight_lines; use crate::render::markdown::render_markdown; use crate::state::AppState; @@ -244,7 +245,11 @@ fn short_time(stamp: &str) -> String { } /// The snippet if the viewer may read it, else 404. -async fn readable(cx: &Cx, viewer_id: Option<&str>, snippet_id: &str) -> Result { +pub(crate) async fn readable( + cx: &Cx, + viewer_id: Option<&str>, + snippet_id: &str, +) -> Result { let store = &app_context::(cx).store; match store.get_snippet(snippet_id).await? { Some(snippet) if snippet.is_visible_to(viewer_id) => Ok(snippet), @@ -262,9 +267,14 @@ async fn owned(cx: &Cx, user: &User, snippet_id: &str) -> Result { } #[component] -async fn snippet_list_body(lang: Lang, rows: Vec, signed_in: bool) -> Result { +async fn snippet_list_body( + lang: Lang, + rows: Vec, + signed_in: bool, + live: Option, +) -> Result { Ok(view! { -
+

(g::t(lang, K::Snippets))

if signed_in { (g::t(lang, K::NewSnippet)) @@ -445,13 +455,14 @@ async fn snippet_body( updated: &str, files: Vec, owner_csrf: Option, + live: Option, ) -> Result { let is_owner = owner_csrf.is_some(); let edit_href = format!("/snippets/{snippet_id}/edit"); let history_href = format!("/snippets/{snippet_id}/revisions"); let delete_action = format!("/snippets/{snippet_id}/delete"); Ok(view! { -
+

(title)

(owner) " · " (visibility) " · " (g::updated_on(lang, updated))
@@ -497,11 +508,12 @@ async fn revisions_body( snippet_id: &str, title: &str, revisions: Vec, + live: Option, ) -> Result { let back = format!("/snippets/{snippet_id}"); let heading = g::revisions_of(lang, title); Ok(view! { -
+

(heading.as_str())

(g::t(lang, K::BackToSnippet))
@@ -573,7 +585,7 @@ async fn snippets_list(cx: &Cx) -> Result { let title = g::t(lang, K::Snippets); Ok(view! { shell(title: title, viewer: viewer.as_ref(), - snippet_list_body(lang: lang, rows: rows, signed_in: signed_in) + snippet_list_body(lang: lang, rows: rows, signed_in: signed_in, live: Some(list_live_url().to_owned())) ) }) } @@ -632,6 +644,7 @@ async fn snippet_create(cx: &Cx, Form(form): Form>) -> R serde_json::Value::Null, ) .await; + bump_snippet(app_context::(cx), &snippet.id); return Err(see_other(format!("/snippets/{}", snippet.id)).into()); } Err(message) => error = Some(message), @@ -669,7 +682,7 @@ async fn snippet_view(cx: &Cx) -> Result { snippet_body(lang: lang, snippet_id: snippet.id.as_str(), title: snippet.title.as_str(), owner: snippet.owner_name.as_str(), description: snippet.description.as_str(), visibility: visibility, updated: updated.as_str(), files: files, - owner_csrf: owner_csrf) + owner_csrf: owner_csrf, live: Some(item_live_url(snippet.id.as_str()))) ) }) } @@ -735,6 +748,7 @@ async fn snippet_update(cx: &Cx, Form(form): Form>) -> R serde_json::Value::Null, ) .await; + bump_snippet(app_context::(cx), &snippet.id); return Err(see_other(format!("/snippets/{}", snippet.id)).into()); } Err(message) => error = Some(message), @@ -778,7 +792,7 @@ async fn snippet_revisions(cx: &Cx) -> Result { Ok(view! { shell(title: snippet.title.as_str(), viewer: viewer.as_ref(), revisions_body(lang: lang, snippet_id: snippet.id.as_str(), title: snippet.title.as_str(), - revisions: rows) + revisions: rows, live: Some(item_live_url(snippet.id.as_str()))) ) }) } @@ -803,6 +817,7 @@ async fn snippet_delete(cx: &Cx, Form(form): Form>) -> R serde_json::Value::Null, ) .await; + bump_snippet(app_context::(cx), &snippet.id); } Ok(see_other("/snippets")) } diff --git a/crates/forge-web/src/pages/snippets_live.rs b/crates/forge-web/src/pages/snippets_live.rs new file mode 100644 index 0000000..1a29b13 --- /dev/null +++ b/crates/forge-web/src/pages/snippets_live.rs @@ -0,0 +1,125 @@ +//! Live snippets. The snippet list (`/snippets`) follows [`Topic::Snippets`] at `/snippets/events`, +//! and a snippet page and its revisions follow [`Topic::Snippet`] at `/snippets/{id}/events`. Both +//! fingerprint what the viewer sees, so a bump that changes nothing visible sends nothing. +//! +//! Every write bumps after the store call succeeded, through [`bump_snippet`], from the create, +//! update and delete routes of `snippets.rs`. The new and edit forms carry no `data-live`: a form +//! is never redrawn while someone types in it. +//! +//! Auth is the page's own: the list is the viewer's list, and a snippet stream answers the same +//! 404 as the page to a viewer who cannot read the snippet (a private one, unless it is theirs). + +use forge_core::store::Store; +use forge_core::store::snippets::{Snippet, SnippetSummary}; +use topcoat::Result; +use topcoat::context::{Cx, app_context}; +use topcoat::router::response::Response; +use topcoat::router::{path_param, route}; + +use crate::auth::current_user; +use crate::live::{Topic, stream_changes}; +use crate::pages::snippets::readable; +use crate::state::AppState; + +path_param!(id); + +/// The stream URL of the snippet list. +pub fn list_live_url() -> &'static str { + "/snippets/events" +} + +/// The stream URL of one snippet, for its page and its revisions. +pub fn item_live_url(snippet_id: &str) -> String { + format!("/snippets/{snippet_id}/events") +} + +/// A snippet was created, changed or deleted: the list and the snippet's own pages follow it. +pub fn bump_snippet(state: &AppState, snippet_id: &str) { + state.live.bump(&Topic::Snippets); + state.live.bump(&Topic::Snippet(snippet_id.to_owned())); +} + +/// What the list shows the viewer, as one string. +async fn list_fingerprint(store: &Store, viewer_id: Option<&str>) -> Result { + let summaries: Vec = store.list_snippets(viewer_id).await?; + Ok(summaries + .iter() + .map(|s| { + format!( + "{} {} {} {} {} {}", + s.id, + s.title, + s.owner_name, + s.visibility.as_str(), + s.updated_at, + s.file_count + ) + }) + .collect::>() + .join("\n")) +} + +/// What one snippet page shows the viewer, as one string. `gone` when the viewer cannot read it. +fn snippet_fingerprint(viewer_id: Option<&str>, snippet: Option) -> String { + match snippet { + Some(s) if s.is_visible_to(viewer_id) => { + let files: Vec = s + .files + .iter() + .map(|f| format!("{}\n{}", f.name, f.content)) + .collect(); + format!( + "{}\n{}\n{}\n{}\n{}\n{}", + s.title, + s.description, + s.owner_name, + s.visibility.as_str(), + s.updated_at, + files.join("\n") + ) + } + _ => "gone".to_owned(), + } +} + +/// The change stream of the snippet list. Auth is the list's: the viewer's own visible snippets. +#[route(GET "/snippets/events")] +async fn snippets_events(cx: &Cx) -> Result { + let viewer = current_user(cx).await?; + let viewer_id = viewer.as_ref().map(|(user, _)| user.id.clone()); + let state = app_context::(cx); + let store = state.store.clone(); + stream_changes(cx, &state.live, &[Topic::Snippets], move || { + let store = store.clone(); + let viewer_id = viewer_id.clone(); + async move { list_fingerprint(&store, viewer_id.as_deref()).await } + }) + .await +} + +/// The change stream of one snippet, its page and its revisions. Auth is the page's: the snippet +/// must be readable by the viewer. +#[route(GET "/snippets/{id}/events")] +async fn snippet_events(cx: &Cx) -> Result { + let snippet_id = path_param::(cx).to_owned(); + let viewer = current_user(cx).await?; + let viewer_id = viewer.as_ref().map(|(user, _)| user.id.clone()); + readable(cx, viewer_id.as_deref(), &snippet_id).await?; + let state = app_context::(cx); + let store = state.store.clone(); + stream_changes( + cx, + &state.live, + &[Topic::Snippet(snippet_id.clone())], + move || { + let store = store.clone(); + let viewer_id = viewer_id.clone(); + let snippet_id = snippet_id.clone(); + async move { + let snippet = store.get_snippet(&snippet_id).await?; + Ok(snippet_fingerprint(viewer_id.as_deref(), snippet)) + } + }, + ) + .await +} diff --git a/crates/forge-web/src/pages/wiki.rs b/crates/forge-web/src/pages/wiki.rs index 800603b..c4740b5 100644 --- a/crates/forge-web/src/pages/wiki.rs +++ b/crates/forge-web/src/pages/wiki.rs @@ -25,6 +25,7 @@ use crate::i18n::community::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::repo::repo_head; use crate::pages::repo_ctx::{RepoCtx, load_repo}; +use crate::pages::wiki_live::{bump_wiki, page_live_url, wiki_live_url}; use crate::render::markdown::render_markdown; use crate::state::AppState; use crate::views::{alert, repo_href, shell}; @@ -62,12 +63,12 @@ where } /// The wiki directory of the repo, whether or not it exists yet. -fn wiki_dir(state: &AppState, ctx: &RepoCtx) -> Result { +pub(crate) fn wiki_dir(state: &AppState, ctx: &RepoCtx) -> Result { Ok(wiki_path(&state.config.data_dir, &ctx.repo.id).map_err(forge_core::Error::from)?) } /// 404 unless the repo's wiki is on. The header tab is hidden in the same case. -async fn ensure_enabled(state: &AppState, ctx: &RepoCtx) -> Result<()> { +pub(crate) async fn ensure_enabled(state: &AppState, ctx: &RepoCtx) -> Result<()> { if state.store.wiki_enabled(&ctx.repo.id).await? { Ok(()) } else { @@ -155,12 +156,13 @@ async fn wiki_head( can_write: bool, title: String, extra: Vec<(String, String)>, + live: Option, ) -> Result { let home = repo_href(&owner, &repo, "wiki", ""); let pages = repo_href(&owner, &repo, "wiki", "-/pages"); let new = repo_href(&owner, &repo, "wiki", "-/new"); Ok(view! { -
+

(title.as_str())