Sam Jeston
History
94 lines·
2.4 KB
·Rust
| 1 | //! Dashboard, explore and user profile pages. |
| 2 | |
| 3 | use askama::Template; |
| 4 | use axum::extract::{Path, State}; |
| 5 | |
| 6 | use super::{AppError, Ctx, WebResult, render}; |
| 7 | use crate::db::{self, PullRequest, Repo, User}; |
| 8 | use crate::state::AppState; |
| 9 | |
| 10 | #[derive(Template)] |
| 11 | #[template(path = "dashboard.html")] |
| 12 | struct DashboardPage { |
| 13 | ctx: Ctx, |
| 14 | repos: Vec<Repo>, |
| 15 | pulls: Vec<(PullRequest, String, String)>, |
| 16 | recent: Vec<Repo>, |
| 17 | } |
| 18 | |
| 19 | #[derive(Template)] |
| 20 | #[template(path = "landing.html")] |
| 21 | struct LandingPage { |
| 22 | ctx: Ctx, |
| 23 | repos: Vec<Repo>, |
| 24 | can_register: bool, |
| 25 | } |
| 26 | |
| 27 | pub async fn index(State(state): State<AppState>, ctx: Ctx) -> WebResult { |
| 28 | match ctx.user.clone() { |
| 29 | Some(user) => { |
| 30 | let repos = db::list_member_repos(&state.db, user.id).await?; |
| 31 | let pulls = db::dashboard_pulls(&state.db, user.id).await?; |
| 32 | let recent = db::list_public_repos(&state.db, 10) |
| 33 | .await? |
| 34 | .into_iter() |
| 35 | .filter(|r| r.owner_id != user.id) |
| 36 | .collect(); |
| 37 | render(&DashboardPage { |
| 38 | ctx, |
| 39 | repos, |
| 40 | pulls, |
| 41 | recent, |
| 42 | }) |
| 43 | } |
| 44 | None => { |
| 45 | let repos = db::list_public_repos(&state.db, 12).await?; |
| 46 | let can_register = |
| 47 | !state.config.disable_registration || db::count_users(&state.db).await? == 0; |
| 48 | render(&LandingPage { |
| 49 | ctx, |
| 50 | repos, |
| 51 | can_register, |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #[derive(Template)] |
| 58 | #[template(path = "explore.html")] |
| 59 | struct ExplorePage { |
| 60 | ctx: Ctx, |
| 61 | repos: Vec<Repo>, |
| 62 | } |
| 63 | |
| 64 | pub async fn explore(State(state): State<AppState>, ctx: Ctx) -> WebResult { |
| 65 | let repos = db::list_public_repos(&state.db, 200).await?; |
| 66 | render(&ExplorePage { ctx, repos }) |
| 67 | } |
| 68 | |
| 69 | #[derive(Template)] |
| 70 | #[template(path = "profile.html")] |
| 71 | struct ProfilePage { |
| 72 | ctx: Ctx, |
| 73 | profile: User, |
| 74 | repos: Vec<Repo>, |
| 75 | is_self: bool, |
| 76 | } |
| 77 | |
| 78 | pub async fn profile( |
| 79 | State(state): State<AppState>, |
| 80 | ctx: Ctx, |
| 81 | Path(owner): Path<String>, |
| 82 | ) -> WebResult { |
| 83 | let profile = db::user_by_username(&state.db, &owner) |
| 84 | .await? |
| 85 | .ok_or(AppError::NotFound)?; |
| 86 | let repos = db::list_owner_repos(&state.db, profile.id, ctx.user.as_deref()).await?; |
| 87 | let is_self = ctx.user.as_ref().is_some_and(|u| u.id == profile.id); |
| 88 | render(&ProfilePage { |
| 89 | ctx, |
| 90 | profile, |
| 91 | repos, |
| 92 | is_self, |
| 93 | }) |
| 94 | } |