//! Live stream of a repo's pages, at `/{owner}/{repo}/events`. The repo header renders `data-live` //! with this URL on every repo page (see `repo::repo_head`). A code view adds its branch to the //! URL. The stream sends `changed` when the header changes: stars, watchers, forks, or the repo's //! name, description or visibility. On a code view of a branch it also sends `changed` when the //! branch moves, or when a branch or tag is created or deleted (the ref selector and the branch //! list depend on that). Moves of other refs send nothing. Commits and tags are not followed: they //! do not move. use std::path::PathBuf; use forge_core::Visibility; use forge_git::browse::{RefInfo, RefKind}; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::response::Response; use topcoat::router::{path_param, query_params, route}; use crate::auth::percent_encode; use crate::live::{Topic, stream_changes}; use crate::pages::refs::load_refs; use crate::pages::repo_ctx::{fork_count, load_repo}; use crate::state::AppState; path_param!(owner); path_param!(repo); #[query_params(error = bad_request)] struct LiveQuery { ref_name: Option, } /// Whether a code view of `ref_name` follows the branch: the ref is a branch of the repo. pub fn follows_branch(refs: &[RefInfo], ref_name: &str) -> bool { refs.iter() .any(|r| r.kind == RefKind::Branch && r.name == ref_name) } /// The stream URL of a code view that follows `branch`, whether or not the branch exists yet. pub fn branch_live_url(owner: &str, repo: &str, branch: &str) -> String { format!("/{owner}/{repo}/events?ref_name={}", percent_encode(branch)) } /// The stream URL of a repo page. A code view of a branch adds the branch to it. pub fn live_url(owner: &str, repo: &str, refs: &[RefInfo], ref_name: &str) -> String { if follows_branch(refs, ref_name) { branch_live_url(owner, repo, ref_name) } else { format!("/{owner}/{repo}/events") } } /// The state the code view shows: every branch and tag name, and the commit `branch` points at. /// A move of another branch leaves it unchanged. async fn refs_fingerprint(path: PathBuf, branch: String) -> Result { let refs = load_refs(path).await?; let mut names: Vec = refs .iter() .map(|r| format!("{} {}", r.kind == RefKind::Branch, r.name)) .collect(); names.sort(); let at = refs .iter() .find(|r| r.kind == RefKind::Branch && r.name == branch) .map_or("", |r| r.target.as_str()); Ok(format!("{}\n{at}", names.join("\n"))) } /// The state the repo header shows. A repo that is gone reads as empty, which ends nothing: the /// stream then sends `changed` once and the page's own re-read shows the 404. async fn header_fingerprint(state: &AppState, repo_id: &str) -> Result { let Some(repo) = state.store.repo_by_id(repo_id).await? else { return Ok(String::new()); }; let stars = state.store.star_count(repo_id).await?; let watchers = state.store.watchers(repo_id).await?.len(); let forks = fork_count(state, repo_id).await?; let public = repo.visibility == Visibility::Public; Ok(format!( "{}\n{}\n{public}\n{}\n{stars} {watchers} {forks}", repo.owner_name, repo.name, repo.description )) } /// The repo's live stream. Auth is the repo read check of the page: a viewer who cannot read the /// repo gets the same 404 as the page. #[route(GET "/{owner}/{repo}/events")] async fn repo_events(cx: &Cx) -> Result { let owner = path_param::(cx); let name = path_param::(cx); let branch = query_params::(cx)? .ref_name .clone() .unwrap_or_default(); let ctx = load_repo(cx, owner, name).await?; let state = app_context::(cx).clone(); let mut topics = vec![Topic::Repo(ctx.repo.id.clone())]; if !branch.is_empty() { topics.push(Topic::RepoRefs(ctx.repo.id.clone())); } let path = ctx.path.clone(); let repo_id = ctx.repo.id.clone(); stream_changes(cx, &state.live.clone(), &topics, move || { let state = state.clone(); let repo_id = repo_id.clone(); let path = path.clone(); let branch = branch.clone(); async move { let header = header_fingerprint(&state, &repo_id).await?; if branch.is_empty() { return Ok(header); } let refs = refs_fingerprint(path, branch).await?; Ok(format!("{header}\n{refs}")) } }) .await }