| 1 |
|
| 2 |
|
| 3 |
use askama::Template as _; |
| 4 |
|
| 5 |
use crate::extractors::ValidatedQuery; |
| 6 |
use axum::{ |
| 7 |
Form, |
| 8 |
extract::{Query, State}, |
| 9 |
http::HeaderMap, |
| 10 |
response::{IntoResponse, Redirect, Response}, |
| 11 |
}; |
| 12 |
use serde::Deserialize; |
| 13 |
use sqlx::PgPool; |
| 14 |
use tower_sessions::Session; |
| 15 |
|
| 16 |
use crate::{ |
| 17 |
AppCaches, Billing, Integrations, |
| 18 |
auth::{AuthUser, MaybeUserUnverified}, |
| 19 |
config::Config, |
| 20 |
constants, db, |
| 21 |
error::{AppError, Result}, |
| 22 |
helpers::get_csrf_token, |
| 23 |
routes::custom_domain, |
| 24 |
templates::{ |
| 25 |
CarouselFrame, CartTemplate, EconomicsTemplate, IndexTemplate, LandingVelocity, |
| 26 |
LibraryCollectionsTabTemplate, LibraryPurchasesTabTemplate, LibraryTemplate, |
| 27 |
}, |
| 28 |
types::{Collection, UserSubscription}, |
| 29 |
}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[derive(Deserialize)] |
| 42 |
pub(super) struct IndexQuery { |
| 43 |
notify: Option<String>, |
| 44 |
} |
| 45 |
|
| 46 |
#[tracing::instrument(skip_all, name = "landing::index")] |
| 47 |
#[allow(clippy::too_many_arguments)] |
| 48 |
pub(super) async fn index( |
| 49 |
State(db): State<PgPool>, |
| 50 |
State(caches): State<AppCaches>, |
| 51 |
State(integrations): State<Integrations>, |
| 52 |
State(config): State<Config>, |
| 53 |
State(billing): State<Billing>, |
| 54 |
Query(q): Query<IndexQuery>, |
| 55 |
headers: HeaderMap, |
| 56 |
session: Session, |
| 57 |
MaybeUserUnverified(maybe_user): MaybeUserUnverified, |
| 58 |
) -> Result<Response> { |
| 59 |
|
| 60 |
if let Some(response) = custom_domain::try_handle( |
| 61 |
&db, |
| 62 |
&caches, |
| 63 |
&integrations, |
| 64 |
&config, |
| 65 |
&headers, |
| 66 |
"/", |
| 67 |
&session, |
| 68 |
maybe_user.as_ref(), |
| 69 |
) |
| 70 |
.await |
| 71 |
{ |
| 72 |
return Ok(response); |
| 73 |
} |
| 74 |
|
| 75 |
match maybe_user { |
| 76 |
Some(_) => Ok(Redirect::to("/library").into_response()), |
| 77 |
None => { |
| 78 |
let total_creators = db::waitlist::count_active_creators(&db).await? as u32; |
| 79 |
let total_items = db::items::count_public_listed(&db).await?; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
let last_shipped = |
| 86 |
db::blog_posts::get_landing_changelog_post(&db, constants::CHANGELOG_PROJECT_SLUG) |
| 87 |
.await? |
| 88 |
.and_then(|post| { |
| 89 |
post.published_at.map(|published_at| LandingVelocity { |
| 90 |
title: post.title, |
| 91 |
date: published_at.format("%b %d, %Y").to_string(), |
| 92 |
href: format!("/changelog/{}", post.slug), |
| 93 |
}) |
| 94 |
}); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
let founder_window_open = config.creator_pricing.founder_window_open; |
| 100 |
const FOUNDER_CAP: u32 = 1_000; |
| 101 |
const URGENCY_THRESHOLD: u32 = 200; |
| 102 |
let founder_slots_remaining = if founder_window_open |
| 103 |
&& total_creators >= FOUNDER_CAP.saturating_sub(URGENCY_THRESHOLD) |
| 104 |
{ |
| 105 |
Some(FOUNDER_CAP.saturating_sub(total_creators)) |
| 106 |
} else { |
| 107 |
None |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
let landing_carousel = vec![ |
| 121 |
CarouselFrame::new( |
| 122 |
"/static/images/shots/storefront.webp", |
| 123 |
"A creator's storefront on Makenotwork, titled and described, above a row of their work in cover art", |
| 124 |
) |
| 125 |
.with_caption("Your storefront: sell anything digital"), |
| 126 |
CarouselFrame::new( |
| 127 |
"/static/images/shots/item.webp", |
| 128 |
"An item page showing its cover art, an $8 price, and a buy button, beside what the purchase includes", |
| 129 |
) |
| 130 |
.with_caption("Every sale is yours. 0% platform fee"), |
| 131 |
CarouselFrame::new( |
| 132 |
"/static/images/shots/library.webp", |
| 133 |
"A buyer's library listing what they have bought, each row with its creator, type, purchase date and a button to open it", |
| 134 |
) |
| 135 |
.with_caption("Buyers keep what they bought. One-click export"), |
| 136 |
]; |
| 137 |
|
| 138 |
Ok(IndexTemplate { |
| 139 |
csrf_token: get_csrf_token(&session).await, |
| 140 |
host_url: config.host_url.clone(), |
| 141 |
total_creators, |
| 142 |
total_items: total_items as u32, |
| 143 |
founder_window_open, |
| 144 |
founder_slots_remaining, |
| 145 |
tier_prices: billing.tier_prices.clone(), |
| 146 |
landing_carousel, |
| 147 |
last_shipped, |
| 148 |
notify_ok: match q.notify.as_deref() { |
| 149 |
Some("ok") => Some(true), |
| 150 |
Some("invalid") => Some(false), |
| 151 |
_ => None, |
| 152 |
}, |
| 153 |
} |
| 154 |
.into_response()) |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
#[tracing::instrument(skip_all, name = "landing::library")] |
| 161 |
pub(super) async fn library( |
| 162 |
State(db): State<PgPool>, |
| 163 |
State(config): State<Config>, |
| 164 |
session: Session, |
| 165 |
AuthUser(user): AuthUser, |
| 166 |
) -> Result<impl IntoResponse> { |
| 167 |
let purchases = db::transactions::get_user_purchases(&db, user.id).await?; |
| 168 |
let db_subs = db::subscriptions::get_user_subscriptions_with_details(&db, user.id).await?; |
| 169 |
let subscriptions: Vec<UserSubscription> = db_subs.iter().map(UserSubscription::from).collect(); |
| 170 |
let has_mt_memberships = config.integrations.mt_base_url.is_some(); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
let shown = LibraryPurchasesTabTemplate { |
| 176 |
purchases, |
| 177 |
subscriptions, |
| 178 |
} |
| 179 |
.render() |
| 180 |
.map_err(|error| AppError::Internal(anyhow::anyhow!(error)))?; |
| 181 |
|
| 182 |
let can_create_projects = user.can_create_projects; |
| 183 |
Ok(LibraryTemplate { |
| 184 |
csrf_token: get_csrf_token(&session).await, |
| 185 |
session_user: Some(user), |
| 186 |
tabs: crate::quasi::library_tabs::html(&shown, has_mt_memberships, can_create_projects), |
| 187 |
}) |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
#[derive(Deserialize)] |
| 192 |
pub(super) struct CartQuery { |
| 193 |
pub checkout: Option<String>, |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
#[tracing::instrument(skip_all, name = "landing::cart_page")] |
| 198 |
pub(super) async fn cart_page( |
| 199 |
State(db): State<PgPool>, |
| 200 |
session: Session, |
| 201 |
AuthUser(user): AuthUser, |
| 202 |
ValidatedQuery(query): ValidatedQuery<CartQuery>, |
| 203 |
) -> Result<impl IntoResponse> { |
| 204 |
use crate::templates::CartSellerGroup; |
| 205 |
use std::collections::BTreeMap; |
| 206 |
|
| 207 |
let cart_items = db::cart::get_cart_items(&db, user.id).await?; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
let buyer = db::users::get_user_by_id(&db, user.id).await?; |
| 213 |
let buyer_currency = buyer |
| 214 |
.as_ref() |
| 215 |
.filter(|u| u.stripe_account_id.is_some()) |
| 216 |
.map(|u| u.settlement_currency); |
| 217 |
let buyer_conversion = buyer |
| 218 |
.as_ref() |
| 219 |
.map(|u| u.conversion_preference) |
| 220 |
.unwrap_or_default(); |
| 221 |
|
| 222 |
|
| 223 |
let mut groups: BTreeMap<String, Vec<db::cart::CartItem>> = BTreeMap::new(); |
| 224 |
for item in &cart_items { |
| 225 |
groups |
| 226 |
.entry(item.seller_id.to_string()) |
| 227 |
.or_default() |
| 228 |
.push(item.clone()); |
| 229 |
} |
| 230 |
|
| 231 |
let seller_groups: Vec<CartSellerGroup> = groups |
| 232 |
.into_iter() |
| 233 |
.map(|(seller_id_str, items)| { |
| 234 |
let subtotal_cents: i64 = items |
| 235 |
.iter() |
| 236 |
.map(|i| i64::from(i.effective_price_cents())) |
| 237 |
.sum(); |
| 238 |
let item_count = items.len(); |
| 239 |
|
| 240 |
let savings_cents = if item_count > 1 { |
| 241 |
(item_count as i32 - 1) * 30 |
| 242 |
} else { |
| 243 |
0 |
| 244 |
}; |
| 245 |
let seller_username = items |
| 246 |
.first() |
| 247 |
.map(|i| i.creator_username.clone()) |
| 248 |
.unwrap_or_default(); |
| 249 |
let stripe_ready = items |
| 250 |
.first() |
| 251 |
.is_some_and(|i| i.seller_stripe_account_id.is_some() && i.seller_charges_enabled); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
let currency = items |
| 257 |
.first() |
| 258 |
.map(|i| i.settlement_currency) |
| 259 |
.unwrap_or_default(); |
| 260 |
|
| 261 |
CartSellerGroup { |
| 262 |
offer_conversion_choice: crate::templates::cart_conversion_applies( |
| 263 |
buyer_currency, |
| 264 |
currency, |
| 265 |
), |
| 266 |
conversion: buyer_conversion, |
| 267 |
seller_username, |
| 268 |
seller_id: seller_id_str, |
| 269 |
stripe_ready, |
| 270 |
items, |
| 271 |
subtotal_cents, |
| 272 |
item_count, |
| 273 |
savings_cents, |
| 274 |
currency, |
| 275 |
} |
| 276 |
}) |
| 277 |
.collect(); |
| 278 |
|
| 279 |
let total_items: usize = seller_groups.iter().map(|g| g.item_count).sum(); |
| 280 |
|
| 281 |
|
| 282 |
let wishlist = db::wishlists::get_wishlist(&db, user.id).await?; |
| 283 |
let cart_item_ids: std::collections::HashSet<_> = |
| 284 |
cart_items.iter().map(|i| i.item_id).collect(); |
| 285 |
let wishlist_suggestions: Vec<_> = wishlist |
| 286 |
.into_iter() |
| 287 |
.filter(|w| !cart_item_ids.contains(&w.item_id)) |
| 288 |
.take(10) |
| 289 |
.collect(); |
| 290 |
|
| 291 |
let offer_conversion_choice = seller_groups.iter().any(|g| g.offer_conversion_choice); |
| 292 |
|
| 293 |
Ok(CartTemplate { |
| 294 |
csrf_token: get_csrf_token(&session).await, |
| 295 |
session_user: Some(user), |
| 296 |
seller_groups, |
| 297 |
wishlist_suggestions, |
| 298 |
total_items, |
| 299 |
checkout_status: query.checkout.unwrap_or_default(), |
| 300 |
offer_conversion_choice, |
| 301 |
conversion: buyer_conversion, |
| 302 |
}) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
#[tracing::instrument(skip_all, name = "landing::library_tab_purchases")] |
| 307 |
pub(super) async fn library_tab_purchases( |
| 308 |
State(db): State<PgPool>, |
| 309 |
AuthUser(user): AuthUser, |
| 310 |
) -> Result<impl IntoResponse> { |
| 311 |
let purchases = db::transactions::get_user_purchases(&db, user.id).await?; |
| 312 |
let db_subs = db::subscriptions::get_user_subscriptions_with_details(&db, user.id).await?; |
| 313 |
let subscriptions: Vec<UserSubscription> = db_subs.iter().map(UserSubscription::from).collect(); |
| 314 |
Ok(LibraryPurchasesTabTemplate { |
| 315 |
purchases, |
| 316 |
subscriptions, |
| 317 |
}) |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
#[derive(Debug, Deserialize)] |
| 324 |
pub(super) struct FeedQuery { |
| 325 |
pub page: Option<u32>, |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
#[tracing::instrument(skip_all, name = "landing::library_tab_feed")] |
| 330 |
pub(super) async fn library_tab_feed( |
| 331 |
State(db): State<PgPool>, |
| 332 |
AuthUser(user): AuthUser, |
| 333 |
ValidatedQuery(query): ValidatedQuery<FeedQuery>, |
| 334 |
) -> Result<impl IntoResponse> { |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
let loaded = crate::quasi::feeds::load(&db, user.id, query.page).await?; |
| 340 |
Ok(axum::response::Html(crate::quasi::feeds::library_fragment( |
| 341 |
&loaded.page(), |
| 342 |
))) |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
#[tracing::instrument(skip_all, name = "landing::library_tab_collections")] |
| 347 |
pub(super) async fn library_tab_collections( |
| 348 |
State(db): State<PgPool>, |
| 349 |
AuthUser(user): AuthUser, |
| 350 |
) -> Result<impl IntoResponse> { |
| 351 |
let db_collections = db::collections::get_collections_by_user(&db, user.id).await?; |
| 352 |
let collections: Vec<Collection> = db_collections.iter().map(Collection::from).collect(); |
| 353 |
let wishlists = db::wishlists::get_wishlist(&db, user.id).await?; |
| 354 |
Ok(LibraryCollectionsTabTemplate { |
| 355 |
collections, |
| 356 |
username: user.username.to_string(), |
| 357 |
wishlists, |
| 358 |
}) |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
#[derive(Deserialize)] |
| 363 |
pub(crate) struct LoginQuery { |
| 364 |
|
| 365 |
|
| 366 |
pub gate: Option<String>, |
| 367 |
|
| 368 |
pub sso_error: Option<String>, |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
#[tracing::instrument(skip_all, name = "landing::login_page")] |
| 373 |
pub(crate) async fn login_page( |
| 374 |
State(config): State<Config>, |
| 375 |
session: Session, |
| 376 |
ValidatedQuery(query): ValidatedQuery<LoginQuery>, |
| 377 |
) -> impl IntoResponse { |
| 378 |
let sso_enabled = config.sso.is_some(); |
| 379 |
let notice = match query.gate.as_deref() { |
| 380 |
Some("fan_plus_or_creator") => Some( |
| 381 |
"This is the testnot.work preview, open to creators and Fan+ members. Log in to continue." |
| 382 |
.to_string(), |
| 383 |
), |
| 384 |
_ => None, |
| 385 |
}; |
| 386 |
let csrf_token = get_csrf_token(&session).await; |
| 387 |
axum::response::Html(crate::quasi::auth_pages::document( |
| 388 |
csrf_token.as_deref(), |
| 389 |
&crate::quasi::auth_pages::login( |
| 390 |
"", |
| 391 |
query.sso_error.as_deref(), |
| 392 |
notice.as_deref(), |
| 393 |
sso_enabled, |
| 394 |
), |
| 395 |
)) |
| 396 |
} |
| 397 |
|
| 398 |
#[derive(Deserialize)] |
| 399 |
pub(super) struct NotifyForm { |
| 400 |
email: String, |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
#[tracing::instrument(skip_all, name = "landing::notify")] |
| 420 |
pub(super) async fn notify( |
| 421 |
State(db): State<PgPool>, |
| 422 |
Form(form): Form<NotifyForm>, |
| 423 |
) -> Result<impl IntoResponse> { |
| 424 |
let Ok(email) = db::Email::new(&form.email) else { |
| 425 |
return Ok(Redirect::to("/?notify=invalid#notify-form")); |
| 426 |
}; |
| 427 |
db::email_signups::insert_email_signup(&db, email.as_str(), "landing").await?; |
| 428 |
Ok(Redirect::to("/?notify=ok#notify-form")) |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
#[tracing::instrument(skip_all, name = "landing::economics_page")] |
| 440 |
pub(super) async fn economics_page( |
| 441 |
State(db): State<PgPool>, |
| 442 |
State(payments): State<Billing>, |
| 443 |
session: Session, |
| 444 |
MaybeUserUnverified(maybe_user): MaybeUserUnverified, |
| 445 |
) -> Result<impl IntoResponse> { |
| 446 |
let paying_creators = crate::db::creator_tiers::count_active_paying(&db).await?; |
| 447 |
let trialing_or_grace = crate::db::creator_tiers::count_trialing_or_grace(&db).await?; |
| 448 |
Ok(EconomicsTemplate { |
| 449 |
csrf_token: get_csrf_token(&session).await, |
| 450 |
session_user: maybe_user, |
| 451 |
runway_config: payments.runway_config.clone(), |
| 452 |
paying_creators, |
| 453 |
trialing_or_grace, |
| 454 |
}) |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
#[tracing::instrument(skip_all, name = "landing::checkout_complete")] |
| 460 |
pub(super) async fn checkout_complete() -> impl IntoResponse { |
| 461 |
axum::response::Html( |
| 462 |
r#"<!DOCTYPE html> |
| 463 |
<html lang="en"> |
| 464 |
<head> |
| 465 |
<meta charset="UTF-8"> |
| 466 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 467 |
<title>Payment Complete | Makenotwork</title> |
| 468 |
<!-- Standalone head, so the three sheets are listed here rather than |
| 469 |
coming from crate::shell. style.css reads the bevel pair out of |
| 470 |
layout.css, so it has to load after it. --> |
| 471 |
<link rel="stylesheet" href="/static/geometry.css"> |
| 472 |
<link rel="stylesheet" href="/static/layout.css"> |
| 473 |
<link rel="stylesheet" href="/static/style.css"> |
| 474 |
<link rel="icon" href="/static/images/favicon.ico" type="image/x-icon"> |
| 475 |
</head> |
| 476 |
<body> |
| 477 |
<main id="main-content"> |
| 478 |
<div class="error-page"> |
| 479 |
<div class="error-container"> |
| 480 |
<h1 class="error-title">Payment complete</h1> |
| 481 |
<p class="error-message">You can close this tab and return to the app.</p> |
| 482 |
</div> |
| 483 |
</div> |
| 484 |
</main> |
| 485 |
</body> |
| 486 |
</html>"#, |
| 487 |
) |
| 488 |
} |
| 489 |
|