Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(email): kafka producer (#4758)

Will Hutchinson0051d4e0e883
diff --git a/.github/kafka-cluster-topics.json b/.github/kafka-cluster-topics.json
index 06053689cd..b0052d835d 100644
--- a/.github/kafka-cluster-topics.json
+++ b/.github/kafka-cluster-topics.json
@@ -1,5 +1,6 @@
[
"macro.channels",
"macro.documents",
+ "macro.email",
"macro.example"
]
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 7dd3eb7a70..e890ed9afa 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -148,6 +148,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -1202,6 +1204,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",
@@ -1352,6 +1356,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -1887,6 +1893,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_sha_count_client",
@@ -1958,6 +1966,8 @@
"crates/macro_auth",
"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",
@@ -2049,6 +2059,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -2108,6 +2120,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -2177,6 +2191,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",
@@ -2245,6 +2261,8 @@
"crates/macro_auth",
"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",
@@ -2299,6 +2317,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -3139,6 +3159,8 @@
"crates/macro_auth",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_project_utils",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -3945,6 +3967,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -4004,6 +4028,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -4572,6 +4598,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
@@ -4822,6 +4850,8 @@
"crates/macro_auth",
"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",
@@ -5079,6 +5109,8 @@
"crates/macro_auth",
"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",
@@ -5215,6 +5247,8 @@
"crates/macro_entrypoint",
"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",
@@ -5274,6 +5308,8 @@
"crates/macro_entrypoint",
"crates/macro_env",
"crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
"crates/macro_queues",
"crates/macro_service_urls",
"crates/macro_user_id",
diff --git a/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json b/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json
deleted file mode 100644
index 15557826d9..0000000000
--- a/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n DELETE FROM email_scheduled_messages\n WHERE message_id = ANY($1) AND link_id = $2 AND sent = false\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "UuidArray",
- "Uuid"
- ]
- },
- "nullable": []
- },
- "hash": "3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220"
-}
diff --git a/.sqlx/query-c68f45e8a939d63144008a9254f03bf5198710a57c1a8bad4901f61c27cb5f6f.json b/.sqlx/query-c68f45e8a939d63144008a9254f03bf5198710a57c1a8bad4901f61c27cb5f6f.json
new file mode 100644
index 0000000000..bb08a58f15
--- /dev/null
+++ b/.sqlx/query-c68f45e8a939d63144008a9254f03bf5198710a57c1a8bad4901f61c27cb5f6f.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n DELETE FROM email_scheduled_messages\n WHERE message_id = ANY($1) AND link_id = $2 AND sent = false\n RETURNING message_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "message_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "c68f45e8a939d63144008a9254f03bf5198710a57c1a8bad4901f61c27cb5f6f"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 2e80dee015..fd27c0b290 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4699,6 +4699,8 @@ dependencies = [
"item_filters",
"lol_html",
"macro_db_migrator",
+ "macro_event_broker",
+ "macro_event_topics",
"macro_user_id",
"macro_uuid",
"model-entity",
@@ -4877,6 +4879,7 @@ dependencies = [
"macro_entrypoint",
"macro_env",
"macro_env_var",
+ "macro_event_broker",
"macro_middleware",
"macro_queues",
"macro_service_urls",
diff --git a/crates/ai_tools/src/build_context.rs b/crates/ai_tools/src/build_context.rs
index a9793ac07d..45799fe02a 100644
--- a/crates/ai_tools/src/build_context.rs
+++ b/crates/ai_tools/src/build_context.rs
@@ -264,7 +264,7 @@ pub async fn build_tool_service_context_from_env(
foreign_entity_service: ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(
pool.clone(),
)),
- macro_event_broker,
+ macro_event_broker: macro_event_broker.clone(),
};
let document_tool_context = DocumentToolContext::new(
@@ -282,16 +282,19 @@ pub async fn build_tool_service_context_from_env(
);
let email_tool_context = email::inbound::toolset::EmailToolContext::new(
- Arc::new(EmailServiceImpl::new(
- EmailPgRepo::new(pool.clone()),
- FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(pool.clone())),
- sqs_client,
- crm_service.clone(),
- entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
- entity_access_management::outbound::PgRepository::new(pool.clone()),
- ),
- 0,
- )),
+ Arc::new(
+ EmailServiceImpl::new(
+ EmailPgRepo::new(pool.clone()),
+ FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(pool.clone())),
+ sqs_client,
+ crm_service.clone(),
+ entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
+ entity_access_management::outbound::PgRepository::new(pool.clone()),
+ ),
+ 0,
+ )
+ .with_macro_event_broker(macro_event_broker.clone()),
+ ),
Arc::new(email::domain::ports::NoOpGmailTokenProvider),
Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
pool.clone(),
diff --git a/crates/ai_tools/src/tool_context.rs b/crates/ai_tools/src/tool_context.rs
index de86fcddf0..f06ac15c89 100644
--- a/crates/ai_tools/src/tool_context.rs
+++ b/crates/ai_tools/src/tool_context.rs
@@ -70,13 +70,16 @@ pub type ToolEmailService = EmailServiceImpl<
ToolEamService,
>;
-/// Type alias for the send-capable email service implementation used by user tools.
+/// Type alias for the send-capable email service implementation used by user
+/// tools. Carries the real event broker: these tools mutate email state, and
+/// the Gmail-echo suppression means events skipped here are never recovered.
pub type ToolUserEmailService = EmailServiceImpl<
EmailPgRepo,
ToolFrecencyService,
sqs_client::SQS,
ToolCrmService,
ToolEamService,
+ macro_event_broker::MacroEventBrokerService<macro_event_broker::KafkaEventPublisher>,
>;
/// Type alias for the channel list service implementation.
diff --git a/crates/email/Cargo.toml b/crates/email/Cargo.toml
index d54e5e467a..de1fa9aadd 100644
--- a/crates/email/Cargo.toml
+++ b/crates/email/Cargo.toml
@@ -38,7 +38,7 @@ gmail_token = ["dep:authentication_service_client", "dep:redis", "ports"]
http_client = ["dep:reqwest", "ports"]
inbound = ["ports"]
outbound = ["dep:sqlx", "dep:email_db_client", "ports"]
-ports = ["crm/ports", "dep:tokio", "frecency/ports"]
+ports = ["crm/ports", "dep:tokio", "frecency/ports", "macro_event_broker/ports"]
[dependencies]
ai_toolset = { path = "../ai_toolset", optional = true }
@@ -64,6 +64,8 @@ item_filters = { path = "../item_filters" }
lol_html = { workspace = true }
scraper = { workspace = true }
email_db_client = { path = "../email_db_client", optional = true }
+macro_event_broker = { path = "../macro_event_broker", default-features = false }
+macro_event_topics = { path = "../macro_event_topics" }
macro_user_id = { path = "../macro_user_id" }
macro_uuid = { path = "../macro_uuid" }
model-entity = { path = "../model-entity" }
@@ -91,6 +93,9 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
crm = { path = "../crm", features = ["outbound"] }
frecency = { path = "../frecency", features = ["outbound", "postgres"] }
+macro_event_broker = { path = "../macro_event_broker", default-features = false, features = [
+ "ports",
+] }
macro_db_migrator = { path = "../macro_db_migrator" }
serde_json = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
diff --git a/crates/email/src/domain.rs b/crates/email/src/domain.rs
index 9b812e494c..b212a7dc2d 100644
--- a/crates/email/src/domain.rs
+++ b/crates/email/src/domain.rs
@@ -1,3 +1,4 @@
+pub mod events;
pub mod models;
#[cfg(feature = "ports")]
diff --git a/crates/email/src/domain/events.rs b/crates/email/src/domain/events.rs
new file mode 100644
index 0000000000..34152bd954
--- /dev/null
+++ b/crates/email/src/domain/events.rs
@@ -0,0 +1,666 @@
+//! Kafka event models for the `macro.email` topic.
+//!
+//! Follows the canonical pattern in `documents/src/domain/events.rs`:
+//! per-variant metadata structs, a [`TopicEvent`] enum tagged by `event_type`,
+//! and a [`MacroEvent`] wrapper keyed by link (inbox) id.
+//!
+//! Every event is keyed by `link_id` so consumers get per-inbox total order:
+//! account lifecycle events are ordered against the inbox's content events,
+//! and per-thread order is preserved (a thread belongs to exactly one link).
+//!
+//! Payloads carry RFC-822 *header* level data only (subject, from, to/cc
+//! addresses) — never message bodies, snippets, bcc addresses, or attachment
+//! names. Consumers needing content must read back through the API.
+//!
+//! Each variant documents its owning emission site: a logical change is
+//! published from the point closest to the committed Macro-DB write.
+//! Changes initiated in Macro are emitted with
+//! [`EmailEventOrigin::UserAction`] from the mutating handler or domain
+//! service; the same change echoing back through Gmail history sync finds no
+//! DB diff and stays silent. Changes first observed from the provider are
+//! emitted with [`EmailEventOrigin::ProviderSync`] from the inbox-sync worker.
+//!
+//! Delivery is best-effort, not exactly-once: publishing is log-and-drop (an
+//! event can be lost if the broker is unavailable), and operation retries
+//! can re-publish (a duplicate carries the same ids and state). Consumers
+//! must treat events as idempotent state notifications and read back through
+//! the API when exact current state matters.
+
+#[cfg(test)]
+mod test;
+
+use chrono::{DateTime, Utc};
+use macro_event_broker::{Event, MacroEvent, TopicEvent};
+use macro_event_topics::MacroEmailTopic;
+use macro_user_id::user_id::MacroUserIdStr;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
+
+/// Whether a change was initiated by a Macro user action or first observed
+/// syncing from the email provider. Lets automation ignore its own echoes.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum EmailEventOrigin {
+ /// The change was initiated by a user action in Macro.
+ UserAction,
+ /// The change was first observed syncing from the provider (e.g. Gmail).
+ ProviderSync,
+}
+
+/// Reference to an email label carried on wire events.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LabelRef {
+ /// Macro's label row id, when the label is known locally.
+ pub label_id: Option<Uuid>,
+ /// Provider (Gmail) label id.
+ pub provider_label_id: String,
+ /// Label display name, when known.
+ pub name: Option<String>,
+}
+
+/// Why an email link (connected inbox) was disconnected.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum LinkDisconnectReason {
+ /// The link was never meaningfully used and was reaped.
+ Unused,
+ /// The link went inactive and was reaped.
+ Inactive,
+ /// The user disconnected the inbox themselves.
+ ManuallyDisabled,
+ /// The Macro user account was deleted.
+ UserDeleted,
+ /// The provider OAuth grant was revoked.
+ AccessRevoked,
+}
+
+/// Why a queued send was cancelled before reaching the provider.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum SendCancelReason {
+ /// The user undid the send (or unscheduled a scheduled send).
+ Undo,
+ /// The containing thread was trashed while the send was pending.
+ ThreadTrashed,
+}
+
+/// Metadata for [`EmailTopicEvent::LinkConnected`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LinkConnectedMetadata {
+ /// The id of the connected link (inbox).
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// Address of the connected inbox.
+ pub email_address: String,
+ /// Email provider (e.g. `GMAIL`).
+ pub provider: String,
+ /// Whether this is the owner's primary inbox.
+ pub is_primary: bool,
+ /// When the link was connected.
+ pub connected_at: DateTime<Utc>,
+}
+
+/// Metadata for [`EmailTopicEvent::LinkDisconnected`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LinkDisconnectedMetadata {
+ /// The id of the disconnected link.
+ pub link_id: Uuid,
+ /// The Macro user who owned the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// Address of the disconnected inbox.
+ pub email_address: String,
+ /// Why the link was disconnected.
+ pub reason: LinkDisconnectReason,
+}
+
+/// Metadata for [`EmailTopicEvent::LinkReauthRequired`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct LinkReauthRequiredMetadata {
+ /// The id of the link needing re-authentication.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// Address of the inbox needing re-authentication.
+ pub email_address: String,
+ /// When the dead grant was observed.
+ pub observed_at: DateTime<Utc>,
+}
+
+/// Metadata for [`EmailTopicEvent::MessageReceived`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MessageReceivedMetadata {
+ /// The link (inbox) that received the message.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// Macro's message row id.
+ pub message_id: Uuid,
+ /// Provider (Gmail) message id.
+ pub provider_message_id: String,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// Provider (Gmail) thread id.
+ pub provider_thread_id: String,
+ /// Whether the message started a thread new to this inbox.
+ pub is_new_thread: bool,
+ /// Message subject header.
+ pub subject: Option<String>,
+ /// Sender address.
+ pub from_email: Option<String>,
+ /// Sender display name.
+ pub from_name: Option<String>,
+ /// Recipient (`To`) addresses.
+ pub to_emails: Vec<String>,
+ /// Number of attachments on the message.
+ pub attachment_count: u32,
+ /// Whether the message arrived as spam or trash.
+ pub is_spam_or_trash: bool,
+ /// Provider-reported receive time.
+ pub received_at: Option<DateTime<Utc>>,
+}
+
+/// Metadata for [`EmailTopicEvent::MessageSent`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MessageSentMetadata {
+ /// The link (inbox) the message was sent from.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who sent the message; `None` when the send was
+ /// first observed from the provider (sent from another client).
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's message row id.
+ pub message_id: Uuid,
+ /// Provider (Gmail) message id.
+ pub provider_message_id: String,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// Provider (Gmail) thread id.
+ pub provider_thread_id: String,
+ /// Message subject header.
+ pub subject: Option<String>,
+ /// Recipient (`To`) addresses.
+ pub to_emails: Vec<String>,
+ /// Carbon-copy (`Cc`) addresses. `Bcc` addresses are never published.
+ pub cc_emails: Vec<String>,
+ /// Whether the send was performed through Macro or observed from the
+ /// provider.
+ pub origin: EmailEventOrigin,
+ /// When the provider accepted the message.
+ pub sent_at: DateTime<Utc>,
+}
+
+/// Metadata for [`EmailTopicEvent::MessageDeleted`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MessageDeletedMetadata {
+ /// The link (inbox) the message belonged to.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// Macro's message row id.
+ pub message_id: Uuid,
+ /// Provider (Gmail) message id.
+ pub provider_message_id: String,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+}
+
+/// Metadata for [`EmailTopicEvent::MessageSendQueued`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MessageSendQueuedMetadata {
+ /// The link (inbox) the message will be sent from.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who queued the send.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's message row id.
+ pub message_id: Uuid,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// When the message is scheduled to leave.
+ pub scheduled_send_at: DateTime<Utc>,
+ /// `true` for an explicit scheduled send; `false` for the undo-window
+ /// delay on an immediate send.
+ pub is_scheduled: bool,
+}
+
+/// Metadata for [`EmailTopicEvent::MessageSendCancelled`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct MessageSendCancelledMetadata {
+ /// The link (inbox) the message would have been sent from.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who cancelled the send.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's message row id.
+ pub message_id: Uuid,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// Why the queued send was cancelled.
+ pub reason: SendCancelReason,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadArchived`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadArchivedMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the state; `None` for
+ /// provider-initiated changes.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// New state: `true` = archived, `false` = moved back to the inbox.
+ pub archived: bool,
+ /// Where the change originated.
+ pub origin: EmailEventOrigin,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadTrashed`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadTrashedMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the state; `None` for
+ /// provider-initiated changes.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// New state: `true` = trashed, `false` = restored.
+ pub trashed: bool,
+ /// Where the change originated.
+ pub origin: EmailEventOrigin,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadRead`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadReadMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the state; `None` for
+ /// provider-initiated changes.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// New state: `true` = read, `false` = unread.
+ pub is_read: bool,
+ /// Where the change originated.
+ pub origin: EmailEventOrigin,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadStarred`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadStarredMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the state; `None` for
+ /// provider-initiated changes.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// New state: `true` = starred, `false` = unstarred.
+ pub starred: bool,
+ /// Where the change originated.
+ pub origin: EmailEventOrigin,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadProjectChanged`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadProjectChangedMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the project.
+ pub actor: MacroUserIdStr<'static>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// Project id before the change, when any.
+ pub previous_project_id: Option<String>,
+ /// New project id; `None` when the thread was removed from its project.
+ pub project_id: Option<String>,
+}
+
+/// Metadata for [`EmailTopicEvent::ThreadLabelsUpdated`].
+///
+/// Carries **user label** changes only; system-label changes (INBOX, TRASH,
+/// UNREAD, STARRED) are published as the semantic
+/// [`EmailTopicEvent::ThreadArchived`] / [`EmailTopicEvent::ThreadTrashed`] /
+/// [`EmailTopicEvent::ThreadRead`] / [`EmailTopicEvent::ThreadStarred`]
+/// events instead.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct ThreadLabelsUpdatedMetadata {
+ /// The link (inbox) containing the thread.
+ pub link_id: Uuid,
+ /// The Macro user who owns the link.
+ pub owner: MacroUserIdStr<'static>,
+ /// The authenticated user who changed the labels; `None` for
+ /// provider-initiated changes.
+ pub actor: Option<MacroUserIdStr<'static>>,
+ /// Macro's thread row id.
+ pub thread_id: Uuid,
+ /// Labels added to the thread's messages.
+ pub added: Vec<LabelRef>,
+ /// Labels removed from the thread's messages.
+ pub removed: Vec<LabelRef>,
+ /// Where the change originated.
+ pub origin: EmailEventOrigin,
+}
+
+/// Events that can be published to [`MacroEmailTopic`].
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+#[serde(tag = "event_type", content = "metadata")]
+pub enum EmailTopicEvent {
+ /// An inbox was connected. Emitted from the email-service init handler
+ /// after the link row is upserted.
+ #[serde(rename = "email.link_connected")]
+ LinkConnected(LinkConnectedMetadata),
+ /// An inbox was disconnected. Emitted from the link-manager worker's
+ /// delete cascade after the link row is deleted.
+ #[serde(rename = "email.link_disconnected")]
+ LinkDisconnected(LinkDisconnectedMetadata),
+ /// An inbox's OAuth grant went dead. Emitted only on the false→true
+ /// `needs_reauth` transition (edge-triggered).
+ #[serde(rename = "email.link_reauth_required")]
+ LinkReauthRequired(LinkReauthRequiredMetadata),
+ /// A new inbound message was persisted. Emitted from the inbox-sync
+ /// worker for messages not seen before; never from the backfill worker.
+ /// When a previously-unsynced thread is pulled in whole, only the
+ /// message that triggered the sync is published — its historical
+ /// siblings are persisted silently (they were not just received).
+ #[serde(rename = "email.message_received")]
+ MessageReceived(MessageReceivedMetadata),
+ /// A message was accepted by the provider. Emitted from the scheduled
+ /// send worker after the provider accepts (`origin = user_action`), or
+ /// from the inbox-sync worker when a message sent from another client is
+ /// first synced (`origin = provider_sync`).
+ #[serde(rename = "email.message_sent")]
+ MessageSent(MessageSentMetadata),
+ /// A message was permanently deleted. Emitted from the inbox-sync worker
+ /// after the delete transaction commits.
+ #[serde(rename = "email.message_deleted")]
+ MessageDeleted(MessageDeletedMetadata),
+ /// A send was committed and queued (undo window or scheduled send).
+ /// Emitted from the email domain service and the schedule handler.
+ /// Always resolved by exactly one later `message_sent` or
+ /// `message_send_cancelled` for the same message.
+ #[serde(rename = "email.message_send_queued")]
+ MessageSendQueued(MessageSendQueuedMetadata),
+ /// A queued send was cancelled before reaching the provider. Emitted
+ /// from the unschedule/undo handler and the trash path.
+ #[serde(rename = "email.message_send_cancelled")]
+ MessageSendCancelled(MessageSendCancelledMetadata),
+ /// A thread was archived or unarchived. Emitted from the archive handler
+ /// (`origin = user_action`) or the inbox-sync INBOX-label diff
+ /// (`origin = provider_sync`).
+ #[serde(rename = "email.thread_archived")]
+ ThreadArchived(ThreadArchivedMetadata),
+ /// A thread was trashed or restored. Emitted from the email domain
+ /// service's TRASH label path (`origin = user_action`) or the inbox-sync
+ /// TRASH-label diff (`origin = provider_sync`).
+ #[serde(rename = "email.thread_trashed")]
+ ThreadTrashed(ThreadTrashedMetadata),
+ /// A thread was marked read or unread. Emitted from the seen handler and
+ /// the email domain service's UNREAD label path (`origin = user_action`)
+ /// or the inbox-sync UNREAD-label diff (`origin = provider_sync`).
+ #[serde(rename = "email.thread_read")]
+ ThreadRead(ThreadReadMetadata),
+ /// A thread was starred or unstarred. Emitted from the email domain
+ /// service's STARRED label path (`origin = user_action`) or the
+ /// inbox-sync STARRED-label diff (`origin = provider_sync`).
+ #[serde(rename = "email.thread_starred")]
+ ThreadStarred(ThreadStarredMetadata),
+ /// A thread was assigned to or removed from a project. Emitted from the
+ /// email domain service's thread-project update.
+ #[serde(rename = "email.thread_project_changed")]
+ ThreadProjectChanged(ThreadProjectChangedMetadata),
+ /// A thread's user labels changed. Emitted from the email domain
+ /// service's label path (`origin = user_action`) or the inbox-sync label
+ /// diff (`origin = provider_sync`). System labels are published as the
+ /// semantic thread events instead.
+ #[serde(rename = "email.thread_labels_updated")]
+ ThreadLabelsUpdated(ThreadLabelsUpdatedMetadata),
+}
+
+impl TopicEvent for EmailTopicEvent {
+ type Topic = MacroEmailTopic;
+
+ fn schema_version(&self) -> u8 {
+ 1
+ }
+}
+
+/// Publishable event for [`MacroEmailTopic`], keyed by link (inbox) id.
+pub struct EmailMacroEvent {
+ key: String,
+ event: Event<EmailTopicEvent>,
+}
+
+impl EmailMacroEvent {
+ /// Build a link-connected event keyed by the link id.
+ pub fn link_connected(metadata: LinkConnectedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::LinkConnected(metadata),
+ )
+ }
+
+ /// Build a link-disconnected event keyed by the link id.
+ pub fn link_disconnected(metadata: LinkDisconnectedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::LinkDisconnected(metadata),
+ )
+ }
+
+ /// Build a link-reauth-required event keyed by the link id.
+ pub fn link_reauth_required(metadata: LinkReauthRequiredMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::LinkReauthRequired(metadata),
+ )
+ }
+
+ /// Build a message-received event keyed by the link id.
+ pub fn message_received(metadata: MessageReceivedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::MessageReceived(metadata),
+ )
+ }
+
+ /// Build a message-sent event keyed by the link id.
+ pub fn message_sent(metadata: MessageSentMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::MessageSent(metadata),
+ )
+ }
+
+ /// Build a message-deleted event keyed by the link id.
+ pub fn message_deleted(metadata: MessageDeletedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::MessageDeleted(metadata),
+ )
+ }
+
+ /// Build a message-send-queued event keyed by the link id.
+ pub fn message_send_queued(metadata: MessageSendQueuedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::MessageSendQueued(metadata),
+ )
+ }
+
+ /// Build a message-send-cancelled event keyed by the link id.
+ pub fn message_send_cancelled(metadata: MessageSendCancelledMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::MessageSendCancelled(metadata),
+ )
+ }
+
+ /// Build a thread-archived event keyed by the link id.
+ pub fn thread_archived(metadata: ThreadArchivedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadArchived(metadata),
+ )
+ }
+
+ /// Build a thread-trashed event keyed by the link id.
+ pub fn thread_trashed(metadata: ThreadTrashedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadTrashed(metadata),
+ )
+ }
+
+ /// Build a thread-read event keyed by the link id.
+ pub fn thread_read(metadata: ThreadReadMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadRead(metadata),
+ )
+ }
+
+ /// Build a thread-starred event keyed by the link id.
+ pub fn thread_starred(metadata: ThreadStarredMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadStarred(metadata),
+ )
+ }
+
+ /// Build a thread-project-changed event keyed by the link id.
+ pub fn thread_project_changed(metadata: ThreadProjectChangedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadProjectChanged(metadata),
+ )
+ }
+
+ /// Build a thread-labels-updated event keyed by the link id.
+ pub fn thread_labels_updated(metadata: ThreadLabelsUpdatedMetadata) -> Self {
+ Self::new(
+ metadata.link_id.to_string(),
+ EmailTopicEvent::ThreadLabelsUpdated(metadata),
+ )
+ }
+
+ /// Map a thread-scoped label change onto the semantic event for that
+ /// label: INBOX/TRASH/UNREAD/STARRED become the dedicated thread-state
+ /// events, SPAM/IMPORTANT/SENT/DRAFT publish nothing (`None`), and any
+ /// other (user) label becomes a `thread_labels_updated` diff.
+ pub fn thread_label_change(
+ link_id: Uuid,
+ owner: MacroUserIdStr<'static>,
+ actor: Option<MacroUserIdStr<'static>>,
+ thread_id: Uuid,
+ label: LabelRef,
+ added: bool,
+ origin: EmailEventOrigin,
+ ) -> Option<Self> {
+ use crate::domain::models::label::system_labels;
+
+ let event = match label.provider_label_id.as_str() {
+ system_labels::UNREAD => Self::thread_read(ThreadReadMetadata {
+ link_id,
+ owner,
+ actor,
+ thread_id,
+ is_read: !added,
+ origin,
+ }),
+ system_labels::STARRED => Self::thread_starred(ThreadStarredMetadata {
+ link_id,
+ owner,
+ actor,
+ thread_id,
+ starred: added,
+ origin,
+ }),
+ system_labels::TRASH => Self::thread_trashed(ThreadTrashedMetadata {
+ link_id,
+ owner,
+ actor,
+ thread_id,
+ trashed: added,
+ origin,
+ }),
+ system_labels::INBOX => Self::thread_archived(ThreadArchivedMetadata {
+ link_id,
+ owner,
+ actor,
+ thread_id,
+ archived: !added,
+ origin,
+ }),
+ system_labels::SPAM
+ | system_labels::IMPORTANT
+ | system_labels::SENT
+ | system_labels::DRAFT => return None,
+ // Gmail category tabs (CATEGORY_PERSONAL/SOCIAL/...) are
+ // provider system labels, not user labels.
+ l if l.starts_with("CATEGORY_") => return None,
+ _ => {
+ let (added, removed) = if added {
+ (vec![label], vec![])
+ } else {
+ (vec![], vec![label])
+ };
+ Self::thread_labels_updated(ThreadLabelsUpdatedMetadata {
+ link_id,
+ owner,
+ actor,
+ thread_id,
+ added,
+ removed,
+ origin,
+ })
+ }
+ };
+ Some(event)
+ }
+
+ /// Build an event from a topic-specific event variant.
+ pub fn new(key: impl Into<String>, event: EmailTopicEvent) -> Self {
+ Self::with_event(key, Event::new(event))
+ }
+
+ /// Build an event from a pre-built envelope.
+ pub fn with_event(key: impl Into<String>, event: Event<EmailTopicEvent>) -> Self {
+ Self {
+ key: key.into(),
+ event,
+ }
+ }
+}
+
+impl MacroEvent for EmailMacroEvent {
+ type EventPayload = EmailTopicEvent;
+
+ 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/email/src/domain/events/test.rs b/crates/email/src/domain/events/test.rs
new file mode 100644
index 0000000000..0ba97ce0ec
--- /dev/null
+++ b/crates/email/src/domain/events/test.rs
@@ -0,0 +1,404 @@
+use chrono::{DateTime, Utc};
+use macro_event_broker::Event;
+use serde_json::json;
+
+use super::*;
+
+fn user_id(id: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(id.to_string()).expect("valid user id")
+}
+
+fn timestamp() -> DateTime<Utc> {
+ DateTime::parse_from_rfc3339("2026-01-02T03:04:05Z")
+ .expect("valid timestamp")
+ .with_timezone(&Utc)
+}
+
+#[test]
+fn message_received_wire_shape() {
+ let event = Event::with_event_id(
+ Uuid::nil(),
+ EmailTopicEvent::MessageReceived(MessageReceivedMetadata {
+ link_id: Uuid::nil(),
+ owner: user_id("macro|[email protected]"),
+ message_id: Uuid::nil(),
+ provider_message_id: "gmail-msg-1".to_string(),
+ thread_id: Uuid::nil(),
+ provider_thread_id: "gmail-thread-1".to_string(),
+ is_new_thread: true,
+ subject: Some("Quarterly report".to_string()),
+ from_email: Some("[email protected]".to_string()),
+ from_name: Some("Sender".to_string()),
+ to_emails: vec!["[email protected]".to_string()],
+ attachment_count: 2,
+ is_spam_or_trash: false,
+ received_at: Some(timestamp()),
+ }),
+ );
+
+ let value = serde_json::to_value(&event).expect("serializable");
+ assert_eq!(
+ value,
+ json!({
+ "event_id": "00000000-0000-0000-0000-000000000000",
+ "schema_version": 1,
+ "event_type": "email.message_received",
+ "metadata": {
+ "link_id": "00000000-0000-0000-0000-000000000000",
+ "owner": "macro|[email protected]",
+ "message_id": "00000000-0000-0000-0000-000000000000",
+ "provider_message_id": "gmail-msg-1",
+ "thread_id": "00000000-0000-0000-0000-000000000000",
+ "provider_thread_id": "gmail-thread-1",
+ "is_new_thread": true,
+ "subject": "Quarterly report",
+ "from_email": "[email protected]",
+ "from_name": "Sender",
+ "to_emails": ["[email protected]"],
+ "attachment_count": 2,
+ "is_spam_or_trash": false,
+ "received_at": "2026-01-02T03:04:05Z",
+ },
+ })
+ );
+}
+
+#[test]
+fn thread_archived_wire_shape_with_origin() {
+ let event = Event::with_event_id(
+ Uuid::nil(),
+ EmailTopicEvent::ThreadArchived(ThreadArchivedMetadata {
+ link_id: Uuid::nil(),
+ owner: user_id("macro|[email protected]"),
+ actor: None,
+ thread_id: Uuid::nil(),
+ archived: true,
+ origin: EmailEventOrigin::ProviderSync,
+ }),
+ );
+
+ let value = serde_json::to_value(&event).expect("serializable");
+ assert_eq!(
+ value,
+ json!({
+ "event_id": "00000000-0000-0000-0000-000000000000",
+ "schema_version": 1,
+ "event_type": "email.thread_archived",
+ "metadata": {
+ "link_id": "00000000-0000-0000-0000-000000000000",
+ "owner": "macro|[email protected]",
+ "actor": null,
+ "thread_id": "00000000-0000-0000-0000-000000000000",
+ "archived": true,
+ "origin": "provider_sync",
+ },
+ })
+ );
+}
+
+#[test]
+fn decode_round_trips() {
+ let link_id = Uuid::new_v4();
+ let original = EmailMacroEvent::message_deleted(MessageDeletedMetadata {
+ link_id,
+ owner: user_id("macro|[email protected]"),
+ message_id: Uuid::new_v4(),
+ provider_message_id: "gmail-msg-1".to_string(),
+ thread_id: Uuid::new_v4(),
+ });
+
+ let payload = serde_json::to_vec(original.event()).expect("serializable");
+ let decoded = EmailMacroEvent::decode(original.key(), &payload).expect("decodable payload");
+
+ assert_eq!(decoded.key(), link_id.to_string());
+ assert_eq!(decoded.event(), original.event());
+}
+
+#[test]
+fn events_are_keyed_by_link_id() {
+ let link_id = Uuid::new_v4();
+ let event = EmailMacroEvent::link_disconnected(LinkDisconnectedMetadata {
+ link_id,
+ owner: user_id("macro|[email protected]"),
+ email_address: "[email protected]".to_string(),
+ reason: LinkDisconnectReason::ManuallyDisabled,
+ });
+ assert_eq!(event.key(), link_id.to_string());
+}
+
+#[test]
+fn event_type_strings_follow_dot_convention() {
+ let link_id = Uuid::nil();
+ let owner = user_id("macro|[email protected]");
+ let actor = Some(user_id("macro|[email protected]"));
+
+ let cases = vec![
+ (
+ EmailMacroEvent::link_connected(LinkConnectedMetadata {
+ link_id,
+ owner: owner.clone(),
+ email_address: "[email protected]".to_string(),
+ provider: "GMAIL".to_string(),
+ is_primary: true,
+ connected_at: timestamp(),
+ }),
+ "email.link_connected",
+ ),
+ (
+ EmailMacroEvent::link_disconnected(LinkDisconnectedMetadata {
+ link_id,
+ owner: owner.clone(),
+ email_address: "[email protected]".to_string(),
+ reason: LinkDisconnectReason::AccessRevoked,
+ }),
+ "email.link_disconnected",
+ ),
+ (
+ EmailMacroEvent::link_reauth_required(LinkReauthRequiredMetadata {
+ link_id,
+ owner: owner.clone(),
+ email_address: "[email protected]".to_string(),
+ observed_at: timestamp(),
+ }),
+ "email.link_reauth_required",
+ ),
+ (
+ EmailMacroEvent::message_received(MessageReceivedMetadata {
+ link_id,
+ owner: owner.clone(),
+ message_id: Uuid::nil(),
+ provider_message_id: "m".to_string(),
+ thread_id: Uuid::nil(),
+ provider_thread_id: "t".to_string(),
+ is_new_thread: false,
+ subject: None,
+ from_email: None,
+ from_name: None,
+ to_emails: vec![],
+ attachment_count: 0,
+ is_spam_or_trash: false,
+ received_at: None,
+ }),
+ "email.message_received",
+ ),
+ (
+ EmailMacroEvent::message_sent(MessageSentMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ message_id: Uuid::nil(),
+ provider_message_id: "m".to_string(),
+ thread_id: Uuid::nil(),
+ provider_thread_id: "t".to_string(),
+ subject: None,
+ to_emails: vec![],
+ cc_emails: vec![],
+ origin: EmailEventOrigin::UserAction,
+ sent_at: timestamp(),
+ }),
+ "email.message_sent",
+ ),
+ (
+ EmailMacroEvent::message_deleted(MessageDeletedMetadata {
+ link_id,
+ owner: owner.clone(),
+ message_id: Uuid::nil(),
+ provider_message_id: "m".to_string(),
+ thread_id: Uuid::nil(),
+ }),
+ "email.message_deleted",
+ ),
+ (
+ EmailMacroEvent::message_send_queued(MessageSendQueuedMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ message_id: Uuid::nil(),
+ thread_id: Uuid::nil(),
+ scheduled_send_at: timestamp(),
+ is_scheduled: false,
+ }),
+ "email.message_send_queued",
+ ),
+ (
+ EmailMacroEvent::message_send_cancelled(MessageSendCancelledMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ message_id: Uuid::nil(),
+ thread_id: Uuid::nil(),
+ reason: SendCancelReason::Undo,
+ }),
+ "email.message_send_cancelled",
+ ),
+ (
+ EmailMacroEvent::thread_archived(ThreadArchivedMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ thread_id: Uuid::nil(),
+ archived: true,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ "email.thread_archived",
+ ),
+ (
+ EmailMacroEvent::thread_trashed(ThreadTrashedMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ thread_id: Uuid::nil(),
+ trashed: true,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ "email.thread_trashed",
+ ),
+ (
+ EmailMacroEvent::thread_read(ThreadReadMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ thread_id: Uuid::nil(),
+ is_read: true,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ "email.thread_read",
+ ),
+ (
+ EmailMacroEvent::thread_starred(ThreadStarredMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ thread_id: Uuid::nil(),
+ starred: true,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ "email.thread_starred",
+ ),
+ (
+ EmailMacroEvent::thread_project_changed(ThreadProjectChangedMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: user_id("macro|[email protected]"),
+ thread_id: Uuid::nil(),
+ previous_project_id: None,
+ project_id: Some("project-1".to_string()),
+ }),
+ "email.thread_project_changed",
+ ),
+ (
+ EmailMacroEvent::thread_labels_updated(ThreadLabelsUpdatedMetadata {
+ link_id,
+ owner: owner.clone(),
+ actor: actor.clone(),
+ thread_id: Uuid::nil(),
+ added: vec![LabelRef {
+ label_id: Some(Uuid::nil()),
+ provider_label_id: "Label_1".to_string(),
+ name: Some("Receipts".to_string()),
+ }],
+ removed: vec![],
+ origin: EmailEventOrigin::ProviderSync,
+ }),
+ "email.thread_labels_updated",
+ ),
+ ];
+
+ for (event, expected_type) in cases {
+ let value = serde_json::to_value(event.event()).expect("serializable");
+ assert_eq!(value["event_type"], *expected_type);
+ }
+}
+
+fn user_label() -> LabelRef {
+ LabelRef {
+ label_id: Some(Uuid::nil()),
+ provider_label_id: "Label_1".to_string(),
+ name: Some("Receipts".to_string()),
+ }
+}
+
+fn system_label(provider_label_id: &str) -> LabelRef {
+ LabelRef {
+ label_id: None,
+ provider_label_id: provider_label_id.to_string(),
+ name: None,
+ }
+}
+
+fn label_change(label: LabelRef, added: bool) -> Option<EmailMacroEvent> {
+ EmailMacroEvent::thread_label_change(
+ Uuid::nil(),
+ user_id("macro|[email protected]"),
+ None,
+ Uuid::nil(),
+ label,
+ added,
+ EmailEventOrigin::UserAction,
+ )
+}
+
+#[test]
+fn thread_label_change_maps_system_labels_to_semantic_events() {
+ let cases = [
+ ("UNREAD", true, "email.thread_read", "is_read", false),
+ ("UNREAD", false, "email.thread_read", "is_read", true),
+ ("STARRED", true, "email.thread_starred", "starred", true),
+ ("STARRED", false, "email.thread_starred", "starred", false),
+ ("TRASH", true, "email.thread_trashed", "trashed", true),
+ ("TRASH", false, "email.thread_trashed", "trashed", false),
+ ("INBOX", true, "email.thread_archived", "archived", false),
+ ("INBOX", false, "email.thread_archived", "archived", true),
+ ];
+
+ for (label, added, expected_type, state_field, expected_state) in cases {
+ let event = label_change(system_label(label), added)
+ .unwrap_or_else(|| panic!("{label} added={added} should map to an event"));
+ let value = serde_json::to_value(event.event()).expect("serializable");
+ assert_eq!(value["event_type"], expected_type, "{label} added={added}");
+ assert_eq!(
+ value["metadata"][state_field], expected_state,
+ "{label} added={added}"
+ );
+ }
+}
+
+#[test]
+fn thread_label_change_skips_unpublished_system_labels() {
+ for label in ["SPAM", "IMPORTANT", "SENT", "DRAFT"] {
+ assert!(
+ label_change(system_label(label), true).is_none(),
+ "{label} add should not publish"
+ );
+ assert!(
+ label_change(system_label(label), false).is_none(),
+ "{label} remove should not publish"
+ );
+ }
+}
+
+#[test]
+fn thread_label_change_maps_user_labels_to_diff() {
+ let added_event = label_change(user_label(), true).expect("user label maps to an event");
+ let value = serde_json::to_value(added_event.event()).expect("serializable");
+ assert_eq!(value["event_type"], "email.thread_labels_updated");
+ assert_eq!(
+ value["metadata"]["added"][0]["provider_label_id"],
+ "Label_1"
+ );
+ assert_eq!(value["metadata"]["removed"], serde_json::json!([]));
+
+ let removed_event = label_change(user_label(), false).expect("user label maps to an event");
+ let value = serde_json::to_value(removed_event.event()).expect("serializable");
+ assert_eq!(value["metadata"]["added"], serde_json::json!([]));
+ assert_eq!(
+ value["metadata"]["removed"][0]["provider_label_id"],
+ "Label_1"
+ );
+}
+
+#[test]
+fn thread_label_change_skips_gmail_category_labels() {
+ assert!(label_change(system_label("CATEGORY_PROMOTIONS"), true).is_none());
+ assert!(label_change(system_label("CATEGORY_SOCIAL"), false).is_none());
+}
diff --git a/crates/email/src/domain/models/label.rs b/crates/email/src/domain/models/label.rs
index c2e13e9eb7..00106b81f5 100644
--- a/crates/email/src/domain/models/label.rs
+++ b/crates/email/src/domain/models/label.rs
@@ -83,4 +83,14 @@ pub mod system_labels {
pub const STARRED: &str = "STARRED";
/// The TRASH label.
pub const TRASH: &str = "TRASH";
+ /// The INBOX label.
+ pub const INBOX: &str = "INBOX";
+ /// The SPAM label.
+ pub const SPAM: &str = "SPAM";
+ /// The IMPORTANT label.
+ pub const IMPORTANT: &str = "IMPORTANT";
+ /// The SENT label.
+ pub const SENT: &str = "SENT";
+ /// The DRAFT label.
+ pub const DRAFT: &str = "DRAFT";
}
diff --git a/crates/email/src/domain/ports.rs b/crates/email/src/domain/ports.rs
index bb4c58b9ed..5c3ac80ea9 100644
--- a/crates/email/src/domain/ports.rs
+++ b/crates/email/src/domain/ports.rs
@@ -261,11 +261,12 @@ pub trait EmailRepo: Send + Sync + 'static {
) -> impl Future<Output = Result<Vec<LinkLabel>, Self::Err>> + Send;
/// Delete unsent scheduled messages for a batch of draft message IDs.
+ /// Returns the message ids that actually had a pending scheduled send.
fn delete_scheduled_messages_batch(
&self,
message_ids: &[Uuid],
link_id: Uuid,
- ) -> impl Future<Output = Result<(), Self::Err>> + Send;
+ ) -> impl Future<Output = Result<Vec<Uuid>, Self::Err>> + Send;
/// Update the project assignment for a thread. Pass `None` to remove from project.
/// Returns `false` if the thread was not found.
diff --git a/crates/email/src/domain/service/draft.rs b/crates/email/src/domain/service/draft.rs
index 3b843809ab..c304992474 100644
--- a/crates/email/src/domain/service/draft.rs
+++ b/crates/email/src/domain/service/draft.rs
@@ -12,7 +12,7 @@ use uuid::Uuid;
use super::EmailServiceImpl;
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
diff --git a/crates/email/src/domain/service/mod.rs b/crates/email/src/domain/service/mod.rs
index 1921a17d3d..8ac8b7cd0f 100644
--- a/crates/email/src/domain/service/mod.rs
+++ b/crates/email/src/domain/service/mod.rs
@@ -6,6 +6,7 @@ mod thread;
mod thread_labels;
use crate::domain::{
+ events::{EmailMacroEvent, ThreadProjectChangedMetadata},
models::{
CreateDraftInput, CreatedDraft, EmailErr, EmailFilter, EnrichedEmailThreadPreview,
GetEmailsRequest, Link, LinkLabel, ParsedThread, Thread, UpdateThreadLabelsResult,
@@ -19,17 +20,19 @@ use entity_access::domain::models::{
};
use entity_access_management::domain::ports::EntityAccessManagementService;
use frecency::domain::ports::FrecencyQueryService;
+use macro_event_broker::{MacroEventBroker, NoopMacroEventBroker};
use model_entity::EntityType;
use models_pagination::{PaginatedCursor, SimpleSortMethod};
use uuid::Uuid;
#[derive(Clone)]
-pub struct EmailServiceImpl<T, U, E, CS, Eam> {
+pub struct EmailServiceImpl<T, U, E, CS, Eam, B = NoopMacroEventBroker> {
pub(crate) email_repo: T,
pub(crate) frecency_service: U,
pub(crate) enqueuer: E,
pub(crate) crm_service: CS,
pub(crate) entity_access_management_service: Eam,
+ pub(crate) macro_event_broker: B,
pub(crate) sent_undo_delay_secs: u32,
}
@@ -55,12 +58,45 @@ where
enqueuer,
crm_service,
entity_access_management_service,
+ macro_event_broker: NoopMacroEventBroker,
sent_undo_delay_secs,
}
}
}
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam> {
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B> {
+ /// Replace the event broker used to publish `macro.email` events.
+ /// [`new`](Self::new) starts with a [`NoopMacroEventBroker`].
+ pub fn with_macro_event_broker<B2: MacroEventBroker>(
+ self,
+ macro_event_broker: B2,
+ ) -> EmailServiceImpl<T, U, E, CS, Eam, B2> {
+ EmailServiceImpl {
+ email_repo: self.email_repo,
+ frecency_service: self.frecency_service,
+ enqueuer: self.enqueuer,
+ crm_service: self.crm_service,
+ entity_access_management_service: self.entity_access_management_service,
+ macro_event_broker,
+ sent_undo_delay_secs: self.sent_undo_delay_secs,
+ }
+ }
+
+ /// Publish an email event to the `macro.email` topic, logging and
+ /// dropping failures — event emission must never fail the operation.
+ pub(crate) async fn publish_email_event(&self, event: &EmailMacroEvent)
+ where
+ B: MacroEventBroker,
+ {
+ let _ = self
+ .macro_event_broker
+ .send_event(event)
+ .await
+ .inspect_err(|e| tracing::error!(error=?e, "failed to publish email macro event"));
+ }
+}
+
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B> {
/// Validate and normalize email filter input.
fn validate_email_filter_input(
input: UpsertEmailFilterInput,
@@ -120,13 +156,14 @@ impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam> {
}
}
-impl<T, U, E, CS, Eam> EmailService for EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailService for EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
E: EmailMessageEnqueuer,
CS: CrmService,
Eam: EntityAccessManagementService,
+ B: MacroEventBroker,
anyhow::Error: From<T::Err>,
anyhow::Error: From<E::Err>,
{
@@ -299,6 +336,46 @@ where
|e| tracing::error!(error=?e, project_id=%new, "unable to add thread project access"),
);
}
+
+ // Best-effort: emit only when the acting user and the thread's
+ // link both resolve (the receipt is authenticated on this route).
+ match thread_receipt.get_authenticated_user() {
+ Ok(actor) => match self
+ .email_repo
+ .owned_link_for_thread(thread_id, actor.clone())
+ .await
+ {
+ Ok(Some(link)) => {
+ self.publish_email_event(&EmailMacroEvent::thread_project_changed(
+ ThreadProjectChangedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: actor.clone(),
+ thread_id,
+ previous_project_id: old_project_id.clone(),
+ project_id: project_id.map(|p| p.to_string()),
+ },
+ ))
+ .await;
+ }
+ Ok(None) => tracing::warn!(
+ %thread_id,
+ "skipping thread_project_changed event: no owned link resolved for actor"
+ ),
+ Err(e) => {
+ let e = anyhow::Error::from(e);
+ tracing::warn!(
+ error=?e,
+ %thread_id,
+ "skipping thread_project_changed event: link lookup failed"
+ );
+ }
+ },
+ Err(_) => tracing::debug!(
+ %thread_id,
+ "skipping thread_project_changed event: receipt has no authenticated user"
+ ),
+ }
}
Ok(old_project_id)
diff --git a/crates/email/src/domain/service/previews.rs b/crates/email/src/domain/service/previews.rs
index 3ad0e1dfeb..d8aa3d56b0 100644
--- a/crates/email/src/domain/service/previews.rs
+++ b/crates/email/src/domain/service/previews.rs
@@ -19,7 +19,7 @@ use uuid::Uuid;
use super::EmailServiceImpl;
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
diff --git a/crates/email/src/domain/service/send.rs b/crates/email/src/domain/service/send.rs
index f6dd0770e3..e3da4b4569 100644
--- a/crates/email/src/domain/service/send.rs
+++ b/crates/email/src/domain/service/send.rs
@@ -1,19 +1,22 @@
use chrono::Duration;
use crate::domain::{
+ events::{EmailMacroEvent, MessageSendQueuedMetadata},
models::{CreateDraftInput, CreatedDraft, EmailErr, Link},
ports::{EmailMessageEnqueuer, EmailRepo},
};
use frecency::domain::ports::FrecencyQueryService;
+use macro_event_broker::MacroEventBroker;
use super::EmailServiceImpl;
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
E: EmailMessageEnqueuer,
CS: crm::domain::service::CrmService,
+ B: MacroEventBroker,
anyhow::Error: From<T::Err>,
anyhow::Error: From<E::Err>,
{
@@ -25,7 +28,8 @@ where
mut input: CreateDraftInput,
) -> Result<CreatedDraft, EmailErr> {
let delay_secs = self.sent_undo_delay_secs;
- input.send_time = Some(chrono::Utc::now() + Duration::seconds(delay_secs as i64));
+ let send_time = chrono::Utc::now() + Duration::seconds(delay_secs as i64);
+ input.send_time = Some(send_time);
let created = self
.prepare_and_insert_db_message(link, accessible_inboxes, input, false)
@@ -38,6 +42,22 @@ where
.await
.map_err(|e| EmailErr::RepoErr(anyhow::Error::from(e)))?;
+ // The undo-window send is now committed and queued; the matching
+ // message_sent / message_send_cancelled event resolves it later.
+ // Actor is not tracked on this path — the inbox owner is on `link`.
+ self.publish_email_event(&EmailMacroEvent::message_send_queued(
+ MessageSendQueuedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ message_id: created.db_id,
+ thread_id: created.thread_db_id,
+ scheduled_send_at: send_time,
+ is_scheduled: false,
+ },
+ ))
+ .await;
+
Ok(created)
}
}
diff --git a/crates/email/src/domain/service/thread.rs b/crates/email/src/domain/service/thread.rs
index 828d615b4b..b42097f377 100644
--- a/crates/email/src/domain/service/thread.rs
+++ b/crates/email/src/domain/service/thread.rs
@@ -27,7 +27,7 @@ struct ThreadFetchResult {
is_owner: bool,
}
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
diff --git a/crates/email/src/domain/service/thread_labels.rs b/crates/email/src/domain/service/thread_labels.rs
index fe3a5265a8..fe549e3762 100644
--- a/crates/email/src/domain/service/thread_labels.rs
+++ b/crates/email/src/domain/service/thread_labels.rs
@@ -1,18 +1,23 @@
use crate::domain::{
- models::{EmailErr, Link, UpdateThreadLabelsResult, label::system_labels},
+ events::{
+ EmailEventOrigin, EmailMacroEvent, LabelRef, MessageSendCancelledMetadata, SendCancelReason,
+ },
+ models::{EmailErr, Link, LinkLabel, UpdateThreadLabelsResult, label::system_labels},
ports::{EmailMessageEnqueuer, EmailRepo},
};
use frecency::domain::ports::FrecencyQueryService;
+use macro_event_broker::MacroEventBroker;
use uuid::Uuid;
use super::EmailServiceImpl;
-impl<T, U, E, CS, Eam> EmailServiceImpl<T, U, E, CS, Eam>
+impl<T, U, E, CS, Eam, B> EmailServiceImpl<T, U, E, CS, Eam, B>
where
T: EmailRepo,
U: FrecencyQueryService,
E: EmailMessageEnqueuer,
CS: crm::domain::service::CrmService,
+ B: MacroEventBroker,
anyhow::Error: From<T::Err>,
anyhow::Error: From<E::Err>,
{
@@ -88,6 +93,7 @@ where
}
// When trashing a thread, cancel any pending scheduled sends for drafts
+ let mut cancelled_send_ids: Vec<Uuid> = Vec::new();
if add && provider_label_id == system_labels::TRASH {
let draft_message_ids: Vec<_> = messages
.iter()
@@ -95,17 +101,27 @@ where
.map(|m| m.db_id)
.collect();
- if !draft_message_ids.is_empty()
- && let Err(e) = self
+ if !draft_message_ids.is_empty() {
+ match self
.email_repo
.delete_scheduled_messages_batch(&draft_message_ids, link.id)
.await
- {
- let err = anyhow::Error::from(e);
- tracing::error!(error=?err, "failed to cancel scheduled sends for trashed drafts");
+ {
+ Ok(deleted) => cancelled_send_ids = deleted,
+ Err(e) => {
+ let err = anyhow::Error::from(e);
+ tracing::error!(error=?err, "failed to cancel scheduled sends for trashed drafts");
+ }
+ }
}
}
+ // Publish semantic macro.email events now that the optimistic DB
+ // writes are committed. The provider echo of this change finds no
+ // label diff during inbox sync, so each change publishes only once.
+ self.publish_thread_label_events(link, thread_id, &label, add, &cancelled_send_ids)
+ .await;
+
// Enqueue Gmail API calls via the gmail_ops worker (provider messages only)
let provider_messages: Vec<(Uuid, String)> = messages
.iter()
@@ -140,4 +156,50 @@ where
failed_ids: vec![],
})
}
+
+ /// Map a user-initiated thread label change onto the semantic
+ /// macro.email event for that label (see
+ /// [`EmailMacroEvent::thread_label_change`]). Scheduled sends cancelled
+ /// by trashing publish `message_send_cancelled`. Actor is not tracked on
+ /// this path — the inbox owner is on `link`.
+ async fn publish_thread_label_events(
+ &self,
+ link: &Link,
+ thread_id: Uuid,
+ label: &LinkLabel,
+ add: bool,
+ cancelled_send_ids: &[Uuid],
+ ) {
+ let event = EmailMacroEvent::thread_label_change(
+ link.id,
+ link.macro_id.clone(),
+ None,
+ thread_id,
+ LabelRef {
+ label_id: Some(label.id),
+ provider_label_id: label.provider_label_id.clone(),
+ name: Some(label.name.clone()),
+ },
+ add,
+ EmailEventOrigin::UserAction,
+ );
+
+ if let Some(event) = event {
+ self.publish_email_event(&event).await;
+ }
+
+ for message_id in cancelled_send_ids {
+ self.publish_email_event(&EmailMacroEvent::message_send_cancelled(
+ MessageSendCancelledMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ message_id: *message_id,
+ thread_id,
+ reason: SendCancelReason::ThreadTrashed,
+ },
+ ))
+ .await;
+ }
+ }
}
diff --git a/crates/email/src/outbound/email_pg_repo/label.rs b/crates/email/src/outbound/email_pg_repo/label.rs
index 9d2ee8feed..a33219d3f6 100644
--- a/crates/email/src/outbound/email_pg_repo/label.rs
+++ b/crates/email/src/outbound/email_pg_repo/label.rs
@@ -339,21 +339,22 @@ pub(crate) async fn delete_scheduled_messages_batch(
pool: &PgPool,
message_ids: &[Uuid],
link_id: Uuid,
-) -> Result<(), sqlx::Error> {
+) -> Result<Vec<Uuid>, sqlx::Error> {
if message_ids.is_empty() {
- return Ok(());
+ return Ok(Vec::new());
}
- sqlx::query!(
+ let deleted = sqlx::query_scalar!(
r#"
DELETE FROM email_scheduled_messages
WHERE message_id = ANY($1) AND link_id = $2 AND sent = false
+ RETURNING message_id
"#,
message_ids,
link_id
)
- .execute(pool)
+ .fetch_all(pool)
.await?;
- Ok(())
+ Ok(deleted)
}
diff --git a/crates/email/src/outbound/email_pg_repo/mod.rs b/crates/email/src/outbound/email_pg_repo/mod.rs
index 12ec663aeb..f643d7cb73 100644
--- a/crates/email/src/outbound/email_pg_repo/mod.rs
+++ b/crates/email/src/outbound/email_pg_repo/mod.rs
@@ -300,7 +300,7 @@ impl EmailRepo for EmailPgRepo {
&self,
message_ids: &[Uuid],
link_id: Uuid,
- ) -> Result<(), Self::Err> {
+ ) -> Result<Vec<Uuid>, Self::Err> {
label::delete_scheduled_messages_batch(&self.pool, message_ids, link_id).await
}
diff --git a/crates/email/src/outbound/email_pg_repo/test/message.rs b/crates/email/src/outbound/email_pg_repo/test/message.rs
index 8fe31b6abf..8f4071992a 100644
--- a/crates/email/src/outbound/email_pg_repo/test/message.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/message.rs
@@ -709,3 +709,55 @@ async fn test_upsert_recipients_empty_deletes_all(pool: Pool<Postgres>) -> anyho
Ok(())
}
+
+// ── delete_scheduled_messages_batch ─────────────────────────────────
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_message"))
+)]
+async fn test_delete_scheduled_messages_batch_returns_only_pending(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let repo = EmailPgRepo::new(pool.clone());
+ let link_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;
+ // msg3 has a pending scheduled send; msg2's scheduled send already went
+ // out; msg1 was never scheduled.
+ let pending = Uuid::parse_str("ee000003-0000-0000-0000-000000000003")?;
+ let already_sent = Uuid::parse_str("ee000002-0000-0000-0000-000000000002")?;
+ let unscheduled = Uuid::parse_str("ee000001-0000-0000-0000-000000000001")?;
+
+ let deleted = repo
+ .delete_scheduled_messages_batch(&[pending, already_sent, unscheduled], link_id)
+ .await?;
+ assert_eq!(
+ deleted,
+ vec![pending],
+ "only the message with a pending scheduled send is reported"
+ );
+
+ let deleted = repo
+ .delete_scheduled_messages_batch(&[pending, already_sent, unscheduled], link_id)
+ .await?;
+ assert!(deleted.is_empty(), "second delete finds nothing pending");
+
+ let sent_rows: i64 =
+ sqlx::query_scalar("SELECT COUNT(*) FROM email_scheduled_messages WHERE sent = true")
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(sent_rows, 1, "already-sent scheduled rows are untouched");
+
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn test_delete_scheduled_messages_batch_empty_input(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let repo = EmailPgRepo::new(pool);
+ let deleted = repo
+ .delete_scheduled_messages_batch(&[], Uuid::new_v4())
+ .await?;
+ assert!(deleted.is_empty());
+ Ok(())
+}
diff --git a/crates/macro_event_broker/src/lib.rs b/crates/macro_event_broker/src/lib.rs
index bf72397cb3..25ba7464d9 100644
--- a/crates/macro_event_broker/src/lib.rs
+++ b/crates/macro_event_broker/src/lib.rs
@@ -14,7 +14,9 @@
pub mod domain;
pub use domain::models::{Event, EventBrokerError, MacroEvent, TopicEvent};
-pub use macro_event_topics::{MacroChannelsTopic, MacroDocumentsTopic, MacroExampleTopic, Topic};
+pub use macro_event_topics::{
+ MacroChannelsTopic, MacroDocumentsTopic, MacroEmailTopic, MacroExampleTopic, Topic,
+};
#[cfg(feature = "ports")]
pub use domain::ports::{EventPublisher, MacroEventBroker};
diff --git a/crates/macro_event_topics/src/lib.rs b/crates/macro_event_topics/src/lib.rs
index 678055ced6..61fcf897cc 100644
--- a/crates/macro_event_topics/src/lib.rs
+++ b/crates/macro_event_topics/src/lib.rs
@@ -53,4 +53,6 @@ topics! {
MacroDocumentsTopic => "macro.documents",
/// Channel lifecycle, message, participant, and attachment events.
MacroChannelsTopic => "macro.channels",
+ /// Email lifecycle events (links, messages, threads, labels).
+ MacroEmailTopic => "macro.email",
}
diff --git a/infra/stacks/email-service/index.ts b/infra/stacks/email-service/index.ts
index c6eefdaa86..fd5b291921 100644
--- a/infra/stacks/email-service/index.ts
+++ b/infra/stacks/email-service/index.ts
@@ -9,6 +9,7 @@ import {
import {
config,
DopplerEcsEnvironment,
+ getKafkaClusterPolicy,
getMacroApiToken,
getMacroNotify,
getSearchEventQueue,
@@ -293,6 +294,7 @@ const emailServiceRole = new aws.iam.Role('email-service-role-2', {
emailServiceSecretsPolicy.arn,
emailServiceSqsPolicy.arn,
emailServiceFrecencyPolicy.arn,
+ getKafkaClusterPolicy(),
],
});
diff --git a/services/document_cognition_service/src/api/context/test.rs b/services/document_cognition_service/src/api/context/test.rs
index bdad43e4eb..753cd6ade6 100644
--- a/services/document_cognition_service/src/api/context/test.rs
+++ b/services/document_cognition_service/src/api/context/test.rs
@@ -249,6 +249,14 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
properties_service.clone(),
entity_access_service.clone(),
);
+
+ // Producer creation is lazy: nothing connects to Kafka unless an event
+ // is published, so a dummy broker address is safe for tests.
+ let macro_event_broker = macro_event_broker::MacroEventBrokerService::new(
+ macro_event_broker::KafkaEventPublisher::new("localhost:9092")
+ .expect("kafka producer config is valid"),
+ );
+
let document_service = documents::domain::service::DocumentServiceImpl::new(
document_repo,
cloudfront_config,
@@ -260,12 +268,7 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
entity_access_management::outbound::PgRepository::new(pool.clone()),
),
ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(pool.clone())),
- // Producer creation is lazy: nothing connects to Kafka unless an event
- // is published, so a dummy broker address is safe for tests.
- macro_event_broker::MacroEventBrokerService::new(
- macro_event_broker::KafkaEventPublisher::new("localhost:9092")
- .expect("kafka producer config is valid"),
- ),
+ macro_event_broker.clone(),
);
let test_lexical_client = LexicalClient::new("test".into(), "http://nofileshere".into());
let test_editing_client =
@@ -288,18 +291,21 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::Ar
ai_tools::build_properties_tool_context(properties_service, entity_access_service.clone());
let email_tool_context = email::inbound::toolset::EmailToolContext::new(
- Arc::new(email::domain::service::EmailServiceImpl::new(
- email::outbound::EmailPgRepo::new(pool.clone()),
- frecency::domain::services::FrecencyQueryServiceImpl::new(
- frecency::outbound::postgres::FrecencyPgStorage::new(pool.clone()),
- ),
- sqs_client.clone(),
- crm_service.clone(),
- entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
- entity_access_management::outbound::PgRepository::new(pool.clone()),
- ),
- 0,
- )),
+ Arc::new(
+ email::domain::service::EmailServiceImpl::new(
+ email::outbound::EmailPgRepo::new(pool.clone()),
+ frecency::domain::services::FrecencyQueryServiceImpl::new(
+ frecency::outbound::postgres::FrecencyPgStorage::new(pool.clone()),
+ ),
+ sqs_client.clone(),
+ crm_service.clone(),
+ entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
+ entity_access_management::outbound::PgRepository::new(pool.clone()),
+ ),
+ 0,
+ )
+ .with_macro_event_broker(macro_event_broker),
+ ),
Arc::new(email::domain::ports::NoOpGmailTokenProvider),
entity_access_service.clone(),
);
diff --git a/services/document_cognition_service/src/main.rs b/services/document_cognition_service/src/main.rs
index ad97e3f3dd..ec213d56fc 100644
--- a/services/document_cognition_service/src/main.rs
+++ b/services/document_cognition_service/src/main.rs
@@ -293,7 +293,7 @@ async fn main() -> anyhow::Result<()> {
entity_access_management::outbound::PgRepository::new(db.clone()),
),
ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),
- macro_event_broker,
+ macro_event_broker.clone(),
);
let lexical_client_for_tools = (*lexical_client).clone();
let document_tool_context = DocumentToolContext::new(
@@ -351,16 +351,19 @@ async fn main() -> anyhow::Result<()> {
tracing::info!("initialized properties tool context");
let email_tool_context = email::inbound::toolset::EmailToolContext::new(
- Arc::new(EmailServiceImpl::new(
- EmailPgRepo::new(db.clone()),
- FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
- sqs_client.clone(),
- crm_service.clone(),
- entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
- entity_access_management::outbound::PgRepository::new(db.clone()),
- ),
- 0,
- )),
+ Arc::new(
+ EmailServiceImpl::new(
+ EmailPgRepo::new(db.clone()),
+ FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
+ sqs_client.clone(),
+ crm_service.clone(),
+ entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
+ entity_access_management::outbound::PgRepository::new(db.clone()),
+ ),
+ 0,
+ )
+ .with_macro_event_broker(macro_event_broker.clone()),
+ ),
Arc::new(email::domain::ports::NoOpGmailTokenProvider),
Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
db.clone(),
diff --git a/services/email_service/Cargo.toml b/services/email_service/Cargo.toml
index 4b203e7151..01607e6630 100644
--- a/services/email_service/Cargo.toml
+++ b/services/email_service/Cargo.toml
@@ -116,6 +116,7 @@ macro_config = { path = "../../crates/macro_config" }
macro_db_client = { path = "../../crates/macro_db_client" }
macro_entrypoint = { path = "../../crates/macro_entrypoint" }
macro_env = { path = "../../crates/macro_env" }
+macro_event_broker = { path = "../../crates/macro_event_broker" }
mailparse = "0.16.1"
macro_env_var = { path = "../../crates/macro_env_var" }
macro_queues = { path = "../../crates/macro_queues" }
diff --git a/services/email_service/src/api/context.rs b/services/email_service/src/api/context.rs
index 7a9dfd7664..93b016ab8a 100644
--- a/services/email_service/src/api/context.rs
+++ b/services/email_service/src/api/context.rs
@@ -15,6 +15,7 @@ use entity_access::{domain::service::EntityAccessServiceImpl, outbound::PgAccess
use entity_access_management::domain::service::EntityAccessManagementServiceImpl;
use frecency::{domain::services::FrecencyQueryServiceImpl, outbound::postgres::FrecencyPgStorage};
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use macro_middleware::auth::internal_access::InternalApiKey;
use static_file_service_client::StaticFileServiceClient;
use std::sync::Arc;
@@ -32,6 +33,7 @@ pub(crate) type EmailSvc = EmailServiceImpl<
crm::outbound::no_op_resolver::NoOpCompanyMetadataResolver,
>,
EmailEntityAccessManagementService,
+ MacroEventBrokerService<KafkaEventPublisher>,
>;
#[derive(Clone, FromRef)]
@@ -52,4 +54,5 @@ pub(crate) struct ApiContext {
pub entity_access_service: Arc<EmailEntityAccessService>,
pub email_thread_state: EmailThreadRouterState<EmailSvc, EmailEntityAccessService>,
pub gmail_token_state: GmailTokenState<GmailTokenProviderImpl>,
+ pub macro_event_broker: Arc<MacroEventBrokerService<KafkaEventPublisher>>,
}
diff --git a/services/email_service/src/api/email/drafts/scheduled/remove.rs b/services/email_service/src/api/email/drafts/scheduled/remove.rs
index 5ef0350f91..335cb601fb 100644
--- a/services/email_service/src/api/email/drafts/scheduled/remove.rs
+++ b/services/email_service/src/api/email/drafts/scheduled/remove.rs
@@ -3,6 +3,8 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use email::domain::events::{EmailMacroEvent, MessageSendCancelledMetadata, SendCancelReason};
+use email_service::pubsub::publish_email_event;
use model::response::ErrorResponse;
use models_email::service::link::Link;
use strum_macros::AsRefStr;
@@ -93,5 +95,33 @@ pub async fn handler(
tx.commit().await?;
+ // Best-effort event: resolve the draft's thread for the payload.
+ let thread_db_id = email_db_client::messages::get_simple_messages::get_simple_message(
+ &ctx.db,
+ &message_id,
+ &link.fusionauth_user_id,
+ )
+ .await
+ .inspect_err(
+ |e| tracing::warn!(error=?e, %message_id, "skipping message_send_cancelled event: draft lookup failed"),
+ )
+ .ok()
+ .flatten()
+ .map(|m| m.thread_db_id);
+ if let Some(thread_id) = thread_db_id {
+ publish_email_event(
+ ctx.macro_event_broker.as_ref(),
+ &EmailMacroEvent::message_send_cancelled(MessageSendCancelledMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: Some(link.macro_id.clone()),
+ message_id,
+ thread_id,
+ reason: SendCancelReason::Undo,
+ }),
+ )
+ .await;
+ }
+
Ok(StatusCode::NO_CONTENT)
}
diff --git a/services/email_service/src/api/email/drafts/scheduled/upsert.rs b/services/email_service/src/api/email/drafts/scheduled/upsert.rs
index a86796f38e..2447fde17e 100644
--- a/services/email_service/src/api/email/drafts/scheduled/upsert.rs
+++ b/services/email_service/src/api/email/drafts/scheduled/upsert.rs
@@ -3,6 +3,8 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use email::domain::events::{EmailMacroEvent, MessageSendQueuedMetadata};
+use email_service::pubsub::publish_email_event;
use model::response::ErrorResponse;
use models_email::service::link::Link;
use models_email::service::message::ScheduledMessage;
@@ -105,6 +107,35 @@ pub async fn handler(
.await?;
tx.commit().await?;
+ // Best-effort event: resolve the draft's thread for the payload.
+ let thread_db_id = email_db_client::messages::get_simple_messages::get_simple_message(
+ &ctx.db,
+ &draft_id,
+ &link.fusionauth_user_id,
+ )
+ .await
+ .inspect_err(
+ |e| tracing::warn!(error=?e, %draft_id, "skipping message_send_queued event: draft lookup failed"),
+ )
+ .ok()
+ .flatten()
+ .map(|m| m.thread_db_id);
+ if let Some(thread_id) = thread_db_id {
+ publish_email_event(
+ ctx.macro_event_broker.as_ref(),
+ &EmailMacroEvent::message_send_queued(MessageSendQueuedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: Some(link.macro_id.clone()),
+ message_id: draft_id,
+ thread_id,
+ scheduled_send_at: request.send_time,
+ is_scheduled: true,
+ }),
+ )
+ .await;
+ }
+
Ok(Json(UpsertScheduledResponse {
message_id: draft_id,
send_time: request.send_time,
diff --git a/services/email_service/src/api/email/init.rs b/services/email_service/src/api/email/init.rs
index d2d15eee67..eb60dc8125 100644
--- a/services/email_service/src/api/email/init.rs
+++ b/services/email_service/src/api/email/init.rs
@@ -7,9 +7,11 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json, Response},
};
+use email::domain::events::{EmailMacroEvent, LinkConnectedMetadata};
use email::domain::models::UserProvider;
use email::domain::ports::EmailRepo;
use email::outbound::EmailPgRepo;
+use email_service::pubsub::publish_email_event;
use email_utils::token_cache_key::TokenCacheKey;
use macro_user_id::email::EmailStr;
use macro_user_id::user_id::MacroUserIdStr;
@@ -659,6 +661,21 @@ async fn init_user(
})
.ok();
+ // Same gate as the history row above: only a genuinely new connection
+ // reaches here, so re-inits and concurrent duplicates don't re-publish.
+ publish_email_event(
+ ctx.macro_event_broker.as_ref(),
+ &EmailMacroEvent::link_connected(LinkConnectedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ email_address: link.email_address.0.as_ref().to_string(),
+ provider: link.provider.as_str().to_string(),
+ is_primary: link.is_primary,
+ connected_at: link.created_at,
+ }),
+ )
+ .await;
+
let ps_message = BackfillPubsubMessage {
backfill_operation: BackfillOperation::Init(JobScopedPayload {
link_id: link.id,
diff --git a/services/email_service/src/api/email/messages/labels.rs b/services/email_service/src/api/email/messages/labels.rs
index 3214eb7133..c53abb146c 100644
--- a/services/email_service/src/api/email/messages/labels.rs
+++ b/services/email_service/src/api/email/messages/labels.rs
@@ -5,6 +5,8 @@ use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use email::domain::events::{EmailEventOrigin, EmailMacroEvent, LabelRef};
+use email_service::pubsub::publish_email_event;
use model::response::ErrorResponse;
use model::user::UserContext;
use models_email::service;
@@ -233,6 +235,31 @@ pub async fn handler(
}
}
+ // Publish semantic macro.email events, rolled up to one per affected
+ // thread (the batch may span several threads in the same inbox).
+ let actor = Some(link.macro_id.clone());
+ let mut event_thread_ids: Vec<Uuid> = db_messages.iter().map(|m| m.thread_db_id).collect();
+ event_thread_ids.sort_unstable();
+ event_thread_ids.dedup();
+ for thread_id in event_thread_ids {
+ let event = EmailMacroEvent::thread_label_change(
+ link.id,
+ link.macro_id.clone(),
+ actor.clone(),
+ thread_id,
+ LabelRef {
+ label_id: label.id,
+ provider_label_id: provider_label_id.clone(),
+ name: label.name.clone(),
+ },
+ is_adding,
+ EmailEventOrigin::UserAction,
+ );
+ if let Some(event) = event {
+ publish_email_event(ctx.macro_event_broker.as_ref(), &event).await;
+ }
+ }
+
// Enqueue gmail ops messages in batch (drafts have no provider_id, skip them)
let (labels_to_add, labels_to_remove) = if is_adding {
(vec![provider_label_id.clone()], Vec::new())
diff --git a/services/email_service/src/api/email/threads/archived.rs b/services/email_service/src/api/email/threads/archived.rs
index 399425d328..f0faa1c671 100644
--- a/services/email_service/src/api/email/threads/archived.rs
+++ b/services/email_service/src/api/email/threads/archived.rs
@@ -4,7 +4,9 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use email::domain::events::{EmailEventOrigin, EmailMacroEvent, ThreadArchivedMetadata};
use email_db_client::threads::update::update_inbox_visible_status;
+use email_service::pubsub::publish_email_event;
use model::response::{EmptyResponse, ErrorResponse};
use model::user::UserContext;
use models_email::service::label::system_labels;
@@ -171,6 +173,19 @@ pub async fn archived_handler(
}
}
+ publish_email_event(
+ ctx.macro_event_broker.as_ref(),
+ &EmailMacroEvent::thread_archived(ThreadArchivedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: Some(link.macro_id.clone()),
+ thread_id,
+ archived: is_archiving,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ )
+ .await;
+
// Enqueue one gmail_ops message per provider message
let (labels_to_add, labels_to_remove) = if is_archiving {
(Vec::new(), vec![system_labels::INBOX.to_string()])
diff --git a/services/email_service/src/api/email/threads/seen.rs b/services/email_service/src/api/email/threads/seen.rs
index af96d0e95a..6dfa2aee8e 100644
--- a/services/email_service/src/api/email/threads/seen.rs
+++ b/services/email_service/src/api/email/threads/seen.rs
@@ -4,6 +4,8 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use email::domain::events::{EmailEventOrigin, EmailMacroEvent, ThreadReadMetadata};
+use email_service::pubsub::publish_email_event;
use model::response::{EmptyResponse, ErrorResponse};
use model::user::UserContext;
use models_email::service::label::system_labels;
@@ -160,6 +162,19 @@ pub async fn seen_handler(
}
}
+ publish_email_event(
+ ctx.macro_event_broker.as_ref(),
+ &EmailMacroEvent::thread_read(ThreadReadMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: Some(link.macro_id.clone()),
+ thread_id,
+ is_read: true,
+ origin: EmailEventOrigin::UserAction,
+ }),
+ )
+ .await;
+
// Enqueue gmail ops messages in batch
let gmail_ops_messages: Vec<_> = unread_messages
.iter()
diff --git a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
index 7fa79de24b..39b3226e16 100644
--- a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
+++ b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
@@ -5,6 +5,7 @@ use email_service::config::Config;
use email_service::pubsub::CrmMetadataResolver;
use macro_entrypoint::MacroEntrypoint;
use macro_env::Environment;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use macro_service_urls::{
AuthServiceUrl, ConnectionGatewayUrl, DocumentStorageServiceUrl, StaticFileServiceUrl,
};
@@ -91,6 +92,11 @@ async fn main() -> anyhow::Result<()> {
.sfs_delete_queue(&sfs_delete_queue)
.email_link_manager_queue(&link_manager_queue);
+ let macro_event_broker = MacroEventBrokerService::new(
+ KafkaEventPublisher::new(config.kafka_brokers.as_ref())
+ .context("failed to create kafka event publisher")?,
+ );
+
let contacts_ingress = Arc::new(contacts::domain::service::SqsContactsIngress {
queue: contacts::outbound::ingress::SqsContactsQueue::new(
aws_sdk_sqs::Client::new(&gmail_queue_aws_config),
@@ -303,6 +309,7 @@ async fn main() -> anyhow::Result<()> {
let dss_client_inbox_sync = dss_client.clone();
let system_properties_service_inbox_sync = system_properties_service.clone();
let crm_service_inbox_sync = crm_service.clone();
+ let macro_event_broker_inbox_sync = macro_event_broker.clone();
tokio::spawn(async move {
email_service::pubsub::inbox_sync::worker::run_worker(
db_inbox_sync,
@@ -318,6 +325,7 @@ async fn main() -> anyhow::Result<()> {
dss_client_inbox_sync,
system_properties_service_inbox_sync,
crm_service_inbox_sync,
+ macro_event_broker_inbox_sync,
config.notifications_enabled,
false,
)
@@ -343,6 +351,7 @@ async fn main() -> anyhow::Result<()> {
let dss_client_inbox_sync = dss_client.clone();
let system_properties_service_inbox_sync = system_properties_service.clone();
let crm_service_inbox_sync = crm_service.clone();
+ let macro_event_broker_inbox_sync = macro_event_broker.clone();
tokio::spawn(async move {
email_service::pubsub::inbox_sync::worker::run_worker(
db_inbox_sync,
@@ -358,6 +367,7 @@ async fn main() -> anyhow::Result<()> {
dss_client_inbox_sync,
system_properties_service_inbox_sync,
crm_service_inbox_sync,
+ macro_event_broker_inbox_sync,
config.notifications_enabled,
true,
)
@@ -433,6 +443,7 @@ async fn main() -> anyhow::Result<()> {
let dss_client_backfill = dss_client.clone();
let system_properties_service_backfill = system_properties_service.clone();
let crm_service_backfill = crm_service_backfill.clone();
+ let macro_event_broker_backfill = macro_event_broker.clone();
tokio::spawn(async move {
email_service::pubsub::backfill::worker::run_worker(
db_backfill,
@@ -448,6 +459,7 @@ async fn main() -> anyhow::Result<()> {
dss_client_backfill,
system_properties_service_backfill,
crm_service_backfill,
+ macro_event_broker_backfill,
config.notifications_enabled,
)
.await;
@@ -466,6 +478,7 @@ async fn main() -> anyhow::Result<()> {
let crm_service_link_manager = crm_service.clone();
let connection_gateway_client_link_manager = connection_gateway_client.clone();
let notification_ingress_service_link_manager = notification_ingress_service.clone();
+ let macro_event_broker_link_manager = macro_event_broker.clone();
// daily link_manager operations for user contacts and inbox subscriptions
tokio::spawn(async move {
email_service::pubsub::link_manager::worker::run_worker(
@@ -478,6 +491,7 @@ async fn main() -> anyhow::Result<()> {
crm_service_link_manager,
connection_gateway_client_link_manager,
notification_ingress_service_link_manager,
+ macro_event_broker_link_manager,
)
.await;
});
@@ -488,6 +502,7 @@ async fn main() -> anyhow::Result<()> {
let redis_client_scheduled = redis_client.clone();
let s3_client_scheduled = s3_client.clone();
let attachment_bucket_scheduled = config.attachment_bucket.to_string();
+ let macro_event_broker_scheduled = macro_event_broker.clone();
// send scheduled emails
tokio::spawn(async move {
email_service::pubsub::scheduled::worker::run_worker(
@@ -498,6 +513,7 @@ async fn main() -> anyhow::Result<()> {
redis_client_scheduled,
s3_client_scheduled,
attachment_bucket_scheduled,
+ macro_event_broker_scheduled,
)
.await;
});
diff --git a/services/email_service/src/config.rs b/services/email_service/src/config.rs
index 85527373a8..7f9360ab31 100644
--- a/services/email_service/src/config.rs
+++ b/services/email_service/src/config.rs
@@ -6,6 +6,7 @@ use secretsmanager_client::LocalOrRemoteSecret;
env_vars! {
pub struct EmailServiceCloudfrontSignerPrivateKey;
+ pub struct KafkaBrokers;
pub struct MacroDbUrl;
pub struct RedisUri;
pub struct GmailGcpQueue;
@@ -32,6 +33,9 @@ pub struct Config {
/// The Redis URI for the Redis this application should use.
pub redis_uri: RedisUri,
+ /// Comma-separated Kafka bootstrap servers for the macro event broker.
+ pub kafka_brokers: KafkaBrokers,
+
/// The GCP queue name that has the subscription that hits our webhook endpoint
pub gmail_gcp_queue: GmailGcpQueue,
diff --git a/services/email_service/src/main.rs b/services/email_service/src/main.rs
index 9e430a003b..d92228153e 100644
--- a/services/email_service/src/main.rs
+++ b/services/email_service/src/main.rs
@@ -15,6 +15,7 @@ use frecency::{domain::services::FrecencyQueryServiceImpl, outbound::postgres::F
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
use macro_entrypoint::MacroEntrypoint;
use macro_env::Environment;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use macro_service_urls::{AuthServiceUrl, DocumentStorageServiceUrl, StaticFileServiceUrl};
use sqlx::postgres::PgPoolOptions;
use static_file_service_client::StaticFileServiceClient;
@@ -133,16 +134,23 @@ async fn main() -> anyhow::Result<()> {
crm::outbound::companies_repo::CompaniesRepositoryImpl::new(db.clone()),
crm::outbound::no_op_resolver::NoOpCompanyMetadataResolver,
);
- let email_service = EmailRouterState::new(EmailServiceImpl::new(
- EmailPgRepo::new(db.clone()),
- FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
- (*sqs_client).clone(),
- crm_service,
- entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
- entity_access_management::outbound::PgRepository::new(db.clone()),
- ),
- config.sent_undo_delay_secs,
- ));
+ let macro_event_broker = MacroEventBrokerService::new(
+ KafkaEventPublisher::new(config.kafka_brokers.as_ref())
+ .context("failed to create kafka event publisher")?,
+ );
+ let email_service = EmailRouterState::new(
+ EmailServiceImpl::new(
+ EmailPgRepo::new(db.clone()),
+ FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
+ (*sqs_client).clone(),
+ crm_service,
+ entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
+ entity_access_management::outbound::PgRepository::new(db.clone()),
+ ),
+ config.sent_undo_delay_secs,
+ )
+ .with_macro_event_broker(macro_event_broker.clone()),
+ );
let entity_access_service = Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
db.clone(),
)));
@@ -178,6 +186,7 @@ async fn main() -> anyhow::Result<()> {
entity_access_service,
email_thread_state,
gmail_token_state,
+ macro_event_broker: Arc::new(macro_event_broker),
})
.await?;
Ok(())
diff --git a/services/email_service/src/pubsub/backfill/worker.rs b/services/email_service/src/pubsub/backfill/worker.rs
index d91f93a231..40a1e08b73 100644
--- a/services/email_service/src/pubsub/backfill/worker.rs
+++ b/services/email_service/src/pubsub/backfill/worker.rs
@@ -7,6 +7,7 @@ use contacts::domain::service::SqsContactsIngress;
use contacts::outbound::ingress::SqsContactsQueue;
use document_storage_service_client::DocumentStorageServiceClient;
use futures::StreamExt;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use static_file_service_client::StaticFileServiceClient;
use std::sync::Arc;
use system_properties::{PgSystemPropertiesRepository, SystemPropertiesServiceImpl};
@@ -27,6 +28,7 @@ pub async fn run_worker(
dss_client: DocumentStorageServiceClient,
system_properties_service: Arc<SystemPropertiesServiceImpl<PgSystemPropertiesRepository>>,
crm_service: CrmServiceType,
+ macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
notifications_enabled: bool,
) {
let ctx = PubSubContext {
@@ -43,6 +45,7 @@ pub async fn run_worker(
dss_client,
system_properties_service,
crm_service,
+ macro_event_broker,
notifications_enabled,
retry_worker: false,
};
diff --git a/services/email_service/src/pubsub/context.rs b/services/email_service/src/pubsub/context.rs
index 9562b5605c..9540fbb53b 100644
--- a/services/email_service/src/pubsub/context.rs
+++ b/services/email_service/src/pubsub/context.rs
@@ -11,6 +11,7 @@ use crm::outbound::companies_repo::CompaniesRepositoryImpl;
use crm::outbound::unfurl_resolver::UnfurlCompanyMetadataResolver;
use document_storage_service_client::DocumentStorageServiceClient;
use gmail_client::GmailClient;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use notification::domain::service::SqsNotificationIngress;
use notification::outbound::queue::SqsQueue;
use sqlx::PgPool;
@@ -69,6 +70,7 @@ pub struct PubSubContext {
pub dss_client: DocumentStorageServiceClient,
pub system_properties_service: Arc<SystemPropertiesServiceImpl<PgSystemPropertiesRepository>>,
pub crm_service: CrmServiceType,
+ pub macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
pub notifications_enabled: bool,
pub retry_worker: bool,
}
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
index 56b5c5afc1..8392643427 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/delete_message.rs
@@ -1,7 +1,9 @@
use crate::pubsub::context::PubSubContext;
use crate::pubsub::util::{
cg_refresh_email, complete_transaction_with_processing_error, enqueue_depopulate_crm_contacts,
+ publish_email_event,
};
+use email::domain::events::{EmailMacroEvent, MessageDeletedMetadata};
use models_email::api::refresh::RefreshEmailEvent;
use models_email::email::service::link;
use models_email::gmail::inbox_sync::DeleteMessagePayload;
@@ -114,6 +116,18 @@ pub async fn delete_message(
complete_transaction_with_processing_error(tx, result).await?;
+ publish_email_event(
+ &ctx.macro_event_broker,
+ &EmailMacroEvent::message_deleted(MessageDeletedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ message_id: message.db_id,
+ provider_message_id: payload.provider_message_id.clone(),
+ thread_id: message.thread_db_id,
+ }),
+ )
+ .await;
+
// tell FE to refresh user's inbox
cg_refresh_email(
&ctx.connection_gateway_client,
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs b/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
index 548d82ec14..4a1e0dab15 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
@@ -2,7 +2,13 @@ use crate::pubsub::context::PubSubContext;
use crate::pubsub::inbox_sync::operations::shared::notify_search;
use crate::pubsub::inbox_sync::process;
use crate::pubsub::inbox_sync::process::check_gmail_rate_limit_inbox_sync;
-use crate::pubsub::util::{cg_refresh_email, complete_transaction_with_processing_error};
+use crate::pubsub::util::{
+ cg_refresh_email, complete_transaction_with_processing_error, publish_email_event,
+};
+use email::domain::events::{
+ EmailEventOrigin, EmailMacroEvent, LabelRef, ThreadArchivedMetadata,
+ ThreadLabelsUpdatedMetadata, ThreadReadMetadata, ThreadStarredMetadata, ThreadTrashedMetadata,
+};
use email_db_client::labels::delete::delete_db_message_labels;
use email_db_client::labels::insert;
use email_db_client::threads::update::update_thread_metadata;
@@ -153,6 +159,19 @@ pub async fn update_labels(
}
if has_label_changes {
+ // Publish semantic macro.email events before the fallible search
+ // notify: a notify failure retries the whole op, and on retry the
+ // label diff is empty so the events would be lost.
+ publish_label_diff_events(
+ ctx,
+ link,
+ db_message.thread_db_id,
+ &labels_to_add,
+ &labels_to_delete,
+ &db_message_labels,
+ )
+ .await;
+
let is_spam_or_trash = gmail_message_labels.iter().any(|label| {
label == service::label::system_labels::SPAM
|| label == service::label::system_labels::TRASH
@@ -172,6 +191,161 @@ pub async fn update_labels(
Ok(())
}
+/// System labels that never appear in a `thread_labels_updated` event:
+/// INBOX/TRASH/UNREAD/STARRED map to dedicated thread-state events, and
+/// SPAM/IMPORTANT/SENT/DRAFT changes are not published at all.
+const NON_USER_LABELS: [&str; 8] = [
+ service::label::system_labels::INBOX,
+ service::label::system_labels::SPAM,
+ service::label::system_labels::TRASH,
+ service::label::system_labels::UNREAD,
+ service::label::system_labels::STARRED,
+ service::label::system_labels::IMPORTANT,
+ service::label::system_labels::SENT,
+ service::label::system_labels::DRAFT,
+];
+
+/// Publish semantic macro.email events for a provider-side label diff, all
+/// with `origin = provider_sync`. Changes made through Macro update the DB
+/// optimistically, so by the time they echo back through Gmail history sync
+/// there is no diff and nothing is published here — user-initiated changes
+/// are published from their handlers, with `origin = user_action`.
+///
+/// The diff is per message, so a thread-wide provider change (e.g. Gmail
+/// marking a whole thread read) produces one event per affected message.
+/// For the read/archived states the payload carries the *thread row* state
+/// (recomputed by `update_thread_metadata` inside the label ops), so the
+/// repeated events are accurate and idempotent rather than message-inferred.
+async fn publish_label_diff_events(
+ ctx: &PubSubContext,
+ link: &link::Link,
+ thread_db_id: Uuid,
+ labels_to_add: &[String],
+ labels_to_delete: &[String],
+ db_message_labels: &[models_email::db::label::Label],
+) {
+ let added = |label: &str| labels_to_add.iter().any(|l| l == label);
+ let removed = |label: &str| labels_to_delete.iter().any(|l| l == label);
+
+ let needs_thread_state = added(service::label::system_labels::INBOX)
+ || removed(service::label::system_labels::INBOX)
+ || added(service::label::system_labels::UNREAD)
+ || removed(service::label::system_labels::UNREAD);
+ let thread_row = if needs_thread_state {
+ email_db_client::threads::get::get_thread_by_id_and_link_id(&ctx.db, thread_db_id, link.id)
+ .await
+ .inspect_err(
+ |e| tracing::warn!(error=?e, thread_id=%thread_db_id, "failed to fetch thread row for macro.email event state"),
+ )
+ .ok()
+ .flatten()
+ } else {
+ None
+ };
+
+ let mut events: Vec<EmailMacroEvent> = Vec::new();
+
+ if added(service::label::system_labels::INBOX) || removed(service::label::system_labels::INBOX)
+ {
+ events.push(EmailMacroEvent::thread_archived(ThreadArchivedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ thread_id: thread_db_id,
+ archived: thread_row
+ .as_ref()
+ .map(|t| !t.inbox_visible)
+ .unwrap_or_else(|| removed(service::label::system_labels::INBOX)),
+ origin: EmailEventOrigin::ProviderSync,
+ }));
+ }
+
+ if added(service::label::system_labels::TRASH) || removed(service::label::system_labels::TRASH)
+ {
+ events.push(EmailMacroEvent::thread_trashed(ThreadTrashedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ thread_id: thread_db_id,
+ trashed: added(service::label::system_labels::TRASH),
+ origin: EmailEventOrigin::ProviderSync,
+ }));
+ }
+
+ if added(service::label::system_labels::UNREAD)
+ || removed(service::label::system_labels::UNREAD)
+ {
+ events.push(EmailMacroEvent::thread_read(ThreadReadMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ thread_id: thread_db_id,
+ is_read: thread_row
+ .as_ref()
+ .map(|t| t.is_read)
+ .unwrap_or_else(|| removed(service::label::system_labels::UNREAD)),
+ origin: EmailEventOrigin::ProviderSync,
+ }));
+ }
+
+ if added(service::label::system_labels::STARRED)
+ || removed(service::label::system_labels::STARRED)
+ {
+ events.push(EmailMacroEvent::thread_starred(ThreadStarredMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ thread_id: thread_db_id,
+ starred: added(service::label::system_labels::STARRED),
+ origin: EmailEventOrigin::ProviderSync,
+ }));
+ }
+
+ // Gmail category tabs (CATEGORY_PERSONAL/SOCIAL/...) are provider system
+ // labels, not user labels.
+ let is_user_label = |l: &str| !NON_USER_LABELS.contains(&l) && !l.starts_with("CATEGORY_");
+
+ let added_user: Vec<LabelRef> = labels_to_add
+ .iter()
+ .filter(|l| is_user_label(l.as_str()))
+ .map(|l| LabelRef {
+ label_id: None,
+ provider_label_id: l.clone(),
+ name: None,
+ })
+ .collect();
+ let removed_user: Vec<LabelRef> = db_message_labels
+ .iter()
+ .filter(|l| {
+ labels_to_delete.contains(&l.provider_label_id)
+ && is_user_label(l.provider_label_id.as_str())
+ })
+ .map(|l| LabelRef {
+ label_id: Some(l.id),
+ provider_label_id: l.provider_label_id.clone(),
+ name: Some(l.name.clone()),
+ })
+ .collect();
+
+ if !added_user.is_empty() || !removed_user.is_empty() {
+ events.push(EmailMacroEvent::thread_labels_updated(
+ ThreadLabelsUpdatedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ thread_id: thread_db_id,
+ added: added_user,
+ removed: removed_user,
+ origin: EmailEventOrigin::ProviderSync,
+ },
+ ));
+ }
+
+ for event in &events {
+ publish_email_event(&ctx.macro_event_broker, event).await;
+ }
+}
+
#[tracing::instrument(skip(db, link))]
pub async fn add_message_labels(
db: &PgPool,
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
index 2292f26b54..632765ffcd 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
@@ -3,13 +3,16 @@ use crate::pubsub::context::PubSubContext;
use crate::pubsub::inbox_sync::operations::shared::notify_search;
use crate::pubsub::inbox_sync::process;
use crate::pubsub::inbox_sync::process::check_gmail_rate_limit_inbox_sync;
-use crate::pubsub::util::cg_refresh_email;
use crate::pubsub::util::{
CrmContactRecipient, build_notification_recipients, enqueue_populate_crm_contacts,
};
+use crate::pubsub::util::{cg_refresh_email, publish_email_event};
use crate::util::process_pre_insert::{process_message_pre_insert, process_threads_pre_insert};
use crate::util::upload_attachment::{UploadAttachmentContext, upload_attachment};
use contacts::domain::ports::ContactsIngress;
+use email::domain::events::{
+ EmailEventOrigin, EmailMacroEvent, MessageReceivedMetadata, MessageSentMetadata,
+};
use email::domain::models::{PreviewCursorQuery, PreviewView, PreviewViewStandardLabel};
use email::domain::ports::EmailRepo;
use email::outbound::EmailPgRepo;
@@ -144,6 +147,16 @@ pub async fn upsert_message(
.collect()
};
+ // Snapshot header-level fields for the macro.email event before
+ // `message` is moved into the insert path. Bodies, snippets, and bcc
+ // addresses are never published.
+ let event_subject = message.subject.clone();
+ let event_from = message.from.clone();
+ let event_to_emails: Vec<String> = message.to.iter().map(|c| c.email.clone()).collect();
+ let event_cc_emails: Vec<String> = message.cc.iter().map(|c| c.email.clone()).collect();
+ let event_sent_at = message.sent_at.or(message.internal_date_ts);
+ let event_received_at = message.internal_date_ts;
+
// determine if message's thread already exists in the database
let thread_provider_to_db_map = threads::get::get_threads_by_link_id_and_provider_ids(
&ctx.db,
@@ -173,6 +186,8 @@ pub async fn upsert_message(
})
})?;
+ let is_new_thread = !thread_provider_to_db_map.contains_key(&provider_thread_id);
+
// if the message's thread doesn't exist in the database, we need to fetch and insert the whole thread.
// if it does exist in the database, we just need to insert the already fetched message.
if let Some(thread_db_id) = thread_provider_to_db_map.get(&provider_thread_id) {
@@ -215,6 +230,51 @@ pub async fn upsert_message(
})
})?;
+ // Publish to the macro.email topic immediately after the committed
+ // insert, BEFORE the fallible side-effect steps below: if any of them
+ // fails, the SQS retry finds the message already existing and would
+ // never emit. Drafts are skipped — a draft synced from the provider is
+ // not a received or sent message.
+ if !message_already_exists && !is_draft {
+ let event = if is_sent {
+ // Sent from another client and first observed via sync; sends
+ // performed through Macro are published by the scheduled-send
+ // worker and already exist here.
+ EmailMacroEvent::message_sent(MessageSentMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ message_id: message_db_id,
+ provider_message_id: payload.provider_message_id.clone(),
+ thread_id: thread_db_id,
+ provider_thread_id: provider_thread_id.clone(),
+ subject: event_subject,
+ to_emails: event_to_emails,
+ cc_emails: event_cc_emails,
+ origin: EmailEventOrigin::ProviderSync,
+ sent_at: event_sent_at.unwrap_or_else(chrono::Utc::now),
+ })
+ } else {
+ EmailMacroEvent::message_received(MessageReceivedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ message_id: message_db_id,
+ provider_message_id: payload.provider_message_id.clone(),
+ thread_id: thread_db_id,
+ provider_thread_id: provider_thread_id.clone(),
+ is_new_thread,
+ subject: event_subject,
+ from_email: event_from.as_ref().map(|c| c.email.clone()),
+ from_name: event_from.as_ref().and_then(|c| c.name.clone()),
+ to_emails: event_to_emails,
+ attachment_count: message_attachment_count as u32,
+ is_spam_or_trash,
+ received_at: event_received_at,
+ })
+ };
+ publish_email_event(&ctx.macro_event_broker, &event).await;
+ }
+
handle_attachment_upload(
ctx,
&gmail_access_token,
diff --git a/services/email_service/src/pubsub/inbox_sync/worker.rs b/services/email_service/src/pubsub/inbox_sync/worker.rs
index f5b1564610..22bd611d3d 100644
--- a/services/email_service/src/pubsub/inbox_sync/worker.rs
+++ b/services/email_service/src/pubsub/inbox_sync/worker.rs
@@ -7,6 +7,7 @@ use contacts::domain::service::SqsContactsIngress;
use contacts::outbound::ingress::SqsContactsQueue;
use document_storage_service_client::DocumentStorageServiceClient;
use futures::StreamExt;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use static_file_service_client::StaticFileServiceClient;
use std::sync::Arc;
use system_properties::{PgSystemPropertiesRepository, SystemPropertiesServiceImpl};
@@ -27,6 +28,7 @@ pub async fn run_worker(
dss_client: DocumentStorageServiceClient,
system_properties_service: Arc<SystemPropertiesServiceImpl<PgSystemPropertiesRepository>>,
crm_service: CrmServiceType,
+ macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
notifications_enabled: bool,
retry_worker: bool,
) {
@@ -44,6 +46,7 @@ pub async fn run_worker(
dss_client,
system_properties_service,
crm_service,
+ macro_event_broker,
notifications_enabled,
retry_worker,
};
diff --git a/services/email_service/src/pubsub/link_manager/context.rs b/services/email_service/src/pubsub/link_manager/context.rs
index ac4658c3d3..015205b3ec 100644
--- a/services/email_service/src/pubsub/link_manager/context.rs
+++ b/services/email_service/src/pubsub/link_manager/context.rs
@@ -3,6 +3,7 @@ use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
use connection_gateway_client::client::ConnectionGatewayClient;
use gmail_client::GmailClient;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use sqlx::PgPool;
use sqs_client::SQS;
use std::sync::Arc;
@@ -18,4 +19,5 @@ pub struct LinkManagerContext {
pub crm_service: CrmServiceType,
pub connection_gateway_client: ConnectionGatewayClient,
pub notification_ingress_service: Arc<NotificationIngressType>,
+ pub macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
}
diff --git a/services/email_service/src/pubsub/link_manager/process.rs b/services/email_service/src/pubsub/link_manager/process.rs
index e01cc09fb7..2995db88f9 100644
--- a/services/email_service/src/pubsub/link_manager/process.rs
+++ b/services/email_service/src/pubsub/link_manager/process.rs
@@ -1,5 +1,5 @@
use crate::pubsub::link_manager::context::LinkManagerContext;
-use crate::pubsub::util::{build_notification_recipients, cg_refresh_email};
+use crate::pubsub::util::{build_notification_recipients, cg_refresh_email, publish_email_event};
use crate::util::gmail::auth::{
fetch_gmail_access_token_from_link, fetch_token_or_mark_reauth,
fetch_token_or_mark_reauth_no_cache, is_forbidden_error, is_reauth_required_error,
@@ -7,6 +7,9 @@ use crate::util::gmail::auth::{
use crate::util::sync_contacts::sync_contacts;
use anyhow::{Context, anyhow};
use crm::domain::service::CrmService;
+use email::domain::events::{
+ EmailMacroEvent, LinkDisconnectReason, LinkDisconnectedMetadata, LinkReauthRequiredMetadata,
+};
use model_entity::EntityType;
use model_notifications::InboxReauthRequiredMetadata;
use models_email::api::refresh::RefreshEmailEvent;
@@ -234,6 +237,19 @@ async fn handle_notify_reauth_required(
.await
.map_err(|e| anyhow!("failed to send reauth notification: {e}"))?;
+ // This message is enqueued only on the false->true needs_reauth
+ // transition, so the event stays edge-triggered.
+ publish_email_event(
+ &ctx.macro_event_broker,
+ &EmailMacroEvent::link_reauth_required(LinkReauthRequiredMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ email_address: link.email_address.0.as_ref().to_string(),
+ observed_at: link.last_sync_error_at.unwrap_or_else(chrono::Utc::now),
+ }),
+ )
+ .await;
+
Ok(())
}
@@ -388,6 +404,23 @@ async fn handle_delete(
)
.await;
+ publish_email_event(
+ &ctx.macro_event_broker,
+ &EmailMacroEvent::link_disconnected(LinkDisconnectedMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ email_address: link.email_address.0.as_ref().to_string(),
+ reason: match deletion_reason {
+ DeletionReason::Unused => LinkDisconnectReason::Unused,
+ DeletionReason::Inactive => LinkDisconnectReason::Inactive,
+ DeletionReason::ManuallyDisabled => LinkDisconnectReason::ManuallyDisabled,
+ DeletionReason::UserDeleted => LinkDisconnectReason::UserDeleted,
+ DeletionReason::AccessRevoked => LinkDisconnectReason::AccessRevoked,
+ },
+ }),
+ )
+ .await;
+
// Mark the link as deleted in history table for tracking (best-effort)
if let Err(e) = email_db_client::links_history::update::set_deleted_at(
&ctx.db,
diff --git a/services/email_service/src/pubsub/link_manager/worker.rs b/services/email_service/src/pubsub/link_manager/worker.rs
index 1cd92ee572..85fe694943 100644
--- a/services/email_service/src/pubsub/link_manager/worker.rs
+++ b/services/email_service/src/pubsub/link_manager/worker.rs
@@ -5,6 +5,7 @@ use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
use connection_gateway_client::client::ConnectionGatewayClient;
use futures::StreamExt;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use sqlx::PgPool;
use sqs_client::SQS;
use std::sync::Arc;
@@ -21,6 +22,7 @@ pub async fn run_worker(
crm_service: CrmServiceType,
connection_gateway_client: ConnectionGatewayClient,
notification_ingress_service: Arc<NotificationIngressType>,
+ macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
) {
let ctx = LinkManagerContext {
db,
@@ -32,6 +34,7 @@ pub async fn run_worker(
crm_service,
connection_gateway_client,
notification_ingress_service,
+ macro_event_broker,
};
loop {
let worker_result = tokio::spawn({
diff --git a/services/email_service/src/pubsub/mod.rs b/services/email_service/src/pubsub/mod.rs
index 0d1fb58e16..48aab6124c 100644
--- a/services/email_service/src/pubsub/mod.rs
+++ b/services/email_service/src/pubsub/mod.rs
@@ -11,3 +11,6 @@ pub mod scheduled;
pub mod sfs_deleter;
pub mod sfs_uploader;
pub(crate) mod util;
+/// Log-and-drop publisher for `macro.email` events, re-exported for the
+/// HTTP binary's handlers.
+pub use util::publish_email_event;
diff --git a/services/email_service/src/pubsub/scheduled/context.rs b/services/email_service/src/pubsub/scheduled/context.rs
index 0f0ce1febf..48fb821478 100644
--- a/services/email_service/src/pubsub/scheduled/context.rs
+++ b/services/email_service/src/pubsub/scheduled/context.rs
@@ -1,6 +1,7 @@
use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
use gmail_client::GmailClient;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use sqlx::PgPool;
#[derive(Clone)]
@@ -12,4 +13,5 @@ pub struct ScheduledContext {
pub redis_client: RedisClient,
pub s3_client: s3_client::S3,
pub attachment_bucket: String,
+ pub macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
}
diff --git a/services/email_service/src/pubsub/scheduled/process.rs b/services/email_service/src/pubsub/scheduled/process.rs
index 881e0ab540..3766444967 100644
--- a/services/email_service/src/pubsub/scheduled/process.rs
+++ b/services/email_service/src/pubsub/scheduled/process.rs
@@ -1,4 +1,5 @@
use crate::pubsub::scheduled::context::ScheduledContext;
+use crate::pubsub::util::publish_email_event;
use crate::util::gmail::auth::fetch_gmail_access_token_from_link;
use crate::util::gmail::send::{
cleanup_draft_attachments, fetch_and_attach_draft_attachments,
@@ -6,6 +7,7 @@ use crate::util::gmail::send::{
};
use anyhow::Context;
use chrono::Utc;
+use email::domain::events::{EmailEventOrigin, EmailMacroEvent, MessageSentMetadata};
use email_db_client::messages::scheduled::get::get_and_start_processing_scheduled_message;
use models_email::service::message::MessageToSend;
use models_email::service::pubsub::ScheduledPubsubMessage;
@@ -180,6 +182,50 @@ async fn process_scheduled_message_inner(
.await
.context("Failed to commit transaction")?;
+ // Gmail accepted the send and the DB updates are committed:
+ // publish the message_sent event resolving the earlier
+ // message_send_queued. Provider ids were set on
+ // `message_to_send` by the Gmail send call above.
+ // Actor is not tracked on scheduled sends; owner is on `link`.
+ if let (Some(message_db_id), Some(thread_db_id)) =
+ (message_to_send.db_id, message_to_send.thread_db_id)
+ {
+ publish_email_event(
+ &ctx.macro_event_broker,
+ &EmailMacroEvent::message_sent(MessageSentMetadata {
+ link_id: link.id,
+ owner: link.macro_id.clone(),
+ actor: None,
+ message_id: message_db_id,
+ thread_id: thread_db_id,
+ provider_message_id: message_to_send
+ .provider_id
+ .clone()
+ .unwrap_or_default(),
+ provider_thread_id: message_to_send
+ .provider_thread_id
+ .clone()
+ .unwrap_or_default(),
+ subject: Some(message_to_send.subject.clone()),
+ to_emails: message_to_send
+ .to
+ .iter()
+ .flatten()
+ .map(|c| c.email.clone())
+ .collect(),
+ cc_emails: message_to_send
+ .cc
+ .iter()
+ .flatten()
+ .map(|c| c.email.clone())
+ .collect(),
+ origin: EmailEventOrigin::UserAction,
+ sent_at: Utc::now(),
+ }),
+ )
+ .await;
+ }
+
// Cleanup attachments in the background after successful send
if let (Some(draft_id), Some(attachments)) = (message_to_send.db_id, db_attachments) {
let db = ctx.db.clone();
diff --git a/services/email_service/src/pubsub/scheduled/worker.rs b/services/email_service/src/pubsub/scheduled/worker.rs
index 496fb972e0..51faca5d71 100644
--- a/services/email_service/src/pubsub/scheduled/worker.rs
+++ b/services/email_service/src/pubsub/scheduled/worker.rs
@@ -3,9 +3,11 @@ use crate::pubsub::scheduled::process;
use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
use futures::StreamExt;
+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};
use sqlx::PgPool;
/// method that ingests sqs messages and calls the process function for each
+#[expect(clippy::too_many_arguments, reason = "matches the other workers")]
pub async fn run_worker(
worker: sqs_worker::SQSWorker,
db: PgPool,
@@ -14,6 +16,7 @@ pub async fn run_worker(
redis_client: RedisClient,
s3_client: s3_client::S3,
attachment_bucket: String,
+ macro_event_broker: MacroEventBrokerService<KafkaEventPublisher>,
) {
let ctx = ScheduledContext {
db,
@@ -23,6 +26,7 @@ pub async fn run_worker(
redis_client,
s3_client,
attachment_bucket,
+ macro_event_broker,
};
loop {
let worker_result = tokio::spawn({
diff --git a/services/email_service/src/pubsub/util.rs b/services/email_service/src/pubsub/util.rs
index f70d581e54..04bb6479aa 100644
--- a/services/email_service/src/pubsub/util.rs
+++ b/services/email_service/src/pubsub/util.rs
@@ -3,6 +3,8 @@ use crate::util::redis::RedisClient;
use crate::util::redis::rate_limit::RateLimitArgs;
use chrono::{DateTime, Utc};
use connection_gateway_client::client::ConnectionGatewayClient;
+use email::domain::events::EmailMacroEvent;
+use macro_event_broker::MacroEventBroker;
use macro_user_id::user_id::MacroUserIdStr;
/// shared utils across different pubsub workers
use models_email::api::refresh::RefreshEmailEvent;
@@ -124,6 +126,15 @@ pub async fn complete_transaction_with_processing_error<T>(
}
}
+/// Publish an email event to the `macro.email` Kafka topic, logging and
+/// dropping failures — event emission must never fail the email operation.
+pub async fn publish_email_event<B: MacroEventBroker>(broker: &B, event: &EmailMacroEvent) {
+ let _ = broker
+ .send_event(event)
+ .await
+ .inspect_err(|e| tracing::error!(error=?e, "failed to publish email macro event"));
+}
+
/// Send message to connection gateway to trigger email refresh if user is active on FE
#[tracing::instrument(skip(client), level = "debug")]
pub async fn cg_refresh_email(
diff --git a/services/mcp_service/src/context.rs b/services/mcp_service/src/context.rs
index a72d798f8c..7210e2511f 100644
--- a/services/mcp_service/src/context.rs
+++ b/services/mcp_service/src/context.rs
@@ -235,7 +235,7 @@ async fn build_tool_context(
entity_access_management::outbound::PgRepository::new(db.clone()),
),
foreign_entity_service: ForeignEntityServiceImpl::new(PgForeignEntityRepo::new(db.clone())),
- macro_event_broker,
+ macro_event_broker: macro_event_broker.clone(),
};
let lexical_client_for_tools = (*lexical_client).clone();
let document_tool_context = DocumentToolContext::new(
@@ -251,16 +251,19 @@ async fn build_tool_context(
ai_tools::build_properties_tool_context(properties_service, entity_access_service.clone());
let email_tool_context = email::inbound::toolset::EmailToolContext::new(
- Arc::new(EmailServiceImpl::new(
- EmailPgRepo::new(db.clone()),
- FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
- sqs_client,
- crm_service.clone(),
- entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
- entity_access_management::outbound::PgRepository::new(db.clone()),
- ),
- 0,
- )),
+ Arc::new(
+ EmailServiceImpl::new(
+ EmailPgRepo::new(db.clone()),
+ FrecencyQueryServiceImpl::new(FrecencyPgStorage::new(db.clone())),
+ sqs_client,
+ crm_service.clone(),
+ entity_access_management::domain::service::EntityAccessManagementServiceImpl::new(
+ entity_access_management::outbound::PgRepository::new(db.clone()),
+ ),
+ 0,
+ )
+ .with_macro_event_broker(macro_event_broker.clone()),
+ ),
Arc::new(email::domain::ports::NoOpGmailTokenProvider),
Arc::new(EntityAccessServiceImpl::new(PgAccessRepository::new(
db.clone(),