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
refactor(middleware): remove need for email service in middleware (#1079)
Will Hutchinsone71fedc1a1d5
diff --git a/infra/stacks/cloud-storage-service/index.ts b/infra/stacks/cloud-storage-service/index.tsindex 7b51d01ace..45ce1df7c8 100644--- a/infra/stacks/cloud-storage-service/index.ts+++ b/infra/stacks/cloud-storage-service/index.ts@@ -319,12 +319,6 @@ const cloudStorageService = new CloudStorageService(stack === 'prod' ? '' : `-${stack}`}.macro.com`,},- {- name: 'EMAIL_SERVICE_URL',- value: `https://email-service${- stack === 'prod' ? '' : `-${stack}`- }.macro.com`,- },{name: 'DOCUMENT_PERMISSION_JWT_SECRET_KEY',value: pulumi.interpolate`${DOCUMENT_STORAGE_PERMISSIONS_KEY}`,diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex 49e6692435..1400b41a8d 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -2732,7 +2732,6 @@ dependencies = ["document_sub_type","dynamodb_client","email",- "email_service_client","frecency","futures","hex",@@ -5066,7 +5065,6 @@ dependencies = ["anyhow","axum","comms_service_client",- "email_service_client","http-body-util","macro_auth","macro_db_client",diff --git a/rust/cloud-storage/document_storage_service/Cargo.toml b/rust/cloud-storage/document_storage_service/Cargo.tomlindex 95b8426375..0c28487de8 100644--- a/rust/cloud-storage/document_storage_service/Cargo.toml+++ b/rust/cloud-storage/document_storage_service/Cargo.toml@@ -51,7 +51,6 @@ connection_gateway = { path = "../connection_gateway" }document_sub_type = { path = "../document_sub_type" }dynamodb_client = { path = "../dynamodb_client" }email = { path = "../email", features = ["outbound", "ports"] }-email_service_client = { path = "../email_service_client" }frecency = { path = "../frecency", features = ["postgres"] }futures = { workspace = true }hex = { workspace = true }diff --git a/rust/cloud-storage/document_storage_service/src/api/channel/update_channel_share_permission.rs b/rust/cloud-storage/document_storage_service/src/api/channel/update_channel_share_permission.rsindex e720bdd964..3c1dfe60df 100644--- a/rust/cloud-storage/document_storage_service/src/api/channel/update_channel_share_permission.rs+++ b/rust/cloud-storage/document_storage_service/src/api/channel/update_channel_share_permission.rs@@ -30,7 +30,7 @@ pub async fn handler(// ensure thread share permissions exist before getting access levelif req.item_type == "thread" {- insert_thread_share_permissions(&ctx.db, &ctx.email_service_client, &req.item_id)+ insert_thread_share_permissions(&ctx.db, &req.item_id).await.map_err(|e| {tracing::error!(error=?e, "failed to insert thread share permissions");diff --git a/rust/cloud-storage/document_storage_service/src/api/context.rs b/rust/cloud-storage/document_storage_service/src/api/context.rsindex 85e591a89d..960e16a1a1 100644--- a/rust/cloud-storage/document_storage_service/src/api/context.rs+++ b/rust/cloud-storage/document_storage_service/src/api/context.rs@@ -7,7 +7,6 @@ use comms::{use connection_gateway_client::client::ConnectionGatewayClient;use dynamodb_client::DynamodbClient;use email::{domain::service::EmailServiceImpl, outbound::EmailPgRepo};-use email_service_client::EmailServiceClient;use frecency::{domain::services::FrecencyQueryServiceImpl, outbound::postgres::FrecencyPgStorage};use macro_auth::middleware::decode_jwt::JwtValidationArgs;use macro_env_var::env_var;@@ -54,7 +53,6 @@ pub(crate) struct ApiContext {pub sqs_client: Arc<sqs_client::SQS>,pub macro_notify_client: Arc<macro_notify::MacroNotify>,pub comms_service_client: Arc<comms_service_client::CommsServiceClient>,- pub email_service_client: Arc<EmailServiceClient>,pub conn_gateway_client: Arc<ConnectionGatewayClient>,pub sync_service_client: Arc<SyncServiceClient>,pub system_properties_service: Arc<SystemPropertiesService>,diff --git a/rust/cloud-storage/document_storage_service/src/config.rs b/rust/cloud-storage/document_storage_service/src/config.rsindex 5c6260372e..67ac614fe3 100644--- a/rust/cloud-storage/document_storage_service/src/config.rs+++ b/rust/cloud-storage/document_storage_service/src/config.rs@@ -42,7 +42,6 @@ env_var! {pub NotificationQueue,pub SearchEventQueue,pub CommsServiceUrl,- pub EmailServiceUrl,pub ConnectionGatewayUrl,pub BulkUploadRequestsTable,pub UploadStagingBucket,diff --git a/rust/cloud-storage/document_storage_service/src/main.rs b/rust/cloud-storage/document_storage_service/src/main.rsindex 81ee2d90eb..d36bdf9a41 100644--- a/rust/cloud-storage/document_storage_service/src/main.rs+++ b/rust/cloud-storage/document_storage_service/src/main.rs@@ -13,7 +13,6 @@ use config::{Config, Environment};use connection_gateway_client::client::ConnectionGatewayClient;use dynamodb_client::DynamodbClient;use email::{domain::service::EmailServiceImpl, outbound::EmailPgRepo};-use email_service_client::EmailServiceClient;use frecency::{domain::services::FrecencyQueryServiceImpl, outbound::postgres::FrecencyPgStorage};use macro_auth::middleware::decode_jwt::JwtValidationArgs;use macro_entrypoint::MacroEntrypoint;@@ -161,11 +160,6 @@ async fn main() -> anyhow::Result<()> {config.vars.comms_service_url.as_ref().to_string(),);- let email_service_client = EmailServiceClient::new(- dss_auth_key.as_ref().to_string(),- config.vars.email_service_url.as_ref().to_string(),- );-let conn_gateway_client = ConnectionGatewayClient::new(internal_api_secret.as_ref().to_string(),config.vars.connection_gateway_url.as_ref().to_string(),@@ -254,7 +248,6 @@ async fn main() -> anyhow::Result<()> {dynamo_db,sqs_client: Arc::new(sqs_client),macro_notify_client: Arc::new(macro_notify_client),- email_service_client: Arc::new(email_service_client),comms_service_client: Arc::new(comms_service_client),conn_gateway_client: Arc::new(conn_gateway_client),sync_service_client: Arc::new(sync_service_client),diff --git a/rust/cloud-storage/macro_db_client/.sqlx/query-0af2bfac1a41e4dbd3c7956d306ffab02e9aee53b37e07531426a068007403ec.json b/rust/cloud-storage/macro_db_client/.sqlx/query-0af2bfac1a41e4dbd3c7956d306ffab02e9aee53b37e07531426a068007403ec.jsonnew file mode 100644index 0000000000..a247c24f9c--- /dev/null+++ b/rust/cloud-storage/macro_db_client/.sqlx/query-0af2bfac1a41e4dbd3c7956d306ffab02e9aee53b37e07531426a068007403ec.json@@ -0,0 +1,22 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT l.macro_id\n FROM email_threads t\n JOIN email_links l ON t.link_id = l.id\n WHERE t.id = $1\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "macro_id",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "0af2bfac1a41e4dbd3c7956d306ffab02e9aee53b37e07531426a068007403ec"+}diff --git a/rust/cloud-storage/macro_db_client/src/share_permission/get.rs b/rust/cloud-storage/macro_db_client/src/share_permission/get.rsindex 64c865c4ee..9d9cabb5b0 100644--- a/rust/cloud-storage/macro_db_client/src/share_permission/get.rs+++ b/rust/cloud-storage/macro_db_client/src/share_permission/get.rs@@ -1,3 +1,4 @@+use anyhow::Context;use model::thread::EmailThreadPermission;use models_permissions::share_permission::SharePermissionV2;use models_permissions::share_permission::access_level::AccessLevel;@@ -353,6 +354,31 @@ pub async fn get_email_thread_permission(}))}+/// THIS IS COPIED FROM EMAIL_DB_CLIENT and is needed in the `insert_thread_share_permission`+/// in macro_middleware crate+#[tracing::instrument(skip(pool))]+pub async fn get_macro_id_from_thread_id(+ pool: &sqlx::Pool<sqlx::Postgres>,+ thread_id: &str,+) -> anyhow::Result<Option<String>> {+ let thread_id = macro_uuid::string_to_uuid(thread_id).context("invalid uuid")?;++ let macro_id = sqlx::query_scalar!(+ r#"+ SELECT l.macro_id+ FROM email_threads t+ JOIN email_links l ON t.link_id = l.id+ WHERE t.id = $1+ "#,+ &thread_id+ )+ .fetch_optional(pool)+ .await+ .with_context(|| format!("Failed to fetch macro_id for thread ID {}", thread_id))?;++ Ok(macro_id)+}+/// Retrieves all document, chat, and project IDs associated with the given share permission IDs/// Returns a map where the key is the share_permission_id, and the value is a tuple containing the/// item id and typediff --git a/rust/cloud-storage/macro_middleware/Cargo.toml b/rust/cloud-storage/macro_middleware/Cargo.tomlindex 7ad58af8ab..5e42dfa873 100644--- a/rust/cloud-storage/macro_middleware/Cargo.toml+++ b/rust/cloud-storage/macro_middleware/Cargo.toml@@ -9,7 +9,6 @@ all = ["auth", "cloud_storage", "tracking", "user_permissions"]auth = ["dep:macro_auth", "dep:url"]cloud_storage = ["dep:comms_service_client",- "dep:email_service_client","dep:macro_share_permissions","dep:permission","macro_db_client",@@ -24,7 +23,6 @@ user_permissions = ["dep:macro_user_id", "dep:user_quota", "macro_db_client"]anyhow = { workspace = true }axum = { workspace = true, features = ["macros"] }comms_service_client = { path = "../comms_service_client", optional = true }-email_service_client = { path = "../email_service_client", optional = true }http-body-util = { workspace = true }macro_auth = { path = "../macro_auth", optional = true }macro_db_client = { path = "../macro_db_client", optional = true }diff --git a/rust/cloud-storage/macro_middleware/src/cloud_storage/thread/ensure_thread_exists.rs b/rust/cloud-storage/macro_middleware/src/cloud_storage/thread/ensure_thread_exists.rsindex 0e07cd74d2..74cf8d5cae 100644--- a/rust/cloud-storage/macro_middleware/src/cloud_storage/thread/ensure_thread_exists.rs+++ b/rust/cloud-storage/macro_middleware/src/cloud_storage/thread/ensure_thread_exists.rs@@ -4,14 +4,13 @@ use axum::extract::{Path, Request, State};use axum::http::StatusCode;use axum::middleware::Next;use axum::response::Response;-use email_service_client::EmailServiceClient;use macro_user_id::cowlike::CowLike;+use macro_user_id::user_id::MacroUserIdStr;use model::thread::EmailThreadPermission;use models_permissions::share_permission::SharePermissionV2;use models_permissions::share_permission::access_level::AccessLevel;use serde::Deserialize;use sqlx::{PgPool, Pool, Postgres};-use std::sync::Arc;#[derive(Deserialize)]pub struct ThreadParams {@@ -21,12 +20,11 @@ pub struct ThreadParams {/// Validates the thread exists and inserts EmailThreadPermission into req contextpub async fn handler(State(db): State<PgPool>,- State(email_client): State<Arc<EmailServiceClient>>,Path(ThreadParams { thread_id }): Path<ThreadParams>,mut req: Request,next: Next,) -> Result<Response, Response> {- let permission = insert_thread_share_permissions(&db, &email_client, &thread_id)+ let permission = insert_thread_share_permissions(&db, &thread_id).await.map_err(|e| {tracing::error!(error=?e, "failed to ensure thread exists");@@ -37,10 +35,9 @@ pub async fn handler(Ok(next.run(req).await)}-// insert SharePermission and EmailThreadPermission for thread if it doesn't already exist.+/// Insert SharePermission and EmailThreadPermission for thread if it doesn't already exist.pub async fn insert_thread_share_permissions(db: &Pool<Postgres>,- email_service_client: &email_service_client::EmailServiceClient,thread_id: &str,) -> anyhow::Result<EmailThreadPermission> {// Ensure permissions don't already exist@@ -54,12 +51,18 @@ pub async fn insert_thread_share_permissions(}// Get the thread owner- let owner_result = email_service_client- .get_thread_owner(thread_id)- .await- .context("Failed to get thread owner from email-service")?;+ let owner_result: Option<String> =+ macro_db_client::share_permission::get::get_macro_id_from_thread_id(db, thread_id)+ .await+ .context("Failed to get thread owner for email")?;- let owner_id = owner_result.user_id;+ let owner_id = if let Some(owner_result) = owner_result {+ MacroUserIdStr::parse_from_str(&owner_result)+ .context("invalid macro user id")?+ .into_owned()+ } else {+ anyhow::bail!("thread not found");+ };// Create a new share permissionlet share_permission = SharePermissionV2 {