Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use super::pending_labels;
use crate::pubsub::context::PubSubContext;
use crate::pubsub::inbox_sync::email_api_error::handle_operation_error;
use crate::pubsub::util::{
CrmContactRecipient, build_contact_connections, 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, UploadAttachmentError, upload_attachment,
};
use contacts::domain::ports::ContactsIngress;
use email::domain::events::{
EmailEventOrigin, EmailMacroEvent, MessageDraftSyncedMetadata, MessageReceivedMetadata,
MessageSentMetadata,
};
use email::domain::models::{PreviewCursorQuery, PreviewView, PreviewViewStandardLabel};
use email::domain::ports::EmailRepo;
use email::outbound::EmailPgRepo;
use email_api_client::domain::models::{EmailApiError, MessageWithCalendarParts};
use email_db_client::attachments::provider::upload_filters::{
attachment_is_document, attachment_is_media,
};
use email_db_client::messages::insert::ResyncSnapshot;
use email_db_client::threads;
use email_utils::{dedupe_emails, is_macro_notification_sender};
use filter_ast::Expr;
use item_filters::{SharedEmailFilter, ast::email::EmailLiteral};
use macro_user_id::user_id::MacroUserIdStr;
use model_entity::EntityType;
use model_notifications::NewEmailMetadata;
use models_email::api::refresh::RefreshEmailEvent;
use models_email::db::address::EmailRecipientType;
use models_email::email::service::link;
use models_email::email::service::message::SimpleMessage;
use models_email::gmail::inbox_sync::{InboxSyncOperation, UpsertMessagePayload};
use models_email::service::attachment::{
Attachment, AttachmentUploadArgs, AttachmentUploadDestination, AttachmentUploadMetadata,
};
use models_email::service::message::{Message, is_inbound, is_outbound, is_spam_or_trash};
use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
use models_email::service::thread::Thread;
use notification::domain::models::{SendNotificationRequest, SendNotificationRequestBuilder};
use notification::domain::service::NotificationIngress;
use sqlx::PgPool;
use std::collections::HashSet;
use std::result;
use std::sync::Arc;
use uuid::Uuid;
#[cfg(test)]
mod test;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MessageSyncEventKind {
DraftSynced,
Received,
Sent,
}
fn select_message_sync_event(
existing_message_was_draft: Option<bool>,
is_draft: bool,
is_sent: bool,
) -> Option<MessageSyncEventKind> {
if is_draft {
return Some(MessageSyncEventKind::DraftSynced);
}
if existing_message_was_draft == Some(true) && is_sent {
return Some(MessageSyncEventKind::Sent);
}
if existing_message_was_draft.is_some() {
return None;
}
if is_sent {
Some(MessageSyncEventKind::Sent)
} else {
Some(MessageSyncEventKind::Received)
}
}
/// Fetches the message from Gmail, or `None` when Gmail no longer has it.
async fn fetch_provider_message(
ctx: &PubSubContext,
link: &link::Link,
payload: &UpsertMessagePayload,
) -> result::Result<Option<MessageWithCalendarParts>, ProcessingError> {
match ctx
.email_api
.get_message(link.id, &payload.provider_message_id)
.await
{
Ok(fetched) => Ok(fetched),
Err(error) => Err(handle_operation_error(
ctx,
link.id,
InboxSyncOperation::UpsertMessage(payload.clone()),
error,
)
.await),
}
}
// 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(
ctx: &PubSubContext,
link: &link::Link,
payload: &UpsertMessagePayload,
) -> result::Result<(), ProcessingError> {
// Snapshot the previous draft state before the upsert. Provider drafts are mutable,
// and this state distinguishes a draft-to-sent transition from an unchanged message.
let existing_message =
email_db_client::messages::get_simple_messages::get_simple_message_by_provider_and_link(
&ctx.db,
&payload.provider_message_id,
&link.id,
)
.await
.map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e
.context("Failed to fetch existing message before provider upsert".to_string()),
})
})?;
let existing_message_was_draft = existing_message.as_ref().map(|message| message.is_draft);
let message_already_exists = existing_message.is_some();
let (fetched, resync) = match &existing_message {
Some(existing) => {
let (fetched, snapshot) = pending_labels::resync_around(
&ctx.db,
existing.db_id,
Box::pin(fetch_provider_message(ctx, link, payload)),
)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to read the stored message before its re-sync"),
})
})?;
(fetched?, Some(snapshot))
}
None => (fetch_provider_message(ctx, link, payload).await?, None),
};
let Some(fetched) = fetched else {
tracing::debug!(provider_message_id = %payload.provider_message_id, link_id = %link.id,
"Message not found in gmail when attempting to upsert");
return Ok(());
};
let mut message = fetched.message;
let message_attachment_count = message.attachments.len();
// will always exist because we just fetched it
let provider_thread_id = message.provider_thread_id.clone().unwrap();
let is_sent = message.is_sent;
let is_spam_or_trash = is_spam_or_trash(&message);
let sender_email = message
.from
.as_ref()
.map(|from| from.email.clone())
.filter(|e| !email_utils::is_generic_email(e));
// deduped list of all non-generic emails the message was sent to
let recipient_emails = dedupe_emails(
message
.to
.iter()
.chain(&message.cc)
.chain(&message.bcc)
.map(|c| c.email.clone())
.collect(),
)
.into_iter()
.filter(|e| !email_utils::is_generic_email(e))
.collect::<Vec<_>>();
// Snapshot `(email, name, first_at, last_at)` tuples for the CRM
// populate fan-out below. Captured here (before `message` is moved
// into `process_and_insert_message`) so the consumer can write
// `crm_contacts.name` without a separate email_contacts lookup.
//
// Single message → single timestamp covers both endpoints; the
// consumer merges with the stored range over time. Sent: enumerate
// to/cc/bcc. Received: enumerate `from`. Drafts are skipped — they
// don't represent real correspondence. No producer-side filtering
// of addresses — the crm crate is the single source of truth.
let is_draft = message.is_draft;
// `Utc::now()` fallback when Gmail returned no internal_date_ts.
let at = message.internal_date_ts.unwrap_or_else(chrono::Utc::now);
let crm_recipients: Vec<CrmContactRecipient> = if is_draft {
Vec::new()
} else if is_sent {
message
.to
.iter()
.chain(&message.cc)
.chain(&message.bcc)
.map(|c| (c.email.clone(), c.name.clone(), at, at))
.collect()
} else {
message
.from
.iter()
.map(|c| (c.email.clone(), c.name.clone(), at, at))
.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,
link.id,
&HashSet::from([provider_thread_id.clone()]),
)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to get threads by link id and provider ids".to_string()),
})
})?;
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) {
// Retryable: a concurrent delete can drop the thread after the lookup
// above, and the redelivery then takes the whole-thread branch below.
process_and_insert_message(
&ctx.db,
link.id,
*thread_db_id,
&mut message,
resync.as_ref(),
)
.await?;
} else {
// Propagated verbatim: fetch_and_insert_thread already routed provider
// errors through handle_operation_error (retry queue vs redelivery),
// and re-wrapping would double-process rate limits and mislabel
// permanent failures as retryable.
fetch_and_insert_thread(ctx, payload, link.id, &provider_thread_id).await?;
}
let (message_db_id, thread_db_id) =
email_db_client::messages::get::get_message_and_thread_id_by_provider_id(
&ctx.db,
link.id,
&payload.provider_message_id,
)
.await
.map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to get new message db id".to_string()),
})
})?;
// Also runs for a message already saved by a whole-thread fetch, which carries no
// calendar parts. Saves skip existing components; an attachment-only invitation is
// downloaded again on a re-sync.
if !message.is_draft {
crate::pubsub::invitation_extraction::save_discovered(
ctx,
link.id,
&payload.provider_message_id,
message_db_id,
&fetched.calendar_parts,
)
.await;
}
// Publish to the macro.email topic immediately after the committed insert.
// Drafts publish after every sync because their bodies are mutable. Existing
// immutable messages remain suppressed except when a previously synced provider
// draft becomes sent.
if let Some(event_kind) =
select_message_sync_event(existing_message_was_draft, is_draft, is_sent)
{
let event = match event_kind {
MessageSyncEventKind::DraftSynced => {
EmailMacroEvent::message_draft_synced(MessageDraftSyncedMetadata {
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_spam_or_trash,
})
}
MessageSyncEventKind::Received => {
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(|contact| contact.email.clone()),
from_name: event_from.as_ref().and_then(|contact| contact.name.clone()),
to_emails: event_to_emails,
attachment_count: message_attachment_count as u32,
is_spam_or_trash,
received_at: event_received_at,
})
}
MessageSyncEventKind::Sent => 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),
}),
};
publish_email_event(&ctx.macro_event_broker, &event);
}
let refused_upload = handle_attachment_upload(ctx, link, payload, &message.attachments).await?;
handle_contacts_sync(
ctx,
link,
&recipient_emails,
sender_email.as_deref(),
is_sent,
)
.await?;
// Fan out a PopulateCrmContact job per address. Mirrors
// `backfill_message.rs`: sent → to/cc/bcc, received → from,
// drafts → skipped. The consumer branches on `is_sent` for the
// company-insert gate.
if !crm_recipients.is_empty() {
let self_email = link.email_address.0.as_ref().to_ascii_lowercase();
enqueue_populate_crm_contacts(ctx, link.id, &self_email, crm_recipients, is_sent).await?;
}
// trigger FE inbox refresh
cg_refresh_email(
&ctx.connection_gateway_client,
link.macro_id.as_ref(),
RefreshEmailEvent::UpsertMessage { link_id: link.id },
)
.await;
// notify downstream services of new messages
if !message_already_exists {
notify_for_new_message(
ctx,
link,
&payload.provider_message_id,
message_db_id,
thread_db_id,
)
.await?;
}
// Requeued only after the steps above, which are safe to repeat: the retry
// finds the message saved, so it doesn't notify again.
if let Some(error) = refused_upload {
return Err(handle_operation_error(
ctx,
link.id,
InboxSyncOperation::UpsertMessage(payload.clone()),
error,
)
.await);
}
Ok(())
}
/// Uploads the message's new attachments, returning the rate limit that
/// refused an attachment fetch, if one did.
#[tracing::instrument(skip(ctx, attachments), err)]
async fn handle_attachment_upload(
ctx: &PubSubContext,
link: &link::Link,
payload: &UpsertMessagePayload,
attachments: &[Attachment],
) -> result::Result<Option<EmailApiError>, ProcessingError> {
if cfg!(not(feature = "attachment_upload")) {
return Ok(None);
}
// Keep this parsed-attachment gate at least as permissive as the database claim filters.
let eligibility = attachment_upload_eligibility(attachments);
if !eligibility.documents && !eligibility.media {
return Ok(None);
}
// upload attachments to Macro
let (document_atts, media_atts) = tokio::try_join!(
async {
if eligibility.documents {
email_db_client::attachments::provider::upload::new_email_document_atts(
&ctx.db,
link.id,
&payload.provider_message_id,
)
.await
} else {
Ok(Vec::<AttachmentUploadMetadata>::new())
}
},
async {
if eligibility.media {
email_db_client::attachments::provider::upload::new_email_media_atts(
&ctx.db,
link.id,
&payload.provider_message_id,
)
.await
} else {
Ok(Vec::<AttachmentUploadMetadata>::new())
}
}
)
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to fetch attachments to insert".to_string()),
})
})?;
let mut attachments = document_atts;
attachments.extend(media_atts);
if !attachments.is_empty() {
let message_ids = attachments
.iter()
.map(|a| a.message_db_id)
.collect::<Vec<_>>();
let message_recipients =
email_db_client::contacts::get::fetch_db_recipients_in_bulk(&ctx.db, &message_ids)
.await
.map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context(
"Failed to fetch db recipients for thread attachment backfill"
.to_string(),
),
})
})?;
let mut uploads = Vec::with_capacity(attachments.len());
for attachment in attachments {
// get the email addresses of the recipients of the message
let recipient_emails: Vec<String> = message_recipients
.get(&attachment.message_db_id)
.map(|v| v.as_slice())
.unwrap_or(&[])
.iter()
.filter(|(_, recipient_type)| *recipient_type == EmailRecipientType::To)
.filter_map(|(contact, _)| contact.email_address.clone())
.collect();
let upload_destination = if matches!(
attachment.mime_type.split('/').next(),
Some("image" | "video")
) {
AttachmentUploadDestination::Sfs
} else {
AttachmentUploadDestination::Dss
};
uploads.push(AttachmentUploadArgs {
recipient_emails,
attachment_metadata: attachment,
backfill: false,
upload_destination,
});
}
return upload_claimed_attachments(&ctx.db, uploads, |args| async move {
let ctx_upload = UploadAttachmentContext {
db: &ctx.db,
email_api: &ctx.email_api,
dss_client: &ctx.dss_client,
sfs_client: &ctx.sfs_client,
system_properties_service: &ctx.system_properties_service,
link,
};
upload_attachment(ctx_upload, &args).await
})
.await;
}
Ok(None)
}
/// Uploads claimed attachments in order. Other upload failures are logged and
/// skipped, but a fetch the rate limit refused stops the run: the refused
/// attachment and the ones not yet tried lose their claims so a retry of the
/// upsert claims them again, and the refusal is returned.
async fn upload_claimed_attachments<F, Fut>(
db: &PgPool,
uploads: Vec<AttachmentUploadArgs>,
mut upload: F,
) -> result::Result<Option<EmailApiError>, ProcessingError>
where
F: FnMut(AttachmentUploadArgs) -> Fut,
Fut: Future<Output = result::Result<String, UploadAttachmentError>>,
{
let mut uploads = uploads.into_iter();
while let Some(args) = uploads.next() {
let attachment_id = args.attachment_metadata.attachment_db_id;
match upload(args).await {
Ok(_) => {}
Err(UploadAttachmentError::RateLimited(error)) => {
let unfinished = std::iter::once(attachment_id)
.chain(uploads.map(|args| args.attachment_metadata.attachment_db_id));
for attachment_id in unfinished {
email_db_client::attachments::provider::upload::release_upload_claim(
db,
attachment_id,
)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to release attachment upload claim"),
})
})?;
}
return Ok(Some(error));
}
// keep processing if it fails, best effort
Err(e) => tracing::error!("Failed to upload attachment to Macro: {e}"),
}
}
Ok(None)
}
#[derive(Debug, Default, PartialEq, Eq)]
struct AttachmentUploadEligibility {
documents: bool,
media: bool,
}
fn attachment_upload_eligibility(attachments: &[Attachment]) -> AttachmentUploadEligibility {
let mut eligibility = AttachmentUploadEligibility::default();
for attachment in attachments {
let Some(mime_type) = attachment.mime_type.as_deref() else {
continue;
};
eligibility.documents |= attachment_is_document(mime_type, attachment.filename.as_deref());
eligibility.media |= attachment_is_media(mime_type);
}
eligibility
}
#[tracing::instrument(skip(ctx, link, recipient_emails, sender_email))]
async fn handle_contacts_sync(
ctx: &PubSubContext,
link: &link::Link,
recipient_emails: &[String],
sender_email: Option<&str>,
is_sent: bool,
) -> result::Result<(), ProcessingError> {
if cfg!(not(feature = "contacts_sync")) {
return Ok(());
}
// Determine which emails to create connections to based on message direction
let connection_emails: Vec<&str> = if is_sent {
recipient_emails.iter().map(String::as_str).collect()
} else {
sender_email.into_iter().collect()
};
let connections = build_contact_connections(&link.macro_id, link.id, connection_emails);
if connections.is_empty() {
return Ok(());
}
ctx.contacts_ingress
.enqueue_contact_connections(connections)
.await
.map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::SqsEnqueueFailed,
source: anyhow::anyhow!("{e:?}").context(format!(
"Failed to enqueue contacts message for {}",
link.macro_id
)),
})
})?;
Ok(())
}
/// Process and insert email threads by handling attachments and images
#[tracing::instrument(skip(ctx))]
async fn fetch_and_insert_thread(
ctx: &PubSubContext,
payload: &UpsertMessagePayload,
link_id: Uuid,
provider_thread_id: &str,
) -> result::Result<(), ProcessingError> {
let messages = match ctx.email_api.get_thread(link_id, provider_thread_id).await {
Ok(fetched) => fetched.into_iter().map(|fetched| fetched.message).collect(),
Err(error) => {
return Err(handle_operation_error(
ctx,
link_id,
InboxSyncOperation::UpsertMessage(payload.clone()),
error,
)
.await);
}
};
let mut threads = vec![thread_from_normalized_messages(
link_id,
provider_thread_id,
messages,
)];
// process threads
process_threads_pre_insert(&mut threads).await;
// insert threads into db
for thread in threads.into_iter() {
threads::insert::insert_thread_and_messages(&ctx.db, thread, link_id)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to insert thread and messages".to_string()),
})
})?;
}
Ok(())
}
fn thread_from_normalized_messages(
link_id: Uuid,
provider_thread_id: &str,
mut messages: Vec<Message>,
) -> Thread {
messages.sort_by_key(|message| message.internal_date_ts);
let inbox_visible = messages.iter().any(|message| {
message.labels.iter().any(|label| {
label.provider_label_id == models_email::service::label::system_labels::INBOX
})
});
let is_read = messages.iter().all(|message| message.is_read);
let latest_inbound_message_ts = messages
.iter()
.rfind(|message| is_inbound(message))
.and_then(|message| message.internal_date_ts);
let latest_outbound_message_ts = messages
.iter()
.rfind(|message| is_outbound(message))
.and_then(|message| message.internal_date_ts);
let latest_non_spam_message_ts = messages
.iter()
.rfind(|message| !is_spam_or_trash(message))
.and_then(|message| message.internal_date_ts);
let thread_db_id = Uuid::now_v7();
for message in &mut messages {
message.thread_db_id = thread_db_id;
}
let now = chrono::Utc::now();
Thread {
db_id: thread_db_id,
provider_id: Some(provider_thread_id.to_string()),
link_id,
inbox_visible,
is_read,
latest_inbound_message_ts,
latest_outbound_message_ts,
latest_non_spam_message_ts,
created_at: now,
updated_at: now,
messages,
}
}
/// Process and insert message
#[tracing::instrument(skip(db))]
async fn process_and_insert_message(
db: &PgPool,
link_id: Uuid,
thread_db_id: Uuid,
message: &mut Message,
resync: Option<&ResyncSnapshot>,
) -> result::Result<(), ProcessingError> {
process_message_pre_insert(message).await;
email_db_client::messages::insert::insert_message(
db,
thread_db_id,
message,
link_id,
true,
resync,
)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to insert message".to_string()),
})
})
}
/// Notify downstream services about new message in a user's inbox
#[tracing::instrument(skip(ctx, link, new_message_provider_id))]
async fn notify_for_new_message(
ctx: &PubSubContext,
link: &link::Link,
new_message_provider_id: &str,
message_db_id: Uuid,
thread_id: Uuid,
) -> result::Result<(), ProcessingError> {
// notify user of new messages
send_notifications(ctx, link, new_message_provider_id).await?;
Ok(())
}
/// Send notifications for new inbound email messages
#[tracing::instrument(skip(ctx, link))]
async fn send_notifications(
ctx: &PubSubContext,
link: &link::Link,
new_message_provider_id: &str,
) -> result::Result<(), ProcessingError> {
if !ctx.notifications_enabled {
return Ok(());
}
let Some(message) = filter_notifiable_message(ctx, link, new_message_provider_id).await? else {
return Ok(());
};
let message_ids: Vec<Uuid> = vec![message.db_id];
let sender_contacts =
email_db_client::contacts::get::fetch_sender_contact_info(&ctx.db, &message_ids)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to fetch contact info".to_string()),
})
})?;
let sender_contact = message
.from_contact_id
.and_then(|from_id| sender_contacts.get(&from_id));
// Macro's own digest / system emails describe activity the user already
// has in-app notifications for; never raise a new-email notification
// (in-app row or push) on top of them.
if sender_contact.is_some_and(|contact| is_macro_notification_sender(&contact.email)) {
tracing::debug!(
thread_id = %message.thread_db_id,
"skipping new-email notification for Macro's own notification sender"
);
return Ok(());
}
let Some(tier) = new_email_tier(ctx, link, message.thread_db_id).await? else {
return Ok(());
};
let sender = sender_contact.map(|contact| {
contact
.name
.clone()
.unwrap_or_else(|| contact.email.clone())
});
let sender_id =
sender_contact.and_then(|contact| MacroUserIdStr::try_from_email(&contact.email).ok());
let notification = NewEmailMetadata {
sender,
to_email: link.email_address.0.as_ref().to_string(),
thread_id: message.thread_db_id.to_string(),
subject: message.subject.unwrap_or_default(),
snippet: message.snippet.unwrap_or_default(),
};
let primaries = macro_db_client::macro_user_links::get_primaries_for_link(
&ctx.db,
link.macro_id.as_ref(),
link.id,
)
.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 (staff_recipients, customer_recipients) = partition_email_push_recipients(recipient_ids);
let notification_entity =
EntityType::EmailThread.with_entity_string(message.thread_db_id.to_string());
if !staff_recipients.is_empty() {
let request = tier.notification_request(SendNotificationRequestBuilder {
notification_entity: notification_entity.clone(),
secondary_notification_entity: None,
notification: notification.clone(),
sender_id: sender_id.clone(),
recipient_ids: staff_recipients,
});
match tier {
NewEmailTier::Signal => publish_new_email_notification(ctx, request.with_apns()).await,
NewEmailTier::StaffInbox => publish_new_email_notification(ctx, request).await,
}
}
if !customer_recipients.is_empty() {
let request = tier.notification_request(SendNotificationRequestBuilder {
notification_entity,
secondary_notification_entity: None,
notification,
sender_id,
recipient_ids: customer_recipients,
});
publish_new_email_notification(ctx, request).await;
}
Ok(())
}
async fn publish_new_email_notification<U: serde::Serialize + Send + Sync + 'static>(
ctx: &PubSubContext,
request: SendNotificationRequest<'_, NewEmailMetadata, U>,
) {
if let Err(e) = ctx
.notification_ingress_service
.send_notification(request)
.await
{
tracing::error!(error=?e, "unable to send notification");
}
}
/// Split recipients so only `@macro.com` users are on the APNS path.
fn partition_email_push_recipients(
recipient_ids: HashSet<MacroUserIdStr<'static>>,
) -> (
HashSet<MacroUserIdStr<'static>>,
HashSet<MacroUserIdStr<'static>>,
) {
recipient_ids
.into_iter()
.partition(|id| id.is_macro_staff())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum NewEmailTier {
/// Everyone gets the in-app row; staff also get APNS.
Signal,
/// Staff dogfood: in-app row only.
StaffInbox,
}
impl NewEmailTier {
fn notification_request<'a>(
self,
builder: SendNotificationRequestBuilder<'a, NewEmailMetadata>,
) -> SendNotificationRequest<'a, NewEmailMetadata, ()> {
let request = builder.into_request();
match self {
Self::Signal => request.with_conn_gateway(),
// Persist the same row, but do not deliver a new-notification event
// over either GraphQL or the legacy gateway (both produce popups).
Self::StaffInbox => request,
}
}
}
fn signal_filter(thread_id: Uuid) -> Expr<EmailLiteral> {
Expr::and(
Expr::Literal(EmailLiteral::ThreadId(thread_id)),
Expr::and(
Expr::Literal(EmailLiteral::Importance(true)),
Expr::Literal(EmailLiteral::Shared(SharedEmailFilter::Exclude)),
),
)
}
async fn thread_in_inbox(
ctx: &PubSubContext,
link: &link::Link,
filter: Expr<EmailLiteral>,
) -> result::Result<bool, ProcessingError> {
let query = PreviewCursorQuery {
view: PreviewView::StandardLabel(PreviewViewStandardLabel::Inbox),
link_ids: vec![link.id],
limit: 1,
query: models_pagination::Query::Sort(
models_pagination::SimpleSortMethod::UpdatedAt,
Some(Arc::new(filter)),
),
team_id: None,
};
let previews = EmailPgRepo::new(ctx.db.clone())
.previews_for_view_cursor(query, link.macro_id.clone())
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: anyhow::Error::new(e).context("Failed to evaluate inbox membership"),
})
})?;
Ok(!previews.is_empty())
}
async fn new_email_tier(
ctx: &PubSubContext,
link: &link::Link,
thread_id: Uuid,
) -> result::Result<Option<NewEmailTier>, ProcessingError> {
if thread_in_inbox(ctx, link, signal_filter(thread_id)).await? {
return Ok(Some(NewEmailTier::Signal));
}
let staff_inbox = link.macro_id.is_macro_staff()
&& thread_in_inbox(ctx, link, Expr::Literal(EmailLiteral::ThreadId(thread_id))).await?;
Ok(staff_inbox.then_some(NewEmailTier::StaffInbox))
}
/// Loads the newly upserted message, returning `None` for messages that never
/// notify (sent or draft). Sender- and tier-based gating happens in
/// `send_notifications`.
#[tracing::instrument(skip(ctx, link))]
async fn filter_notifiable_message(
ctx: &PubSubContext,
link: &link::Link,
new_message_provider_id: &str,
) -> result::Result<Option<SimpleMessage>, ProcessingError> {
let new_message =
email_db_client::messages::get_simple_messages::get_simple_message_by_provider_and_link(
&ctx.db,
new_message_provider_id,
&link.id,
)
.await
.map_err(|e| {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to fetch simple message".to_string()),
})
})?;
let Some(new_message) = new_message else {
return Ok(None);
};
if new_message.is_sent || new_message.is_draft {
return Ok(None);
}
Ok(Some(new_message))
}