use std::collections::{BTreeMap, BTreeSet}; use forge_core::mail::{MailKind, OutgoingMail}; use forge_core::notifications::{Reason, SubState, WatchLevel}; use forge_core::store::orgs::OrgMemberRole; use forge_core::{Action, Issue, Repo, Result, User, authorize}; use http::HeaderMap; use crate::i18n; use crate::mail::{ self, templates::{self, Verb, Why}, }; use crate::notify_hub::publish_unread; use crate::render::markdown::mentioned_usernames; use crate::state::AppState; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Activity { Opened, Comment, Closed, Reopened, Merged, Review, Assigned { assignee_id: String }, ReviewRequested { reviewer_id: String }, Pushed, Edited, } impl Activity { pub fn as_str(&self) -> &'static str { match self { Activity::Opened => "opened", Activity::Comment => "comment", Activity::Closed => "closed", Activity::Reopened => "reopened", Activity::Merged => "merged", Activity::Review => "review", Activity::Assigned { .. } => "assigned", Activity::ReviewRequested { .. } => "review_requested", Activity::Pushed => "pushed", Activity::Edited => "edited", } } fn changes_state(&self) -> bool { matches!( self, Activity::Closed | Activity::Reopened | Activity::Merged ) } } pub async fn dispatch( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, activity: Activity, mention_text: Option<&str>, ) { let mentions = mention_text.map(mentioned_usernames).unwrap_or_default(); fan_out( state, repo, issue, actor_id, &activity, &mentions, mention_text, ) .await; } pub async fn dispatch_edit( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, old_text: &str, new_text: &str, ) { let before = mentioned_usernames(old_text); let added: Vec = mentioned_usernames(new_text) .into_iter() .filter(|name| !before.contains(name)) .collect(); if added.is_empty() { return; } fan_out( state, repo, issue, actor_id, &Activity::Edited, &added, None, ) .await; } pub async fn dispatch_assigned( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, before: &[String], after: &[String], ) { for name in after.iter().filter(|name| !before.contains(name)) { match state.store.user_by_username(name).await { Ok(Some(user)) => { let activity = Activity::Assigned { assignee_id: user.id, }; dispatch(state, repo, issue, actor_id, activity, None).await; } Ok(None) => {} Err(e) => { tracing::error!(error = %e, repo_id = %repo.id, name, "notify: assignee lookup failed"); } } } } pub async fn dispatch_review_requested( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, reviewer_id: &str, ) { let activity = Activity::ReviewRequested { reviewer_id: reviewer_id.to_owned(), }; dispatch(state, repo, issue, actor_id, activity, None).await; } async fn fan_out( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, activity: &Activity, mentions: &[String], body_text: Option<&str>, ) { if let Err(e) = run(state, repo, issue, actor_id, activity, mentions, body_text).await { tracing::error!( error = %e, repo_id = %repo.id, issue_id = %issue.id, activity = activity.as_str(), "notify: dispatch failed" ); } } fn add(candidates: &mut BTreeMap, user_id: String, reason: Reason) { candidates .entry(user_id) .and_modify(|r| *r = strongest(*r, reason)) .or_insert(reason); } fn strongest(a: Reason, b: Reason) -> Reason { if a.rank() >= b.rank() { a } else { b } } async fn readable_user(state: &AppState, repo: &Repo, user_id: &str) -> Result> { let Some(user) = state.store.user_by_id(user_id).await? else { return Ok(None); }; if user.is_org() { return Ok(None); } let role = state.store.collaborator_role(&repo.id, user_id).await?; let org_role = state .store .org_repo_role(&repo.owner_id, user_id, &repo.id) .await?; Ok(authorize(Some(&user), repo, role, org_role, Action::Read) .is_ok() .then_some(user)) } async fn recipients_for(state: &AppState, repo: &Repo, user_id: &str) -> Result> { let Some(user) = state.store.user_by_id(user_id).await? else { return Ok(Vec::new()); }; if !user.is_org() { return Ok(readable_user(state, repo, user_id) .await? .map(|u| vec![u.id]) .unwrap_or_default()); } let mut owners = Vec::new(); for (member, role) in state.store.org_members(&user.id).await? { if role == OrgMemberRole::Owner && readable_user(state, repo, &member.id).await?.is_some() { owners.push(member.id); } } Ok(owners) } async fn run( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, activity: &Activity, mentions: &[String], body_text: Option<&str>, ) -> Result<()> { let store = &state.store; let actor = (!actor_id.is_empty()).then_some(actor_id); let issue_id = issue.id.as_str(); let plan = match mail_plan(state, repo, issue, actor_id, activity, body_text).await { Ok(plan) => plan, Err(e) => { tracing::error!(error = %e, issue_id, "notify: email plan failed"); None } }; let mut candidates: BTreeMap = BTreeMap::new(); let mut mentioned: BTreeSet = BTreeSet::new(); if !matches!( activity, Activity::Edited | Activity::ReviewRequested { .. } ) { for (user_id, stored) in store.subscribers(issue_id).await? { let reason = if activity.changes_state() { strongest(stored, Reason::StateChange) } else { stored }; add(&mut candidates, user_id, reason); } if *activity != Activity::Pushed { for user_id in store.watchers(&repo.id).await? { if is_ignored(state, &user_id, issue_id).await? { continue; } add(&mut candidates, user_id, Reason::Watching); } } if let Activity::Assigned { assignee_id } = activity && !is_ignored(state, assignee_id, issue_id).await? { add(&mut candidates, assignee_id.clone(), Reason::Assign); } } if let Activity::ReviewRequested { reviewer_id } = activity && !is_ignored(state, reviewer_id, issue_id).await? { add( &mut candidates, reviewer_id.clone(), Reason::ReviewRequested, ); } for name in mentions { if let Some(user) = store.user_by_username(name).await? { mentioned.insert(user.id.clone()); add(&mut candidates, user.id, Reason::Mention); } } let mut recipients: BTreeMap = BTreeMap::new(); for (user_id, reason) in candidates { let is_mention = mentioned.contains(&user_id); for recipient in recipients_for(state, repo, &user_id).await? { if Some(recipient.as_str()) == actor { continue; } if !is_mention && store.watch_level(&recipient, &repo.id).await? == WatchLevel::Ignoring { continue; } add(&mut recipients, recipient, reason); } } if let Some(actor_id) = actor { match activity { Activity::Opened => { store .auto_subscribe(actor_id, issue_id, Reason::Author) .await? } Activity::Comment | Activity::Review => { store .auto_subscribe(actor_id, issue_id, Reason::Comment) .await?; } _ => {} } } if let Activity::Assigned { assignee_id } = activity && readable_user(state, repo, assignee_id).await?.is_some() { store .auto_subscribe(assignee_id, issue_id, Reason::Assign) .await?; } if let Activity::ReviewRequested { reviewer_id } = activity && readable_user(state, repo, reviewer_id).await?.is_some() { store .auto_subscribe(reviewer_id, issue_id, Reason::ReviewRequested) .await?; } for user_id in &mentioned { if readable_user(state, repo, user_id).await?.is_some() { store .auto_subscribe(user_id, issue_id, Reason::Mention) .await?; } } for (user_id, reason) in recipients { match store .upsert_notification( &user_id, &repo.id, issue_id, reason, activity.as_str(), actor, ) .await { Ok(()) => { publish_unread(state, &user_id).await; } Err(e) => tracing::error!(error = %e, user_id, issue_id, "notify: inbox write failed"), } if let Some(plan) = &plan && let Err(e) = queue_email(state, plan, &user_id, reason).await { tracing::error!(error = %e, user_id, issue_id, "notify: email copy failed"); } } Ok(()) } struct MailPlan { host: String, root: String, event_id: String, opens_thread: bool, actor: Option, verb: Verb, repo: String, number: i64, title: String, comment: Option, url: String, settings_url: String, } async fn mail_plan( state: &AppState, repo: &Repo, issue: &Issue, actor_id: &str, activity: &Activity, body_text: Option<&str>, ) -> Result> { if !state.store.mail_settings().await?.enabled { return Ok(None); } let base = state.config.base_url.trim_end_matches('/'); let host = host_of(base); let actor = match actor_id { "" => None, id => state.store.user_by_id(id).await?.map(|u| u.username), }; let section = if state .store .pull_by_number(&repo.id, issue.number) .await? .is_some() { "pulls" } else { "issues" }; Ok(Some(MailPlan { root: format!("", issue.id), host, event_id: ulid::Ulid::generate().to_string(), opens_thread: *activity == Activity::Opened, actor, verb: verb_for(activity), repo: format!("{}/{}", repo.owner_name, repo.name), number: issue.number, title: issue.title.clone(), comment: body_text.map(str::to_owned), url: format!( "{base}/{}/{}/{section}/{}", repo.owner_name, repo.name, issue.number ), settings_url: format!("{base}/settings/notifications"), })) } fn verb_for(activity: &Activity) -> Verb { match activity { Activity::Opened => Verb::Opened, Activity::Comment => Verb::Commented, Activity::Closed => Verb::Closed, Activity::Reopened => Verb::Reopened, Activity::Merged => Verb::Merged, Activity::Review => Verb::Reviewed, Activity::Assigned { .. } => Verb::AssignedYou, Activity::ReviewRequested { .. } => Verb::ReviewRequested, Activity::Pushed => Verb::Pushed, Activity::Edited => Verb::MentionedYou, } } fn why_for(reason: Reason) -> Why { match reason { Reason::Mention => Why::Mention, Reason::Assign => Why::Assign, Reason::ReviewRequested => Why::ReviewRequested, Reason::Author => Why::Author, Reason::Comment => Why::Comment, Reason::StateChange | Reason::Subscribed => Why::Subscribed, Reason::Watching => Why::Watching, } } async fn queue_email( state: &AppState, plan: &MailPlan, user_id: &str, reason: Reason, ) -> Result<()> { let Some(user) = state.store.user_by_id(user_id).await? else { return Ok(()); }; let Some(address) = user.email.as_deref().filter(|address| !address.is_empty()) else { return Ok(()); }; if !user.email_notifications || !state.store.email_is_verified(user_id).await? { return Ok(()); } let lang = i18n::resolve(Some(user.language.as_str()), &HeaderMap::new()); let (subject, body) = templates::notification( lang, &templates::Notification { actor: plan.actor.as_deref(), verb: plan.verb, repo: &plan.repo, number: plan.number, title: &plan.title, comment: plan.comment.as_deref(), url: &plan.url, why: why_for(reason), settings_url: &plan.settings_url, }, ); let (message_id, in_reply_to) = if plan.opens_thread { (plan.root.clone(), None) } else { ( format!("<{}.{user_id}@{}>", plan.event_id, plan.host), Some(plan.root.clone()), ) }; let outgoing = OutgoingMail { to: address.to_owned(), subject, body, message_id: Some(message_id), in_reply_to, kind: MailKind::Notification, user_id: Some(user_id.to_owned()), }; let idem_key = format!("notif:{}:{user_id}", plan.event_id); mail::enqueue_message(state, &idem_key, &outgoing).await?; Ok(()) } fn host_of(base_url: &str) -> String { let rest = base_url .split_once("://") .map_or(base_url, |(_, rest)| rest); let authority = rest.split(['/', '?', '#']).next().unwrap_or_default(); let host = authority .rsplit_once('@') .map_or(authority, |(_, host)| host); let host = host.split(':').next().unwrap_or_default(); if host.is_empty() { "localhost".to_owned() } else { host.to_owned() } } async fn is_ignored(state: &AppState, user_id: &str, issue_id: &str) -> Result { Ok(matches!( state.store.subscription(user_id, issue_id).await?, Some((SubState::Ignored, _)) )) } #[cfg(test)] mod tests { use forge_core::{Notification, Role, Visibility}; use super::*; use crate::test_support::test_state; struct Fixture { state: AppState, repo: Repo, issue: Issue, owner: User, tahinli: User, bob: User, carol: User, _dir: tempfile::TempDir, } async fn fixture(visibility: Visibility) -> Fixture { let dir = tempfile::tempdir().expect("tempdir"); let state = test_state(dir.path()).await; let store = &state.store; let owner = store .create_user("owner", None, "unused", false) .await .unwrap(); let tahinli = store .create_user("tahinli", None, "unused", false) .await .unwrap(); let bob = store .create_user("bob", None, "unused", false) .await .unwrap(); let carol = store .create_user("carol", None, "unused", false) .await .unwrap(); let repo = store .create_repo(&owner.id, "proj", "", visibility, "main") .await .unwrap(); let issue = store .create_issue(&repo.id, &owner.id, "a problem", "body") .await .unwrap(); Fixture { state, repo, issue, owner, tahinli, bob, carol, _dir: dir, } } async fn inbox(fx: &Fixture, user: &User) -> Vec { fx.state .store .list_notifications(&user.id, false, None, 50) .await .unwrap() } #[test] fn mentioned_usernames_cases() { let cases: Vec<(&str, &str, Vec<&str>)> = vec![ ( "skips code, links and emails", "hi @Tahinli and @bob. mail a@b.com `@code`\n\n\ ```\n@blocked\n```\n[@linked](https://x.test) @tahinli again", vec!["tahinli", "bob"], ), ( "found inside html blocks", "
\n@carol\n
\n", vec!["carol"], ), ("deduped case-insensitively", "@Bob @bob @BOB", vec!["bob"]), ("no mentions", "no mentions here", vec![]), ]; for (name, text, want) in cases { assert_eq!(mentioned_usernames(text), want, "case {name}"); } } #[tokio::test] async fn comment_notifies_subscriber_not_the_commenter() { let fx = fixture(Visibility::Public).await; let store = &fx.state.store; store .subscribe( &fx.bob.id, &fx.issue.id, SubState::Subscribed, Reason::Subscribed, ) .await .unwrap(); store .subscribe( &fx.tahinli.id, &fx.issue.id, SubState::Subscribed, Reason::Subscribed, ) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Comment, Some("hello"), ) .await; let bob_inbox = inbox(&fx, &fx.bob).await; assert_eq!(bob_inbox.len(), 1); assert_eq!(bob_inbox[0].reason, Reason::Subscribed); assert_eq!(bob_inbox[0].last_activity, "comment"); assert!( inbox(&fx, &fx.tahinli).await.is_empty(), "the actor is never notified" ); } #[tokio::test] async fn opened_notifies_watchers_and_auto_subscribes_the_author() { let fx = fixture(Visibility::Public).await; fx.state .store .set_watch(&fx.bob.id, &fx.repo.id, WatchLevel::Watching) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Opened, None, ) .await; let bob_inbox = inbox(&fx, &fx.bob).await; assert_eq!(bob_inbox.len(), 1); assert_eq!(bob_inbox[0].reason, Reason::Watching); assert!(inbox(&fx, &fx.owner).await.is_empty()); let sub = fx .state .store .subscription(&fx.owner.id, &fx.issue.id) .await .unwrap(); assert_eq!(sub, Some((SubState::Subscribed, Reason::Author))); } #[tokio::test] async fn ignored_subscription_suppresses_watcher() { let fx = fixture(Visibility::Public).await; let store = &fx.state.store; store .set_watch(&fx.bob.id, &fx.repo.id, WatchLevel::Watching) .await .unwrap(); store .subscribe( &fx.bob.id, &fx.issue.id, SubState::Ignored, Reason::Subscribed, ) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Opened, None, ) .await; assert!(inbox(&fx, &fx.bob).await.is_empty()); } #[tokio::test] async fn ignoring_repo_suppresses_but_mention_still_notifies() { let fx = fixture(Visibility::Public).await; let store = &fx.state.store; store .set_watch(&fx.tahinli.id, &fx.repo.id, WatchLevel::Ignoring) .await .unwrap(); store .subscribe( &fx.tahinli.id, &fx.issue.id, SubState::Subscribed, Reason::Subscribed, ) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.bob.id, Activity::Comment, Some("plain"), ) .await; assert!(inbox(&fx, &fx.tahinli).await.is_empty()); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.bob.id, Activity::Comment, Some("@tahinli look"), ) .await; let tahinli_inbox = inbox(&fx, &fx.tahinli).await; assert_eq!(tahinli_inbox.len(), 1); assert_eq!(tahinli_inbox[0].reason, Reason::Mention); } #[tokio::test] async fn mention_without_read_access_on_private_repo_is_not_notified() { let fx = fixture(Visibility::Private).await; let store = &fx.state.store; dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Comment, Some("@bob hi"), ) .await; assert!(inbox(&fx, &fx.bob).await.is_empty()); let sub = store.subscription(&fx.bob.id, &fx.issue.id).await.unwrap(); assert_eq!(sub, None, "an unreadable mention does not subscribe"); store .set_collaborator(&fx.repo.id, &fx.bob.id, Role::Read) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Comment, Some("@bob hi"), ) .await; let bob_inbox = inbox(&fx, &fx.bob).await; assert_eq!(bob_inbox.len(), 1); assert_eq!(bob_inbox[0].reason, Reason::Mention); } #[tokio::test] async fn assignee_is_notified_with_assign_reason() { let fx = fixture(Visibility::Public).await; dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Assigned { assignee_id: fx.bob.id.clone(), }, None, ) .await; let bob_inbox = inbox(&fx, &fx.bob).await; assert_eq!(bob_inbox.len(), 1); assert_eq!(bob_inbox[0].reason, Reason::Assign); assert_eq!(bob_inbox[0].last_activity, "assigned"); } #[tokio::test] async fn pushed_reaches_subscribers_not_watchers() { let fx = fixture(Visibility::Public).await; let store = &fx.state.store; store .set_watch(&fx.bob.id, &fx.repo.id, WatchLevel::Watching) .await .unwrap(); store .subscribe( &fx.carol.id, &fx.issue.id, SubState::Subscribed, Reason::Author, ) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, Activity::Pushed, None, ) .await; assert!(inbox(&fx, &fx.bob).await.is_empty()); let carol_inbox = inbox(&fx, &fx.carol).await; assert_eq!(carol_inbox.len(), 1); assert_eq!(carol_inbox[0].reason, Reason::Author); } #[tokio::test] async fn edit_notifies_only_the_newly_mentioned_user() { let fx = fixture(Visibility::Public).await; fx.state .store .set_watch(&fx.bob.id, &fx.repo.id, WatchLevel::Watching) .await .unwrap(); dispatch_edit( &fx.state, &fx.repo, &fx.issue, &fx.owner.id, "hi @bob", "hi @bob and @carol", ) .await; assert!( inbox(&fx, &fx.bob).await.is_empty(), "bob was mentioned before the edit" ); let carol_inbox = inbox(&fx, &fx.carol).await; assert_eq!(carol_inbox.len(), 1); assert_eq!(carol_inbox[0].reason, Reason::Mention); assert_eq!(carol_inbox[0].last_activity, "edited"); } async fn mail_fixture(enabled: bool) -> Fixture { let fx = fixture(Visibility::Public).await; let store = &fx.state.store; store .update_mail_settings(&forge_core::mail::MailSettingsUpdate { host: "smtp.test.example", port: 587, security: forge_core::mail::Security::StartTls, username: "", from_addr: "forge@test.example", from_name: "forge", enabled, password: None, }) .await .unwrap(); store .set_email(&fx.bob.id, "bob@test.example") .await .unwrap(); let token = store .create_verify_token(&fx.bob.id, "bob@test.example") .await .unwrap(); store.redeem_verify_token(&token).await.unwrap(); store .subscribe( &fx.bob.id, &fx.issue.id, SubState::Subscribed, Reason::Subscribed, ) .await .unwrap(); fx } async fn mail_to(fx: &Fixture, address: &str) -> Vec { fx.state .store .recent_mail(50) .await .unwrap() .into_iter() .filter(|row| row.to_addr == address) .collect() } #[tokio::test] async fn comment_emails_the_subscriber_threaded_under_the_issue() { let fx = mail_fixture(true).await; dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Comment, Some("hello there"), ) .await; let rows = mail_to(&fx, "bob@test.example").await; assert_eq!(rows.len(), 1, "one mail to the subscriber"); let host = host_of(&fx.state.config.base_url); assert_eq!( rows[0].in_reply_to.as_deref(), Some(format!("", fx.issue.id).as_str()) ); let message_id = rows[0].message_id.as_deref().unwrap_or_default(); assert!(message_id.starts_with('<') && message_id.ends_with(&format!("@{host}>"))); assert_ne!( message_id, rows[0].in_reply_to.as_deref().unwrap_or_default() ); assert!(rows[0].body.contains("hello there"), "{}", rows[0].body); assert!( rows[0].body.contains("tahinli commented on this."), "{}", rows[0].body ); assert!( rows[0].idem_key.starts_with("notif:"), "{}", rows[0].idem_key ); assert!(rows[0].idem_key.ends_with(&format!(":{}", fx.bob.id))); assert!( inbox(&fx, &fx.tahinli).await.is_empty(), "the actor gets no mail or inbox row" ); } #[tokio::test] async fn opening_mail_is_the_thread_root() { let fx = mail_fixture(true).await; dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Opened, Some("the body"), ) .await; let rows = mail_to(&fx, "bob@test.example").await; assert_eq!(rows.len(), 1); let host = host_of(&fx.state.config.base_url); let root = format!("", fx.issue.id); assert_eq!(rows[0].message_id.as_deref(), Some(root.as_str())); assert_eq!(rows[0].in_reply_to, None); } #[tokio::test] async fn no_mail_without_a_verified_address() { let fx = mail_fixture(true).await; fx.state .store .clear_email_verified(&fx.bob.id) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Comment, Some("hi"), ) .await; assert!(mail_to(&fx, "bob@test.example").await.is_empty()); assert_eq!( inbox(&fx, &fx.bob).await.len(), 1, "the inbox still gets it" ); } #[tokio::test] async fn no_mail_when_the_user_turned_email_notifications_off() { let fx = mail_fixture(true).await; fx.state .store .set_email_notifications(&fx.bob.id, false) .await .unwrap(); dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Comment, Some("hi"), ) .await; assert!(mail_to(&fx, "bob@test.example").await.is_empty()); assert_eq!(inbox(&fx, &fx.bob).await.len(), 1); } #[tokio::test] async fn no_mail_when_instance_mail_is_disabled() { let fx = mail_fixture(false).await; dispatch( &fx.state, &fx.repo, &fx.issue, &fx.tahinli.id, Activity::Comment, Some("hi"), ) .await; assert!(mail_to(&fx, "bob@test.example").await.is_empty()); } }