- /heti-horoszkop for visitors: what it is, a real example written for the example chart, login and sign-up buttons; its own link preview image. - Forum: link previews (image, title; a public thread's opening words) for every forum page; none of a members-only thread's content. - Review fixes: a removed thread's photos are no longer served; public photos are cached for an hour, not a year (removals take effect); photos of threads removed a year ago are cleaned up; deleting one forum question no longer revokes the chart link another question still shows; the daily digest leaves out closed topics the member is no longer in; typing notices at most one a second per connection; the weekly page asks for the text once and then only reads it (no repeated paid runs after a failure); turning the Monday e-mail back on from the e-mail page waits for next Monday, like the site's switch; the week's last day is right in the week the clocks go back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
129 lines
4.4 KiB
Rust
129 lines
4.4 KiB
Rust
//! 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<Uuid, ApiError> {
|
|
let token = Uuid::parse_str(token.trim()).map_err(|_| ApiError::NotFound("token"))?;
|
|
let user: Option<Uuid> =
|
|
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<Value, ApiError> {
|
|
let weekly: bool =
|
|
sqlx::query_scalar("SELECT weekly_email FROM users WHERE id = $1").bind(user).fetch_one(&state.db).await?;
|
|
let forum: Option<ForumRow> = 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<Query<TokenQuery>, ApiError>,
|
|
) -> ApiResult<Json<Value>> {
|
|
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<ForumSettings>,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct ForumSettings {
|
|
replies: String,
|
|
followed: String,
|
|
messages: String,
|
|
mentions: String,
|
|
#[serde(default)]
|
|
listener: Option<String>,
|
|
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<Query<TokenQuery>, ApiError>,
|
|
WithRejection(Json(req), _): Body<Settings>,
|
|
) -> ApiResult<Json<Value>> {
|
|
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?))
|
|
}
|