| 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_declare::declare; |
| 33 |
use quasi_router::screen::Tag; |
| 34 |
use quasi_router::{Document, Request, Response, RouteError}; |
| 35 |
use quasi_webview::Webview; |
| 36 |
|
| 37 |
use crate::db; |
| 38 |
|
| 39 |
|
| 40 |
pub const PATH: &str = "/git/{owner}"; |
| 41 |
|
| 42 |
|
| 43 |
pub const PAGE_REGION: &str = "git-repos"; |
| 44 |
|
| 45 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 46 |
|
| 47 |
|
| 48 |
const EMPTY: &str = "git-repos-empty"; |
| 49 |
|
| 50 |
|
| 51 |
pub(crate) struct Loaded { |
| 52 |
owner: String, |
| 53 |
is_owner: bool, |
| 54 |
repos: Vec<Repo>, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
pub(crate) struct Repo { |
| 59 |
name: String, |
| 60 |
description: String, |
| 61 |
|
| 62 |
|
| 63 |
visibility: Option<String>, |
| 64 |
} |
| 65 |
|
| 66 |
impl Repo { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
fn visibility_label(&self) -> &str { |
| 72 |
self.visibility.as_deref().unwrap_or_default() |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
pub fn screen(viewer: &super::Viewer, request: Request) -> Result<Response, RouteError> { |
| 78 |
|
| 79 |
|
| 80 |
let captures = request.captures; |
| 81 |
let owner = captures |
| 82 |
.get("owner") |
| 83 |
.ok_or_else(|| RouteError::not_found("no such account"))?; |
| 84 |
|
| 85 |
let loaded = load(viewer, owner)?; |
| 86 |
|
| 87 |
Ok(page_screen(&loaded).into()) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
pub(crate) fn reading( |
| 92 |
viewer: &super::Viewer, |
| 93 |
carried: &super::Carried, |
| 94 |
) -> Result<Loaded, RouteError> { |
| 95 |
load(viewer, carried.capture("owner")?) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
fn load(viewer: &super::Viewer, owner: &str) -> Result<Loaded, RouteError> { |
| 100 |
let missing = || RouteError::not_found("no such account"); |
| 101 |
|
| 102 |
let username = db::Username::new(owner).map_err(|_| missing())?; |
| 103 |
let db_user = viewer |
| 104 |
.block_on(db::users::get_user_by_username(&viewer.app.db, &username)) |
| 105 |
.map_err(|_| RouteError::internal("that account could not be read"))? |
| 106 |
.ok_or_else(missing)?; |
| 107 |
|
| 108 |
let is_owner = viewer.user.as_ref().is_some_and(|u| u.id == db_user.id); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
let repos = viewer |
| 114 |
.block_on(async { |
| 115 |
if is_owner { |
| 116 |
db::git_repos::get_repos_by_user(&viewer.app.db, db_user.id).await |
| 117 |
} else { |
| 118 |
db::git_repos::get_public_repos_by_user(&viewer.app.db, db_user.id).await |
| 119 |
} |
| 120 |
}) |
| 121 |
.map_err(|_| RouteError::internal("those repositories could not be read"))?; |
| 122 |
|
| 123 |
Ok(Loaded { |
| 124 |
owner: db_user.username.to_string(), |
| 125 |
is_owner, |
| 126 |
repos: repos |
| 127 |
.iter() |
| 128 |
.map(|repo| Repo { |
| 129 |
name: repo.name.clone(), |
| 130 |
description: repo.description.clone(), |
| 131 |
visibility: (is_owner && repo.visibility != db::Visibility::Public) |
| 132 |
.then(|| repo.visibility.to_string()), |
| 133 |
}) |
| 134 |
.collect(), |
| 135 |
}) |
| 136 |
} |
| 137 |
|
| 138 |
declare! { |
| 139 |
|
| 140 |
pub(crate) shape page_screen(loaded: &Loaded) -> Screen; |
| 141 |
|
| 142 |
let heading = "{loaded.owner}'s Repositories"; |
| 143 |
|
| 144 |
screen single "{heading} - Git - Makenotwork" { |
| 145 |
measured MEASURE; |
| 146 |
documented Document::default().classed(crate::shell::body_class(MEASURE, &[])); |
| 147 |
|
| 148 |
include page_region(loaded); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
declare! { |
| 153 |
|
| 154 |
#[staged] |
| 155 |
pub(crate) shape page_region(loaded: &Loaded) -> Slot; |
| 156 |
|
| 157 |
region PAGE_REGION as Pane { |
| 158 |
page "{loaded.owner}'s Repositories"; |
| 159 |
include empty(loaded.is_owner, &loaded.owner) when loaded.repos.is_empty(); |
| 160 |
include owner_listing(loaded) when loaded.is_owner and not loaded.repos.is_empty(); |
| 161 |
include listing(loaded) unless loaded.is_owner or loaded.repos.is_empty(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
declare! { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
#[staged] |
| 185 |
shape empty(is_owner: bool, owner: &str) -> Slot; |
| 186 |
|
| 187 |
region EMPTY as Group { |
| 188 |
empty "No repositories yet."; |
| 189 |
rich "Push a new repository:\n\n```\ngit remote add origin \ |
| 190 |
https://makenot.work/git/{owner}/my-repo.git\ngit push -u origin main\n```" |
| 191 |
when is_owner |
| 192 |
{ |
| 193 |
trust quasi_router::Trust::Trusted; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
declare! { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
#[staged] |
| 226 |
shape owner_listing(loaded: &Loaded) -> Node; |
| 227 |
|
| 228 |
table { |
| 229 |
column "Repository" { |
| 230 |
width Content; |
| 231 |
priority Essential; |
| 232 |
} |
| 233 |
column "Description" { |
| 234 |
width Fill; |
| 235 |
} |
| 236 |
column "Visibility" { |
| 237 |
width Content; |
| 238 |
} |
| 239 |
|
| 240 |
for repo in loaded.repos.iter() { |
| 241 |
cells { |
| 242 |
cell at "Repository" repo.name.clone(); |
| 243 |
cell at "Description" repo.description.clone(); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
cell at "Visibility" "" when repo.visibility.is_some() { |
| 251 |
token Tag::badge(repo.visibility_label()); |
| 252 |
} |
| 253 |
|
| 254 |
activate to get "/git/{loaded.owner}/{repo.name}" navigating; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
declare! { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#[staged] |
| 265 |
shape listing(loaded: &Loaded) -> Node; |
| 266 |
|
| 267 |
table { |
| 268 |
column "Repository" { |
| 269 |
width Content; |
| 270 |
priority Essential; |
| 271 |
} |
| 272 |
column "Description" { |
| 273 |
width Fill; |
| 274 |
} |
| 275 |
|
| 276 |
for repo in loaded.repos.iter() { |
| 277 |
cells { |
| 278 |
cell at "Repository" repo.name.clone(); |
| 279 |
cell at "Description" repo.description.clone(); |
| 280 |
|
| 281 |
activate to get "/git/{loaded.owner}/{repo.name}" navigating; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
#[must_use] |
| 289 |
pub fn renderer(viewer: &super::Viewer) -> Webview { |
| 290 |
Webview::new().with_shell(viewer.document_shell().with_body_first(format!( |
| 291 |
"{}{}", |
| 292 |
crate::shell::skip_link(PAGE_REGION), |
| 293 |
crate::shell::site_header(viewer.user.as_ref()), |
| 294 |
))) |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#[cfg(test)] |
| 302 |
pub(crate) fn sample(count: usize, is_owner: bool) -> Loaded { |
| 303 |
Loaded { |
| 304 |
owner: "ada".into(), |
| 305 |
is_owner, |
| 306 |
repos: (0..count) |
| 307 |
.map(|n| Repo { |
| 308 |
name: format!("repo{n}"), |
| 309 |
description: format!("Number {n}"), |
| 310 |
visibility: (is_owner && n == 0).then(|| "private".to_string()), |
| 311 |
}) |
| 312 |
.collect(), |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
#[cfg(test)] |
| 317 |
mod tests { |
| 318 |
use super::*; |
| 319 |
|
| 320 |
use super::sample as loaded; |
| 321 |
|
| 322 |
fn html(loaded: &Loaded) -> String { |
| 323 |
use quasi_axum::Serves as _; |
| 324 |
|
| 325 |
Webview::new().screen(&page_screen(loaded)) |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
#[test] |
| 331 |
fn the_document_carries_the_class_the_template_carried() { |
| 332 |
let screen = page_screen(&loaded(1, false)); |
| 333 |
|
| 334 |
assert_eq!(screen.document.body_class.as_deref(), Some("padded-page")); |
| 335 |
let rendered = html(&loaded(1, false)); |
| 336 |
assert!(rendered.contains("class=\"padded-page\""), "{rendered}"); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
#[test] |
| 341 |
fn the_document_is_titled_for_the_account() { |
| 342 |
let screen = page_screen(&loaded(1, false)); |
| 343 |
|
| 344 |
assert_eq!(screen.title, "ada's Repositories - Git - Makenotwork"); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
#[test] |
| 349 |
fn every_repository_is_a_row_that_opens_it() { |
| 350 |
let html = html(&loaded(3, false)); |
| 351 |
|
| 352 |
for n in 0..3 { |
| 353 |
assert!(html.contains(&format!("repo{n}")), "{html}"); |
| 354 |
assert!(html.contains(&format!("/git/ada/repo{n}")), "{html}"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
#[test] |
| 361 |
fn a_visitor_is_shown_no_visibility_column() { |
| 362 |
assert!(!html(&loaded(2, false)).contains("Visibility")); |
| 363 |
assert!(html(&loaded(2, true)).contains("Visibility")); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
#[test] |
| 368 |
fn the_owner_sees_which_of_their_repositories_are_not_public() { |
| 369 |
let html = html(&loaded(2, true)); |
| 370 |
|
| 371 |
assert!(html.contains("private"), "{html}"); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
#[test] |
| 377 |
fn only_the_owner_is_told_how_to_push() { |
| 378 |
let owner = html(&loaded(0, true)); |
| 379 |
let visitor = html(&loaded(0, false)); |
| 380 |
|
| 381 |
assert!(owner.contains("git remote add origin"), "{owner}"); |
| 382 |
assert!( |
| 383 |
owner.contains("makenot.work/git/ada/my-repo.git"), |
| 384 |
"{owner}" |
| 385 |
); |
| 386 |
assert!(!visitor.contains("git remote add"), "{visitor}"); |
| 387 |
assert!(visitor.contains("No repositories yet"), "{visitor}"); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
#[test] |
| 392 |
fn the_page_spells_no_spinner() { |
| 393 |
let html = html(&loaded(2, true)); |
| 394 |
|
| 395 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 396 |
assert!(!html.contains(spelling), "{spelling} survives in {html}"); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|