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
Seanaye/feat/put notifs in kafka (#5609)
Sean Aye46a0106395ac
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.jsonindex 7ba9774f6c..8eca986c46 100644--- a/.github/kafka-cluster-topics.json+++ b/.github/kafka-cluster-topics.json@@ -7,6 +7,7 @@"macro.email","macro.example","macro.mentions",+ "macro.notifications","macro.projects","macro.properties","macro.soup",diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex e799d3a780..4d5bd02cda 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -613,11 +613,14 @@"crates/channel_sender","crates/cowlike","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -2134,11 +2137,14 @@"crates/cowlike","crates/email_formatting","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -3073,11 +3079,14 @@"crates/graphql_common","crates/graphql_notification","crates/item_filters",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -3379,11 +3388,14 @@"crates/bot_id","crates/cowlike","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4136,11 +4148,14 @@"crates/channel_sender","crates/cowlike","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4432,11 +4447,14 @@"crates/ai_toolset","crates/bot_id","crates/cowlike",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4455,11 +4473,14 @@"crates/channel_sender","crates/cowlike","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4482,12 +4503,15 @@"crates/cowlike","crates/email_formatting","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_aws_config","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4517,6 +4541,7 @@"crates/entity_access_db_utils","crates/invite_email","crates/ip_extractor",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization",@@ -4528,6 +4553,8 @@"crates/macro_entrypoint","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_middleware","crates/macro_queues","crates/macro_service_urls",@@ -4949,11 +4976,14 @@"crates/cowlike","crates/invite_email","crates/ip_extractor",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",@@ -4978,11 +5008,14 @@"crates/document_sub_type","crates/entity_access","crates/invite_email",+ "crates/kafka_util","crates/last_online_tracker","crates/macro_auth","crates/macro_authorization","crates/macro_env","crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics","crates/macro_service_urls","crates/macro_user_id","crates/macro_uuid",diff --git a/Cargo.lock b/Cargo.lockindex db09cb7ba5..66677f2825 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -10033,6 +10033,8 @@ dependencies = ["macro_authorization","macro_db_migrator","macro_env",+ "macro_event_broker",+ "macro_event_topics","macro_service_urls","macro_user_id","macro_uuid",@@ -10137,6 +10139,7 @@ dependencies = ["macro_entrypoint","macro_env","macro_env_var",+ "macro_event_broker","macro_middleware","macro_queues","macro_service_urls",diff --git a/bun.lock b/bun.lockindex 07ca0f17cf..c4dd01796e 100644--- a/bun.lock+++ b/bun.lock@@ -943,7 +943,7 @@"@img/sharp-win32-x64": ["@img/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw=="],- "@inkibra/tauri-plugins": ["@inkibra/tauri-plugins@github:macro-inc/tauri-plugins#6ddd660", { "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1" } }, "macro-inc-tauri-plugins-6ddd660"],+ "@inkibra/tauri-plugins": ["@inkibra/tauri-plugins@github:macro-inc/tauri-plugins#6ddd660", { "dependencies": { "react": "^18.3.1", "react-dom": "^18.3.1" } }, "macro-inc-tauri-plugins-6ddd660", "sha512-7+0ebEOYqedkOY67Clwu65Np3qJ3aJc/H/NjNxmjgA/7HIu0C+DSoKAArG7HTdTVpdcAtHyhrqsJujdEAyJf6Q=="],"@inquirer/ansi": ["@inquirer/[email protected]", "", {}, "sha512-3eTuUO1vH2cZm2ZKHeQxnOqlTi9EfZDGgIe3BL3I4u+rJHocr9Fz86M4fjYABPvFnQG/gGK551HqDiIcETwU6Q=="],@@ -2721,7 +2721,7 @@"pathval": ["[email protected]", "", {}, "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ=="],- "pdfjs-dist": ["pdfjs-dist@github:macro-inc/pdf.js#f9b2ce6", { "dependencies": { "dommatrix": "^1.0.3", "web-streams-polyfill": "^3.2.1" }, "peerDependencies": { "worker-loader": "^3.0.8" }, "optionalPeers": ["worker-loader"] }, "macro-inc-pdf.js-f9b2ce6"],+ "pdfjs-dist": ["pdfjs-dist@github:macro-inc/pdf.js#f9b2ce6", { "dependencies": { "dommatrix": "^1.0.3", "web-streams-polyfill": "^3.2.1" }, "peerDependencies": { "worker-loader": "^3.0.8" }, "optionalPeers": ["worker-loader"] }, "macro-inc-pdf.js-f9b2ce6", "sha512-HDFaPbCxLG2GHpf1smUG97nku5aPGBGaCcIbK05dGdp07TBLUlDcXsM4WcE82mO5W4RBPIBVYobX5N/6M6NFrA=="],"performance-now": ["[email protected]", "", {}, "sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow=="],diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rsindex d5872e8f03..1034e31b8f 100644--- a/crates/macro_event_topics/src/lib.rs+++ b/crates/macro_event_topics/src/lib.rs@@ -69,6 +69,8 @@ topics! {MacroWebhooksTopic => "macro.webhooks",/// Entity mention events (created / deleted) across channels and docs.MacroMentionsTopic => "macro.mentions",+ /// Notifications awaiting delivery through WebSocket connections.+ MacroNotificationsTopic => "macro.notifications",/// AI chat lifecycle and message events.MacroChatsTopic => "macro.chats",}diff --git a/crates/macro_event_topics/src/test.rs b/crates/macro_event_topics/src/test.rsindex a97147950c..7899c17540 100644--- a/crates/macro_event_topics/src/test.rs+++ b/crates/macro_event_topics/src/test.rs@@ -28,5 +28,6 @@ fn all_topic_names_includes_declared_topics() {assert!(all_topic_names().contains(&MacroCallsTopic::TOPIC_STR));assert!(all_topic_names().contains(&MacroWebhooksTopic::TOPIC_STR));assert!(all_topic_names().contains(&MacroMentionsTopic::TOPIC_STR));+ assert!(all_topic_names().contains(&MacroNotificationsTopic::TOPIC_STR));assert!(all_topic_names().contains(&MacroChatsTopic::TOPIC_STR));}diff --git a/crates/notification/Cargo.toml b/crates/notification/Cargo.tomlindex f443969947..3456f2e44c 100644--- a/crates/notification/Cargo.toml+++ b/crates/notification/Cargo.toml@@ -36,6 +36,8 @@ hex = { workspace = true }hmac = { workspace = true }itertools = { workspace = true }last_online_tracker = { path = "../last_online_tracker" }+macro_event_broker = { path = "../macro_event_broker", default-features = false }+macro_event_topics = { path = "../macro_event_topics" }macro_authorization = { path = "../macro_authorization", default-features = false, features = ["axum",], optional = true }diff --git a/crates/notification/src/domain/models.rs b/crates/notification/src/domain/models.rsindex f10b3722c2..65eb68bee9 100644--- a/crates/notification/src/domain/models.rs+++ b/crates/notification/src/domain/models.rs@@ -12,6 +12,8 @@ pub mod recipient;pub mod request;/// Cryptographic URL signing and verification.pub mod signing;+/// Broker event models for WebSocket notification delivery.+pub mod websocket_notification_event;use crate::domain::models::{apple::APNSPushNotification, queue_message::EmailContent};use chrono::{DateTime, Utc};diff --git a/crates/notification/src/domain/models/websocket_notification_event.rs b/crates/notification/src/domain/models/websocket_notification_event.rsnew file mode 100644index 0000000000..6178d6ee5b--- /dev/null+++ b/crates/notification/src/domain/models/websocket_notification_event.rs@@ -0,0 +1,74 @@+//! Broker event models for WebSocket notification delivery.++use macro_event_broker::{Event, MacroEvent, TopicEvent};+use macro_event_topics::MacroNotificationsTopic;+use macro_user_id::user_id::MacroUserIdStr;+use serde::{Deserialize, Serialize, de::DeserializeOwned};++/// Recipients and payload for one WebSocket notification delivery request.+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct WebSocketNotificationMetadata<T> {+ /// Users who should receive the notification.+ pub recipients: Vec<MacroUserIdStr<'static>>,+ /// Notification payload forwarded to each recipient.+ pub notification: T,+}++/// Events published to [`MacroNotificationsTopic`].+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+#[serde(tag = "event_type", content = "metadata")]+pub enum NotificationTopicEvent<T> {+ /// A notification should be delivered to active WebSocket connections.+ #[serde(rename = "notification.websocket_delivery_requested")]+ WebSocketDeliveryRequested(WebSocketNotificationMetadata<T>),+}++impl<T: Serialize + DeserializeOwned + Send + Sync> TopicEvent for NotificationTopicEvent<T> {+ type Topic = MacroNotificationsTopic;++ const SCHEMA_VERSION: u8 = 1;+}++/// Publishable WebSocket notification event.+pub struct NotificationMacroEvent<T> {+ key: String,+ event: Event<NotificationTopicEvent<T>>,+}++impl<T: Serialize + DeserializeOwned + Send + Sync> NotificationMacroEvent<T> {+ /// Creates a delivery event keyed by its generated event ID.+ pub fn new(recipients: Vec<MacroUserIdStr<'static>>, notification: T) -> Self {+ let metadata = WebSocketNotificationMetadata {+ recipients,+ notification,+ };+ let event = Event::new(NotificationTopicEvent::WebSocketDeliveryRequested(metadata));+ let key = event.event_id.to_string();++ Self::with_event(key, event)+ }++ /// Builds an event from its Kafka key and pre-built envelope.+ pub fn with_event(key: impl Into<String>, event: Event<NotificationTopicEvent<T>>) -> Self {+ Self {+ key: key.into(),+ event,+ }+ }+}++impl<T: Serialize + DeserializeOwned + Send + Sync> MacroEvent for NotificationMacroEvent<T> {+ type EventPayload = NotificationTopicEvent<T>;++ fn key(&self) -> &str {+ &self.key+ }++ fn event(&self) -> &Event<Self::EventPayload> {+ &self.event+ }++ fn from_event(key: String, event: Event<Self::EventPayload>) -> Self {+ Self::with_event(key, event)+ }+}diff --git a/crates/notification/src/outbound.rs b/crates/notification/src/outbound.rsindex d75a9af3fb..4a8e1270ed 100644--- a/crates/notification/src/outbound.rs+++ b/crates/notification/src/outbound.rs@@ -6,6 +6,10 @@pub mod device_registration;pub mod digest_batcher;pub mod email;+/// WebSocket notification fanout across multiple delivery adapters.+pub mod fanout_websocket;+/// Kafka-backed WebSocket notification delivery.+pub mod kafka_websocket;pub mod last_online_checker;pub mod message_receipt_repository;pub mod mobile;diff --git a/crates/notification/src/outbound/fanout_websocket.rs b/crates/notification/src/outbound/fanout_websocket.rsnew file mode 100644index 0000000000..86042c1643--- /dev/null+++ b/crates/notification/src/outbound/fanout_websocket.rs@@ -0,0 +1,47 @@+//! WebSocket sender that fans each delivery out to two adapters.++#[cfg(test)]+mod test;++use std::collections::HashSet;++use macro_user_id::user_id::MacroUserIdStr;+use rootcause::Report;+use serde::Serialize;++use crate::domain::ports::WebSocketSender;++/// WebSocket sender that delivers through two underlying senders.+///+/// Both sends are awaited concurrently. The delivery receipts are combined when both sends+/// succeed; an error from either sender fails the combined send after both attempts complete.+pub struct FanoutWebSocketSender<A, B> {+ first: A,+ second: B,+}++impl<A, B> FanoutWebSocketSender<A, B> {+ /// Creates a sender that fans each delivery out to `first` and `second`.+ pub fn new(first: A, second: B) -> Self {+ Self { first, second }+ }+}++impl<A: WebSocketSender, B: WebSocketSender> WebSocketSender for FanoutWebSocketSender<A, B> {+ #[tracing::instrument(err, skip_all, fields(recipient_count = recipients.len()))]+ async fn send_notifications<'a, T: Serialize + Send + Sync>(+ &self,+ recipients: &[MacroUserIdStr<'a>],+ notification: &T,+ ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {+ let (first, second) = futures::future::join(+ self.first.send_notifications(recipients, notification),+ self.second.send_notifications(recipients, notification),+ )+ .await;++ let mut delivered = first?;+ delivered.extend(second?);+ Ok(delivered)+ }+}diff --git a/crates/notification/src/outbound/fanout_websocket/test.rs b/crates/notification/src/outbound/fanout_websocket/test.rsnew file mode 100644index 0000000000..cd57de564b--- /dev/null+++ b/crates/notification/src/outbound/fanout_websocket/test.rs@@ -0,0 +1,101 @@+use std::sync::{Arc, Mutex};++use rootcause::report;++use super::*;++struct RecordingSender {+ calls: Arc<Mutex<Vec<serde_json::Value>>>,+ delivered: Vec<MacroUserIdStr<'static>>,+ fail: bool,+}++impl WebSocketSender for RecordingSender {+ async fn send_notifications<'a, T: Serialize + Send + Sync>(+ &self,+ _recipients: &[MacroUserIdStr<'a>],+ notification: &T,+ ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {+ self.calls+ .lock()+ .expect("calls lock")+ .push(serde_json::to_value(notification)?);++ if self.fail {+ return Err(report!("send failed"));+ }++ Ok(self.delivered.iter().cloned().collect())+ }+}++fn user(id: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(id.to_string()).expect("valid user ID")+}++#[tokio::test]+async fn sends_through_both_adapters_and_combines_receipts() {+ let first_calls = Arc::new(Mutex::new(Vec::new()));+ let second_calls = Arc::new(Mutex::new(Vec::new()));+ let first_user = user("macro|[email protected]");+ let second_user = user("macro|[email protected]");+ let sender = FanoutWebSocketSender::new(+ RecordingSender {+ calls: first_calls.clone(),+ delivered: vec![first_user.clone()],+ fail: false,+ },+ RecordingSender {+ calls: second_calls.clone(),+ delivered: vec![first_user.clone(), second_user.clone()],+ fail: false,+ },+ );+ let notification = serde_json::json!({ "kind": "test" });++ let delivered = sender+ .send_notifications(&[first_user, second_user], ¬ification)+ .await+ .expect("both sends succeed");++ assert_eq!(delivered.len(), 2);+ assert!(delivered.contains(&user("macro|[email protected]")));+ assert!(delivered.contains(&user("macro|[email protected]")));+ assert_eq!(+ first_calls.lock().expect("first calls lock").as_slice(),+ &[notification.clone()]+ );+ assert_eq!(+ second_calls.lock().expect("second calls lock").as_slice(),+ &[notification]+ );+}++#[tokio::test]+async fn attempts_both_adapters_when_one_fails() {+ let first_calls = Arc::new(Mutex::new(Vec::new()));+ let second_calls = Arc::new(Mutex::new(Vec::new()));+ let sender = FanoutWebSocketSender::new(+ RecordingSender {+ calls: first_calls.clone(),+ delivered: Vec::new(),+ fail: true,+ },+ RecordingSender {+ calls: second_calls.clone(),+ delivered: Vec::new(),+ fail: false,+ },+ );++ sender+ .send_notifications(+ &[user("macro|[email protected]")],+ &serde_json::json!({ "kind": "test" }),+ )+ .await+ .expect_err("one failed send fails the fanout");++ assert_eq!(first_calls.lock().expect("first calls lock").len(), 1);+ assert_eq!(second_calls.lock().expect("second calls lock").len(), 1);+}diff --git a/crates/notification/src/outbound/kafka_websocket.rs b/crates/notification/src/outbound/kafka_websocket.rsnew file mode 100644index 0000000000..6006fb8975--- /dev/null+++ b/crates/notification/src/outbound/kafka_websocket.rs@@ -0,0 +1,65 @@+//! Kafka adapter for asynchronous WebSocket notification delivery.++#[cfg(test)]+mod test;++use std::collections::HashSet;++use macro_event_broker::MacroEventBroker;+use macro_user_id::cowlike::CowLike as _;+use macro_user_id::user_id::MacroUserIdStr;+use rootcause::Report;+use rootcause::prelude::ResultExt as _;+use serde::Serialize;++use crate::domain::models::websocket_notification_event::NotificationMacroEvent;+use crate::domain::ports::WebSocketSender;++/// Kafka-backed implementation of the WebSocket sender port.+///+/// Each call publishes one message containing the full recipient list and notification payload.+pub struct KafkaWebSocketSender<B> {+ broker: B,+}++impl<B> KafkaWebSocketSender<B> {+ /// Creates a WebSocket sender backed by `broker`.+ pub fn new(broker: B) -> Self {+ Self { broker }+ }+}++impl<B: MacroEventBroker> WebSocketSender for KafkaWebSocketSender<B> {+ #[tracing::instrument(+ err,+ skip_all,+ fields(recipient_count = recipients.len(), topic = "macro.notifications")+ )]+ async fn send_notifications<'a, T: Serialize + Send + Sync>(+ &self,+ recipients: &[MacroUserIdStr<'a>],+ notification: &T,+ ) -> Result<HashSet<MacroUserIdStr<'static>>, Report> {+ let notification = serde_json::to_value(notification)+ .context("failed to serialize websocket notification for Kafka")?;+ let event = NotificationMacroEvent::new(+ recipients+ .iter()+ .map(|recipient| recipient.clone().into_owned())+ .collect(),+ notification,+ );++ let publish = self+ .broker+ .send_event(&event)+ .context("failed to dispatch websocket notification Kafka event")?;+ publish+ .await+ .context("websocket notification Kafka publish task failed")?+ .context("failed to publish websocket notification to Kafka")?;++ // Kafka acknowledges durable publication, not delivery to an active WebSocket connection.+ Ok(HashSet::new())+ }+}diff --git a/crates/notification/src/outbound/kafka_websocket/test.rs b/crates/notification/src/outbound/kafka_websocket/test.rsnew file mode 100644index 0000000000..c0394b98e1--- /dev/null+++ b/crates/notification/src/outbound/kafka_websocket/test.rs@@ -0,0 +1,195 @@+use std::sync::{Arc, Mutex};++use macro_event_broker::{+ EventBrokerError, EventPublisher, MacroEvent, MacroEventBrokerService, Spawner,+};+use macro_event_topics::Topic;+use serde::ser::Error as _;++use super::*;+use crate::domain::models::websocket_notification_event::{+ NotificationMacroEvent, NotificationTopicEvent, WebSocketNotificationMetadata,+};++#[derive(Debug)]+struct PublishedRecord {+ topic: String,+ key: String,+ payload: Vec<u8>,+}++struct RecordingPublisher {+ records: Arc<Mutex<Vec<PublishedRecord>>>,+ fail: bool,+}++impl EventPublisher for RecordingPublisher {+ async fn publish<T: Topic>(&self, key: &str, payload: &[u8]) -> Result<(), EventBrokerError> {+ self.records+ .lock()+ .expect("records lock")+ .push(PublishedRecord {+ topic: T::TOPIC_STR.to_string(),+ key: key.to_string(),+ payload: payload.to_vec(),+ });++ if self.fail {+ Err(EventBrokerError::Publish("broker unavailable".to_string()))+ } else {+ Ok(())+ }+ }+}++#[derive(Clone, Copy)]+struct TokioSpawner;++impl Spawner for TokioSpawner {+ fn spawn<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>+ where+ F: Future + Send + 'static,+ F::Output: Send + 'static,+ {+ tokio::spawn(future)+ }+}++fn user(id: &str) -> MacroUserIdStr<'static> {+ MacroUserIdStr::try_from(id.to_string()).expect("valid user id")+}++#[tokio::test]+async fn publishes_one_event_per_call_with_all_recipients() {+ let records = Arc::new(Mutex::new(Vec::new()));+ let sender = KafkaWebSocketSender::new(MacroEventBrokerService::new(+ RecordingPublisher {+ records: records.clone(),+ fail: false,+ },+ TokioSpawner,+ ));+ let recipients = [+ user("macro|[email protected]"),+ user("macro|[email protected]"),+ ];+ let notification = serde_json::json!({+ "notification_id": "01952f4d-6890-7df2-8598-89d4e18c07db",+ "notification_event_type": "channel_mention",+ });++ let delivered = sender+ .send_notifications(&recipients, ¬ification)+ .await+ .expect("publish succeeds");++ assert!(delivered.is_empty());+ let records = records.lock().expect("records lock");+ assert_eq!(records.len(), 1);++ let record = &records[0];+ assert_eq!(record.topic, "macro.notifications");+ let expected_metadata = WebSocketNotificationMetadata {+ recipients: recipients.to_vec(),+ notification: notification.clone(),+ };++ let payload: serde_json::Value =+ serde_json::from_slice(&record.payload).expect("payload is valid JSON");+ assert_eq!(+ record.key,+ payload["event_id"].as_str().expect("event id is a string")+ );+ assert_eq!(payload["schema_version"], 1);+ assert_eq!(+ payload["event_type"],+ "notification.websocket_delivery_requested"+ );+ assert_eq!(+ payload["metadata"]["recipients"],+ serde_json::json!(["macro|[email protected]", "macro|[email protected]"])+ );+ assert_eq!(payload["metadata"]["notification"], notification);++ let decoded = NotificationMacroEvent::decode(record.key.clone(), &record.payload)+ .expect("event round-trips");+ assert_eq!(+ decoded.event().event,+ NotificationTopicEvent::WebSocketDeliveryRequested(expected_metadata)+ );+}++#[tokio::test]+async fn empty_recipients_still_publishes_one_event() {+ let records = Arc::new(Mutex::new(Vec::new()));+ let sender = KafkaWebSocketSender::new(MacroEventBrokerService::new(+ RecordingPublisher {+ records: records.clone(),+ fail: false,+ },+ TokioSpawner,+ ));++ sender+ .send_notifications(&[], &serde_json::json!({ "kind": "test" }))+ .await+ .expect("publish succeeds");++ let records = records.lock().expect("records lock");+ assert_eq!(records.len(), 1);+ let payload: serde_json::Value =+ serde_json::from_slice(&records[0].payload).expect("payload is valid JSON");+ assert_eq!(payload["metadata"]["recipients"], serde_json::json!([]));+}++#[tokio::test]+async fn propagates_publish_failures() {+ let sender = KafkaWebSocketSender::new(MacroEventBrokerService::new(+ RecordingPublisher {+ records: Arc::new(Mutex::new(Vec::new())),+ fail: true,+ },+ TokioSpawner,+ ));++ sender+ .send_notifications(+ &[user("macro|[email protected]")],+ &serde_json::json!({ "kind": "test" }),+ )+ .await+ .expect_err("publish failure propagates");+}++struct SerializationFailure;++impl Serialize for SerializationFailure {+ fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>+ where+ S: serde::Serializer,+ {+ Err(S::Error::custom("serialization failed"))+ }+}++#[tokio::test]+async fn propagates_serialization_failures_without_publishing() {+ let records = Arc::new(Mutex::new(Vec::new()));+ let sender = KafkaWebSocketSender::new(MacroEventBrokerService::new(+ RecordingPublisher {+ records: records.clone(),+ fail: false,+ },+ TokioSpawner,+ ));++ sender+ .send_notifications(+ &[user("macro|[email protected]")],+ &SerializationFailure,+ )+ .await+ .expect_err("serialization failure propagates");++ assert!(records.lock().expect("records lock").is_empty());+}diff --git a/crates/notification/src/outbound/websocket.rs b/crates/notification/src/outbound/websocket.rsindex 0ad3cad933..f893178df4 100644--- a/crates/notification/src/outbound/websocket.rs+++ b/crates/notification/src/outbound/websocket.rs@@ -16,14 +16,8 @@ use crate::domain::ports::{NotificationRealtimePublisher, WebSocketSender};/// This adapter sends notifications to users via WebSocket connections/// through the connection gateway service.pub struct WebSocketGatewayAdapter<W> {- gateway: W,-}--impl<W> WebSocketGatewayAdapter<W> {- /// Create a new WebSocket gateway adapter.- pub fn new(gateway: W) -> Self {- Self { gateway }- }+ /// Underlying connection gateway client.+ pub gateway: W,}/// Trait for WebSocket gateway operations.diff --git a/infra/stacks/notification-service/index.ts b/infra/stacks/notification-service/index.tsindex 137e692809..df9addfbba 100644--- a/infra/stacks/notification-service/index.ts+++ b/infra/stacks/notification-service/index.ts@@ -1,7 +1,12 @@import * as aws from '@pulumi/aws';import * as pulumi from '@pulumi/pulumi';import { Queue } from '../../packages/resources';-import { config, getMacroApiToken, stack } from '../../packages/shared';+import {+ config,+ getKafkaClusterPolicy,+ getMacroApiToken,+ stack,+} from '../../packages/shared';import { get_coparse_api_vpc } from '../../packages/vpc';import { PushNotificationEventHandler } from './push';import { NotificationService } from './service';@@ -159,6 +164,7 @@ const notificationService = new NotificationService('notification-service', {notificationIngressQueueArn,],snsPlatformArns: notificationSnsPlatformArns,+ extraManagedPolicyArns: [getKafkaClusterPolicy()],serviceContainerPort: 8080,isPrivate: false,healthCheckPath: '/health',diff --git a/infra/stacks/notification-service/service.ts b/infra/stacks/notification-service/service.tsindex 14c7f63fb1..6d70af8613 100644--- a/infra/stacks/notification-service/service.ts+++ b/infra/stacks/notification-service/service.ts@@ -40,6 +40,7 @@ type CreateNotificationServiceArgs = {secretKeyArns: (pulumi.Output<string> | string)[];queueArns: Array<pulumi.Output<string> | string>;snsPlatformArns: Array<pulumi.Output<string> | string>;+ extraManagedPolicyArns?: Array<pulumi.Output<string> | string>;tags: { [key: string]: string };};@@ -69,6 +70,7 @@ export class NotificationService extends pulumi.ComponentResource {cloudStorageClusterName,queueArns,snsPlatformArns,+ extraManagedPolicyArns = [],secretKeyArns,tags,}: CreateNotificationServiceArgs,@@ -195,6 +197,7 @@ export class NotificationService extends pulumi.ComponentResource {queuePolicy.arn,sesPolicy.arn,snsPolicy.arn,+ ...extraManagedPolicyArns,],},{ parent: this }diff --git a/packages/sdk/generated/auth/types.gen.ts b/packages/sdk/generated/auth/types.gen.tsindex 0d1e27e1df..8e21965640 100644--- a/packages/sdk/generated/auth/types.gen.ts+++ b/packages/sdk/generated/auth/types.gen.ts@@ -1200,6 +1200,10 @@ export type InitGmailLinkData = {* **OPTIONAL**. The original url to redirect to.*/original_url: string;+ /**+ * **OPTIONAL**. Also request the Google Calendar scope. Only honored when the deployment allows calendar scope requests; pass it from calendar entry points only, since the extra scope changes the Google consent screen.+ */+ include_calendar?: boolean;};url: '/link/gmail';};diff --git a/packages/sdk/specs/auth.json b/packages/sdk/specs/auth.jsonindex 7e0f962549..7bb832ad8a 100644--- a/packages/sdk/specs/auth.json+++ b/packages/sdk/specs/auth.json@@ -624,6 +624,15 @@"schema": {"type": "string"}+ },+ {+ "name": "include_calendar",+ "in": "query",+ "description": "**OPTIONAL**. Also request the Google Calendar scope. Only honored when the deployment allows calendar scope requests; pass it from calendar entry points only, since the extra scope changes the Google consent screen.",+ "required": false,+ "schema": {+ "type": "boolean"+ }}],"responses": {diff --git a/services/notification_service/Cargo.toml b/services/notification_service/Cargo.tomlindex 1f598bf052..1ad3c37dc8 100644--- a/services/notification_service/Cargo.toml+++ b/services/notification_service/Cargo.toml@@ -40,6 +40,7 @@ macro_cors = { path = "../../crates/macro_cors" }macro_entrypoint = { path = "../../crates/macro_entrypoint" }macro_env = { path = "../../crates/macro_env" }macro_env_var = { path = "../../crates/macro_env_var" }+macro_event_broker = { path = "../../crates/macro_event_broker" }macro_middleware = { path = "../../crates/macro_middleware", default-features = false }macro_queues = { path = "../../crates/macro_queues" }macro_tower_layers = { path = "../../crates/macro_tower_layers" }diff --git a/services/notification_service/src/config.rs b/services/notification_service/src/config.rsindex fa6c6c6910..4ab0400179 100644--- a/services/notification_service/src/config.rs+++ b/services/notification_service/src/config.rs@@ -27,6 +27,9 @@ env_vars! {pub(crate) struct SenderBaseAddress;#[derive(Debug, Clone)]pub(crate) struct LastOnlineRedisUri;+ /// Comma-separated Kafka bootstrap servers for WebSocket notification delivery.+ #[derive(Debug, Clone)]+ pub(crate) struct KafkaBrokers;}maybe_env_var! {@@ -80,6 +83,9 @@ pub struct Config {/// Redis used by connection-gateway for last-online state.pub(crate) last_online_redis_uri: LastOnlineRedisUri,+ /// Comma-separated Kafka bootstrap servers for WebSocket notification delivery.+ pub(crate) kafka_brokers: KafkaBrokers,+/// Apple app bundle id for APNS pushes.pub(crate) apple_bundle_id: AppleBundleId,diff --git a/services/notification_service/src/main.rs b/services/notification_service/src/main.rsindex f1a3244bf8..8f0c451c25 100644--- a/services/notification_service/src/main.rs+++ b/services/notification_service/src/main.rs@@ -6,6 +6,8 @@ use ::notification::domain::service::NotificationEgressService;use ::notification::inbound::notification_events_listener::NotificationEventsListener;use ::notification::inbound::worker::NotificationWorker;use ::notification::outbound::email::EmailAdapter;+use ::notification::outbound::fanout_websocket::FanoutWebSocketSender;+use ::notification::outbound::kafka_websocket::KafkaWebSocketSender;use ::notification::outbound::mobile::MobilePushAdapter;use ::notification::outbound::notification_events::PgNotificationEventsReceiver;use ::notification::outbound::rate_limit::RedisRateLimitAdapter;@@ -19,6 +21,7 @@ use macro_auth::middleware::decode_jwt::JwtValidationArgs;use macro_authorization::{InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState};use macro_entrypoint::MacroEntrypoint;use macro_env::Environment;+use macro_event_broker::{GlobalSpawner, KafkaEventPublisher, MacroEventBrokerService};use macro_service_urls::ConnectionGatewayUrl;use secretsmanager_client::SecretManager;use sha2::Sha256;@@ -155,15 +158,18 @@ pub async fn main() -> anyhow::Result<()> {fcm_platform_arn: config.sns_fcm_platform_arn.as_ref().to_string(),apns_voip_platform_arn: config.sns_apns_voip_platform_arn().to_string(),};- let reader_realtime_adapter = WebSocketGatewayAdapter::new(ConnectionGatewayClient::new(- config.internal_api_key.as_ref().to_string(),- connection_gateway_url.clone(),- ));- let notification_events_realtime_adapter =- WebSocketGatewayAdapter::new(ConnectionGatewayClient::new(+ let reader_realtime_adapter = WebSocketGatewayAdapter {+ gateway: ConnectionGatewayClient::new(config.internal_api_key.as_ref().to_string(),connection_gateway_url.clone(),- ));+ ),+ };+ let notification_events_realtime_adapter = WebSocketGatewayAdapter {+ gateway: ConnectionGatewayClient::new(+ config.internal_api_key.as_ref().to_string(),+ connection_gateway_url.clone(),+ ),+ };let notification_events_receiver = PgNotificationEventsReceiver::new(db.clone());let mut notification_events_listener = NotificationEventsListener::new(notification_events_receiver,@@ -192,10 +198,19 @@ pub async fn main() -> anyhow::Result<()> {let egress_repository =::notification::outbound::repository::DbNotificationRepository::new(db.clone());- let websocket_adapter = WebSocketGatewayAdapter::new(ConnectionGatewayClient::new(- config.internal_api_key.as_ref().to_string(),- connection_gateway_url,- ));+ let websocket_adapter = FanoutWebSocketSender::new(+ WebSocketGatewayAdapter {+ gateway: ConnectionGatewayClient::new(+ config.internal_api_key.as_ref().to_string(),+ connection_gateway_url,+ ),+ },+ KafkaWebSocketSender::new(MacroEventBrokerService::new(+ KafkaEventPublisher::new(config.kafka_brokers.as_ref())+ .context("failed to create Kafka WebSocket notification publisher")?,+ GlobalSpawner,+ )),+ );let mobile_adapter = MobilePushAdapter {push_service: aws_sdk_sns::Client::new(&aws_config),