Forum: e-mail notifications with a daily digest
Members choose per kind: replies to them (daily digest, instant or off), new posts in followed threads (daily or off), private messages and requests (instant or off). The forum service queues the instant e-mails (none while online, at most one per thread an hour); the worker's new forum task makes the evening digest of what is unread. Every forum e-mail greets by the nickname and has a one-click unsubscribe (RFC 8058). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -48,6 +48,11 @@ pub async fn me(State(state): St, viewer: Viewer) -> ApiResult<Json<Value>> {
|
||||
if u.year() >= 9999 { "infinity".to_string() } else { rfc3339(u) }
|
||||
}),
|
||||
"ban_reason": m.ban_reason.as_ref().filter(|_| m.banned(now)),
|
||||
"email": {
|
||||
"replies": m.email_replies,
|
||||
"followed": m.email_followed,
|
||||
"messages": m.email_messages,
|
||||
},
|
||||
})
|
||||
});
|
||||
let signs = shared_signs(&state.db, &[user.id]).await?.remove(&user.id);
|
||||
@@ -66,6 +71,27 @@ pub struct SaveMe {
|
||||
chart_share: Option<String>,
|
||||
#[serde(default)]
|
||||
invisible: Option<bool>,
|
||||
/// E-mail notifications: `{"replies": "off|daily|instant", "followed": "off|daily", "messages": "off|instant"}`.
|
||||
#[serde(default)]
|
||||
email: Option<EmailSettings>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct EmailSettings {
|
||||
#[serde(default)]
|
||||
replies: Option<String>,
|
||||
#[serde(default)]
|
||||
followed: Option<String>,
|
||||
#[serde(default)]
|
||||
messages: Option<String>,
|
||||
}
|
||||
|
||||
fn one_of(value: Option<&str>, allowed: &[&str]) -> ApiResult<Option<String>> {
|
||||
match value {
|
||||
None => Ok(None),
|
||||
Some(v) if allowed.contains(&v) => Ok(Some(v.to_string())),
|
||||
Some(_) => Err(ApiError::bad("Ismeretlen értesítési beállítás.")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether someone else has (or recently had) this folded nickname.
|
||||
@@ -101,6 +127,10 @@ pub async fn save_me(
|
||||
Some(level @ ("none" | "sun" | "big3" | "planets")) => Some(level.to_string()),
|
||||
Some(_) => return Err(ApiError::bad("Ismeretlen megosztási szint.")),
|
||||
};
|
||||
let email = req.email.as_ref();
|
||||
let email_replies = one_of(email.and_then(|e| e.replies.as_deref()), &["off", "daily", "instant"])?;
|
||||
let email_followed = one_of(email.and_then(|e| e.followed.as_deref()), &["off", "daily"])?;
|
||||
let email_messages = one_of(email.and_then(|e| e.messages.as_deref()), &["off", "instant"])?;
|
||||
if nickname_taken(&state.db, &key, user.id).await? {
|
||||
return Err(taken());
|
||||
}
|
||||
@@ -175,6 +205,17 @@ pub async fn save_me(
|
||||
Err(sqlx::Error::Database(e)) if e.is_unique_violation() => return Err(taken()),
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
sqlx::query(
|
||||
"UPDATE forum.members SET email_replies = COALESCE($2, email_replies), \
|
||||
email_followed = COALESCE($3, email_followed), email_messages = COALESCE($4, email_messages) \
|
||||
WHERE user_id = $1",
|
||||
)
|
||||
.bind(user.id)
|
||||
.bind(email_replies)
|
||||
.bind(email_followed)
|
||||
.bind(email_messages)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
let viewer = Viewer { member: auth::load_member(&state.db, user.id).await?, ..viewer };
|
||||
me(State(state), viewer).await
|
||||
@@ -202,6 +243,30 @@ pub async fn nickname_check(
|
||||
Ok(Json(json!({ "ok": true, "nickname": nickname })))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct TokenQuery {
|
||||
token: String,
|
||||
}
|
||||
|
||||
/// One click from a forum e-mail (its footer link, or the mail client's
|
||||
/// unsubscribe button, RFC 8058): every forum e-mail off, no login needed.
|
||||
pub async fn unsubscribe(
|
||||
State(state): St,
|
||||
WithRejection(Query(q), _): WithRejection<Query<TokenQuery>, ApiError>,
|
||||
) -> ApiResult<Json<Value>> {
|
||||
let done = sqlx::query(
|
||||
"UPDATE forum.members SET email_replies = 'off', email_followed = 'off', email_messages = 'off' \
|
||||
WHERE email_token = $1",
|
||||
)
|
||||
.bind(q.token.trim())
|
||||
.execute(&state.db)
|
||||
.await?;
|
||||
if done.rows_affected() == 0 {
|
||||
return Err(ApiError::NotFound("token"));
|
||||
}
|
||||
Ok(Json(json!({ "unsubscribed": true })))
|
||||
}
|
||||
|
||||
/// The page is open: keeps the online dot (the viewer extractor does the work).
|
||||
pub async fn presence(_viewer: Viewer) -> StatusCode {
|
||||
StatusCode::NO_CONTENT
|
||||
|
||||
Reference in New Issue
Block a user