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 crate::pubsub::context::PubSubContext;
use crate::util::upload_attachment::{
UploadAttachmentContext, UploadAttachmentError, upload_attachment,
};
use models_email::service::backfill::BackfillAttachmentPayload;
use models_email::service::link;
use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
use uuid::Uuid;
/// this step is invoked by the UpdateMetadata step. it uploads the specified attachment as a
/// Macro document for the user. first checks the attachment doesn't already exist by querying
/// document_email table before fetching and uploading the attachment data.
#[tracing::instrument(skip(ctx))]
pub async fn backfill_attachment(
ctx: &PubSubContext,
link: &link::Link,
p: &BackfillAttachmentPayload,
) -> Result<(), ProcessingError> {
// Check if a document for this attachment already exists before uploading.
if attachment_document_exists(
ctx,
link.id,
p.metadata.attachment_metadata.attachment_db_id,
)
.await?
{
return Ok(());
}
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,
};
let attachment_args = p.metadata.clone();
upload_attachment(ctx_upload, &attachment_args)
.await
.map_err(|e| match e {
UploadAttachmentError::RateLimited(error) => {
ProcessingError::Retryable(DetailedError {
reason: FailureReason::GmailApiRateLimited,
source: anyhow::Error::new(error).context("Failed to upload attachment"),
})
}
_ => ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::GmailApiFailed,
source: anyhow::Error::new(e).context("Failed to upload attachment"),
}),
})?;
Ok(())
}
/// Checks the database to see if a document has already been created for this attachment.
async fn attachment_document_exists(
ctx: &PubSubContext,
link_id: Uuid,
attachment_db_id: Uuid,
) -> Result<bool, ProcessingError> {
let document_id = email_db_client::attachments::provider::get_document_id_by_att_id_and_link(
&ctx.db,
link_id,
attachment_db_id,
)
.await
.map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
reason: FailureReason::DatabaseQueryFailed,
source: e.context("Failed to query for document email record"),
})
})?;
Ok(document_id.is_some())
}