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.

Seanaye/fix/unexpanded soup ast (#3151)

Sean Ayeee8014918617
diff --git a/rust/cloud-storage/item_filters/src/ast.rs b/rust/cloud-storage/item_filters/src/ast.rs
index 96018b52dc..bde916d25f 100644
--- a/rust/cloud-storage/item_filters/src/ast.rs
+++ b/rust/cloud-storage/item_filters/src/ast.rs
@@ -76,6 +76,9 @@ pub enum ExpandErr {
/// invalid entity reference id
#[error(transparent)]
EntityRefId(#[from] properties::EntityRefIdError),
+ /// invalid API AST expansion
+ #[error("invalid API AST expansion: {0}")]
+ ApiAst(String),
}
/// type alias for a maybe empty, cheaply cloneable ast literal tree
diff --git a/rust/cloud-storage/soup/src/inbound/axum_router.rs b/rust/cloud-storage/soup/src/inbound/axum_router.rs
index 32cf27ae3b..c495623bf3 100644
--- a/rust/cloud-storage/soup/src/inbound/axum_router.rs
+++ b/rust/cloud-storage/soup/src/inbound/axum_router.rs
@@ -1,5 +1,8 @@
use crate::domain::{
- models::{FrecencySoupItem, IntoSoupReqAst, SoupErr, SoupQuery, SoupRequest, SoupType},
+ models::{
+ FrecencyQueryInner, FrecencySoupItem, IntoSoupReqAst, SimpleQueryInner, SoupErr, SoupQuery,
+ SoupRequest, SoupType,
+ },
ports::SoupService,
};
use axum::{
@@ -34,6 +37,7 @@ use models_pagination::{
TypeEraseCursor,
};
use models_soup::item::SoupItem;
+use non_empty::IsEmpty;
use recursion::CollapsibleExt;
use rootcause::{Report, report};
use serde::{Deserialize, Serialize};
@@ -398,7 +402,7 @@ pub async fn post_soup_ast_handler<T, U>(
State(service): State<SoupRouterState<T, U>>,
Cached(MacroUserExtractor { macro_user_id, .. }): Cached<MacroUserExtractor>,
email_link: Result<Cached<EmailLinkExtractor<U>>, EmailLinkErr>,
- cursor: SoupCursor<EntityFilterAst>,
+ cursor: SoupCursor<ApiEntityFilterAst>,
Json(PostSoupAstRequest {
filters,
params,
@@ -414,9 +418,6 @@ where
Err(EmailLinkErr::NotFound) => None,
Err(e) => Err(e)?,
};
- let filters = filters
- .into_entity_ast()
- .map_err(|_| SoupHandlerErr::Expand)?;
service
.handle(
macro_user_id,
@@ -469,7 +470,47 @@ pub enum CompoundDocumentLiteral {
FileAssoc(String),
}
+impl IntoSoupReqAst for SoupRequest<ApiEntityFilterAst> {
+ fn into_ast(self) -> Result<SoupRequest<Option<EntityFilterAst>>, ExpandErr> {
+ let SoupRequest {
+ soup_type,
+ limit,
+ cursor,
+ user,
+ email_preview_view,
+ link_id,
+ } = self;
+
+ let cursor = match cursor {
+ SoupQuery::Simple(SimpleQueryInner(query)) => SoupQuery::Simple(SimpleQueryInner(
+ query.try_map_filter(ApiEntityFilterAst::into_optional_entity_ast)?,
+ )),
+ SoupQuery::Frecency(FrecencyQueryInner(query)) => {
+ SoupQuery::Frecency(FrecencyQueryInner(
+ query.try_map_filter(ApiEntityFilterAst::into_optional_entity_ast)?,
+ ))
+ }
+ };
+
+ Ok(SoupRequest {
+ soup_type,
+ limit,
+ cursor,
+ user,
+ email_preview_view,
+ link_id,
+ })
+ }
+}
+
impl ApiEntityFilterAst {
+ fn into_optional_entity_ast(self) -> Result<Option<EntityFilterAst>, ExpandErr> {
+ let ast = self
+ .into_entity_ast()
+ .map_err(|e| ExpandErr::ApiAst(e.to_string()))?;
+ Ok((!ast.is_empty()).then_some(ast))
+ }
+
#[tracing::instrument(err, skip(self))]
fn into_entity_ast(self) -> Result<EntityFilterAst, Report> {
let ApiEntityFilterAst {
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 4fb08ed0e5..fdac472c94 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::{Method, Request, StatusCode},
};
+use chrono::{Duration, Utc};
use email::domain::{
models::{EmailErr, PreviewView, PreviewViewStandardLabel, UserProvider},
ports::EmailService,
@@ -12,7 +13,8 @@ use item_filters::EntityFilters;
use macro_user_id::{email::EmailStr, user_id::MacroUserIdStr};
use model_user::UserContext;
use models_pagination::{
- CursorVal, Frecency, FrecencyValue, Identify, PaginateOn, Query, SortOn, TypeEraseCursor,
+ CursorVal, Frecency, FrecencyValue, Identify, PaginateOn, Query, SimpleSortMethod, SortOn,
+ TypeEraseCursor,
};
use serde::Serialize;
use serde_json::json;
@@ -30,6 +32,7 @@ use crate::{
},
ports::{SoupOutput, SoupService},
},
+ inbound::axum_router::ApiEntityFilterAst,
inbound::axum_router::{SoupRouterState, soup_router},
};
@@ -50,6 +53,7 @@ struct MockSoupCall {
link_id: Option<Uuid>,
cursor_kind: MockCursorKind,
filter: serde_json::Value,
+ expanded_filter: serde_json::Value,
}
#[derive(Clone)]
@@ -81,14 +85,19 @@ impl SoupService for MockSoup {
MockCursorKind::FrecencyCursor
}
};
+ let soup_type = req.soup_type;
+ let email_preview_view = req.email_preview_view.clone();
+ let link_id = req.link_id;
let filter = serde_json::to_value(req.cursor.filter()).unwrap();
+ let expanded_filter = serde_json::to_value(req.into_ast()?.cursor.filter()).unwrap();
let mut guard = self.called.lock().unwrap();
guard.push(MockSoupCall {
- soup_type: req.soup_type,
- email_preview_view: req.email_preview_view,
- link_id: req.link_id,
+ soup_type,
+ email_preview_view,
+ link_id,
cursor_kind,
filter,
+ expanded_filter,
});
Err(SoupErr::SoupDbErr(anyhow::anyhow!("Not implemented")))
}
@@ -983,7 +992,7 @@ async fn ast_endpoint_expands_file_assoc_pdf() {
.expect("SoupService::handle should have been called")
};
- let filter: EntityFilterAst = serde_json::from_value(arg.filter).unwrap();
+ let filter: EntityFilterAst = serde_json::from_value(arg.expanded_filter).unwrap();
let doc_tree = filter
.document_filter
.expect("document_filter should be set");
@@ -1030,7 +1039,7 @@ async fn ast_endpoint_passes_through_plain_document_literal() {
.expect("SoupService::handle should have been called")
};
- let filter: EntityFilterAst = serde_json::from_value(arg.filter).unwrap();
+ let filter: EntityFilterAst = serde_json::from_value(arg.expanded_filter).unwrap();
let doc_tree = filter
.document_filter
.expect("document_filter should be set");
@@ -1076,7 +1085,7 @@ async fn ast_endpoint_expands_file_assoc_image_to_or_tree() {
.expect("SoupService::handle should have been called")
};
- let filter: EntityFilterAst = serde_json::from_value(arg.filter).unwrap();
+ let filter: EntityFilterAst = serde_json::from_value(arg.expanded_filter).unwrap();
let doc_tree = filter
.document_filter
.expect("document_filter should be set");
@@ -1128,3 +1137,146 @@ async fn ast_endpoint_expands_file_assoc_image_to_or_tree() {
"image association should expand to multiple file types"
);
}
+
+#[tokio::test]
+async fn it_can_expand_assoc_ast() {
+ let js = json!({
+ "df": {
+ "&": [
+ {
+ "l": {
+ "fa": "assoc:code"
+ }
+ },
+ {
+ "!": {
+ "l": {
+ "dst": "task"
+ }
+ }
+ }
+ ]
+ },
+ "ef": {
+ "l": {
+ "ThreadId": "00000000-0000-0000-0000-000000000000"
+ }
+ },
+ "chanf": {
+ "l": {
+ "ChannelId": "00000000-0000-0000-0000-000000000000"
+ }
+ },
+ "cf": {
+ "l": {
+ "cid": "00000000-0000-0000-0000-000000000000"
+ }
+ },
+ "pf": {
+ "l": {
+ "pid": "00000000-0000-0000-0000-000000000000"
+ }
+ },
+ "callf": {
+ "l": {
+ "ChannelId": "00000000-0000-0000-0000-000000000000"
+ }
+ },
+ "limit": 100,
+ "sort_method": "updated_at"
+ });
+
+ let soup = MockSoup::new();
+ let inner_counter = soup.called.clone();
+ let router: Router = soup_router(SoupRouterState::new(
+ soup.clone(),
+ 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("/soup/ast")
+ .method(Method::POST)
+ .header("content-type", "application/json")
+ .body(axum::body::Body::from(serde_json::to_vec(&js).unwrap()))
+ .unwrap();
+
+ let _res = router.oneshot(request).await.unwrap();
+
+ {
+ let mut guard = inner_counter.lock().unwrap();
+ guard
+ .pop()
+ .expect("SoupService::handle should have been called");
+ }
+
+ let filter: ApiEntityFilterAst = serde_json::from_value(js.clone()).unwrap();
+
+ #[derive(Serialize)]
+ struct Data(chrono::DateTime<Utc>, Uuid);
+
+ impl Identify for Data {
+ type Id = Uuid;
+
+ fn id(&self) -> Uuid {
+ self.1
+ }
+ }
+
+ impl SortOn<SimpleSortMethod> for Data {
+ fn sort_on(
+ sort_type: SimpleSortMethod,
+ ) -> impl FnMut(&Self) -> CursorVal<SimpleSortMethod> {
+ move |v| CursorVal {
+ sort_type,
+ last_val: v.0,
+ }
+ }
+ }
+
+ let now = Utc::now();
+ let res = (0..1000)
+ .map(|x| Data(now - Duration::seconds(x), Uuid::new_v4()))
+ .paginate_on(100, SimpleSortMethod::UpdatedAt)
+ .filter_on(filter)
+ .into_page();
+
+ let cursor = res.type_erase().next_cursor.unwrap();
+
+ let request2 = Request::builder()
+ .uri(format!("/soup/ast?cursor={cursor}"))
+ .method(Method::POST)
+ .header("content-type", "application/json")
+ .body(axum::body::Body::from(
+ serde_json::to_vec(&serde_json::json!({})).unwrap(),
+ ))
+ .unwrap();
+
+ 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 _res = router.oneshot(request2).await.unwrap();
+
+ let guard = inner_counter.lock().unwrap();
+ let req = guard
+ .first()
+ .expect("SoupService::handle should have been called with next cursor");
+ assert!(matches!(req.cursor_kind, MockCursorKind::SimpleCursor));
+}