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