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