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): Message contact name saving (#463)

Evan Hutnik7ef528f5d88d
diff --git a/rust/cloud-storage/email_db_client/src/contacts/normalize.rs b/rust/cloud-storage/email_db_client/src/contacts/normalize.rs
index dbfe8902a5..b74940528b 100644
--- a/rust/cloud-storage/email_db_client/src/contacts/normalize.rs
+++ b/rust/cloud-storage/email_db_client/src/contacts/normalize.rs
@@ -1,3 +1,6 @@
+#[cfg(test)]
+mod tests;
+
use models_email::db::contact::ContactPhotoless;
use once_cell::sync::Lazy;
use regex::Regex;
@@ -6,10 +9,13 @@ pub fn normalize_contact(contact: ContactPhotoless) -> ContactPhotoless {
let normalized_name = if email_utils::is_generic_email(&contact.email_address) {
None
} else {
- contact
- .name
- .as_ref()
- .map(|name_str| remove_name_suffix(name_str))
+ contact.name.as_ref().and_then(|name_str| {
+ if contains_email(name_str) {
+ None
+ } else {
+ Some(remove_name_suffix(name_str))
+ }
+ })
};
ContactPhotoless {
@@ -20,6 +26,15 @@ pub fn normalize_contact(contact: ContactPhotoless) -> ContactPhotoless {
}
}
+/// Checks if a string contains an email address pattern
+///
+/// This function detects email patterns like "[email protected]" but not
+/// patterns like "Gordon @ Calendly" (spaces around @ without domain).
+fn contains_email(text: &str) -> bool {
+ static EMAIL_PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new(r"\S+@\S+\.\S+").unwrap());
+ EMAIL_PATTERN.is_match(text)
+}
+
/// Removes service suffixes from a contact name and normalizes spaces
///
/// This function detects and removes:
@@ -52,81 +67,3 @@ fn remove_name_suffix(name: &str) -> String {
SPECIFIC_SUFFIX_PATTERN.replace(&name, "").to_string()
}
-
-#[test]
-fn test_remove_name_suffix() {
- // Test "via X" pattern (without parentheses)
- assert_eq!(remove_name_suffix("John Doe via Gmail"), "John Doe");
- assert_eq!(remove_name_suffix("Jane Smith via Outlook"), "Jane Smith");
- assert_eq!(
- remove_name_suffix("Alice Johnson via Yahoo Mail"),
- "Alice Johnson"
- );
-
- // Test "(via X)" pattern (with parentheses)
- assert_eq!(
- remove_name_suffix("Bob Williams (via Gmail)"),
- "Bob Williams"
- );
- assert_eq!(
- remove_name_suffix("Charlie Brown (via Outlook)"),
- "Charlie Brown"
- );
- assert_eq!(
- remove_name_suffix("Diana Prince (via Yahoo Mail)"),
- "Diana Prince"
- );
-
- // Test specific common suffixes
- assert_eq!(remove_name_suffix("Ethan Hunt (Figma)"), "Ethan Hunt");
- assert_eq!(
- remove_name_suffix("Fiona Apple (Google Calendar)"),
- "Fiona Apple"
- );
- assert_eq!(
- remove_name_suffix("George Lucas (Shared via Google)"),
- "George Lucas"
- );
- assert_eq!(
- remove_name_suffix("Hannah Montana (Google Drive)"),
- "Hannah Montana"
- );
- assert_eq!(remove_name_suffix("Ian McKellen (Dropbox)"), "Ian McKellen");
- assert_eq!(
- remove_name_suffix("Jennifer Lopez (Microsoft 365)"),
- "Jennifer Lopez"
- );
-
- // Test with non-breaking spaces
- let name_with_nbsp = format!("John{}Doe via Teams", '\u{00A0}');
- assert_eq!(remove_name_suffix(&name_with_nbsp), "John Doe");
-
- // Test with combined issues
- let complex_name = format!("Jane{}Smith (via Microsoft 365)", '\u{00A0}');
- assert_eq!(remove_name_suffix(&complex_name), "Jane Smith");
-
- // Test with no changes needed
- assert_eq!(remove_name_suffix("Regular Name"), "Regular Name");
-
- // Test with more complex cases
- assert_eq!(
- remove_name_suffix("Team Notification via Slack"),
- "Team Notification"
- );
- assert_eq!(
- remove_name_suffix("Project Update (via Jira Cloud)"),
- "Project Update"
- );
- assert_eq!(
- remove_name_suffix("Marketing Team via MailChimp"),
- "Marketing Team"
- );
- assert_eq!(
- remove_name_suffix("Olivia Rios (via MailChimp)"),
- "Olivia Rios"
- );
- assert_eq!(
- remove_name_suffix("Aviation Week & Space Technology"),
- "Aviation Week & Space Technology"
- );
-}
diff --git a/rust/cloud-storage/email_db_client/src/contacts/normalize/tests.rs b/rust/cloud-storage/email_db_client/src/contacts/normalize/tests.rs
new file mode 100644
index 0000000000..5b9a32a731
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/src/contacts/normalize/tests.rs
@@ -0,0 +1,154 @@
+use crate::contacts::normalize::{contains_email, remove_name_suffix};
+
+#[test]
+fn test_remove_name_suffix() {
+ // Test "via X" pattern (without parentheses)
+ assert_eq!(remove_name_suffix("John Doe via Gmail"), "John Doe");
+ assert_eq!(remove_name_suffix("Jane Smith via Outlook"), "Jane Smith");
+ assert_eq!(
+ remove_name_suffix("Alice Johnson via Yahoo Mail"),
+ "Alice Johnson"
+ );
+
+ // Test "(via X)" pattern (with parentheses)
+ assert_eq!(
+ remove_name_suffix("Bob Williams (via Gmail)"),
+ "Bob Williams"
+ );
+ assert_eq!(
+ remove_name_suffix("Charlie Brown (via Outlook)"),
+ "Charlie Brown"
+ );
+ assert_eq!(
+ remove_name_suffix("Diana Prince (via Yahoo Mail)"),
+ "Diana Prince"
+ );
+
+ // Test specific common suffixes
+ assert_eq!(remove_name_suffix("Ethan Hunt (Figma)"), "Ethan Hunt");
+ assert_eq!(
+ remove_name_suffix("Fiona Apple (Google Calendar)"),
+ "Fiona Apple"
+ );
+ assert_eq!(
+ remove_name_suffix("George Lucas (Shared via Google)"),
+ "George Lucas"
+ );
+ assert_eq!(
+ remove_name_suffix("Hannah Montana (Google Drive)"),
+ "Hannah Montana"
+ );
+ assert_eq!(remove_name_suffix("Ian McKellen (Dropbox)"), "Ian McKellen");
+ assert_eq!(
+ remove_name_suffix("Jennifer Lopez (Microsoft 365)"),
+ "Jennifer Lopez"
+ );
+
+ // Test with non-breaking spaces
+ let name_with_nbsp = format!("John{}Doe via Teams", '\u{00A0}');
+ assert_eq!(remove_name_suffix(&name_with_nbsp), "John Doe");
+
+ // Test with combined issues
+ let complex_name = format!("Jane{}Smith (via Microsoft 365)", '\u{00A0}');
+ assert_eq!(remove_name_suffix(&complex_name), "Jane Smith");
+
+ // Test with no changes needed
+ assert_eq!(remove_name_suffix("Regular Name"), "Regular Name");
+
+ // Test with more complex cases
+ assert_eq!(
+ remove_name_suffix("Team Notification via Slack"),
+ "Team Notification"
+ );
+ assert_eq!(
+ remove_name_suffix("Project Update (via Jira Cloud)"),
+ "Project Update"
+ );
+ assert_eq!(
+ remove_name_suffix("Marketing Team via MailChimp"),
+ "Marketing Team"
+ );
+ assert_eq!(
+ remove_name_suffix("Olivia Rios (via MailChimp)"),
+ "Olivia Rios"
+ );
+ assert_eq!(
+ remove_name_suffix("Aviation Week & Space Technology"),
+ "Aviation Week & Space Technology"
+ );
+}
+
+#[test]
+fn test_contains_email_with_valid_emails() {
+ // Standard email formats
+ assert!(contains_email("[email protected]"));
+ assert!(contains_email("[email protected]"));
+ assert!(contains_email("[email protected]"));
+ assert!(contains_email("[email protected]"));
+
+ // Emails within text
+ assert!(contains_email("Contact [email protected] for info"));
+ assert!(contains_email("John Doe ([email protected])"));
+ assert!(contains_email("Email: [email protected]"));
+ assert!(contains_email("[email protected] - John Doe"));
+ assert!(contains_email("Reply to [email protected] today"));
+}
+
+#[test]
+fn test_contains_email_with_spaces_around_at() {
+ // These should NOT match (spaces around @)
+ assert!(!contains_email("Gordon @ Calendly"));
+ assert!(!contains_email("Sales @ Company"));
+ assert!(!contains_email("Support @ Microsoft"));
+ assert!(!contains_email("Team @ Notion"));
+ assert!(!contains_email("John @ Work"));
+}
+
+#[test]
+fn test_contains_email_without_domain() {
+ // No dot after @ - should not match
+ assert!(!contains_email("user@localhost"));
+ assert!(!contains_email("test@domain"));
+ assert!(!contains_email("Contact @ HQ"));
+}
+
+#[test]
+fn test_contains_email_edge_cases() {
+ // Empty and whitespace
+ assert!(!contains_email(""));
+ assert!(!contains_email(" "));
+
+ // Just @ symbol
+ assert!(!contains_email("@"));
+ assert!(!contains_email(" @ "));
+
+ // Multiple @ symbols
+ assert!(contains_email("test@@example.com")); // Matches because of pattern
+ assert!(contains_email("[email protected] and [email protected]"));
+
+ // Special characters
+ assert!(contains_email("[email protected]"));
+ assert!(contains_email("[email protected]"));
+
+ // No @ at all
+ assert!(!contains_email("John Doe"));
+ assert!(!contains_email("Regular Name"));
+ assert!(!contains_email("Company Name Inc."));
+}
+
+#[test]
+fn test_contains_email_with_formatting() {
+ // Parentheses
+ assert!(contains_email("([email protected])"));
+ assert!(contains_email("[[email protected]]"));
+
+ // Quotes
+ assert!(contains_email("\"[email protected]\""));
+ assert!(contains_email("'[email protected]'"));
+
+ // Mixed with other text
+ assert!(contains_email(
+ "For questions, email [email protected] or call us"
+ ));
+ assert!(contains_email("John Doe <[email protected]>"));
+}