ÁSZF: the forum chapter, and the moderation e-mails it promises

A new point 8 (the old 8–11 become 9–12): reading, writing and the
nickname, the house rules, posts (they stay as "Törölt tag" after an
account is deleted), private messages, ranks and limits, reports and
moderation under the DSA (receipt, decision notice, statement of reasons,
objection, point of contact; every decision is made by a person),
notifications. In force from 2026-11-26 (TERMS_UPDATED, TERMS_VERSION).

So that it holds: the member gets an e-mail with the reason when a
moderator removes their post or thread or bans them, and every reporter
gets the decision on their report, whatever their notification settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-26 11:08:13 +00:00
co-authored by Claude Opus 5.5
parent 7c768fccff
commit da42c92e71
13 changed files with 385 additions and 26 deletions
+62
View File
@@ -155,3 +155,65 @@ async fn message_mail(
});
queue(&state.db, &state.base_url, &to, "forum_message", payload).await
}
#[derive(sqlx::FromRow)]
struct Person {
email: String,
locale: String,
nickname: Option<String>,
}
/// A notice that has to reach the user whatever their notification settings
/// (a moderation step concerning them, the decision on their report).
pub async fn notify_user(state: &AppState, user: Uuid, template: &str, mut payload: Value) {
let person: Result<Option<Person>, sqlx::Error> = sqlx::query_as(
"SELECT u.email, u.locale, m.nickname FROM users u LEFT JOIN forum.members m ON m.user_id = u.id WHERE u.id = $1",
)
.bind(user)
.fetch_optional(&state.db)
.await;
let result = match person {
Ok(Some(p)) => {
payload["nickname"] = json!(p.nickname);
sqlx::query(
"INSERT INTO email_outbox (id, user_id, to_email, template, locale, payload) \
VALUES ($1, $2, $3, $4, $5, $6)",
)
.bind(Uuid::now_v7())
.bind(user)
.bind(&p.email)
.bind(template)
.bind(&p.locale)
.bind(payload)
.execute(&state.db)
.await
.map(|_| ())
}
Ok(None) => Ok(()),
Err(e) => Err(e),
};
if let Err(e) = result {
tracing::error!(error = %e, template, "queueing a moderation e-mail");
}
}
/// The forum rules, where the moderation e-mails point.
pub fn rules_url(state: &AppState) -> String {
format!("{}/aszf#forum", state.base_url)
}
/// "3 napos" or "12 órás": a ban's length for the e-mail.
pub fn ban_duration(hours: i64) -> String {
if hours % 24 == 0 { format!("{} napos", hours / 24) } else { format!("{hours} órás") }
}
/// Tells each reporter (once) how their report was decided.
pub async fn notify_reporters(state: &AppState, reporters: &[Option<Uuid>], outcome: &str) {
let mut seen: Vec<Uuid> = reporters.iter().flatten().copied().collect();
seen.sort_unstable();
seen.dedup();
for reporter in seen {
let payload = json!({ "url": format!("{}/forum", state.base_url), "outcome": outcome });
notify_user(state, reporter, "forum_report_result", payload).await;
}
}
+7
View File
@@ -160,6 +160,13 @@ pub async fn ban(
record(&mut *tx, moderator.id, Action::Ban, Some(user), None, None, Some(&reason), Some(until)).await?;
tx.commit().await?;
tracing::info!(moderator = %moderator.id, member = %user, hours = ?req.hours, "member banned");
let payload = json!({
"url": crate::mail::rules_url(&state),
"action": "banned",
"reason": reason,
"duration": req.hours.map(crate::mail::ban_duration),
});
crate::mail::notify_user(&state, user, "forum_moderation", payload).await;
Ok(Json(json!({ "banned_until": if req.hours.is_none() { "infinity".to_string() } else { rfc3339(until) } })))
}
+15 -2
View File
@@ -67,7 +67,7 @@ pub async fn remove(
WithRejection(Json(req), _): Body<Reason>,
) -> ApiResult<StatusCode> {
let user = viewer.require_user()?;
let (post, _) = load(&state.db, &viewer, id).await?;
let (post, thread) = load(&state.db, &viewer, id).await?;
if post.deleted_at.is_some() {
return Ok(StatusCode::NO_CONTENT);
}
@@ -92,8 +92,9 @@ pub async fn remove(
.execute(&mut *tx)
.await?;
}
let mut reporters = Vec::new();
if !own {
super::reports::resolve_for_post(&mut tx, id, user.id).await?;
reporters = super::reports::resolve_for_post(&mut tx, id, user.id).await?;
moderation::record(
&mut *tx,
user.id,
@@ -107,6 +108,18 @@ pub async fn remove(
.await?;
}
tx.commit().await?;
if !own {
if let Some(author) = post.author_id {
let payload = serde_json::json!({
"url": crate::mail::rules_url(&state),
"action": "post_removed",
"reason": reason,
"thread_title": thread.title,
});
crate::mail::notify_user(&state, author, "forum_moderation", payload).await;
}
crate::mail::notify_reporters(&state, &reporters, "removed").await;
}
Ok(StatusCode::NO_CONTENT)
}
+16 -9
View File
@@ -305,34 +305,41 @@ pub async fn handle(
.await?;
let (post_id, message_id) = report.ok_or(ApiError::NotFound("report"))?;
let mut tx = state.db.begin().await?;
sqlx::query(
let reporters: Vec<Option<Uuid>> = sqlx::query_scalar(
"UPDATE forum.reports SET status = $2, handled_by = $3, handled_at = now() \
WHERE status = 'open' AND (id = $1 OR ($4::bigint IS NOT NULL AND post_id = $4) \
OR ($5::bigint IS NOT NULL AND message_id = $5))",
OR ($5::bigint IS NOT NULL AND message_id = $5)) \
RETURNING reporter_id",
)
.bind(id)
.bind(status)
.bind(moderator.id)
.bind(post_id)
.bind(message_id)
.execute(&mut *tx)
.fetch_all(&mut *tx)
.await?;
moderation::record_report(&mut *tx, moderator.id, action, id, post_id).await?;
tx.commit().await?;
let outcome = if status == "resolved" { "handled" } else { "no_violation" };
crate::mail::notify_reporters(&state, &reporters, outcome).await;
Ok(StatusCode::NO_CONTENT)
}
/// When a moderator removes a post, its open reports are resolved with it.
pub async fn resolve_for_post(tx: &mut sqlx::PgConnection, post_id: i64, moderator: Uuid) -> Result<(), sqlx::Error> {
sqlx::query(
/// Returns the reporters, to tell them.
pub async fn resolve_for_post(
tx: &mut sqlx::PgConnection,
post_id: i64,
moderator: Uuid,
) -> Result<Vec<Option<Uuid>>, sqlx::Error> {
sqlx::query_scalar(
"UPDATE forum.reports SET status = 'resolved', handled_by = $2, handled_at = now() \
WHERE post_id = $1 AND status = 'open'",
WHERE post_id = $1 AND status = 'open' RETURNING reporter_id",
)
.bind(post_id)
.bind(moderator)
.execute(&mut *tx)
.await?;
Ok(())
.fetch_all(&mut *tx)
.await
}
/// Open reports, for the moderators' badge.
+9
View File
@@ -548,6 +548,15 @@ pub async fn remove(
.await?;
tx.commit().await?;
tracing::info!(thread = id, moderator = %moderator.id, "thread removed");
if let Some(author) = thread.author_id {
let payload = json!({
"url": crate::mail::rules_url(&state),
"action": "thread_removed",
"reason": reason,
"thread_title": thread.title,
});
crate::mail::notify_user(&state, author, "forum_moderation", payload).await;
}
Ok(StatusCode::NO_CONTENT)
}
+27
View File
@@ -104,6 +104,15 @@ impl TestApp {
(status, value)
}
/// The queued e-mails of a template: (recipient, payload), oldest first.
async fn mails(&self, template: &str) -> Vec<(String, Value)> {
sqlx::query_as("SELECT to_email, payload FROM email_outbox WHERE template = $1 ORDER BY created_at, id")
.bind(template)
.fetch_all(&self.pool)
.await
.unwrap()
}
async fn get(&self, uri: &str, user: Option<&User>) -> Value {
let (status, body) = self.call("GET", uri, user, Value::Null).await;
assert_eq!(status, StatusCode::OK, "GET {uri}: {body}");
@@ -373,6 +382,14 @@ async fn moderators_remove_and_ban(pool: PgPool) {
assert_eq!(seen["posts"][1]["deleted"]["by_moderator"], true);
let seen = app.get(&format!("/v1/forum/threads/{thread}"), Some(&moderator)).await;
assert!(seen["posts"][1]["body"].is_string(), "moderators still see the text");
// The author is told what and why (whatever their notification settings).
let told = app.mails("forum_moderation").await;
assert_eq!(told.len(), 1);
assert_eq!(told[0].0, "spam@example.com");
assert_eq!(told[0].1["action"], "post_removed");
assert_eq!(told[0].1["reason"], "szolgáltatás hirdetése");
assert_eq!(told[0].1["thread_title"], "Energiák");
assert_eq!(told[0].1["nickname"], "Gyógyító");
// Only the admin restores.
let (status, _) =
@@ -386,6 +403,8 @@ async fn moderators_remove_and_ban(pool: PgPool) {
.call("POST", "/v1/forum/members/gyogyito/ban", Some(&moderator), json!({ "hours": 48, "reason": "hirdetés" }))
.await;
assert_eq!(status, StatusCode::OK, "{body}");
let told = app.mails("forum_moderation").await;
assert_eq!((told[1].1["action"].as_str(), told[1].1["duration"].as_str()), (Some("banned"), Some("2 napos")));
let (status, body) = app
.call("POST", &format!("/v1/forum/threads/{thread}/posts"), Some(&spam), json!({ "body": "Még egyszer" }))
.await;
@@ -573,6 +592,12 @@ async fn reports_reach_moderators(pool: PgPool) {
assert_eq!(status, StatusCode::NO_CONTENT);
let list = app.get("/v1/forum/mod/reports", Some(&moderator)).await;
assert!(list["reports"].as_array().unwrap().iter().all(|r| r["status"] == "resolved"));
// Both reporters hear the decision, once each.
let told = app.mails("forum_report_result").await;
let mut to: Vec<&str> = told.iter().map(|(t, _)| t.as_str()).collect();
to.sort_unstable();
assert_eq!(to, vec!["anna@example.com", "cili@example.com"]);
assert!(told.iter().all(|(_, p)| p["outcome"] == "removed"));
assert_eq!(app.get("/v1/forum/nav", Some(&moderator)).await["reports"], 0);
// A report can also be dismissed.
@@ -583,6 +608,8 @@ async fn reports_reach_moderators(pool: PgPool) {
.call("POST", &format!("/v1/forum/mod/reports/{id}"), Some(&moderator), json!({ "action": "dismiss" }))
.await;
assert_eq!(status, StatusCode::NO_CONTENT);
let told = app.mails("forum_report_result").await;
assert_eq!((told[2].0.as_str(), told[2].1["outcome"].as_str()), ("anna@example.com", Some("no_violation")));
let log = app.get("/v1/forum/mod/log", Some(&moderator)).await;
assert_eq!(log["entries"][0]["action"], "dismiss_report");
assert_eq!(log["entries"][0]["target"], "Holdfény");