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:
+112
-1
@@ -30,7 +30,7 @@ struct User {
|
||||
|
||||
impl TestApp {
|
||||
fn new(pool: PgPool) -> Self {
|
||||
let state = AppState { db: pool.clone(), flood_secs: 0 };
|
||||
let state = AppState { db: pool.clone(), flood_secs: 0, base_url: "https://sorsfonalak.test".into() };
|
||||
Self { router: routes::router(Arc::new(state)), pool }
|
||||
}
|
||||
|
||||
@@ -704,3 +704,114 @@ async fn private_messages_start_with_a_request(pool: PgPool) {
|
||||
let (status, _) = app.call("DELETE", &format!("/v1/forum/conversations/{id}/block"), Some(&bea), Value::Null).await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn instant_mails_and_unsubscribe(pool: PgPool) {
|
||||
let app = TestApp::new(pool);
|
||||
let anna = app.member("anna@example.com", "Csendesvíz", &["user"]).await;
|
||||
let bea = app.member("bea@example.com", "Holdfény", &["user"]).await;
|
||||
let cili = app.member("cili@example.com", "Zöldág", &["user"]).await;
|
||||
let me = app.get("/v1/forum/me", Some(&anna)).await;
|
||||
assert_eq!(me["member"]["email"], json!({"replies": "daily", "followed": "off", "messages": "instant"}));
|
||||
let (status, _) = app
|
||||
.call(
|
||||
"PUT",
|
||||
"/v1/forum/me",
|
||||
Some(&anna),
|
||||
json!({ "nickname": "Csendesvíz", "email": { "replies": "instant" } }),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
let (status, _) = app
|
||||
.call(
|
||||
"PUT",
|
||||
"/v1/forum/me",
|
||||
Some(&anna),
|
||||
json!({ "nickname": "Csendesvíz", "email": { "followed": "instant" } }),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST);
|
||||
let thread = app.thread(&anna, "belso-utak", "Reggeli csend").await;
|
||||
let offline = |who: &str| {
|
||||
let pool = app.pool.clone();
|
||||
let who = who.to_string();
|
||||
async move {
|
||||
sqlx::query(
|
||||
"UPDATE forum.members m SET last_seen_at = now() - interval '10 minutes' \
|
||||
FROM users u WHERE u.id = m.user_id AND u.email = $1",
|
||||
)
|
||||
.bind(who)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
let mails = |template: &str| {
|
||||
let pool = app.pool.clone();
|
||||
let template = template.to_string();
|
||||
async move {
|
||||
sqlx::query_as::<_, (String, Value)>(
|
||||
"SELECT to_email, payload FROM email_outbox WHERE template = $1 ORDER BY created_at",
|
||||
)
|
||||
.bind(template)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
// Online: no e-mail (the badge is enough).
|
||||
app.reply(&bea, thread, "Én hajnalban ülök le.", None).await;
|
||||
assert!(mails("forum_reply").await.is_empty());
|
||||
// Away: one e-mail, then no more for this thread within the hour.
|
||||
offline("anna@example.com").await;
|
||||
let post = app.reply(&bea, thread, "Négyig be, hatig ki :)", None).await;
|
||||
app.reply(&cili, thread, "Gyertyát gyújtok.", None).await;
|
||||
let sent = mails("forum_reply").await;
|
||||
assert_eq!(sent.len(), 1);
|
||||
assert_eq!(sent[0].0, "anna@example.com");
|
||||
assert_eq!(sent[0].1["author"], "Holdfény");
|
||||
assert_eq!(sent[0].1["nickname"], "Csendesvíz");
|
||||
assert_eq!(sent[0].1["excerpt"], "Négyig be, hatig ki 🙂");
|
||||
assert_eq!(sent[0].1["url"], format!("https://sorsfonalak.test/forum/szal/{thread}-reggeli-csend#h{}", post["id"]));
|
||||
// Bea answered under her own post too, but asked only for the daily digest.
|
||||
offline("bea@example.com").await;
|
||||
app.reply(&cili, thread, "Szép gondolat.", post["id"].as_i64()).await;
|
||||
assert!(mails("forum_reply").await.iter().all(|(to, _)| to != "bea@example.com"));
|
||||
|
||||
// A request to Bea (instant messages by default), while she is away.
|
||||
app.season(&anna, 5, 10).await;
|
||||
offline("bea@example.com").await;
|
||||
let (status, _) = app
|
||||
.call(
|
||||
"POST",
|
||||
"/v1/forum/conversations",
|
||||
Some(&anna),
|
||||
json!({ "to": "Holdfény", "intro": "Szia, beszélgetnénk?" }),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::CREATED);
|
||||
let sent = mails("forum_message").await;
|
||||
assert_eq!((sent.len(), sent[0].0.as_str()), (1, "bea@example.com"));
|
||||
assert_eq!(sent[0].1["kind"], "request");
|
||||
assert_eq!(sent[0].1["excerpt"], "Szia, beszélgetnénk?");
|
||||
|
||||
// One click turns every forum e-mail off.
|
||||
let token: String = sqlx::query_scalar("SELECT email_token FROM forum.members WHERE user_id = $1")
|
||||
.bind(anna.id)
|
||||
.fetch_one(&app.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
sent[0].1["unsubscribe_post_url"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.starts_with("https://sorsfonalak.test/api/v1/forum/unsubscribe?token=")
|
||||
);
|
||||
let (status, _) = app.call("POST", "/v1/forum/unsubscribe?token=nope", None, Value::Null).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
let (status, _) = app.call("POST", &format!("/v1/forum/unsubscribe?token={token}"), None, Value::Null).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
let me = app.get("/v1/forum/me", Some(&anna)).await;
|
||||
assert_eq!(me["member"]["email"], json!({"replies": "off", "followed": "off", "messages": "off"}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user