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
add [properties-be] - email metadata (#611)
Daniel Kweona6a27cd2f2f4
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex aaa87c115c..d4ab4f427a 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -6574,6 +6574,7 @@ dependencies = ["axum","chrono","comms_service_client",+ "email_db_client","http-body-util","macro_auth","macro_cors",diff --git a/rust/cloud-storage/models_properties/src/service/mod.rs b/rust/cloud-storage/models_properties/src/service/mod.rsindex 7b6e986b30..74f3c504b1 100644--- a/rust/cloud-storage/models_properties/src/service/mod.rs+++ b/rust/cloud-storage/models_properties/src/service/mod.rs@@ -10,3 +10,4 @@ pub mod property_definition;pub mod property_definition_with_options;pub mod property_option;pub mod property_value;+pub mod thread_metadata;diff --git a/rust/cloud-storage/models_properties/src/service/thread_metadata.rs b/rust/cloud-storage/models_properties/src/service/thread_metadata.rsnew file mode 100644index 0000000000..15771f972c--- /dev/null+++ b/rust/cloud-storage/models_properties/src/service/thread_metadata.rs@@ -0,0 +1,15 @@+//! Thread metadata model for properties service++use sqlx::FromRow;+use uuid::Uuid;++/// Thread metadata aggregated from email_threads and email_messages tables+#[derive(Debug, Clone, FromRow)]+pub struct ThreadMetadata {+ pub id: Uuid,+ pub subject: Option<String>,+ pub thread_started: Option<chrono::DateTime<chrono::Utc>>,+ pub last_received: Option<chrono::DateTime<chrono::Utc>>,+ pub last_sent: Option<chrono::DateTime<chrono::Utc>>,+ pub message_count: i64,+}diff --git a/rust/cloud-storage/properties_db_client/.sqlx/query-dc836e7dd12a3df4a05ba072af32cb1aca7d6b8c20e636adff6fb3977577e62a.json b/rust/cloud-storage/properties_db_client/.sqlx/query-dc836e7dd12a3df4a05ba072af32cb1aca7d6b8c20e636adff6fb3977577e62a.jsonnew file mode 100644index 0000000000..2ce75f867a--- /dev/null+++ b/rust/cloud-storage/properties_db_client/.sqlx/query-dc836e7dd12a3df4a05ba072af32cb1aca7d6b8c20e636adff6fb3977577e62a.json@@ -0,0 +1,52 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT\n t.id,\n t.latest_inbound_message_ts as last_received,\n t.latest_outbound_message_ts as last_sent,\n first_msg.internal_date_ts as thread_started,\n first_msg.subject,\n (SELECT COUNT(*)::bigint FROM email_messages WHERE thread_id = t.id) as \"message_count!\"\n FROM\n email_threads t\n -- LATERAL join to get subject and timestamp from the first message\n LEFT JOIN LATERAL (\n SELECT internal_date_ts, subject\n FROM email_messages\n WHERE thread_id = t.id\n ORDER BY internal_date_ts ASC NULLS LAST\n LIMIT 1\n ) first_msg ON true\n WHERE\n t.id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "last_received",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 2,+ "name": "last_sent",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 3,+ "name": "thread_started",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 4,+ "name": "subject",+ "type_info": "Text"+ },+ {+ "ordinal": 5,+ "name": "message_count!",+ "type_info": "Int8"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false,+ true,+ true,+ true,+ true,+ null+ ]+ },+ "hash": "dc836e7dd12a3df4a05ba072af32cb1aca7d6b8c20e636adff6fb3977577e62a"+}diff --git a/rust/cloud-storage/properties_db_client/src/lib.rs b/rust/cloud-storage/properties_db_client/src/lib.rsindex 15e8a105a7..aebafbf502 100644--- a/rust/cloud-storage/properties_db_client/src/lib.rs+++ b/rust/cloud-storage/properties_db_client/src/lib.rs@@ -8,3 +8,4 @@ pub mod entity_properties;pub mod error;pub mod property_definitions;pub mod property_options;+pub mod thread_metadata;diff --git a/rust/cloud-storage/properties_db_client/src/thread_metadata/get.rs b/rust/cloud-storage/properties_db_client/src/thread_metadata/get.rsnew file mode 100644index 0000000000..efffc9c7d5--- /dev/null+++ b/rust/cloud-storage/properties_db_client/src/thread_metadata/get.rs@@ -0,0 +1,44 @@+//! Get thread metadata from macrodb.++use crate::error::PropertiesDatabaseError;+use models_properties::service::thread_metadata::ThreadMetadata;+use sqlx::{Pool, Postgres};+use uuid::Uuid;++type Result<T> = std::result::Result<T, PropertiesDatabaseError>;++/// Get thread metadata by thread ID from macrodb+#[tracing::instrument(skip(db), err)]+pub async fn get_thread_metadata(+ db: &Pool<Postgres>,+ thread_id: Uuid,+) -> Result<Option<ThreadMetadata>> {+ sqlx::query_as!(+ ThreadMetadata,+ r#"+ SELECT+ t.id,+ t.latest_inbound_message_ts as last_received,+ t.latest_outbound_message_ts as last_sent,+ first_msg.internal_date_ts as thread_started,+ first_msg.subject,+ (SELECT COUNT(*)::bigint FROM email_messages WHERE thread_id = t.id) as "message_count!"+ FROM+ email_threads t+ -- LATERAL join to get subject and timestamp from the first message+ LEFT JOIN LATERAL (+ SELECT internal_date_ts, subject+ FROM email_messages+ WHERE thread_id = t.id+ ORDER BY internal_date_ts ASC NULLS LAST+ LIMIT 1+ ) first_msg ON true+ WHERE+ t.id = $1+ "#,+ thread_id+ )+ .fetch_optional(db)+ .await+ .map_err(PropertiesDatabaseError::Query)+}diff --git a/rust/cloud-storage/properties_db_client/src/thread_metadata/mod.rs b/rust/cloud-storage/properties_db_client/src/thread_metadata/mod.rsnew file mode 100644index 0000000000..0d38bf20ea--- /dev/null+++ b/rust/cloud-storage/properties_db_client/src/thread_metadata/mod.rs@@ -0,0 +1,3 @@+//! Thread metadata operations for property queries.++pub mod get;diff --git a/rust/cloud-storage/properties_service/Cargo.toml b/rust/cloud-storage/properties_service/Cargo.tomlindex a9ea86a52b..b04f86149c 100644--- a/rust/cloud-storage/properties_service/Cargo.toml+++ b/rust/cloud-storage/properties_service/Cargo.toml@@ -23,6 +23,7 @@ aws-sdk-secretsmanager = { workspace = true }axum = { workspace = true, features = ["tower-log", "ws"] }chrono = { workspace = true }comms_service_client = { path = "../comms_service_client" }+email_db_client = { path = "../email_db_client" }http-body-util = { workspace = true }macro_auth = { path = "../macro_auth" }macro_cors = { path = "../macro_cors" }diff --git a/rust/cloud-storage/properties_service/src/api/permissions.rs b/rust/cloud-storage/properties_service/src/api/permissions.rsindex 394bb817ba..6354ee540f 100644--- a/rust/cloud-storage/properties_service/src/api/permissions.rs+++ b/rust/cloud-storage/properties_service/src/api/permissions.rs@@ -100,5 +100,18 @@ async fn get_access_level(PermissionError::InternalError(message)})?;+ // Fallback for threads: check ownership via link_id if no permission records exist.+ // This handles owned threads where EmailThreadPermission/UserItemAccess were never created.+ if access_level.is_none()+ && entity_ref.entity_type == EntityType::Thread+ && let Ok(thread_id) = uuid::Uuid::parse_str(&entity_ref.entity_id)+ && let Ok(Some(owner_id)) =+ email_db_client::threads::get::get_macro_id_from_thread_id(&context.db, thread_id).await+ && owner_id == user_id+ {+ tracing::debug!("user owns thread via link_id, granting owner access");+ return Ok(Some(AccessLevel::Owner));+ }+Ok(access_level)}diff --git a/rust/cloud-storage/properties_service/src/api/properties/entities/get.rs b/rust/cloud-storage/properties_service/src/api/properties/entities/get.rsindex 922529ac0e..e97614eec3 100644--- a/rust/cloud-storage/properties_service/src/api/properties/entities/get.rs+++ b/rust/cloud-storage/properties_service/src/api/properties/entities/get.rs@@ -100,13 +100,24 @@ pub async fn get_entity_properties(async {match entity_type {EntityType::Document | EntityType::Task => {- super::super::metadata::get_document_metadata_properties(+ crate::api::properties::metadata::get_document_metadata_properties(&context.db,&entity_id,entity_type,).await}+ EntityType::Thread => {+ let thread_id = uuid::Uuid::parse_str(&entity_id).map_err(|e| {+ tracing::error!(error = ?e, entity_id = %entity_id, "invalid thread UUID");+ crate::api::properties::metadata::MetadataError::NotFound+ })?;+ crate::api::properties::metadata::get_thread_metadata_properties(+ &context.db,+ thread_id,+ )+ .await+ }_ => {tracing::debug!(entity_type = ?entity_type,@@ -130,7 +141,7 @@ pub async fn get_entity_properties(tracing::error!(error = ?e,entity_id = %entity_id,- "failed to get document system properties"+ "failed to get metadata properties");})?;diff --git a/rust/cloud-storage/properties_service/src/api/properties/metadata.rs b/rust/cloud-storage/properties_service/src/api/properties/metadata.rsindex d4d0687c35..c10d3e5c79 100644--- a/rust/cloud-storage/properties_service/src/api/properties/metadata.rs+++ b/rust/cloud-storage/properties_service/src/api/properties/metadata.rs@@ -6,6 +6,7 @@ use models_properties::{EntityReference, EntityType};use properties_db_client::error::PropertiesDatabaseError;use sqlx::{Pool, Postgres};use thiserror::Error;+use uuid::Uuid;use crate::constants::{METADATA_PROPERTY_ID, metadata};@@ -19,127 +20,160 @@ pub enum MetadataError {}/// Get document metadata properties from macrodb+#[tracing::instrument(skip(db), err)]pub async fn get_document_metadata_properties(db: &Pool<Postgres>,document_id: &str,entity_type: EntityType,) -> Result<Vec<EntityPropertyWithDefinition>, MetadataError> {- tracing::info!("getting document metadata properties from macrodb");-- // Fetch document metadata from macrodblet document_metadata =properties_db_client::document_metadata::get::get_document_metadata(db, document_id)- .await- .inspect_err(|e| {- tracing::error!(- error = ?e,- document_id = %document_id,- "failed to get document metadata from database"- );- })?+ .await?.ok_or(MetadataError::NotFound)?;- tracing::debug!(- document_id = %document_id,- has_document_name = !document_metadata.name.is_empty(),- has_owner = !document_metadata.owner.is_empty(),- has_project_id = document_metadata.project_id.is_some(),- "parsed document metadata"- );-let mut metadata_properties = Vec::new();// 1. Document name property- if !document_metadata.name.is_empty() {- metadata_properties.push(create_metadata_property_str(- metadata::DOCUMENT_NAME,- models_properties::DataType::String,- document_metadata.name,- entity_type,- ));- }+ let name = (!document_metadata.name.is_empty()).then_some(document_metadata.name);+ metadata_properties.push(create_metadata_property_str(+ metadata::DOCUMENT_NAME,+ models_properties::DataType::String,+ name,+ entity_type,+ ));// 2. Owner property- if !document_metadata.owner.is_empty() {- let owner_entity_ref = EntityReference::new(document_metadata.owner, EntityType::User);- metadata_properties.push(create_metadata_property_entity_ref(- metadata::OWNER,- models_properties::DataType::Entity,- owner_entity_ref,- entity_type,- Some(EntityType::User),- ));- }+ let owner = (!document_metadata.owner.is_empty())+ .then(|| EntityReference::new(document_metadata.owner, EntityType::User));+ metadata_properties.push(create_metadata_property_entity_ref(+ metadata::DOCUMENT_OWNER,+ models_properties::DataType::Entity,+ owner,+ entity_type,+ Some(EntityType::User),+ ));// 3. Created time propertymetadata_properties.push(create_metadata_property_date(- metadata::CREATED_AT,+ metadata::DOCUMENT_CREATED_AT,models_properties::DataType::Date,- document_metadata.created_at,+ Some(document_metadata.created_at),entity_type,));// 4. Last updated time propertymetadata_properties.push(create_metadata_property_date(- metadata::LAST_UPDATED,+ metadata::DOCUMENT_LAST_UPDATED,models_properties::DataType::Date,- document_metadata.updated_at,+ Some(document_metadata.updated_at),entity_type,));// 5. Project property- if let Some(project_id) = document_metadata.project_id {- let project_entity_ref = EntityReference::new(project_id, EntityType::Project);- metadata_properties.push(create_metadata_property_entity_ref(- metadata::PROJECT,- models_properties::DataType::Entity,- project_entity_ref,+ let project = document_metadata+ .project_id+ .map(|id| EntityReference::new(id, EntityType::Project));+ metadata_properties.push(create_metadata_property_entity_ref(+ metadata::DOCUMENT_PROJECT,+ models_properties::DataType::Entity,+ project,+ entity_type,+ Some(EntityType::Project),+ ));++ Ok(metadata_properties)+}++/// Get thread metadata properties from macrodb+#[tracing::instrument(skip(db), err)]+pub async fn get_thread_metadata_properties(+ db: &Pool<Postgres>,+ thread_id: Uuid,+) -> Result<Vec<EntityPropertyWithDefinition>, MetadataError> {+ let thread_metadata =+ properties_db_client::thread_metadata::get::get_thread_metadata(db, thread_id)+ .await?+ .ok_or(MetadataError::NotFound)?;++ let entity_type = EntityType::Thread;++ let metadata_properties = vec![+ // 1. Subject property+ create_metadata_property_str(+ metadata::THREAD_SUBJECT,+ models_properties::DataType::String,+ thread_metadata.subject.clone(),entity_type,- Some(EntityType::Project),- ));- } else {- // Add project property with null value- metadata_properties.push(create_metadata_property_null(- metadata::PROJECT,- models_properties::DataType::Entity,+ ),+ // 2. Thread Started property+ create_metadata_property_date(+ metadata::THREAD_STARTED,+ models_properties::DataType::Date,+ thread_metadata.thread_started,entity_type,- Some(EntityType::Project),- ));- }-- tracing::debug!(- document_id = %document_id,- metadata_properties_count = metadata_properties.len(),- "created document metadata properties"- );+ ),+ // 3. Last Received property+ create_metadata_property_date(+ metadata::THREAD_LAST_RECEIVED,+ models_properties::DataType::Date,+ thread_metadata.last_received,+ entity_type,+ ),+ // 4. Last Sent property+ create_metadata_property_date(+ metadata::THREAD_LAST_SENT,+ models_properties::DataType::Date,+ thread_metadata.last_sent,+ entity_type,+ ),+ // 5. Messages property (count)+ create_metadata_property_number(+ metadata::THREAD_MESSAGES,+ models_properties::DataType::Number,+ thread_metadata.message_count,+ entity_type,+ ),+ ];Ok(metadata_properties)}-/// Create a metadata property with a string value+// ===== Metadata Property Helpers =====+//+// These helpers create read-only metadata properties that are computed on-the-fly+// from entity data (not stored in the properties tables). They share a special+// METADATA_PROPERTY_ID and are marked with is_metadata=true.++/// Create a metadata property with a string value (e.g., document name, subject)pub fn create_metadata_property_str(display_name: &str,data_type: models_properties::DataType,- value: String,+ value: Option<String>,entity_type: EntityType,) -> EntityPropertyWithDefinition {- let property_value = PropertyValue::Str(value);- create_metadata_property_inner(- display_name,- data_type,- Some(property_value),- entity_type,- None,- )+ let property_value = value.map(PropertyValue::Str);+ create_metadata_property_inner(display_name, data_type, property_value, entity_type, None)}+/// Create a metadata property with a date/timestamp value (e.g., created_at, last_updated)pub fn create_metadata_property_date(display_name: &str,data_type: models_properties::DataType,- value: chrono::DateTime<chrono::Utc>,+ value: Option<chrono::DateTime<chrono::Utc>>,+ entity_type: EntityType,+) -> EntityPropertyWithDefinition {+ let property_value = value.map(PropertyValue::Date);+ create_metadata_property_inner(display_name, data_type, property_value, entity_type, None)+}++/// Create a metadata property with a numeric value (e.g., message count)+pub fn create_metadata_property_number(+ display_name: &str,+ data_type: models_properties::DataType,+ value: i64,entity_type: EntityType,) -> EntityPropertyWithDefinition {- let property_value = PropertyValue::Date(value);+ let property_value = PropertyValue::Num(value as f64);create_metadata_property_inner(display_name,data_type,@@ -149,23 +183,26 @@ pub fn create_metadata_property_date()}+/// Create a metadata property with an entity reference value (e.g., owner, project)pub fn create_metadata_property_entity_ref(display_name: &str,data_type: models_properties::DataType,- value: EntityReference,+ value: Option<EntityReference>,entity_type: EntityType,specific_entity_type: Option<EntityType>,) -> EntityPropertyWithDefinition {- let property_value = PropertyValue::EntityRef(vec![value]);+ let property_value = value.map(|v| PropertyValue::EntityRef(vec![v]));create_metadata_property_inner(display_name,data_type,- Some(property_value),+ property_value,entity_type,specific_entity_type,)}+/// Internal helper that constructs the EntityPropertyWithDefinition struct.+/// Sets up the property definition with METADATA_PROPERTY_ID and is_metadata=true.fn create_metadata_property_inner(display_name: &str,data_type: models_properties::DataType,@@ -206,19 +243,3 @@ fn create_metadata_property_inner(options: None,}}--/// Create a metadata property with null/empty value-pub fn create_metadata_property_null(- property_name: &str,- data_type: models_properties::DataType,- entity_type: EntityType,- specific_entity_type: Option<EntityType>,-) -> EntityPropertyWithDefinition {- create_metadata_property_inner(- property_name,- data_type,- None,- entity_type,- specific_entity_type,- )-}diff --git a/rust/cloud-storage/properties_service/src/constants.rs b/rust/cloud-storage/properties_service/src/constants.rsindex e9e00cb49b..dcbad75bd8 100644--- a/rust/cloud-storage/properties_service/src/constants.rs+++ b/rust/cloud-storage/properties_service/src/constants.rs@@ -2,11 +2,19 @@ use uuid::Uuid;/// Metadata property display namespub mod metadata {+ // Document metadatapub const DOCUMENT_NAME: &str = "Document Name";- pub const OWNER: &str = "Owner";- pub const CREATED_AT: &str = "Created At";- pub const LAST_UPDATED: &str = "Last Updated";- pub const PROJECT: &str = "Project";+ pub const DOCUMENT_OWNER: &str = "Owner";+ pub const DOCUMENT_CREATED_AT: &str = "Created At";+ pub const DOCUMENT_LAST_UPDATED: &str = "Last Updated";+ pub const DOCUMENT_PROJECT: &str = "Project";++ // Thread metadata+ pub const THREAD_SUBJECT: &str = "Subject";+ pub const THREAD_STARTED: &str = "Thread Started";+ pub const THREAD_LAST_RECEIVED: &str = "Last Received";+ pub const THREAD_LAST_SENT: &str = "Last Sent";+ pub const THREAD_MESSAGES: &str = "Messages";}/// Special UUID used for system-generated metadata properties.