| 18e381ea Andy McCall ↶ |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::db::{self, accounts::Account, notify as ndb}; |
| 5 |
use crate::Shared; |
| 6 |
use std::collections::BTreeSet; |
| 7 |
|
| 8 |
|
| 9 |
pub struct Event<'a> { |
| 10 |
pub kind: &'a str, |
| 11 |
pub title: String, |
| 12 |
pub url: String, |
| 13 |
pub actor: &'a Account, |
| 14 |
pub repo_id: i64, |
| 15 |
|
| 16 |
pub thread: Option<(&'a str, i64)>, |
| 17 |
|
| 18 |
pub mention_text: &'a str, |
| 19 |
|
| 20 |
pub direct: Vec<i64>, |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
pub fn mentions(text: &str) -> Vec<String> { |
| 25 |
let re = regex::Regex::new(r"(?:^|[^\w/])@([A-Za-z0-9][A-Za-z0-9._-]{0,63})").unwrap(); |
| 26 |
let mut out = BTreeSet::new(); |
| 27 |
for c in re.captures_iter(text) { |
| 28 |
out.insert(c[1].trim_end_matches('.').to_string()); |
| 29 |
} |
| 30 |
out.into_iter().collect() |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub async fn deliver(app: &Shared, ev: Event<'_>) { |
| 36 |
let pool = &app.db; |
| eb0c44a9 Andy McCall ↶ |
37 |
|
| 38 |
|
| 39 |
let mut participants: BTreeSet<i64> = BTreeSet::new(); |
| 18e381ea Andy McCall ↶ |
40 |
if let Some((kind, id)) = ev.thread { |
| 41 |
if let Ok(subs) = ndb::subscribers(pool, kind, id).await { |
| eb0c44a9 Andy McCall ↶ |
42 |
participants.extend(subs); |
| 18e381ea Andy McCall ↶ |
43 |
} |
| 44 |
|
| 45 |
ndb::subscribe(pool, ev.actor.id, kind, id).await.ok(); |
| 46 |
} |
| 47 |
for name in mentions(ev.mention_text) { |
| 48 |
if let Ok(Some(a)) = db::accounts::account_by_name(pool, &name).await { |
| eb0c44a9 Andy McCall ↶ |
49 |
participants.insert(a.id); |
| 18e381ea Andy McCall ↶ |
50 |
} |
| 51 |
} |
| eb0c44a9 Andy McCall ↶ |
52 |
participants.extend(ev.direct.iter().copied()); |
| 53 |
let mut targets: Vec<(i64, ndb::Reason)> = participants.iter().map(|id| (*id, ndb::Reason::Participating)).collect(); |
| 54 |
if let Ok(w) = ndb::watchers(pool, ev.repo_id).await { |
| 55 |
targets.extend(w.into_iter().filter(|id| !participants.contains(id)).map(|id| (id, ndb::Reason::Watching))); |
| 56 |
} |
| 57 |
targets.retain(|(id, _)| *id != ev.actor.id); |
| 58 |
let repo_id = (ev.repo_id > 0).then_some(ev.repo_id); |
| 59 |
for (id, reason) in targets { |
| 60 |
ndb::add( |
| 61 |
pool, |
| 62 |
ndb::New { account_id: id, repo_id, reason, kind: ev.kind, title: &ev.title, url: &ev.url, actor: &ev.actor.username }, |
| 63 |
) |
| 64 |
.await |
| 65 |
.ok(); |
| 18e381ea Andy McCall ↶ |
66 |
if let Ok(Some(a)) = db::accounts::account_by_id(pool, id).await { |
| 34beb1a7 Andy McCall ↶ |
67 |
|
| 68 |
if a.email_notifications && a.email_verified && !a.email.is_empty() && crate::mail::enabled(app) { |
| 18e381ea Andy McCall ↶ |
69 |
email(app, &a, &ev.title, &ev.url, &ev.actor.username).await; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
async fn email(app: &Shared, to: &Account, title: &str, url: &str, actor: &str) { |
| 76 |
let unsubscribe = format!("{}/settings/notifications?unsubscribe={}", app.config.base_url(), to.unsubscribe_key); |
| 77 |
let body = format!("{actor}: {title}\n\n{}{url}\n\nStop these emails: {unsubscribe}\n", app.config.base_url()); |
| 34beb1a7 Andy McCall ↶ |
78 |
if let Err(e) = crate::mail::send(app, to.name(), &to.email, title, &body).await { |
| 18e381ea Andy McCall ↶ |
79 |
tracing::warn!("email to {}: {e}", to.email); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(test)] |
| 84 |
mod tests { |
| 85 |
use super::*; |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn finds_mentions() { |
| 89 |
assert_eq!(mentions("cc @andy and @bob. not an email [email protected], nor /@path"), vec!["andy", "bob"]); |
| 90 |
assert!(mentions("nothing here").is_empty()); |
| 91 |
} |
| 92 |
} |