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(soup): soup without email link (#427)

Sean Aye22b6bd1c4335
diff --git a/rust/cloud-storage/soup/Cargo.toml b/rust/cloud-storage/soup/Cargo.toml
index 28aaf48cbb..6bcfb99f32 100644
--- a/rust/cloud-storage/soup/Cargo.toml
+++ b/rust/cloud-storage/soup/Cargo.toml
@@ -12,6 +12,7 @@ axum = [
"dep:model_user",
"dep:utoipa",
"email/axum",
+ "item_filters/schema",
"models_pagination/axum",
"models_soup/schema",
]
diff --git a/rust/cloud-storage/soup/src/domain/models.rs b/rust/cloud-storage/soup/src/domain/models.rs
index 2252ee9dfc..c544a4b8ea 100644
--- a/rust/cloud-storage/soup/src/domain/models.rs
+++ b/rust/cloud-storage/soup/src/domain/models.rs
@@ -91,6 +91,7 @@ pub struct AdvancedSortParams<'a> {
pub user_id: MacroUserIdStr<'a>,
}
+#[derive(Debug)]
pub enum SoupQuery {
Simple(Query<Uuid, SimpleSortMethod, Option<EntityFilterAst>>),
Frecency(Query<Uuid, Frecency, Option<EntityFilterAst>>),
@@ -105,13 +106,14 @@ impl SoupQuery {
}
}
+#[derive(Debug)]
pub struct SoupRequest {
pub soup_type: SoupType,
pub limit: u16,
pub cursor: SoupQuery,
pub user: MacroUserIdStr<'static>,
pub email_preview_view: PreviewView,
- pub link_id: Uuid,
+ pub link_id: Option<Uuid>,
}
impl SoupRequest {
@@ -120,7 +122,7 @@ impl SoupRequest {
pub(crate) fn build_email_request(&self) -> Option<GetEmailsRequest> {
Some(GetEmailsRequest {
view: self.email_preview_view.clone(),
- link_id: self.link_id,
+ link_id: self.link_id?,
macro_id: self.user.clone(),
limit: Some(self.limit as u32),
query: match &self.cursor {
diff --git a/rust/cloud-storage/soup/src/domain/service/tests.rs b/rust/cloud-storage/soup/src/domain/service/tests.rs
index 56188b7125..2b0284f820 100644
--- a/rust/cloud-storage/soup/src/domain/service/tests.rs
+++ b/rust/cloud-storage/soup/src/domain/service/tests.rs
@@ -130,7 +130,7 @@ async fn it_should_not_query_frecency() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 0,
cursor: SoupQuery::Simple(Query::Sort(SimpleSortMethod::ViewedUpdated, None)),
@@ -207,7 +207,7 @@ async fn it_should_query_frecency() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: u16::MAX,
cursor: SoupQuery::Frecency(Query::Sort(Frecency, None)),
@@ -284,7 +284,7 @@ async fn it_should_sort_frecency_descending() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: u16::MAX,
cursor: SoupQuery::Frecency(Query::Sort(Frecency, None)),
@@ -371,7 +371,7 @@ async fn frecency_should_fallback() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 100,
cursor: SoupQuery::Frecency(Query::Sort(Frecency, None)),
@@ -441,7 +441,7 @@ async fn frecency_should_paginate() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 100,
cursor: SoupQuery::Frecency(Query::Sort(Frecency, None)),
@@ -513,7 +513,7 @@ async fn frecency_should_resume_cursor() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 100,
cursor: SoupQuery::Frecency(Query::Cursor(Cursor {
@@ -601,7 +601,7 @@ async fn frecency_fallback_cursor_should_resume() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 100,
cursor: SoupQuery::Frecency(Query::Cursor(Cursor {
@@ -666,7 +666,7 @@ async fn cursor_should_return_simple_sort() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 0,
cursor: SoupQuery::Simple(Query::Sort(SimpleSortMethod::ViewedUpdated, None)),
@@ -724,7 +724,7 @@ async fn cursor_should_return_frecency() {
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: Uuid::new_v4(),
+ link_id: Some(Uuid::new_v4()),
soup_type: SoupType::UnExpanded,
limit: 100,
cursor: SoupQuery::Frecency(Query::Sort(Frecency, None)),
diff --git a/rust/cloud-storage/soup/src/inbound/axum_router.rs b/rust/cloud-storage/soup/src/inbound/axum_router.rs
index 36b1ee04fb..8315a593ae 100644
--- a/rust/cloud-storage/soup/src/inbound/axum_router.rs
+++ b/rust/cloud-storage/soup/src/inbound/axum_router.rs
@@ -15,7 +15,7 @@ use email::{
models::{Link, PreviewView},
ports::EmailService,
},
- inbound::{EmailLinkExtractor, EmailPreviewState},
+ inbound::{EmailLinkErr, EmailLinkExtractor, EmailPreviewState},
};
use item_filters::{
EntityFilters,
@@ -115,7 +115,7 @@ where
async fn handle(
&self,
macro_user_id: MacroUserIdStr<'static>,
- email_link: Link,
+ email_link: Option<Link>,
PostSoupRequest { filters, params }: PostSoupRequest,
cursor: SoupCursor,
) -> Result<Json<PaginatedOpaqueCursor<SoupApiItem>>, SoupHandlerErr> {
@@ -162,7 +162,7 @@ where
email_preview_view: PreviewView::StandardLabel(
email::domain::models::PreviewViewStandardLabel::Inbox,
),
- link_id: email_link.id,
+ link_id: email_link.map(|l| l.id),
})
.await?;
@@ -210,6 +210,8 @@ impl SoupApiItem {
pub enum SoupHandlerErr {
#[error("An internal server error has occurred")]
Internal(#[from] SoupErr),
+ #[error("An internal server error has occurred")]
+ EmailLinkErr(#[from] EmailLinkErr),
#[error("Invalid filter arguments provided")]
ExpandErr(#[from] ExpandErr),
}
@@ -243,7 +245,7 @@ impl IntoResponse for SoupHandlerErr {
pub async fn get_soup_handler<T, U>(
State(service): State<SoupRouterState<T, U>>,
Cached(MacroUserExtractor { macro_user_id, .. }): Cached<MacroUserExtractor>,
- Cached(EmailLinkExtractor(link, _)): Cached<EmailLinkExtractor<U>>,
+ email_link: Result<Cached<EmailLinkExtractor<U>>, EmailLinkErr>,
Query(params): Query<Params>,
cursor: SoupCursor,
) -> Result<Json<PaginatedOpaqueCursor<SoupApiItem>>, SoupHandlerErr>
@@ -251,6 +253,11 @@ where
T: SoupService,
U: EmailService,
{
+ let link = match email_link {
+ Ok(l) => Some(l.0.0),
+ Err(EmailLinkErr::NotFound) => None,
+ Err(e) => Err(e)?,
+ };
service
.handle(
macro_user_id,
@@ -293,7 +300,7 @@ type SoupCursor = EitherWrapper<
pub async fn post_soup_handler<T, U>(
State(service): State<SoupRouterState<T, U>>,
Cached(MacroUserExtractor { macro_user_id, .. }): Cached<MacroUserExtractor>,
- Cached(EmailLinkExtractor(link, _)): Cached<EmailLinkExtractor<U>>,
+ email_link: Result<Cached<EmailLinkExtractor<U>>, EmailLinkErr>,
cursor: SoupCursor,
Json(post_soup_request): Json<PostSoupRequest>,
) -> Result<Json<PaginatedOpaqueCursor<SoupApiItem>>, SoupHandlerErr>
@@ -301,6 +308,11 @@ where
T: SoupService,
U: EmailService,
{
+ let link = match email_link {
+ Ok(l) => Some(l.0.0),
+ Err(EmailLinkErr::NotFound) => None,
+ Err(e) => Err(e)?,
+ };
service
.handle(macro_user_id, link, post_soup_request, cursor)
.await
diff --git a/rust/cloud-storage/soup/src/inbound/axum_router/tests.rs b/rust/cloud-storage/soup/src/inbound/axum_router/tests.rs
index 4c33cb2f83..47af4edb7c 100644
--- a/rust/cloud-storage/soup/src/inbound/axum_router/tests.rs
+++ b/rust/cloud-storage/soup/src/inbound/axum_router/tests.rs
@@ -2,6 +2,7 @@ use axum::{
Extension, Router,
http::{Request, StatusCode},
};
+use cool_asserts::assert_matches;
use email::domain::{
models::{EmailErr, UserProvider},
ports::EmailService,
@@ -10,12 +11,13 @@ use http_body_util::BodyExt;
use macro_user_id::{email::EmailStr, user_id::MacroUserIdStr};
use model_user::UserContext;
use serde_json::json;
+use std::sync::Arc;
use tower::util::ServiceExt;
use uuid::Uuid;
use crate::{
domain::{
- models::SoupErr,
+ models::{SoupErr, SoupRequest},
ports::{SoupOutput, SoupService},
},
inbound::axum_router::{SoupRouterState, soup_router},
@@ -23,13 +25,25 @@ use crate::{
static CURSOR: &str = "eyJpZCI6ImUzNmM5MTJlLTU2M2MtNDIxZS1iMTAzLWE0YjAwY2ZmMzBlZSIsImxpbWl0IjoxMDAsInZhbCI6eyJzb3J0X3R5cGUiOiJ1cGRhdGVkX2F0IiwibGFzdF92YWwiOiIyMDI1LTExLTA3VDE5OjEyOjU5Ljc4MFoifX0=";
-struct MockSoup;
+struct MockSoup {
+ called: Arc<std::sync::Mutex<Vec<SoupRequest>>>,
+}
+
+impl MockSoup {
+ fn new() -> Self {
+ MockSoup {
+ called: Arc::new(std::sync::Mutex::new(Vec::new())),
+ }
+ }
+}
impl SoupService for MockSoup {
async fn get_user_soup(
&self,
- _req: crate::domain::models::SoupRequest,
+ req: crate::domain::models::SoupRequest,
) -> Result<SoupOutput, SoupErr> {
+ let mut guard = self.called.lock().unwrap();
+ guard.push(req);
Err(SoupErr::SoupDbErr(anyhow::anyhow!("Not implemented")))
}
}
@@ -71,7 +85,7 @@ impl EmailService for MockEmail {
}
fn mock_router() -> Router {
- soup_router(SoupRouterState::new(MockSoup, MockEmail)).layer(Extension(UserContext {
+ soup_router(SoupRouterState::new(MockSoup::new(), MockEmail)).layer(Extension(UserContext {
user_id: "macro|[email protected]".to_string(),
fusion_user_id: "1234".to_string(),
permissions: None,
@@ -99,3 +113,97 @@ async fn it_should_deserialize_empty_filter() {
})
);
}
+
+struct MockEmailLinkResult {
+ get_link_result: Arc<
+ dyn Fn() -> Result<Option<email::domain::models::Link>, email::domain::models::EmailErr>
+ + Send
+ + Sync,
+ >,
+}
+
+impl EmailService for MockEmailLinkResult {
+ async fn get_email_thread_previews(
+ &self,
+ _req: email::domain::models::GetEmailsRequest,
+ ) -> Result<
+ models_pagination::PaginatedCursor<
+ email::domain::models::EnrichedEmailThreadPreview,
+ uuid::Uuid,
+ models_pagination::SimpleSortMethod,
+ (),
+ >,
+ email::domain::models::EmailErr,
+ > {
+ Err(EmailErr::RepoErr(anyhow::anyhow!("Not implemented")))
+ }
+
+ async fn get_link_by_auth_id_and_macro_id(
+ &self,
+ _auth_id: &str,
+ _macro_id: macro_user_id::user_id::MacroUserIdStr<'_>,
+ ) -> Result<Option<email::domain::models::Link>, email::domain::models::EmailErr> {
+ (self.get_link_result)()
+ }
+}
+
+#[tokio::test]
+async fn it_calls_soup_with_missing_link() {
+ let soup = MockSoup::new();
+ let inner_counter = soup.called.clone();
+ let router: Router = soup_router(SoupRouterState::new(
+ soup,
+ MockEmailLinkResult {
+ get_link_result: Arc::new(|| Ok(None)),
+ },
+ ))
+ .layer(Extension(UserContext {
+ user_id: "macro|[email protected]".to_string(),
+ fusion_user_id: "1234".to_string(),
+ permissions: None,
+ organization_id: None,
+ }));
+
+ let request = Request::builder()
+ .uri(format!("/soup?cursor={CURSOR}"))
+ .body(axum::body::Body::empty())
+ .unwrap();
+
+ let _res = router.oneshot(request).await.unwrap();
+
+ let guard = inner_counter.lock().unwrap();
+
+ assert_eq!(guard.len(), 1);
+ assert_matches!(guard.first().unwrap(), SoupRequest { link_id: None, .. })
+}
+
+#[tokio::test]
+async fn it_does_not_call_soup_with_db_err() {
+ let soup = MockSoup::new();
+ let inner_counter = soup.called.clone();
+ let router: Router = soup_router(SoupRouterState::new(
+ soup,
+ MockEmailLinkResult {
+ get_link_result: Arc::new(|| {
+ Err(EmailErr::RepoErr(anyhow::anyhow!("failed to fetch")))
+ }),
+ },
+ ))
+ .layer(Extension(UserContext {
+ user_id: "macro|[email protected]".to_string(),
+ fusion_user_id: "1234".to_string(),
+ permissions: None,
+ organization_id: None,
+ }));
+
+ let request = Request::builder()
+ .uri(format!("/soup?cursor={CURSOR}"))
+ .body(axum::body::Body::empty())
+ .unwrap();
+
+ let _res = router.oneshot(request).await.unwrap();
+
+ let guard = inner_counter.lock().unwrap();
+
+ assert_eq!(guard.len(), 0);
+}