| d23464c0 Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
use super::{filters, Error, Page, Shell}; |
| 19bb31ff Andy McCall ↶ |
4 |
use crate::auth::Viewer; |
| d23464c0 Andy McCall ↶ |
5 |
use crate::{db, Shared}; |
| 6 |
use askama::Template; |
| 1553bfc9 Andy McCall ↶ |
7 |
use axum::extract::{Path, Query, State}; |
| d23464c0 Andy McCall ↶ |
8 |
use axum::http::header; |
| 9 |
use axum::response::{IntoResponse, Response}; |
| 10 |
use hub_api::Visibility; |
| 1553bfc9 Andy McCall ↶ |
11 |
use serde::Deserialize; |
| d23464c0 Andy McCall ↶ |
12 |
|
| 13 |
#[derive(Template)] |
| 14 |
#[template(path = "home.html")] |
| 15 |
pub struct HomePage { |
| 16 |
shell: Shell, |
| 17 |
description: String, |
| 18 |
repos: Vec<db::repos::RepoRow>, |
| 19 |
owner: Option<String>, |
| 1553bfc9 Andy McCall ↶ |
20 |
|
| 21 |
q: String, |
| 22 |
sort: String, |
| 23 |
topic: String, |
| 24 |
stars: std::collections::HashMap<i64, i64>, |
| 25 |
} |
| 26 |
|
| 27 |
impl HomePage { |
| 28 |
fn star_count(&self, id: &i64) -> i64 { |
| 29 |
self.stars.get(id).copied().unwrap_or(0) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
#[derive(Deserialize, Default)] |
| 35 |
pub struct ExploreQuery { |
| 36 |
#[serde(default)] |
| 37 |
q: String, |
| 38 |
#[serde(default)] |
| 39 |
sort: String, |
| 40 |
#[serde(default)] |
| 41 |
topic: String, |
| d23464c0 Andy McCall ↶ |
42 |
} |
| 43 |
|
| 25ab8c73 Andy McCall ↶ |
44 |
|
| 45 |
|
| 46 |
|
| 47 |
#[derive(Template)] |
| 48 |
#[template(path = "dashboard.html")] |
| 49 |
pub struct Dashboard { |
| 50 |
shell: Shell, |
| 51 |
mine: Vec<db::repos::RepoRow>, |
| 52 |
reviews: Vec<db::activity::ProposalLine>, |
| 53 |
authored: Vec<db::activity::ProposalLine>, |
| 54 |
assigned: Vec<db::activity::IssueLine>, |
| 55 |
feed: Vec<db::activity::Activity>, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
const FEED_LIMIT: i64 = 40; |
| 60 |
|
| 61 |
|
| 62 |
pub async fn index(State(app): State<Shared>, viewer: Viewer) -> Result<Response, Error> { |
| 63 |
let Some(account) = viewer.account().cloned() else { |
| 1553bfc9 Andy McCall ↶ |
64 |
return Ok(explore(State(app), viewer, Query(ExploreQuery::default())).await?.into_response()); |
| 25ab8c73 Andy McCall ↶ |
65 |
}; |
| 19bb31ff Andy McCall ↶ |
66 |
let shell = Shell::new(&app, "/", &viewer); |
| 25ab8c73 Andy McCall ↶ |
67 |
let db_err = |e| Error::from_db(shell.clone(), e); |
| 68 |
let all = db::repos::list(&app.db).await.map_err(db_err)?; |
| 69 |
let member: Vec<i64> = db::accounts::visible_repo_ids(&app.db, account.id).await.map_err(db_err)?; |
| 70 |
let seen = visible(&app, &shell, &viewer, all).await?; |
| 71 |
let seen_ids: Vec<i64> = seen.iter().map(|r| r.id).collect(); |
| 72 |
let mut mine: Vec<db::repos::RepoRow> = seen.into_iter().filter(|r| member.contains(&r.id)).collect(); |
| 73 |
mine.sort_by_key(|r| std::cmp::Reverse(r.updated_ms)); |
| 74 |
let reviews = db::activity::awaiting_review(&app.db, account.id, &member).await.map_err(db_err)?; |
| 75 |
let authored = db::activity::authored(&app.db, account.id, &seen_ids).await.map_err(db_err)?; |
| 76 |
let assigned = db::activity::assigned(&app.db, account.id, &seen_ids).await.map_err(db_err)?; |
| 77 |
let feed = db::activity::feed(&app.db, &seen_ids, FEED_LIMIT).await.map_err(db_err)?; |
| 78 |
Ok(Page(Dashboard { shell, mine, reviews, authored, assigned, feed }).into_response()) |
| d23464c0 Andy McCall ↶ |
79 |
} |
| 80 |
|
| 1553bfc9 Andy McCall ↶ |
81 |
|
| 82 |
|
| 83 |
pub async fn explore(State(app): State<Shared>, viewer: Viewer, Query(q): Query<ExploreQuery>) -> Result<Page<HomePage>, Error> { |
| 25ab8c73 Andy McCall ↶ |
84 |
let shell = Shell::new(&app, "/explore", &viewer); |
| 1553bfc9 Andy McCall ↶ |
85 |
let db_err = |e| Error::from_db(shell.clone(), e); |
| 86 |
let mut repos = visible(&app, &shell, &viewer, db::repos::list(&app.db).await.map_err(db_err)?).await?; |
| 87 |
let stars = db::repos::star_counts(&app.db).await.map_err(db_err)?; |
| 88 |
let needle = q.q.trim().to_lowercase(); |
| 89 |
if !needle.is_empty() { |
| 90 |
repos.retain(|r| { |
| 91 |
r.slug().to_lowercase().contains(&needle) |
| 92 |
|| r.description.to_lowercase().contains(&needle) |
| 93 |
|| r.topic_list().iter().any(|t| t.contains(&needle)) |
| 94 |
}); |
| 95 |
} |
| 96 |
let topic = q.topic.trim().to_lowercase(); |
| 97 |
if !topic.is_empty() { |
| 98 |
repos.retain(|r| r.topic_list().contains(&topic.as_str())); |
| 99 |
} |
| 100 |
let sort = match q.sort.as_str() { |
| 101 |
"stars" | "name" => q.sort.clone(), |
| 102 |
_ => "activity".to_string(), |
| 103 |
}; |
| 104 |
match sort.as_str() { |
| 105 |
"stars" => repos.sort_by_key(|r| (std::cmp::Reverse(stars.get(&r.id).copied().unwrap_or(0)), std::cmp::Reverse(r.updated_ms))), |
| 106 |
"name" => repos.sort_by_key(|r| (r.name.to_lowercase(), r.owner.to_lowercase())), |
| 107 |
_ => repos.sort_by_key(|r| std::cmp::Reverse(r.updated_ms)), |
| 108 |
} |
| 109 |
Ok(Page(HomePage { description: app.config.site.description.clone(), shell, repos, owner: None, q: q.q, sort, topic, stars })) |
| d23464c0 Andy McCall ↶ |
110 |
} |
| 111 |
|
| 10174045 Andy McCall ↶ |
112 |
|
| 113 |
|
| 114 |
#[derive(Template)] |
| 115 |
#[template(path = "profile.html")] |
| 116 |
pub struct ProfilePage { |
| 117 |
shell: Shell, |
| 118 |
account: db::accounts::Account, |
| 119 |
keys: Vec<db::accounts::LinkedIdentity>, |
| 120 |
orgs: Vec<(db::accounts::Organisation, String)>, |
| 121 |
repos: Vec<db::repos::RepoRow>, |
| 122 |
feed: Vec<db::activity::Activity>, |
| 123 |
is_me: bool, |
| 124 |
} |
| 125 |
|
| 126 |
impl ProfilePage { |
| 127 |
|
| 128 |
fn website_href(&self) -> Option<&str> { |
| 129 |
let w = self.account.website.trim(); |
| 130 |
(w.starts_with("https://") || w.starts_with("http://")).then_some(w) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
#[derive(Template)] |
| 137 |
#[template(path = "org.html")] |
| 138 |
pub struct OrgPage { |
| 139 |
shell: Shell, |
| 140 |
org: db::accounts::Organisation, |
| 141 |
members: Vec<(db::accounts::Account, String)>, |
| 142 |
teams: Vec<(db::accounts::Team, usize)>, |
| 143 |
repos: Vec<db::repos::RepoRow>, |
| 144 |
feed: Vec<db::activity::Activity>, |
| 145 |
can_admin: bool, |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
const OWNER_FEED: i64 = 20; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
pub async fn owner(State(app): State<Shared>, viewer: Viewer, Path(owner): Path<String>) -> Result<Response, Error> { |
| 19bb31ff Andy McCall ↶ |
155 |
let shell = Shell::new(&app, &format!("/{owner}"), &viewer); |
| d23464c0 Andy McCall ↶ |
156 |
if !crate::repos::names::valid_owner(&owner) { |
| 157 |
return Err(Error::not_found(shell)); |
| 158 |
} |
| 10174045 Andy McCall ↶ |
159 |
let db_err = |e| Error::from_db(shell.clone(), e); |
| 160 |
let mut repos = visible(&app, &shell, &viewer, db::repos::list_by_owner(&app.db, &owner).await.map_err(db_err)?).await?; |
| 161 |
repos.sort_by_key(|r| std::cmp::Reverse(r.updated_ms)); |
| 162 |
if let Some(account) = db::accounts::account_by_name(&app.db, &owner).await.map_err(db_err)? { |
| 163 |
|
| 164 |
let all = visible(&app, &shell, &viewer, db::repos::list(&app.db).await.map_err(db_err)?).await?; |
| 165 |
let ids: Vec<i64> = all.iter().map(|r| r.id).collect(); |
| 166 |
let feed = db::activity::feed_by(&app.db, &ids, Some(&account.username), OWNER_FEED).await.map_err(db_err)?; |
| 167 |
let keys = db::accounts::identities_of(&app.db, account.id).await.map_err(db_err)?; |
| 168 |
let orgs = db::accounts::orgs_of(&app.db, account.id).await.map_err(db_err)?; |
| 169 |
let is_me = viewer.account().is_some_and(|a| a.id == account.id); |
| 170 |
return Ok(Page(ProfilePage { shell, account, keys, orgs, repos, feed, is_me }).into_response()); |
| 171 |
} |
| 172 |
if let Some(org) = db::accounts::org_by_name(&app.db, &owner).await.map_err(db_err)? { |
| 173 |
let members = db::accounts::org_members(&app.db, org.id).await.map_err(db_err)?; |
| 174 |
let mut teams = Vec::new(); |
| 175 |
for t in db::accounts::teams(&app.db, org.id).await.map_err(db_err)? { |
| 176 |
let n = db::accounts::team_members(&app.db, t.id).await.map_err(db_err)?.len(); |
| 177 |
teams.push((t, n)); |
| 178 |
} |
| 179 |
let ids: Vec<i64> = repos.iter().map(|r| r.id).collect(); |
| 180 |
let feed = db::activity::feed(&app.db, &ids, OWNER_FEED).await.map_err(db_err)?; |
| 181 |
let can_admin = |
| 182 |
viewer.is_admin() || viewer.account().is_some_and(|a| members.iter().any(|(m, role)| m.id == a.id && role == "owner")); |
| 183 |
return Ok(Page(OrgPage { shell, org, members, teams, repos, feed, can_admin }).into_response()); |
| 184 |
} |
| 185 |
if repos.is_empty() { |
| d23464c0 Andy McCall ↶ |
186 |
return Err(Error::not_found(shell)); |
| 187 |
} |
| 1553bfc9 Andy McCall ↶ |
188 |
let stars = db::repos::star_counts(&app.db).await.unwrap_or_default(); |
| 189 |
Ok(Page(HomePage { |
| 190 |
description: String::new(), |
| 191 |
shell, |
| 192 |
repos, |
| 193 |
owner: Some(owner), |
| 194 |
q: String::new(), |
| 195 |
sort: "activity".into(), |
| 196 |
topic: String::new(), |
| 197 |
stars, |
| 198 |
}) |
| 199 |
.into_response()) |
| d23464c0 Andy McCall ↶ |
200 |
} |
| 201 |
|
| 19bb31ff Andy McCall ↶ |
202 |
|
| 203 |
|
| 07b1a5bb Andy McCall ↶ |
204 |
pub(crate) async fn visible( |
| 205 |
app: &Shared, |
| 206 |
shell: &Shell, |
| 207 |
viewer: &Viewer, |
| 208 |
rows: Vec<db::repos::RepoRow>, |
| 209 |
) -> Result<Vec<db::repos::RepoRow>, Error> { |
| 19bb31ff Andy McCall ↶ |
210 |
let mine: Vec<i64> = match viewer.account() { |
| 211 |
Some(a) if a.is_admin => rows.iter().map(|r| r.id).collect(), |
| 212 |
Some(a) => db::accounts::visible_repo_ids(&app.db, a.id).await.map_err(|e| Error::from_db(shell.clone(), e))?, |
| 213 |
None => vec![], |
| 214 |
}; |
| 215 |
let signed_in = viewer.signed_in(); |
| 216 |
Ok(rows |
| 217 |
.into_iter() |
| 218 |
.filter(|r| match r.visibility() { |
| 219 |
Visibility::Public => true, |
| 220 |
Visibility::Internal => signed_in, |
| 221 |
Visibility::Private => mine.contains(&r.id), |
| 222 |
}) |
| 223 |
.collect()) |
| 224 |
} |
| 225 |
|
| d23464c0 Andy McCall ↶ |
226 |
pub async fn health(State(app): State<Shared>) -> Response { |
| 227 |
let body = serde_json::json!({ |
| 228 |
"ok": true, |
| 229 |
"version": env!("CARGO_PKG_VERSION"), |
| e9bfea8e Andy McCall ↶ |
230 |
"build": env!("OXY_BUILD_ID"), |
| d23464c0 Andy McCall ↶ |
231 |
"uptime_s": app.started.elapsed().as_secs(), |
| 232 |
}); |
| 233 |
axum::Json(body).into_response() |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
pub async fn hub_key(State(app): State<Shared>) -> Response { |
| 239 |
( |
| 240 |
[(header::CONTENT_TYPE, "application/oxy-identity"), (header::CONTENT_DISPOSITION, "attachment; filename=\"hub.identity\"")], |
| 241 |
app.hub_key.bytes.clone(), |
| 242 |
) |
| 243 |
.into_response() |
| 244 |
} |
| 8f3d2ccf Andy McCall ↶ |
245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
pub async fn hub_ca(State(app): State<Shared>) -> Response { |
| 250 |
if !app.config.tls.self_signed { |
| 251 |
return (axum::http::StatusCode::NOT_FOUND, "this hub does not use a self-signed certificate").into_response(); |
| 252 |
} |
| 253 |
match tokio::fs::read(app.config.tls_cert()).await { |
| 254 |
Ok(pem) => ( |
| 255 |
[(header::CONTENT_TYPE, "application/x-pem-file"), (header::CONTENT_DISPOSITION, "attachment; filename=\"oxyhub-ca.pem\"")], |
| 256 |
pem, |
| 257 |
) |
| 258 |
.into_response(), |
| 259 |
Err(_) => (axum::http::StatusCode::NOT_FOUND, "no certificate").into_response(), |
| 260 |
} |
| e9bfea8e Andy McCall ↶ |
261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
pub fn versions() -> serde_json::Value { |
| 266 |
serde_json::json!({ |
| 267 |
"oxygenhub": { "version": env!("CARGO_PKG_VERSION"), "build": env!("OXY_BUILD_ID") }, |
| 268 |
"oxy_core": oxy_core::VERSION, |
| 269 |
"flow_core": flow_core::VERSION, |
| 270 |
}) |
| 271 |
} |
| 272 |
|
| 273 |
#[derive(askama::Template)] |
| 274 |
#[template(path = "about.html")] |
| 275 |
pub struct AboutPage { |
| 276 |
shell: Shell, |
| 277 |
oxy_core: &'static str, |
| 278 |
flow_core: &'static str, |
| 279 |
|
| 280 |
runners: Vec<crate::db::runners::Runner>, |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
pub async fn about(State(app): State<Shared>, viewer: Viewer) -> Result<Page<AboutPage>, Error> { |
| 286 |
let shell = Shell::new(&app, "/about", &viewer); |
| 287 |
let runners = if viewer.is_admin() { crate::db::runners::list(&app.db).await.unwrap_or_default() } else { vec![] }; |
| 288 |
Ok(Page(AboutPage { shell, oxy_core: oxy_core::VERSION, flow_core: flow_core::VERSION, runners })) |
| 8f3d2ccf Andy McCall ↶ |
289 |
} |