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): open DMs with colleagues and top contacts on inbox connect

Jacob Beckerman086aa37aa08f
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 47e1319ec2..dd4f0aeea9 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -6104,6 +6104,7 @@
"crates/folds/agent_fold",
"crates/frecency",
"crates/genai_telemetry",
+ "crates/generic_email_domains",
"crates/gmail_client",
"crates/google_token",
"crates/harness_id",
diff --git a/.sqlx/query-08e7175628f5252818f5e30174baa41a272cb34d12c9f50d9a7c3cc7913dfa4b.json b/.sqlx/query-08e7175628f5252818f5e30174baa41a272cb34d12c9f50d9a7c3cc7913dfa4b.json
new file mode 100644
index 0000000000..402d747fa6
--- /dev/null
+++ b/.sqlx/query-08e7175628f5252818f5e30174baa41a272cb34d12c9f50d9a7c3cc7913dfa4b.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT DISTINCT\n LOWER(email_address) AS \"email!\"\n FROM\n email_contacts\n WHERE\n link_id = $1\n ORDER BY\n 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "08e7175628f5252818f5e30174baa41a272cb34d12c9f50d9a7c3cc7913dfa4b"
+}
diff --git a/.sqlx/query-6fe843846a518b0b1892c91eba4ebc55371e92d5b43b60bfe737c16b2387ef5a.json b/.sqlx/query-6fe843846a518b0b1892c91eba4ebc55371e92d5b43b60bfe737c16b2387ef5a.json
new file mode 100644
index 0000000000..add806b8e3
--- /dev/null
+++ b/.sqlx/query-6fe843846a518b0b1892c91eba4ebc55371e92d5b43b60bfe737c16b2387ef5a.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n LOWER(c.email_address) AS \"email!\"\n FROM\n email_messages m\n JOIN\n email_message_recipients mr ON m.id = mr.message_id\n JOIN\n email_contacts c ON mr.contact_id = c.id\n WHERE\n m.link_id = $1\n AND m.is_sent = TRUE\n AND m.is_draft = FALSE\n GROUP BY\n LOWER(c.email_address)\n ORDER BY\n COUNT(DISTINCT m.id) DESC,\n LOWER(c.email_address)\n LIMIT $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "6fe843846a518b0b1892c91eba4ebc55371e92d5b43b60bfe737c16b2387ef5a"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 1b94f90849..5efff2125b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7271,6 +7271,7 @@ dependencies = [
"base64 0.22.1",
"bytes",
"calendar_events",
+ "channels",
"chrono",
"cloudfront_sign",
"connection_gateway_client",
@@ -7289,6 +7290,7 @@ dependencies = [
"filter_ast",
"frecency",
"futures",
+ "generic_email_domains",
"gmail_client",
"google_token",
"html-escape",
diff --git a/crates/email_db_client/fixtures/address_book_and_top_contacts.sql b/crates/email_db_client/fixtures/address_book_and_top_contacts.sql
new file mode 100644
index 0000000000..62c20aaa53
--- /dev/null
+++ b/crates/email_db_client/fixtures/address_book_and_top_contacts.sql
@@ -0,0 +1,40 @@
+-- One inbox with a synced address book of five contacts (one automated) and
+-- sent mail that ranks two of them: carol three messages, alice one.
+
+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
+VALUES
+ ('ab000000-0000-0000-0000-000000000001', 'macro|[email protected]', 'fa-owner', '[email protected]', 'GMAIL', true, NOW(), NOW());
+
+INSERT INTO email_contacts (id, link_id, email_address, name, created_at, updated_at)
+VALUES
+ ('ac000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001', '[email protected]', 'Alice', NOW(), NOW()),
+ ('ac000000-0000-0000-0000-000000000002', 'ab000000-0000-0000-0000-000000000001', '[email protected]', 'Bob', NOW(), NOW()),
+ ('ac000000-0000-0000-0000-000000000003', 'ab000000-0000-0000-0000-000000000001', '[email protected]', 'Carol', NOW(), NOW()),
+ ('ac000000-0000-0000-0000-000000000004', 'ab000000-0000-0000-0000-000000000001', '[email protected]', NULL, NOW(), NOW()),
+ ('ac000000-0000-0000-0000-000000000005', 'ab000000-0000-0000-0000-000000000001', '[email protected]', 'Dave', NOW(), NOW());
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, created_at, updated_at)
+VALUES
+ ('ad000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001', true, true, NOW(), NOW());
+
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, internal_date_ts,
+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
+VALUES
+ ('ae000000-0000-0000-0000-000000000001', 'ad000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001',
+ 'provider-ab-1', TRUE, '2026-01-05 10:00:00 +00:00', false, true, false, false, NOW(), NOW()),
+ ('ae000000-0000-0000-0000-000000000002', 'ad000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001',
+ 'provider-ab-2', TRUE, '2026-01-05 11:00:00 +00:00', false, true, false, false, NOW(), NOW()),
+ ('ae000000-0000-0000-0000-000000000003', 'ad000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001',
+ 'provider-ab-3', TRUE, '2026-01-05 12:00:00 +00:00', false, true, false, false, NOW(), NOW()),
+ -- A draft never counts toward the ranking.
+ ('ae000000-0000-0000-0000-000000000004', 'ad000000-0000-0000-0000-000000000001', 'ab000000-0000-0000-0000-000000000001',
+ 'provider-ab-4', FALSE, '2026-01-05 13:00:00 +00:00', false, true, false, true, NOW(), NOW());
+
+INSERT INTO email_message_recipients (message_id, contact_id, recipient_type)
+VALUES
+ ('ae000000-0000-0000-0000-000000000001', 'ac000000-0000-0000-0000-000000000003', 'TO'),
+ ('ae000000-0000-0000-0000-000000000002', 'ac000000-0000-0000-0000-000000000003', 'TO'),
+ ('ae000000-0000-0000-0000-000000000003', 'ac000000-0000-0000-0000-000000000003', 'CC'),
+ ('ae000000-0000-0000-0000-000000000003', 'ac000000-0000-0000-0000-000000000001', 'TO'),
+ ('ae000000-0000-0000-0000-000000000001', 'ac000000-0000-0000-0000-000000000004', 'TO'),
+ ('ae000000-0000-0000-0000-000000000004', 'ac000000-0000-0000-0000-000000000005', 'TO');
diff --git a/crates/email_db_client/src/contacts/get.rs b/crates/email_db_client/src/contacts/get.rs
index 4e49efd31a..8e3bba357e 100644
--- a/crates/email_db_client/src/contacts/get.rs
+++ b/crates/email_db_client/src/contacts/get.rs
@@ -602,6 +602,84 @@ pub async fn fetch_contacts_by_link_id(
Ok(db_contacts.into_iter().map(Into::into).collect())
}
+/// Every non-automated address in the link's synced address book: contacts the
+/// user saved in Google plus the "other contacts" Google builds from mail. Unlike
+/// [`fetch_contacts_emails_by_link_id`] this does not require the user to have
+/// emailed the address.
+#[tracing::instrument(skip(pool), err)]
+pub async fn fetch_address_book_emails_by_link_id(
+ pool: &PgPool,
+ link_id: Uuid,
+) -> anyhow::Result<Vec<String>> {
+ let rows = sqlx::query!(
+ r#"
+ SELECT DISTINCT
+ LOWER(email_address) AS "email!"
+ FROM
+ email_contacts
+ WHERE
+ link_id = $1
+ ORDER BY
+ 1
+ "#,
+ link_id
+ )
+ .fetch_all(pool)
+ .await?;
+
+ Ok(rows
+ .into_iter()
+ .map(|row| row.email)
+ .filter(|email| !is_generic_email(email))
+ .collect())
+}
+
+/// The non-automated addresses the link has sent the most messages to, most
+/// emailed first, at most `limit` of them.
+#[tracing::instrument(skip(pool), err)]
+pub async fn fetch_top_sent_contact_emails_by_link_id(
+ pool: &PgPool,
+ link_id: Uuid,
+ limit: usize,
+) -> anyhow::Result<Vec<String>> {
+ // Automated addresses are dropped after the query, so read a wider window
+ // than the caller asked for.
+ let window = i64::try_from(limit.saturating_mul(5)).unwrap_or(i64::MAX);
+ let rows = sqlx::query!(
+ r#"
+ SELECT
+ LOWER(c.email_address) AS "email!"
+ FROM
+ email_messages m
+ JOIN
+ email_message_recipients mr ON m.id = mr.message_id
+ JOIN
+ email_contacts c ON mr.contact_id = c.id
+ WHERE
+ m.link_id = $1
+ AND m.is_sent = TRUE
+ AND m.is_draft = FALSE
+ GROUP BY
+ LOWER(c.email_address)
+ ORDER BY
+ COUNT(DISTINCT m.id) DESC,
+ LOWER(c.email_address)
+ LIMIT $2
+ "#,
+ link_id,
+ window
+ )
+ .fetch_all(pool)
+ .await?;
+
+ Ok(rows
+ .into_iter()
+ .map(|row| row.email)
+ .filter(|email| !is_generic_email(email))
+ .take(limit)
+ .collect())
+}
+
/// returns all non-generic email addresses the passed link has sent emails to.
#[tracing::instrument(skip(pool), err)]
pub async fn fetch_contacts_emails_by_link_id(
diff --git a/crates/email_db_client/src/contacts/get/test.rs b/crates/email_db_client/src/contacts/get/test.rs
index d105c8c31a..0c408c36d2 100644
--- a/crates/email_db_client/src/contacts/get/test.rs
+++ b/crates/email_db_client/src/contacts/get/test.rs
@@ -1,5 +1,6 @@
use crate::contacts::get::{
- fetch_contacts_by_link_id, fetch_db_recipients_in_bulk, fetch_senders_by_message_ids,
+ fetch_address_book_emails_by_link_id, fetch_contacts_by_link_id, fetch_db_recipients_in_bulk,
+ fetch_senders_by_message_ids, fetch_top_sent_contact_emails_by_link_id,
};
use anyhow::Result;
use macro_db_migrator::MACRO_DB_MIGRATIONS;
@@ -273,3 +274,44 @@ async fn fetch_db_recipients_in_bulk_returns_empty_for_nonexistent_messages(
Ok(())
}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("address_book_and_top_contacts"))
+)]
+async fn fetch_address_book_emails_returns_every_non_automated_contact(
+ pool: Pool<Postgres>,
+) -> Result<()> {
+ let link_id = Uuid::parse_str("ab000000-0000-0000-0000-000000000001")?;
+
+ let emails = fetch_address_book_emails_by_link_id(&pool, link_id).await?;
+
+ // Includes contacts the user never emailed, lowercased, without noreply@.
+ assert_eq!(
+ emails,
+ vec![
+ ]
+ );
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../fixtures", scripts("address_book_and_top_contacts"))
+)]
+async fn fetch_top_sent_contact_emails_ranks_by_messages_sent(pool: Pool<Postgres>) -> Result<()> {
+ let link_id = Uuid::parse_str("ab000000-0000-0000-0000-000000000001")?;
+
+ let top = fetch_top_sent_contact_emails_by_link_id(&pool, link_id, 10).await?;
+ // carol: 3 messages, alice: 1. The automated address and the contact only
+ // addressed in a draft are excluded or ranked by actual sends.
+ assert_eq!(top, vec!["[email protected]", "[email protected]"]);
+
+ let capped = fetch_top_sent_contact_emails_by_link_id(&pool, link_id, 1).await?;
+ assert_eq!(capped, vec!["[email protected]"]);
+ Ok(())
+}
diff --git a/services/email_service/Cargo.toml b/services/email_service/Cargo.toml
index 7eda20fd4e..56697bfbf2 100644
--- a/services/email_service/Cargo.toml
+++ b/services/email_service/Cargo.toml
@@ -131,7 +131,9 @@ notification = { path = "../../crates/notification" }
macro_user_id = { path = "../../crates/macro_user_id" }
macro_uuid = { path = "../../crates/macro_uuid" }
mime_guess = "2.0.5"
+channels = { path = "../../crates/channels", features = ["outbound"] }
contacts = { path = "../../crates/contacts" }
+generic_email_domains = { path = "../../crates/generic_email_domains" }
model = { path = "../../crates/model" }
model-entity = { path = "../../crates/model-entity" }
model_file_type = { path = "../../crates/model_file_type" }
diff --git a/services/email_service/src/backfill_completion_service.rs b/services/email_service/src/backfill_completion_service.rs
index 27ef400c7d..97c9f07245 100644
--- a/services/email_service/src/backfill_completion_service.rs
+++ b/services/email_service/src/backfill_completion_service.rs
@@ -256,6 +256,9 @@ async fn finalize_claimed_backfill(
handle_contacts_sync(ctx, &link)
.await
.map_err(completion_effect_retry)?;
+ // Best effort: the mailbox now has sent mail to rank, so open direct
+ // messages with the people the user emails most.
+ crate::util::contact_dms::ensure_top_contact_dms(&ctx.db, &link).await;
cg_refresh_email(
&ctx.connection_gateway_client,
link.macro_id.as_ref(),
diff --git a/services/email_service/src/backfill_init_service.rs b/services/email_service/src/backfill_init_service.rs
index eaaab7d61a..3566918c81 100644
--- a/services/email_service/src/backfill_init_service.rs
+++ b/services/email_service/src/backfill_init_service.rs
@@ -85,7 +85,7 @@ async fn initialize_backfill(
})
})?;
- if let Err(e) = sync_contacts(
+ match sync_contacts(
link,
&ctx.db,
&ctx.email_api,
@@ -95,7 +95,8 @@ async fn initialize_backfill(
)
.await
{
- tracing::error!(error = ?e, "Failed to sync contacts");
+ Ok(()) => crate::util::contact_dms::ensure_colleague_dms(&ctx.db, link).await,
+ Err(e) => tracing::error!(error = ?e, "Failed to sync contacts"),
}
let threads_requested_limit = backfill_job.threads_requested_limit;
diff --git a/services/email_service/src/util/contact_dms.rs b/services/email_service/src/util/contact_dms.rs
new file mode 100644
index 0000000000..b74eada0f4
--- /dev/null
+++ b/services/email_service/src/util/contact_dms.rs
@@ -0,0 +1,132 @@
+//! Direct messages for people a new user already works with.
+//!
+//! The contacts a user's Google account shares are the best signal for who they
+//! collaborate with, so a connected inbox starts with direct messages to
+//! colleagues at their work domain and to the people they email most. Creation
+//! is idempotent and best-effort: a failure never fails the sync that triggered it.
+
+use channels::{
+ domain::{
+ dm::ensure_dms_for_joining_member, ports::ChannelService, service::ChannelServiceImpl,
+ },
+ outbound::pg_channels_repo::PgChannelsRepo,
+};
+use generic_email_domains::is_generic_email_domain;
+use macro_user_id::user_id::MacroUserIdStr;
+use models_email::service::link::Link;
+use sqlx::PgPool;
+
+#[cfg(test)]
+mod test;
+
+/// Colleagues a single inbox opens direct messages with.
+const MAX_COLLEAGUE_DMS: usize = 200;
+
+/// The most-emailed contacts a single inbox opens direct messages with.
+const MAX_TOP_CONTACT_DMS: usize = 10;
+
+/// The lowercased domain of `email`.
+fn domain_of(email: &str) -> Option<String> {
+ email
+ .rsplit_once('@')
+ .map(|(_, domain)| domain.to_ascii_lowercase())
+}
+
+/// Addresses in `emails` on the work domain of `own_email`, excluding `own_email`.
+///
+/// Returns nothing when `own_email` is on a consumer domain such as gmail.com,
+/// where sharing a domain says nothing about working together.
+pub(crate) fn colleague_emails(own_email: &str, emails: &[String]) -> Vec<String> {
+ let Some(own_domain) = domain_of(own_email).filter(|domain| !is_generic_email_domain(domain))
+ else {
+ return Vec::new();
+ };
+ let own_email = own_email.to_ascii_lowercase();
+ emails
+ .iter()
+ .filter(|email| **email != own_email)
+ .filter(|email| domain_of(email).as_deref() == Some(own_domain.as_str()))
+ .take(MAX_COLLEAGUE_DMS)
+ .cloned()
+ .collect()
+}
+
+/// Opens direct messages from the link's owner to each address. Invalid
+/// addresses and the owner's own address are skipped.
+async fn ensure_dms_with(db: &PgPool, link: &Link, emails: impl IntoIterator<Item = String>) {
+ let own_email = link.email_address.0.as_ref().to_ascii_lowercase();
+ let roster: Vec<MacroUserIdStr<'static>> = emails
+ .into_iter()
+ .filter(|email| *email != own_email)
+ .filter_map(|email| MacroUserIdStr::try_from_email(&email).ok())
+ .filter(|contact| *contact != link.macro_id)
+ .collect();
+ if roster.is_empty() {
+ return;
+ }
+
+ let channels = ChannelServiceImpl::new(PgChannelsRepo::new(db.clone()));
+ match channels
+ .ensure_dms(ensure_dms_for_joining_member(link.macro_id.clone(), roster))
+ .await
+ {
+ Ok(summary) => tracing::info!(
+ link_id = %link.id,
+ created = summary.created,
+ existing = summary.existing,
+ failed = summary.failed,
+ "ensured direct messages from synced contacts"
+ ),
+ Err(error) => tracing::warn!(
+ link_id = %link.id,
+ error = ?error,
+ "unable to ensure direct messages from synced contacts"
+ ),
+ }
+}
+
+/// Opens direct messages with the colleagues in the inbox's synced address book.
+///
+/// Only the primary inbox counts; a secondary inbox's address book is not the
+/// user's work circle.
+#[tracing::instrument(skip(db, link), fields(link_id = %link.id))]
+pub async fn ensure_colleague_dms(db: &PgPool, link: &Link) {
+ if !link.is_primary {
+ return;
+ }
+ let emails =
+ match email_db_client::contacts::get::fetch_address_book_emails_by_link_id(db, link.id)
+ .await
+ {
+ Ok(emails) => emails,
+ Err(error) => {
+ tracing::warn!(error = ?error, "unable to read the address book for colleague DMs");
+ return;
+ }
+ };
+ let colleagues = colleague_emails(link.email_address.0.as_ref(), &emails);
+ ensure_dms_with(db, link, colleagues).await;
+}
+
+/// Opens direct messages with the people the user emails most. It runs after
+/// the mailbox backfill, once there is sent mail to rank.
+#[tracing::instrument(skip(db, link), fields(link_id = %link.id))]
+pub async fn ensure_top_contact_dms(db: &PgPool, link: &Link) {
+ if !link.is_primary {
+ return;
+ }
+ let emails = match email_db_client::contacts::get::fetch_top_sent_contact_emails_by_link_id(
+ db,
+ link.id,
+ MAX_TOP_CONTACT_DMS,
+ )
+ .await
+ {
+ Ok(emails) => emails,
+ Err(error) => {
+ tracing::warn!(error = ?error, "unable to rank contacts for top-contact DMs");
+ return;
+ }
+ };
+ ensure_dms_with(db, link, emails).await;
+}
diff --git a/services/email_service/src/util/contact_dms/test.rs b/services/email_service/src/util/contact_dms/test.rs
new file mode 100644
index 0000000000..3ef9f58b07
--- /dev/null
+++ b/services/email_service/src/util/contact_dms/test.rs
@@ -0,0 +1,34 @@
+use super::colleague_emails;
+
+fn emails(values: &[&str]) -> Vec<String> {
+ values.iter().map(|value| (*value).to_owned()).collect()
+}
+
+#[test]
+fn keeps_only_addresses_on_the_owners_work_domain() {
+ let contacts = emails(&[
+ ]);
+
+ assert_eq!(
+ colleague_emails("[email protected]", &contacts),
+ );
+}
+
+#[test]
+fn consumer_domains_have_no_colleagues() {
+ let contacts = emails(&["[email protected]", "[email protected]"]);
+
+ assert!(colleague_emails("[email protected]", &contacts).is_empty());
+}
+
+#[test]
+fn caps_the_number_of_colleagues() {
+ let contacts: Vec<String> = (0..500).map(|i| format!("person{i}@acme.com")).collect();
+
+ assert_eq!(colleague_emails("[email protected]", &contacts).len(), 200);
+}
diff --git a/services/email_service/src/util/mod.rs b/services/email_service/src/util/mod.rs
index 3e3aff2840..d89ca704e6 100644
--- a/services/email_service/src/util/mod.rs
+++ b/services/email_service/src/util/mod.rs
@@ -1,3 +1,4 @@
+pub mod contact_dms;
pub mod gmail;
pub mod process_pre_insert;
pub mod redis;