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(email): notify delegated primaries of new emails in shared inboxes (#3788)

gbirman1c6786aa6042
diff --git a/rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.json b/rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.json
new file mode 100644
index 0000000000..6812c685dc
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT primary_macro_id\n FROM macro_user_links\n WHERE child_macro_id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "primary_macro_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "a577fec197448ea02b8378cc0ddca6fc4fcfc26228c3d3f8f055702598616a64"
+}
diff --git a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
index 1bccc931e4..29ab80336e 100644
--- a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
@@ -34,6 +34,9 @@ use std::result;
use std::sync::Arc;
use uuid::Uuid;
+#[cfg(test)]
+mod test;
+
// upsert a message into the db. could be a new message or an existing one that had changes
#[tracing::instrument(skip(ctx))]
pub async fn upsert_message(
@@ -571,20 +574,24 @@ async fn send_notifications(
snippet: message.snippet.unwrap_or_default(),
};
- let macro_id_str = link.macro_id.to_string();
- let recipient = MacroUserIdStr::parse_from_str(&macro_id_str).map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::InvalidData,
- source: anyhow::anyhow!("failed to parse macro user id: {}", e),
- })
- })?;
+ let primaries =
+ macro_db_client::macro_user_links::get_primaries_for_child(&ctx.db, link.macro_id.as_ref())
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to fetch delegated primaries".to_string()),
+ })
+ })?;
+
+ let recipient_ids = build_notification_recipients(&link.macro_id, primaries);
let request = SendNotificationRequestBuilder {
notification_entity: EntityType::EmailThread
.with_entity_string(message.thread_db_id.to_string()),
notification,
sender_id,
- recipient_ids: HashSet::from([recipient]),
+ recipient_ids,
}
.into_request()
.with_conn_gateway();
@@ -600,6 +607,30 @@ async fn send_notifications(
Ok(())
}
+fn build_notification_recipients(
+ owner: &MacroUserIdStr<'static>,
+ primaries: Vec<String>,
+) -> HashSet<MacroUserIdStr<'static>> {
+ let mut recipient_ids = HashSet::from([owner.clone()]);
+
+ for primary in primaries {
+ match MacroUserIdStr::try_from(primary) {
+ Ok(id) => {
+ recipient_ids.insert(id);
+ }
+ Err(e) => {
+ tracing::warn!(
+ error=?e,
+ inbox_owner=%owner,
+ "skipping delegated primary that failed to parse as a macro user id"
+ );
+ }
+ }
+ }
+
+ recipient_ids
+}
+
// filter out messages we don't want to send notifications for
#[tracing::instrument(skip(ctx, link))]
async fn filter_notifiable_message(
diff --git a/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message/test.rs b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message/test.rs
new file mode 100644
index 0000000000..cc0f92f975
--- /dev/null
+++ b/rust/cloud-storage/email_service/src/pubsub/inbox_sync/operations/upsert_message/test.rs
@@ -0,0 +1,51 @@
+use super::*;
+
+fn id(s: &str) -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from(s.to_string()).unwrap()
+}
+
+#[test]
+fn includes_owner_and_all_delegated_primaries() {
+ let owner = id("macro|[email protected]");
+ let recipients = build_notification_recipients(
+ &owner,
+ vec![
+ "macro|[email protected]".to_string(),
+ "macro|[email protected]".to_string(),
+ ],
+ );
+
+ assert_eq!(
+ recipients,
+ HashSet::from([
+ owner,
+ id("macro|[email protected]"),
+ id("macro|[email protected]"),
+ ])
+ );
+}
+
+#[test]
+fn returns_only_owner_when_no_primaries() {
+ let owner = id("macro|[email protected]");
+ let recipients = build_notification_recipients(&owner, vec![]);
+
+ assert_eq!(recipients, HashSet::from([owner]));
+}
+
+#[test]
+fn skips_unparseable_primaries_keeping_valid_ones() {
+ let owner = id("macro|[email protected]");
+ let recipients = build_notification_recipients(
+ &owner,
+ vec![
+ "macro|[email protected]".to_string(),
+ "not-a-valid-id".to_string(),
+ ],
+ );
+
+ assert_eq!(
+ recipients,
+ HashSet::from([owner, id("macro|[email protected]")])
+ );
+}
diff --git a/rust/cloud-storage/macro_db_client/src/macro_user_links.rs b/rust/cloud-storage/macro_db_client/src/macro_user_links.rs
index 163d8caaf5..3bdaad663b 100644
--- a/rust/cloud-storage/macro_db_client/src/macro_user_links.rs
+++ b/rust/cloud-storage/macro_db_client/src/macro_user_links.rs
@@ -122,3 +122,25 @@ pub async fn children_for_primary(
Ok(rows)
}
+
+/// Returns the `primary_macro_id`s delegated to read the given child's inboxes.
+/// Inverse of [`children_for_primary`]; used to fan a child inbox's notifications
+/// out to every primary that can view it.
+#[tracing::instrument(skip(db), err)]
+pub async fn get_primaries_for_child(
+ db: &Pool<Postgres>,
+ child_macro_id: &str,
+) -> anyhow::Result<Vec<String>> {
+ let rows = sqlx::query_scalar!(
+ r#"
+ SELECT primary_macro_id
+ FROM macro_user_links
+ WHERE child_macro_id = $1
+ "#,
+ child_macro_id
+ )
+ .fetch_all(db)
+ .await?;
+
+ Ok(rows)
+}
diff --git a/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs b/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs
index 9b09db206f..ccf3703aca 100644
--- a/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs
+++ b/rust/cloud-storage/macro_db_client/src/macro_user_links/test.rs
@@ -117,6 +117,40 @@ async fn delete_edges_for_child_removes_all_grants(pool: Pool<Postgres>) -> anyh
Ok(())
}
+#[sqlx::test]
+async fn get_primaries_for_child_returns_all_grantees(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ insert_user(&pool, "macro|[email protected]").await?;
+ insert_user(&pool, "macro|[email protected]").await?;
+ insert_user(&pool, "macro|[email protected]").await?;
+ insert_user(&pool, "macro|[email protected]").await?;
+
+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;
+ insert_edge(&pool, "macro|[email protected]", "macro|[email protected]").await?;
+
+ let mut primaries = get_primaries_for_child(&pool, "macro|[email protected]").await?;
+ primaries.sort();
+ assert_eq!(
+ primaries,
+ vec!["macro|[email protected]", "macro|[email protected]"]
+ );
+
+ // A child nobody delegates from yields no primaries.
+ assert!(
+ get_primaries_for_child(&pool, "macro|[email protected]")
+ .await?
+ .is_empty()
+ );
+
+ // Direction matters: querying a primary as if it were a child returns nothing.
+ assert!(
+ get_primaries_for_child(&pool, "macro|[email protected]")
+ .await?
+ .is_empty()
+ );
+
+ Ok(())
+}
+
#[sqlx::test]
async fn insert_edge_within_transaction_commits(pool: Pool<Postgres>) -> anyhow::Result<()> {
insert_user(&pool, "macro|[email protected]").await?;