forge
Explore
Log in

Don't you dare go Hollow

Repo settings, security, transfer and mirror pages update live

Browse files at this commit

ee30eab6b1dfbc92664cac1e26d88769fe1cb9e7Ahmet Kaan GümüşParent: 514beba
  • feat
    • repo settings, security, transfer and mirror pages update live
Show full message
⚡ feat: repo settings, security, transfer and mirror pages update live
crates/forge-web/src/advisories/scan.rsmodified+4 -0
@@ -10,6 +10,7 @@ use forge_core::store::advisories::ScanOutcome;
10 use forge_core::{Error, Repo, Result};10 use forge_core::{Error, Repo, Result};
11 use forge_git::browse::{EntryKind, default_branch_commit, read_text_blob, read_tree};11 use forge_git::browse::{EntryKind, default_branch_commit, read_text_blob, read_tree};
12 12
13+use crate::pages::repo_settings_live::bump_settings;
13 use crate::state::AppState;14 use crate::state::AppState;
14 15
15 /// Deepest directory searched for lockfiles. The root is depth 0.16 /// Deepest directory searched for lockfiles. The root is depth 0.
@@ -73,6 +74,9 @@ pub async fn scan_repo(state: &AppState, repo: &Repo) -> Result<ScanOutcome> {
73 .map_err(|e| Error::Git(format!("scan task failed: {e}")))?74 .map_err(|e| Error::Git(format!("scan task failed: {e}")))?
74 .map_err(Error::Git)?;75 .map_err(Error::Git)?;
75 let outcome = state.store.scan_repo(&repo.id, &deps).await?;76 let outcome = state.store.scan_repo(&repo.id, &deps).await?;
77+ if outcome.opened > 0 || outcome.fixed > 0 {
78+ bump_settings(&state.live, &repo.id);
79+ }
76 if outcome.opened > 0 {80 if outcome.opened > 0 {
77 tracing::info!(81 tracing::info!(
78 repo_id = %repo.id,82 repo_id = %repo.id,
crates/forge-web/src/boot.rsmodified+1 -0
@@ -172,6 +172,7 @@ pub async fn serve(
172 .layer(crate::pages::pull_live::PullWriteLayer)172 .layer(crate::pages::pull_live::PullWriteLayer)
173 .layer(crate::pages::discussion_live::DiscussionWriteLayer)173 .layer(crate::pages::discussion_live::DiscussionWriteLayer)
174 .layer(crate::pages::org_live::OrgWriteLayer)174 .layer(crate::pages::org_live::OrgWriteLayer)
175+ .layer(crate::pages::repo_settings_live::RepoSettingsWriteLayer)
175 .layer(crate::pages::not_found_page::NotFoundLayer)176 .layer(crate::pages::not_found_page::NotFoundLayer)
176 .layer(TowerLayer::new(crate::compression::ResponseCompression))177 .layer(TowerLayer::new(crate::compression::ResponseCompression))
177 .compression(Compression::off())178 .compression(Compression::off())
crates/forge-web/src/live.rsmodified+4 -0
@@ -107,6 +107,10 @@ pub enum Topic {
107 /// setting changed, or a sign-in or a key or token use happened. Keyed by user id. Streamed only107 /// setting changed, or a sign-in or a key or token use happened. Keyed by user id. Streamed only
108 /// to that user, never to the public profile viewers who follow [`Topic::User`].108 /// to that user, never to the public profile viewers who follow [`Topic::User`].
109 Account(String),109 Account(String),
110+ /// A repo's settings pages and the security and transfer pages: a setting, collaborator, rule,
111+ /// secret, webhook or delivery, mirror sync, security alert, or a pending transfer changed.
112+ /// Keyed by repo id. Bumped by the settings write layer and by the background writers.
113+ RepoSettings(String),
110 }114 }
111 115
112 /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the116 /// The hub of the admin pages. It is process-wide, not part of [`crate::state::AppState`]: the
crates/forge-web/src/mirrors/mod.rsmodified+3 -0
@@ -22,6 +22,7 @@ use reqwest::Url;
22 use serde_json::json;22 use serde_json::json;
23 23
24 use crate::audit::{self, Actor, Target};24 use crate::audit::{self, Actor, Target};
25+use crate::pages::repo_settings_live::bump_settings;
25 use crate::post_update::after_ref_update;26 use crate::post_update::after_ref_update;
26 use crate::state::AppState;27 use crate::state::AppState;
27 use crate::webhooks::client::{WebhookPolicy, is_forbidden_ip, validate_url};28 use crate::webhooks::client::{WebhookPolicy, is_forbidden_ip, validate_url};
@@ -280,6 +281,8 @@ pub async fn run_one(state: &AppState, mirror: &Mirror, policy: &WebhookPolicy)
280 {281 {
281 tracing::error!(error = %e, mirror_id = %mirror.id, "mirrors: could not record the sync result");282 tracing::error!(error = %e, mirror_id = %mirror.id, "mirrors: could not record the sync result");
282 }283 }
284+ // The mirror list and detail of the repo's settings show the sync state, so they wake now.
285+ bump_settings(&state.live, &mirror.repo_id);
283 if let Some(error) = &error {286 if let Some(error) = &error {
284 tracing::warn!(mirror_id = %mirror.id, error = %error, "mirrors: sync failed");287 tracing::warn!(mirror_id = %mirror.id, error = %error, "mirrors: sync failed");
285 if mirror.last_ok != Some(false) {288 if mirror.last_ok != Some(false) {
crates/forge-web/src/pages/mod.rsmodified+3 -0
@@ -110,6 +110,9 @@ pub mod repo_rename;
110 pub mod repo_secrets;110 pub mod repo_secrets;
111 pub mod repo_security;111 pub mod repo_security;
112 pub mod repo_settings;112 pub mod repo_settings;
113+pub mod repo_settings_live;
114+#[cfg(test)]
115+mod repo_settings_live_tests;
113 pub mod repo_transfer;116 pub mod repo_transfer;
114 pub mod repo_webhooks;117 pub mod repo_webhooks;
115 pub mod review_requests;118 pub mod review_requests;
crates/forge-web/src/pages/repo_collaborators.rsmodified+2 -1
@@ -28,6 +28,7 @@ use crate::i18n::{self, Lang};
28 use crate::pages::repo::repo_head;28 use crate::pages::repo::repo_head;
29 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};29 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
30 use crate::pages::repo_settings::settings_nav_for;30 use crate::pages::repo_settings::settings_nav_for;
31+use crate::pages::repo_settings_live::settings_head;
31 use crate::state::AppState;32 use crate::state::AppState;
32 use crate::views::{alert, avatar_url, display_name, shell};33 use crate::views::{alert, avatar_url, display_name, shell};
33 34
@@ -276,7 +277,7 @@ impl FrameData {
276 Ok(Self {277 Ok(Self {
277 page_title: g::collaborators_page_title(lang, &title),278 page_title: g::collaborators_page_title(lang, &title),
278 title,279 title,
279- head: ctx.head(state, "settings").await?,280+ head: settings_head(ctx, state).await?,
280 })281 })
281 }282 }
282 }283 }
crates/forge-web/src/pages/repo_mirrors.rsmodified+2 -1
@@ -33,6 +33,7 @@ use crate::pages::refs::load_refs;
33 use crate::pages::repo::repo_head;33 use crate::pages::repo::repo_head;
34 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};34 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
35 use crate::pages::repo_settings::settings_nav_for;35 use crate::pages::repo_settings::settings_nav_for;
36+use crate::pages::repo_settings_live::settings_head;
36 use crate::state::AppState;37 use crate::state::AppState;
37 use crate::views::{alert, enc, shell, user_facing_message};38 use crate::views::{alert, enc, shell, user_facing_message};
38 use crate::webhooks::client::{WebhookPolicy, validate_url};39 use crate::webhooks::client::{WebhookPolicy, validate_url};
@@ -285,7 +286,7 @@ impl Frame {
285 archived: ctx.repo.archived,286 archived: ctx.repo.archived,
286 tz: ctx.viewer_tz().to_owned(),287 tz: ctx.viewer_tz().to_owned(),
287 lang,288 lang,
288- head: ctx.head(state, "settings").await?,289+ head: settings_head(ctx, state).await?,
289 })290 })
290 }291 }
291 }292 }
crates/forge-web/src/pages/repo_rename.rsmodified+2 -1
@@ -23,6 +23,7 @@ use crate::i18n::{self, Lang};
23 use crate::pages::repo::repo_head;23 use crate::pages::repo::repo_head;
24 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};24 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
25 use crate::pages::repo_settings::settings_nav_for;25 use crate::pages::repo_settings::settings_nav_for;
26+use crate::pages::repo_settings_live::settings_head;
26 use crate::state::AppState;27 use crate::state::AppState;
27 use crate::views::{alert, shell};28 use crate::views::{alert, shell};
28 29
@@ -181,7 +182,7 @@ impl Frame {
181 page_title: g::page_title(lang, &title),182 page_title: g::page_title(lang, &title),
182 title,183 title,
183 can_delete: ctx.can_delete(),184 can_delete: ctx.can_delete(),
184- head: ctx.head(state, "settings").await?,185+ head: settings_head(ctx, state).await?,
185 })186 })
186 }187 }
187 }188 }
crates/forge-web/src/pages/repo_secrets.rsmodified+2 -1
@@ -22,6 +22,7 @@ use crate::pages::repo::repo_head;
22 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};22 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
23 use crate::pages::repo_mirrors::stamp_in_tz;23 use crate::pages::repo_mirrors::stamp_in_tz;
24 use crate::pages::repo_settings::settings_nav_for;24 use crate::pages::repo_settings::settings_nav_for;
25+use crate::pages::repo_settings_live::settings_head;
25 use crate::state::AppState;26 use crate::state::AppState;
26 use crate::views::{alert, enc, shell};27 use crate::views::{alert, enc, shell};
27 28
@@ -142,7 +143,7 @@ impl SecretsData {
142 page_title: g::secrets_page_title(lang, &title),143 page_title: g::secrets_page_title(lang, &title),
143 title,144 title,
144 archived: ctx.repo.archived,145 archived: ctx.repo.archived,
145- head: ctx.head(state, "settings").await?,146+ head: settings_head(ctx, state).await?,
146 rows,147 rows,
147 })148 })
148 }149 }
crates/forge-web/src/pages/repo_security.rsmodified+3 -1
@@ -22,6 +22,7 @@ use crate::i18n::{self, Key, Lang};
22 use crate::pages::repo::repo_head;22 use crate::pages::repo::repo_head;
23 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};23 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
24 use crate::pages::repo_mirrors::stamp_in_tz;24 use crate::pages::repo_mirrors::stamp_in_tz;
25+use crate::pages::repo_settings_live::security_live_url;
25 use crate::state::AppState;26 use crate::state::AppState;
26 use crate::views::shell;27 use crate::views::shell;
27 28
@@ -203,7 +204,8 @@ async fn security_page(cx: &Cx) -> Result<impl View> {
203 let lang = i18n::lang(cx, Some(&user));204 let lang = i18n::lang(cx, Some(&user));
204 let state = app_context::<AppState>(cx);205 let state = app_context::<AppState>(cx);
205 let title = title_of(&ctx);206 let title = title_of(&ctx);
206- let head = ctx.head(state, "security").await?;207+ let mut head = ctx.head(state, "security").await?;
208+ head.live = security_live_url(&ctx.repo.owner_name, &ctx.repo.name);
207 let dependencies = state.store.dependency_count(&ctx.repo.id).await?;209 let dependencies = state.store.dependency_count(&ctx.repo.id).await?;
208 let tz = ctx.viewer_tz().to_owned();210 let tz = ctx.viewer_tz().to_owned();
209 let rows: Vec<AlertRow> = state211 let rows: Vec<AlertRow> = state
crates/forge-web/src/pages/repo_settings.rsmodified+2 -1
@@ -37,6 +37,7 @@ use crate::pages::discussion_settings::discussions_card;
37 use crate::pages::refs::load_refs;37 use crate::pages::refs::load_refs;
38 use crate::pages::repo::repo_head;38 use crate::pages::repo::repo_head;
39 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};39 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
40+use crate::pages::repo_settings_live::settings_head;
40 use crate::state::AppState;41 use crate::state::AppState;
41 use crate::views::{alert, shell};42 use crate::views::{alert, shell};
42 43
@@ -730,7 +731,7 @@ impl SettingsFrame {
730 archived: ctx.repo.archived,731 archived: ctx.repo.archived,
731 title,732 title,
732 can_delete: ctx.can_delete(),733 can_delete: ctx.can_delete(),
733- head: ctx.head(state, "settings").await?,734+ head: settings_head(ctx, state).await?,
734 })735 })
735 }736 }
736 }737 }
crates/forge-web/src/pages/repo_settings_live.rsadded+149 -0
@@ -0,0 +1,149 @@
1+//! Live streams of the repo settings pages. The settings pages (`/{o}/{r}/settings/...`), the
2+//! security tab and the transfer page follow [`Topic::RepoSettings`] of the repo. The settings
3+//! pages' header follows the settings stream too, so they all carry the same `data-live` URL.
4+//!
5+//! Every write reaches the topic through one of these paths:
6+//! - A POST under `/{o}/{r}/settings/...` or `/{o}/{r}/security/...`: [`RepoSettingsWriteLayer`]
7+//! bumps after the handler ran, so no route calls a bump itself.
8+//! - A transfer accepted, declined or cancelled (`/-/transfers/...`), which has no repo in its
9+//! path: those routes bump it.
10+//! - A webhook delivery queued or sent, a mirror sync that finished, a security scan that opened or
11+//! fixed an alert: the background writers bump it next to their write.
12+//!
13+//! The streams send one `changed` per wake, and the page re-reads on the client (see `nav.js`).
14+//! Each stream takes the check of its page: the settings stream is for repo admins only, the
15+//! security and transfer streams for the signed-in viewers their pages admit.
16+
17+use topcoat::Result;
18+use topcoat::context::{Cx, app_context};
19+use topcoat::router::error::forbidden;
20+use topcoat::router::request::{method, uri};
21+use topcoat::router::response::Response;
22+use topcoat::router::{Body, Layer, LayerFuture, Method, Next, Path, path_param, route};
23+
24+use crate::auth::require_user;
25+use crate::live::{LiveHub, Topic, stream_wakes};
26+use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
27+use crate::state::AppState;
28+
29+path_param!(owner);
30+path_param!(repo);
31+
32+/// The stream URL of the settings pages of `owner/repo`.
33+pub fn settings_live_url(owner: &str, repo: &str) -> String {
34+ format!("/{owner}/{repo}/settings/live")
35+}
36+
37+/// The stream URL of the security tab of `owner/repo`.
38+pub fn security_live_url(owner: &str, repo: &str) -> String {
39+ format!("/{owner}/{repo}/security/live")
40+}
41+
42+/// The stream URL of the transfer page of `owner/repo`.
43+pub fn transfer_live_url(owner: &str, repo: &str) -> String {
44+ format!("/{owner}/{repo}/transfer/live")
45+}
46+
47+/// The header of a settings page. It follows the settings stream, which carries the refs too, since
48+/// the branch picker of the protection rules lists them.
49+pub async fn settings_head(ctx: &RepoCtx, state: &AppState) -> Result<RepoHead> {
50+ let mut head = ctx.head(state, "settings").await?;
51+ head.live = settings_live_url(&ctx.repo.owner_name, &ctx.repo.name);
52+ Ok(head)
53+}
54+
55+/// Bumps the settings topic of a repo, after a write that changes one of its settings pages.
56+pub fn bump_settings(hub: &LiveHub, repo_id: &str) {
57+ hub.bump(&Topic::RepoSettings(repo_id.to_owned()));
58+}
59+
60+/// The settings stream: the settings pages, and the refs their branch picker lists. Admins only,
61+/// as the settings page is.
62+#[route(GET "/{owner}/{repo}/settings/live")]
63+async fn settings_live(cx: &Cx) -> Result<Response> {
64+ let ctx = load_repo(cx, path_param::<Owner>(cx), path_param::<Repo>(cx)).await?;
65+ if !ctx.can_admin() {
66+ return Err(forbidden().into());
67+ }
68+ let state = app_context::<AppState>(cx);
69+ let topics = [
70+ Topic::RepoSettings(ctx.repo.id.clone()),
71+ Topic::RepoRefs(ctx.repo.id.clone()),
72+ ];
73+ stream_wakes(cx, &state.live, &topics)
74+}
75+
76+/// The security tab's stream. It takes the check of the security page: a signed-in viewer who may
77+/// read the repo.
78+#[route(GET "/{owner}/{repo}/security/live")]
79+async fn security_live(cx: &Cx) -> Result<Response> {
80+ let ctx = load_repo(cx, path_param::<Owner>(cx), path_param::<Repo>(cx)).await?;
81+ require_user(cx).await?;
82+ let state = app_context::<AppState>(cx);
83+ stream_wakes(cx, &state.live, &[Topic::RepoSettings(ctx.repo.id.clone())])
84+}
85+
86+/// The transfer page's stream. It takes the check of the transfer page: a signed-in viewer who may
87+/// read the repo.
88+#[route(GET "/{owner}/{repo}/transfer/live")]
89+async fn transfer_live(cx: &Cx) -> Result<Response> {
90+ let ctx = load_repo(cx, path_param::<Owner>(cx), path_param::<Repo>(cx)).await?;
91+ require_user(cx).await?;
92+ let state = app_context::<AppState>(cx);
93+ stream_wakes(cx, &state.live, &[Topic::RepoSettings(ctx.repo.id.clone())])
94+}
95+
96+// The write layer
97+
98+/// Wraps every request. Only a POST under the settings or security paths does anything. Registered
99+/// on the public router in `boot.rs`, and on the test router, next to the other request layers.
100+pub struct RepoSettingsWriteLayer;
101+
102+impl Layer for RepoSettingsWriteLayer {
103+ fn path(&self) -> Option<&Path> {
104+ None
105+ }
106+
107+ fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> {
108+ Box::pin(bump_after_write(cx, body, next))
109+ }
110+}
111+
112+/// The `(owner, name)` of a POST under `/{o}/{r}/settings/...` or `/{o}/{r}/security/...`.
113+fn written(cx: &Cx) -> Option<(String, String)> {
114+ if *method(cx) != Method::POST {
115+ return None;
116+ }
117+ let path = uri(cx).path().to_owned();
118+ let parts: Vec<&str> = path.trim_start_matches('/').split('/').collect();
119+ match parts.as_slice() {
120+ [owner, name, "settings" | "security", ..] => {
121+ Some(((*owner).to_owned(), (*name).to_owned()))
122+ }
123+ _ => None,
124+ }
125+}
126+
127+/// Bumps the settings topic once the handler has run. The repo is looked up before the handler,
128+/// because a rename moves its name away. The handler's result passes through unchanged, so a
129+/// refused write bumps too: the page re-reads and agrees.
130+async fn bump_after_write(cx: &Cx, body: Body, next: Next<'_>) -> Result<Response> {
131+ let repo_id = match written(cx) {
132+ Some((owner, name)) => {
133+ let state = app_context::<AppState>(cx);
134+ match state.store.repo_by_owner_and_name(&owner, &name).await {
135+ Ok(repo) => repo.map(|repo| repo.id),
136+ Err(e) => {
137+ tracing::error!(error = %e, owner, name, "live: loading the repo to bump failed");
138+ None
139+ }
140+ }
141+ }
142+ None => None,
143+ };
144+ let result = next.run(cx, body).await;
145+ if let Some(repo_id) = repo_id {
146+ bump_settings(&app_context::<AppState>(cx).live, &repo_id);
147+ }
148+ result
149+}
crates/forge-web/src/pages/repo_settings_live_tests.rsadded+224 -0
@@ -0,0 +1,224 @@
1+//! Router-level tests for the live streams of the repo settings, security and transfer pages: who
2+//! may open each stream, and that a settings write wakes the open settings stream.
3+
4+use std::sync::Arc;
5+
6+use forge_core::{Store, Visibility};
7+use futures_util::StreamExt;
8+use topcoat::router::request::Request;
9+use topcoat::router::{Body, Router};
10+
11+use crate::auth::SESSION_COOKIE;
12+use crate::live::{LiveHub, Topic};
13+
14+const CSRF: &str = "settings-live-test-csrf";
15+
16+struct Site {
17+ router: Router,
18+ store: Store,
19+ live: Arc<LiveHub>,
20+ _dir: tempfile::TempDir,
21+}
22+
23+async fn site() -> Site {
24+ let dir = tempfile::tempdir().expect("tempdir");
25+ let state = crate::test_support::test_state(dir.path()).await;
26+ let store = state.store.clone();
27+ let live = state.live.clone();
28+ let router = crate::test_support::app_router(state);
29+ Site {
30+ router,
31+ store,
32+ live,
33+ _dir: dir,
34+ }
35+}
36+
37+/// Signs `user_id` in and returns the session cookie header.
38+async fn sign_in(store: &Store, user_id: &str, token: &str) -> String {
39+ crate::test_support::create_session(store, user_id, token, CSRF).await;
40+ format!("__Host-{SESSION_COOKIE}={token}")
41+}
42+
43+/// Sends a GET with an optional cookie and returns the response, its body still unread.
44+async fn get(site: &Site, uri: &str, cookie: Option<&str>) -> topcoat::router::response::Response {
45+ let mut builder = Request::builder().method("GET").uri(uri);
46+ if let Some(cookie) = cookie {
47+ builder = builder.header("cookie", cookie);
48+ }
49+ site.router
50+ .handle(builder.body(Body::from(String::new())).expect("request"))
51+ .await
52+}
53+
54+async fn post_form(site: &Site, uri: &str, cookie: &str, form: &str) -> u16 {
55+ let request = Request::builder()
56+ .method("POST")
57+ .uri(uri)
58+ .header("cookie", cookie)
59+ .header("content-type", "application/x-www-form-urlencoded")
60+ .body(Body::from(form.to_owned()))
61+ .expect("request");
62+ site.router.handle(request).await.status().as_u16()
63+}
64+
65+/// An owner of the public repo `owner/proj`, and a plain reader who can read it.
66+async fn fixture(site: &Site) -> (String, String) {
67+ let owner = site
68+ .store
69+ .create_user("owner", None, "unused", false)
70+ .await
71+ .unwrap();
72+ site.store
73+ .create_repo(&owner.id, "proj", "", Visibility::Public, "main")
74+ .await
75+ .unwrap();
76+ let owner_cookie = sign_in(&site.store, &owner.id, "owner-token").await;
77+ let reader = site
78+ .store
79+ .create_user("reader", None, "unused", false)
80+ .await
81+ .unwrap();
82+ let reader_cookie = sign_in(&site.store, &reader.id, "reader-token").await;
83+ (owner_cookie, reader_cookie)
84+}
85+
86+#[tokio::test]
87+async fn the_settings_stream_is_for_admins_only() {
88+ let site = site().await;
89+ let (owner_cookie, reader_cookie) = fixture(&site).await;
90+
91+ let owner = get(&site, "/owner/proj/settings/live", Some(&owner_cookie)).await;
92+ assert_eq!(owner.status().as_u16(), 200);
93+ assert_eq!(
94+ owner.headers().get("content-type").unwrap(),
95+ "text/event-stream"
96+ );
97+
98+ let reader = get(&site, "/owner/proj/settings/live", Some(&reader_cookie)).await;
99+ assert_eq!(reader.status().as_u16(), 403);
100+
101+ let anonymous = get(&site, "/owner/proj/settings/live", None).await;
102+ assert_ne!(anonymous.status().as_u16(), 200);
103+}
104+
105+#[tokio::test]
106+async fn a_settings_write_wakes_the_open_settings_stream() {
107+ let site = site().await;
108+ let (owner_cookie, _) = fixture(&site).await;
109+
110+ let res = get(&site, "/owner/proj/settings/live", Some(&owner_cookie)).await;
111+ let mut frames = res.into_body().into_data_stream();
112+ // The stream opens with its retry field, and sends nothing until a change.
113+ let retry = frames.next().await.expect("a frame").expect("data");
114+ assert!(retry.starts_with(b"retry: 1000"), "{retry:?}");
115+
116+ // Archiving the repo is a settings write: the open stream sends one `changed`.
117+ let status = post_form(
118+ &site,
119+ "/owner/proj/settings/archive",
120+ &owner_cookie,
121+ &format!("csrf={CSRF}&archived=1"),
122+ )
123+ .await;
124+ assert_eq!(status, 303);
125+ let changed = frames.next().await.expect("a frame").expect("data");
126+ assert_eq!(&changed[..], b"event: changed\ndata: 1\n\n");
127+}
128+
129+#[tokio::test]
130+async fn the_security_stream_takes_the_security_page_check() {
131+ let site = site().await;
132+ let (_, reader_cookie) = fixture(&site).await;
133+
134+ let reader = get(&site, "/owner/proj/security/live", Some(&reader_cookie)).await;
135+ assert_eq!(reader.status().as_u16(), 200);
136+ let anonymous = get(&site, "/owner/proj/security/live", None).await;
137+ assert_ne!(anonymous.status().as_u16(), 200);
138+}
139+
140+#[tokio::test]
141+async fn the_transfer_stream_takes_the_transfer_page_check() {
142+ let site = site().await;
143+ let (_, reader_cookie) = fixture(&site).await;
144+
145+ let reader = get(&site, "/owner/proj/transfer/live", Some(&reader_cookie)).await;
146+ assert_eq!(reader.status().as_u16(), 200);
147+ let anonymous = get(&site, "/owner/proj/transfer/live", None).await;
148+ assert_ne!(anonymous.status().as_u16(), 200);
149+}
150+
151+#[tokio::test]
152+async fn a_visibility_change_wakes_the_repo_owner_explore_and_the_repo() {
153+ let site = site().await;
154+ let (owner_cookie, _) = fixture(&site).await;
155+ let repo = site
156+ .store
157+ .repo_by_owner_and_name("owner", "proj")
158+ .await
159+ .unwrap()
160+ .expect("repo");
161+ let repo_wake = site.live.subscribe(&Topic::Repo(repo.id.clone()));
162+ let owner_wake = site.live.subscribe(&Topic::User(repo.owner_id.clone()));
163+ let explore_wake = site.live.subscribe(&Topic::Explore);
164+ let settings_wake = site.live.subscribe(&Topic::RepoSettings(repo.id.clone()));
165+
166+ let status = post_form(
167+ &site,
168+ "/owner/proj/settings/visibility",
169+ &owner_cookie,
170+ &format!("csrf={CSRF}&visibility=private&understand=1"),
171+ )
172+ .await;
173+ assert_eq!(status, 303);
174+ assert!(repo_wake.has_changed().expect("open"));
175+ assert!(owner_wake.has_changed().expect("open"));
176+ assert!(explore_wake.has_changed().expect("open"));
177+ assert!(settings_wake.has_changed().expect("open"));
178+}
179+
180+#[tokio::test]
181+async fn accepting_a_transfer_wakes_both_owners_explore_and_the_repo() {
182+ let site = site().await;
183+ fixture(&site).await;
184+ let owner = site
185+ .store
186+ .user_by_username("owner")
187+ .await
188+ .unwrap()
189+ .expect("owner");
190+ let target = site
191+ .store
192+ .create_user("target", None, "unused", false)
193+ .await
194+ .unwrap();
195+ let target_cookie = sign_in(&site.store, &target.id, "target-token").await;
196+ let repo = site
197+ .store
198+ .repo_by_owner_and_name("owner", "proj")
199+ .await
200+ .unwrap()
201+ .expect("repo");
202+ site.store
203+ .initiate_transfer(&repo.id, &target.id, &owner.id)
204+ .await
205+ .unwrap();
206+
207+ let repo_wake = site.live.subscribe(&Topic::Repo(repo.id.clone()));
208+ let from_wake = site.live.subscribe(&Topic::User(owner.id.clone()));
209+ let to_wake = site.live.subscribe(&Topic::User(target.id.clone()));
210+ let explore_wake = site.live.subscribe(&Topic::Explore);
211+
212+ let status = post_form(
213+ &site,
214+ &format!("/-/transfers/{}/accept", repo.id),
215+ &target_cookie,
216+ &format!("csrf={CSRF}"),
217+ )
218+ .await;
219+ assert_eq!(status, 303);
220+ assert!(repo_wake.has_changed().expect("open"));
221+ assert!(from_wake.has_changed().expect("open"));
222+ assert!(to_wake.has_changed().expect("open"));
223+ assert!(explore_wake.has_changed().expect("open"));
224+}
crates/forge-web/src/pages/repo_transfer.rsmodified+16 -1
@@ -24,6 +24,7 @@ use crate::i18n::repo_admin::{self as g, K};
24 use crate::i18n::{self, Lang};24 use crate::i18n::{self, Lang};
25 use crate::pages::repo::repo_head;25 use crate::pages::repo::repo_head;
26 use crate::pages::repo_ctx::{RepoHead, load_repo};26 use crate::pages::repo_ctx::{RepoHead, load_repo};
27+use crate::pages::repo_settings_live::{bump_settings, transfer_live_url};
27 use crate::state::AppState;28 use crate::state::AppState;
28 use crate::views::{csrf_field, shell};29 use crate::views::{csrf_field, shell};
29 30
@@ -130,7 +131,8 @@ async fn transfer_get(cx: &Cx) -> Result<impl View> {
130 None => (String::new(), None, None, None),131 None => (String::new(), None, None, None),
131 };132 };
132 let settings = may_delete.then(|| format!("/{title}/settings/rename"));133 let settings = may_delete.then(|| format!("/{title}/settings/rename"));
133- let head = ctx.head(state, "settings").await?;134+ let mut head = ctx.head(state, "settings").await?;
135+ head.live = transfer_live_url(&ctx.repo.owner_name, &ctx.repo.name);
134 let page_title = g::transfer_page_title(lang, &title);136 let page_title = g::transfer_page_title(lang, &title);
135 let csrf = session.csrf_token.clone();137 let csrf = session.csrf_token.clone();
136 Ok(view! {138 Ok(view! {
@@ -156,7 +158,18 @@ async fn accept_post(cx: &Cx, Form(form): Form<ActionForm>) -> Result<SeeOther>
156 let (user, _) = require_csrf(cx, &form.csrf).await?;158 let (user, _) = require_csrf(cx, &form.csrf).await?;
157 let state = app_context::<AppState>(cx);159 let state = app_context::<AppState>(cx);
158 let pending = open_transfer(state, &repo_id).await?;160 let pending = open_transfer(state, &repo_id).await?;
161+ let from_owner = state
162+ .store
163+ .repo_by_id(&repo_id)
164+ .await?
165+ .map(|repo| repo.owner_id);
159 state.store.accept_transfer(&repo_id, &user.id).await?;166 state.store.accept_transfer(&repo_id, &user.id).await?;
167+ bump_settings(&state.live, &repo_id);
168+ // The repo leaves its old owner's listing and joins the new one's, and the public listing sees it.
169+ state.live.bump_repo(&pending.to_owner_id, &repo_id);
170+ if let Some(from_owner) = from_owner {
171+ state.live.bump_repo(&from_owner, &repo_id);
172+ }
160 audit::record(173 audit::record(
161 &state.store,174 &state.store,
162 Some(cx),175 Some(cx),
@@ -184,6 +197,7 @@ async fn decline_post(cx: &Cx, Form(form): Form<ActionForm>) -> Result<SeeOther>
184 let state = app_context::<AppState>(cx);197 let state = app_context::<AppState>(cx);
185 let pending = open_transfer(state, &repo_id).await?;198 let pending = open_transfer(state, &repo_id).await?;
186 state.store.decline_transfer(&repo_id, &user.id).await?;199 state.store.decline_transfer(&repo_id, &user.id).await?;
200+ bump_settings(&state.live, &repo_id);
187 audit::record(201 audit::record(
188 &state.store,202 &state.store,
189 Some(cx),203 Some(cx),
@@ -221,6 +235,7 @@ async fn cancel_post(cx: &Cx, Form(form): Form<ActionForm>) -> Result<SeeOther>
221 return Err(forbidden().into());235 return Err(forbidden().into());
222 }236 }
223 state.store.cancel_transfer(&repo_id).await?;237 state.store.cancel_transfer(&repo_id).await?;
238+ bump_settings(&state.live, &repo_id);
224 audit::record(239 audit::record(
225 &state.store,240 &state.store,
226 Some(cx),241 Some(cx),
crates/forge-web/src/pages/repo_webhooks.rsmodified+2 -1
@@ -29,6 +29,7 @@ use crate::i18n::{
29 use crate::pages::repo::repo_head;29 use crate::pages::repo::repo_head;
30 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};30 use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo};
31 use crate::pages::repo_settings::settings_nav_for;31 use crate::pages::repo_settings::settings_nav_for;
32+use crate::pages::repo_settings_live::settings_head;
32 use crate::state::AppState;33 use crate::state::AppState;
33 use crate::views::{alert, enc, shell};34 use crate::views::{alert, enc, shell};
34 use crate::webhooks::client::{WebhookPolicy, validate_url};35 use crate::webhooks::client::{WebhookPolicy, validate_url};
@@ -390,7 +391,7 @@ impl Frame {
390 page_title: format!("{title} \u{b7} {}", g::t(lang, K::Webhooks)),391 page_title: format!("{title} \u{b7} {}", g::t(lang, K::Webhooks)),
391 title,392 title,
392 archived: ctx.repo.archived,393 archived: ctx.repo.archived,
393- head: ctx.head(state, "settings").await?,394+ head: settings_head(ctx, state).await?,
394 })395 })
395 }396 }
396 }397 }
crates/forge-web/src/test_support.rsmodified+1 -0
@@ -101,6 +101,7 @@ pub fn app_router(state: AppState) -> Router {
101 .cookies()101 .cookies()
102 .layer(avatar_body_limit())102 .layer(avatar_body_limit())
103 .layer(RepoMovedLayer)103 .layer(RepoMovedLayer)
104+ .layer(crate::pages::repo_settings_live::RepoSettingsWriteLayer)
104 .layer(crate::pages::pull_live::PullWriteLayer)105 .layer(crate::pages::pull_live::PullWriteLayer)
105 .layer(crate::pages::discussion_live::DiscussionWriteLayer)106 .layer(crate::pages::discussion_live::DiscussionWriteLayer)
106 .layer(crate::pages::org_live::OrgWriteLayer)107 .layer(crate::pages::org_live::OrgWriteLayer)
crates/forge-web/src/webhooks/emit.rsmodified+1 -0
@@ -446,6 +446,7 @@ async fn enqueue(
446 Ok(_) => {446 Ok(_) => {
447 queued += 1;447 queued += 1;
448 state.webhook_waker.notify_one();448 state.webhook_waker.notify_one();
449+ crate::pages::repo_settings_live::bump_settings(&state.live, &hook.repo_id);
449 }450 }
450 Err(e) => {451 Err(e) => {
451 tracing::error!(error = %e, hook_id = %hook.id, event = event.as_str(), "webhooks: enqueue failed");452 tracing::error!(error = %e, hook_id = %hook.id, event = event.as_str(), "webhooks: enqueue failed");
crates/forge-web/src/webhooks/worker.rsmodified+10 -0
@@ -20,6 +20,7 @@ use time::format_description::well_known::Rfc3339;
20 use tokio::sync::Notify;20 use tokio::sync::Notify;
21 21
22 use super::client::{HookClient, SendOutcome, WebhookPolicy, sign, validate_url};22 use super::client::{HookClient, SendOutcome, WebhookPolicy, sign, validate_url};
23+use crate::pages::repo_settings_live::bump_settings;
23 use crate::state::AppState;24 use crate::state::AppState;
24 25
25 /// Handle that wakes the worker. `emit` calls `notify_one` after queueing deliveries.26 /// Handle that wakes the worker. `emit` calls `notify_one` after queueing deliveries.
@@ -145,6 +146,15 @@ pub(crate) async fn process_due(
145 {146 {
146 tracing::error!(error = %e, delivery_id = %delivery.id, "webhooks: recording the result failed");147 tracing::error!(error = %e, delivery_id = %delivery.id, "webhooks: recording the result failed");
147 }148 }
149+ // The delivery list of the repo's settings shows the result. A deleted hook has no repo to
150+ // wake, and its deliveries are gone with it.
151+ match state.store.webhook(&delivery.hook_id).await {
152+ Ok(Some(hook)) => bump_settings(&state.live, &hook.repo_id),
153+ Ok(None) => {}
154+ Err(e) => {
155+ tracing::error!(error = %e, delivery_id = %delivery.id, "webhooks: loading the hook to bump failed")
156+ }
157+ }
148 })158 })
149 .await;159 .await;
150 Ok(count)160 Ok(count)
docs/architecture.mdmodified+1 -0
@@ -274,6 +274,7 @@ Pages update without a reload. A write that changes what a page shows calls `Liv
274 | Admin server `/admin/server` | `/admin/server/events` | saved or reset settings. A page waiting for a restart gets one event when a new process answers |274 | Admin server `/admin/server` | `/admin/server/events` | saved or reset settings. A page waiting for a restart gets one event when a new process answers |
275 | Settings `/settings/...` (profile, keys, GPG, tokens, sessions, security, passkeys, notifications, appearance, export) | `/settings/events` (the section sidebar) | the viewer's own account topic: a setting, key, token, passkey or session changed, a sign-in or key use happened, or a suspension or account deletion. A revoked current session leaves for the login page on the next re-read |275 | Settings `/settings/...` (profile, keys, GPG, tokens, sessions, security, passkeys, notifications, appearance, export) | `/settings/events` (the section sidebar) | the viewer's own account topic: a setting, key, token, passkey or session changed, a sign-in or key use happened, or a suspension or account deletion. A revoked current session leaves for the login page on the next re-read |
276 | Every signed-in page (`/-/alive`, the stream every page holds) | the viewer's account topic. A `theme` event carries the appearance attributes (theme, accents, base, sidebar side) when they change, and `nav.js` applies them with no reload. The stream is one connection, shared with the restart check |276 | Every signed-in page (`/-/alive`, the stream every page holds) | the viewer's account topic. A `theme` event carries the appearance attributes (theme, accents, base, sidebar side) when they change, and `nav.js` applies them with no reload. The stream is one connection, shared with the restart check |
277+| Repo settings `/{owner}/{repo}/settings/...`, security `/{owner}/{repo}/security`, transfer `/{owner}/{repo}/transfer` | `/{owner}/{repo}/settings/live` (admins only), `/{owner}/{repo}/security/live`, `/{owner}/{repo}/transfer/live` | the repo settings topic: a settings POST, a webhook delivery queued or sent, a mirror sync that finished, a security scan that opened or fixed an alert, a transfer accepted, declined or cancelled. The settings stream also follows the repo refs for the branch picker |
277 278
278 The org streams follow the same access as their pages: a non-member gets 404, and a member who is not an owner gets 403 on the settings stream. A team page names only the linked repos the viewer may read. Each org write (a POST under `/org/{org}/members`, `teams` or `settings`) wakes the org's streams. The org notices (for example "Member added.") are carried in the redirect's `?notice=` query, not a cookie, so a live refresh of the page shows the same notice again.279 The org streams follow the same access as their pages: a non-member gets 404, and a member who is not an owner gets 403 on the settings stream. A team page names only the linked repos the viewer may read. Each org write (a POST under `/org/{org}/members`, `teams` or `settings`) wakes the org's streams. The org notices (for example "Member added.") are carried in the redirect's `?notice=` query, not a cookie, so a live refresh of the page shows the same notice again.
279 280