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-service): email upload attachment endpoint issues (#483)

Evan Hutnika7403c228e1c
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-e46bb839438b0dc805a6336d9ab0e64bad441f07222bfe922c9da81c7d2c3a47.json b/rust/cloud-storage/email_db_client/.sqlx/query-e46bb839438b0dc805a6336d9ab0e64bad441f07222bfe922c9da81c7d2c3a47.json
new file mode 100644
index 0000000000..edb2659776
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-e46bb839438b0dc805a6336d9ab0e64bad441f07222bfe922c9da81c7d2c3a47.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT document_id\n FROM document_email de\n INNER JOIN \"Document\" d on de.document_id = d.id\n INNER JOIN email_attachments ea on de.email_attachment_id = ea.id\n INNER JOIN email_messages em on ea.message_id = em.id\n WHERE em.link_id = $1 AND email_attachment_id = $2 AND d.\"deletedAt\" IS NULL\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "document_id",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "e46bb839438b0dc805a6336d9ab0e64bad441f07222bfe922c9da81c7d2c3a47"
+}
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-f253c2b856a3a264b27a08d68b36286344bdef6e504225847b094c87d7cdaa95.json b/rust/cloud-storage/email_db_client/.sqlx/query-f253c2b856a3a264b27a08d68b36286344bdef6e504225847b094c87d7cdaa95.json
deleted file mode 100644
index 643f654141..0000000000
--- a/rust/cloud-storage/email_db_client/.sqlx/query-f253c2b856a3a264b27a08d68b36286344bdef6e504225847b094c87d7cdaa95.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT document_id\n FROM document_email de\n INNER JOIN email_attachments ea on de.email_attachment_id = ea.id\n INNER JOIN email_messages em on ea.message_id = em.id\n WHERE em.link_id = $1 AND email_attachment_id = $2\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "document_id",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "f253c2b856a3a264b27a08d68b36286344bdef6e504225847b094c87d7cdaa95"
-}
diff --git a/rust/cloud-storage/email_db_client/src/attachments/provider/mod.rs b/rust/cloud-storage/email_db_client/src/attachments/provider/mod.rs
index 1cf24b5890..505f53299e 100644
--- a/rust/cloud-storage/email_db_client/src/attachments/provider/mod.rs
+++ b/rust/cloud-storage/email_db_client/src/attachments/provider/mod.rs
@@ -320,9 +320,10 @@ pub async fn get_document_id_by_attachment_id(
r#"
SELECT document_id
FROM document_email de
+ INNER JOIN "Document" d on de.document_id = d.id
INNER JOIN email_attachments ea on de.email_attachment_id = ea.id
INNER JOIN email_messages em on ea.message_id = em.id
- WHERE em.link_id = $1 AND email_attachment_id = $2
+ WHERE em.link_id = $1 AND email_attachment_id = $2 AND d."deletedAt" IS NULL
"#,
link_id,
email_attachment_id,
diff --git a/rust/cloud-storage/email_service/src/api/email/attachments/get_document_id.rs b/rust/cloud-storage/email_service/src/api/email/attachments/get_document_id.rs
index f0ebd63dc3..2e27ce7564 100644
--- a/rust/cloud-storage/email_service/src/api/email/attachments/get_document_id.rs
+++ b/rust/cloud-storage/email_service/src/api/email/attachments/get_document_id.rs
@@ -101,6 +101,7 @@ pub async fn handler(
&gmail_token,
&link,
&attachment_metadata,
+ false,
)
.await
.map_err(GetAttachmentDocumentIdError::UploadError)?;
diff --git a/rust/cloud-storage/email_service/src/pubsub/backfill/backfill_attachment.rs b/rust/cloud-storage/email_service/src/pubsub/backfill/backfill_attachment.rs
index e22e3a89b5..479211b64a 100644
--- a/rust/cloud-storage/email_service/src/pubsub/backfill/backfill_attachment.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/backfill/backfill_attachment.rs
@@ -27,6 +27,7 @@ pub async fn backfill_attachment(
access_token,
link,
&p.metadata,
+ true,
)
.await
.map_err(|e| {
diff --git a/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs b/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
index 68e8fe861c..0cdea7ac64 100644
--- a/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
@@ -228,6 +228,7 @@ async fn handle_attachment_upload(
gmail_access_token,
link,
&attachment,
+ false,
)
.await
{
diff --git a/rust/cloud-storage/email_service/src/util/upload_attachment.rs b/rust/cloud-storage/email_service/src/util/upload_attachment.rs
index bd0a644dac..f1b88d54b4 100644
--- a/rust/cloud-storage/email_service/src/util/upload_attachment.rs
+++ b/rust/cloud-storage/email_service/src/util/upload_attachment.rs
@@ -20,6 +20,7 @@ pub async fn upload_attachment(
access_token: &str,
link: &link::Link,
p: &AttachmentUploadMetadata,
+ backfill: bool,
) -> anyhow::Result<String> {
// 1. Check rate limits before making a Gmail API call.
check_gmail_rate_limit(
@@ -41,8 +42,10 @@ pub async fn upload_attachment(
let (file_name, file_type) = determine_file_metadata(p)?;
// 5. Create the document record in DSS and get a presigned URL for the upload.
- let dss_response =
- create_dss_document_record(dss_client, link, p, &hex_hash, &file_name, &file_type).await?;
+ let dss_response = create_dss_document_record(
+ dss_client, link, p, &hex_hash, &file_name, &file_type, backfill,
+ )
+ .await?;
// 6. Upload the attachment data to the presigned URL.
upload_data_to_presigned_url(&dss_response, attachment_data, &base64_hash).await?;
@@ -115,7 +118,12 @@ async fn create_dss_document_record(
hex_hash: &str,
file_name: &str,
file_type: &str,
+ backfill: bool,
) -> anyhow::Result<CreateDocumentResponse> {
+ // if we are backfilling, use the email timestamp. if it's an on-demand upload, use the current
+ // time so the document shows up at the top of soup views.
+ let created_at = backfill.then_some(p.internal_date_ts);
+
let request = CreateDocumentRequest {
id: None,
sha: hex_hash.to_string(),
@@ -127,7 +135,7 @@ async fn create_dss_document_record(
branched_from_version_id: None,
job_id: None,
project_id: None,
- created_at: Some(p.internal_date_ts),
+ created_at,
email_attachment_id: Some(p.attachment_db_id),
is_task: false,
};