- The week's sky on the member's own chart (horoscope-api POST /v1/week, astro_engine::week): the planets' aspects to the natal points with the day each is exact, positions, the Moon's signs, lunar phases, stations and sign changes; stored once per chart and week (weekly_skies, migration 0047). - Its text is a 'weekly' analysis written by the summary's (cheap) model when the member opens /heti-horoszkop: one month per page, the newest week first, the waiting animation while it is written. In the menu, and linked from the chart page. - Monday 7:00 e-mail (worker task 'weekly'): only that it is there; on by default for everyone with a chart, members of today too. Its footer opens /ertesitesek?token=…, where the weekly e-mail and every forum e-mail can be set without logging in; the mail client's one-click unsubscribe turns off the weekly one. Also a switch on the page and on the Profile page. - Privacy notice (HU/EN) describes it; ratings, admin labels and the model page know the new kind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
132 lines
4.4 KiB
Rust
132 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?;
|
|
sqlx::query("UPDATE users SET weekly_email = $2, updated_at = now() WHERE id = $1")
|
|
.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?))
|
|
}
|