//! The page of every e-mail setting, opened from an e-mail's footer with the //! user's `email_token` (no login needed): Monday's weekly horoscope e-mail //! and, for forum members, the forum's e-mails (the same settings as on the //! forum profile). The e-mails that belong to something the user does //! (password, purchase, invoice) always go, so they are not listed. use axum::Json; use axum::extract::{Query, State}; use axum_extra::extract::WithRejection; use serde::Deserialize; use serde_json::{Value, json}; use uuid::Uuid; use super::{Body, St}; use crate::AppState; use crate::error::{ApiError, ApiResult}; #[derive(Deserialize)] pub struct TokenQuery { token: String, } async fn user_of(state: &AppState, token: &str) -> Result { let token = Uuid::parse_str(token.trim()).map_err(|_| ApiError::NotFound("token"))?; let user: Option = sqlx::query_scalar("SELECT id FROM users WHERE email_token = $1").bind(token).fetch_optional(&state.db).await?; user.ok_or(ApiError::NotFound("token")) } type ForumRow = (String, String, String, String, String, bool, bool); async fn settings(state: &AppState, user: Uuid) -> Result { let weekly: bool = sqlx::query_scalar("SELECT weekly_email FROM users WHERE id = $1").bind(user).fetch_one(&state.db).await?; let forum: Option = sqlx::query_as( "SELECT email_replies, email_followed, email_messages, email_mentions, email_listener, email_weekly, listener \ FROM forum.members WHERE user_id = $1", ) .bind(user) .fetch_optional(&state.db) .await?; Ok(json!({ "weekly_horoscope": weekly, "forum": forum.map(|(replies, followed, messages, mentions, listener_mail, weekly, listener)| json!({ "replies": replies, "followed": followed, "messages": messages, "mentions": mentions, // Only volunteer listeners get these. "listener": listener.then_some(listener_mail), "weekly": weekly, })), })) } pub async fn get_settings( State(state): St, WithRejection(Query(q), _): WithRejection, ApiError>, ) -> ApiResult> { let user = user_of(&state, &q.token).await?; Ok(Json(settings(&state, user).await?)) } #[derive(Deserialize)] #[serde(deny_unknown_fields)] pub struct Settings { weekly_horoscope: bool, #[serde(default)] forum: Option, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] pub struct ForumSettings { replies: String, followed: String, messages: String, mentions: String, #[serde(default)] listener: Option, weekly: bool, } fn one_of(value: &str, allowed: &[&str], what: &str) -> Result<(), ApiError> { if allowed.contains(&value) { Ok(()) } else { Err(ApiError::BadRequest(format!("{what} must be one of {}", allowed.join(", ")))) } } pub async fn put_settings( State(state): St, WithRejection(Query(q), _): WithRejection, ApiError>, WithRejection(Json(req), _): Body, ) -> ApiResult> { let user = user_of(&state, &q.token).await?; if let Some(f) = &req.forum { one_of(&f.replies, &["off", "daily", "instant"], "replies")?; one_of(&f.followed, &["off", "daily"], "followed")?; one_of(&f.messages, &["off", "instant"], "messages")?; one_of(&f.mentions, &["off", "instant"], "mentions")?; if let Some(l) = &f.listener { one_of(l, &["off", "instant"], "listener")?; } } let mut tx = state.db.begin().await?; // As the switch on the site: turned on again, the first one comes next Monday. sqlx::query(&super::weekly::switch_sql()).bind(user).bind(req.weekly_horoscope).execute(&mut *tx).await?; if let Some(f) = &req.forum { sqlx::query( "UPDATE forum.members SET email_replies = $2, email_followed = $3, email_messages = $4, \ email_mentions = $5, email_listener = coalesce($6, email_listener), email_weekly = $7 \ WHERE user_id = $1", ) .bind(user) .bind(&f.replies) .bind(&f.followed) .bind(&f.messages) .bind(&f.mentions) .bind(&f.listener) .bind(f.weekly) .execute(&mut *tx) .await?; } tx.commit().await?; Ok(Json(settings(&state, user).await?)) }