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): Allow users without email enabled to see shared threads (#882)

Evan Hutnik5322f35093ef
diff --git a/rust/cloud-storage/email/src/inbound/axum/axum_impls.rs b/rust/cloud-storage/email/src/inbound/axum/axum_impls.rs
index 4c30ff3d4b..09e8a13cbc 100644
--- a/rust/cloud-storage/email/src/inbound/axum/axum_impls.rs
+++ b/rust/cloud-storage/email/src/inbound/axum/axum_impls.rs
@@ -123,3 +123,36 @@ where
Ok(Self(res, PhantomData))
}
}
+
+/// Extractor that returns `Option<Link>` - returns `None` if no link is found
+/// instead of failing with a 404 error.
+pub struct OptionalEmailLinkExtractor<U>(pub Option<Link>, pub PhantomData<U>);
+
+impl<U> Clone for OptionalEmailLinkExtractor<U> {
+ fn clone(&self) -> Self {
+ Self(self.0.clone(), PhantomData)
+ }
+}
+
+#[async_trait]
+impl<S, U> FromRequestParts<S> for OptionalEmailLinkExtractor<U>
+where
+ EmailPreviewState<U>: FromRef<S>,
+ U: EmailService,
+ S: Send + Sync + 'static,
+{
+ type Rejection = EmailLinkErr;
+
+ async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
+ let Cached(MacroUserExtractor {
+ macro_user_id,
+ user_context,
+ ..
+ }) = parts.extract_with_state(state).await?;
+ let res = <EmailPreviewState<U>>::from_ref(state)
+ .inner
+ .get_link_by_auth_id_and_macro_id(&user_context.fusion_user_id, macro_user_id)
+ .await?;
+ Ok(Self(res, PhantomData))
+ }
+}
diff --git a/rust/cloud-storage/email_service/src/api/email/threads/get.rs b/rust/cloud-storage/email_service/src/api/email/threads/get.rs
index 162547c0a5..df2ea6918e 100644
--- a/rust/cloud-storage/email_service/src/api/email/threads/get.rs
+++ b/rust/cloud-storage/email_service/src/api/email/threads/get.rs
@@ -4,6 +4,8 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json, extract};
+use email::domain::ports::EmailService;
+use email::inbound::OptionalEmailLinkExtractor;
use futures::future::join_all;
use model::response::ErrorResponse;
use model::user::UserContext;
@@ -106,14 +108,15 @@ const MESSAGE_MAX: i64 = 100;
(status = 500, body=ErrorResponse),
)
)]
-#[tracing::instrument(skip(ctx, user_context), fields(user_id=user_context.user_id, fusionauth_user_id=user_context.fusion_user_id))]
-pub async fn get_thread_handler(
+#[tracing::instrument(skip(ctx, user_context, link), fields(user_id=user_context.user_id, fusionauth_user_id=user_context.fusion_user_id))]
+pub async fn get_thread_handler<U: EmailService>(
State(ctx): State<ApiContext>,
user_context: Extension<UserContext>,
- link: Extension<Link>,
+ link: OptionalEmailLinkExtractor<U>,
Path(PathParams { id: thread_id }): Path<PathParams>,
extract::Query(query_params): extract::Query<GetThreadParams>,
) -> Result<Response, GetThreadError> {
+ let link = link.0;
let p = process_get_thread_params(&query_params);
let mut thread = email_db_client::threads::get::fetch_thread_with_messages_paginated(
@@ -127,12 +130,24 @@ pub async fn get_thread_handler(
return Err(GetThreadError::ThreadNotFound);
}
- // if the requester doesn't own the thread, check if it has been shared with the requester
- let access_level = if thread.link_id != link.id {
- // call will fail if user doesn't have an access level. otherwise, we can return the thread
+ // Check if user owns the thread via their link
+ let is_owner = link
+ .as_ref()
+ .map(|l| thread.link_id == l.id)
+ .unwrap_or(false);
+
+ let access_level = if is_owner {
+ AccessLevel::Owner
+ } else {
+ // If user doesn't have email enabled (and thus has no link), use user_context's macro_id
+ let macro_id = link
+ .as_ref()
+ .map(|l| l.macro_id.as_ref())
+ .unwrap_or(&user_context.user_id);
+
let access_level = ctx
.dss_client
- .get_thread_access_level(link.macro_id.as_ref(), &thread.db_id.unwrap().to_string())
+ .get_thread_access_level(macro_id, &thread.db_id.unwrap().to_string())
.await
.map_err(|e| {
tracing::error!(error=?e, "unable to get access level for thread for user");
@@ -141,9 +156,6 @@ pub async fn get_thread_handler(
// don't include the owner's drafts if it's a shared thread
thread.messages.retain(|m| !m.is_draft);
access_level
- } else {
- // it's the user's thread
- AccessLevel::Owner
};
let tasks: Vec<_> = thread
diff --git a/rust/cloud-storage/email_service/src/api/email/threads/mod.rs b/rust/cloud-storage/email_service/src/api/email/threads/mod.rs
index 504d63aad2..6ca892ffc9 100644
--- a/rust/cloud-storage/email_service/src/api/email/threads/mod.rs
+++ b/rust/cloud-storage/email_service/src/api/email/threads/mod.rs
@@ -9,12 +9,11 @@ use tower::ServiceBuilder;
use crate::api::ApiContext;
pub fn router(state: ApiContext) -> Router<ApiContext> {
- Router::new()
+ let required_link_routes = Router::new()
.nest(
"/previews",
email::inbound::router(state.email_service.clone()),
)
- .route("/:id", get(get::get_thread_handler))
.route(
"/:id/seen",
post(seen::seen_handler).layer(axum::middleware::from_fn_with_state(
@@ -33,7 +32,14 @@ pub fn router(state: ApiContext) -> Router<ApiContext> {
)),
)
.layer(axum::middleware::from_fn_with_state(
- state.email_service,
+ state.email_service.clone(),
crate::api::middleware::link::attach_link_context,
- ))
+ ));
+
+ // user can still view threads shared with them if they don't have email enabled
+ let optional_link_routes = Router::new().route("/:id", get(get::get_thread_handler));
+
+ Router::new()
+ .merge(required_link_routes)
+ .merge(optional_link_routes)
}