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): keep attachment properties when an address is not a user id (#8413)

teofea7b5a43c2a
diff --git a/services/email_service/src/util/upload_attachment.rs b/services/email_service/src/util/upload_attachment.rs
index 0753042066..593909855e 100644
--- a/services/email_service/src/util/upload_attachment.rs
+++ b/services/email_service/src/util/upload_attachment.rs
@@ -3,8 +3,8 @@ use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use document_storage_service_client::DocumentStorageServiceClient;
use email_api_client::domain::models::EmailApiError;
-use macro_user_id::cowlike::ArcCowStr;
-use macro_user_id::user_id::MacroUserId;
+use macro_user_id::lowercased::Lowercase;
+use macro_user_id::user_id::{MacroUserId, MacroUserIdStr};
use model::document::MAX_DOCUMENT_NAME_GRAPHEMES;
use model::document::response::{CreateDocumentRequest, CreateDocumentResponse};
use models_email::service::attachment::{
@@ -365,55 +365,51 @@ async fn set_email_attachment_properties(
document_id: &str,
p: &AttachmentUploadArgs,
) -> Result<(), UploadAttachmentError> {
- let sender_email = format!("macro|{}", p.attachment_metadata.sender_email);
- let sender = MacroUserId::parse_from_str(&sender_email)
- .map_err(|e| {
- UploadAttachmentError::ParseError(format!(
- "Failed to parse sender email {} into macro user id: {}",
- p.attachment_metadata.sender_email, e
- ))
- })?
- .lowercase();
-
- // parse_from_str only accepts &str, so we need to store prefixed emails somewhere that outlives parsing
- let prefixed_emails: Vec<String> = p
- .recipient_emails
- .iter()
- .map(|email| format!("macro|{}", email))
- .collect();
-
- let recipients: Result<Vec<MacroUserId<ArcCowStr>>, _> = prefixed_emails
- .iter()
- .map(|email| {
- MacroUserId::parse_from_str(email).map_err(|e| {
- UploadAttachmentError::ParseError(format!(
- "Failed to parse recipient email {} into macro user id: {}",
- email, e
- ))
- })
- })
- .collect();
-
- let recipients: Vec<_> = recipients?.into_iter().map(|id| id.lowercase()).collect();
-
system_properties_service
.set_email_attachment_properties(vec![EmailAttachmentInput {
entity_id: document_id.to_string(),
- properties: EmailAttachmentProperty {
- source: Some(SourceEntity {
- entity_type: models_properties::EntityType::Thread,
- entity_id: p.attachment_metadata.thread_db_id.to_string(),
- specific_message_id: Some(p.attachment_metadata.message_db_id),
- }),
- // TODO: companies support
- companies: None,
- sender: Some(sender),
- recipients: Some(recipients),
- subject: p.attachment_metadata.subject.clone(),
- },
+ properties: email_attachment_property(p),
}])
.await
.map_err(|e| UploadAttachmentError::SystemPropertiesSetFailed(e.to_string()))?;
Ok(())
}
+
+/// Builds the attachment's properties from its message.
+///
+/// Header addresses are stored as Gmail sent them, so some are not valid Macro
+/// user ids. Those are left out rather than dropping every property.
+fn email_attachment_property(p: &AttachmentUploadArgs) -> EmailAttachmentProperty<'static> {
+ EmailAttachmentProperty {
+ source: Some(SourceEntity {
+ entity_type: models_properties::EntityType::Thread,
+ entity_id: p.attachment_metadata.thread_db_id.to_string(),
+ specific_message_id: Some(p.attachment_metadata.message_db_id),
+ }),
+ // TODO: companies support
+ companies: None,
+ sender: macro_user_id(p, &p.attachment_metadata.sender_email),
+ recipients: Some(
+ p.recipient_emails
+ .iter()
+ .filter_map(|email| macro_user_id(p, email))
+ .collect(),
+ ),
+ subject: p.attachment_metadata.subject.clone(),
+ }
+}
+
+fn macro_user_id(p: &AttachmentUploadArgs, email: &str) -> Option<MacroUserId<Lowercase<'static>>> {
+ match MacroUserIdStr::try_from_email(email) {
+ Ok(id) => Some(id.0),
+ // The parse error echoes the address, so it stays out of the log.
+ Err(_) => {
+ tracing::warn!(
+ message_id = %p.attachment_metadata.message_db_id,
+ "Leaving an address that is not a valid Macro user id out of attachment properties"
+ );
+ None
+ }
+ }
+}
diff --git a/services/email_service/src/util/upload_attachment/test.rs b/services/email_service/src/util/upload_attachment/test.rs
index 1e2854673e..f2ca4d5f48 100644
--- a/services/email_service/src/util/upload_attachment/test.rs
+++ b/services/email_service/src/util/upload_attachment/test.rs
@@ -36,3 +36,60 @@ fn long_names_keep_the_leading_graphemes_dss_accepts() {
check(&format!("{}.pdf", unit.repeat(250)), &unit.repeat(200));
}
}
+
+fn upload_args(sender: &str, recipients: &[&str]) -> AttachmentUploadArgs {
+ AttachmentUploadArgs {
+ attachment_metadata: AttachmentUploadMetadata {
+ sender_email: sender.to_string(),
+ subject: Some("Quarterly report".to_string()),
+ thread_db_id: Uuid::from_u128(1),
+ message_db_id: Uuid::from_u128(2),
+ ..metadata("report.pdf")
+ },
+ recipient_emails: recipients.iter().map(|r| r.to_string()).collect(),
+ backfill: false,
+ upload_destination: models_email::service::attachment::AttachmentUploadDestination::Dss,
+ }
+}
+
+fn recipients(property: &EmailAttachmentProperty) -> Vec<String> {
+ property
+ .recipients
+ .iter()
+ .flatten()
+ .map(|id| id.as_ref().to_string())
+ .collect()
+}
+
+#[test]
+fn invalid_recipients_are_left_out_of_attachment_properties() {
+ let property = email_attachment_property(&upload_args(
+ &[
+ ],
+ ));
+
+ assert_eq!(
+ property.sender.as_ref().map(|id| id.as_ref()),
+ Some("macro|[email protected]")
+ );
+ assert_eq!(recipients(&property), ["macro|[email protected]"]);
+ assert_eq!(property.subject.as_deref(), Some("Quarterly report"));
+ let source = property.source.expect("source is set");
+ assert_eq!(source.entity_id, Uuid::from_u128(1).to_string());
+ assert_eq!(source.specific_message_id, Some(Uuid::from_u128(2)));
+}
+
+#[test]
+fn invalid_sender_keeps_the_other_attachment_properties() {
+ let property =
+ email_attachment_property(&upload_args("'[email protected]'", &["[email protected]"]));
+
+ assert!(property.sender.is_none());
+ assert_eq!(recipients(&property), ["macro|[email protected]"]);
+ assert_eq!(property.subject.as_deref(), Some("Quarterly report"));
+ assert!(property.source.is_some());
+}