| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::db::{self, accounts::Account, notify as ndb}; |
| 5 |
use crate::Shared; |
| 6 |
use std::collections::BTreeSet; |
| 7 |
|
| 8 |
tokio::task_local! { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
pub static AGENT: Option<String>; |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
pub fn agent_acting() -> Option<String> { |
| 17 |
AGENT.try_with(|a| a.clone()).ok().flatten() |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
pub struct Event<'a> { |
| 22 |
pub kind: &'a str, |
| 23 |
pub title: String, |
| 24 |
pub url: String, |
| 25 |
pub actor: &'a Account, |
| 26 |
pub repo_id: i64, |
| 27 |
|
| 28 |
pub thread: Option<(&'a str, i64)>, |
| 29 |
|
| 30 |
pub mention_text: &'a str, |
| 31 |
|
| 32 |
pub direct: Vec<i64>, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
pub fn mentions(text: &str) -> Vec<String> { |
| 37 |
let re = regex::Regex::new(r"(?:^|[^\w/])@([A-Za-z0-9][A-Za-z0-9._-]{0,63})").unwrap(); |
| 38 |
let mut out = BTreeSet::new(); |
| 39 |
for c in re.captures_iter(text) { |
| 40 |
out.insert(c[1].trim_end_matches('.').to_string()); |
| 41 |
} |
| 42 |
out.into_iter().collect() |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub async fn deliver(app: &Shared, ev: Event<'_>) { |
| 48 |
let pool = &app.db; |
| 49 |
|
| 50 |
|
| 51 |
let mut participants: BTreeSet<i64> = BTreeSet::new(); |
| 52 |
if let Some((kind, id)) = ev.thread { |
| 53 |
if let Ok(subs) = ndb::subscribers(pool, kind, id).await { |
| 54 |
participants.extend(subs); |
| 55 |
} |
| 56 |
|
| 57 |
ndb::subscribe(pool, ev.actor.id, kind, id).await.ok(); |
| 58 |
} |
| 59 |
for name in mentions(ev.mention_text) { |
| 60 |
if let Ok(Some(a)) = db::accounts::account_by_name(pool, &name).await { |
| 61 |
participants.insert(a.id); |
| 62 |
} |
| 63 |
} |
| 64 |
participants.extend(ev.direct.iter().copied()); |
| 65 |
let mut targets: Vec<(i64, ndb::Reason)> = participants.iter().map(|id| (*id, ndb::Reason::Participating)).collect(); |
| 66 |
if let Ok(w) = ndb::watchers(pool, ev.repo_id).await { |
| 67 |
targets.extend(w.into_iter().filter(|id| !participants.contains(id)).map(|id| (id, ndb::Reason::Watching))); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
let agent = agent_acting(); |
| 72 |
if agent.is_none() { |
| 73 |
targets.retain(|(id, _)| *id != ev.actor.id); |
| 74 |
} |
| 75 |
let actor = match &agent { |
| 76 |
Some(token) => format!("{} (agent token {token})", ev.actor.username), |
| 77 |
None => ev.actor.username.clone(), |
| 78 |
}; |
| 79 |
tell(app, ev.kind, &ev.title, &ev.url, &actor, (ev.repo_id > 0).then_some(ev.repo_id), targets).await; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pub async fn tell(app: &Shared, kind: &str, title: &str, url: &str, actor: &str, repo_id: Option<i64>, targets: Vec<(i64, ndb::Reason)>) { |
| 85 |
let pool = &app.db; |
| 86 |
for (id, reason) in targets { |
| 87 |
ndb::add(pool, ndb::New { account_id: id, repo_id, reason, kind, title, url, actor }).await.ok(); |
| 88 |
if let Ok(Some(a)) = db::accounts::account_by_id(pool, id).await { |
| 89 |
|
| 90 |
if a.email_notifications && a.email_verified && !a.email.is_empty() && crate::mail::enabled(app) { |
| 91 |
email(app, &a, kind, title, url, actor).await; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pub fn label(kind: &str) -> Option<&'static str> { |
| 100 |
Some(match kind { |
| 101 |
"flow.failed" => "💥 Build Broken", |
| 102 |
"flow.fixed" => "🎉 Build Success", |
| 103 |
"flow.runner_drained" => "🛑 Runner Drained", |
| 104 |
"backup" => "⚠️ Backup Failed", |
| 105 |
"backup.recovered" => "✅ Backup Recovered", |
| 106 |
"backup.summary" => "📦 Daily Backup Summary", |
| 107 |
"proposal.opened" => "🔀 New Proposal", |
| 108 |
"proposal.review_requested" => "👀 Review Requested", |
| 109 |
"proposal.review" => "📝 New Review", |
| 110 |
"proposal.landed" => "🚀 Proposal Landed", |
| 111 |
"proposal.comment" | "issue.comment" => "💬 New Comment", |
| 112 |
"issue.opened" => "🐛 New Issue", |
| 113 |
"issue.assigned" => "📌 Issue Assigned", |
| 114 |
"issue.closed" => "✔️ Issue Closed", |
| 115 |
"account.registered" => "👋 New Sign-up", |
| 116 |
_ => return None, |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
pub fn subject(kind: &str, title: &str) -> String { |
| 122 |
match label(kind) { |
| 123 |
Some(l) => format!("{l}: {title}"), |
| 124 |
None => title.to_string(), |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
async fn email(app: &Shared, to: &Account, kind: &str, title: &str, url: &str, actor: &str) { |
| 129 |
let unsubscribe = format!("{}/settings/notifications?unsubscribe={}", app.config.base_url(), to.unsubscribe_key); |
| 130 |
let link = format!("{}{url}", app.config.base_url()); |
| 131 |
let body = format!("{actor}: {title}\n\n{link}\n\nStop these emails: {unsubscribe}\n"); |
| 132 |
let action = crate::mail::Action { label: "Open it", url: &link }; |
| 133 |
let subject = subject(kind, title); |
| 134 |
if let Err(e) = crate::mail::send_letter(app, to.name(), &to.email, &subject, &body, Some(action), Some(&unsubscribe)).await { |
| 135 |
tracing::warn!("email to {}: {e}", to.email); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg(test)] |
| 140 |
mod tests { |
| 141 |
use super::*; |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn subjects_say_what_happened() { |
| 145 |
assert_eq!( |
| 146 |
subject("flow.failed", "andy/demo: run #4 on main failed (test)"), |
| 147 |
"💥 Build Broken: andy/demo: run #4 on main failed (test)" |
| 148 |
); |
| 149 |
assert_eq!( |
| 150 |
subject("flow.fixed", "andy/demo: run #5 on main passed: fixed"), |
| 151 |
"🎉 Build Success: andy/demo: run #5 on main passed: fixed" |
| 152 |
); |
| 153 |
assert_eq!(subject("backup.summary", "x"), "📦 Daily Backup Summary: x"); |
| 154 |
assert_eq!(subject("something.new", "x"), "x", "a kind without a label keeps its title"); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn finds_mentions() { |
| 159 |
assert_eq!(mentions("cc @andy and @bob. not an email [email protected], nor /@path"), vec!["andy", "bob"]); |
| 160 |
assert!(mentions("nothing here").is_empty()); |
| 161 |
} |
| 162 |
} |