| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
use makeover_layout as layout; |
| 55 |
use quasi_declare::declare; |
| 56 |
use quasi_router::screen::Figure; |
| 57 |
use quasi_router::{Document, Request, Response, RouteError}; |
| 58 |
use quasi_webview::Webview; |
| 59 |
|
| 60 |
use crate::db; |
| 61 |
|
| 62 |
|
| 63 |
pub const PATH: &str = "/fan-plus"; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
const SUBSCRIBE: &str = "/stripe/fan-plus"; |
| 69 |
|
| 70 |
|
| 71 |
pub const PAGE_REGION: &str = "fan-plus"; |
| 72 |
|
| 73 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 74 |
|
| 75 |
|
| 76 |
const PRICE: &str = "$8"; |
| 77 |
|
| 78 |
|
| 79 |
const MEMBERSHIP: &str = "fan-plus-membership"; |
| 80 |
|
| 81 |
|
| 82 |
const PITCH: &str = "fan-plus-pitch"; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub(crate) enum Standing { |
| 90 |
|
| 91 |
Visitor, |
| 92 |
|
| 93 |
Unsubscribed, |
| 94 |
|
| 95 |
|
| 96 |
Member { period_end: Option<String> }, |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
impl Standing { |
| 104 |
|
| 105 |
const fn is_member(&self) -> bool { |
| 106 |
matches!(self, Self::Member { .. }) |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
const fn is_unsubscribed(&self) -> bool { |
| 111 |
matches!(self, Self::Unsubscribed) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
const fn is_visitor(&self) -> bool { |
| 116 |
matches!(self, Self::Visitor) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
fn period_end(&self) -> &str { |
| 124 |
match self { |
| 125 |
Self::Member { |
| 126 |
period_end: Some(end), |
| 127 |
} => end, |
| 128 |
_ => "", |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
pub fn screen(viewer: &super::Viewer, request: Request) -> Result<Response, RouteError> { |
| 141 |
|
| 142 |
|
| 143 |
let carried = request.carried; |
| 144 |
let just_subscribed = carried |
| 145 |
.get("subscribed") |
| 146 |
.is_some_and(|value| value.trim() == "true"); |
| 147 |
|
| 148 |
let standing = standing(viewer)?; |
| 149 |
|
| 150 |
Ok(page_screen(&standing, just_subscribed).into()) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
pub(crate) fn reading( |
| 162 |
viewer: &super::Viewer, |
| 163 |
carried: &super::Carried, |
| 164 |
) -> Result<(Standing, bool), RouteError> { |
| 165 |
Ok((standing(viewer)?, carried.says("subscribed", "true"))) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
fn standing(viewer: &super::Viewer) -> Result<Standing, RouteError> { |
| 170 |
let Some(user) = viewer.user.as_ref() else { |
| 171 |
return Ok(Standing::Visitor); |
| 172 |
}; |
| 173 |
|
| 174 |
let subscription = viewer |
| 175 |
.block_on(db::fan_plus::get_fan_plus_by_user(&viewer.app.db, user.id)) |
| 176 |
.map_err(|_| RouteError::internal("your membership could not be read"))?; |
| 177 |
|
| 178 |
Ok(match subscription { |
| 179 |
Some(sub) if sub.status == "active" => Standing::Member { |
| 180 |
period_end: sub |
| 181 |
.current_period_end |
| 182 |
.map(|end| end.format("%B %-d, %Y").to_string()), |
| 183 |
}, |
| 184 |
_ => Standing::Unsubscribed, |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
declare! { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
pub(crate) shape page_screen(standing: &Standing, just_subscribed: bool) -> Screen; |
| 196 |
|
| 197 |
screen single "Fan+ - Makenotwork" { |
| 198 |
measured MEASURE; |
| 199 |
documented Document::default().classed(crate::shell::body_class(MEASURE, &["fan-plus-page"])); |
| 200 |
summarised "Support the platform and get $5 of credit back every month, plus forum badges, \ |
| 201 |
signatures and image embeds."; |
| 202 |
|
| 203 |
include page_region(standing, just_subscribed); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
declare! { |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
#[staged] |
| 217 |
pub(crate) shape page_region(standing: &Standing, just_subscribed: bool) -> Slot; |
| 218 |
|
| 219 |
region PAGE_REGION as Pane { |
| 220 |
page "Fan+"; |
| 221 |
banner layout::Tone::Success "You're now a Fan+ member. Welcome." when just_subscribed; |
| 222 |
|
| 223 |
include membership(standing) when standing.is_member(); |
| 224 |
include pitch() unless standing.is_member(); |
| 225 |
|
| 226 |
act "Join Fan+" to post SUBSCRIBE awaiting when standing.is_unsubscribed(); |
| 227 |
for visitor in copy "content/fan-plus.toml" as visitor { |
| 228 |
include super::own_prose(visitor.body) when standing.is_visitor(); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
declare! { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
#[staged] |
| 243 |
shape membership(standing: &Standing) -> Slot; |
| 244 |
|
| 245 |
region MEMBERSHIP as Group { |
| 246 |
text "Your Fan+ membership is active."; |
| 247 |
text "Current period ends: {standing.period_end()}" |
| 248 |
unless standing.period_end().is_empty(); |
| 249 |
text "You'll receive a $5 credit code each billing cycle via email."; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
declare! { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
#[constant] |
| 263 |
shape pitch() -> Slot; |
| 264 |
|
| 265 |
region PITCH as Group { |
| 266 |
text "Support the platform and get something back every month."; |
| 267 |
section "What you get"; |
| 268 |
list { |
| 269 |
for benefit in copy "content/fan-plus.toml" as benefits { |
| 270 |
row benefit.name { |
| 271 |
secondary benefit.detail; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
stats [Figure::new(PRICE, "per month")]; |
| 276 |
text "Makenotwork is built on 0% platform fees. Fan+ is how you directly support the \ |
| 277 |
platform's development and operations, while getting real value back each month."; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
#[must_use] |
| 283 |
pub fn renderer(viewer: &super::Viewer) -> Webview { |
| 284 |
Webview::new().with_shell(viewer.document_shell().with_body_first(format!( |
| 285 |
"{}{}", |
| 286 |
crate::shell::skip_link(PAGE_REGION), |
| 287 |
crate::shell::site_header(viewer.user.as_ref()), |
| 288 |
))) |
| 289 |
} |
| 290 |
|
| 291 |
#[cfg(test)] |
| 292 |
mod tests { |
| 293 |
use super::*; |
| 294 |
|
| 295 |
fn html(standing: &Standing, just_subscribed: bool) -> String { |
| 296 |
use quasi_axum::Serves as _; |
| 297 |
|
| 298 |
Webview::new().screen(&page_screen(standing, just_subscribed)) |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
#[test] |
| 303 |
fn the_document_carries_the_classes_the_template_carried() { |
| 304 |
let screen = page_screen(&Standing::Visitor, false); |
| 305 |
|
| 306 |
assert_eq!( |
| 307 |
screen.document.body_class.as_deref(), |
| 308 |
Some("padded-page fan-plus-page") |
| 309 |
); |
| 310 |
let rendered = html(&Standing::Visitor, false); |
| 311 |
assert!( |
| 312 |
rendered.contains("class=\"padded-page fan-plus-page\""), |
| 313 |
"{rendered}" |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
#[test] |
| 320 |
fn a_visitor_is_offered_an_account_rather_than_a_dead_end() { |
| 321 |
let html = html(&Standing::Visitor, false); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
assert!(html.contains(r#"href="/join""#), "{html}"); |
| 335 |
assert!(html.contains(">Create an account</a> to join"), "{html}"); |
| 336 |
assert!(html.contains(r#"href="/login""#), "{html}"); |
| 337 |
assert!( |
| 338 |
html.contains(">log in</a> if you already have one"), |
| 339 |
"{html}" |
| 340 |
); |
| 341 |
assert!( |
| 342 |
!html.contains("nofollow"), |
| 343 |
"the page nofollowed its own links: {html}" |
| 344 |
); |
| 345 |
assert!( |
| 346 |
!html.contains(SUBSCRIBE), |
| 347 |
"a visitor cannot subscribe, so the control must not be drawn: {html}" |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
#[test] |
| 353 |
fn a_reader_who_is_not_a_member_is_offered_the_subscription() { |
| 354 |
let html = html(&Standing::Unsubscribed, false); |
| 355 |
|
| 356 |
assert!(html.contains(SUBSCRIBE), "{html}"); |
| 357 |
assert!(html.contains("Join Fan+"), "{html}"); |
| 358 |
assert!( |
| 359 |
!html.contains(r#"href="/join""#), |
| 360 |
"somebody signed in does not need an account: {html}" |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
#[test] |
| 366 |
fn a_member_is_shown_their_period_and_offered_nothing() { |
| 367 |
let html = html( |
| 368 |
&Standing::Member { |
| 369 |
period_end: Some("March 4, 2027".into()), |
| 370 |
}, |
| 371 |
false, |
| 372 |
); |
| 373 |
|
| 374 |
assert!(html.contains("March 4, 2027"), "{html}"); |
| 375 |
assert!(html.contains("membership is active"), "{html}"); |
| 376 |
assert!( |
| 377 |
!html.contains(SUBSCRIBE), |
| 378 |
"a member must not be sold to again: {html}" |
| 379 |
); |
| 380 |
assert!( |
| 381 |
!html.contains("What you get"), |
| 382 |
"the pitch is for people who have not bought: {html}" |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
#[test] |
| 389 |
fn a_member_with_no_known_period_end_is_told_the_rest_anyway() { |
| 390 |
let html = html(&Standing::Member { period_end: None }, false); |
| 391 |
|
| 392 |
assert!(html.contains("membership is active"), "{html}"); |
| 393 |
assert!(!html.contains("Current period ends"), "{html}"); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
#[test] |
| 402 |
fn the_welcome_banner_shows_only_on_the_way_back_from_checkout() { |
| 403 |
assert!(html(&Standing::Member { period_end: None }, true).contains("now a Fan+ member")); |
| 404 |
assert!(!html(&Standing::Member { period_end: None }, false).contains("now a Fan+ member")); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
#[test] |
| 415 |
fn the_four_benefits_are_all_stated() { |
| 416 |
let html = html(&Standing::Unsubscribed, false); |
| 417 |
|
| 418 |
let copy: toml::Table = include_str!("../../content/fan-plus.toml") |
| 419 |
.parse() |
| 420 |
.expect("the fan-plus copy is TOML"); |
| 421 |
let benefits = copy["benefits"].as_array().expect("a list of benefits"); |
| 422 |
assert_eq!(benefits.len(), 4); |
| 423 |
for benefit in benefits { |
| 424 |
let name = benefit["name"].as_str().expect("a name"); |
| 425 |
let escaped = crate::helpers::escape_html(name); |
| 426 |
assert!(html.contains(&escaped), "{name} missing"); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
#[test] |
| 436 |
fn the_subscribe_control_spells_no_spinner() { |
| 437 |
assert!( |
| 438 |
html(&Standing::Unsubscribed, false).contains("data-awaiting="), |
| 439 |
"{}", |
| 440 |
html(&Standing::Unsubscribed, false) |
| 441 |
); |
| 442 |
|
| 443 |
for rendered in [ |
| 444 |
html(&Standing::Visitor, false), |
| 445 |
html(&Standing::Unsubscribed, false), |
| 446 |
html(&Standing::Member { period_end: None }, false), |
| 447 |
html(&Standing::Unsubscribed, true), |
| 448 |
] { |
| 449 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 450 |
assert!( |
| 451 |
!rendered.contains(spelling), |
| 452 |
"{spelling} survives in {rendered}" |
| 453 |
); |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|