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
chore(email-service): Refactor backfilling attachments job (#212)
Evan Hutnik31d7200ec171
diff --git a/rust/cloud-storage/email_service/Cargo.toml b/rust/cloud-storage/email_service/Cargo.tomlindex 5da01e36d0..3b2b7c3aa1 100644--- a/rust/cloud-storage/email_service/Cargo.toml+++ b/rust/cloud-storage/email_service/Cargo.toml@@ -11,11 +11,11 @@ path = "src/main.rs"[[bin]]name = "backfill_gmail_attachments"-path = "src/bin/backfill_gmail_attachments.rs"+path = "src/bin/backfill_gmail_attachments/backfill_gmail_attachments.rs"[[bin]]name = "backfill_search"-path = "src/bin/backfill_search.rs"+path = "src/bin/backfill_search/backfill_search.rs"[features]disable_attachment_upload = []diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments.rsdeleted file mode 100644index 4f057b25be..0000000000--- a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments.rs+++ /dev/null@@ -1,584 +0,0 @@-//! # Attachment Backfill Utility-//!-//! This binary is used to find and upload relevant email attachments for a given user account.-//! It fetches attachment metadata from the local database based on several heuristics,-//! downloads the actual attachment data from Gmail, and uploads it to a document storage service.-//!-//! ## Required Environment Variables:-//! - `DATABASE_URL`: The connection string for the PostgreSQL database.-//! - `DSS_URL`: The URL for the Document Storage Service.-//! - `GMAIL_ACCESS_TOKEN`: An OAuth token to access the user's Gmail account.-//! - `INTERNAL_AUTH_KEY`: An access token for authenticating with internal Macro services.-//! - `MACRO_ID_SOURCE`: The Macro ID of the user account to fetch relevant attachments from.-//! - `MACRO_ID_DESTINATION`: The destination Macro ID for document storage.-//! - `UPLOAD_CONCURRENCY`: Number of concurrent uploads to process (optional, defaults to 10).--use crate::processing::AttachmentProcessor;-use anyhow::Context;-use futures::{StreamExt, stream};-use std::sync::Arc;-use std::sync::atomic::{AtomicUsize, Ordering};--#[tokio::main]-async fn main() -> anyhow::Result<()> {- // 1. Load configuration from environment variables. Fails early if anything is missing.- println!("Loading configuration...");- let config = config::Config::from_env().context("Failed to load configuration")?;-- // 2. Establish a connection pool to the database.- println!("Connecting to the database...");- let db_pool = database::create_db_pool(&config.database_url, config.upload_concurrency as u32)- .await- .context("Failed to create database pool")?;-- // 3. Set up clients for external services.- let dss_client = document_storage_service_client::DocumentStorageServiceClient::new(- config.internal_auth_key.clone(),- config.dss_url.clone(),- );- let gmail_client = gmail_client::GmailClient::new("unused".to_string());-- // 4. Fetch the link_id associated with the user's account.- let link_id =- email_db_client::links::get::fetch_link_by_macro_id(&db_pool, &config.macro_id_source)- .await?- .ok_or_else(|| anyhow::anyhow!("No link found for macro account source"))?- .id;-- // 5. Fetch all relevant attachment metadata from the database.- println!("Fetching unique attachment metadata from database...");- let attachments = database::fetch_unique_attachments(&db_pool, link_id)- .await- .context("Failed to fetch attachment metadata")?;- println!("Found {} unique attachments to process.", attachments.len());-- // 6. Process and upload each attachment.- let processor = Arc::new(processing::AttachmentProcessor::new(- db_pool.clone(),- dss_client,- gmail_client,- config.gmail_access_token.clone(),- config.macro_id_destination.clone(),- ));-- let success_count = Arc::new(AtomicUsize::new(0));- let total_attachments = attachments.len();-- println!("Starting concurrent upload process...");-- stream::iter(attachments.into_iter().enumerate())- .for_each_concurrent(config.upload_concurrency, |(index, attachment)| {- let processor: Arc<AttachmentProcessor> = Arc::clone(&processor);- let success_count = Arc::clone(&success_count);-- async move {- match processor.upload(&attachment).await {- Ok(_) => {- success_count.fetch_add(1, Ordering::Relaxed);- println!("Successfully uploaded '{}' (index: {})", attachment.filename, index);- }- Err(e) => {- // ignore weird file types. annoying game of whack a mole- if e.to_string().contains("file extension") {- println!(- "Skipping '{}' (index: {}) due to unsupported mime type {}",- attachment.filename, index, attachment.mime_type- );- return;- }- panic!(- "Failed to upload attachment - filename: {}, provider_attachment_id: {}, provider_message_id: {}, index: {}, error: {:?}",- attachment.filename,- attachment.provider_attachment_id,- attachment.email_provider_id,- index,- e- );- }- }- }- })- .await;-- let final_success_count = success_count.load(Ordering::SeqCst);- println!(- "Processing complete. Successfully uploaded {} out of {} attachments.",- final_success_count, total_attachments- );-- Ok(())-}--// --- Data Models -----mod models {- use sqlx::FromRow;- use sqlx::types::chrono::{DateTime, Utc};- use uuid::Uuid;-- /// Represents the metadata for an email attachment stored in our database.- #[derive(Clone, Debug, FromRow)]- pub struct AttachmentMetadata {- pub attachment_db_id: Uuid,- pub email_provider_id: String,- pub provider_attachment_id: String,- pub mime_type: String,- pub filename: String,- pub internal_date_ts: DateTime<Utc>,- }-}--// --- Configuration -----mod config {- use anyhow::{Context, Result};-- /// Holds all configuration loaded from environment variables.- pub struct Config {- pub dss_url: String,- pub gmail_access_token: String,- pub internal_auth_key: String,- pub macro_id_destination: String,- pub macro_id_source: String,- pub database_url: String,- pub upload_concurrency: usize,- }-- impl Config {- /// Creates a new `Config` instance by reading from environment variables.- /// Returns an error if any required variable is not set.- pub fn from_env() -> Result<Self> {- let upload_concurrency =- std::env::var("UPLOAD_CONCURRENCY").context("UPLOAD_CONCURRENCY not set")?;- let upload_concurrency = upload_concurrency- .parse::<usize>()- .context("UPLOAD_CONCURRENCY is not a number")?;-- Ok(Self {- dss_url: std::env::var("DSS_URL").context("DSS_URL not set")?,- gmail_access_token: std::env::var("GMAIL_ACCESS_TOKEN")- .context("GMAIL_ACCESS_TOKEN not set")?,- internal_auth_key: std::env::var("INTERNAL_AUTH_KEY")- .context("INTERNAL_AUTH_KEY not set")?,- macro_id_destination: std::env::var("MACRO_ID_DESTINATION")- .context("MACRO_ID_DESTINATION not set")?,- macro_id_source: std::env::var("MACRO_ID_SOURCE")- .context("MACRO_ID_SOURCE not set")?,- database_url: std::env::var("DATABASE_URL").context("DATABASE_URL not set")?,- upload_concurrency,- })- }- }-}--// --- Database Operations -----mod database {- use super::models::AttachmentMetadata;- use anyhow::Context;- use email_db_client::attachments::provider::upload_filters::{- ATTACHMENT_MIME_TYPE_FILTERS, ATTACHMENT_WHITELISTED_DOMAINS,- };- use sqlx::PgPool;- use sqlx::postgres::PgPoolOptions;- use uuid::Uuid;-- /// Attachments for a thread should be uploaded if any message in the- /// thread meets any of the following criteria:- /// 1. the user sent the message- /// 2. the message has the IMPORTANT label- /// 3. the message came from someone with the same domain as the user- /// 4. the domain the email was sent from is part of the whitelisted domains- /// 5. the user has previously sent a message to any participant in the thread- ///- ///- ///- /// Combined query for conditions 1, 2, 3, and 4 - similar to fetch_insertable_attachments_for_new_email- /// but modified to work for all threads for a given link_id instead of a specific message- const COMBINED_CONDITIONS_QUERY_PREFIX: &str = r#"- SELECT DISTINCT- a.id AS attachment_db_id,- m.provider_id as email_provider_id,- a.provider_attachment_id,- a.mime_type,- a.filename,- m.internal_date_ts- FROM email_attachments a- JOIN email_messages m ON a.message_id = m.id- JOIN email_threads t ON m.thread_id = t.id- WHERE t.link_id = $1- AND a.filename IS NOT NULL- "#;-- const COMBINED_CONDITIONS_QUERY_SUFFIX: &str = r#"- AND EXISTS ( -- only fetch if at least one message in the thread meets any of the criteria- SELECT 1- FROM email_messages m2- LEFT JOIN email_message_labels ml ON m2.id = ml.message_id- LEFT JOIN email_labels l ON ml.label_id = l.id- LEFT JOIN email_contacts c ON m2.from_contact_id = c.id- JOIN email_links link ON t.link_id = link.id- WHERE m2.thread_id = t.id -- check against the thread- AND (- m2.is_sent = true -- condition 1- OR l.name = 'IMPORTANT' -- condition 2- OR (- -- condition 3: same domain- c.email_address IS NOT NULL- AND RIGHT(c.email_address, LENGTH(RIGHT(link.email_address,- LENGTH(link.email_address) - POSITION('@' IN link.email_address)))) =- RIGHT(link.email_address, LENGTH(link.email_address) - POSITION('@' IN link.email_address))- )- "#;-- const COMBINED_CONDITIONS_QUERY_END: &str = r#"- )- )- ORDER BY m.internal_date_ts DESC- "#;-- /// Condition 5 Query: Thread involves a recipient the user has previously sent mail to.- const PREVIOUSLY_CONTACTED_CONDITION_PREFIX: &str = r#"-WITH--- Step 1: Get the user's own email address from the link_id. This is our exclusion criteria.-user_email AS (- SELECT- email_address- FROM- public.email_links- WHERE- id = $1 -- Your link_id-),---- Step 2: Create a distinct list of OTHER people this user has ever sent mail to.-previously_contacted_emails AS (- SELECT DISTINCT- ec.email_address- FROM- public.email_messages em- JOIN- public.email_message_recipients emr ON em.id = emr.message_id- JOIN- public.email_contacts ec ON emr.contact_id = ec.id- WHERE- em.link_id = $1- AND em.is_sent = true- AND ec.email_address != (SELECT email_address FROM user_email)-),---- Step 3: For each thread, create a complete list of OTHER participant email addresses.-thread_participants AS (- -- Get all senders in each thread (excluding the user)- SELECT DISTINCT- em.thread_id,- ec.email_address- FROM- public.email_messages em- JOIN- public.email_contacts ec ON em.from_contact_id = ec.id- WHERE- em.link_id = $1- AND ec.email_address != (SELECT email_address FROM user_email)-- UNION-- -- Get all recipients in each thread (excluding the user)- SELECT DISTINCT- em.thread_id,- ec.email_address- FROM- public.email_messages em- JOIN- public.email_message_recipients emr ON em.id = emr.message_id- JOIN- public.email_contacts ec ON emr.contact_id = ec.id- WHERE- em.link_id = $1- AND ec.email_address != (SELECT email_address FROM user_email)-)---- Final Step: Select attachments from threads where AT LEAST ONE of the OTHER participants--- is in the list of OTHER previously_contacted_emails.-SELECT- a.id AS attachment_db_id,- m.provider_id as email_provider_id,- a.provider_attachment_id,- a.filename,- a.mime_type,- m.internal_date_ts-FROM- public.email_attachments a-JOIN- public.email_messages m ON a.message_id = m.id-JOIN- public.email_contacts from_contact ON m.from_contact_id = from_contact.id-WHERE- m.thread_id IN (- SELECT DISTINCT- tp.thread_id- FROM- thread_participants tp- INNER JOIN- previously_contacted_emails pce ON tp.email_address = pce.email_address- )- -- Apply standard filters at the very end- AND a.filename IS NOT NULL- "#;-- const PREVIOUSLY_CONTACTED_CONDITION_SUFFIX: &str = r#"-ORDER BY- m.internal_date_ts DESC;- "#;-- /// Creates and returns a new PostgreSQL connection pool.- pub async fn create_db_pool(- database_url: &str,- min_connections: u32,- ) -> anyhow::Result<PgPool> {- PgPoolOptions::new()- .min_connections(min_connections)- .max_connections(60)- .connect(database_url)- .await- .context("Could not connect to db")- }-- /// Fetches attachments from the database that match one of several conditions.- /// Uses a combined query for conditions 1-4, and a separate query for condition 5.- pub async fn fetch_unique_attachments(- db: &PgPool,- link_id: Uuid,- ) -> anyhow::Result<Vec<AttachmentMetadata>> {- let mut attachments = Vec::new();-- // Combined query for conditions 1-4: User sent, IMPORTANT label, same domain, whitelisted domains- // Build the query by concatenating the parts with the filter constants- let combined_query = format!(- "{}{}{}{}{}",- COMBINED_CONDITIONS_QUERY_PREFIX,- ATTACHMENT_MIME_TYPE_FILTERS,- COMBINED_CONDITIONS_QUERY_SUFFIX,- ATTACHMENT_WHITELISTED_DOMAINS,- COMBINED_CONDITIONS_QUERY_END- );-- let rows_combined = sqlx::query_as::<_, AttachmentMetadata>(&combined_query)- .bind(link_id)- .fetch_all(db)- .await- .with_context(|| "Failed to fetch attachments for conditions 1-4 (combined query)")?;-- println!("Conditions 1-4 returned {} rows", rows_combined.len());- attachments.extend(rows_combined);-- // Query for condition 5: Previously contacted participants- let previously_contacted_query = format!(- "{}{}{}",- PREVIOUSLY_CONTACTED_CONDITION_PREFIX,- ATTACHMENT_MIME_TYPE_FILTERS,- PREVIOUSLY_CONTACTED_CONDITION_SUFFIX- );-- let rows5 = sqlx::query_as::<_, AttachmentMetadata>(&previously_contacted_query)- .bind(link_id)- .fetch_all(db)- .await- .with_context(- || "Failed to fetch attachments for condition 5 (previously contacted)",- )?;-- println!("Condition 5 returned {} rows", rows5.len());-- attachments.extend(rows5);-- // Deduplicate by attachment_db_id- let mut unique_attachments = std::collections::HashMap::new();- for attachment in attachments {- unique_attachments.insert(attachment.attachment_db_id, attachment);- }-- let mut result: Vec<AttachmentMetadata> = unique_attachments.into_values().collect();- result.sort_by(|a, b| b.internal_date_ts.cmp(&a.internal_date_ts));-- println!("Total unique rows: {}", result.len());-- Ok(result)- }-}--// --- Attachment Processing and Uploading -----mod processing {- use super::models::AttachmentMetadata;- use anyhow::{Context, bail};- use base64::{Engine as _, engine::general_purpose::STANDARD};- use model::document::response::CreateDocumentRequest;- use sha2::{Digest, Sha256};- use sqlx::PgPool;- use tracing::instrument;-- /// A helper struct to manage clients and tokens required for processing.- pub struct AttachmentProcessor {- db: PgPool,- dss_client: document_storage_service_client::DocumentStorageServiceClient,- gmail_client: gmail_client::GmailClient,- gmail_access_token: String,- macro_id_destination: String,- }-- impl AttachmentProcessor {- pub fn new(- db: PgPool,- dss_client: document_storage_service_client::DocumentStorageServiceClient,- gmail_client: gmail_client::GmailClient,- gmail_access_token: String,- macro_id_destination: String,- ) -> Self {- Self {- db,- dss_client,- gmail_client,- gmail_access_token,- macro_id_destination,- }- }-- /// Orchestrates the full upload process for a single attachment.- #[instrument(skip(self), fields(file_name = %attachment.filename, mime_type = %attachment.mime_type))]- pub async fn upload(&self, attachment: &AttachmentMetadata) -> anyhow::Result<()> {- let exists = email_db_client::attachments::provider::document_email_record_exists(- &self.db,- attachment.attachment_db_id,- )- .await?;- if exists {- println!(- "Attachment {} already exists in DSS, skipping",- attachment.attachment_db_id- );- return Ok(());- }-- // 1. Fetch attachment data from Gmail.- let data = self.fetch_gmail_data(attachment).await?;-- // 2. Calculate hashes required for validation.- let (sha256_hex, sha256_base64) = Self::calculate_hashes(&data);-- // 3. Get a presigned URL from the Document Storage Service.- let (presigned_url, content_type) =- self.get_presigned_url(attachment, &sha256_hex).await?;-- // 4. Upload the data to the presigned URL (e.g., S3).- self.upload_to_storage(&presigned_url, &content_type, &sha256_base64, data)- .await?;-- Ok(())- }-- /// Fetches the raw attachment data from the Gmail API.- async fn fetch_gmail_data(- &self,- attachment: &AttachmentMetadata,- ) -> anyhow::Result<Vec<u8>> {- self.gmail_client- .get_attachment_data(- &self.gmail_access_token,- &attachment.email_provider_id,- &attachment.provider_attachment_id,- )- .await- .context("Failed to get attachment data from Gmail")- }-- /// Calculates the hex and base64 encoded SHA256 hash of the attachment data.- fn calculate_hashes(data: &[u8]) -> (String, String) {- let mut hasher = Sha256::new();- hasher.update(data);- let hash_bytes = hasher.finalize();-- let hex_hash = format!("{:x}", hash_bytes);- let base64_hash = STANDARD.encode(hash_bytes);-- (hex_hash, base64_hash)- }-- /// Requests a pre-signed upload URL from the Document Storage Service.- async fn get_presigned_url(- &self,- attachment: &AttachmentMetadata,- sha256_hex: &str,- ) -> anyhow::Result<(String, String)> {- let file_name = attachment- .filename- .split('.')- .next()- .unwrap_or(&attachment.filename)- .to_string();-- let file_type = mime_guess::get_mime_extensions_str(&attachment.mime_type)- .and_then(|exts| exts.first().map(|s| s.to_string()))- .context("Could not determine file extension from MIME type")?;-- let dss_response = self- .dss_client- .create_document_internal(- CreateDocumentRequest {- id: None,- sha: sha256_hex.to_string(),- document_name: file_name,- file_type: Some(file_type),- mime_type: Some(attachment.mime_type.clone()),- document_family_id: None,- branched_from_id: None,- branched_from_version_id: None,- job_id: None,- project_id: None,- created_at: Some(attachment.internal_date_ts),- email_attachment_id: Some(attachment.attachment_db_id),- },- &self.macro_id_destination,- )- .await- .context("DSS create_document call failed")?;-- let presigned_url = dss_response- .data- .document_response- .presigned_url- .context("DSS response did not include a presigned URL")?;-- Ok((presigned_url, dss_response.data.content_type))- }-- /// Performs the final PUT request to upload the file data to cloud storage.- async fn upload_to_storage(- &self,- url: &str,- content_type: &str,- sha256_base64: &str,- data: Vec<u8>,- ) -> anyhow::Result<()> {- let response = reqwest::Client::new()- .put(url)- .header("content-type", content_type)- .header("x-amz-checksum-sha256", sha256_base64)- .body(data)- .send()- .await- .context("HTTP request to presigned URL failed")?;-- if !response.status().is_success() {- let status = response.status();- let body = response.text().await.unwrap_or_default();- bail!(- "Upload to storage failed with status {}. Body: {}",- status,- body- );- }-- Ok(())- }- }-}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/README.md b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/README.mdnew file mode 100644index 0000000000..c24114f7df--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/README.md@@ -0,0 +1,18 @@+# Attachment Backfill Utility++This binary is used to find and upload relevant email attachments for a given user account to DSS.+It fetches attachment metadata from the local database based on several heuristics,+downloads the actual attachment data from Gmail, and uploads it to document storage service.++## Required Environment Variables:++- `DATABASE_URL`: The connection string for the PostgreSQL database.+- `DSS_URL`: The URL for the Document Storage Service.+- `INTERNAL_AUTH_KEY`: The access token for authenticating with internal Macro services.+- `MACRO_IDS`: The Macro IDs of the user accounts to backfill attachments for+- `UPLOAD_CONCURRENCY`: Number of concurrent uploads to process (optional, defaults to 10).+- `FUSIONAUTH_API_KEY`: The API key for authenticating with FusionAuth.+- `FUSIONAUTH_BASE_URL`: The base URL for the FusionAuth service.+- `FUSIONAUTH_IDENTITY_PROVIDER_ID`: The identity provider ID for FusionAuth.+- `GMAIL_CLIENT_ID`: The client ID for Gmail OAuth.+- `GMAIL_CLIENT_SECRET`: The client secret for Gmail OAuth.diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/auth.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/auth.rsnew file mode 100644index 0000000000..55062cd5e4--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/auth.rs@@ -0,0 +1,74 @@+use crate::config::Config;+use anyhow::Context;++/// Gets a fresh Gmail access token by:+/// 1. Looking up the user in FusionAuth+/// 2. Getting their identity provider link with refresh token+/// 3. Using that refresh token to get a new Gmail access token+pub async fn get_gmail_access_token(config: &Config, macro_id: &str) -> anyhow::Result<String> {+ let client = reqwest::Client::builder().build()?;++ // First request: Get user by email+ let mut headers = reqwest::header::HeaderMap::new();+ headers.insert("Authorization", config.fusionauth_api_key.parse()?);++ let user_email = macro_id.strip_prefix("macro|").unwrap();++ let url = format!(+ "{}/api/user?email={}",+ config.fusionauth_base_url, user_email+ );+ let request = client.request(reqwest::Method::GET, &url).headers(headers);++ let response = request.send().await?;+ let user_response: serde_json::Value = response.json().await?;++ // Extract user.id from the first response+ let user_id = user_response["user"]["id"]+ .as_str()+ .context("Failed to extract user.id from first response")?;++ // Second request: Get identity provider links using the user.id+ let mut headers = reqwest::header::HeaderMap::new();+ headers.insert("Authorization", config.fusionauth_api_key.parse()?);++ let url = format!(+ "{}/api/identity-provider/link?identityProviderId={}&userId={}",+ config.fusionauth_base_url, config.fusionauth_identity_provider_id, user_id+ );+ let request = client.request(reqwest::Method::GET, &url).headers(headers);++ let response = request.send().await?;+ let identity_response: serde_json::Value = response.json().await?;++ // Extract identityProviderLinks[0].token from the second response+ let refresh_token = identity_response["identityProviderLinks"][0]["token"]+ .as_str()+ .context("Failed to extract identityProviderLinks[0].token from second response")?;++ // Third request: Use the extracted token as refresh_token+ let mut headers = reqwest::header::HeaderMap::new();+ headers.insert("Content-Type", "application/json".parse()?);++ let data = serde_json::json!({+ "client_id": config.gmail_client_id,+ "client_secret": config.gmail_client_secret,+ "refresh_token": refresh_token,+ "grant_type": "refresh_token"+ });++ let request = client+ .request(reqwest::Method::POST, "https://oauth2.googleapis.com/token")+ .headers(headers)+ .json(&data);++ let response = request.send().await?;+ let token_response: serde_json::Value = response.json().await?;++ // Extract the access token from the response+ let access_token = token_response["access_token"]+ .as_str()+ .context("Failed to extract access_token from Gmail OAuth response")?;++ Ok(access_token.to_string())+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/backfill_gmail_attachments.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/backfill_gmail_attachments.rsnew file mode 100644index 0000000000..f66e11446c--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/backfill_gmail_attachments.rs@@ -0,0 +1,78 @@+//! # Attachment Backfill Utility+//!+//! This binary is used to find and upload relevant email attachments for a given user account.+//! It fetches attachment metadata from the local database based on several heuristics,+//! downloads the actual attachment data from Gmail, and uploads it to a document storage service.+//!+//! ## Required Environment Variables:+//! - `DATABASE_URL`: The connection string for the PostgreSQL database.+//! - `DSS_URL`: The URL for the Document Storage Service.+//! - `INTERNAL_AUTH_KEY`: An access token for authenticating with internal Macro services.+//! - `MACRO_IDS`: The Macro IDs of the user accounts to backfill attachments for+//! - `UPLOAD_CONCURRENCY`: Number of concurrent uploads to process (optional, defaults to 10).+//! - `FUSIONAUTH_API_KEY`: The API key for authenticating with FusionAuth.+//! - `FUSIONAUTH_BASE_URL`: The base URL for the FusionAuth service.+//! - `FUSIONAUTH_IDENTITY_PROVIDER_ID`: The identity provider ID for FusionAuth.+//! - `GMAIL_CLIENT_ID`: The client ID for Gmail OAuth.+//! - `GMAIL_CLIENT_SECRET`: The client secret for Gmail OAuth.++mod auth;+mod config;+mod database;+mod process;+mod upload;++use anyhow::Context;+use macro_entrypoint::MacroEntrypoint;+#[tokio::main]+async fn main() -> anyhow::Result<()> {+ println!("Loading configuration...");+ MacroEntrypoint::default().init();+ let config = config::Config::from_env().context("Failed to load configuration")?;++ println!("Connecting to the database...");+ let db_pool = database::create_db_pool(&config.database_url, config.upload_concurrency as u32)+ .await+ .context("Failed to create database pool")?;++ let dss_client = document_storage_service_client::DocumentStorageServiceClient::new(+ config.internal_auth_key.clone(),+ config.dss_url.clone(),+ );+ let gmail_client = gmail_client::GmailClient::new("unused".to_string());++ let macro_ids: Vec<String> = config+ .macro_ids+ .split(',')+ .map(|id| id.trim().to_string())+ .collect();++ println!("Processing {} macro IDs: {:?}", macro_ids.len(), macro_ids);++ for (index, macro_id) in macro_ids.iter().enumerate() {+ println!(+ "\n=== Processing macro ID {} ({}/{}) ===",+ macro_id,+ index + 1,+ macro_ids.len()+ );++ match process::process_macro_id(&config, &db_pool, &dss_client, &gmail_client, macro_id)+ .await+ {+ Ok((success_count, total_attachments)) => {+ println!(+ "Completed processing for {}. Successfully uploaded {} out of {} attachments.",+ macro_id, success_count, total_attachments+ );+ }+ Err(e) => {+ println!("Failed to process macro ID {}: {:?}", macro_id, e);+ // Continue with next macro ID instead of failing completely+ }+ }+ }++ println!("\n=== All macro IDs processed ===");+ Ok(())+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/config.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/config.rsnew file mode 100644index 0000000000..6668a9ab79--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/config.rs@@ -0,0 +1,45 @@+use anyhow::Context;++/// Holds all configuration loaded from environment variables.+pub struct Config {+ pub dss_url: String,+ pub internal_auth_key: String,+ pub macro_ids: String,+ pub database_url: String,+ pub upload_concurrency: usize,+ pub fusionauth_api_key: String,+ pub fusionauth_base_url: String,+ pub fusionauth_identity_provider_id: String,+ pub gmail_client_id: String,+ pub gmail_client_secret: String,+}++impl Config {+ /// Creates a new `Config` instance by reading from environment variables.+ /// Returns an error if any required variable is not set.+ pub fn from_env() -> anyhow::Result<Self> {+ let upload_concurrency =+ std::env::var("UPLOAD_CONCURRENCY").context("UPLOAD_CONCURRENCY not set")?;+ let upload_concurrency = upload_concurrency+ .parse::<usize>()+ .context("UPLOAD_CONCURRENCY is not a number")?;++ Ok(Self {+ dss_url: std::env::var("DSS_URL").context("DSS_URL not set")?,+ internal_auth_key: std::env::var("INTERNAL_AUTH_KEY")+ .context("INTERNAL_AUTH_KEY not set")?,+ macro_ids: std::env::var("MACRO_IDS").context("MACRO_IDS not set")?, // Changed+ database_url: std::env::var("DATABASE_URL").context("DATABASE_URL not set")?,+ upload_concurrency,+ fusionauth_api_key: std::env::var("FUSIONAUTH_API_KEY")+ .context("FUSIONAUTH_API_KEY not set")?,+ fusionauth_base_url: std::env::var("FUSIONAUTH_BASE_URL")+ .context("FUSIONAUTH_BASE_URL not set")?,+ fusionauth_identity_provider_id: std::env::var("FUSIONAUTH_IDENTITY_PROVIDER_ID")+ .context("FUSIONAUTH_IDENTITY_PROVIDER_ID not set")?,+ gmail_client_id: std::env::var("GMAIL_CLIENT_ID").context("GMAIL_CLIENT_ID not set")?,+ gmail_client_secret: std::env::var("GMAIL_CLIENT_SECRET")+ .context("GMAIL_CLIENT_SECRET not set")?,+ })+ }+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/database.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/database.rsnew file mode 100644index 0000000000..254680276e--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/database.rs@@ -0,0 +1,225 @@+use anyhow::Context;+use email_db_client::attachments::provider::upload_filters::{+ ATTACHMENT_MIME_TYPE_FILTERS, ATTACHMENT_WHITELISTED_DOMAINS,+};+use models_email::service::attachment::AttachmentUploadMetadata;+use sqlx::PgPool;+use sqlx::postgres::PgPoolOptions;+use uuid::Uuid;++/// Attachments for a thread should be uploaded if any message in the+/// thread meets any of the following criteria:+/// 1. the user sent the message+/// 2. the message has the IMPORTANT label+/// 3. the message came from someone with the same domain as the user+/// 4. the domain the email was sent from is part of the whitelisted domains+/// 5. the user has previously sent a message to any participant in the thread+///+///+///+/// Combined query for conditions 1, 2, 3, and 4 - similar to fetch_insertable_attachments_for_new_email+/// but modified to work for all threads for a given link_id instead of a specific message+const COMBINED_CONDITIONS_QUERY_PREFIX: &str = r#"+ SELECT DISTINCT+ a.id AS attachment_db_id,+ m.provider_id as email_provider_id,+ a.provider_attachment_id,+ a.mime_type,+ a.filename,+ m.internal_date_ts+ FROM email_attachments a+ JOIN email_messages m ON a.message_id = m.id+ JOIN email_threads t ON m.thread_id = t.id+ WHERE t.link_id = $1+ AND a.filename IS NOT NULL+ "#;++const COMBINED_CONDITIONS_QUERY_SUFFIX: &str = r#"+ AND EXISTS ( -- only fetch if at least one message in the thread meets any of the criteria+ SELECT 1+ FROM email_messages m2+ LEFT JOIN email_message_labels ml ON m2.id = ml.message_id+ LEFT JOIN email_labels l ON ml.label_id = l.id+ LEFT JOIN email_contacts c ON m2.from_contact_id = c.id+ JOIN email_links link ON t.link_id = link.id+ WHERE m2.thread_id = t.id -- check against the thread+ AND (+ m2.is_sent = true -- condition 1+ OR l.name = 'IMPORTANT' -- condition 2+ OR (+ -- condition 3: same domain+ c.email_address IS NOT NULL+ AND RIGHT(c.email_address, LENGTH(RIGHT(link.email_address,+ LENGTH(link.email_address) - POSITION('@' IN link.email_address)))) =+ RIGHT(link.email_address, LENGTH(link.email_address) - POSITION('@' IN link.email_address))+ )+ "#;++const COMBINED_CONDITIONS_QUERY_END: &str = r#"+ )+ )+ ORDER BY m.internal_date_ts DESC+ "#;++/// Condition 5 Query: Thread involves a recipient the user has previously sent mail to.+const PREVIOUSLY_CONTACTED_CONDITION_PREFIX: &str = r#"+WITH+-- Step 1: Get the user's own email address from the link_id. This is our exclusion criteria.+user_email AS (+ SELECT+ email_address+ FROM+ public.email_links+ WHERE+ id = $1 -- Your link_id+),++-- Step 2: Create a distinct list of OTHER people this user has ever sent mail to.+previously_contacted_emails AS (+ SELECT DISTINCT+ ec.email_address+ FROM+ public.email_messages em+ JOIN+ public.email_message_recipients emr ON em.id = emr.message_id+ JOIN+ public.email_contacts ec ON emr.contact_id = ec.id+ WHERE+ em.link_id = $1+ AND em.is_sent = true+ AND ec.email_address != (SELECT email_address FROM user_email)+),++-- Step 3: For each thread, create a complete list of OTHER participant email addresses.+thread_participants AS (+ -- Get all senders in each thread (excluding the user)+ SELECT DISTINCT+ em.thread_id,+ ec.email_address+ FROM+ public.email_messages em+ JOIN+ public.email_contacts ec ON em.from_contact_id = ec.id+ WHERE+ em.link_id = $1+ AND ec.email_address != (SELECT email_address FROM user_email)++ UNION++ -- Get all recipients in each thread (excluding the user)+ SELECT DISTINCT+ em.thread_id,+ ec.email_address+ FROM+ public.email_messages em+ JOIN+ public.email_message_recipients emr ON em.id = emr.message_id+ JOIN+ public.email_contacts ec ON emr.contact_id = ec.id+ WHERE+ em.link_id = $1+ AND ec.email_address != (SELECT email_address FROM user_email)+)++-- Final Step: Select attachments from threads where AT LEAST ONE of the OTHER participants+-- is in the list of OTHER previously_contacted_emails.+SELECT+ a.id AS attachment_db_id,+ m.provider_id as email_provider_id,+ a.provider_attachment_id,+ a.filename,+ a.mime_type,+ m.internal_date_ts+FROM+ public.email_attachments a+JOIN+ public.email_messages m ON a.message_id = m.id+JOIN+ public.email_contacts from_contact ON m.from_contact_id = from_contact.id+WHERE+ m.thread_id IN (+ SELECT DISTINCT+ tp.thread_id+ FROM+ thread_participants tp+ INNER JOIN+ previously_contacted_emails pce ON tp.email_address = pce.email_address+ )+ -- Apply standard filters at the very end+ AND a.filename IS NOT NULL+ "#;++const PREVIOUSLY_CONTACTED_CONDITION_SUFFIX: &str = r#"+ORDER BY+ m.internal_date_ts DESC;+ "#;++/// Creates and returns a new PostgreSQL connection pool.+pub async fn create_db_pool(database_url: &str, min_connections: u32) -> anyhow::Result<PgPool> {+ PgPoolOptions::new()+ .min_connections(min_connections)+ .max_connections(60)+ .connect(database_url)+ .await+ .context("Could not connect to db")+}++/// Fetches attachments from the database that match one of several conditions.+/// Uses a combined query for conditions 1-4, and a separate query for condition 5.+pub async fn fetch_unique_attachments(+ db: &PgPool,+ link_id: Uuid,+) -> anyhow::Result<Vec<AttachmentUploadMetadata>> {+ let mut attachments = Vec::new();++ // Combined query for conditions 1-4: User sent, IMPORTANT label, same domain, whitelisted domains+ // Build the query by concatenating the parts with the filter constants+ let combined_query = format!(+ "{}{}{}{}{}",+ COMBINED_CONDITIONS_QUERY_PREFIX,+ ATTACHMENT_MIME_TYPE_FILTERS,+ COMBINED_CONDITIONS_QUERY_SUFFIX,+ ATTACHMENT_WHITELISTED_DOMAINS,+ COMBINED_CONDITIONS_QUERY_END+ );++ let rows_combined = sqlx::query_as::<_, AttachmentUploadMetadata>(&combined_query)+ .bind(link_id)+ .fetch_all(db)+ .await+ .with_context(|| "Failed to fetch attachments for conditions 1-4 (combined query)")?;++ println!("Conditions 1-4 returned {} rows", rows_combined.len());+ attachments.extend(rows_combined);++ // Query for condition 5: Previously contacted participants+ let previously_contacted_query = format!(+ "{}{}{}",+ PREVIOUSLY_CONTACTED_CONDITION_PREFIX,+ ATTACHMENT_MIME_TYPE_FILTERS,+ PREVIOUSLY_CONTACTED_CONDITION_SUFFIX+ );++ let rows5 = sqlx::query_as::<_, AttachmentUploadMetadata>(&previously_contacted_query)+ .bind(link_id)+ .fetch_all(db)+ .await+ .with_context(|| "Failed to fetch attachments for condition 5 (previously contacted)")?;++ println!("Condition 5 returned {} rows", rows5.len());++ attachments.extend(rows5);++ // Deduplicate by attachment_db_id+ let mut unique_attachments = std::collections::HashMap::new();+ for attachment in attachments {+ unique_attachments.insert(attachment.attachment_db_id, attachment);+ }++ let mut result: Vec<AttachmentUploadMetadata> = unique_attachments.into_values().collect();+ result.sort_by(|a, b| b.internal_date_ts.cmp(&a.internal_date_ts));++ println!("Total unique rows: {}", result.len());++ Ok(result)+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rsnew file mode 100644index 0000000000..cfe491dfe6--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs@@ -0,0 +1,96 @@+use crate::upload::AttachmentProcessor;+use crate::{auth, config, database, upload};+use anyhow::Context;+use futures::{StreamExt, stream};+use std::sync::Arc;+use std::sync::atomic::{AtomicUsize, Ordering};++/// Process attachments for a single macro ID+pub async fn process_macro_id(+ config: &config::Config,+ db_pool: &sqlx::PgPool,+ dss_client: &document_storage_service_client::DocumentStorageServiceClient,+ gmail_client: &gmail_client::GmailClient,+ macro_id: &str,+) -> anyhow::Result<(usize, usize)> {+ // Get fresh Gmail access token for this macro ID+ let gmail_access_token = auth::get_gmail_access_token(config, macro_id).await?;+ println!("Successfully obtained Gmail access token for {}", macro_id);++ // Fetch the link_id associated with the user's account.+ let link_id = email_db_client::links::get::fetch_link_by_macro_id(db_pool, macro_id)+ .await?+ .ok_or_else(|| anyhow::anyhow!("No link found for macro ID: {}", macro_id))?+ .id;++ // Fetch all relevant attachment metadata from the database.+ println!(+ "Fetching unique attachment metadata from database for {}...",+ macro_id+ );+ let attachments = database::fetch_unique_attachments(db_pool, link_id)+ .await+ .context("Failed to fetch attachment metadata")?;+ println!(+ "Found {} unique attachments to process for {}.",+ attachments.len(),+ macro_id+ );++ if attachments.is_empty() {+ return Ok((0, 0));+ }++ // Process and upload each attachment.+ let processor = Arc::new(upload::AttachmentProcessor::new(+ db_pool.clone(),+ dss_client.clone(),+ gmail_client.clone(),+ gmail_access_token,+ macro_id.to_string(),+ ));++ let success_count = Arc::new(AtomicUsize::new(0));+ let total_attachments = attachments.len();++ println!("Starting concurrent upload process for {}...", macro_id);++ stream::iter(attachments.into_iter().enumerate())+ .for_each_concurrent(config.upload_concurrency, |(index, attachment)| {+ let processor: Arc<AttachmentProcessor> = Arc::clone(&processor);+ let success_count = Arc::clone(&success_count);+ let macro_id = macro_id.to_string();++ async move {+ match processor.upload(&attachment).await {+ Ok(_) => {+ success_count.fetch_add(1, Ordering::Relaxed);+ println!("Successfully uploaded '{}' (index: {}) for {}", attachment.filename, index, macro_id);+ }+ Err(e) => {+ // ignore weird file types. annoying game of whack a mole+ if e.to_string().contains("file extension") {+ println!(+ "Skipping '{}' (index: {}) for {} due to unsupported mime type {}",+ attachment.filename, index, macro_id, attachment.mime_type+ );+ return;+ }+ panic!(+ "Failed to upload attachment - filename: {}, provider_attachment_id: {}, provider_message_id: {}, index: {}, macro_id: {}, error: {:?}",+ attachment.filename,+ attachment.provider_attachment_id,+ attachment.email_provider_id,+ index,+ macro_id,+ e+ );+ }+ }+ }+ })+ .await;++ let final_success_count = success_count.load(Ordering::SeqCst);+ Ok((final_success_count, total_attachments))+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rsnew file mode 100644index 0000000000..51bbe870f3--- /dev/null+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs@@ -0,0 +1,171 @@+use anyhow::{Context, bail};+use base64::{Engine as _, engine::general_purpose::STANDARD};+use model::document::response::CreateDocumentRequest;+use models_email::service::attachment::AttachmentUploadMetadata;+use sha2::{Digest, Sha256};+use sqlx::PgPool;+use tracing::instrument;++/// A helper struct to manage clients and tokens required for processing.+pub struct AttachmentProcessor {+ db: PgPool,+ dss_client: document_storage_service_client::DocumentStorageServiceClient,+ gmail_client: gmail_client::GmailClient,+ gmail_access_token: String,+ macro_id_destination: String,+}++impl AttachmentProcessor {+ pub fn new(+ db: PgPool,+ dss_client: document_storage_service_client::DocumentStorageServiceClient,+ gmail_client: gmail_client::GmailClient,+ gmail_access_token: String,+ macro_id_destination: String,+ ) -> Self {+ Self {+ db,+ dss_client,+ gmail_client,+ gmail_access_token,+ macro_id_destination,+ }+ }++ /// Orchestrates the full upload process for a single attachment.+ #[instrument(skip(self), fields(file_name = %attachment.filename, mime_type = %attachment.mime_type))]+ pub async fn upload(&self, attachment: &AttachmentUploadMetadata) -> anyhow::Result<()> {+ let exists = email_db_client::attachments::provider::document_email_record_exists(+ &self.db,+ attachment.attachment_db_id,+ )+ .await?;+ if exists {+ println!(+ "Attachment {} already exists in DSS, skipping",+ attachment.attachment_db_id+ );+ return Ok(());+ }++ // 1. Fetch attachment data from Gmail.+ let data = self.fetch_gmail_data(attachment).await?;++ // 2. Calculate hashes required for validation.+ let (sha256_hex, sha256_base64) = Self::calculate_hashes(&data);++ // 3. Get a presigned URL from the Document Storage Service.+ let (presigned_url, content_type) = self.get_presigned_url(attachment, &sha256_hex).await?;++ // 4. Upload the data to the presigned URL (e.g., S3).+ self.upload_to_storage(&presigned_url, &content_type, &sha256_base64, data)+ .await?;++ Ok(())+ }++ /// Fetches the raw attachment data from the Gmail API.+ async fn fetch_gmail_data(+ &self,+ attachment: &AttachmentUploadMetadata,+ ) -> anyhow::Result<Vec<u8>> {+ self.gmail_client+ .get_attachment_data(+ &self.gmail_access_token,+ &attachment.email_provider_id,+ &attachment.provider_attachment_id,+ )+ .await+ .context("Failed to get attachment data from Gmail")+ }++ /// Calculates the hex and base64 encoded SHA256 hash of the attachment data.+ fn calculate_hashes(data: &[u8]) -> (String, String) {+ let mut hasher = Sha256::new();+ hasher.update(data);+ let hash_bytes = hasher.finalize();++ let hex_hash = format!("{:x}", hash_bytes);+ let base64_hash = STANDARD.encode(hash_bytes);++ (hex_hash, base64_hash)+ }++ /// Requests a pre-signed upload URL from the Document Storage Service.+ async fn get_presigned_url(+ &self,+ attachment: &AttachmentUploadMetadata,+ sha256_hex: &str,+ ) -> anyhow::Result<(String, String)> {+ let file_name = attachment+ .filename+ .split('.')+ .next()+ .unwrap_or(&attachment.filename)+ .to_string();++ let file_type = mime_guess::get_mime_extensions_str(&attachment.mime_type)+ .and_then(|exts| exts.first().map(|s| s.to_string()))+ .context("Could not determine file extension from MIME type")?;++ let dss_response = self+ .dss_client+ .create_document_internal(+ CreateDocumentRequest {+ id: None,+ sha: sha256_hex.to_string(),+ document_name: file_name,+ file_type: Some(file_type),+ mime_type: Some(attachment.mime_type.clone()),+ document_family_id: None,+ branched_from_id: None,+ branched_from_version_id: None,+ job_id: None,+ project_id: None,+ created_at: Some(attachment.internal_date_ts),+ email_attachment_id: Some(attachment.attachment_db_id),+ },+ &self.macro_id_destination,+ )+ .await+ .context("DSS create_document call failed")?;++ let presigned_url = dss_response+ .data+ .document_response+ .presigned_url+ .context("DSS response did not include a presigned URL")?;++ Ok((presigned_url, dss_response.data.content_type))+ }++ /// Performs the final PUT request to upload the file data to cloud storage.+ async fn upload_to_storage(+ &self,+ url: &str,+ content_type: &str,+ sha256_base64: &str,+ data: Vec<u8>,+ ) -> anyhow::Result<()> {+ let response = reqwest::Client::new()+ .put(url)+ .header("content-type", content_type)+ .header("x-amz-checksum-sha256", sha256_base64)+ .body(data)+ .send()+ .await+ .context("HTTP request to presigned URL failed")?;++ if !response.status().is_success() {+ let status = response.status();+ let body = response.text().await.unwrap_or_default();+ bail!(+ "Upload to storage failed with status {}. Body: {}",+ status,+ body+ );+ }++ Ok(())+ }+}diff --git a/rust/cloud-storage/email_service/src/bin/backfill_search.rs b/rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rssimilarity index 100%rename from rust/cloud-storage/email_service/src/bin/backfill_search.rsrename to rust/cloud-storage/email_service/src/bin/backfill_search/backfill_search.rs