Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(slack): x joined macro team email on slack import (#7504)
Will Hutchinsone7c87d64dcdd
diff --git a/.github/services-config.json b/.github/services-config.jsonindex 2e82fdc2c7..64311aad96 100644--- a/.github/services-config.json+++ b/.github/services-config.json@@ -129,6 +129,10 @@"crates/import/**","crates/macro_queues/**","crates/macro_service_urls/**",+ "crates/teams/**",+ "crates/invite_email/**",+ "crates/notification/**",+ "crates/model_notifications/**","crates/call/**","crates/github/**","crates/initiative/**",diff --git a/.sqlx/query-11d1a56900bb3e1aaf82dd15e5d3fb81aa1cdf392ad3dd87b1815a9dc4fe50ca.json b/.sqlx/query-11d1a56900bb3e1aaf82dd15e5d3fb81aa1cdf392ad3dd87b1815a9dc4fe50ca.jsonnew file mode 100644index 0000000000..c8c8dad293--- /dev/null+++ b/.sqlx/query-11d1a56900bb3e1aaf82dd15e5d3fb81aa1cdf392ad3dd87b1815a9dc4fe50ca.json@@ -0,0 +1,26 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT team_id, email FROM team_joined_macro_email",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "team_id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "email",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ false,+ false+ ]+ },+ "hash": "11d1a56900bb3e1aaf82dd15e5d3fb81aa1cdf392ad3dd87b1815a9dc4fe50ca"+}diff --git a/.sqlx/query-15871789e389c9d408aaf5e1ad03cb47e91dfd0e0bedaa24f3d5a2fbaa88d623.json b/.sqlx/query-15871789e389c9d408aaf5e1ad03cb47e91dfd0e0bedaa24f3d5a2fbaa88d623.jsonnew file mode 100644index 0000000000..11c8ce643f--- /dev/null+++ b/.sqlx/query-15871789e389c9d408aaf5e1ad03cb47e91dfd0e0bedaa24f3d5a2fbaa88d623.json@@ -0,0 +1,20 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT count(*) FROM team_joined_macro_email",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "count",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": []+ },+ "nullable": [+ null+ ]+ },+ "hash": "15871789e389c9d408aaf5e1ad03cb47e91dfd0e0bedaa24f3d5a2fbaa88d623"+}diff --git a/.sqlx/query-8fdc25ca235cbaf6c1b37a64e2449111e1732140a8958686ba8c23299ad3e9a0.json b/.sqlx/query-8fdc25ca235cbaf6c1b37a64e2449111e1732140a8958686ba8c23299ad3e9a0.jsonnew file mode 100644index 0000000000..1dc259e8fb--- /dev/null+++ b/.sqlx/query-8fdc25ca235cbaf6c1b37a64e2449111e1732140a8958686ba8c23299ad3e9a0.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE team SET auto_join_domain = $2 WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "8fdc25ca235cbaf6c1b37a64e2449111e1732140a8958686ba8c23299ad3e9a0"+}diff --git a/.sqlx/query-a8080ff3d4a2ec860380f02d239a7a671c0afe3c37aacc59ccc6408ee0f56080.json b/.sqlx/query-a8080ff3d4a2ec860380f02d239a7a671c0afe3c37aacc59ccc6408ee0f56080.jsonnew file mode 100644index 0000000000..a8276feb05--- /dev/null+++ b/.sqlx/query-a8080ff3d4a2ec860380f02d239a7a671c0afe3c37aacc59ccc6408ee0f56080.json@@ -0,0 +1,30 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH claimed AS (\n INSERT INTO team_joined_macro_email (team_id, email)\n SELECT t.id, c.email\n FROM team t\n CROSS JOIN UNNEST($2::text[], $3::text[]) AS c(email, user_id)\n WHERE t.id = $1\n AND t.auto_join_domain IS NOT NULL\n AND split_part(c.email, '@', 2) = t.auto_join_domain\n AND NOT EXISTS (\n SELECT 1 FROM team_user tu\n WHERE tu.team_id = t.id AND tu.user_id = c.user_id\n )\n AND NOT EXISTS (\n SELECT 1 FROM team_invite ti\n WHERE ti.team_id = t.id AND ti.email = c.email\n )\n ON CONFLICT (team_id, email) DO NOTHING\n RETURNING email\n )\n SELECT claimed.email AS \"email!\", t.name AS \"team_name!\"\n FROM claimed\n JOIN team t ON t.id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "email!",+ "type_info": "Text"+ },+ {+ "ordinal": 1,+ "name": "team_name!",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "TextArray",+ "TextArray"+ ]+ },+ "nullable": [+ false,+ false+ ]+ },+ "hash": "a8080ff3d4a2ec860380f02d239a7a671c0afe3c37aacc59ccc6408ee0f56080"+}diff --git a/.sqlx/query-bb5c2b997a973899bb78cb2a28d8c4faae2e68d674123ecc2b6c5b11434d020c.json b/.sqlx/query-bb5c2b997a973899bb78cb2a28d8c4faae2e68d674123ecc2b6c5b11434d020c.jsonnew file mode 100644index 0000000000..3aee0f3b09--- /dev/null+++ b/.sqlx/query-bb5c2b997a973899bb78cb2a28d8c4faae2e68d674123ecc2b6c5b11434d020c.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "INSERT INTO team (id, name, owner_id, auto_join_domain) VALUES ($1, 'Atomic history', 'macro|[email protected]', 'example.com')",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "bb5c2b997a973899bb78cb2a28d8c4faae2e68d674123ecc2b6c5b11434d020c"+}diff --git a/.sqlx/query-be6acd044d4ad3dd1e0165756ae84f6a20ad1463670e32e3fb188673efa93f61.json b/.sqlx/query-be6acd044d4ad3dd1e0165756ae84f6a20ad1463670e32e3fb188673efa93f61.jsonnew file mode 100644index 0000000000..8072303154--- /dev/null+++ b/.sqlx/query-be6acd044d4ad3dd1e0165756ae84f6a20ad1463670e32e3fb188673efa93f61.json@@ -0,0 +1,14 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM team WHERE id = $1",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "be6acd044d4ad3dd1e0165756ae84f6a20ad1463670e32e3fb188673efa93f61"+}diff --git a/.sqlx/query-dcfc09b1d22a1ecb939dac75303533d0a2a581f31424d0632070bfb0f0dafbd5.json b/.sqlx/query-dcfc09b1d22a1ecb939dac75303533d0a2a581f31424d0632070bfb0f0dafbd5.jsonnew file mode 100644index 0000000000..3349cc433d--- /dev/null+++ b/.sqlx/query-dcfc09b1d22a1ecb939dac75303533d0a2a581f31424d0632070bfb0f0dafbd5.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "SELECT email FROM team_joined_macro_email WHERE team_id = $1 ORDER BY email",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "email",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "dcfc09b1d22a1ecb939dac75303533d0a2a581f31424d0632070bfb0f0dafbd5"+}diff --git a/.sqlx/query-e7bd686869e6f3d54a383eea6e4a5c4a1edc9c137f9076d0ef1389edf8aadccf.json b/.sqlx/query-e7bd686869e6f3d54a383eea6e4a5c4a1edc9c137f9076d0ef1389edf8aadccf.jsonnew file mode 100644index 0000000000..aec76fc3db--- /dev/null+++ b/.sqlx/query-e7bd686869e6f3d54a383eea6e4a5c4a1edc9c137f9076d0ef1389edf8aadccf.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "DELETE FROM team_joined_macro_email WHERE team_id = $1 AND email = $2",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text"+ ]+ },+ "nullable": []+ },+ "hash": "e7bd686869e6f3d54a383eea6e4a5c4a1edc9c137f9076d0ef1389edf8aadccf"+}diff --git a/Cargo.lock b/Cargo.lockindex 0039f03211..ebb9b4deb9 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -17505,6 +17505,7 @@ dependencies = ["macro_user_id","mention_utils","messages",+ "notification","reqwest 0.13.4","rootcause","serde",@@ -17512,8 +17513,10 @@ dependencies = ["sha2 0.10.9","slack_integration","sqlx",+ "teams","tokio","tokio-util",+ "tracing","uuid","workspace-hack",]diff --git a/crates/invite_email/src/colleague_joined.rs b/crates/invite_email/src/colleague_joined.rsnew file mode 100644index 0000000000..7a45e3d458--- /dev/null+++ b/crates/invite_email/src/colleague_joined.rs@@ -0,0 +1,82 @@+use super::*;++const DISPLAY_NAME_MAX_CHARS: usize = 64;++/// Tells someone whose address matches a team's auto-join domain that a colleague joined Macro.+#[derive(Debug, Clone, Serialize, Deserialize, Template)]+#[template(path = "colleague_joined_macro.html")]+pub struct ColleagueJoinedMacro {+ /// The team the recipient joins automatically when they sign up.+ pub team_name: String,+ /// The colleague who joined Macro.+ pub joined_by: MacroUserIdStr<'static>,+ /// The colleague's display name from an untrusted source. It is shown on one line, capped,+ /// and escaped.+ pub joined_name: Option<String>,+ /// The address the recipient signs up with to join the team.+ pub recipient_email: EmailStr<'static>,+}++impl ColleagueJoinedMacro {+ fn display_name(&self) -> Option<String> {+ let words: Vec<&str> = self+ .joined_name+ .as_deref()?+ .split(|c: char| c.is_whitespace() || c.is_control())+ .filter(|word| !word.is_empty())+ .collect();+ let capped: String = words+ .join(" ")+ .chars()+ .take(DISPLAY_NAME_MAX_CHARS)+ .collect();+ let name = capped.trim_end();+ (!name.is_empty()).then(|| name.to_owned())+ }++ fn joined_display(&self) -> String {+ self.display_name()+ .unwrap_or_else(|| self.joined_email().to_owned())+ }++ fn joined_email(&self) -> &str {+ self.joined_by.email_str()+ }++ fn recipient(&self) -> &str {+ self.recipient_email.0.as_ref()+ }++ fn signup_url(&self) -> Url {+ signup_url(Environment::new_or_prod())+ }+}++impl Notification for ColleagueJoinedMacro {+ const TYPE_NAME: &'static str = "colleague_joined_macro";+}++impl NotificationExtEmail for ColleagueJoinedMacro {+ fn format_email(&self) -> EmailContent {+ EmailContent {+ subject: format!("{} joined Macro", self.joined_display()),+ body: self+ .render()+ .expect("ColleagueJoinedMacro template render failed in format_email"),+ }+ }++ fn rate_limit_config() -> RateLimitConfig {+ RateLimitConfig {+ max_count: 1,+ window: Duration::from_hours(24 * 365),+ }+ }++ fn rate_limit_key(&self) -> RateLimitKey {+ RateLimitKey::builder(&Self::TYPE_NAME)+ .append(&self.joined_by)+ .append(&self.recipient())+ .finish()+ }+}diff --git a/crates/invite_email/src/lib.rs b/crates/invite_email/src/lib.rsindex c7034fb70b..c79837fb95 100644--- a/crates/invite_email/src/lib.rs+++ b/crates/invite_email/src/lib.rs@@ -2,8 +2,8 @@//! Domain models for invitation email notifications.//!//! Contains the [`InviteToMacro`] referral notification, the [`InviteToTeamMetadata`] team-//! invitation notification, the [`ChannelInviteMetadata`] channel invitation, and the-//! [`ReferralCode`] newtype.+//! invitation notification, the [`ChannelInviteMetadata`] channel invitation, the+//! [`ColleagueJoinedMacro`] colleague join email, and the [`ReferralCode`] newtype.#[cfg(test)]mod test;@@ -11,6 +11,9 @@ mod test;mod call_invite;pub use call_invite::CallInvite;+mod colleague_joined;+pub use colleague_joined::ColleagueJoinedMacro;+use askama::Template;use macro_env::Environment;use macro_user_id::cowlike::CowLike;diff --git a/crates/invite_email/src/test.rs b/crates/invite_email/src/test.rsindex eaa4c41352..690138ed41 100644--- a/crates/invite_email/src/test.rs+++ b/crates/invite_email/src/test.rs@@ -20,6 +20,111 @@ fn call_invitation_links_directly_to_guest_join_and_escapes_titles() {assert!(email.subject.contains("[email protected]"));}+fn make_colleague_joined() -> ColleagueJoinedMacro {+ ColleagueJoinedMacro {+ team_name: "Acme".to_string(),+ joined_by: MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ joined_name: Some("Alice Example".to_string()),+ recipient_email: EmailStr::try_from("[email protected]".to_string()).unwrap(),+ }+}++#[test]+fn colleague_joined_names_the_colleague_and_links_to_signup() {+ let email = make_colleague_joined().format_email();+ assert_eq!(email.subject, "Alice Example joined Macro");+ assert!(+ .body+ .contains("Alice Example ([email protected]) joined Macro with the")+ );+ assert!(email.body.contains("<strong>Acme</strong> team."));+ assert!(+ .body+ .contains("Sign up with <strong>[email protected]</strong>")+ );+ assert!(email.body.contains("/app/signup\""));+ assert!(email.body.contains(">Join Acme</a"));+ assert!(!email.body.contains("referral_code"));+}++#[test]+fn colleague_joined_falls_back_to_the_email_without_a_usable_name() {+ for joined_name in [None, Some(" \n\t\u{7} ".to_string())] {+ let email = ColleagueJoinedMacro {+ joined_name,+ ..make_colleague_joined()+ }+ .format_email();+ assert_eq!(email.subject, "[email protected] joined Macro");+ assert!(email.body.contains("[email protected] joined Macro with the"));+ assert!(!email.body.contains("([email protected])"));+ }+}++#[test]+fn colleague_joined_puts_long_multiline_names_on_one_capped_line() {+ let email = ColleagueJoinedMacro {+ joined_name: Some(format!(" Alice\r\nExample\t\u{0}{}", "x".repeat(100))),+ ..make_colleague_joined()+ }+ .format_email();+ assert_eq!(+ email.subject,+ format!("Alice Example {} joined Macro", "x".repeat(50))+ );+}++#[test]+fn colleague_joined_escapes_the_untrusted_name_in_the_body() {+ let email = ColleagueJoinedMacro {+ joined_name: Some("<b>x</b>".to_string()),+ ..make_colleague_joined()+ }+ .format_email();+ assert_eq!(email.subject, "<b>x</b> joined Macro");+ assert!(!email.body.contains("<b>x</b>"));+ assert!(+ .body+ .contains("<b>x</b> ([email protected])")+ );+}++#[test]+fn colleague_joined_rate_limit_key_is_per_recipient() {+ let to_bob = make_colleague_joined();+ let to_carol = ColleagueJoinedMacro {+ recipient_email: EmailStr::try_from("[email protected]".to_string()).unwrap(),+ ..make_colleague_joined()+ };+ assert_eq!(+ to_bob.rate_limit_key().as_bytes(),+ to_bob.clone().rate_limit_key().as_bytes()+ );+ assert_ne!(+ to_bob.rate_limit_key().as_bytes(),+ to_carol.rate_limit_key().as_bytes()+ );+}++#[test]+fn colleague_joined_serde_round_trip_keeps_the_wire_shape() {+ let wire = serde_json::json!({+ "team_name": "Acme",+ "joined_by": "macro|[email protected]",+ "joined_name": "Alice Example",+ "recipient_email": "[email protected]",+ });+ assert_eq!(serde_json::to_value(make_colleague_joined()).unwrap(), wire);+ let parsed: ColleagueJoinedMacro = serde_json::from_value(wire).unwrap();+ assert_eq!(parsed.team_name, "Acme");+ assert_eq!(parsed.joined_by.to_string(), "macro|[email protected]");+ assert_eq!(parsed.joined_name.as_deref(), Some("Alice Example"));+ assert_eq!(String::from(parsed.recipient_email), "[email protected]");+}+fn make_invite() -> InviteToMacro {InviteToMacro {recipient_email: EmailStr::try_from("[email protected]".to_string()).unwrap(),diff --git a/crates/invite_email/templates/colleague_joined_macro.html b/crates/invite_email/templates/colleague_joined_macro.htmlnew file mode 100644index 0000000000..99f22f9faf--- /dev/null+++ b/crates/invite_email/templates/colleague_joined_macro.html@@ -0,0 +1,217 @@+<!doctype html>+<html lang="en" xmlns:o="urn:schemas-microsoft-com:office:office">+ <head>+ <meta charset="utf-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1" />+ <meta name="x-apple-disable-message-reformatting" />+ <meta+ name="format-detection"+ content="telephone=no, date=no, address=no, email=no"+ />+ <meta name="color-scheme" content="light" />+ <meta name="supported-color-schemes" content="light" />+ <title>{{ self.joined_display() }} joined Macro</title>+ <!--[if mso]>+ <noscript+ ><xml+ ><o:OfficeDocumentSettings+ ><o:PixelsPerInch>96</o:PixelsPerInch+ ><o:AllowPNG /></o:OfficeDocumentSettings></xml+ ></noscript>+ <![endif]-->+ <style>+ @media screen and (max-width: 600px) {+ .email-page {+ padding: 24px 12px !important;+ }+ .email-content {+ padding: 28px 24px !important;+ }+ .email-heading {+ font-size: 26px !important;+ line-height: 34px !important;+ }+ }+ </style>+ </head>+ <body+ style="+ margin: 0;+ padding: 0;+ width: 100%;+ background-color: #f5f5f4;+ -webkit-text-size-adjust: 100%;+ -ms-text-size-adjust: 100%;+ "+ >+ <div+ style="+ display: none;+ max-height: 0;+ overflow: hidden;+ opacity: 0;+ mso-hide: all;+ "+ >+ Sign up with your work email to join {{ team_name }} on Macro.+ </div>+ <table+ role="presentation"+ width="100%"+ cellpadding="0"+ cellspacing="0"+ bgcolor="#f5f5f4"+ style="+ font-family: Arial, Helvetica, sans-serif;+ color: #222222;+ mso-table-lspace: 0pt;+ mso-table-rspace: 0pt;+ "+ >+ <tr>+ <td class="email-page" align="center" style="padding: 48px 20px">+ <!--[if mso]><table role="presentation" width="560" cellpadding="0" cellspacing="0"><tr><td><![endif]-->+ <table+ role="presentation"+ width="100%"+ cellpadding="0"+ cellspacing="0"+ style="max-width: 560px; table-layout: fixed"+ >+ <tr>+ <td+ class="email-content"+ bgcolor="#ffffff"+ style="+ padding: 36px 40px 40px;+ border: 1px solid #e7e7e5;+ border-radius: 12px;+ overflow-wrap: break-word;+ word-wrap: break-word;+ "+ >+ <table+ role="presentation"+ cellpadding="0"+ cellspacing="0"+ style="margin-bottom: 36px"+ >+ <tr>+ <td width="36" style="vertical-align: middle">+ <a href="https://macro.com" style="text-decoration: none"+ ><img+ src="https://static-file-service.macro.com/file/b911aba9-94a6-435c-aa16-8404739f13b0"+ width="36"+ height="36"+ alt="Macro"+ style="+ display: block;+ border: 0;+ background-color: #ffffff;+ -ms-interpolation-mode: bicubic;+ "+ /></a>+ </td>+ </tr>+ </table>+ <h1+ class="email-heading"+ style="+ margin: 0 0 16px;+ font-size: 28px;+ line-height: 36px;+ font-weight: 600;+ letter-spacing: -0.5px;+ color: #222222;+ "+ >+ {{ team_name }} is on Macro.+ </h1>+ <p+ style="+ margin: 0 0 20px;+ font-size: 16px;+ line-height: 26px;+ color: #525252;+ "+ >+ {% if let Some(name) = self.display_name() -%}+ {{ name }} ({{ self.joined_email() }})+ {%- else -%}+ {{ self.joined_email() }}+ {%- endif %} joined Macro with the+ <strong>{{ team_name }}</strong> team.+ </p>+ <p+ style="+ margin: 0 0 20px;+ font-size: 16px;+ line-height: 26px;+ color: #525252;+ "+ >+ Sign up with <strong>{{ self.recipient() }}</strong> to join+ {{ team_name }} automatically.+ </p>+ <table+ role="presentation"+ cellpadding="0"+ cellspacing="0"+ style="margin: 28px 0 8px; max-width: 100%"+ >+ <tr>+ <td+ bgcolor="#222222"+ style="+ border-radius: 6px;+ text-align: center;+ mso-padding-alt: 14px 24px;+ "+ >+ <a+ href="{{ self.signup_url() }}"+ style="+ display: inline-block;+ padding: 14px 24px;+ border: 1px solid #222222;+ border-radius: 6px;+ color: #ffffff;+ background-color: #222222;+ font-size: 15px;+ line-height: 20px;+ font-weight: 600;+ text-decoration: none;+ mso-padding-alt: 0;+ "+ >Join {{ team_name }}</a+ >+ </td>+ </tr>+ </table>+ </td>+ </tr>+ <tr>+ <td+ style="+ padding: 24px 16px 0;+ text-align: center;+ font-size: 12px;+ line-height: 20px;+ color: #737373;+ "+ >+ Need a hand?+ <a+ href="mailto:[email protected]"+ style="color: #525252; text-decoration: underline"+ >Contact support</a+ >+ </td>+ </tr>+ </table>+ <!--[if mso]></td></tr></table><![endif]-->+ </td>+ </tr>+ </table>+ </body>+</html>diff --git a/crates/macro_db_client/migrations/20261005155630_team_joined_macro_email.sql b/crates/macro_db_client/migrations/20261005155630_team_joined_macro_email.sqlnew file mode 100644index 0000000000..f5ab7ad759--- /dev/null+++ b/crates/macro_db_client/migrations/20261005155630_team_joined_macro_email.sql@@ -0,0 +1,6 @@+CREATE TABLE team_joined_macro_email (+ team_id UUID NOT NULL REFERENCES team (id) ON DELETE CASCADE,+ email TEXT NOT NULL,+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),+ PRIMARY KEY (team_id, email)+);diff --git a/crates/model_notifications/src/digest_state.rs b/crates/model_notifications/src/digest_state.rsindex 684bd0f771..41e9610c98 100644--- a/crates/model_notifications/src/digest_state.rs+++ b/crates/model_notifications/src/digest_state.rs@@ -1,13 +1,17 @@-use crate::{InviteToTeamMetadata, NewEmailMetadata};+use crate::{ColleagueJoinedMacro, InviteToTeamMetadata, NewEmailMetadata};use invite_email::InviteToMacro;use notification::domain::models::email_notification_digest::{EmailBlockList, NotificationSetBuilder,};+#[cfg(test)]+mod test;+/// define a blocklist of notification types which will never be templated into a digest emailpub fn digest_email_block_list() -> EmailBlockList {EmailBlockList::new::<NewEmailMetadata>().append::<InviteToTeamMetadata>().append::<InviteToMacro>().append::<invite_email::CallInvite>()+ .append::<ColleagueJoinedMacro>()}diff --git a/crates/model_notifications/src/digest_state/test.rs b/crates/model_notifications/src/digest_state/test.rsnew file mode 100644index 0000000000..ece8bc6939--- /dev/null+++ b/crates/model_notifications/src/digest_state/test.rs@@ -0,0 +1,39 @@+use super::*;+use crate::ChannelMentionMetadata;+use chrono::Utc;+use macro_user_id::user_id::MacroUserIdStr;+use model_entity::EntityType;+use notification::domain::models::{Notification, NotificationState, UserNotificationRow};+use std::sync::Arc;++fn row_of<T: Notification>() -> UserNotificationRow<Arc<()>> {+ UserNotificationRow {+ owner_id: MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ notification_id: uuid::Uuid::nil(),+ notification_event_type: T::TYPE_NAME.to_string(),+ entity: EntityType::Team.with_entity_str("11111111-1111-1111-1111-111111111111"),+ sent: false,+ state: NotificationState::Unseen,+ created_at: Utc::now(),+ viewed_at: None,+ updated_at: Utc::now(),+ deleted_at: None,+ notification_metadata: Arc::new(()),+ sender_id: None,+ }+}++#[test]+fn colleague_joined_macro_never_lands_in_a_digest() {+ let block_list = digest_email_block_list();+ assert!(+ block_list+ .notification_is_allowed(row_of::<ColleagueJoinedMacro>())+ .is_right()+ );+ assert!(+ block_list+ .notification_is_allowed(row_of::<ChannelMentionMetadata>())+ .is_left()+ );+}diff --git a/crates/model_notifications/src/lib.rs b/crates/model_notifications/src/lib.rsindex 8a15a95ed1..0f9d4de564 100644--- a/crates/model_notifications/src/lib.rs+++ b/crates/model_notifications/src/lib.rs@@ -14,15 +14,16 @@ pub use metadata::{AgentSessionSettledMetadata, AgentSessionWaitingForInputMetadata, AiResponseMetadata,CalendarEventReminderMetadata, CallStartedMetadata, ChannelInviteMetadata,ChannelMentionMetadata, ChannelMessageReactionMetadata, ChannelMessageSendMetadata,- ChannelReplyMetadata, ChannelType, CommentedOnDocumentMetadata, CommonChannelMetadata,- CrmDiscussionMetadata, CrmDiscussionReason, DocumentMentionMetadata, GithubPrCheckRun,- GithubPrCheckRunState, GithubPrComment, GithubPrCommentKind, GithubPrEventAction,- GithubPrEventStatus, GithubPrMention, GithubPrMentionLocation, GithubPrNotificationCommon,- GithubPrReview, GithubPrReviewState, GithubPrStatusChanged, GithubReviewRequested,- InboxReauthRequiredMetadata, InitiativeDiscussionMetadata, InitiativeDiscussionReason,- InviteToTeamMetadata, ItemSharedMetadata, MentionedInDocumentCommentMetadata, NewEmailMetadata,- NotificationDocumentSubType, NotificationTitle, ReminderMetadata,- RepliedToDocumentCommentThreadMetadata, TaskAssignedMetadata,+ ChannelReplyMetadata, ChannelType, ColleagueJoinedMacro, CommentedOnDocumentMetadata,+ CommonChannelMetadata, CrmDiscussionMetadata, CrmDiscussionReason, DocumentMentionMetadata,+ GithubPrCheckRun, GithubPrCheckRunState, GithubPrComment, GithubPrCommentKind,+ GithubPrEventAction, GithubPrEventStatus, GithubPrMention, GithubPrMentionLocation,+ GithubPrNotificationCommon, GithubPrReview, GithubPrReviewState, GithubPrStatusChanged,+ GithubReviewRequested, InboxReauthRequiredMetadata, InitiativeDiscussionMetadata,+ InitiativeDiscussionReason, InviteToTeamMetadata, ItemSharedMetadata,+ MentionedInDocumentCommentMetadata, NewEmailMetadata, NotificationDocumentSubType,+ NotificationTitle, ReminderMetadata, RepliedToDocumentCommentThreadMetadata,+ TaskAssignedMetadata,};pub use unsubscribe::UserUnsubscribe;#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]diff --git a/crates/model_notifications/src/metadata.rs b/crates/model_notifications/src/metadata.rsindex 0cdd006a58..7e843938ec 100644--- a/crates/model_notifications/src/metadata.rs+++ b/crates/model_notifications/src/metadata.rs@@ -1,5 +1,5 @@use chrono::{DateTime, NaiveDate, Utc};-pub use invite_email::{ChannelInviteMetadata, InviteToTeamMetadata};+pub use invite_email::{ChannelInviteMetadata, ColleagueJoinedMacro, InviteToTeamMetadata};use macro_user_id::cowlike::CowLike;use macro_user_id::{email::ReadEmailParts, user_id::MacroUserIdStr};use mention_utils::parse::{ParsedXmlText, PlainTextFormatter, XmlFormatter};diff --git a/crates/slack_integration/src/domain/importer.rs b/crates/slack_integration/src/domain/importer.rsindex 8a19fa2eea..e550836f87 100644--- a/crates/slack_integration/src/domain/importer.rs+++ b/crates/slack_integration/src/domain/importer.rs@@ -24,7 +24,7 @@ pub mod targets;use targets::{ImportTargets, TargetFacts, TargetKind, TargetPlan};-impl<R, S, L, T, H, A> ImportWorker for ConversationImporter<R, S, L, T, H, A>+impl<R, S, L, T, H, A, J> ImportWorker for ConversationImporter<R, S, L, T, H, A, J>whereR: ExecutionRepo,S: ImportStorage,@@ -32,6 +32,7 @@ whereT: ImportTargets,H: HistoricalSink,A: ImportAuthorizer,+ J: JoinAnnouncer,{async fn claim(&self, event: &ImportEvent, owner: WorkerId) -> PortResult<ClaimOutcome> {self.claim(event, owner).await@@ -56,18 +57,23 @@ pub struct ImporterConfig {}/// Domain workflow over persistence, storage, ledger, target and authorization ports.-pub struct ConversationImporter<R, S, L, T, H, A> {+pub struct ConversationImporter<R, S, L, T, H, A, J> {repo: R,storage: S,ledger: L,targets: T,sink: H,authorizer: A,+ announcer: J,limits: ImportLimits,}-impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A> {+impl<R, S, L, T, H, A, J> ConversationImporter<R, S, L, T, H, A, J> {/// Construct with validated bounds; no environment access in the domain.+ #[expect(+ clippy::too_many_arguments,+ reason = "each persistence port stays a separate generic at the composition root"+ )]pub fn new(repo: R,storage: S,@@ -75,6 +81,7 @@ impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A> {targets: T,sink: H,authorizer: A,+ announcer: J,config: ImporterConfig,) -> Result<Self, ImportError> {let ImporterConfig { limits } = config;@@ -96,12 +103,13 @@ impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A> {targets,sink,authorizer,+ announcer,limits,})}}-impl<R, S, L, T, H, A> ConversationImporter<R, S, L, T, H, A>+impl<R, S, L, T, H, A, J> ConversationImporter<R, S, L, T, H, A, J>whereR: ExecutionRepo,S: ImportStorage,@@ -109,6 +117,7 @@ whereT: ImportTargets,H: HistoricalSink,A: ImportAuthorizer,+ J: JoinAnnouncer,{/// Revalidate the durable requester BEFORE claiming/reclaiming. An unauthorized/// request cannot acquire a lease; the driver reconciles exhausted deliveries.@@ -336,9 +345,34 @@ where}self.ledger.complete(&reservation, metadata.kind).await?;self.targets.bind(&context.lease, &plan, &warnings).await?;+ self.announce_join(context.lease.event.job_id, &plan, users)+ .await;Ok(Some(plan))}+ async fn announce_join(&self, job: JobId, plan: &TargetPlan, users: &UserDirectory) {+ let mut members: Vec<_> = plan.members.iter().cloned().collect();+ members.sort_by(|left, right| left.as_ref().cmp(right.as_ref()));+ let candidate_count = members.len();+ let announcement = JoinAnnouncement {+ team_id: plan.team_id,+ joined: plan.requested_by.clone(),+ joined_name: users.name_of(&plan.requested_by).map(str::to_owned),+ members,+ };+ if let Err(e) = self.announcer.announce(announcement).await {+ tracing::warn!(+ team_id = %plan.team_id,+ job_id = %job,+ conversation_id = %plan.metadata.slack_channel_id,+ channel_id = %plan.channel_id,+ candidate_count,+ error = ?e,+ "colleague join announcement failed"+ );+ }+ }+async fn authorize(&self,context: &ClaimedConversation,diff --git a/crates/slack_integration/src/domain/importer/test.rs b/crates/slack_integration/src/domain/importer/test.rsindex 539ee452cb..2b20a8aba3 100644--- a/crates/slack_integration/src/domain/importer/test.rs+++ b/crates/slack_integration/src/domain/importer/test.rs@@ -31,12 +31,16 @@ struct State {receipts: Vec<Uuid>,polls: Vec<Uuid>,recorded_search: Vec<SearchState>,+ announcements: Vec<JoinAnnouncement>,+ fail_announce: bool,+ fail_bind: bool,+ trace: Vec<&'static str>,}#[derive(Clone, Default)]struct Fake(Arc<Mutex<State>>);-type Importer = ConversationImporter<Fake, Fake, Fake, Fake, Fake, Fake>;+type Importer = ConversationImporter<Fake, Fake, Fake, Fake, Fake, Fake, Fake>;fn user(email: &str) -> MacroUserIdStr<'static> {MacroUserIdStr::try_from_email(email).unwrap()@@ -56,6 +60,7 @@ fn importer(fake: &Fake, limits: ImportLimits) -> Importer {fake.clone(),fake.clone(),fake.clone(),+ fake.clone(),ImporterConfig { limits },).unwrap()@@ -92,6 +97,7 @@ fn fixture(kind: ConversationKind, parts: &[&str]) -> (Fake, ClaimedConversationlet job_id = Uuid::now_v7().try_into().unwrap();let slack_channel_id: ConversationId = "C123".parse().unwrap();let users = upload(&fake, UploadId::Users, "users", serde_json::to_vec(&serde_json::json!([+ {"id":"U0","name":"importer","profile":{"email":"[email protected]","display_name":"Importer Admin"}},{"id":"U1","name":"alice","profile":{"email":"[email protected]","display_name":"Alice"}},{"id":"U2","name":"bob","profile":{"email":"[email protected]","display_name":"Bob"}},{"id":"U3","name":"phantom"}@@ -267,6 +273,10 @@ impl ImportTargets for Fake {warnings: &[ImportWarning],) -> PortResult<()> {let mut state = self.0.lock().unwrap();+ if state.fail_bind {+ return Err(ImportError::Internal.into());+ }+ state.trace.push("bind");state.plans.push(plan.clone());state.warnings.extend_from_slice(warnings);Ok(())@@ -277,6 +287,19 @@ impl ImportTargets for Fake {}}+impl JoinAnnouncer for Fake {+ async fn announce(&self, announcement: JoinAnnouncement) -> PortResult<()> {+ let mut state = self.0.lock().unwrap();+ state.trace.push("announce");+ state.announcements.push(announcement);+ if state.fail_announce {+ Err(ImportError::Internal.into())+ } else {+ Ok(())+ }+ }+}+impl HistoricalSink for Fake {async fn lookup(&self,@@ -962,3 +985,130 @@ async fn failed_missing_and_stale_search_receipts_retry_and_only_publication_com.all(|scope| scope == &(request.job_id, request.channel_ids.clone(), 7)));}++#[tokio::test]+async fn new_public_channel_announces_sorted_members_once_after_bind() {+ let (fake, context) = fixture(ConversationKind::PublicChannel, &[]);+ importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap();+ let state = fake.0.lock().unwrap();+ assert_eq!(state.trace, ["bind", "announce"]);+ assert_eq!(state.announcements.len(), 1);+ let announcement = &state.announcements[0];+ assert_eq!(announcement.team_id, context.team_id);+ assert_eq!(announcement.joined, user("[email protected]"));+ assert_eq!(announcement.joined_name.as_deref(), Some("Importer Admin"));+ assert_eq!(+ announcement.members,+ vec![+ user("[email protected]"),+ user("[email protected]"),+ user("[email protected]"),+ ]+ );+ assert_eq!(state.settlements, [(ConversationStatus::Completed, None)]);+}++#[tokio::test]+async fn resolved_dm_announces_its_two_members() {+ let (fake, context) = fixture(ConversationKind::DirectMessage, &[]);+ importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap();+ let state = fake.0.lock().unwrap();+ assert_eq!(state.announcements.len(), 1);+ let announcement = &state.announcements[0];+ assert_eq!(announcement.joined, context.requested_by);+ assert_eq!(announcement.joined_name.as_deref(), Some("Importer Admin"));+ assert_eq!(+ announcement.members,+ vec![user("[email protected]"), user("[email protected]")]+ );+ assert!(!announcement.members.contains(&context.requested_by));+}++#[tokio::test]+async fn unresolvable_dm_announces_nothing() {+ let (fake, mut context) = fixture(ConversationKind::DirectMessage, &[]);+ context.metadata.member_ids = vec!["U1".parse().unwrap(), "U3".parse().unwrap()];+ importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap();+ let state = fake.0.lock().unwrap();+ assert!(state.announcements.is_empty());+ assert!(state.trace.is_empty());+ assert_eq!(state.settlements[0].0, ConversationStatus::Skipped);+}++#[tokio::test]+async fn unauthorized_existing_target_announces_nothing() {+ let (fake, context) = fixture(ConversationKind::PublicChannel, &[]);+ let id = Uuid::now_v7();+ {+ let mut state = fake.0.lock().unwrap();+ state.reserved = Some(id);+ state.targets.insert(+ id,+ TargetFacts {+ id,+ kind: TargetKind::Team(context.team_id),+ dm_members: HashSet::new(),+ },+ );+ state.target_denied = true;+ }+ importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap();+ let state = fake.0.lock().unwrap();+ assert!(state.announcements.is_empty());+ assert!(state.plans.is_empty());+ assert!(state.trace.is_empty());+ assert_eq!(+ state.settlements,+ [(ConversationStatus::Skipped, Some(ImportError::Unavailable))]+ );+}++#[tokio::test]+async fn failed_bind_announces_nothing() {+ let (fake, context) = fixture(ConversationKind::PublicChannel, &[]);+ fake.0.lock().unwrap().fail_bind = true;+ let error = importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap_err();+ assert_eq!(error.into_current_context(), ImportError::Internal);+ let state = fake.0.lock().unwrap();+ assert!(state.announcements.is_empty());+ assert!(state.plans.is_empty());+ assert!(state.trace.is_empty());+ assert!(state.settlements.is_empty());+}++#[tokio::test]+async fn announcer_error_still_completes_after_one_announcement() {+ let (fake, context) = fixture(ConversationKind::PublicChannel, &[]);+ fake.0.lock().unwrap().fail_announce = true;+ importer(&fake, ImportLimits::default())+ .import(&context)+ .await+ .unwrap();+ let state = fake.0.lock().unwrap();+ assert_eq!(state.trace, ["bind", "announce"]);+ assert_eq!(state.announcements.len(), 1);+ assert_eq!(+ state.announcements[0].members,+ vec![+ user("[email protected]"),+ user("[email protected]"),+ user("[email protected]"),+ ]+ );+ assert_eq!(state.settlements, [(ConversationStatus::Completed, None)]);+}diff --git a/crates/slack_integration/src/domain/ports.rs b/crates/slack_integration/src/domain/ports.rsindex 4ac5527a0d..c8ba5ab2d8 100644--- a/crates/slack_integration/src/domain/ports.rs+++ b/crates/slack_integration/src/domain/ports.rs@@ -339,6 +339,31 @@ pub trait ImportProgressAccess: Send + Sync + 'static {) -> impl Future<Output = PortResult<Vec<Uuid>>> + Send;}+/// Members of a conversation that just bound, offered for the "x joined Macro" email.+/// Built by the importer after `ImportTargets::bind` succeeded; may include `joined`.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct JoinAnnouncement {+ /// Importing team.+ pub team_id: TeamId,+ /// The importing admin, "x".+ pub joined: MacroUserIdStr<'static>,+ /// x's archive display name when the export has one; untrusted.+ pub joined_name: Option<String>,+ /// `TargetPlan.members`, sorted for determinism.+ pub members: Vec<MacroUserIdStr<'static>>,+}++/// Best-effort colleague email. Called on every attempt and re-import; implementations+/// must be idempotent per (team, recipient) forever. The importer logs errors and never+/// lets them change the import result.+pub trait JoinAnnouncer: Send + Sync + 'static {+ /// Offer this conversation's resolved members. Idempotent for the life of the team.+ fn announce(+ &self,+ announcement: JoinAnnouncement,+ ) -> impl Future<Output = PortResult<()>> + Send;+}+/// Best-effort invalidation only; persisted polling is the source of truth.pub trait ImportNotifier: Send + Sync + 'static {/// Notify the requesting administrator's gateway entity of a job revision usingdiff --git a/crates/slack_integration/src/domain/slack/users.rs b/crates/slack_integration/src/domain/slack/users.rsindex b1b94ca209..3835b5742d 100644--- a/crates/slack_integration/src/domain/slack/users.rs+++ b/crates/slack_integration/src/domain/slack/users.rs@@ -122,6 +122,18 @@ impl UserDirectory {Ok(directory)}+ /// Display name of the first non-bot export user, in Slack id order, whose+ /// [`Self::participant`] is `user`. Precedence is profile display name, real name,+ /// then username. `None` when no such user has a name. Archive text is untrusted.+ pub fn name_of(&self, user: &MacroUserIdStr<'_>) -> Option<&str> {+ self.users+ .iter()+ .find_map(|(id, export)| {+ (self.participant(id).as_ref() == Some(user)).then(|| export.display_name())+ })+ .flatten()+ }+/// Display name for source membership labels, including no-email/unknown members.pub fn display_name<'a>(&'a self, id: &'a SlackUserId) -> &'a str {self.usersdiff --git a/crates/slack_integration/src/domain/slack/users/test.rs b/crates/slack_integration/src/domain/slack/users/test.rsindex 0ea9b4af33..13fe89da68 100644--- a/crates/slack_integration/src/domain/slack/users/test.rs+++ b/crates/slack_integration/src/domain/slack/users/test.rs@@ -182,6 +182,51 @@ fn invalid_emails_and_blank_display_names_fall_back_safely() {}}+fn directory_from(json: serde_json::Value) -> UserDirectory {+ UserDirectory::new(serde_json::from_value::<Vec<ExportUser>>(json).unwrap()).unwrap()+}++#[test]+fn name_of_matches_email_case_insensitively() {+ let directory = directory_from(serde_json::json!([{+ "id": "U17",+ "name": "ignored",+ "real_name": "Also ignored",+ "profile": {+ "email": "[email protected]",+ "display_name": "T Seventeen",+ "real_name": "Ignored Real"+ }+ }]));+ let id = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ assert_eq!(directory.name_of(&id), Some("T Seventeen"));+}++#[test]+fn name_of_is_the_first_slack_id_when_emails_collide() {+ let directory = directory_from(serde_json::json!([+ {"id":"U2","name":"Second","profile":{"email":"[email protected]","display_name":"Second"}},+ {"id":"U1","name":"First","profile":{"email":"[email protected]","display_name":"First"}}+ ]));+ let id = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ assert_eq!(directory.name_of(&id), Some("First"));+}++#[test]+fn name_of_is_none_for_unknown_or_bot_only_matches() {+ let directory = directory_from(serde_json::json!([+ {"id":"U0","name":"Bot","is_bot":true,"profile":{"email":"[email protected]","display_name":"Bot Name"}},+ {"id":"U8","name":"Only Bot","is_bot":true,"profile":{"email":"[email protected]","display_name":"Only Bot"}},+ {"id":"U9","name":"Human","profile":{"email":"[email protected]","display_name":"Human Name"}}+ ]));+ let t17 = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let bot_only = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ let unknown = MacroUserIdStr::try_from_email("[email protected]").unwrap();+ assert_eq!(directory.name_of(&t17), Some("Human Name"));+ assert_eq!(directory.name_of(&bot_only), None);+ assert_eq!(directory.name_of(&unknown), None);+}+#[test]fn repeated_ids_are_idempotent_but_conflicting_ids_fail_closed() {let user: ExportUser =diff --git a/crates/teams/Cargo.toml b/crates/teams/Cargo.tomlindex 9e5446ed23..132b765cfa 100644--- a/crates/teams/Cargo.toml+++ b/crates/teams/Cargo.toml@@ -79,6 +79,7 @@ non_empty = { path = "../non_empty" }roles_and_permissions = { path = "../roles_and_permissions", default-features = false, features = ["ports",] }+rootcause = { workspace = true }schemars = { workspace = true, optional = true }serde = { workspace = true }thiserror = { workspace = true }@@ -115,7 +116,6 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }[dev-dependencies]macro_db_migrator = { path = "../macro_db_migrator" }models_pagination = { path = "../models_pagination" }-rootcause = { workspace = true }serde_json = { workspace = true }tokio = { workspace = true }tower = { workspace = true }diff --git a/crates/teams/src/domain.rs b/crates/teams/src/domain.rsindex 9f575607fe..a378a8167a 100644--- a/crates/teams/src/domain.rs+++ b/crates/teams/src/domain.rs@@ -2,6 +2,8 @@ pub mod contacts_enqueuer;pub mod crm_enqueuer;pub mod customer_repo;pub mod events;+#[cfg(feature = "ports")]+pub mod join_announcement;pub mod model;pub mod open_seat_release;pub mod team_analytics;diff --git a/crates/teams/src/domain/join_announcement.rs b/crates/teams/src/domain/join_announcement.rsnew file mode 100644index 0000000000..65445cf776--- /dev/null+++ b/crates/teams/src/domain/join_announcement.rs@@ -0,0 +1,177 @@+//! "x joined Macro" emails to colleagues who share a team's auto-join domain.+//!+//! Each address is claimed once per team, forever, before its email is enqueued. The claim+//! decides eligibility. This module only shapes and sends the emails it accepted.++#[cfg(test)]+mod test;++use std::collections::HashSet;++use macro_user_id::{cowlike::CowLike, email::EmailStr, user_id::MacroUserIdStr};+use model_entity::EntityType;+use model_notifications::ColleagueJoinedMacro;+use notification::domain::{models::SendNotificationRequestBuilder, service::NotificationIngress};+use rootcause::Report;+use uuid::Uuid;++const MAX_UNSENT_CLAIMS_PER_CRASH: usize = 50;++const NOTIFICATION_ID_NAMESPACE: Uuid = Uuid::from_u128(0x7465_616d_2d6a_6f69_6e65_642d_6d61_6372);++/// A user who joined a team, and the colleagues who might hear about it.+#[derive(Debug, Clone)]+pub struct AnnounceJoin {+ /// The team the user joined.+ pub team_id: Uuid,+ /// The user who joined.+ pub joined: MacroUserIdStr<'static>,+ /// The joined user's display name from an untrusted source.+ pub joined_name: Option<String>,+ /// Possible recipients. They may include `joined` and duplicates.+ pub candidates: Vec<MacroUserIdStr<'static>>,+}++/// Counts from one announcement. It never carries addresses.+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]+pub struct AnnounceReport {+ /// Addresses claimed for an email.+ pub claimed: u32,+ /// Emails enqueued.+ pub sent: u32,+ /// Claims released because their email could not be enqueued.+ pub released: u32,+}++/// Errors from announcing a join.+#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]+pub enum JoinAnnouncementError {+ /// Reading or writing the claims failed.+ #[error("join announcement storage failed")]+ Storage,+}++/// A recipient that the claim accepted.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct ClaimedJoinEmail {+ /// The colleague to email.+ pub recipient: MacroUserIdStr<'static>,+ /// The name of the team.+ pub team_name: String,+}++/// Storage for the once-per-address claims.+pub trait JoinAnnouncementRepository: Send + Sync + 'static {+ /// Claims every candidate who matches the team's auto-join domain, is neither a member nor+ /// invited, and was never claimed for this team. It is one statement, so concurrent claims+ /// of an address have exactly one winner. Returns nothing when the team has no auto-join+ /// domain.+ fn claim(+ &self,+ team_id: Uuid,+ candidates: &[MacroUserIdStr<'static>],+ ) -> impl Future<Output = Result<Vec<ClaimedJoinEmail>, Report<JoinAnnouncementError>>> + Send;++ /// Undoes one claim whose email was not enqueued, so a later announcement can retry it.+ fn release(+ &self,+ team_id: Uuid,+ recipient: &MacroUserIdStr<'_>,+ ) -> impl Future<Output = Result<(), Report<JoinAnnouncementError>>> + Send;+}++/// Tells colleagues that someone joined Macro.+pub trait JoinAnnouncementService: Send + Sync + 'static {+ /// Emails each candidate other than `joined` whom+ /// [`JoinAnnouncementRepository::claim`] accepts. Every address is claimed before its+ /// email is enqueued.+ fn announce_join(+ &self,+ request: AnnounceJoin,+ ) -> impl Future<Output = Result<AnnounceReport, Report<JoinAnnouncementError>>> + Send;+}++/// The [`JoinAnnouncementService`] over a claim repository and the notification ingress.+#[derive(Clone)]+pub struct JoinAnnouncementServiceImpl<R, N> {+ repository: R,+ ingress: N,+}++impl<R, N> JoinAnnouncementServiceImpl<R, N> {+ /// Creates the service.+ pub fn new(repository: R, ingress: N) -> Self {+ Self {+ repository,+ ingress,+ }+ }+}++fn idempotent_notification_id(team_id: Uuid, recipient: &MacroUserIdStr<'_>) -> Uuid {+ Uuid::new_v5(+ &NOTIFICATION_ID_NAMESPACE,+ format!("{team_id}:{}", recipient.email_str()).as_bytes(),+ )+}++impl<R, N> JoinAnnouncementService for JoinAnnouncementServiceImpl<R, N>+where+ R: JoinAnnouncementRepository,+ N: NotificationIngress,+{+ #[tracing::instrument(skip_all, fields(team_id = %request.team_id), err)]+ async fn announce_join(+ &self,+ request: AnnounceJoin,+ ) -> Result<AnnounceReport, Report<JoinAnnouncementError>> {+ let AnnounceJoin {+ team_id,+ joined,+ joined_name,+ mut candidates,+ } = request;+ candidates.retain(|candidate| *candidate != joined);+ candidates.sort_by(|a, b| a.email_str().cmp(b.email_str()));+ candidates.dedup();++ let mut report = AnnounceReport::default();+ for chunk in candidates.chunks(MAX_UNSENT_CLAIMS_PER_CRASH) {+ for ClaimedJoinEmail {+ recipient,+ team_name,+ } in self.repository.claim(team_id, chunk).await?+ {+ report.claimed += 1;+ let request = SendNotificationRequestBuilder {+ notification_entity: EntityType::Team.with_entity_string(team_id.to_string()),+ secondary_notification_entity: None,+ sender_id: Some(joined.clone()),+ recipient_ids: HashSet::from([recipient.clone()]),+ notification: ColleagueJoinedMacro {+ team_name,+ joined_by: joined.clone(),+ joined_name: joined_name.clone(),+ recipient_email: EmailStr(recipient.email_part()).into_owned(),+ },+ }+ .into_request_with_id(idempotent_notification_id(team_id, &recipient))+ .with_email();+ match self.ingress.send_notification(request).await {+ Ok(_) => report.sent += 1,+ Err(error) => {+ tracing::warn!(+ %team_id,+ error = ?error,+ "join email enqueue failed; releasing the claim"+ );+ if self.repository.release(team_id, &recipient).await.is_ok() {+ report.released += 1;+ }+ }+ }+ }+ }+ Ok(report)+ }+}diff --git a/crates/teams/src/domain/join_announcement/test.rs b/crates/teams/src/domain/join_announcement/test.rsnew file mode 100644index 0000000000..80208277e1--- /dev/null+++ b/crates/teams/src/domain/join_announcement/test.rs@@ -0,0 +1,301 @@+use std::{collections::HashSet, sync::Mutex};++use notification::domain::{+ models::{Notification, NotificationResult, request::SendNotificationRequest},+ service::SendNotificationError,+};+use serde_json::json;++use super::*;++const TEAM: Uuid = Uuid::from_u128(0x1111_1111_1111_1111_1111_1111_1111_1111);++fn user(email: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email(email).unwrap()+}++fn announce(candidates: &[&str]) -> AnnounceJoin {+ AnnounceJoin {+ team_id: TEAM,+ joined: user("[email protected]"),+ joined_name: Some("Alice Example".to_owned()),+ candidates: candidates.iter().map(|email| user(email)).collect(),+ }+}++fn strings(values: &[&str]) -> Vec<String> {+ values.iter().map(|value| value.to_string()).collect()+}++#[derive(Default)]+struct FakeRepository {+ eligible: HashSet<String>,+ fail_claims: bool,+ claimed: Mutex<HashSet<String>>,+ claim_calls: Mutex<Vec<Vec<String>>>,+ released: Mutex<Vec<String>>,+}++impl FakeRepository {+ fn eligible(emails: &[&str]) -> Self {+ Self {+ eligible: strings(emails).into_iter().collect(),+ ..Self::default()+ }+ }+}++impl JoinAnnouncementRepository for FakeRepository {+ async fn claim(+ &self,+ _team_id: Uuid,+ candidates: &[MacroUserIdStr<'static>],+ ) -> Result<Vec<ClaimedJoinEmail>, Report<JoinAnnouncementError>> {+ self.claim_calls.lock().unwrap().push(+ candidates+ .iter()+ .map(|candidate| candidate.email_str().to_owned())+ .collect(),+ );+ if self.fail_claims {+ return Err(Report::new(JoinAnnouncementError::Storage));+ }+ let mut claimed = self.claimed.lock().unwrap();+ Ok(candidates+ .iter()+ .filter(|candidate| {+ self.eligible.contains(candidate.email_str())+ && claimed.insert(candidate.email_str().to_owned())+ })+ .map(|candidate| ClaimedJoinEmail {+ recipient: candidate.clone(),+ team_name: "Acme".to_owned(),+ })+ .collect())+ }++ async fn release(+ &self,+ _team_id: Uuid,+ recipient: &MacroUserIdStr<'_>,+ ) -> Result<(), Report<JoinAnnouncementError>> {+ self.claimed.lock().unwrap().remove(recipient.email_str());+ self.released+ .lock()+ .unwrap()+ .push(recipient.email_str().to_owned());+ Ok(())+ }+}++#[derive(Default)]+struct FakeIngress {+ failing: HashSet<String>,+ requests: Mutex<Vec<serde_json::Value>>,+}++impl FakeIngress {+ fn failing_for(recipient_id: &str) -> Self {+ Self {+ failing: HashSet::from([recipient_id.to_owned()]),+ ..Self::default()+ }+ }+}++impl NotificationIngress for FakeIngress {+ fn send_notification<+ 'a,+ T: Notification + Clone + 'static,+ U: serde::Serialize + Send + Sync + 'static,+ >(+ &'a self,+ req: SendNotificationRequest<'a, T, U>,+ ) -> impl Future<Output = Result<Option<NotificationResult<'a>>, Report<SendNotificationError>>> + Send+ {+ let request = serde_json::to_value(&req).unwrap();+ let fails = request["req"]["recipient_ids"]+ .as_array()+ .unwrap()+ .iter()+ .any(|recipient| self.failing.contains(recipient.as_str().unwrap()));+ self.requests.lock().unwrap().push(request);+ async move {+ if fails {+ Err(Report::new(SendNotificationError::Other))+ } else {+ Ok(None)+ }+ }+ }+}++#[tokio::test]+async fn nobody_claimed_means_no_email() {+ let service =+ JoinAnnouncementServiceImpl::new(FakeRepository::default(), FakeIngress::default());++ let report = service+ .announce_join(announce(&["[email protected]"]))+ .await+ .unwrap();++ assert_eq!(report, AnnounceReport::default());+ assert_eq!(+ *service.repository.claim_calls.lock().unwrap(),+ vec![strings(&["[email protected]"])]+ );+ assert!(service.ingress.requests.lock().unwrap().is_empty());+}++#[tokio::test]+async fn the_joiner_and_duplicates_are_dropped_before_the_claim() {+ let service =+ JoinAnnouncementServiceImpl::new(FakeRepository::default(), FakeIngress::default());++ service+ .announce_join(announce(&[+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ ]))+ .await+ .unwrap();++ assert_eq!(+ *service.repository.claim_calls.lock().unwrap(),+ vec![strings(&["[email protected]", "[email protected]"])]+ );+}++#[tokio::test]+async fn a_claimed_colleague_gets_one_email_from_the_joiner() {+ let service = JoinAnnouncementServiceImpl::new(+ FakeRepository::eligible(&["[email protected]"]),+ FakeIngress::default(),+ );++ let report = service+ .announce_join(announce(&["[email protected]", "[email protected]"]))+ .await+ .unwrap();++ assert_eq!(+ report,+ AnnounceReport {+ claimed: 1,+ sent: 1,+ released: 0+ }+ );+ let requests = service.ingress.requests.lock().unwrap();+ let [request] = requests.as_slice() else {+ panic!("expected one request, got {requests:?}");+ };+ assert_eq!(+ request["uuid_to_write"],+ "aaf6d3a5-abab-5fea-8baa-01aa7a385f8d"+ );+ assert_eq!(+ request["req"]["notification_entity"],+ json!({"entity_type": "team", "entity_id": "11111111-1111-1111-1111-111111111111"})+ );+ assert_eq!(+ request["req"]["secondary_notification_entity"],+ serde_json::Value::Null+ );+ assert_eq!(request["req"]["sender_id"], "macro|[email protected]");+ assert_eq!(+ request["req"]["recipient_ids"],+ json!(["macro|[email protected]"])+ );+ assert_eq!(+ request["req"]["notification"],+ json!({+ "tag": "colleague_joined_macro",+ "content": {+ "team_name": "Acme",+ "joined_by": "macro|[email protected]",+ "joined_name": "Alice Example",+ "recipient_email": "[email protected]",+ },+ })+ );+ assert_eq!(+ request["build_email"]["content"]["subject"],+ "Alice Example joined Macro"+ );+ assert_eq!(request["build_apns"], serde_json::Value::Null);+ assert_eq!(request["send_conn_gateway"], false);+}++#[tokio::test]+async fn a_failed_enqueue_releases_only_that_claim() {+ let service = JoinAnnouncementServiceImpl::new(+ FakeRepository::eligible(&["[email protected]", "[email protected]"]),+ FakeIngress::failing_for("macro|[email protected]"),+ );++ let report = service+ .announce_join(announce(&["[email protected]", "[email protected]"]))+ .await+ .unwrap();++ assert_eq!(+ report,+ AnnounceReport {+ claimed: 2,+ sent: 1,+ released: 1+ }+ );+ assert_eq!(+ *service.repository.released.lock().unwrap(),+ strings(&["[email protected]"])+ );+ assert_eq!(+ *service.repository.claimed.lock().unwrap(),+ HashSet::from(["[email protected]".to_owned()])+ );+}++#[tokio::test]+async fn large_candidate_lists_are_claimed_in_chunks_of_fifty() {+ let service =+ JoinAnnouncementServiceImpl::new(FakeRepository::default(), FakeIngress::default());+ let emails: Vec<String> = (0..120).map(|i| format!("user{i:03}@acme.com")).collect();+ let candidates: Vec<&str> = emails.iter().map(String::as_str).collect();++ service.announce_join(announce(&candidates)).await.unwrap();++ let chunk_sizes: Vec<usize> = service+ .repository+ .claim_calls+ .lock()+ .unwrap()+ .iter()+ .map(Vec::len)+ .collect();+ assert_eq!(chunk_sizes, vec![50, 50, 20]);+}++#[tokio::test]+async fn a_storage_failure_is_an_error_and_sends_nothing() {+ let service = JoinAnnouncementServiceImpl::new(+ FakeRepository {+ fail_claims: true,+ ..FakeRepository::eligible(&["[email protected]"])+ },+ FakeIngress::default(),+ );++ let error = service+ .announce_join(announce(&["[email protected]"]))+ .await+ .unwrap_err();++ assert_eq!(*error.current_context(), JoinAnnouncementError::Storage);+ assert!(service.ingress.requests.lock().unwrap().is_empty());+}diff --git a/crates/teams/src/outbound.rs b/crates/teams/src/outbound.rsindex 4563134c10..4952248e58 100644--- a/crates/teams/src/outbound.rs+++ b/crates/teams/src/outbound.rs@@ -1,6 +1,7 @@pub mod contacts_enqueuer;pub mod crm_enqueuer;pub mod customer_repo;+pub mod join_announcement_repo;pub mod team_analytics;pub mod team_crm_settings_repo;pub mod team_repo;diff --git a/crates/teams/src/outbound/join_announcement_repo.rs b/crates/teams/src/outbound/join_announcement_repo.rsnew file mode 100644index 0000000000..298693bfab--- /dev/null+++ b/crates/teams/src/outbound/join_announcement_repo.rs@@ -0,0 +1,113 @@+//! MacroDB-backed adapter for+//! [`crate::domain::join_announcement::JoinAnnouncementRepository`].+//!+//! Owns the `team_joined_macro_email` ledger.++#[cfg(test)]+mod test;++use std::collections::HashMap;++use macro_user_id::user_id::MacroUserIdStr;+use rootcause::prelude::*;+use sqlx::PgPool;+use uuid::Uuid;++use crate::domain::join_announcement::{+ ClaimedJoinEmail, JoinAnnouncementError, JoinAnnouncementRepository,+};++/// [`JoinAnnouncementRepository`] over MacroDB.+#[derive(Clone)]+pub struct JoinAnnouncementRepositoryImpl {+ pool: PgPool,+}++impl JoinAnnouncementRepositoryImpl {+ /// Creates a new instance of JoinAnnouncementRepositoryImpl+ pub fn new(pool: PgPool) -> Self {+ Self { pool }+ }+}++impl JoinAnnouncementRepository for JoinAnnouncementRepositoryImpl {+ #[tracing::instrument(skip(self, candidates), err)]+ async fn claim(+ &self,+ team_id: Uuid,+ candidates: &[MacroUserIdStr<'static>],+ ) -> Result<Vec<ClaimedJoinEmail>, Report<JoinAnnouncementError>> {+ let by_email: HashMap<&str, &MacroUserIdStr<'static>> = candidates+ .iter()+ .map(|candidate| (candidate.email_str(), candidate))+ .collect();+ let emails: Vec<String> = candidates+ .iter()+ .map(|candidate| candidate.email_str().to_owned())+ .collect();+ let user_ids: Vec<String> = candidates+ .iter()+ .map(|candidate| candidate.to_string())+ .collect();+ let rows = sqlx::query!(+ r#"+ WITH claimed AS (+ INSERT INTO team_joined_macro_email (team_id, email)+ SELECT t.id, c.email+ FROM team t+ CROSS JOIN UNNEST($2::text[], $3::text[]) AS c(email, user_id)+ WHERE t.id = $1+ AND t.auto_join_domain IS NOT NULL+ AND split_part(c.email, '@', 2) = t.auto_join_domain+ AND NOT EXISTS (+ SELECT 1 FROM team_user tu+ WHERE tu.team_id = t.id AND tu.user_id = c.user_id+ )+ AND NOT EXISTS (+ SELECT 1 FROM team_invite ti+ WHERE ti.team_id = t.id AND ti.email = c.email+ )+ ON CONFLICT (team_id, email) DO NOTHING+ RETURNING email+ )+ SELECT claimed.email AS "email!", t.name AS "team_name!"+ FROM claimed+ JOIN team t ON t.id = $1+ "#,+ team_id,+ &emails[..],+ &user_ids[..],+ )+ .fetch_all(&self.pool)+ .await+ .context(JoinAnnouncementError::Storage)?;+ Ok(rows+ .into_iter()+ .filter_map(|row| {+ by_email+ .get(row.email.as_str())+ .map(|recipient| ClaimedJoinEmail {+ recipient: (*recipient).clone(),+ team_name: row.team_name,+ })+ })+ .collect())+ }++ #[tracing::instrument(skip(self, recipient), err)]+ async fn release(+ &self,+ team_id: Uuid,+ recipient: &MacroUserIdStr<'_>,+ ) -> Result<(), Report<JoinAnnouncementError>> {+ sqlx::query!(+ "DELETE FROM team_joined_macro_email WHERE team_id = $1 AND email = $2",+ team_id,+ recipient.email_str(),+ )+ .execute(&self.pool)+ .await+ .context(JoinAnnouncementError::Storage)?;+ Ok(())+ }+}diff --git a/crates/teams/src/outbound/join_announcement_repo/test.rs b/crates/teams/src/outbound/join_announcement_repo/test.rsnew file mode 100644index 0000000000..78b4a7a4fb--- /dev/null+++ b/crates/teams/src/outbound/join_announcement_repo/test.rs@@ -0,0 +1,180 @@+use std::collections::HashSet;++use macro_db_migrator::MACRO_DB_MIGRATIONS;++use super::*;++const TEAM1: Uuid = Uuid::from_u128(0x1111_1111_1111_1111_1111_1111_1111_1111);++fn user(email: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from_email(email).unwrap()+}++fn users(emails: &[&str]) -> Vec<MacroUserIdStr<'static>> {+ emails.iter().map(|email| user(email)).collect()+}++fn team1_claims(emails: &[&str]) -> Vec<(String, String)> {+ emails+ .iter()+ .map(|email| (format!("macro|{email}"), "team1".to_owned()))+ .collect()+}++async fn claim(repo: &JoinAnnouncementRepositoryImpl, emails: &[&str]) -> Vec<(String, String)> {+ repo.claim(TEAM1, &users(emails))+ .await+ .unwrap()+ .into_iter()+ .map(|claimed| (claimed.recipient.to_string(), claimed.team_name))+ .collect()+}++async fn set_auto_join_domain(pool: &PgPool, domain: &str) {+ sqlx::query!(+ "UPDATE team SET auto_join_domain = $2 WHERE id = $1",+ TEAM1,+ domain+ )+ .execute(pool)+ .await+ .unwrap();+}++async fn ledger(pool: &PgPool) -> Vec<String> {+ sqlx::query_scalar!(+ "SELECT email FROM team_joined_macro_email WHERE team_id = $1 ORDER BY email",+ TEAM1+ )+ .fetch_all(pool)+ .await+ .unwrap()+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn a_team_without_an_auto_join_domain_claims_nobody(pool: PgPool) {+ let repo = JoinAnnouncementRepositoryImpl::new(pool.clone());++ assert!(claim(&repo, &["[email protected]"]).await.is_empty());+ assert!(ledger(&pool).await.is_empty());++ set_auto_join_domain(&pool, "user.com").await;+ assert_eq!(+ claim(&repo, &["[email protected]"]).await,+ team1_claims(&["[email protected]"])+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn only_same_domain_outsiders_without_an_invite_are_claimed(pool: PgPool) {+ set_auto_join_domain(&pool, "user.com").await;+ let repo = JoinAnnouncementRepositoryImpl::new(pool.clone());++ let claims = claim(+ &repo,+ &[+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ "[email protected]",+ ],+ )+ .await;++ assert_eq!(claims, team1_claims(&["[email protected]"]));+ assert_eq!(ledger(&pool).await, vec!["[email protected]".to_owned()]);+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn an_address_is_claimed_once_per_team(pool: PgPool) {+ set_auto_join_domain(&pool, "user.com").await;+ let repo = JoinAnnouncementRepositoryImpl::new(pool.clone());++ assert_eq!(+ claim(&repo, &["[email protected]"]).await,+ team1_claims(&["[email protected]"])+ );+ assert_eq!(+ claim(&repo, &["[email protected]", "[email protected]"]).await,+ team1_claims(&["[email protected]"])+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn concurrent_claims_never_share_an_address(pool: PgPool) {+ set_auto_join_domain(&pool, "user.com").await;+ let left_repo = JoinAnnouncementRepositoryImpl::new(pool.clone());+ let right_repo = JoinAnnouncementRepositoryImpl::new(pool.clone());+ let emails: Vec<String> = (0..50)+ .map(|i| format!("colleague{i:02}@user.com"))+ .collect();+ let candidates: Vec<MacroUserIdStr<'static>> = emails.iter().map(|email| user(email)).collect();++ let (left, right) = tokio::join!(+ left_repo.claim(TEAM1, &candidates),+ right_repo.claim(TEAM1, &candidates)+ );++ let left: HashSet<String> = left+ .unwrap()+ .into_iter()+ .map(|claimed| claimed.recipient.to_string())+ .collect();+ let right: HashSet<String> = right+ .unwrap()+ .into_iter()+ .map(|claimed| claimed.recipient.to_string())+ .collect();+ assert!(left.is_disjoint(&right));+ assert_eq!(left.len() + right.len(), 50);+ assert_eq!(ledger(&pool).await, emails);+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn a_released_address_can_be_claimed_again(pool: PgPool) {+ set_auto_join_domain(&pool, "user.com").await;+ let repo = JoinAnnouncementRepositoryImpl::new(pool.clone());+ claim(&repo, &["[email protected]", "[email protected]"]).await;++ repo.release(TEAM1, &user("[email protected]")).await.unwrap();++ assert_eq!(ledger(&pool).await, vec!["[email protected]".to_owned()]);+ assert_eq!(+ claim(&repo, &["[email protected]", "[email protected]"]).await,+ team1_claims(&["[email protected]"])+ );+}++#[sqlx::test(+ migrator = "MACRO_DB_MIGRATIONS",+ fixtures(path = "../../../fixtures", scripts("teams"))+)]+async fn deleting_the_team_deletes_its_claims(pool: PgPool) {+ set_auto_join_domain(&pool, "user.com").await;+ let repo = JoinAnnouncementRepositoryImpl::new(pool.clone());+ claim(&repo, &["[email protected]"]).await;+ assert_eq!(ledger(&pool).await, vec!["[email protected]".to_owned()]);++ sqlx::query!("DELETE FROM team WHERE id = $1", TEAM1)+ .execute(&pool)+ .await+ .unwrap();++ assert!(ledger(&pool).await.is_empty());+}diff --git a/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md b/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.mdindex 0a229beb2e..5b31e5e13f 100644--- a/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md+++ b/docs/SLACK_ARCHIVE_IMPORT_CONTRACT.md@@ -394,7 +394,8 @@ current batch. Preserve `orphaned_thread_ts` when missing/skipped, without a latrepair promise. Convert reactions, dedupe mapped reactors, and use source messagetime when reaction time is absent. Ignore files/attachments and skip emptyconverted messages. No live-message notifications, activity rows, Kafka, bots,-contacts, sharing changes or realtime sends. Historical activity timestamps use+contacts, sharing changes or realtime sends. The colleague join email is the one+deliberate exception. Historical activity timestamps use`GREATEST` so newer live activity never regresses.`SearchBackfillClient` submits only affected channel IDs (empty scope means no@@ -411,6 +412,29 @@ Staging may expire after 14 days. Job/staging cleanup must not delete user-visibhistory, long-lived dedupe mappings, canonical reservations or source bindings.Team/channel deletion semantics belong to owning schema migrations.+## Colleague join email++`SLACK_IMPORT_JOIN_EMAIL_ENABLED` gates this email and defaults to off. When it+is on, the worker sends the email after a conversation binds. That send runs on+every bind, including a retry of the same attempt and a later re-import. Public+channels, private channels, DMs, and group DMs all participate.++A member is eligible only when all four of these conditions hold.++- The team has `auto_join_domain` set.+- The member's email domain equals the team's `auto_join_domain`.+- The member is not a `team_user`.+- The member has no pending `team_invite`.++`team_joined_macro_email` records one row per team and email. The worker sends+the email at most once for that pair, including across channels and later+imports. If the enqueue fails, the worker removes the row so a later import can+try again. x is the importing admin. The email shows x's Slack display name when+the export has one, and x's email otherwise.++The worker logs a send failure and continues. The failure does not change the+import result, and it does not retry the import.+## Native links and deferred source referencesThe pure `slack::mrkdwn::MrkdwnConverter` retains its existing `message`/`convert`diff --git a/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md b/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.mdindex 0a9ada1e0f..53b462b96f 100644--- a/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md+++ b/infra/stacks/cloud-storage-service/SLACK_IMPORT_ROLLOUT.md@@ -40,13 +40,20 @@ from the runtime `SLACK_IMPORT_ENABLED` intake flag.4. Infrastructure supplies `ENVIRONMENT`, `UPLOAD_STAGING_BUCKET` (bulk-upload'sbucket output), `OVERRIDE_SLACK_IMPORT_QUEUE`, `OVERRIDE_SLACK_IMPORT_DLQ`,`OVERRIDE_CONNECTION_GATEWAY_URL`, `OVERRIDE_SEARCH_PROCESSING_SERVICE_URL`,- `DD_SERVICE` and `DD_ENV`. Document/register those names in Doppler's service- configuration inventory, but do not add competing overrides. There is no- `SEARCH_SERVICE_URL` dependency. No Slack OAuth/bot credentials are needed.+ `DD_SERVICE`, `DD_ENV`, and `SLACK_IMPORT_JOIN_EMAIL_ENABLED`.+ `SLACK_IMPORT_JOIN_EMAIL_ENABLED` comes from the stack config key+ `slack_import_join_email_enabled`, which defaults to false. Leave that key+ out of the Doppler sync. A missing Doppler key fails task start. The task+ definition sets the value from stack config. Document/register the+ infrastructure-supplied names in Doppler's service configuration inventory,+ but do not add competing overrides. There is no `SEARCH_SERVICE_URL`+ dependency. No Slack OAuth/bot credentials are needed.5. Ensure private-subnet egress can reach MacroDB, AWS endpoints and the gateway.- The task role only receives the staging-read and main/DLQ policy from- `SlackImportQueue`; it does not inherit DSS's permissions. The execution role- reads the worker's Doppler sync, not the task role.+ The task role receives the `SlackImportQueue` worker policy only. That policy+ allows reading Slack staging objects, operating the import main queue and DLQ,+ and `sqs:SendMessage` on the notification ingress queue. The task role does+ not inherit DSS's permissions. The execution role reads the worker's Doppler+ sync, not the task role.No remote secrets are created by the source change itself. After completing theabove prerequisites, opt in from `infra/stacks/cloud-storage-service/`:@@ -75,6 +82,18 @@ instead, so maintenance and DLQ reconciliation continue.Then set `SLACK_IMPORT_ENABLED=true` and redeploy the worker before openingadmission. This flag pauses claims/outbox sends, **not** maintenance/DLQreconciliation; even paused tasks require working DB/queue/search configuration.+- Colleague join email stays off until you set+ `slack_import_join_email_enabled`. From `infra/stacks/cloud-storage-service/`,+ set the stack config, then deploy that stack through the normal deployment+ workflow:++ ```sh+ pulumi config set cloud-storage-service:slack_import_join_email_enabled true --stack <dev-or-prod>+ ```++ The `sqs:SendMessage` grant on the notification ingress queue is already on+ the worker policy. The config key only changes `SLACK_IMPORT_JOIN_EMAIL_ENABLED`+ in the task definition.- Monitor ECS deployment failure alarms, task OOM/restarts, queue age, the existingDLQ alarm, import leases and search receipts. Pause admission and set the workerflag false to stop new imports; do not purge queues or delete imported data.@@ -111,4 +130,6 @@ Local env wiring uses `bulk-upload-staging`, full LocalStack queue URL overrides`http://connection-gateway:8080`, and`http://search-processing-service:8080`. The local `INTERNAL_API_KEY` matchessearch-processing's internal auth key. The default worker flag is false unless-explicitly overridden. Do not inject a deployed `APP_SECRETS_JSON` locally.+explicitly overridden. `SLACK_IMPORT_JOIN_EMAIL_ENABLED` also defaults to false+unless the env file sets it to `true`. Do not inject a deployed+`APP_SECRETS_JSON` locally.diff --git a/infra/stacks/cloud-storage-service/index.ts b/infra/stacks/cloud-storage-service/index.tsindex 45bad7ffbb..7609704288 100644--- a/infra/stacks/cloud-storage-service/index.ts+++ b/infra/stacks/cloud-storage-service/index.ts@@ -200,6 +200,7 @@ const bulkUploadBucketName = bulkUploadStackconst slackImportQueue = new SlackImportQueue(`slack-import-${stack}`, {stagingBucketArn: pulumi.interpolate`arn:aws:s3:::${bulkUploadBucketName}`,+ notificationIngressQueueArn,tags,});diff --git a/infra/stacks/cloud-storage-service/slack-import-queue.test.ts b/infra/stacks/cloud-storage-service/slack-import-queue.test.tsindex 0e0a4c5456..2aaf57e234 100644--- a/infra/stacks/cloud-storage-service/slack-import-queue.test.ts+++ b/infra/stacks/cloud-storage-service/slack-import-queue.test.ts@@ -5,6 +5,8 @@ const resources: pulumi.runtime.MockResourceArgs[] = [];const bucketArn = 'arn:aws:s3:::bulk-upload-staging-dev';const mainArn = 'arn:aws:sqs:us-east-1:123456789012:slack-import-queue-dev';const dlqArn = 'arn:aws:sqs:us-east-1:123456789012:slack-import-dlq-dev';+const notificationIngressQueueArn =+ 'arn:aws:sqs:us-east-1:123456789012:notification-ingress';beforeAll(async () => {pulumi.runtime.setAllConfig({ 'aws:region': 'us-east-1' });@@ -36,6 +38,7 @@ beforeAll(async () => {const { SlackImportQueue } = await import('./slack-import-queue');new SlackImportQueue('slack-import-dev', {stagingBucketArn: bucketArn,+ notificationIngressQueueArn,tags: {},});await pulumi.runtime.waitForRPCs();@@ -95,7 +98,7 @@ describe('Slack import queue and IAM', () => {});});- test('worker can read staging, reconcile both queues and publish only to main', () => {+ test('worker can read staging, reconcile both queues, publish to main, and send to notification ingress', () => {const policy = resource('aws:iam/policy:Policy','slack-import-dev-worker-policy'@@ -123,6 +126,11 @@ describe('Slack import queue and IAM', () => {Action: ['sqs:SendMessage'],Resource: [mainArn],},+ {+ Effect: 'Allow',+ Action: ['sqs:SendMessage'],+ Resource: [notificationIngressQueueArn],+ },],});expect(JSON.stringify(policy)).not.toContain('sqs:*');diff --git a/infra/stacks/cloud-storage-service/slack-import-queue.ts b/infra/stacks/cloud-storage-service/slack-import-queue.tsindex 8348ecfa41..11e487869d 100644--- a/infra/stacks/cloud-storage-service/slack-import-queue.ts+++ b/infra/stacks/cloud-storage-service/slack-import-queue.ts@@ -4,6 +4,7 @@ import { Queue } from '../../packages/resources/src/resources/queue';interface SlackImportQueueArgs {stagingBucketArn: pulumi.Input<string>;+ notificationIngressQueueArn: pulumi.Input<string>;tags: { [key: string]: string };}@@ -16,7 +17,11 @@ export class SlackImportQueue extends pulumi.ComponentResource {constructor(name: string,- { stagingBucketArn, tags }: SlackImportQueueArgs,+ {+ stagingBucketArn,+ notificationIngressQueueArn,+ tags,+ }: SlackImportQueueArgs,opts?: pulumi.ComponentResourceOptions) {super('my:components:SlackImportQueue', name, {}, opts);@@ -77,6 +82,11 @@ export class SlackImportQueue extends pulumi.ComponentResource {Action: ['sqs:SendMessage'],Resource: [this.queue.arn],},+ {+ Effect: 'Allow',+ Action: ['sqs:SendMessage'],+ Resource: [notificationIngressQueueArn],+ },],},tags,diff --git a/infra/stacks/cloud-storage-service/slack-import-worker.test.ts b/infra/stacks/cloud-storage-service/slack-import-worker.test.tsindex 9e01f9b8eb..6f3dcf2e00 100644--- a/infra/stacks/cloud-storage-service/slack-import-worker.test.ts+++ b/infra/stacks/cloud-storage-service/slack-import-worker.test.ts@@ -222,6 +222,7 @@ describe('Slack import Fargate worker', () => {'https://dev-gateway.macro.com/connection-gateway',OVERRIDE_SEARCH_PROCESSING_SERVICE_URL:'https://dev-gateway.macro.com/search-processing',+ SLACK_IMPORT_JOIN_EMAIL_ENABLED: 'false',});expect(env.SEARCH_SERVICE_URL).toBeUndefined();expect(env.OVERRIDE_SEARCH_SERVICE_URL).toBeUndefined();diff --git a/infra/stacks/cloud-storage-service/slack-import-worker.ts b/infra/stacks/cloud-storage-service/slack-import-worker.tsindex 3c4253ab35..63d6cc1c35 100644--- a/infra/stacks/cloud-storage-service/slack-import-worker.ts+++ b/infra/stacks/cloud-storage-service/slack-import-worker.ts@@ -166,6 +166,13 @@ export class SlackImportWorker extends pulumi.ComponentResource {name: 'OVERRIDE_SEARCH_PROCESSING_SERVICE_URL',value: args.searchProcessingUrl,},+ {+ name: 'SLACK_IMPORT_JOIN_EMAIL_ENABLED',+ value: String(+ config.getBoolean('slack_import_join_email_enabled') ??+ false+ ),+ },{ name: 'DD_SERVICE', value: SERVICE_NAME },{ name: 'DD_ENV', value: stack },],diff --git a/services/slack_import_worker/Cargo.toml b/services/slack_import_worker/Cargo.tomlindex a1ca896ef3..4693db30a9 100644--- a/services/slack_import_worker/Cargo.toml+++ b/services/slack_import_worker/Cargo.toml@@ -21,13 +21,16 @@ macro_service_urls = { path = "../../crates/macro_service_urls" }macro_user_id = { path = "../../crates/macro_user_id" }mention_utils = { path = "../../crates/mention_utils" }messages = { path = "../../crates/messages", features = ["outbound"] }+notification = { path = "../../crates/notification" }rootcause = { workspace = true }serde = { workspace = true }serde_json = { workspace = true }slack_integration = { path = "../../crates/slack_integration", features = ["postgres", "s3", "sqs", "search", "worker"] }sqlx = { workspace = true }+teams = { path = "../../crates/teams", default-features = false, features = ["outbound"] }tokio = { workspace = true }tokio-util = { workspace = true }+tracing = { workspace = true }uuid = { workspace = true }workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }diff --git a/services/slack_import_worker/src/composition.rs b/services/slack_import_worker/src/composition.rsindex 16c48631a3..c41487a92a 100644--- a/services/slack_import_worker/src/composition.rs+++ b/services/slack_import_worker/src/composition.rs@@ -2,6 +2,7 @@pub mod authorizer;pub mod channel_sink;+pub mod join_announcer;pub mod reference_reconciliation;pub mod references;diff --git a/services/slack_import_worker/src/composition/join_announcer.rs b/services/slack_import_worker/src/composition/join_announcer.rsnew file mode 100644index 0000000000..81a3d9e9e9--- /dev/null+++ b/services/slack_import_worker/src/composition/join_announcer.rs@@ -0,0 +1,53 @@+//! Forwards a bound conversation to the teams join-announcement service.++#[cfg(test)]+mod test;++use slack_integration::domain::{+ models::ImportError,+ ports::{JoinAnnouncement, JoinAnnouncer, PortResult},+};+use teams::domain::join_announcement::{AnnounceJoin, JoinAnnouncementService};+use uuid::Uuid;++/// Forwards a bound conversation when the deployment kill switch is on.+#[derive(Clone)]+pub struct WorkerJoinAnnouncer<S> {+ service: S,+ enabled: bool,+}++impl<S> WorkerJoinAnnouncer<S> {+ /// `enabled` is the kill switch. Off drops the announcement before the service runs.+ pub fn new(service: S, enabled: bool) -> Self {+ Self { service, enabled }+ }+}++impl<S: JoinAnnouncementService> JoinAnnouncer for WorkerJoinAnnouncer<S> {+ async fn announce(&self, announcement: JoinAnnouncement) -> PortResult<()> {+ if !self.enabled {+ return Ok(());+ }+ let team_id = Uuid::from(announcement.team_id);+ let report = self+ .service+ .announce_join(AnnounceJoin {+ team_id,+ joined: announcement.joined,+ joined_name: announcement.joined_name,+ candidates: announcement.members,+ })+ .await+ .map_err(|error| error.context(ImportError::Internal))?;+ if report.claimed > 0 {+ tracing::info!(+ operation = "join_announcement",+ %team_id,+ ?report,+ "colleague join emails enqueued"+ );+ }+ Ok(())+ }+}diff --git a/services/slack_import_worker/src/composition/join_announcer/test.rs b/services/slack_import_worker/src/composition/join_announcer/test.rsnew file mode 100644index 0000000000..5bee7470b0--- /dev/null+++ b/services/slack_import_worker/src/composition/join_announcer/test.rs@@ -0,0 +1,83 @@+use std::sync::{Arc, Mutex};++use macro_user_id::user_id::MacroUserIdStr;+use rootcause::Report;+use slack_integration::domain::{+ models::ImportError,+ ports::{JoinAnnouncement, JoinAnnouncer},+};+use teams::domain::join_announcement::{+ AnnounceJoin, AnnounceReport, JoinAnnouncementError, JoinAnnouncementService,+};+use uuid::Uuid;++use super::WorkerJoinAnnouncer;++#[derive(Clone, Default)]+struct RecordingService {+ calls: Arc<Mutex<Vec<AnnounceJoin>>>,+ fail: bool,+}++impl JoinAnnouncementService for RecordingService {+ async fn announce_join(+ &self,+ request: AnnounceJoin,+ ) -> Result<AnnounceReport, Report<JoinAnnouncementError>> {+ self.calls.lock().unwrap().push(request);+ if self.fail {+ Err(Report::new(JoinAnnouncementError::Storage))+ } else {+ Ok(AnnounceReport {+ claimed: 1,+ sent: 1,+ released: 0,+ })+ }+ }+}++fn announcement() -> JoinAnnouncement {+ JoinAnnouncement {+ team_id: Uuid::from_u128(1).try_into().unwrap(),+ joined: MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ joined_name: Some("Admin".to_owned()),+ members: vec![+ MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ MacroUserIdStr::try_from_email("[email protected]").unwrap(),+ ],+ }+}++#[tokio::test]+async fn disabled_announcer_drops_the_offer() {+ let service = RecordingService::default();+ let announcer = WorkerJoinAnnouncer::new(service.clone(), false);+ announcer.announce(announcement()).await.unwrap();+ assert!(service.calls.lock().unwrap().is_empty());+}++#[tokio::test]+async fn enabled_announcer_maps_the_offer_unchanged() {+ let service = RecordingService::default();+ let announcer = WorkerJoinAnnouncer::new(service.clone(), true);+ let announcement = announcement();+ announcer.announce(announcement.clone()).await.unwrap();+ let calls = service.calls.lock().unwrap();+ assert_eq!(calls.len(), 1);+ assert_eq!(calls[0].team_id, Uuid::from(announcement.team_id));+ assert_eq!(calls[0].joined, announcement.joined);+ assert_eq!(calls[0].joined_name.as_deref(), Some("Admin"));+ assert_eq!(calls[0].candidates, announcement.members);+}++#[tokio::test]+async fn service_storage_error_becomes_internal() {+ let service = RecordingService {+ fail: true,+ ..RecordingService::default()+ };+ let announcer = WorkerJoinAnnouncer::new(service, true);+ let error = announcer.announce(announcement()).await.unwrap_err();+ assert_eq!(error.into_current_context(), ImportError::Internal);+}diff --git a/services/slack_import_worker/src/config.rs b/services/slack_import_worker/src/config.rsindex f3aad08361..28a1b5fae9 100644--- a/services/slack_import_worker/src/config.rs+++ b/services/slack_import_worker/src/config.rs@@ -19,6 +19,9 @@ pub struct Config {/// Pause new claims and outbox sends, not reconciliation or queues.#[macro_config_default(false)]pub slack_import_enabled: bool,+ /// One-time colleague emails on import. Off by default: emails cannot be recalled.+ #[macro_config_default(false)]+ pub slack_import_join_email_enabled: bool,/// Initially one; fail startup above two rather than silently widening work.#[macro_config_default(1)]pub slack_import_concurrency: u8,diff --git a/services/slack_import_worker/src/config/test.rs b/services/slack_import_worker/src/config/test.rsindex 2c96d709a3..295cc986a4 100644--- a/services/slack_import_worker/src/config/test.rs+++ b/services/slack_import_worker/src/config/test.rs@@ -9,10 +9,23 @@ fn rollout_defaults_are_disabled_and_single_slot() {})).unwrap();assert!(!config.slack_import_enabled);+ assert!(!config.slack_import_join_email_enabled);assert_eq!(config.slack_import_concurrency, 1);assert!(config.worker().is_ok());}+#[test]+fn join_email_flag_follows_slack_import_join_email_enabled() {+ let config: Config = serde_json::from_value(serde_json::json!({+ "DATABASE_URL": "postgres://localhost/test",+ "UPLOAD_STAGING_BUCKET": "test-staging",+ "INTERNAL_API_KEY": "test-only",+ "SLACK_IMPORT_JOIN_EMAIL_ENABLED": true+ }))+ .unwrap();+ assert!(config.slack_import_join_email_enabled);+}+#[test]fn rejects_unbounded_or_zero_concurrency_at_startup() {for concurrency in [0, 3, 255] {diff --git a/services/slack_import_worker/src/lib.rs b/services/slack_import_worker/src/lib.rsindex e605b0f1d6..aad0b0f16b 100644--- a/services/slack_import_worker/src/lib.rs+++ b/services/slack_import_worker/src/lib.rs@@ -1,4 +1,4 @@#![deny(missing_docs)]-//! Composition for side-effect-free, fenced Slack archive persistence.+//! Composition for fenced Slack archive persistence and one best-effort colleague join email.pub mod composition;diff --git a/services/slack_import_worker/src/main.rs b/services/slack_import_worker/src/main.rsindex 6511e2ba6d..b5fa013d47 100644--- a/services/slack_import_worker/src/main.rs+++ b/services/slack_import_worker/src/main.rs@@ -9,11 +9,12 @@ use chrono::{DateTime, Utc};use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccessRepository};use import::outbound::pg_import_repo::PgImportRepo;use macro_entrypoint::{MacroEntrypoint, shutdown_signal};-use macro_queues::{SlackImportDlq, SlackImportQueue};+use macro_queues::{NotificationIngressQueue, SlackImportDlq, SlackImportQueue};use macro_service_urls::SearchProcessingServiceUrl;+use notification::{domain::service::SqsNotificationIngress, outbound::queue::SqsQueue};use slack_import_worker::composition::{authorizer::WorkerAuthorizer, channel_sink::ChannelImportSink,- reference_reconciliation::WorkerReferenceReconciler,+ join_announcer::WorkerJoinAnnouncer, reference_reconciliation::WorkerReferenceReconciler,};use slack_integration::{domain::{@@ -30,6 +31,10 @@ use slack_integration::{},};use sqlx::postgres::PgPoolOptions;+use teams::{+ domain::join_announcement::JoinAnnouncementServiceImpl,+ outbound::join_announcement_repo::JoinAnnouncementRepositoryImpl,+};use tokio_util::sync::CancellationToken;use uuid::Uuid;@@ -71,13 +76,23 @@ async fn run() -> Result<(), rootcause::Report> {config.upload_staging_bucket,limits,)?;+ let sqs = aws_sdk_sqs::Client::new(¯o_aws_config::get_macro_aws_config().await);let queue = SqsImportQueue::new(- aws_sdk_sqs::Client::new(¯o_aws_config::get_macro_aws_config().await),+ sqs.clone(),&SlackImportQueue::new(),&SlackImportDlq::new(),).await.map_err(|error| *error.current_context())?;+ let announcer = WorkerJoinAnnouncer::new(+ JoinAnnouncementServiceImpl::new(+ JoinAnnouncementRepositoryImpl::new(pool.clone()),+ SqsNotificationIngress {+ queue: SqsQueue::new(sqs, NotificationIngressQueue::new().to_string()),+ },+ ),+ config.slack_import_join_email_enabled,+ );let search = HttpSearchBackfill::new(SearchProcessingServiceUrl::new()?.as_ref(),&config.internal_api_key,@@ -89,6 +104,7 @@ async fn run() -> Result<(), rootcause::Report> {sink.clone(),sink,authorizer,+ announcer,ImporterConfig { limits },)?;let maintenance = ImportMaintenance::new(diff --git a/services/slack_import_worker/tests/archive_import.rs b/services/slack_import_worker/tests/archive_import.rsindex 534a9171f7..e3ce93f7d9 100644--- a/services/slack_import_worker/tests/archive_import.rs+++ b/services/slack_import_worker/tests/archive_import.rs@@ -20,10 +20,11 @@ use entity_access::{use import::outbound::pg_import_repo::PgImportRepo;use macro_queues::{SlackImportDlq, SlackImportQueue};use macro_user_id::user_id::MacroUserIdStr;+use notification::{domain::service::SqsNotificationIngress, outbound::queue::SqsQueue};use sha2::{Digest, Sha256};use slack_import_worker::composition::{authorizer::WorkerAuthorizer, channel_sink::ChannelImportSink,- reference_reconciliation::WorkerReferenceReconciler,+ join_announcer::WorkerJoinAnnouncer, reference_reconciliation::WorkerReferenceReconciler,};use slack_integration::{domain::{@@ -40,6 +41,10 @@ use slack_integration::{},};use sqlx::PgPool;+use teams::{+ domain::join_announcement::JoinAnnouncementServiceImpl,+ outbound::join_announcement_repo::JoinAnnouncementRepositoryImpl,+};use tokio::time::{sleep, timeout};use tokio_util::sync::CancellationToken;use uuid::Uuid;@@ -111,17 +116,20 @@ fn docker(args: &[&str]) -> String {String::from_utf8(output.stdout).unwrap().trim().to_owned()}-async fn resources(- endpoint: &str,-) -> (- S3ImportStorage,- SqsImportQueue,- process_recovery::ProcessConfig,-) {+struct Resources {+ storage: S3ImportStorage,+ queue: SqsImportQueue,+ process: process_recovery::ProcessConfig,+ sqs: aws_sdk_sqs::Client,+ ingress_url: String,+}++async fn resources(endpoint: &str) -> Resources {let suffix = Uuid::now_v7().simple().to_string();let bucket = format!("archive-test-{suffix}");let main = format!("archive-main-{suffix}");let dlq = format!("archive-dlq-{suffix}");+ let ingress = format!("archive-ingress-{suffix}");let s3 = aws_sdk_s3::Client::from_conf(aws_sdk_s3::Config::builder().behavior_version(BehaviorVersion::latest())@@ -152,7 +160,7 @@ async fn resources(.endpoint_url(endpoint).build(),);- for name in [&main, &dlq] {+ for name in [&main, &dlq, &ingress] {sqs.create_queue().queue_name(name).send().await.unwrap();}use aws_sdk_sqs::types::QueueAttributeName;@@ -172,6 +180,14 @@ async fn resources(.unwrap().queue_url.unwrap();+ let ingress_url = sqs+ .get_queue_url()+ .queue_name(&ingress)+ .send()+ .await+ .unwrap()+ .queue_url+ .unwrap();let attributes = sqs.get_queue_attributes().queue_url(dlq_url)@@ -190,7 +206,7 @@ async fn resources(.await.unwrap();let queue = SqsImportQueue::new(- sqs,+ sqs.clone(),&SlackImportQueue::from_owned(main.clone()),&SlackImportDlq::from_owned(dlq.clone()),)@@ -203,7 +219,13 @@ async fn resources(dlq,};let storage = S3ImportStorage::new(s3, bucket, ImportLimits::default()).unwrap();- (storage, queue, process)+ Resources {+ storage,+ queue,+ process,+ sqs,+ ingress_url,+ }}fn user() -> MacroUserIdStr<'static> {@@ -215,7 +237,7 @@ async fn team(pool: &PgPool) -> TeamId {let team = Uuid::now_v7();sqlx::query!("INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, 't17', '[email protected]', 't17-customer')", account).execute(pool).await.unwrap();sqlx::query!(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)"#, account).execute(pool).await.unwrap();- sqlx::query!("INSERT INTO team (id, name, owner_id) VALUES ($1, 'Atomic history', 'macro|[email protected]')", team).execute(pool).await.unwrap();+ sqlx::query!("INSERT INTO team (id, name, owner_id, auto_join_domain) VALUES ($1, 'Atomic history', 'macro|[email protected]', 'example.com')", team).execute(pool).await.unwrap();sqlx::query!("INSERT INTO team_user (team_id, user_id, team_role) VALUES ($1, 'macro|[email protected]', 'owner')", team).execute(pool).await.unwrap();team.try_into().unwrap()}@@ -529,10 +551,7 @@ async fn progress(repo: &PgSlackImportRepo, team: TeamId, job: JobId) -> ImportPrepo.progress(team, job).await.unwrap().unwrap()}-/// The historical composition has no live notification/event publisher. Check its-/// real database effects too: mentions, reactions, external members and replies-/// must not produce notifications, invitation emails or unread activity.-async fn assert_backfill_is_silent(pool: &PgPool) {+async fn assert_no_notification_or_activity_rows(pool: &PgPool) {let counts = sqlx::query!(r#"SELECT@@ -570,11 +589,77 @@ async fn assert_backfill_is_silent(pool: &PgPool) {);}+async fn assert_one_external_colleague(pool: &PgPool, team: TeamId) {+ let count = sqlx::query_scalar!(r#"SELECT count(*) FROM team_joined_macro_email"#)+ .fetch_one(pool)+ .await+ .unwrap();+ assert_eq!(count, Some(1));+ let rows = sqlx::query!(r#"SELECT team_id, email FROM team_joined_macro_email"#)+ .fetch_all(pool)+ .await+ .unwrap();+ assert_eq!(rows.len(), 1);+ assert_eq!(rows[0].team_id, Uuid::from(team));+ assert_eq!(rows[0].email, "[email protected]");+}++async fn ingress_bodies(sqs: &aws_sdk_sqs::Client, url: &str) -> Vec<serde_json::Value> {+ let mut bodies = Vec::new();+ loop {+ let page = sqs+ .receive_message()+ .queue_url(url)+ .max_number_of_messages(10)+ .wait_time_seconds(1)+ .send()+ .await+ .unwrap();+ let Some(messages) = page.messages.filter(|messages| !messages.is_empty()) else {+ break;+ };+ for message in messages {+ bodies.push(serde_json::from_str(message.body.as_deref().unwrap()).unwrap());+ sqs.delete_message()+ .queue_url(url)+ .receipt_handle(message.receipt_handle.unwrap())+ .send()+ .await+ .unwrap();+ }+ }+ bodies+}++fn assert_colleague_joined(body: &serde_json::Value, team: Uuid) {+ let request = &body["request"];+ assert_eq!(+ request["req"]["notification"]["tag"],+ "colleague_joined_macro"+ );+ assert_eq!(request["req"]["sender_id"], "macro|[email protected]");+ assert_eq!(+ request["req"]["recipient_ids"],+ serde_json::json!(["macro|[email protected]"])+ );+ let id = Uuid::new_v5(+ &Uuid::from_u128(0x7465_616d_2d6a_6f69_6e65_642d_6d61_6372),+ format!("{team}:[email protected]").as_bytes(),+ );+ assert_eq!(request["uuid_to_write"], id.to_string());+}+#[ignore = "requires Docker with localstack/localstack:4 and a local PostgreSQL test role"]#[sqlx::test(migrations = "../../crates/macro_db_client/migrations")]async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool) {let (_container, endpoint) = LocalStack::start().await;- let (storage, queue, _) = resources(&endpoint).await;+ let Resources {+ storage,+ queue,+ sqs,+ ingress_url,+ ..+ } = resources(&endpoint).await;let team = team(&pool).await;let limits = ImportLimits::default();let repo = PgSlackImportRepo::new(pool.clone(), limits);@@ -610,14 +695,23 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool)sink.clone(),sink.clone(),authorizer,+ WorkerJoinAnnouncer::new(+ JoinAnnouncementServiceImpl::new(+ JoinAnnouncementRepositoryImpl::new(pool.clone()),+ SqsNotificationIngress {+ queue: SqsQueue::new(sqs.clone(), ingress_url.clone()),+ },+ ),+ true,+ ),ImporterConfig { limits },).unwrap(),);- assert_backfill_is_silent(&pool).await;+ assert_no_notification_or_activity_rows(&pool).await;// Shape creation followed by history and an exact repeat reuse the same IDs.let mut canonical = None;- for history in [false, true, true] {+ for (attempt, history) in [false, true, true].into_iter().enumerate() {search.0.store(false, Ordering::SeqCst);let job = upload_job(&service, team, history).await;let events = repo.pending_events(50).await.unwrap();@@ -672,6 +766,14 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool).await.unwrap().unwrap();+ assert_one_external_colleague(&pool, team).await;+ let messages = ingress_bodies(&sqs, &ingress_url).await;+ if attempt == 0 {+ assert_eq!(messages.len(), 1);+ assert_colleague_joined(&messages[0], Uuid::from(team));+ } else {+ assert!(messages.is_empty());+ }// Reconcile links and publish search without a competing maintenance loop.maintenance.reconcile().await.unwrap();let receipt = progress(&repo, team, job).await;@@ -742,7 +844,7 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool)progress(&repo, team, job).await.status,JobStatus::Completed);- assert_backfill_is_silent(&pool).await;+ assert_no_notification_or_activity_rows(&pool).await;}assert_eq!(sqlx::query_scalar!("SELECT count(*) FROM comms_messages")@@ -756,7 +858,7 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool)// DLQ. Disabling admission must still run the production DLQ driver.// Cancelled HTTP long polls can still reserve messages server-side until// their timeout. A fresh queue keeps this redrive scenario independent.- let (storage, queue, _) = resources(&endpoint).await;+ let Resources { storage, queue, .. } = resources(&endpoint).await;let authorizer = WorkerAuthorizer::new(pool.clone(),Access::new(PgAccessRepository::new(pool.clone())),@@ -844,7 +946,9 @@ async fn sealed_uploads_queue_replay_and_search_completion_barrier(pool: PgPool)}).await.expect("disabled worker did not persist exhausted work as Failed");- assert_backfill_is_silent(&pool).await;+ assert_no_notification_or_activity_rows(&pool).await;+ assert_one_external_colleague(&pool, team).await;+ assert!(ingress_bodies(&sqs, &ingress_url).await.is_empty());stop.cancel();timeout(Duration::from_secs(35), driver).awaitdiff --git a/services/slack_import_worker/tests/archive_import/process_recovery.rs b/services/slack_import_worker/tests/archive_import/process_recovery.rsindex a74a3ce439..6053ed7737 100644--- a/services/slack_import_worker/tests/archive_import/process_recovery.rs+++ b/services/slack_import_worker/tests/archive_import/process_recovery.rs@@ -71,7 +71,12 @@ impl Drop for WorkerProcess {async fn recover(pool: PgPool, boundary: &str) {let (_container, endpoint) = LocalStack::start().await;- let (storage, queue, config) = resources(&endpoint).await;+ let Resources {+ storage,+ queue,+ process: config,+ ..+ } = resources(&endpoint).await;let team = team(&pool).await;let limits = ImportLimits::default();let repo = PgSlackImportRepo::new(pool.clone(), limits);