| 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_router::screen::{Figure, Row}; |
| 56 |
use quasi_router::{ |
| 57 |
Action, Document, Node, RegionKind, Request, Response, RouteError, Screen as Described, Slot, |
| 58 |
}; |
| 59 |
use quasi_webview::Webview; |
| 60 |
|
| 61 |
use crate::db; |
| 62 |
|
| 63 |
|
| 64 |
pub const PATH: &str = "/fan-plus"; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
const SUBSCRIBE: &str = "/stripe/fan-plus"; |
| 70 |
|
| 71 |
|
| 72 |
pub const PAGE_REGION: &str = "fan-plus"; |
| 73 |
|
| 74 |
const MEASURE: layout::Measure = layout::Measure::Wide; |
| 75 |
|
| 76 |
|
| 77 |
const PRICE: &str = "$8"; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
const BENEFITS: &[(&str, &str)] = &[ |
| 86 |
( |
| 87 |
"$5 monthly credit", |
| 88 |
"A promo code delivered by email each billing cycle, usable toward any purchase on the platform", |
| 89 |
), |
| 90 |
("+ badge", "Displayed next to your name on your forum posts"), |
| 91 |
( |
| 92 |
"Forum signatures", |
| 93 |
"A signature block rendered under everything you post", |
| 94 |
), |
| 95 |
("Image embeds", "Post images in forum threads"), |
| 96 |
]; |
| 97 |
|
| 98 |
|
| 99 |
enum Standing { |
| 100 |
|
| 101 |
Visitor, |
| 102 |
|
| 103 |
Unsubscribed, |
| 104 |
|
| 105 |
|
| 106 |
Member { period_end: Option<String> }, |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
pub fn screen(viewer: &super::Viewer, request: Request) -> Result<Response, RouteError> { |
| 111 |
|
| 112 |
|
| 113 |
let carried = request.carried; |
| 114 |
let just_subscribed = carried |
| 115 |
.get("subscribed") |
| 116 |
.is_some_and(|value| value.trim() == "true"); |
| 117 |
|
| 118 |
let standing = standing(viewer)?; |
| 119 |
|
| 120 |
Ok(page_screen(&standing, just_subscribed).into()) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
fn standing(viewer: &super::Viewer) -> Result<Standing, RouteError> { |
| 125 |
let Some(user) = viewer.user.as_ref() else { |
| 126 |
return Ok(Standing::Visitor); |
| 127 |
}; |
| 128 |
|
| 129 |
let subscription = viewer |
| 130 |
.block_on(db::fan_plus::get_fan_plus_by_user(&viewer.app.db, user.id)) |
| 131 |
.map_err(|_| RouteError::internal("your membership could not be read"))?; |
| 132 |
|
| 133 |
Ok(match subscription { |
| 134 |
Some(sub) if sub.status == "active" => Standing::Member { |
| 135 |
period_end: sub |
| 136 |
.current_period_end |
| 137 |
.map(|end| end.format("%B %-d, %Y").to_string()), |
| 138 |
}, |
| 139 |
_ => Standing::Unsubscribed, |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
fn page_screen(standing: &Standing, just_subscribed: bool) -> Described { |
| 145 |
let mut page = Slot::new(PAGE_REGION, RegionKind::Pane).with(Node::page("Fan+")); |
| 146 |
|
| 147 |
if just_subscribed { |
| 148 |
page = page.with(Node::banner( |
| 149 |
layout::Tone::Success, |
| 150 |
"You're now a Fan+ member. Welcome.", |
| 151 |
)); |
| 152 |
} |
| 153 |
|
| 154 |
page = match standing { |
| 155 |
Standing::Member { period_end } => membership(page, period_end.as_deref()), |
| 156 |
Standing::Unsubscribed => { |
| 157 |
pitch(page).with(Node::act("Join Fan+", Action::post(SUBSCRIBE).awaiting())) |
| 158 |
} |
| 159 |
Standing::Visitor => { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
pitch(page).with(super::own_prose( |
| 171 |
"[Create an account](/join) to join, or [log in](/login) if you already have one.", |
| 172 |
)) |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
Described::single("Fan+ - Makenotwork") |
| 177 |
.measured(MEASURE) |
| 178 |
.documented( |
| 179 |
Document::default().classed(crate::shell::body_class(MEASURE, &["fan-plus-page"])), |
| 180 |
) |
| 181 |
.summarised( |
| 182 |
"Support the platform and get $5 of credit back every month, plus forum badges, \ |
| 183 |
signatures and image embeds.", |
| 184 |
) |
| 185 |
.with(page) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
fn membership(page: Slot, period_end: Option<&str>) -> Slot { |
| 190 |
let mut page = page.with(Node::text("Your Fan+ membership is active.")); |
| 191 |
|
| 192 |
if let Some(end) = period_end { |
| 193 |
page = page.with(Node::text(format!("Current period ends: {end}"))); |
| 194 |
} |
| 195 |
|
| 196 |
page.with(Node::text( |
| 197 |
"You'll receive a $5 credit code each billing cycle via email.", |
| 198 |
)) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
fn pitch(page: Slot) -> Slot { |
| 204 |
page.with(Node::text( |
| 205 |
"Support the platform and get something back every month.", |
| 206 |
)) |
| 207 |
.with(Node::section("What you get")) |
| 208 |
.with(Node::list( |
| 209 |
BENEFITS |
| 210 |
.iter() |
| 211 |
.map(|(name, detail)| Row::new(*name).secondary(*detail)), |
| 212 |
)) |
| 213 |
.with(Node::stats([Figure::new(PRICE, "per month")])) |
| 214 |
.with(Node::text( |
| 215 |
"Makenotwork is built on 0% platform fees. Fan+ is how you directly support the \ |
| 216 |
platform's development and operations, while getting real value back each month.", |
| 217 |
)) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
#[must_use] |
| 222 |
pub fn renderer(viewer: &super::Viewer) -> Webview { |
| 223 |
Webview::new().with_shell(viewer.document_shell().with_body_first(format!( |
| 224 |
"{}{}", |
| 225 |
crate::shell::skip_link(PAGE_REGION), |
| 226 |
crate::shell::site_header(viewer.user.as_ref()), |
| 227 |
))) |
| 228 |
} |
| 229 |
|
| 230 |
#[cfg(test)] |
| 231 |
mod tests { |
| 232 |
use super::*; |
| 233 |
|
| 234 |
fn html(standing: &Standing, just_subscribed: bool) -> String { |
| 235 |
use quasi_axum::Serves as _; |
| 236 |
|
| 237 |
Webview::new().screen(&page_screen(standing, just_subscribed)) |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
#[test] |
| 242 |
fn the_document_carries_the_classes_the_template_carried() { |
| 243 |
let screen = page_screen(&Standing::Visitor, false); |
| 244 |
|
| 245 |
assert_eq!( |
| 246 |
screen.document.body_class.as_deref(), |
| 247 |
Some("padded-page fan-plus-page") |
| 248 |
); |
| 249 |
let rendered = html(&Standing::Visitor, false); |
| 250 |
assert!( |
| 251 |
rendered.contains("class=\"padded-page fan-plus-page\""), |
| 252 |
"{rendered}" |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#[test] |
| 259 |
fn a_visitor_is_offered_an_account_rather_than_a_dead_end() { |
| 260 |
let html = html(&Standing::Visitor, false); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
assert!(html.contains(r#"href="/join""#), "{html}"); |
| 274 |
assert!(html.contains(">Create an account</a> to join"), "{html}"); |
| 275 |
assert!(html.contains(r#"href="/login""#), "{html}"); |
| 276 |
assert!( |
| 277 |
html.contains(">log in</a> if you already have one"), |
| 278 |
"{html}" |
| 279 |
); |
| 280 |
assert!( |
| 281 |
!html.contains("nofollow"), |
| 282 |
"the page nofollowed its own links: {html}" |
| 283 |
); |
| 284 |
assert!( |
| 285 |
!html.contains(SUBSCRIBE), |
| 286 |
"a visitor cannot subscribe, so the control must not be drawn: {html}" |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
#[test] |
| 292 |
fn a_reader_who_is_not_a_member_is_offered_the_subscription() { |
| 293 |
let html = html(&Standing::Unsubscribed, false); |
| 294 |
|
| 295 |
assert!(html.contains(SUBSCRIBE), "{html}"); |
| 296 |
assert!(html.contains("Join Fan+"), "{html}"); |
| 297 |
assert!( |
| 298 |
!html.contains(r#"href="/join""#), |
| 299 |
"somebody signed in does not need an account: {html}" |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
#[test] |
| 305 |
fn a_member_is_shown_their_period_and_offered_nothing() { |
| 306 |
let html = html( |
| 307 |
&Standing::Member { |
| 308 |
period_end: Some("March 4, 2027".into()), |
| 309 |
}, |
| 310 |
false, |
| 311 |
); |
| 312 |
|
| 313 |
assert!(html.contains("March 4, 2027"), "{html}"); |
| 314 |
assert!(html.contains("membership is active"), "{html}"); |
| 315 |
assert!( |
| 316 |
!html.contains(SUBSCRIBE), |
| 317 |
"a member must not be sold to again: {html}" |
| 318 |
); |
| 319 |
assert!( |
| 320 |
!html.contains("What you get"), |
| 321 |
"the pitch is for people who have not bought: {html}" |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
#[test] |
| 328 |
fn a_member_with_no_known_period_end_is_told_the_rest_anyway() { |
| 329 |
let html = html(&Standing::Member { period_end: None }, false); |
| 330 |
|
| 331 |
assert!(html.contains("membership is active"), "{html}"); |
| 332 |
assert!(!html.contains("Current period ends"), "{html}"); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
#[test] |
| 341 |
fn the_welcome_banner_shows_only_on_the_way_back_from_checkout() { |
| 342 |
assert!(html(&Standing::Member { period_end: None }, true).contains("now a Fan+ member")); |
| 343 |
assert!(!html(&Standing::Member { period_end: None }, false).contains("now a Fan+ member")); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#[test] |
| 349 |
fn the_four_benefits_are_all_stated() { |
| 350 |
let html = html(&Standing::Unsubscribed, false); |
| 351 |
|
| 352 |
assert_eq!(BENEFITS.len(), 4); |
| 353 |
for (name, _) in BENEFITS { |
| 354 |
assert!(html.contains(name), "{name} missing"); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
#[test] |
| 364 |
fn the_subscribe_control_spells_no_spinner() { |
| 365 |
assert!( |
| 366 |
html(&Standing::Unsubscribed, false).contains("data-awaiting="), |
| 367 |
"{}", |
| 368 |
html(&Standing::Unsubscribed, false) |
| 369 |
); |
| 370 |
|
| 371 |
for rendered in [ |
| 372 |
html(&Standing::Visitor, false), |
| 373 |
html(&Standing::Unsubscribed, false), |
| 374 |
html(&Standing::Member { period_end: None }, false), |
| 375 |
html(&Standing::Unsubscribed, true), |
| 376 |
] { |
| 377 |
for spelling in ["htmx-indicator", "spinner", "loading-text", "loading-state"] { |
| 378 |
assert!( |
| 379 |
!rendered.contains(spelling), |
| 380 |
"{spelling} survives in {rendered}" |
| 381 |
); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|