| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
use makeover_layout as layout; |
| 38 |
use quasi_declare::declare; |
| 39 |
use quasi_router::{Document, Feed, FeedKind}; |
| 40 |
use quasi_webview::Webview; |
| 41 |
|
| 42 |
use crate::types::{Collection, CustomLink, Project, User}; |
| 43 |
|
| 44 |
|
| 45 |
pub const PAGE_REGION: &str = "user-profile"; |
| 46 |
|
| 47 |
|
| 48 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 49 |
|
| 50 |
|
| 51 |
#[must_use] |
| 52 |
pub fn feed_path(username: &str) -> String { |
| 53 |
format!("/u/{username}/rss") |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
pub struct Profile<'a> { |
| 62 |
|
| 63 |
pub user: &'a User, |
| 64 |
|
| 65 |
pub user_id: &'a str, |
| 66 |
|
| 67 |
|
| 68 |
pub host_url: &'a str, |
| 69 |
|
| 70 |
pub custom_links: &'a [CustomLink], |
| 71 |
|
| 72 |
pub projects: &'a [Project], |
| 73 |
|
| 74 |
pub collections: &'a [Collection], |
| 75 |
|
| 76 |
pub follower_count: i64, |
| 77 |
|
| 78 |
pub is_following: bool, |
| 79 |
|
| 80 |
pub is_own_profile: bool, |
| 81 |
|
| 82 |
pub signed_in: bool, |
| 83 |
|
| 84 |
pub paused: bool, |
| 85 |
|
| 86 |
pub tips_enabled: bool, |
| 87 |
} |
| 88 |
|
| 89 |
impl Profile<'_> { |
| 90 |
|
| 91 |
fn canonical(&self) -> String { |
| 92 |
format!("{}/u/{}", self.host_url, self.user.username) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
fn summary(&self) -> String { |
| 100 |
self.user |
| 101 |
.bio |
| 102 |
.as_deref() |
| 103 |
.filter(|bio| !bio.trim().is_empty()) |
| 104 |
.map_or_else(|| "Creator on Makenotwork".to_owned(), str::to_owned) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
fn has_bio(&self) -> bool { |
| 113 |
!self.bio().trim().is_empty() |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
fn bio(&self) -> &str { |
| 119 |
self.user.bio.as_deref().unwrap_or_default() |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
fn can_follow(&self) -> bool { |
| 128 |
self.signed_in && !self.is_own_profile |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
fn tip_offer(&self) -> super::tip::Offer<'_> { |
| 137 |
super::tip::Offer { |
| 138 |
creator_id: self.user_id, |
| 139 |
project_id: None, |
| 140 |
signed_in: self.signed_in, |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
fn image(&self) -> String { |
| 146 |
self.user |
| 147 |
.avatar_url |
| 148 |
.clone() |
| 149 |
.unwrap_or_else(|| format!("{}/static/images/og-card.png", self.host_url)) |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
fn structured_data(&self) -> String { |
| 159 |
let mut json = format!( |
| 160 |
"{{\"@context\":\"https://schema.org\",\"@type\":\"ProfilePage\",\ |
| 161 |
\"mainEntity\":{{\"@type\":\"Person\",\"name\":\"{}\",\"url\":\"{}\"", |
| 162 |
self.user.display_name_json(), |
| 163 |
crate::types::json_escape(&self.canonical()), |
| 164 |
); |
| 165 |
if self.user.bio.is_some() { |
| 166 |
use std::fmt::Write as _; |
| 167 |
let _ = write!(json, ",\"description\":\"{}\"", self.user.bio_json()); |
| 168 |
} |
| 169 |
json.push_str("}}"); |
| 170 |
format!("<script type=\"application/ld+json\">{json}</script>") |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
declare! { |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[must_use] |
| 181 |
pub shape screen(profile: &Profile<'_>, theme_css: &str) -> Screen; |
| 182 |
|
| 183 |
let name = profile.user.display_name_or_username(); |
| 184 |
let feed = feed_path(&profile.user.username); |
| 185 |
|
| 186 |
screen single "{name} - Makenotwork" { |
| 187 |
measured MEASURE; |
| 188 |
documented Document::default() |
| 189 |
.classed(crate::shell::body_class(MEASURE, &["user-page"])) |
| 190 |
.styled(theme_css.to_owned()); |
| 191 |
summarised profile.summary(); |
| 192 |
illustrated profile.image(); |
| 193 |
canonical_at profile.canonical(); |
| 194 |
about quasi_router::SocialKind::Profile; |
| 195 |
syndicating Feed::new(FeedKind::Rss, "{name} - RSS Feed", feed); |
| 196 |
|
| 197 |
region PAGE_REGION as Pane { |
| 198 |
|
| 199 |
|
| 200 |
banner layout::Tone::Info |
| 201 |
"This creator is currently on break. Existing purchases remain accessible." |
| 202 |
when profile.paused; |
| 203 |
|
| 204 |
page name; |
| 205 |
text profile.user.username.clone(); |
| 206 |
text profile.bio() when profile.has_bio(); |
| 207 |
|
| 208 |
act "Edit Profile" to get "/dashboard?tab=settings" navigating |
| 209 |
when profile.is_own_profile; |
| 210 |
|
| 211 |
include super::follow::control( |
| 212 |
"user", |
| 213 |
profile.user_id, |
| 214 |
profile.is_following, |
| 215 |
profile.follower_count |
| 216 |
) when profile.can_follow(); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
for counted in super::follow::count_only(profile.follower_count).into_iter() { |
| 224 |
include counted unless profile.can_follow(); |
| 225 |
} |
| 226 |
|
| 227 |
include super::tip::control(&profile.tip_offer()) when profile.tips_enabled; |
| 228 |
|
| 229 |
act "RSS Feed" to get feed.clone() navigating; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
act "Copy link" to local { |
| 235 |
copying profile.canonical(); |
| 236 |
} |
| 237 |
|
| 238 |
list { |
| 239 |
for link in profile.custom_links.iter() { |
| 240 |
row link.title.clone() { |
| 241 |
secondary link.description.clone(); |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
activate to external link.url.clone(); |
| 247 |
} |
| 248 |
} |
| 249 |
} unless profile.custom_links.is_empty(); |
| 250 |
|
| 251 |
section "Projects" unless profile.projects.is_empty(); |
| 252 |
list { |
| 253 |
for project in profile.projects.iter() { |
| 254 |
row project.title.clone() { |
| 255 |
meta "{project.project_type} - {project.item_count} items"; |
| 256 |
activate to get "/p/{project.slug}" navigating; |
| 257 |
} |
| 258 |
} |
| 259 |
} unless profile.projects.is_empty(); |
| 260 |
|
| 261 |
section "Collections" unless profile.collections.is_empty(); |
| 262 |
list { |
| 263 |
for collection in profile.collections.iter() { |
| 264 |
row collection.title.clone() { |
| 265 |
meta "{collection.item_count} items"; |
| 266 |
activate to get |
| 267 |
"/c/{profile.user.username}/{collection.slug}" navigating; |
| 268 |
} |
| 269 |
} |
| 270 |
} unless profile.collections.is_empty(); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
link "Powered by Makenot.work" to get "/" navigating; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
#[must_use] |
| 282 |
pub fn renderer( |
| 283 |
user: Option<&crate::auth::SessionUser>, |
| 284 |
csrf: Option<&str>, |
| 285 |
structured_data: &str, |
| 286 |
) -> Webview { |
| 287 |
let csrf = csrf.unwrap_or_default(); |
| 288 |
Webview::new().with_shell( |
| 289 |
crate::shell::described() |
| 290 |
.sending("X-CSRF-Token", csrf) |
| 291 |
.with_body_last(crate::shell::body_last()) |
| 292 |
.with_body_first(format!( |
| 293 |
"{}{}", |
| 294 |
crate::shell::skip_link(PAGE_REGION), |
| 295 |
crate::shell::site_header(user) |
| 296 |
)) |
| 297 |
.with_head(format!( |
| 298 |
"<meta name=\"csrf-token\" content=\"{}\">{structured_data}", |
| 299 |
crate::helpers::escape_html(csrf) |
| 300 |
)), |
| 301 |
) |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
#[must_use] |
| 306 |
pub fn document( |
| 307 |
viewer: Option<&crate::auth::SessionUser>, |
| 308 |
csrf: Option<&str>, |
| 309 |
profile: &Profile<'_>, |
| 310 |
theme_css: &str, |
| 311 |
) -> String { |
| 312 |
use quasi_axum::Serves as _; |
| 313 |
|
| 314 |
let screen = screen(profile, theme_css); |
| 315 |
renderer(viewer, csrf, &profile.structured_data()).screen(&screen) |
| 316 |
} |
| 317 |
|
| 318 |
#[cfg(test)] |
| 319 |
mod tests { |
| 320 |
use super::*; |
| 321 |
|
| 322 |
fn creator() -> User { |
| 323 |
User { |
| 324 |
username: "maxj".to_owned(), |
| 325 |
email: "max@example.com".to_owned(), |
| 326 |
display_name: Some("Max Johnson".to_owned()), |
| 327 |
bio: Some("Writes things".to_owned()), |
| 328 |
avatar_initials: "MJ".to_owned(), |
| 329 |
avatar_url: None, |
| 330 |
stripe_connected: false, |
| 331 |
stripe_account_id: None, |
| 332 |
stripe_onboarding_complete: false, |
| 333 |
stripe_payouts_enabled: false, |
| 334 |
stripe_charges_enabled: false, |
| 335 |
stripe_tax_enabled: false, |
| 336 |
tips_enabled: false, |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
fn profile(user: &User) -> Profile<'_> { |
| 341 |
Profile { |
| 342 |
user, |
| 343 |
user_id: "u1", |
| 344 |
host_url: "https://makenot.work", |
| 345 |
custom_links: &[], |
| 346 |
projects: &[], |
| 347 |
collections: &[], |
| 348 |
follower_count: 0, |
| 349 |
is_following: false, |
| 350 |
is_own_profile: false, |
| 351 |
signed_in: false, |
| 352 |
paused: false, |
| 353 |
tips_enabled: false, |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
fn html(profile: &Profile<'_>) -> String { |
| 358 |
document(None, Some("t"), profile, ":root{--x:1}") |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
#[test] |
| 364 |
fn the_feed_is_declared_once_and_spelled_by_the_vocabulary() { |
| 365 |
let user = creator(); |
| 366 |
let screen = screen(&profile(&user), ""); |
| 367 |
let feed = screen.discovery.feed.as_ref().expect("declared"); |
| 368 |
assert_eq!(feed.href, "/u/maxj/rss"); |
| 369 |
assert_eq!(feed.title, "Max Johnson - RSS Feed"); |
| 370 |
|
| 371 |
let rendered = html(&profile(&user)); |
| 372 |
assert!( |
| 373 |
rendered.contains( |
| 374 |
"<link rel=\"alternate\" type=\"application/rss+xml\" \ |
| 375 |
title=\"Max Johnson - RSS Feed\" href=\"/u/maxj/rss\">" |
| 376 |
), |
| 377 |
"{rendered}" |
| 378 |
); |
| 379 |
assert_eq!( |
| 380 |
rendered.matches("rel=\"alternate\"").count(), |
| 381 |
1, |
| 382 |
"{rendered}" |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
#[test] |
| 389 |
fn the_feed_is_offered_to_a_reader_as_well_as_to_their_app() { |
| 390 |
let user = creator(); |
| 391 |
assert!(html(&profile(&user)).contains(">RSS Feed<")); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
#[test] |
| 399 |
fn the_creator_theme_outranks_the_site_stylesheets() { |
| 400 |
let user = creator(); |
| 401 |
let rendered = document(None, Some("t"), &profile(&user), ":root{--accent:red}"); |
| 402 |
let theme = rendered.find(":root{--accent:red}").expect("written"); |
| 403 |
let last_sheet = rendered.rfind("/static/style.css").expect("linked"); |
| 404 |
assert!(theme > last_sheet, "the theme landed before style.css"); |
| 405 |
|
| 406 |
|
| 407 |
assert!( |
| 408 |
rendered.contains("<style>:root{--accent:red}</style>"), |
| 409 |
"{rendered}" |
| 410 |
); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
#[test] |
| 416 |
fn the_page_still_describes_itself_to_a_crawler() { |
| 417 |
let user = creator(); |
| 418 |
let rendered = html(&profile(&user)); |
| 419 |
assert!(rendered.contains("application/ld+json"), "{rendered}"); |
| 420 |
assert!(rendered.contains("\"@type\":\"ProfilePage\""), "{rendered}"); |
| 421 |
assert!(rendered.contains("\"name\":\"Max Johnson\""), "{rendered}"); |
| 422 |
assert!( |
| 423 |
rendered.contains("\"url\":\"https://makenot.work/u/maxj\""), |
| 424 |
"{rendered}" |
| 425 |
); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
#[test] |
| 431 |
fn only_a_reader_who_could_follow_is_offered_the_control() { |
| 432 |
let user = creator(); |
| 433 |
|
| 434 |
let mut signed_out = profile(&user); |
| 435 |
signed_out.follower_count = 4; |
| 436 |
let rendered = html(&signed_out); |
| 437 |
assert!(!rendered.contains("/api/follow/user/u1"), "{rendered}"); |
| 438 |
assert!(rendered.contains("4 followers"), "{rendered}"); |
| 439 |
|
| 440 |
let mut own = profile(&user); |
| 441 |
own.signed_in = true; |
| 442 |
own.is_own_profile = true; |
| 443 |
let rendered = html(&own); |
| 444 |
assert!(!rendered.contains("/api/follow/user/u1"), "{rendered}"); |
| 445 |
assert!(rendered.contains("Edit Profile"), "{rendered}"); |
| 446 |
|
| 447 |
let mut other = profile(&user); |
| 448 |
other.signed_in = true; |
| 449 |
let rendered = html(&other); |
| 450 |
assert!(rendered.contains("/api/follow/user/u1"), "{rendered}"); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
#[test] |
| 455 |
fn a_profile_with_no_followers_says_nothing_about_it() { |
| 456 |
let user = creator(); |
| 457 |
assert!(!html(&profile(&user)).contains("followers")); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
#[test] |
| 463 |
fn the_share_control_copies_the_whole_address() { |
| 464 |
let user = creator(); |
| 465 |
let rendered = html(&profile(&user)); |
| 466 |
assert!( |
| 467 |
rendered.contains("data-copies=\"https://makenot.work/u/maxj\""), |
| 468 |
"{rendered}" |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
#[test] |
| 475 |
fn a_custom_link_opens_away_from_the_page() { |
| 476 |
let user = creator(); |
| 477 |
let links = vec![CustomLink { |
| 478 |
url: "https://example.com".to_owned(), |
| 479 |
title: "My shop".to_owned(), |
| 480 |
description: "Elsewhere".to_owned(), |
| 481 |
}]; |
| 482 |
let mut with_links = profile(&user); |
| 483 |
with_links.custom_links = &links; |
| 484 |
let rendered = html(&with_links); |
| 485 |
assert!(rendered.contains("https://example.com"), "{rendered}"); |
| 486 |
assert!(rendered.contains("target=\"_blank\""), "{rendered}"); |
| 487 |
assert!(rendered.contains("noopener"), "{rendered}"); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
#[test] |
| 492 |
fn the_published_work_keeps_its_rows_and_its_addresses() { |
| 493 |
let user = creator(); |
| 494 |
let projects = vec![Project { |
| 495 |
id: "p1".to_owned(), |
| 496 |
slug: "blue-hour".to_owned(), |
| 497 |
title: "Blue Hour".to_owned(), |
| 498 |
description: String::new(), |
| 499 |
item_count: 3, |
| 500 |
project_type: "Music".to_owned(), |
| 501 |
cover_image_url: None, |
| 502 |
}]; |
| 503 |
let collections = vec![Collection { |
| 504 |
id: "c1".to_owned(), |
| 505 |
slug: "favourites".to_owned(), |
| 506 |
title: "Favourites".to_owned(), |
| 507 |
description: None, |
| 508 |
is_public: true, |
| 509 |
item_count: 2, |
| 510 |
created_at: String::new(), |
| 511 |
}]; |
| 512 |
let mut full = profile(&user); |
| 513 |
full.projects = &projects; |
| 514 |
full.collections = &collections; |
| 515 |
|
| 516 |
let rendered = html(&full); |
| 517 |
assert!(rendered.contains("Blue Hour"), "{rendered}"); |
| 518 |
assert!(rendered.contains("/p/blue-hour"), "{rendered}"); |
| 519 |
assert!(rendered.contains("3 items"), "{rendered}"); |
| 520 |
assert!(rendered.contains("Favourites"), "{rendered}"); |
| 521 |
assert!(rendered.contains("/c/maxj/favourites"), "{rendered}"); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
#[test] |
| 527 |
fn a_paused_creator_says_so_before_anything_they_published() { |
| 528 |
let user = creator(); |
| 529 |
let mut paused = profile(&user); |
| 530 |
paused.paused = true; |
| 531 |
let rendered = html(&paused); |
| 532 |
let notice = rendered.find("currently on break").expect("said"); |
| 533 |
let name = rendered.rfind("Max Johnson").expect("drawn"); |
| 534 |
assert!(notice < name, "the notice landed under the work"); |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
#[test] |
| 540 |
fn a_creator_with_no_bio_still_previews() { |
| 541 |
let mut user = creator(); |
| 542 |
user.bio = None; |
| 543 |
let rendered = html(&profile(&user)); |
| 544 |
assert!( |
| 545 |
rendered.contains("content=\"Creator on Makenotwork\""), |
| 546 |
"{rendered}" |
| 547 |
); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
#[test] |
| 552 |
fn the_page_spells_no_spinner() { |
| 553 |
let user = creator(); |
| 554 |
let rendered = html(&profile(&user)); |
| 555 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 556 |
assert!(!rendered.contains(spelling), "{spelling} survives"); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|