| 1 |
|
| 2 |
|
| 3 |
use super::{filters, Error, Page, Shell}; |
| 4 |
use crate::auth::Viewer; |
| 5 |
use crate::{db, Shared}; |
| 6 |
use askama::Template; |
| 7 |
use axum::extract::{Path, State}; |
| 8 |
use axum::http::header; |
| 9 |
use axum::response::{IntoResponse, Response}; |
| 10 |
use hub_api::Visibility; |
| 11 |
|
| 12 |
#[derive(Template)] |
| 13 |
#[template(path = "home.html")] |
| 14 |
pub struct HomePage { |
| 15 |
shell: Shell, |
| 16 |
description: String, |
| 17 |
repos: Vec<db::repos::RepoRow>, |
| 18 |
owner: Option<String>, |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#[derive(Template)] |
| 25 |
#[template(path = "dashboard.html")] |
| 26 |
pub struct Dashboard { |
| 27 |
shell: Shell, |
| 28 |
mine: Vec<db::repos::RepoRow>, |
| 29 |
reviews: Vec<db::activity::ProposalLine>, |
| 30 |
authored: Vec<db::activity::ProposalLine>, |
| 31 |
assigned: Vec<db::activity::IssueLine>, |
| 32 |
feed: Vec<db::activity::Activity>, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
const FEED_LIMIT: i64 = 40; |
| 37 |
|
| 38 |
|
| 39 |
pub async fn index(State(app): State<Shared>, viewer: Viewer) -> Result<Response, Error> { |
| 40 |
let Some(account) = viewer.account().cloned() else { |
| 41 |
return Ok(explore(State(app), viewer).await?.into_response()); |
| 42 |
}; |
| 43 |
let shell = Shell::new(&app, "/", &viewer); |
| 44 |
let db_err = |e| Error::from_db(shell.clone(), e); |
| 45 |
let all = db::repos::list(&app.db).await.map_err(db_err)?; |
| 46 |
let member: Vec<i64> = db::accounts::visible_repo_ids(&app.db, account.id).await.map_err(db_err)?; |
| 47 |
let seen = visible(&app, &shell, &viewer, all).await?; |
| 48 |
let seen_ids: Vec<i64> = seen.iter().map(|r| r.id).collect(); |
| 49 |
let mut mine: Vec<db::repos::RepoRow> = seen.into_iter().filter(|r| member.contains(&r.id)).collect(); |
| 50 |
mine.sort_by_key(|r| std::cmp::Reverse(r.updated_ms)); |
| 51 |
let reviews = db::activity::awaiting_review(&app.db, account.id, &member).await.map_err(db_err)?; |
| 52 |
let authored = db::activity::authored(&app.db, account.id, &seen_ids).await.map_err(db_err)?; |
| 53 |
let assigned = db::activity::assigned(&app.db, account.id, &seen_ids).await.map_err(db_err)?; |
| 54 |
let feed = db::activity::feed(&app.db, &seen_ids, FEED_LIMIT).await.map_err(db_err)?; |
| 55 |
Ok(Page(Dashboard { shell, mine, reviews, authored, assigned, feed }).into_response()) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
pub async fn explore(State(app): State<Shared>, viewer: Viewer) -> Result<Page<HomePage>, Error> { |
| 60 |
let shell = Shell::new(&app, "/explore", &viewer); |
| 61 |
let repos = visible(&app, &shell, &viewer, db::repos::list(&app.db).await.map_err(|e| Error::from_db(shell.clone(), e))?).await?; |
| 62 |
Ok(Page(HomePage { description: app.config.site.description.clone(), shell, repos, owner: None })) |
| 63 |
} |
| 64 |
|
| 65 |
pub async fn owner(State(app): State<Shared>, viewer: Viewer, Path(owner): Path<String>) -> Result<Page<HomePage>, Error> { |
| 66 |
let shell = Shell::new(&app, &format!("/{owner}"), &viewer); |
| 67 |
if !crate::repos::names::valid_owner(&owner) { |
| 68 |
return Err(Error::not_found(shell)); |
| 69 |
} |
| 70 |
let repos = |
| 71 |
visible(&app, &shell, &viewer, db::repos::list_by_owner(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?) |
| 72 |
.await?; |
| 73 |
let known = db::accounts::account_by_name(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?.is_some() |
| 74 |
|| db::accounts::org_by_name(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?.is_some(); |
| 75 |
if repos.is_empty() && !known { |
| 76 |
return Err(Error::not_found(shell)); |
| 77 |
} |
| 78 |
Ok(Page(HomePage { description: String::new(), shell, repos, owner: Some(owner) })) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
async fn visible(app: &Shared, shell: &Shell, viewer: &Viewer, rows: Vec<db::repos::RepoRow>) -> Result<Vec<db::repos::RepoRow>, Error> { |
| 84 |
let mine: Vec<i64> = match viewer.account() { |
| 85 |
Some(a) if a.is_admin => rows.iter().map(|r| r.id).collect(), |
| 86 |
Some(a) => db::accounts::visible_repo_ids(&app.db, a.id).await.map_err(|e| Error::from_db(shell.clone(), e))?, |
| 87 |
None => vec![], |
| 88 |
}; |
| 89 |
let signed_in = viewer.signed_in(); |
| 90 |
Ok(rows |
| 91 |
.into_iter() |
| 92 |
.filter(|r| match r.visibility() { |
| 93 |
Visibility::Public => true, |
| 94 |
Visibility::Internal => signed_in, |
| 95 |
Visibility::Private => mine.contains(&r.id), |
| 96 |
}) |
| 97 |
.collect()) |
| 98 |
} |
| 99 |
|
| 100 |
pub async fn health(State(app): State<Shared>) -> Response { |
| 101 |
let body = serde_json::json!({ |
| 102 |
"ok": true, |
| 103 |
"version": env!("CARGO_PKG_VERSION"), |
| 104 |
"uptime_s": app.started.elapsed().as_secs(), |
| 105 |
}); |
| 106 |
axum::Json(body).into_response() |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
pub async fn hub_key(State(app): State<Shared>) -> Response { |
| 112 |
( |
| 113 |
[(header::CONTENT_TYPE, "application/oxy-identity"), (header::CONTENT_DISPOSITION, "attachment; filename=\"hub.identity\"")], |
| 114 |
app.hub_key.bytes.clone(), |
| 115 |
) |
| 116 |
.into_response() |
| 117 |
} |