| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use makeover_layout as layout; |
| 32 |
use quasi_router::screen::{Cell, Cells, Column, Tag}; |
| 33 |
use quasi_router::{ |
| 34 |
Action, Document, Node, RegionKind, Request, Response, RouteError, Screen as Described, Slot, |
| 35 |
}; |
| 36 |
use quasi_webview::Webview; |
| 37 |
|
| 38 |
use crate::db; |
| 39 |
|
| 40 |
|
| 41 |
pub const PATH: &str = "/git/{owner}"; |
| 42 |
|
| 43 |
|
| 44 |
pub const PAGE_REGION: &str = "git-repos"; |
| 45 |
|
| 46 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 47 |
|
| 48 |
|
| 49 |
struct Loaded { |
| 50 |
owner: String, |
| 51 |
is_owner: bool, |
| 52 |
repos: Vec<Repo>, |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
struct Repo { |
| 57 |
name: String, |
| 58 |
description: String, |
| 59 |
|
| 60 |
|
| 61 |
visibility: Option<String>, |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
pub fn screen(viewer: &super::Viewer, request: Request) -> Result<Response, RouteError> { |
| 66 |
|
| 67 |
|
| 68 |
let captures = request.captures; |
| 69 |
let owner = captures |
| 70 |
.get("owner") |
| 71 |
.ok_or_else(|| RouteError::not_found("no such account"))?; |
| 72 |
|
| 73 |
let loaded = load(viewer, owner)?; |
| 74 |
|
| 75 |
Ok(page_screen(&loaded).into()) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
fn load(viewer: &super::Viewer, owner: &str) -> Result<Loaded, RouteError> { |
| 80 |
let missing = || RouteError::not_found("no such account"); |
| 81 |
|
| 82 |
let username = db::Username::new(owner).map_err(|_| missing())?; |
| 83 |
let db_user = viewer |
| 84 |
.block_on(db::users::get_user_by_username(&viewer.app.db, &username)) |
| 85 |
.map_err(|_| RouteError::internal("that account could not be read"))? |
| 86 |
.ok_or_else(missing)?; |
| 87 |
|
| 88 |
let is_owner = viewer.user.as_ref().is_some_and(|u| u.id == db_user.id); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
let repos = viewer |
| 94 |
.block_on(async { |
| 95 |
if is_owner { |
| 96 |
db::git_repos::get_repos_by_user(&viewer.app.db, db_user.id).await |
| 97 |
} else { |
| 98 |
db::git_repos::get_public_repos_by_user(&viewer.app.db, db_user.id).await |
| 99 |
} |
| 100 |
}) |
| 101 |
.map_err(|_| RouteError::internal("those repositories could not be read"))?; |
| 102 |
|
| 103 |
Ok(Loaded { |
| 104 |
owner: db_user.username.to_string(), |
| 105 |
is_owner, |
| 106 |
repos: repos |
| 107 |
.iter() |
| 108 |
.map(|repo| Repo { |
| 109 |
name: repo.name.clone(), |
| 110 |
description: repo.description.clone(), |
| 111 |
visibility: (is_owner && repo.visibility != db::Visibility::Public) |
| 112 |
.then(|| repo.visibility.to_string()), |
| 113 |
}) |
| 114 |
.collect(), |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
fn page_screen(loaded: &Loaded) -> Described { |
| 120 |
let heading = format!("{}'s Repositories", loaded.owner); |
| 121 |
|
| 122 |
let mut page = Slot::new(PAGE_REGION, RegionKind::Pane).with(Node::page(heading.clone())); |
| 123 |
|
| 124 |
page = if loaded.repos.is_empty() { |
| 125 |
empty(page, loaded.is_owner, &loaded.owner) |
| 126 |
} else { |
| 127 |
page.with(listing(loaded)) |
| 128 |
}; |
| 129 |
|
| 130 |
Described::single(format!("{heading} - Git - Makenotwork")) |
| 131 |
.measured(MEASURE) |
| 132 |
.documented(Document::default().classed(crate::shell::body_class(MEASURE, &[]))) |
| 133 |
.with(page) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
fn empty(page: Slot, is_owner: bool, owner: &str) -> Slot { |
| 142 |
let page = page.with(Node::empty("No repositories yet.")); |
| 143 |
|
| 144 |
if !is_owner { |
| 145 |
return page; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
page.with(super::own_prose(format!( |
| 154 |
"Push a new repository:\n\ |
| 155 |
\n\ |
| 156 |
```\n\ |
| 157 |
git remote add origin https://makenot.work/git/{owner}/my-repo.git\n\ |
| 158 |
git push -u origin main\n\ |
| 159 |
```" |
| 160 |
))) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
fn listing(loaded: &Loaded) -> Node { |
| 169 |
let mut columns = vec![ |
| 170 |
Column::new("Repository") |
| 171 |
.width(layout::Width::Content) |
| 172 |
.priority(layout::Priority::Essential), |
| 173 |
Column::new("Description").width(layout::Width::Fill), |
| 174 |
]; |
| 175 |
|
| 176 |
|
| 177 |
if loaded.is_owner { |
| 178 |
columns.push(Column::new("Visibility").width(layout::Width::Content)); |
| 179 |
} |
| 180 |
|
| 181 |
Node::Table { |
| 182 |
columns, |
| 183 |
rows: loaded |
| 184 |
.repos |
| 185 |
.iter() |
| 186 |
.map(|repo| { |
| 187 |
let mut cells = vec![ |
| 188 |
Cell::new(repo.name.clone()), |
| 189 |
Cell::new(repo.description.clone()), |
| 190 |
]; |
| 191 |
if loaded.is_owner { |
| 192 |
cells.push(match repo.visibility.as_deref() { |
| 193 |
Some(visibility) => Cell::new(String::new()).token(Tag::badge(visibility)), |
| 194 |
None => Cell::new(String::new()), |
| 195 |
}); |
| 196 |
} |
| 197 |
Cells::new(cells).activate( |
| 198 |
Action::get(format!("/git/{}/{}", loaded.owner, repo.name)).navigating(), |
| 199 |
) |
| 200 |
}) |
| 201 |
.collect(), |
| 202 |
more: None, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
#[must_use] |
| 208 |
pub fn renderer(viewer: &super::Viewer) -> Webview { |
| 209 |
Webview::new().with_shell(viewer.document_shell().with_body_first(format!( |
| 210 |
"{}{}", |
| 211 |
crate::shell::skip_link(PAGE_REGION), |
| 212 |
crate::shell::site_header(viewer.user.as_ref()), |
| 213 |
))) |
| 214 |
} |
| 215 |
|
| 216 |
#[cfg(test)] |
| 217 |
mod tests { |
| 218 |
use super::*; |
| 219 |
|
| 220 |
fn loaded(count: usize, is_owner: bool) -> Loaded { |
| 221 |
Loaded { |
| 222 |
owner: "ada".into(), |
| 223 |
is_owner, |
| 224 |
repos: (0..count) |
| 225 |
.map(|n| Repo { |
| 226 |
name: format!("repo{n}"), |
| 227 |
description: format!("Number {n}"), |
| 228 |
visibility: (is_owner && n == 0).then(|| "private".to_string()), |
| 229 |
}) |
| 230 |
.collect(), |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
fn html(loaded: &Loaded) -> String { |
| 235 |
use quasi_axum::Serves as _; |
| 236 |
|
| 237 |
Webview::new().screen(&page_screen(loaded)) |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
#[test] |
| 243 |
fn the_document_carries_the_class_the_template_carried() { |
| 244 |
let screen = page_screen(&loaded(1, false)); |
| 245 |
|
| 246 |
assert_eq!(screen.document.body_class.as_deref(), Some("padded-page")); |
| 247 |
let rendered = html(&loaded(1, false)); |
| 248 |
assert!(rendered.contains("class=\"padded-page\""), "{rendered}"); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
#[test] |
| 253 |
fn the_document_is_titled_for_the_account() { |
| 254 |
let screen = page_screen(&loaded(1, false)); |
| 255 |
|
| 256 |
assert_eq!(screen.title, "ada's Repositories - Git - Makenotwork"); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
#[test] |
| 261 |
fn every_repository_is_a_row_that_opens_it() { |
| 262 |
let html = html(&loaded(3, false)); |
| 263 |
|
| 264 |
for n in 0..3 { |
| 265 |
assert!(html.contains(&format!("repo{n}")), "{html}"); |
| 266 |
assert!(html.contains(&format!("/git/ada/repo{n}")), "{html}"); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#[test] |
| 273 |
fn a_visitor_is_shown_no_visibility_column() { |
| 274 |
assert!(!html(&loaded(2, false)).contains("Visibility")); |
| 275 |
assert!(html(&loaded(2, true)).contains("Visibility")); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
#[test] |
| 280 |
fn the_owner_sees_which_of_their_repositories_are_not_public() { |
| 281 |
let html = html(&loaded(2, true)); |
| 282 |
|
| 283 |
assert!(html.contains("private"), "{html}"); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
#[test] |
| 289 |
fn only_the_owner_is_told_how_to_push() { |
| 290 |
let owner = html(&loaded(0, true)); |
| 291 |
let visitor = html(&loaded(0, false)); |
| 292 |
|
| 293 |
assert!(owner.contains("git remote add origin"), "{owner}"); |
| 294 |
assert!( |
| 295 |
owner.contains("makenot.work/git/ada/my-repo.git"), |
| 296 |
"{owner}" |
| 297 |
); |
| 298 |
assert!(!visitor.contains("git remote add"), "{visitor}"); |
| 299 |
assert!(visitor.contains("No repositories yet"), "{visitor}"); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
#[test] |
| 304 |
fn the_page_spells_no_spinner() { |
| 305 |
let html = html(&loaded(2, true)); |
| 306 |
|
| 307 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 308 |
assert!(!html.contains(spelling), "{spelling} survives in {html}"); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|