| 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 |
use makeover_layout as layout; |
| 31 |
use quasi_router::screen::{Act, Cell, Cells, Column}; |
| 32 |
use quasi_router::{Action, Method, Node, RegionKind, Request, Response, RouteError, Slot}; |
| 33 |
use quasi_webview::Webview; |
| 34 |
|
| 35 |
use super::Viewer; |
| 36 |
use crate::db; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub const SCREEN: &str = "library_contacts"; |
| 41 |
|
| 42 |
|
| 43 |
pub const PATH: &str = "/library/tabs/contacts"; |
| 44 |
|
| 45 |
|
| 46 |
pub const REGION: &str = "library-contacts"; |
| 47 |
|
| 48 |
|
| 49 |
const REVOKE: &str = "/revoke/{seller_id}"; |
| 50 |
|
| 51 |
|
| 52 |
pub const WRITES: &[(Method, &str, super::Screen)] = &[(Method::Delete, REVOKE, revoke)]; |
| 53 |
|
| 54 |
|
| 55 |
pub struct BuyerView { |
| 56 |
username: String, |
| 57 |
email: String, |
| 58 |
purchases: String, |
| 59 |
spent: String, |
| 60 |
last_purchase: String, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
pub struct SharedView { |
| 65 |
seller_id: String, |
| 66 |
username: String, |
| 67 |
name: String, |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
pub fn screen(viewer: &Viewer, _request: Request) -> Result<Response, RouteError> { |
| 72 |
let user_id = viewer.user.id; |
| 73 |
|
| 74 |
let shared = viewer |
| 75 |
.block_on(db::transactions::get_shared_creators( |
| 76 |
&viewer.app.db, |
| 77 |
user_id, |
| 78 |
)) |
| 79 |
.map_err(|_| RouteError::internal("your contacts could not be read"))?; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
let profile = viewer |
| 86 |
.block_on(db::users::get_user_by_id(&viewer.app.db, user_id)) |
| 87 |
.map_err(|_| RouteError::internal("your account could not be read"))? |
| 88 |
.ok_or_else(|| RouteError::not_found("that account is gone"))?; |
| 89 |
let buyers = if profile.can_create_projects { |
| 90 |
viewer |
| 91 |
.block_on(db::transactions::get_seller_contacts( |
| 92 |
&viewer.app.db, |
| 93 |
user_id, |
| 94 |
)) |
| 95 |
.map_err(|_| RouteError::internal("your buyers could not be read"))? |
| 96 |
} else { |
| 97 |
Vec::new() |
| 98 |
}; |
| 99 |
|
| 100 |
let buyers: Vec<BuyerView> = buyers |
| 101 |
.into_iter() |
| 102 |
.map(|c| BuyerView { |
| 103 |
username: c.username, |
| 104 |
email: c.email, |
| 105 |
purchases: c.total_purchases.to_string(), |
| 106 |
spent: crate::formatting::format_revenue( |
| 107 |
c.total_spent_cents, |
| 108 |
viewer.user.settlement_currency, |
| 109 |
), |
| 110 |
last_purchase: c.last_purchase_at.format("%b %d, %Y").to_string(), |
| 111 |
}) |
| 112 |
.collect(); |
| 113 |
let shared: Vec<SharedView> = shared |
| 114 |
.into_iter() |
| 115 |
.map(|creator| SharedView { |
| 116 |
seller_id: creator.seller_id.to_string(), |
| 117 |
name: creator |
| 118 |
.display_name |
| 119 |
.clone() |
| 120 |
.unwrap_or_else(|| creator.username.clone()), |
| 121 |
username: creator.username, |
| 122 |
}) |
| 123 |
.collect(); |
| 124 |
|
| 125 |
Ok(Response::fragment(REGION, pane(&buyers, &shared))) |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
pub fn revoke(viewer: &Viewer, request: Request) -> Result<Response, RouteError> { |
| 137 |
|
| 138 |
let captures = request.captures; |
| 139 |
let seller: crate::db::UserId = captures |
| 140 |
.get("seller_id") |
| 141 |
.and_then(|id| id.parse().ok()) |
| 142 |
.ok_or_else(|| RouteError::not_found("no such creator"))?; |
| 143 |
|
| 144 |
viewer |
| 145 |
.block_on(db::transactions::revoke_contact_sharing( |
| 146 |
&viewer.app.db, |
| 147 |
viewer.user.id, |
| 148 |
seller, |
| 149 |
)) |
| 150 |
.map_err(|_| RouteError::internal("that sharing could not be revoked"))?; |
| 151 |
|
| 152 |
screen(viewer, Request::get(PATH)) |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
fn pane(buyers: &[BuyerView], shared: &[SharedView]) -> Node { |
| 160 |
let mut slot = Slot::new(REGION, RegionKind::Pane); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if buyers.is_empty() && shared.is_empty() { |
| 165 |
return Node::Region(slot.with(Node::empty("No contacts yet."))); |
| 166 |
} |
| 167 |
|
| 168 |
if !buyers.is_empty() { |
| 169 |
slot = slot |
| 170 |
.with(Node::section(format!("Your Buyers ({})", buyers.len()))) |
| 171 |
.with(Node::text( |
| 172 |
"Buyers who opted to share their email with you at purchase time.", |
| 173 |
)) |
| 174 |
.with(buyers_table(buyers)); |
| 175 |
} |
| 176 |
|
| 177 |
if !shared.is_empty() { |
| 178 |
slot = slot |
| 179 |
.with(Node::section("Shared With")) |
| 180 |
.with(Node::text( |
| 181 |
"You've shared your email with these creators. You can revoke sharing at any time.", |
| 182 |
)) |
| 183 |
.with(shared_table(shared)); |
| 184 |
} |
| 185 |
|
| 186 |
Node::Region(slot) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
fn buyers_table(buyers: &[BuyerView]) -> Node { |
| 191 |
Node::Table { |
| 192 |
columns: vec![ |
| 193 |
Column::new("Username") |
| 194 |
.width(layout::Width::Content) |
| 195 |
.priority(layout::Priority::Essential), |
| 196 |
Column::new("Email") |
| 197 |
.width(layout::Width::Fill) |
| 198 |
.priority(layout::Priority::Essential), |
| 199 |
Column::new("Purchases").width(layout::Width::Content), |
| 200 |
Column::new("Total Spent").width(layout::Width::Content), |
| 201 |
Column::new("Last Purchase") |
| 202 |
.width(layout::Width::Content) |
| 203 |
.priority(layout::Priority::Optional), |
| 204 |
], |
| 205 |
rows: buyers |
| 206 |
.iter() |
| 207 |
.map(|buyer| { |
| 208 |
Cells::new([ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
Cell::new(buyer.username.clone()) |
| 218 |
.activate(Action::get(format!("/u/{}", buyer.username)).navigating()), |
| 219 |
Cell::new(buyer.email.clone()) |
| 220 |
.activate(Action::external(format!("mailto:{}", buyer.email))), |
| 221 |
Cell::new(buyer.purchases.clone()), |
| 222 |
Cell::new(buyer.spent.clone()), |
| 223 |
Cell::new(buyer.last_purchase.clone()), |
| 224 |
]) |
| 225 |
}) |
| 226 |
.collect(), |
| 227 |
|
| 228 |
|
| 229 |
more: None, |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
fn shared_table(shared: &[SharedView]) -> Node { |
| 235 |
Node::Table { |
| 236 |
columns: vec![ |
| 237 |
Column::new("Creator") |
| 238 |
.width(layout::Width::Fill) |
| 239 |
.priority(layout::Priority::Essential), |
| 240 |
Column::new("") |
| 241 |
.width(layout::Width::Content) |
| 242 |
.priority(layout::Priority::Essential), |
| 243 |
], |
| 244 |
rows: shared |
| 245 |
.iter() |
| 246 |
.map(|creator| { |
| 247 |
Cells::new([ |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
Cell::new(creator.name.clone()) |
| 252 |
.activate(Action::get(format!("/u/{}", creator.username)).navigating()), |
| 253 |
Cell::acts([Act::new( |
| 254 |
"Revoke", |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
Action::delete(format!("{PATH}/revoke/{}", creator.seller_id)).awaiting(), |
| 259 |
) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
.confirm(format!("Revoke contact sharing with {}?", creator.username)) |
| 264 |
.tone(layout::Tone::Danger)]), |
| 265 |
]) |
| 266 |
}) |
| 267 |
.collect(), |
| 268 |
|
| 269 |
|
| 270 |
more: None, |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
pub fn renderer(viewer: &Viewer) -> Webview { |
| 276 |
Webview::new().with_shell(viewer.shell()) |
| 277 |
} |
| 278 |
|
| 279 |
#[cfg(test)] |
| 280 |
mod tests { |
| 281 |
use super::*; |
| 282 |
use quasi_axum::Serves; |
| 283 |
|
| 284 |
fn buyer(username: &str) -> BuyerView { |
| 285 |
BuyerView { |
| 286 |
username: username.into(), |
| 287 |
email: format!("{username}@example.com"), |
| 288 |
purchases: "3".into(), |
| 289 |
spent: "$42.00".into(), |
| 290 |
last_purchase: "Aug 10, 2026".into(), |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
fn creator(id: &str, username: &str, name: &str) -> SharedView { |
| 295 |
SharedView { |
| 296 |
seller_id: id.into(), |
| 297 |
username: username.into(), |
| 298 |
name: name.into(), |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
fn render(node: &Node) -> String { |
| 303 |
Webview::new().fragment(node) |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn the_region_matches_the_panel_the_library_strip_gives_this_tab() { |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
let strip = crate::quasi::library_tabs::html("", true, true); |
| 318 |
assert!( |
| 319 |
strip.contains(&format!("id=\"{REGION}\"")), |
| 320 |
"the library strip has no panel called {REGION}:\n{strip}" |
| 321 |
); |
| 322 |
assert!(strip.contains(&format!("hx-get=\"{PATH}\"")), "{strip}"); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn a_reader_with_no_contacts_gets_one_sentence_and_no_tables() { |
| 327 |
let html = render(&pane(&[], &[])); |
| 328 |
assert!(html.contains("No contacts yet.")); |
| 329 |
assert!(!html.contains("role=\"table\""), "{html}"); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn each_table_appears_only_when_it_has_rows() { |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
let buyers_only = render(&pane(&[buyer("ada")], &[])); |
| 338 |
assert!(buyers_only.contains("Your Buyers (1)")); |
| 339 |
assert!(!buyers_only.contains("Shared With")); |
| 340 |
|
| 341 |
let shared_only = render(&pane(&[], &[creator("s1", "grace", "Grace H")])); |
| 342 |
assert!(!shared_only.contains("Your Buyers")); |
| 343 |
assert!(shared_only.contains("Shared With")); |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
fn a_buyers_name_goes_to_their_profile_and_their_address_leaves() { |
| 348 |
let html = render(&buyers_table(&[buyer("ada")])); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
assert!(html.contains("href=\"/u/ada\""), "{html}"); |
| 355 |
assert!( |
| 356 |
!html.contains("hx-get=\"/u/ada\""), |
| 357 |
"a navigation carries no verb: {html}" |
| 358 |
); |
| 359 |
assert!( |
| 360 |
!html.contains("hx-get=\"mailto"), |
| 361 |
"no htmx on a mailto: {html}" |
| 362 |
); |
| 363 |
assert!( |
| 364 |
html.contains("href=\"mailto:ada@example.com\""), |
| 365 |
"the address is a link: {html}" |
| 366 |
); |
| 367 |
assert!(html.contains("target=\"_blank\""), "{html}"); |
| 368 |
} |
| 369 |
|
| 370 |
#[test] |
| 371 |
fn revoking_asks_first_and_every_row_asks_about_itself() { |
| 372 |
let html = render(&shared_table(&[ |
| 373 |
creator("s1", "grace", "Grace H"), |
| 374 |
creator("s2", "alan", "Alan T"), |
| 375 |
])); |
| 376 |
|
| 377 |
assert_eq!(html.matches("hx-confirm").count(), 2, "both ask: {html}"); |
| 378 |
assert!( |
| 379 |
html.contains("Revoke contact sharing with grace?"), |
| 380 |
"{html}" |
| 381 |
); |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
assert_eq!( |
| 387 |
html.matches(r#"data-tone="danger""#).count(), |
| 388 |
2, |
| 389 |
"revoking is destructive and says so: {html}" |
| 390 |
); |
| 391 |
|
| 392 |
|
| 393 |
assert!( |
| 394 |
html.contains(&format!("hx-delete=\"{PATH}/revoke/s1\"")), |
| 395 |
"{html}" |
| 396 |
); |
| 397 |
assert!( |
| 398 |
html.contains(&format!("hx-delete=\"{PATH}/revoke/s2\"")), |
| 399 |
"{html}" |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
#[test] |
| 404 |
fn the_row_addresses_are_the_ones_the_api_actually_answers() { |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
assert!( |
| 412 |
WRITES |
| 413 |
.iter() |
| 414 |
.any(|(method, path, _)| *method == Method::Delete && *path == REVOKE), |
| 415 |
"the revoke route is registered" |
| 416 |
); |
| 417 |
let control = render(&shared_table(&[creator("s1", "grace", "Grace H")])); |
| 418 |
assert!( |
| 419 |
control.contains(&format!("hx-delete=\"{PATH}/revoke/s1\"")), |
| 420 |
"{control}" |
| 421 |
); |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
let pages = include_str!("../routes/pages/public/mod.rs"); |
| 427 |
assert!( |
| 428 |
pages.contains("\"/u/{username}\""), |
| 429 |
"a buyer's name goes to a registered route" |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn a_name_a_reader_chose_cannot_smuggle_markup() { |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
let html = render(&shared_table(&[creator( |
| 439 |
"s1", |
| 440 |
"grace", |
| 441 |
"<script>x()</script>", |
| 442 |
)])); |
| 443 |
assert!(!html.contains("<script>x()"), "{html}"); |
| 444 |
} |
| 445 |
} |
| 446 |
|