| 757195ad Andy McCall ↶ |
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, |
| 6246d1e5 Andy McCall ↶ |
23 |
|
| 24 |
|
| 25 |
backdrop: String, |
| 757195ad Andy McCall ↶ |
26 |
} |
| 27 |
|
| 6246d1e5 Andy McCall ↶ |
28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
fn ripples_json(slug: &str, used_by: &[Consumer], follows: &[(RepoRow, Consumer)]) -> String { |
| 33 |
let tone = |class: &str| class.trim_start_matches("df-").to_string(); |
| 34 |
let upstreams: Vec<(String, String)> = |
| 35 |
follows.iter().map(|(up, c)| (format!("{}/{}", up.owner, up.name), tone(c.upstream.class))).collect(); |
| 36 |
let consumers: Vec<(String, String)> = used_by.iter().map(|c| (format!("{}/{}", c.owner, c.name), tone(c.downstream.class))).collect(); |
| 37 |
serde_json::json!({ "scene": "ripples", "repo": slug, "upstreams": upstreams, "consumers": consumers }) |
| 38 |
.to_string() |
| 39 |
.replace('<', "\\u003c") |
| 40 |
} |
| 41 |
|
| 757195ad Andy McCall ↶ |
42 |
|
| 43 |
pub async fn page( |
| 44 |
State(app): State<Shared>, |
| 45 |
viewer: Viewer, |
| 46 |
Path((owner, name)): Path<(String, String)>, |
| 47 |
) -> Result<Page<DiffusionPage>, Error> { |
| 48 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/diffusion"), &viewer); |
| 49 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 50 |
let used_by = crate::diffusion::consumers(&app, &row, viewer.account()).await.unwrap_or_default(); |
| 51 |
let follows = crate::diffusion::follows(&app, &row, viewer.account()).await; |
| 52 |
let unverified = crate::diffusion::unverified(&app, &row).await; |
| 6246d1e5 Andy McCall ↶ |
53 |
let backdrop = ripples_json(&format!("{owner}/{name}"), &used_by, &follows); |
| 54 |
Ok(Page(DiffusionPage { shell, repo: RepoShell::new(&app, &row, "diffusion").with_role(role), used_by, follows, unverified, backdrop })) |
| 757195ad Andy McCall ↶ |
55 |
} |
| 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 |
pub async fn admin(State(app): State<Shared>, viewer: Viewer) -> Result<Page<AdminPage>, Error> { |
| 66 |
let shell = Shell::new(&app, "/admin/diffusion", &viewer); |
| 67 |
if !viewer.is_admin() { |
| 68 |
return Err(Error::not_found(shell)); |
| 69 |
} |
| 70 |
let upstreams = crate::diffusion::everything(&app, viewer.account()).await; |
| 71 |
Ok(Page(AdminPage { shell, upstreams })) |
| 72 |
} |