//! Notifications (hub spec §10): who to tell about an event, the inbox //! row, and email when the account asked for it and SMTP is configured. use crate::db::{self, accounts::Account, notify as ndb}; use crate::Shared; use std::collections::BTreeSet; tokio::task_local! { /// The name of the agent-marked token this request came with, if any /// (oxygenhub#183): what an agent does with it is told to the token's /// owner too, though the owner is the actor. pub static AGENT: Option; } /// The agent token acting now, when there is one. pub fn agent_acting() -> Option { AGENT.try_with(|a| a.clone()).ok().flatten() } /// An event worth telling people about. pub struct Event<'a> { pub kind: &'a str, pub title: String, pub url: String, pub actor: &'a Account, pub repo_id: i64, /// `issue` or `proposal` and its id, for thread subscribers. pub thread: Option<(&'a str, i64)>, /// Bodies to scan for `@mentions`. pub mention_text: &'a str, /// Accounts to tell whatever their subscriptions (author, assignees). pub direct: Vec, } /// `@username` tokens in a text, unique. pub fn mentions(text: &str) -> Vec { let re = regex::Regex::new(r"(?:^|[^\w/])@([A-Za-z0-9][A-Za-z0-9._-]{0,63})").unwrap(); let mut out = BTreeSet::new(); for c in re.captures_iter(text) { out.insert(c[1].trim_end_matches('.').to_string()); } out.into_iter().collect() } /// Deliver an event: subscribers of the thread, watchers of the /// repository, mentioned accounts and direct targets, never the actor. pub async fn deliver(app: &Shared, ev: Event<'_>) { let pool = &app.db; // Participants: the thread's subscribers, the mentioned, direct targets. // Watchers of the repository who are none of those are only watching. let mut participants: BTreeSet = BTreeSet::new(); if let Some((kind, id)) = ev.thread { if let Ok(subs) = ndb::subscribers(pool, kind, id).await { participants.extend(subs); } // Acting on a thread subscribes the actor to it. ndb::subscribe(pool, ev.actor.id, kind, id).await.ok(); } for name in mentions(ev.mention_text) { if let Ok(Some(a)) = db::accounts::account_by_name(pool, &name).await { participants.insert(a.id); } } participants.extend(ev.direct.iter().copied()); let mut targets: Vec<(i64, ndb::Reason)> = participants.iter().map(|id| (*id, ndb::Reason::Participating)).collect(); if let Ok(w) = ndb::watchers(pool, ev.repo_id).await { targets.extend(w.into_iter().filter(|id| !participants.contains(id)).map(|id| (id, ndb::Reason::Watching))); } // Never the actor, unless an agent acted with their token: then they // hear what their agent did. let agent = agent_acting(); if agent.is_none() { targets.retain(|(id, _)| *id != ev.actor.id); } let actor = match &agent { Some(token) => format!("{} (agent token {token})", ev.actor.username), None => ev.actor.username.clone(), }; tell(app, ev.kind, &ev.title, &ev.url, &actor, (ev.repo_id > 0).then_some(ev.repo_id), targets).await; } /// Tell each of `targets` about something: an inbox row, and an email /// where the account asked for them and has proven its address. pub async fn tell(app: &Shared, kind: &str, title: &str, url: &str, actor: &str, repo_id: Option, targets: Vec<(i64, ndb::Reason)>) { let pool = &app.db; for (id, reason) in targets { ndb::add(pool, ndb::New { account_id: id, repo_id, reason, kind, title, url, actor }).await.ok(); app.events.live.publish(&crate::events::topic::user(id), "notification"); if let Ok(Some(a)) = db::accounts::account_by_id(pool, id).await { // Only to addresses the account has proven it reads (hub spec §5.1.1). if a.email_notifications && a.email_verified && !a.email.is_empty() && crate::mail::enabled(app) { email(app, &a, title, url, actor).await; } } } } async fn email(app: &Shared, to: &Account, title: &str, url: &str, actor: &str) { let unsubscribe = format!("{}/settings/notifications?unsubscribe={}", app.config.base_url(), to.unsubscribe_key); let body = format!("{actor}: {title}\n\n{}{url}\n\nStop these emails: {unsubscribe}\n", app.config.base_url()); if let Err(e) = crate::mail::send(app, to.name(), &to.email, title, &body).await { tracing::warn!("email to {}: {e}", to.email); } } #[cfg(test)] mod tests { use super::*; #[test] fn finds_mentions() { assert_eq!(mentions("cc @andy and @bob. not an email x@y.z, nor /@path"), vec!["andy", "bob"]); assert!(mentions("nothing here").is_empty()); } }