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.

fix(activity): record email sender (#5683)

teo8501f5443260
diff --git a/.sqlx/query-27cffe57cded3d1b63df02e8dca8b2158fe2072af6b7baacd30b3f0095cb7e0c.json b/.sqlx/query-3141697998b06e7e2f7ef6c8d38260fd5aedb677cf8c2c41a9cfd1eeb5d0f41e.json
similarity index 70%
rename from .sqlx/query-27cffe57cded3d1b63df02e8dca8b2158fe2072af6b7baacd30b3f0095cb7e0c.json
rename to .sqlx/query-3141697998b06e7e2f7ef6c8d38260fd5aedb677cf8c2c41a9cfd1eeb5d0f41e.json
index 53bc4e1cd9..aa54ed680e 100644
--- a/.sqlx/query-27cffe57cded3d1b63df02e8dca8b2158fe2072af6b7baacd30b3f0095cb7e0c.json
+++ b/.sqlx/query-3141697998b06e7e2f7ef6c8d38260fd5aedb677cf8c2c41a9cfd1eeb5d0f41e.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT link_id, message_id, send_time, sent, processing\n FROM email_scheduled_messages\n WHERE message_id = ANY($1) AND sent = false\n ",
+ "query": "\n SELECT link_id, message_id, send_time, sent, processing, actor_id\n FROM email_scheduled_messages\n WHERE message_id = ANY($1) AND sent = false\n ",
"describe": {
"columns": [
{
@@ -27,6 +27,11 @@
"ordinal": 4,
"name": "processing",
"type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "actor_id",
+ "type_info": "Text"
}
],
"parameters": {
@@ -39,8 +44,9 @@
false,
false,
false,
- false
+ false,
+ true
]
},
- "hash": "27cffe57cded3d1b63df02e8dca8b2158fe2072af6b7baacd30b3f0095cb7e0c"
+ "hash": "3141697998b06e7e2f7ef6c8d38260fd5aedb677cf8c2c41a9cfd1eeb5d0f41e"
}
diff --git a/.sqlx/query-58b8b566779518776b13cabdb3498ac55b426b0493e769ea607618db7e246a71.json b/.sqlx/query-58b8b566779518776b13cabdb3498ac55b426b0493e769ea607618db7e246a71.json
deleted file mode 100644
index 2a84c80297..0000000000
--- a/.sqlx/query-58b8b566779518776b13cabdb3498ac55b426b0493e769ea607618db7e246a71.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO email_scheduled_messages (\n link_id, message_id, send_time, sent,\n created_at, updated_at\n )\n VALUES ($1, $2, $3, $4, NOW(), NOW())\n ON CONFLICT (link_id, message_id) DO UPDATE SET\n send_time = EXCLUDED.send_time,\n sent = EXCLUDED.sent,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Timestamptz",
- "Bool"
- ]
- },
- "nullable": []
- },
- "hash": "58b8b566779518776b13cabdb3498ac55b426b0493e769ea607618db7e246a71"
-}
diff --git a/.sqlx/query-9d88208a8e7ea73f315567eb1f46794e5f09a5a25644028a02e226e3119e229a.json b/.sqlx/query-9d88208a8e7ea73f315567eb1f46794e5f09a5a25644028a02e226e3119e229a.json
new file mode 100644
index 0000000000..7a7043c251
--- /dev/null
+++ b/.sqlx/query-9d88208a8e7ea73f315567eb1f46794e5f09a5a25644028a02e226e3119e229a.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_scheduled_messages (\n link_id, message_id, send_time, sent, actor_id,\n created_at, updated_at\n )\n VALUES ($1, $2, $3, $4, $5, NOW(), NOW())\n ON CONFLICT (link_id, message_id) DO UPDATE SET\n send_time = EXCLUDED.send_time,\n sent = EXCLUDED.sent,\n actor_id = EXCLUDED.actor_id,\n updated_at = NOW()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Bool",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9d88208a8e7ea73f315567eb1f46794e5f09a5a25644028a02e226e3119e229a"
+}
diff --git a/.sqlx/query-a939cd7abe81e0f4099bbbdcafd34ec0ddd6f1b08299387468847012ba28cac4.json b/.sqlx/query-a939cd7abe81e0f4099bbbdcafd34ec0ddd6f1b08299387468847012ba28cac4.json
new file mode 100644
index 0000000000..328813f714
--- /dev/null
+++ b/.sqlx/query-a939cd7abe81e0f4099bbbdcafd34ec0ddd6f1b08299387468847012ba28cac4.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_scheduled_messages (\n link_id, message_id, send_time, sent, actor_id,\n created_at, updated_at\n )\n VALUES ($1, $2, $3, $4, $5, NOW(), NOW())\n ON CONFLICT (link_id, message_id) DO UPDATE SET\n send_time = EXCLUDED.send_time,\n sent = EXCLUDED.sent,\n actor_id = EXCLUDED.actor_id,\n updated_at = NOW()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Timestamptz",
+ "Bool",
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "a939cd7abe81e0f4099bbbdcafd34ec0ddd6f1b08299387468847012ba28cac4"
+}
diff --git a/.sqlx/query-e2cadbb29d007b092339274024c3638798761653ca72db90aa2ce7417976ef61.json b/.sqlx/query-bc0bb71825307624ae241d13ee3d1cae2a9e610923e7cd7977b1a7357eebd883.json
similarity index 55%
rename from .sqlx/query-e2cadbb29d007b092339274024c3638798761653ca72db90aa2ce7417976ef61.json
rename to .sqlx/query-bc0bb71825307624ae241d13ee3d1cae2a9e610923e7cd7977b1a7357eebd883.json
index a24ebef4da..87a29cac69 100644
--- a/.sqlx/query-e2cadbb29d007b092339274024c3638798761653ca72db90aa2ce7417976ef61.json
+++ b/.sqlx/query-bc0bb71825307624ae241d13ee3d1cae2a9e610923e7cd7977b1a7357eebd883.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n WITH old AS (\n SELECT link_id, message_id, send_time, sent, processing\n FROM email_scheduled_messages\n WHERE link_id = $1 AND message_id = $2\n ), updated AS (\n UPDATE email_scheduled_messages\n SET processing = true, updated_at = NOW()\n WHERE link_id = $1 AND message_id = $2\n )\n SELECT link_id, message_id, send_time, sent, processing\n FROM old\n ",
+ "query": "\n WITH old AS (\n SELECT link_id, message_id, send_time, sent, processing, actor_id\n FROM email_scheduled_messages\n WHERE link_id = $1 AND message_id = $2\n ), updated AS (\n UPDATE email_scheduled_messages\n SET processing = true, updated_at = NOW()\n WHERE link_id = $1 AND message_id = $2\n )\n SELECT link_id, message_id, send_time, sent, processing, actor_id\n FROM old\n ",
"describe": {
"columns": [
{
@@ -27,6 +27,11 @@
"ordinal": 4,
"name": "processing",
"type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "actor_id",
+ "type_info": "Text"
}
],
"parameters": {
@@ -40,8 +45,9 @@
false,
false,
false,
- false
+ false,
+ true
]
},
- "hash": "e2cadbb29d007b092339274024c3638798761653ca72db90aa2ce7417976ef61"
+ "hash": "bc0bb71825307624ae241d13ee3d1cae2a9e610923e7cd7977b1a7357eebd883"
}
diff --git a/.sqlx/query-d61fd0eda66672d4ba0ed8d648c93fdc9b145bcea0d50bfb62acabba93d5818c.json b/.sqlx/query-d61fd0eda66672d4ba0ed8d648c93fdc9b145bcea0d50bfb62acabba93d5818c.json
deleted file mode 100644
index 56463591cc..0000000000
--- a/.sqlx/query-d61fd0eda66672d4ba0ed8d648c93fdc9b145bcea0d50bfb62acabba93d5818c.json
+++ /dev/null
@@ -1,17 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n INSERT INTO email_scheduled_messages (\n link_id, message_id, send_time, sent,\n created_at, updated_at\n )\n VALUES ($1, $2, $3, $4, NOW(), NOW())\n ON CONFLICT (link_id, message_id) DO UPDATE SET\n send_time = EXCLUDED.send_time,\n sent = EXCLUDED.sent,\n updated_at = NOW()\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Timestamptz",
- "Bool"
- ]
- },
- "nullable": []
- },
- "hash": "d61fd0eda66672d4ba0ed8d648c93fdc9b145bcea0d50bfb62acabba93d5818c"
-}
diff --git a/.sqlx/query-4dd84a2e543e14d4ed1655a0895eab7815948fcad38c5ce05925ebc4a5ebea69.json b/.sqlx/query-da630564e89c0cacb50e8e4b0ce1e41baf5d0476799f63a46b0b0ea3ee342991.json
similarity index 70%
rename from .sqlx/query-4dd84a2e543e14d4ed1655a0895eab7815948fcad38c5ce05925ebc4a5ebea69.json
rename to .sqlx/query-da630564e89c0cacb50e8e4b0ce1e41baf5d0476799f63a46b0b0ea3ee342991.json
index dfc668e9a2..31afc7f26b 100644
--- a/.sqlx/query-4dd84a2e543e14d4ed1655a0895eab7815948fcad38c5ce05925ebc4a5ebea69.json
+++ b/.sqlx/query-da630564e89c0cacb50e8e4b0ce1e41baf5d0476799f63a46b0b0ea3ee342991.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT link_id, message_id, send_time, sent, processing\n FROM email_scheduled_messages\n WHERE link_id = $1 AND message_id = $2\n ",
+ "query": "\n SELECT link_id, message_id, send_time, sent, processing, actor_id\n FROM email_scheduled_messages\n WHERE link_id = $1 AND message_id = $2\n ",
"describe": {
"columns": [
{
@@ -27,6 +27,11 @@
"ordinal": 4,
"name": "processing",
"type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "actor_id",
+ "type_info": "Text"
}
],
"parameters": {
@@ -40,8 +45,9 @@
false,
false,
false,
- false
+ false,
+ true
]
},
- "hash": "4dd84a2e543e14d4ed1655a0895eab7815948fcad38c5ce05925ebc4a5ebea69"
+ "hash": "da630564e89c0cacb50e8e4b0ce1e41baf5d0476799f63a46b0b0ea3ee342991"
}
diff --git a/.sqlx/query-5a45cbeeb6fa756b256688f6c4e05be4abae89c4eb3c5532a96b91f3d07e232f.json b/.sqlx/query-e049a110febcb8a76c2228e43a3fee8fcfbcb0a787875f10758336f97033c6cd.json
similarity index 70%
rename from .sqlx/query-5a45cbeeb6fa756b256688f6c4e05be4abae89c4eb3c5532a96b91f3d07e232f.json
rename to .sqlx/query-e049a110febcb8a76c2228e43a3fee8fcfbcb0a787875f10758336f97033c6cd.json
index e76379715c..330c761291 100644
--- a/.sqlx/query-5a45cbeeb6fa756b256688f6c4e05be4abae89c4eb3c5532a96b91f3d07e232f.json
+++ b/.sqlx/query-e049a110febcb8a76c2228e43a3fee8fcfbcb0a787875f10758336f97033c6cd.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT link_id, message_id, send_time, sent, processing\n FROM email_scheduled_messages\n WHERE message_id = $1 and sent = false\n ",
+ "query": "\n SELECT link_id, message_id, send_time, sent, processing, actor_id\n FROM email_scheduled_messages\n WHERE message_id = $1 and sent = false\n ",
"describe": {
"columns": [
{
@@ -27,6 +27,11 @@
"ordinal": 4,
"name": "processing",
"type_info": "Bool"
+ },
+ {
+ "ordinal": 5,
+ "name": "actor_id",
+ "type_info": "Text"
}
],
"parameters": {
@@ -39,8 +44,9 @@
false,
false,
false,
- false
+ false,
+ true
]
},
- "hash": "5a45cbeeb6fa756b256688f6c4e05be4abae89c4eb3c5532a96b91f3d07e232f"
+ "hash": "e049a110febcb8a76c2228e43a3fee8fcfbcb0a787875f10758336f97033c6cd"
}
diff --git a/crates/email/src/domain/models/draft.rs b/crates/email/src/domain/models/draft.rs
index b8af1583dd..42174375af 100644
--- a/crates/email/src/domain/models/draft.rs
+++ b/crates/email/src/domain/models/draft.rs
@@ -1,4 +1,5 @@
use chrono::{DateTime, Utc};
+use macro_user_id::user_id::MacroUserIdStr;
use serde_json::Value as JsonValue;
use uuid::Uuid;
@@ -39,6 +40,11 @@ pub struct CreateDraftInput {
/// (the user dismissed it), `Some(true)` to force-include, `None` to apply
/// the inbox's default policy. Only consulted on send (ignored for drafts).
pub include_signature: Option<bool>,
+ /// The authenticated user performing the send, set by the transport
+ /// layer (never from the request body). Distinct from the link owner on
+ /// delegated inboxes; attribution (events, activity) follows the actor.
+ /// Ignored for drafts.
+ pub actor: Option<MacroUserIdStr<'static>>,
}
/// A draft input with all IDs resolved, ready for database insertion.
@@ -73,6 +79,9 @@ pub struct ResolvedDraftInput {
pub headers_json: Option<JsonValue>,
/// Scheduled send time.
pub send_time: Option<DateTime<Utc>>,
+ /// The sending user's principal string, persisted with the scheduled
+ /// send so the eventual `message_sent` event can attribute the actor.
+ pub actor_id: Option<String>,
}
/// Simplified message info used for validation queries.
diff --git a/crates/email/src/domain/service/draft.rs b/crates/email/src/domain/service/draft.rs
index c304992474..f69637b096 100644
--- a/crates/email/src/domain/service/draft.rs
+++ b/crates/email/src/domain/service/draft.rs
@@ -100,6 +100,7 @@ where
body_macro: input.body_macro,
headers_json: input.headers_json,
send_time: input.send_time,
+ actor_id: input.actor.as_ref().map(|actor| actor.as_ref().to_owned()),
};
self.email_repo
diff --git a/crates/email/src/domain/service/send.rs b/crates/email/src/domain/service/send.rs
index 0daca42e1b..5d19639aa3 100644
--- a/crates/email/src/domain/service/send.rs
+++ b/crates/email/src/domain/service/send.rs
@@ -30,6 +30,7 @@ where
let delay_secs = self.sent_undo_delay_secs;
let send_time = chrono::Utc::now() + Duration::seconds(delay_secs as i64);
input.send_time = Some(send_time);
+ let actor = input.actor.clone();
let created = self
.prepare_and_insert_db_message(link, accessible_inboxes, input, false)
@@ -43,13 +44,13 @@ where
.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`.
+ // message_sent / message_send_cancelled event resolves it later,
+ // reading the actor back off the scheduled row.
self.publish_email_event(&EmailMacroEvent::message_send_queued(
MessageSendQueuedMetadata {
link_id: link.id,
owner: link.macro_id.clone(),
- actor: None,
+ actor,
message_id: created.db_id,
thread_id: created.thread_db_id,
scheduled_send_at: send_time,
diff --git a/crates/email/src/inbound/axum/api_types/draft.rs b/crates/email/src/inbound/axum/api_types/draft.rs
index f2dc1f592f..502de27eb6 100644
--- a/crates/email/src/inbound/axum/api_types/draft.rs
+++ b/crates/email/src/inbound/axum/api_types/draft.rs
@@ -160,6 +160,9 @@ impl CreateDraftRequest {
headers_json: draft.headers_json,
send_time: self.send_time,
include_signature: draft.include_signature,
+ // Drafts carry no actor; attribution happens when the draft is
+ // actually sent.
+ actor: None,
}
}
}
diff --git a/crates/email/src/inbound/axum/api_types/send.rs b/crates/email/src/inbound/axum/api_types/send.rs
index 0ddd786c4d..8ea4872cfa 100644
--- a/crates/email/src/inbound/axum/api_types/send.rs
+++ b/crates/email/src/inbound/axum/api_types/send.rs
@@ -57,6 +57,9 @@ impl SendMessageRequest {
headers_json: msg.headers_json,
send_time: None,
include_signature: msg.include_signature,
+ // The actor comes from the transport's auth context, never the
+ // request body; the handler fills it in.
+ actor: None,
}
}
}
diff --git a/crates/email/src/inbound/axum/send_router.rs b/crates/email/src/inbound/axum/send_router.rs
index 0c9153ece7..ca94dce447 100644
--- a/crates/email/src/inbound/axum/send_router.rs
+++ b/crates/email/src/inbound/axum/send_router.rs
@@ -1,6 +1,8 @@
use axum::{Json, Router, extract::State, http::StatusCode, response::IntoResponse, routing::post};
use axum_extra::extract::Cached;
-use macro_authorization::{MacroAuthorizationService, MacroAuthorizationState};
+use macro_authorization::{
+ MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState, UserOrInternal,
+};
use model_error_response::ErrorResponse;
use thiserror::Error;
@@ -88,16 +90,20 @@ impl From<EmailErr> for SendMessageError {
(status = 500, body = ErrorResponse),
)
)]
-#[tracing::instrument(err, skip(state, link, accessible_inboxes, body))]
+#[tracing::instrument(err, skip(state, link, accessible_inboxes, authorization, body))]
pub async fn send_message_handler<T: EmailService, Auth: MacroAuthorizationService>(
State(state): State<EmailRouterState<T>>,
Cached(EmailLinkExtractor(link, _)): Cached<EmailLinkExtractor<T, Auth>>,
Cached(MultiEmailLinkExtractor(accessible_inboxes, _)): Cached<
MultiEmailLinkExtractor<T, Auth>,
>,
+ Cached(authorization): Cached<MacroAuthorizationExtractor<Auth, UserOrInternal>>,
Json(body): Json<SendMessageRequest>,
) -> Result<impl IntoResponse, SendMessageError> {
- let input = body.into_domain();
+ let mut input = body.into_domain();
+ // Attribute the send to the authenticated caller, who is not necessarily
+ // the link owner (delegated inboxes).
+ input.actor = Some(authorization.authorization.user.macro_user_id.clone());
let created = state
.inner
.send_message(&link, &accessible_inboxes, input)
diff --git a/crates/email/src/inbound/toolset/send_email.rs b/crates/email/src/inbound/toolset/send_email.rs
index aafd1c73dc..1b445a9adb 100644
--- a/crates/email/src/inbound/toolset/send_email.rs
+++ b/crates/email/src/inbound/toolset/send_email.rs
@@ -107,9 +107,8 @@ where
) -> ToolResult<Self::Output> {
println!("CALL SEND EMAIL {:?}", request_context);
- let link = service_context
- .resolve_link(MacroUserIdStr((*request_context.user_id).clone()))
- .await?;
+ let acting_user = MacroUserIdStr((*request_context.user_id).clone());
+ let link = service_context.resolve_link(acting_user.clone()).await?;
let input = CreateDraftInput {
db_id: None,
@@ -129,6 +128,7 @@ where
// Composer override when present; otherwise None lets the backend
// apply the default signature policy.
include_signature: self.include_signature,
+ actor: Some(acting_user),
};
let sent = service_context
diff --git a/crates/email/src/outbound/email_pg_repo/draft.rs b/crates/email/src/outbound/email_pg_repo/draft.rs
index efda8789bb..b68c2c84f0 100644
--- a/crates/email/src/outbound/email_pg_repo/draft.rs
+++ b/crates/email/src/outbound/email_pg_repo/draft.rs
@@ -40,8 +40,14 @@ pub(crate) async fn insert_message(
// Only touch scheduling if send_time is explicitly provided.
// Scheduling is managed via the dedicated /drafts/scheduled endpoints.
if input.send_time.is_some() {
- message::process_scheduled_message(&mut tx, link_id, message_db_id, input.send_time)
- .await?;
+ message::process_scheduled_message(
+ &mut tx,
+ link_id,
+ message_db_id,
+ input.send_time,
+ input.actor_id.as_deref(),
+ )
+ .await?;
}
message::upsert_recipients(&mut tx, message_db_id, contacts).await?;
diff --git a/crates/email/src/outbound/email_pg_repo/message.rs b/crates/email/src/outbound/email_pg_repo/message.rs
index aff54d04c6..3ddc501418 100644
--- a/crates/email/src/outbound/email_pg_repo/message.rs
+++ b/crates/email/src/outbound/email_pg_repo/message.rs
@@ -395,24 +395,27 @@ pub(super) async fn process_scheduled_message(
link_id: Uuid,
message_db_id: Uuid,
send_time: Option<DateTime<Utc>>,
+ actor_id: Option<&str>,
) -> Result<(), sqlx::Error> {
if let Some(send_time) = send_time {
sqlx::query!(
r#"
INSERT INTO email_scheduled_messages (
- link_id, message_id, send_time, sent,
+ link_id, message_id, send_time, sent, actor_id,
created_at, updated_at
)
- VALUES ($1, $2, $3, $4, NOW(), NOW())
+ VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
ON CONFLICT (link_id, message_id) DO UPDATE SET
send_time = EXCLUDED.send_time,
sent = EXCLUDED.sent,
+ actor_id = EXCLUDED.actor_id,
updated_at = NOW()
"#,
link_id,
message_db_id,
send_time,
false,
+ actor_id,
)
.execute(&mut *tx)
.await?;
diff --git a/crates/email/src/outbound/email_pg_repo/test/draft.rs b/crates/email/src/outbound/email_pg_repo/test/draft.rs
index 770e58ac6d..26b250fada 100644
--- a/crates/email/src/outbound/email_pg_repo/test/draft.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/draft.rs
@@ -503,6 +503,7 @@ async fn test_insert_draft_message_into_existing_thread(
body_macro: None,
headers_json: None,
send_time: None,
+ actor_id: None,
};
let contacts = UpsertedContacts {
@@ -587,6 +588,7 @@ async fn test_insert_draft_message_with_new_thread(pool: Pool<Postgres>) -> anyh
body_macro: None,
headers_json: None,
send_time: None,
+ actor_id: None,
};
let contacts = UpsertedContacts {
@@ -659,6 +661,7 @@ async fn test_insert_draft_message_with_scheduled_send(pool: Pool<Postgres>) ->
body_macro: None,
headers_json: None,
send_time: Some(send_time),
+ actor_id: None,
};
let contacts = UpsertedContacts {
@@ -716,6 +719,7 @@ async fn test_insert_draft_message_upsert_existing(pool: Pool<Postgres>) -> anyh
body_macro: None,
headers_json: None,
send_time: None,
+ actor_id: None,
};
let contacts = UpsertedContacts {
@@ -783,6 +787,7 @@ async fn test_insert_draft_message_updates_thread_metadata(
body_macro: None,
headers_json: None,
send_time: None,
+ actor_id: None,
};
let contacts = UpsertedContacts {
@@ -835,6 +840,7 @@ async fn test_insert_message_with_is_draft_false(pool: Pool<Postgres>) -> anyhow
body_macro: None,
headers_json: None,
send_time: None,
+ actor_id: None,
};
let contacts = UpsertedContacts {
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 8f4071992a..cb9fe27c81 100644
--- a/crates/email/src/outbound/email_pg_repo/test/message.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/message.rs
@@ -470,12 +470,18 @@ async fn test_process_scheduled_message_insert(pool: Pool<Postgres>) -> anyhow::
};
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, Some(send_time))
- .await?;
+ super::super::message::process_scheduled_message(
+ &mut *tx,
+ link_id,
+ msg_id,
+ Some(send_time),
+ Some("macro|[email protected]"),
+ )
+ .await?;
tx.commit().await?;
let row = sqlx::query(
- "SELECT send_time, sent FROM email_scheduled_messages WHERE link_id = $1 AND message_id = $2",
+ "SELECT send_time, sent, actor_id FROM email_scheduled_messages WHERE link_id = $1 AND message_id = $2",
)
.bind(link_id)
.bind(msg_id)
@@ -484,8 +490,10 @@ async fn test_process_scheduled_message_insert(pool: Pool<Postgres>) -> anyhow::
let stored_time = row.get::<chrono::DateTime<chrono::Utc>, _>("send_time");
let sent = row.get::<bool, _>("sent");
+ let actor_id = row.get::<Option<String>, _>("actor_id");
assert_eq!(stored_time, send_time);
assert!(!sent);
+ assert_eq!(actor_id.as_deref(), Some("macro|[email protected]"));
Ok(())
}
@@ -506,14 +514,26 @@ async fn test_process_scheduled_message_upsert(pool: Pool<Postgres>) -> anyhow::
// Insert first
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, Some(send_time1))
- .await?;
+ super::super::message::process_scheduled_message(
+ &mut *tx,
+ link_id,
+ msg_id,
+ Some(send_time1),
+ None,
+ )
+ .await?;
tx.commit().await?;
// Upsert with new time
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, Some(send_time2))
- .await?;
+ super::super::message::process_scheduled_message(
+ &mut *tx,
+ link_id,
+ msg_id,
+ Some(send_time2),
+ None,
+ )
+ .await?;
tx.commit().await?;
let row = sqlx::query(
@@ -552,13 +572,19 @@ async fn test_process_scheduled_message_delete(pool: Pool<Postgres>) -> anyhow::
// Insert first
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, Some(send_time))
- .await?;
+ super::super::message::process_scheduled_message(
+ &mut *tx,
+ link_id,
+ msg_id,
+ Some(send_time),
+ Some("macro|[email protected]"),
+ )
+ .await?;
tx.commit().await?;
// Delete by passing None
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, None).await?;
+ super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, None, None).await?;
tx.commit().await?;
let count: i64 = sqlx::query_scalar(
@@ -585,7 +611,7 @@ async fn test_process_scheduled_message_delete_nonexistent(
// Delete when nothing exists — should not error
let mut tx = pool.begin().await?;
- super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, None).await?;
+ super::super::message::process_scheduled_message(&mut *tx, link_id, msg_id, None, None).await?;
tx.commit().await?;
Ok(())
diff --git a/crates/email_db_client/src/messages/scheduled/get.rs b/crates/email_db_client/src/messages/scheduled/get.rs
index 7d4ef772df..fed2e76b6f 100644
--- a/crates/email_db_client/src/messages/scheduled/get.rs
+++ b/crates/email_db_client/src/messages/scheduled/get.rs
@@ -17,7 +17,7 @@ where
let record = sqlx::query_as!(
service::message::ScheduledMessage,
r#"
- SELECT link_id, message_id, send_time, sent, processing
+ SELECT link_id, message_id, send_time, sent, processing, actor_id
FROM email_scheduled_messages
WHERE link_id = $1 AND message_id = $2
"#,
@@ -43,7 +43,7 @@ pub async fn get_and_start_processing_scheduled_message(
service::message::ScheduledMessage,
r#"
WITH old AS (
- SELECT link_id, message_id, send_time, sent, processing
+ SELECT link_id, message_id, send_time, sent, processing, actor_id
FROM email_scheduled_messages
WHERE link_id = $1 AND message_id = $2
), updated AS (
@@ -51,7 +51,7 @@ pub async fn get_and_start_processing_scheduled_message(
SET processing = true, updated_at = NOW()
WHERE link_id = $1 AND message_id = $2
)
- SELECT link_id, message_id, send_time, sent, processing
+ SELECT link_id, message_id, send_time, sent, processing, actor_id
FROM old
"#,
link_id,
@@ -73,7 +73,7 @@ pub async fn get_scheduled_message_no_auth(
let record = sqlx::query_as!(
db::message::ScheduledMessage,
r#"
- SELECT link_id, message_id, send_time, sent, processing
+ SELECT link_id, message_id, send_time, sent, processing, actor_id
FROM email_scheduled_messages
WHERE message_id = $1 and sent = false
"#,
@@ -115,7 +115,7 @@ pub async fn fetch_scheduled_messages_in_bulk(
let records = sqlx::query_as!(
db::message::ScheduledMessage,
r#"
- SELECT link_id, message_id, send_time, sent, processing
+ SELECT link_id, message_id, send_time, sent, processing, actor_id
FROM email_scheduled_messages
WHERE message_id = ANY($1) AND sent = false
"#,
diff --git a/crates/email_db_client/src/messages/scheduled/upsert.rs b/crates/email_db_client/src/messages/scheduled/upsert.rs
index ee7c59ce74..d55d949b88 100644
--- a/crates/email_db_client/src/messages/scheduled/upsert.rs
+++ b/crates/email_db_client/src/messages/scheduled/upsert.rs
@@ -10,19 +10,21 @@ pub async fn upsert_scheduled_message(
sqlx::query!(
r#"
INSERT INTO email_scheduled_messages (
- link_id, message_id, send_time, sent,
+ link_id, message_id, send_time, sent, actor_id,
created_at, updated_at
)
- VALUES ($1, $2, $3, $4, NOW(), NOW())
+ VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
ON CONFLICT (link_id, message_id) DO UPDATE SET
send_time = EXCLUDED.send_time,
sent = EXCLUDED.sent,
+ actor_id = EXCLUDED.actor_id,
updated_at = NOW()
"#,
db_message.link_id,
db_message.message_id,
db_message.send_time,
db_message.sent,
+ db_message.actor_id,
)
.execute(&mut *tx)
.await?;
diff --git a/crates/macro_db_client/migrations/20260816035007_add_actor_to_email_scheduled_messages.sql b/crates/macro_db_client/migrations/20260816035007_add_actor_to_email_scheduled_messages.sql
new file mode 100644
index 0000000000..8667312f5f
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260816035007_add_actor_to_email_scheduled_messages.sql
@@ -0,0 +1,6 @@
+-- The authenticated user who initiated the send, as a principal string
+-- (`macro|…`). Nullable: rows created before this column (and provider-side
+-- flows) have no recorded actor, and downstream consumers treat NULL as
+-- "actor unknown" (no activity attribution).
+ALTER TABLE email_scheduled_messages
+ ADD COLUMN actor_id TEXT;
diff --git a/crates/models_email/src/email/db/message.rs b/crates/models_email/src/email/db/message.rs
index df2f46fb18..51a7490f66 100644
--- a/crates/models_email/src/email/db/message.rs
+++ b/crates/models_email/src/email/db/message.rs
@@ -75,6 +75,9 @@ pub struct ScheduledMessage {
pub sent: bool,
/// whether the message is currently being processed by the background job.
pub processing: bool,
+ /// The authenticated user who initiated the send, as a principal string
+ /// (`macro|…`). `None` for rows created before actor tracking existed.
+ pub actor_id: Option<String>,
}
impl From<service::message::ScheduledMessage> for ScheduledMessage {
@@ -85,6 +88,7 @@ impl From<service::message::ScheduledMessage> for ScheduledMessage {
send_time: other.send_time,
sent: other.sent,
processing: other.processing,
+ actor_id: other.actor_id,
}
}
}
diff --git a/crates/models_email/src/email/service/message.rs b/crates/models_email/src/email/service/message.rs
index 83d81c791b..e06484b89d 100644
--- a/crates/models_email/src/email/service/message.rs
+++ b/crates/models_email/src/email/service/message.rs
@@ -322,6 +322,9 @@ pub struct ScheduledMessage {
pub sent: bool,
/// whether the message is currently being processed by the background job.
pub processing: bool,
+ /// The authenticated user who initiated the send, as a principal string
+ /// (`macro|…`). `None` for rows created before actor tracking existed.
+ pub actor_id: Option<String>,
}
impl From<db::message::ScheduledMessage> for ScheduledMessage {
@@ -332,6 +335,7 @@ impl From<db::message::ScheduledMessage> for ScheduledMessage {
send_time: other.send_time,
sent: other.sent,
processing: other.processing,
+ actor_id: other.actor_id,
}
}
}
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 d1ee6a0d0f..9e209dfced 100644
--- a/services/email_service/src/api/email/drafts/scheduled/upsert.rs
+++ b/services/email_service/src/api/email/drafts/scheduled/upsert.rs
@@ -96,6 +96,10 @@ pub async fn handler(
send_time: request.send_time,
sent: false,
processing: false,
+ // This legacy transport only knows the link; the owner is the best
+ // available attribution (matching the message_send_queued event
+ // below).
+ actor_id: Some(link.macro_id.to_string()),
};
// Upsert the scheduled message
diff --git a/services/email_service/src/pubsub/scheduled/process.rs b/services/email_service/src/pubsub/scheduled/process.rs
index 45d84a58a7..614949e087 100644
--- a/services/email_service/src/pubsub/scheduled/process.rs
+++ b/services/email_service/src/pubsub/scheduled/process.rs
@@ -9,6 +9,8 @@ use chrono::Utc;
use email::domain::events::{EmailEventOrigin, EmailMacroEvent, MessageSentMetadata};
use email_api_client::domain::models::{SendRequest, SentIds};
use email_db_client::messages::scheduled::get::get_and_start_processing_scheduled_message;
+use macro_user_id::cowlike::CowLike as _;
+use macro_user_id::user_id::MacroUserIdStr;
use models_email::service::message::MessageToSend;
use models_email::service::pubsub::ScheduledPubsubMessage;
use sqlx_core::any::AnyConnectionBackend;
@@ -182,8 +184,14 @@ async fn process_scheduled_message_inner(
// Gmail accepted the send and the DB updates are committed:
// publish the message_sent event resolving the earlier
- // message_send_queued.
- // Actor is not tracked on scheduled sends; owner is on `link`.
+ // message_send_queued. The actor was persisted on the scheduled
+ // row at enqueue time; rows from before actor tracking decode to
+ // `None` (no attribution).
+ let actor = scheduled_message
+ .actor_id
+ .as_deref()
+ .and_then(|raw| MacroUserIdStr::parse_from_str(raw).ok())
+ .map(|actor| actor.into_owned());
if let (Some(message_db_id), Some(thread_db_id)) =
(message_to_send.db_id, message_to_send.thread_db_id)
{
@@ -192,7 +200,7 @@ async fn process_scheduled_message_inner(
&EmailMacroEvent::message_sent(MessageSentMetadata {
link_id: link.id,
owner: link.macro_id.clone(),
- actor: None,
+ actor,
message_id: message_db_id,
thread_id: thread_db_id,
provider_message_id: message_to_send