Inviting shown but off until launch; waitlist confirmation; waitlist invites with a package
USER_INVITES (default false) keeps users from inviting others until the launch: the 'Meghívom' option and a 'Hívd meg, akit szeretsz' card on the persons page are shown, disabled, with 'Hamarosan'; the backend refuses with invites_disabled (admins may always). Joining the waitlist sends one confirmation e-mail (waitlist_joined) with a link that takes the address off the list and opts it out; at most 10 per address in 15 minutes. Admins can pick a package for waitlist invitations, like on the invitation form. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -587,10 +587,12 @@ pub async fn unsubscribe(
|
||||
WithRejection(Query(q), _): WithRejection<Query<TokenQuery>, ApiError>,
|
||||
) -> ApiResult<Json<Value>> {
|
||||
let mut tx = state.db.begin().await?;
|
||||
// The link of a site invitation, or of a "how compatible are we?" one a user had us e-mail.
|
||||
// The link of a site invitation, of a "how compatible are we?" one a user
|
||||
// had us e-mail, or of the waitlist confirmation.
|
||||
let email: Option<String> = sqlx::query_scalar(
|
||||
"SELECT i.email FROM invitation_tokens t JOIN invitations i ON i.id = t.invitation_id WHERE t.token_hash = $1 \
|
||||
UNION ALL SELECT invitee_email FROM synastry_invites WHERE token_hash = $1 AND invitee_email IS NOT NULL \
|
||||
UNION ALL SELECT email FROM waitlist WHERE token_hash = $1 \
|
||||
LIMIT 1",
|
||||
)
|
||||
.bind(db::token_hash(q.token.trim()))
|
||||
@@ -607,6 +609,7 @@ pub async fn unsubscribe(
|
||||
.bind(&email)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM waitlist WHERE email = $1").bind(&email).execute(&mut *tx).await?;
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({ "unsubscribed": true })))
|
||||
}
|
||||
@@ -621,19 +624,46 @@ pub struct WaitlistRequest {
|
||||
/// Joins the waitlist. Always answers the same, so it does not reveal who has an account.
|
||||
pub async fn join_waitlist(
|
||||
State(state): St,
|
||||
headers: axum::http::HeaderMap,
|
||||
WithRejection(Json(req), _): Body<WaitlistRequest>,
|
||||
) -> ApiResult<(StatusCode, Json<Value>)> {
|
||||
let email = normalize_email(&req.email)?;
|
||||
let locale = validate_locale(&state, req.locale.as_deref())?;
|
||||
sqlx::query(
|
||||
"INSERT INTO waitlist (email, locale) SELECT $1, $2 \
|
||||
// Every sign-up sends an e-mail: a few per address, so the form cannot be used to flood someone.
|
||||
let ip = headers
|
||||
.get("x-forwarded-for")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|v| v.split(',').next())
|
||||
.map(str::trim)
|
||||
.unwrap_or("unknown");
|
||||
let key = format!("waitlist:{ip}");
|
||||
state.login_limiter.check(&key)?;
|
||||
let token = db::new_token();
|
||||
let mut tx = state.db.begin().await?;
|
||||
let added = sqlx::query(
|
||||
"INSERT INTO waitlist (email, locale, token_hash) SELECT $1, $2, $3 \
|
||||
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = $1) \
|
||||
AND (SELECT count(*) FROM waitlist) < 10000 \
|
||||
ON CONFLICT DO NOTHING",
|
||||
)
|
||||
.bind(&email)
|
||||
.bind(&locale)
|
||||
.execute(&state.db)
|
||||
.await?;
|
||||
.bind(db::token_hash(&token))
|
||||
.execute(&mut *tx)
|
||||
.await?
|
||||
.rows_affected()
|
||||
== 1;
|
||||
// A confirmation, once, unless the address asked for no more mail from us.
|
||||
if added && !opted_out(&mut tx, &email).await? {
|
||||
let (_, unsubscribe_url, unsubscribe_post_url) = links(&state, &token);
|
||||
let payload = json!({
|
||||
"url": state.config.public_base_url,
|
||||
"unsubscribe_url": unsubscribe_url,
|
||||
"unsubscribe_post_url": unsubscribe_post_url,
|
||||
});
|
||||
queue_email_to(&mut tx, None, &email, "waitlist_joined", &locale, payload).await?;
|
||||
state.login_limiter.failed(&key);
|
||||
}
|
||||
tx.commit().await?;
|
||||
Ok((StatusCode::ACCEPTED, Json(json!({ "ok": true }))))
|
||||
}
|
||||
|
||||
@@ -162,6 +162,7 @@ async fn public_config(State(state): St) -> Json<Value> {
|
||||
"locales": c.supported_locales,
|
||||
"password_login": c.password_login,
|
||||
"mock_payments": c.mock_payments,
|
||||
"user_invites": c.user_invites,
|
||||
"signup_mode": c.signup_mode.as_str(),
|
||||
"password_min_length": crate::auth::MIN_PASSWORD_LEN,
|
||||
}))
|
||||
|
||||
@@ -260,6 +260,9 @@ pub async fn create_invite(
|
||||
auth: AuthUser,
|
||||
WithRejection(Json(req), _): Body<CreateInvite>,
|
||||
) -> ApiResult<(StatusCode, Json<Value>)> {
|
||||
if !state.config.user_invites && !auth.user.has_role(db::Role::Admin) {
|
||||
return Err(ApiError::conflict("invites_disabled", "A meghívás hamarosan indul."));
|
||||
}
|
||||
let chart = chart_or_primary(&state.db, req.chart_id, auth.id()).await?;
|
||||
// No links: the message may go out in an e-mail from our domain.
|
||||
let message = super::invitations::clean_message(req.message)?;
|
||||
|
||||
Reference in New Issue
Block a user