| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::{filters, lookup, Error, Page, RepoShell, Shell}; |
| 7 | | use crate::auth::Viewer; |
| 8 | | use crate::db::repos::RepoRow; |
| 9 | | use crate::diffusion::Consumer; |
| 10 | | use crate::Shared; |
| 11 | | use askama::Template; |
| 12 | | use axum::extract::{Path, State}; |
| 13 | |
|
| 14 | | #[derive(Template)] |
| 15 | | #[template(path = "diffusion.html")] |
| 16 | | pub struct DiffusionPage { |
| 17 | | shell: Shell, |
| 18 | | repo: RepoShell, |
| 19 | | used_by: Vec<Consumer>, |
| 20 | | follows: Vec<(RepoRow, Consumer)>, |
| 21 | | |
| 22 | | unverified: bool, |
| 23 | | |
| 24 | | |
| 25 | | backdrop: String, |
| 26 | | } |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 3× | fn ripples_json(slug: &str, used_by: &[Consumer], follows: &[(RepoRow, Consumer)]) -> String { |
| 33 | 3× | let tone = |class: &str| class.trim_start_matches("df-").to_string(); |
| 34 | 3× | let upstreams: Vec<(String, String)> = |
| 35 | 3× | follows.iter().map(|(up, c)| (format!("{}/{}", up.owner, up.name), tone(c.upstream.class))).collect(); |
| 36 | 3× | let consumers: Vec<(String, String)> = used_by.iter().map(|c| (format!("{}/{}", c.owner, c.name), tone(c.downstream.class))).collect(); |
| 37 | 3× | serde_json::json!({ "scene": "ripples", "repo": slug, "upstreams": upstreams, "consumers": consumers }) |
| 38 | 3× | .to_string() |
| 39 | 3× | .replace('<', "\\u003c") |
| 40 | 3× | } |
| 41 | |
|
| 42 | |
|
| 43 | 3× | pub async fn page( |
| 44 | 3× | State(app): State<Shared>, |
| 45 | 3× | viewer: Viewer, |
| 46 | 3× | Path((owner, name)): Path<(String, String)>, |
| 47 | 3× | ) -> Result<Page<DiffusionPage>, Error> { |
| 48 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/diffusion"), &viewer); |
| 49 | 3× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 50 | 3× | let used_by = crate::diffusion::consumers(&app, &row, viewer.account()).await.unwrap_or_default(); |
| 51 | 3× | let follows = crate::diffusion::follows(&app, &row, viewer.account()).await; |
| 52 | 3× | let unverified = crate::diffusion::unverified(&app, &row).await; |
| 53 | 3× | let backdrop = ripples_json(&format!("{owner}/{name}"), &used_by, &follows); |
| 54 | 3× | Ok(Page(DiffusionPage { shell, repo: RepoShell::new(&app, &row, "diffusion").with_role(role), used_by, follows, unverified, backdrop })) |
| 55 | 3× | } |
| 56 | |
|
| 57 | | #[derive(Template)] |
| 58 | | #[template(path = "admin_diffusion.html")] |
| 59 | | pub struct AdminPage { |
| 60 | | shell: Shell, |
| 61 | | upstreams: Vec<(RepoRow, Vec<Consumer>, bool)>, |
| 62 | | } |
| 63 | |
|
| 64 | |
|
| 65 | 2× | pub async fn admin(State(app): State<Shared>, viewer: Viewer) -> Result<Page<AdminPage>, Error> { |
| 66 | 2× | let shell = Shell::new(&app, "/admin/diffusion", &viewer); |
| 67 | 2× | if !viewer.is_admin() { |
| 68 | 1× | return Err(Error::not_found(shell)); |
| 69 | 1× | } |
| 70 | 1× | let upstreams = crate::diffusion::everything(&app, viewer.account()).await; |
| 71 | 1× | Ok(Page(AdminPage { shell, upstreams })) |
| 72 | 2× | } |