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/feat/grouped gql soup (#4872)

Sean Aye19ec42555e65
diff --git a/Cargo.lock b/Cargo.lock
index ce558ccacd..caaffcfada 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2866,6 +2866,7 @@ dependencies = [
"model_user",
"models_pagination",
"models_properties",
+ "models_soup",
"serde",
"soup",
"tokio",
@@ -6035,6 +6036,7 @@ dependencies = [
"model-entity",
"model_file_type",
"model_user",
+ "models_grouping",
"models_pagination",
"models_soup",
"serde",
@@ -13271,6 +13273,7 @@ dependencies = [
"frecency",
"futures",
"http-body-util",
+ "indexmap 2.14.0",
"item_filters",
"macro_db_migrator",
"macro_user_id",
diff --git a/crates/complete_graph/Cargo.toml b/crates/complete_graph/Cargo.toml
index 493d49eacd..8151dd5250 100644
--- a/crates/complete_graph/Cargo.toml
+++ b/crates/complete_graph/Cargo.toml
@@ -33,6 +33,7 @@ apollo-compiler = { workspace = true }
macro_user_id = { path = "../macro_user_id" }
models_pagination = { path = "../models_pagination" }
models_properties = { path = "../models_properties" }
+models_soup = { path = "../models_soup" }
serde = { workspace = true }
tokio = { workspace = true }
uuid = { workspace = true }
diff --git a/crates/complete_graph/src/schema.rs b/crates/complete_graph/src/schema.rs
index 5042a131fb..c0bf15f021 100644
--- a/crates/complete_graph/src/schema.rs
+++ b/crates/complete_graph/src/schema.rs
@@ -18,7 +18,9 @@ use graphql_properties::{
EntityPropertyReader, EntityPropertyWriter, NoOpEntityPropertyReader, NoOpEntityPropertyWriter,
PropertiesMutationRoot,
};
-use graphql_soup::{SoupInput, SoupPage, resolve_soup};
+use graphql_soup::{
+ GroupedSoup, GroupedSoupInput, SoupInput, SoupPage, resolve_grouped_soup, resolve_soup,
+};
use model_user::axum_extractor::MacroUserExtractor;
use soup::domain::ports::{NoOpSoupService, SoupService};
@@ -190,6 +192,15 @@ where
Ok(async_graphql::ID(macro_user_id.to_string()))
}
+ /// Fetch Soup items nested into grouping bins.
+ async fn group_soup(
+ &self,
+ ctx: &Context<'_>,
+ input: GroupedSoupInput,
+ ) -> async_graphql::Result<GroupedSoup<SoupEdges<NR, PR, ER>>> {
+ resolve_grouped_soup::<S, St, SoupEdges<NR, PR, ER>>(&self.service, ctx, input).await
+ }
+
/// Fetch a page of Soup items using the existing Soup filter AST format.
async fn soup(
&self,
diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rs
index b34f644318..1ca6265af6 100644
--- a/crates/complete_graph/src/schema/test.rs
+++ b/crates/complete_graph/src/schema/test.rs
@@ -16,6 +16,7 @@ use macro_user_id::{
};
use model_user::UserContext;
use models_pagination::{PaginatedCursor, SimpleSortMethod};
+use models_soup::{document::SoupDocument, item::SoupItem};
use uuid::Uuid;
use super::*;
@@ -24,6 +25,28 @@ use super::*;
struct CountingSoupService {
raw_calls: Arc<AtomicUsize>,
frecency_calls: Arc<AtomicUsize>,
+ grouped_calls: Arc<AtomicUsize>,
+}
+
+fn grouped_document(id: Uuid) -> SoupItem<soup::domain::models::SoupPropertiesField> {
+ SoupItem::Document(SoupDocument {
+ id,
+ document_version_id: 1,
+ owner_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ name: format!("Document {id}"),
+ file_type: None,
+ sha: None,
+ project_id: None,
+ branched_from_id: None,
+ branched_from_version_id: None,
+ document_family_id: None,
+ created_at: Default::default(),
+ updated_at: Default::default(),
+ viewed_at: Default::default(),
+ sub_type: None,
+ deleted_at: None,
+ extra: soup::domain::models::SoupPropertiesField::default(),
+ })
}
fn test_soup_err() -> soup::domain::models::SoupErr {
@@ -94,10 +117,29 @@ impl SoupService for CountingSoupService {
&self,
_req: soup::domain::models::GroupedSortRequest<'_>,
) -> Result<
- Vec<soup::domain::models::GroupedSoupItem<soup::domain::models::SoupPropertiesField>>,
+ impl Iterator<
+ Item = soup::domain::models::grouping::ItemGroupingInfo<
+ soup::domain::models::SoupPropertiesField,
+ >,
+ > + Send,
soup::domain::models::SoupErr,
> {
- Err(test_soup_err())
+ self.grouped_calls.fetch_add(1, Ordering::SeqCst);
+ Ok(vec![
+ soup::domain::models::grouping::ItemGroupingInfo {
+ key: "document".to_string(),
+ total_group_count: 2,
+ index_in_group: 1,
+ item: grouped_document(Uuid::from_u128(1)),
+ },
+ soup::domain::models::grouping::ItemGroupingInfo {
+ key: "document".to_string(),
+ total_group_count: 2,
+ index_in_group: 2,
+ item: grouped_document(Uuid::from_u128(2)),
+ },
+ ]
+ .into_iter())
}
async fn caller_tag_sets<'a>(
@@ -377,6 +419,7 @@ struct TestHarness {
team_calls: Arc<AtomicUsize>,
raw_soup_calls: Arc<AtomicUsize>,
frecency_soup_calls: Arc<AtomicUsize>,
+ grouped_soup_calls: Arc<AtomicUsize>,
}
fn harness() -> TestHarness {
@@ -387,6 +430,7 @@ fn harness() -> TestHarness {
let team_calls = Arc::clone(&entity_access.team_calls);
let raw_soup_calls = Arc::clone(&soup.raw_calls);
let frecency_soup_calls = Arc::clone(&soup.frecency_calls);
+ let grouped_soup_calls = Arc::clone(&soup.grouped_calls);
TestHarness {
schema: build_schema_with_service(soup),
state: TestState {
@@ -397,6 +441,7 @@ fn harness() -> TestHarness {
team_calls,
raw_soup_calls,
frecency_soup_calls,
+ grouped_soup_calls,
}
}
@@ -461,6 +506,26 @@ async fn soup_requests_frecency_only_when_selected() {
assert_eq!(harness.frecency_soup_calls.load(Ordering::SeqCst), 1);
}
+#[tokio::test]
+async fn group_soup_nests_items_in_bins_and_preserves_database_order() {
+ let harness = harness();
+
+ let response = harness
+ .execute(
+ "{ user { groupSoup(input: {groupBy: {field: ENTITY_TYPE}}) { bins { key totalCount items { id entityType } } } } }",
+ )
+ .await;
+
+ assert!(response.errors.is_empty(), "{:?}", response.errors);
+ assert_eq!(
+ response.data.to_string(),
+ r#"{user: {groupSoup: {bins: [{key: "document", totalCount: 2, items: [{id: "00000000-0000-0000-0000-000000000001", entityType: DOCUMENT}, {id: "00000000-0000-0000-0000-000000000002", entityType: DOCUMENT}]}]}}}"#
+ );
+ assert_eq!(harness.grouped_soup_calls.load(Ordering::SeqCst), 1);
+ assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);
+ assert_eq!(harness.team_calls.load(Ordering::SeqCst), 0);
+}
+
#[tokio::test]
async fn crm_scoped_soup_resolves_team_membership_lazily() {
let harness = harness();
diff --git a/crates/graphql_soup/Cargo.toml b/crates/graphql_soup/Cargo.toml
index 7ad56bc18b..0f3a3e5599 100644
--- a/crates/graphql_soup/Cargo.toml
+++ b/crates/graphql_soup/Cargo.toml
@@ -25,6 +25,7 @@ macro_user_id = { path = "../macro_user_id" }
model-entity = { path = "../model-entity" }
model_file_type = { path = "../model_file_type" }
model_user = { path = "../model_user", features = ["axum"] }
+models_grouping = { path = "../models_grouping" }
models_pagination = { path = "../models_pagination" }
models_soup = { path = "../models_soup" }
serde = { workspace = true }
diff --git a/crates/graphql_soup/src/inputs.rs b/crates/graphql_soup/src/inputs.rs
index 8084eba35a..e02e9d0ffe 100644
--- a/crates/graphql_soup/src/inputs.rs
+++ b/crates/graphql_soup/src/inputs.rs
@@ -5,8 +5,8 @@ use chrono::{DateTime, Utc};
use document_sub_type::DocumentSubType;
use filter_ast::Expr;
use graphql_common::{
- GraphqlPropertiesExpr, IntoFilterExpr, filter_expr_input, optional_tree, parse_id,
- parse_macro_user_id,
+ GraphqlPropertiesExpr, GraphqlPropertyEntityType, IntoFilterExpr, filter_expr_input,
+ optional_tree, parse_id, parse_macro_user_id,
};
use item_filters::{
CallStatus, SharedEmailFilter,
@@ -21,12 +21,14 @@ use item_filters::{
email::{Email, EmailLiteral},
foreign_entity::ForeignEntityLiteral,
project::ProjectLiteral,
+ properties::PropertyEntityType,
},
};
use macro_user_id::{cowlike::CowLike, email::EmailStr, user_id::MacroUserIdStr};
use model_file_type::FileType;
-use models_pagination::{Base64Str, CursorWithValAndFilter, SimpleSortMethod};
-use soup::domain::models::{SoupQuery, SoupRequest, SoupType};
+use models_grouping::{GroupByField, GroupingConfig};
+use models_pagination::{Base64Str, CursorWithValAndFilter, Query, SimpleSortMethod};
+use soup::domain::models::{GroupedSortRequest, SoupQuery, SoupRequest, SoupType};
use uuid::Uuid;
/// Input for `Query.soup`.
@@ -47,6 +49,123 @@ pub struct SoupInput {
filters: Option<GraphqlEntityFilterAst>,
}
+/// Input for `Query.groupSoup`.
+#[derive(async_graphql::InputObject)]
+pub struct GroupedSoupInput {
+ /// The field used to divide Soup items into bins.
+ group_by: GraphqlGroupByInput,
+ /// Maximum number of items to return per bin. Defaults to 20, max 500.
+ limit: Option<u16>,
+ /// Sort order within each bin. Defaults to `VIEWED_UPDATED`.
+ sort_method: Option<GraphqlSimpleSortMethod>,
+ /// AST-shaped filters applied to each Soup entity type.
+ filters: Option<GraphqlEntityFilterAst>,
+}
+
+impl GroupedSoupInput {
+ /// Convert this value into the grouped Soup domain request.
+ pub(crate) fn into_request(
+ self,
+ macro_user_id: MacroUserIdStr<'static>,
+ ) -> async_graphql::Result<GroupedSortRequest<'static>> {
+ let filters = self
+ .filters
+ .map(GraphqlEntityFilterAst::into_ast)
+ .transpose()?
+ .unwrap_or_default();
+ let sort_method = self
+ .sort_method
+ .map(SimpleSortMethod::from)
+ .unwrap_or(SimpleSortMethod::ViewedUpdated);
+ let limit = self.limit.unwrap_or(20).min(500);
+
+ Ok(GroupedSortRequest {
+ limit,
+ cursor: Query::Sort(sort_method, filters),
+ user_id: macro_user_id,
+ grouping: GroupingConfig {
+ field: self.group_by.into_group_by_field()?,
+ group_key: None,
+ per_group_limit: Some(u32::from(limit)),
+ },
+ })
+ }
+}
+
+/// GraphQL representation of a field used to group Soup items.
+#[derive(async_graphql::InputObject)]
+struct GraphqlGroupByInput {
+ /// The kind of grouping to perform.
+ field: GraphqlGroupByField,
+ /// Property definition to group by when `field` is `PROPERTY`.
+ property_definition_id: Option<ID>,
+ /// Optional property entity type restriction.
+ entity_type: Option<GraphqlPropertyEntityType>,
+}
+
+impl GraphqlGroupByInput {
+ /// Convert this input into the grouping domain model.
+ fn into_group_by_field(self) -> async_graphql::Result<GroupByField> {
+ match self.field {
+ GraphqlGroupByField::Date => self.without_property_options(GroupByField::Date),
+ GraphqlGroupByField::EntityType => {
+ self.without_property_options(GroupByField::EntityType)
+ }
+ GraphqlGroupByField::Project => self.without_property_options(GroupByField::Project),
+ GraphqlGroupByField::Property => {
+ let property_definition_id = self.property_definition_id.ok_or_else(|| {
+ async_graphql::Error::new(
+ "propertyDefinitionId is required when grouping by PROPERTY",
+ )
+ })?;
+ let property_definition_id =
+ parse_id(property_definition_id, "propertyDefinitionId")?;
+ let entity_type = self
+ .entity_type
+ .map(PropertyEntityType::try_from)
+ .transpose()
+ .map_err(|_| {
+ async_graphql::Error::new(
+ "CALL_RECORD is not supported for property grouping",
+ )
+ })?
+ .map(|entity_type| entity_type.to_string());
+
+ Ok(GroupByField::Property {
+ property_definition_id,
+ entity_type,
+ })
+ }
+ }
+ }
+
+ /// Reject property-only options for non-property grouping modes.
+ fn without_property_options(
+ self,
+ group_by: GroupByField,
+ ) -> async_graphql::Result<GroupByField> {
+ if self.property_definition_id.is_some() || self.entity_type.is_some() {
+ return Err(async_graphql::Error::new(
+ "propertyDefinitionId and entityType require PROPERTY grouping",
+ ));
+ }
+ Ok(group_by)
+ }
+}
+
+/// Grouping modes supported by grouped Soup.
+#[derive(async_graphql::Enum, Copy, Clone, Eq, PartialEq)]
+enum GraphqlGroupByField {
+ /// Group into date buckets.
+ Date,
+ /// Group by Soup entity type.
+ EntityType,
+ /// Group by containing project.
+ Project,
+ /// Group by a property value.
+ Property,
+}
+
impl SoupInput {
/// Convert this value into the request representation.
pub(crate) fn into_request(
diff --git a/crates/graphql_soup/src/lib.rs b/crates/graphql_soup/src/lib.rs
index 055411f3d5..2dc1437b54 100644
--- a/crates/graphql_soup/src/lib.rs
+++ b/crates/graphql_soup/src/lib.rs
@@ -14,11 +14,11 @@ mod objects;
mod resolvers;
pub use graphql_common::{GraphqlSoupEntityType, GraphqlSoupRequestParts};
-pub use inputs::{GraphqlSimpleSortMethod, SoupInput};
+pub use inputs::{GraphqlSimpleSortMethod, GroupedSoupInput, SoupInput};
pub use objects::{
- GraphqlSoupCall, GraphqlSoupChannel, GraphqlSoupChannelThread, GraphqlSoupChat,
+ GraphqlSoupBin, GraphqlSoupCall, GraphqlSoupChannel, GraphqlSoupChannelThread, GraphqlSoupChat,
GraphqlSoupCrmCompany, GraphqlSoupDocument, GraphqlSoupDocumentSubType, GraphqlSoupEmailThread,
- GraphqlSoupEntity, GraphqlSoupForeignEntity, GraphqlSoupItem, GraphqlSoupProject,
+ GraphqlSoupEntity, GraphqlSoupForeignEntity, GraphqlSoupItem, GraphqlSoupProject, GroupedSoup,
SoupEntityEdges, SoupPage,
};
-pub use resolvers::resolve_soup;
+pub use resolvers::{resolve_grouped_soup, resolve_soup};
diff --git a/crates/graphql_soup/src/objects.rs b/crates/graphql_soup/src/objects.rs
index 59082b597a..47c2152824 100644
--- a/crates/graphql_soup/src/objects.rs
+++ b/crates/graphql_soup/src/objects.rs
@@ -1,4 +1,4 @@
-use async_graphql::{ID, Json, Object, ObjectType, Union};
+use async_graphql::{ID, Json, Object, ObjectType, SimpleObject, Union};
use graphql_common::GraphqlSoupEntityType;
use models_pagination::PaginatedOpaqueCursor;
use models_soup::{
@@ -16,7 +16,7 @@ use models_soup::{
project::SoupProject,
};
use serde_json::Value;
-use soup::domain::models::EnrichedSoupItem;
+use soup::domain::models::{EnrichedSoupItem, SoupPropertiesField, grouping::NestedSoupGroups};
use uuid::Uuid;
/// Extension fields attached to every top-level Soup entity.
@@ -36,35 +36,14 @@ pub trait SoupEntityEdges: ObjectType + Clone + Send + Sync + 'static {
}
/// Page returned by `Query.soup`.
+#[derive(SimpleObject)]
pub struct SoupPage<E: SoupEntityEdges> {
- /// The items.
- items: Vec<GraphqlSoupItem<E>>,
- /// The next cursor.
- next_cursor: Option<String>,
- /// Whether the result has more.
- has_more: bool,
-}
-
-/// Page returned by `Query.soup`.
-#[Object(name = "SoupPage")]
-impl<E> SoupPage<E>
-where
- E: SoupEntityEdges,
-{
/// Items in the current page.
- async fn items(&self) -> &[GraphqlSoupItem<E>] {
- &self.items
- }
-
+ items: Vec<GraphqlSoupItem<E>>,
/// Opaque cursor for the next page, if one exists.
- async fn next_cursor(&self) -> Option<&str> {
- self.next_cursor.as_deref()
- }
-
+ next_cursor: Option<String>,
/// Whether more items are available after this page.
- async fn has_more(&self) -> bool {
- self.has_more
- }
+ has_more: bool,
}
impl<E: SoupEntityEdges> From<PaginatedOpaqueCursor<SoupItem<()>>> for SoupPage<E> {
@@ -89,6 +68,42 @@ impl<E: SoupEntityEdges> From<PaginatedOpaqueCursor<EnrichedSoupItem>> for SoupP
}
}
+/// GraphQL representation of grouped Soup items.
+#[derive(SimpleObject)]
+pub struct GroupedSoup<E: SoupEntityEdges> {
+ /// Bins containing the grouped Soup items.
+ bins: Vec<GraphqlSoupBin<E>>,
+}
+
+impl<E: SoupEntityEdges> From<NestedSoupGroups<SoupPropertiesField>> for GroupedSoup<E> {
+ fn from(groups: NestedSoupGroups<SoupPropertiesField>) -> Self {
+ Self {
+ bins: groups
+ .into_bins()
+ .map(|(key, bin)| GraphqlSoupBin {
+ key,
+ total_count: bin.group_total_size(),
+ items: bin
+ .into_items()
+ .map(|item| GraphqlSoupItem::from(item.map_extra(|_| ())))
+ .collect(),
+ })
+ .collect(),
+ }
+ }
+}
+
+/// GraphQL representation of a Soup group bin.
+#[derive(SimpleObject)]
+pub struct GraphqlSoupBin<E: SoupEntityEdges> {
+ /// The grouping key.
+ key: String,
+ /// Total number of items in this group across all pages.
+ total_count: usize,
+ /// Items in this bin, ordered by their index within the group.
+ items: Vec<GraphqlSoupItem<E>>,
+}
+
/// GraphQL Soup item envelope.
pub struct GraphqlSoupItem<E: SoupEntityEdges> {
/// The unique identifier.
@@ -320,7 +335,8 @@ where
}
}
-/// GraphQL document subtype details.
+/// GraphQL representation of the soup document sub type.
+#[derive(SimpleObject)]
pub struct GraphqlSoupDocumentSubType {
/// The kind.
kind: &'static str,
@@ -343,20 +359,6 @@ impl From<&SoupDocumentSubType> for GraphqlSoupDocumentSubType {
}
}
-/// GraphQL representation of the soup document sub type.
-#[Object]
-impl GraphqlSoupDocumentSubType {
- /// The kind.
- async fn kind(&self) -> &str {
- self.kind
- }
-
- /// Whether the task is completed.
- async fn is_completed(&self) -> Option<bool> {
- self.is_completed
- }
-}
-
/// GraphQL chat entity.
pub struct GraphqlSoupChat<E: SoupEntityEdges>(SoupChat<()>, E);
@@ -474,7 +476,8 @@ where
}
}
-/// GraphQL email participant/contact.
+/// GraphQL representation of the soup email participant.
+#[derive(SimpleObject)]
pub struct GraphqlSoupEmailParticipant {
/// The unique identifier.
id: ID,
@@ -500,36 +503,8 @@ impl From<&SoupContact> for GraphqlSoupEmailParticipant {
}
}
-/// GraphQL representation of the soup email participant.
-#[Object]
-impl GraphqlSoupEmailParticipant {
- /// The unique identifier.
- async fn id(&self) -> &ID {
- &self.id
- }
-
- /// The identifier of the link.
- async fn link_id(&self) -> &ID {
- &self.link_id
- }
-
- /// The name.
- async fn name(&self) -> Option<&str> {
- self.name.as_deref()
- }
-
- /// The email.
- async fn email(&self) -> Option<&str> {
- self.email.as_deref()
- }
-
- /// The sfs photo url.
- async fn sfs_photo_url(&self) -> Option<&str> {
- self.sfs_photo_url.as_deref()
- }
-}
-
-/// GraphQL email label.
+/// GraphQL representation of the soup email label.
+#[derive(SimpleObject)]
pub struct GraphqlSoupEmailLabel {
/// The unique identifier.
id: ID,
@@ -546,6 +521,7 @@ pub struct GraphqlSoupEmailLabel {
/// The label list visibility.
label_list_visibility: &'static str,
/// The type.
+ #[graphql(name = "type")]
type_: &'static str,
}
@@ -574,51 +550,8 @@ impl From<&SoupLabel> for GraphqlSoupEmailLabel {
}
}
-/// GraphQL representation of the soup email label.
-#[Object]
-impl GraphqlSoupEmailLabel {
- /// The unique identifier.
- async fn id(&self) -> &ID {
- &self.id
- }
-
- /// The identifier of the link.
- async fn link_id(&self) -> &ID {
- &self.link_id
- }
-
- /// The identifier of the provider label.
- async fn provider_label_id(&self) -> &str {
- &self.provider_label_id
- }
-
- /// The name.
- async fn name(&self) -> &str {
- &self.name
- }
-
- /// The created timestamp in RFC 3339 format.
- async fn created_at(&self) -> &str {
- &self.created_at
- }
-
- /// The message list visibility.
- async fn message_list_visibility(&self) -> &'static str {
- self.message_list_visibility
- }
-
- /// The label list visibility.
- async fn label_list_visibility(&self) -> &'static str {
- self.label_list_visibility
- }
-
- /// The type.
- async fn type_(&self) -> &'static str {
- self.type_
- }
-}
-
-/// GraphQL email attachment.
+/// GraphQL representation of the soup email attachment.
+#[derive(SimpleObject)]
pub struct GraphqlSoupEmailAttachment {
/// The unique identifier.
id: ID,
@@ -653,50 +586,6 @@ impl From<&SoupAttachment> for GraphqlSoupEmailAttachment {
}
}
-/// GraphQL representation of the soup email attachment.
-#[Object]
-impl GraphqlSoupEmailAttachment {
- /// The unique identifier.
- async fn id(&self) -> &ID {
- &self.id
- }
-
- /// The identifier of the message.
- async fn message_id(&self) -> &ID {
- &self.message_id
- }
-
- /// The identifier of the provider attachment.
- async fn provider_attachment_id(&self) -> Option<&str> {
- self.provider_attachment_id.as_deref()
- }
-
- /// The filename.
- async fn filename(&self) -> Option<&str> {
- self.filename.as_deref()
- }
-
- /// The mime type.
- async fn mime_type(&self) -> Option<&str> {
- self.mime_type.as_deref()
- }
-
- /// The size bytes.
- async fn size_bytes(&self) -> Option<i64> {
- self.size_bytes
- }
-
- /// The identifier of the content.
- async fn content_id(&self) -> Option<&str> {
- self.content_id.as_deref()
- }
-
- /// The created timestamp in RFC 3339 format.
- async fn created_at(&self) -> &str {
- &self.created_at
- }
-}
-
/// GraphQL email thread entity.
pub struct GraphqlSoupEmailThread<E: SoupEntityEdges>(SoupEnrichedEmailThreadPreview<()>, E);
@@ -1120,7 +1009,8 @@ where
}
}
-/// GraphQL call participant.
+/// GraphQL representation of the soup call participant.
+#[derive(SimpleObject)]
pub struct GraphqlSoupCallParticipant {
/// The identifier of the user.
user_id: String,
@@ -1140,25 +1030,6 @@ impl From<&SoupCallRecordParticipant> for GraphqlSoupCallParticipant {
}
}
-/// GraphQL representation of the soup call participant.
-#[Object]
-impl GraphqlSoupCallParticipant {
- /// The identifier of the user.
- async fn user_id(&self) -> &str {
- &self.user_id
- }
-
- /// The joined timestamp in RFC 3339 format.
- async fn joined_at(&self) -> &str {
- &self.joined_at
- }
-
- /// The left timestamp in RFC 3339 format.
- async fn left_at(&self) -> Option<&str> {
- self.left_at.as_deref()
- }
-}
-
/// GraphQL call entity.
pub struct GraphqlSoupCall<E: SoupEntityEdges>(SoupCallRecord<()>, E);
diff --git a/crates/graphql_soup/src/resolvers.rs b/crates/graphql_soup/src/resolvers.rs
index 83fd3ad514..d9697e9149 100644
--- a/crates/graphql_soup/src/resolvers.rs
+++ b/crates/graphql_soup/src/resolvers.rs
@@ -14,13 +14,32 @@ use entity_access::{
use graphql_common::extract_part;
use model_user::axum_extractor::MacroUserExtractor;
use models_pagination::TypeEraseCursor;
-use soup::domain::ports::SoupService;
+use soup::domain::{models::grouping::NestedSoupGroups, ports::SoupService};
use crate::{
- inputs::SoupInput,
- objects::{SoupEntityEdges, SoupPage},
+ inputs::{GroupedSoupInput, SoupInput},
+ objects::{GroupedSoup, SoupEntityEdges, SoupPage},
};
+/// Resolve Soup items nested into grouping bins for the authenticated user.
+pub async fn resolve_grouped_soup<S, St, Edges>(
+ service: &S,
+ ctx: &Context<'_>,
+ input: GroupedSoupInput,
+) -> async_graphql::Result<GroupedSoup<Edges>>
+where
+ S: SoupService,
+ St: Clone + Send + Sync + 'static,
+ Edges: SoupEntityEdges,
+{
+ let Cached(MacroUserExtractor { macro_user_id, .. }) =
+ extract_part::<Cached<MacroUserExtractor>, St>(ctx).await?;
+ let request = input.into_request(macro_user_id)?;
+ let items = service.get_user_soup_grouped(request).await?;
+ let groups: NestedSoupGroups<_, _> = items.collect();
+ Ok(GroupedSoup::from(groups))
+}
+
/// Resolve a page of Soup items for the authenticated user: runs the lazy
/// axum extractors against the request context, converts the GraphQL input
/// into a Soup request, and executes it against the Soup service.
diff --git a/crates/soup/Cargo.toml b/crates/soup/Cargo.toml
index 47754af32c..4a91959908 100644
--- a/crates/soup/Cargo.toml
+++ b/crates/soup/Cargo.toml
@@ -50,8 +50,10 @@ async-trait = { workspace = true, optional = true }
axum = { workspace = true, optional = true }
axum-extra = { workspace = true, optional = true }
call = { path = "../call", default-features = false, features = ["ports"] }
+channels = { path = "../channels", default-features = false, features = [
+ "list",
+] }
chrono = { workspace = true }
-channels = { path = "../channels", default-features = false, features = ["list"] }
cowlike = { path = "../cowlike" }
crm = { path = "../crm", default-features = false }
document_sub_type = { path = "../document_sub_type" }
@@ -60,8 +62,11 @@ either = { workspace = true }
email = { path = "../email", features = ["ports"] }
entity_access = { path = "../entity_access", default-features = false }
filter_ast = { path = "../filter_ast" }
-foreign_entity = { path = "../foreign_entity", default-features = false, features = ["ports"] }
+foreign_entity = { path = "../foreign_entity", default-features = false, features = [
+ "ports",
+] }
frecency = { path = "../frecency" }
+indexmap = { workspace = true }
item_filters = { path = "../item_filters" }
macro_user_id = { path = "../macro_user_id" }
model-entity = { path = "../model-entity" }
diff --git a/crates/soup/src/domain/models.rs b/crates/soup/src/domain/models.rs
index cf3b6d9df9..db0c0b3051 100644
--- a/crates/soup/src/domain/models.rs
+++ b/crates/soup/src/domain/models.rs
@@ -1,9 +1,4 @@
-mod grouping;
-
-pub use grouping::{
- GroupMeta, GroupedResponse, build_grouped_response, entity_type_labels,
- resolve_group_label_and_order,
-};
+pub mod grouping;
use call::domain::models::GetCallRecordsRequest;
use channels::domain::models::{GetChannelsRequest, GetThreadReplyRowsRequest};
@@ -772,25 +767,6 @@ pub struct GroupedSoupRequest<T> {
pub grouping: Option<GroupingConfig>,
}
-/// A soup item with group metadata attached (returned from grouped queries).
-#[derive(Debug)]
-pub struct GroupedSoupItem<T = ()> {
- /// The soup item
- pub item: SoupItem<T>,
- /// The frecency score (if available)
- pub frecency_score: Option<AggregateFrecency>,
- /// Which group this item belongs to
- pub group_key: String,
- /// Total items in this group (computed via window function)
- pub group_total_count: u32,
- /// This item's position within the group (1-indexed)
- pub row_in_group: u32,
- /// Label for this group (from property_options or computed)
- pub group_label: Option<String>,
- /// Display order for this group
- pub group_display_order: Option<i32>,
-}
-
/// Errors returned while building or executing soup queries.
#[derive(Debug, Error)]
pub enum SoupErr {
diff --git a/crates/soup/src/domain/models/grouping.rs b/crates/soup/src/domain/models/grouping.rs
index ebd9963363..5aaa5d1206 100644
--- a/crates/soup/src/domain/models/grouping.rs
+++ b/crates/soup/src/domain/models/grouping.rs
@@ -1,6 +1,10 @@
//! Grouping utilities for soup queries.
+#[cfg(test)]
+mod test;
+
use super::{EnrichedSoupItem, SoupPropertiesField};
+use indexmap::IndexMap;
use item_filters::ast::EntityFilterAst;
use models_grouping::{GroupByField, date_bucket_label, date_bucket_order};
use models_pagination::{
@@ -12,7 +16,7 @@ use std::collections::HashMap;
use uuid::Uuid;
/// Resolve label and display order for a group key based on the grouping field.
-pub fn resolve_group_label_and_order(key: &str, group_by: &GroupByField) -> (String, Option<i32>) {
+fn resolve_group_label_and_order(key: &str, group_by: &GroupByField) -> (String, Option<i32>) {
match group_by {
GroupByField::Date => (
date_bucket_label(key).to_string(),
@@ -61,7 +65,7 @@ pub struct GroupedResponse {
/// Build a grouped response from grouped soup items.
pub fn build_grouped_response(
- items: Vec<super::GroupedSoupItem<SoupPropertiesField>>,
+ items: impl IntoIterator<Item = ItemGroupingInfo<SoupPropertiesField>>,
group_by: &GroupByField,
sort_method: SimpleSortMethod,
requested_group_key: Option<String>,
@@ -77,22 +81,20 @@ pub fn build_grouped_response(
}
let mut group_stats: HashMap<String, GroupData> = HashMap::new();
- let mut items_pool: HashMap<Uuid, EnrichedSoupItem> = HashMap::with_capacity(items.len());
+ let items = items.into_iter();
+ let mut items_pool: HashMap<Uuid, EnrichedSoupItem> =
+ HashMap::with_capacity(items.size_hint().0);
let mut get_cursor_val = SoupItem::sort_on(sort_method);
- for grouped_item in items.into_iter() {
- let key = grouped_item.group_key.clone();
+ for grouped_item in items {
+ let key = grouped_item.key.clone();
let item_id = grouped_item.item.id();
let cursor_val = get_cursor_val(&grouped_item.item);
let entry = group_stats.entry(key.clone()).or_insert_with(|| {
- let (label, display_order) =
- match (&grouped_item.group_label, grouped_item.group_display_order) {
- (Some(l), d) => (l.clone(), d),
- (None, _) => resolve_group_label_and_order(&key, group_by),
- };
+ let (label, display_order) = resolve_group_label_and_order(&key, group_by);
GroupData {
- total_count: grouped_item.group_total_count,
+ total_count: grouped_item.total_group_count.min(u32::MAX as usize) as u32,
item_ids: Vec::new(),
last_item_id: None,
last_cursor_val: None,
@@ -110,7 +112,7 @@ pub fn build_grouped_response(
.entry(item_id)
.or_insert_with(|| EnrichedSoupItem {
item: grouped_item.item,
- frecency_score: grouped_item.frecency_score,
+ frecency_score: None,
});
}
@@ -196,3 +198,68 @@ pub mod entity_type_labels {
}
}
}
+
+/// Items and metadata belonging to one nested Soup group.
+pub struct BinData<T = ()> {
+ data: Vec<SoupItem<T>>,
+ group_total_size: usize,
+}
+
+impl<T> BinData<T> {
+ /// The total number of items in this group across all pages.
+ pub fn group_total_size(&self) -> usize {
+ self.group_total_size
+ }
+
+ /// Consume the bin and return its items in their group order.
+ pub fn into_items(self) -> impl Iterator<Item = SoupItem<T>> {
+ self.data.into_iter()
+ }
+}
+
+/// Soup items nested into bins keyed by their grouping value.
+pub struct NestedSoupGroups<T = (), Bin = String>(IndexMap<Bin, BinData<T>>);
+
+impl<Bin, T> NestedSoupGroups<T, Bin> {
+ /// Consume the nested groups and iterate over their bins.
+ pub fn into_bins(self) -> impl Iterator<Item = (Bin, BinData<T>)> {
+ self.0.into_iter()
+ }
+}
+
+/// A Soup item paired with the metadata needed to place it into a group.
+#[derive(Debug)]
+pub struct ItemGroupingInfo<T = (), Bin = String> {
+ /// The key of the bin containing the item.
+ pub key: Bin,
+ /// The total number of items in the bin across all pages.
+ pub total_group_count: usize,
+ /// The item's position within its bin.
+ pub index_in_group: usize,
+ /// The Soup item.
+ pub item: SoupItem<T>,
+}
+
+impl<Bin, T> FromIterator<ItemGroupingInfo<T, Bin>> for NestedSoupGroups<T, Bin>
+where
+ Bin: Eq + std::hash::Hash,
+{
+ fn from_iter<I: IntoIterator<Item = ItemGroupingInfo<T, Bin>>>(iter: I) -> Self {
+ let mut out = IndexMap::new();
+
+ for item in iter {
+ let ItemGroupingInfo {
+ key,
+ total_group_count,
+ item,
+ ..
+ } = item;
+ let entry = out.entry(key).or_insert_with(|| BinData {
+ data: Vec::new(),
+ group_total_size: total_group_count,
+ });
+ entry.data.push(item);
+ }
+ NestedSoupGroups(out)
+ }
+}
diff --git a/crates/soup/src/domain/models/grouping/test.rs b/crates/soup/src/domain/models/grouping/test.rs
new file mode 100644
index 0000000000..276106fb1b
--- /dev/null
+++ b/crates/soup/src/domain/models/grouping/test.rs
@@ -0,0 +1,98 @@
+use chrono::DateTime;
+use item_filters::ast::EntityFilterAst;
+use macro_user_id::user_id::MacroUserIdStr;
+use models_grouping::GroupByField;
+use models_pagination::SimpleSortMethod;
+use models_soup::{item::SoupItem, project::SoupProject};
+use uuid::Uuid;
+
+use super::*;
+
+fn project(id: u128) -> SoupItem {
+ SoupItem::Project(SoupProject {
+ id: Uuid::from_u128(id),
+ name: format!("Project {id}"),
+ owner_id: MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap(),
+ parent_id: None,
+ created_at: DateTime::default(),
+ updated_at: DateTime::default(),
+ viewed_at: None,
+ deleted_at: None,
+ extra: (),
+ })
+}
+
+#[test]
+fn nested_groups_preserve_bin_and_item_insertion_order() {
+ let groups: NestedSoupGroups = vec![
+ ItemGroupingInfo {
+ key: "project".to_string(),
+ total_group_count: 2,
+ index_in_group: 2,
+ item: project(2),
+ },
+ ItemGroupingInfo {
+ key: "project".to_string(),
+ total_group_count: 2,
+ index_in_group: 1,
+ item: project(1),
+ },
+ ItemGroupingInfo {
+ key: "document".to_string(),
+ total_group_count: 1,
+ index_in_group: 1,
+ item: project(3),
+ },
+ ]
+ .into_iter()
+ .collect();
+
+ let mut bins = groups.into_bins();
+ let (project_key, project_bin) = bins.next().unwrap();
+ assert_eq!(project_key, "project");
+ assert_eq!(project_bin.group_total_size(), 2);
+ assert_eq!(
+ project_bin
+ .into_items()
+ .map(|item| item.id())
+ .collect::<Vec<_>>(),
+ vec![Uuid::from_u128(2), Uuid::from_u128(1)]
+ );
+
+ let (document_key, document_bin) = bins.next().unwrap();
+ assert_eq!(document_key, "document");
+ assert_eq!(
+ document_bin
+ .into_items()
+ .map(|item| item.id())
+ .collect::<Vec<_>>(),
+ vec![Uuid::from_u128(3)]
+ );
+ assert!(bins.next().is_none());
+}
+
+#[test]
+fn rest_grouped_response_remains_normalized() {
+ let id = Uuid::from_u128(1);
+ let response = build_grouped_response(
+ vec![ItemGroupingInfo {
+ key: "project".to_string(),
+ total_group_count: 1,
+ index_in_group: 1,
+ item: project(1).map_extra(|()| SoupPropertiesField::default()),
+ }],
+ &GroupByField::EntityType,
+ SimpleSortMethod::ViewedUpdated,
+ None,
+ EntityFilterAst::default(),
+ );
+
+ assert_eq!(response.items.len(), 1);
+ assert!(response.items.contains_key(&id));
+ assert_eq!(response.groups.len(), 1);
+ assert_eq!(response.groups[0].key, "project");
+ assert_eq!(response.groups[0].label, "Projects");
+ assert_eq!(response.groups[0].total_count, 1);
+ assert_eq!(response.groups[0].item_ids, vec![id]);
+ assert!(response.groups[0].next_cursor.is_none());
+}
diff --git a/crates/soup/src/domain/ports.rs b/crates/soup/src/domain/ports.rs
index a3230762d6..6ab762a613 100644
--- a/crates/soup/src/domain/ports.rs
+++ b/crates/soup/src/domain/ports.rs
@@ -1,8 +1,8 @@
use std::sync::Arc;
use crate::domain::models::{
- AdvancedSortParams, EnrichedSoupItem, GroupedSortRequest, GroupedSoupItem, IntoSoupReqAst,
- SimpleSortRequest, SoupErr, SoupPropertiesField, SoupRequest,
+ AdvancedSortParams, EnrichedSoupItem, GroupedSortRequest, IntoSoupReqAst, SimpleSortRequest,
+ SoupErr, SoupPropertiesField, SoupRequest, grouping::ItemGroupingInfo,
};
use either::Either;
use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};
@@ -13,10 +13,18 @@ use models_soup::item::SoupItem;
use serde::Serialize;
/// Repository abstraction for loading soup items from storage.
-#[cfg_attr(test, mockall::automock(type Err = anyhow::Error;))]
+#[cfg_attr(
+ test,
+ mockall::automock(
+ type Err = anyhow::Error;
+ type GroupedItems = std::vec::IntoIter<ItemGroupingInfo>;
+ )
+)]
pub trait SoupRepo: Send + Sync + 'static {
/// Error returned by repository operations.
type Err;
+ /// Iterator returned by grouped Soup queries.
+ type GroupedItems: Iterator<Item = ItemGroupingInfo> + Send;
/// Fetch expanded soup items for a simple sorted cursor query.
fn expanded_generic_cursor_soup<'a>(
&self,
@@ -61,7 +69,7 @@ pub trait SoupRepo: Send + Sync + 'static {
fn expanded_grouped_cursor_soup<'a>(
&self,
req: GroupedSortRequest<'a>,
- ) -> impl Future<Output = Result<Vec<GroupedSoupItem<()>>, Self::Err>> + Send;
+ ) -> impl Future<Output = Result<Self::GroupedItems, Self::Err>> + Send;
}
/// type alias which represents the posible outputs of soup
@@ -129,7 +137,12 @@ pub trait SoupService: Send + Sync + 'static {
fn get_user_soup_grouped(
&self,
req: GroupedSortRequest<'_>,
- ) -> impl Future<Output = Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr>> + Send;
+ ) -> impl Future<
+ Output = Result<
+ impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>> + Send,
+ SoupErr,
+ >,
+ > + Send;
/// Fetch the tag definitions visible to a user — their own plus their
/// teams' — with options attached.
@@ -200,7 +213,7 @@ where
async fn get_user_soup_grouped(
&self,
req: GroupedSortRequest<'_>,
- ) -> Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr> {
+ ) -> Result<impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>> + Send, SoupErr> {
(**self).get_user_soup_grouped(req).await
}
@@ -274,8 +287,8 @@ impl SoupService for NoOpSoupService {
async fn get_user_soup_grouped(
&self,
_req: GroupedSortRequest<'_>,
- ) -> Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr> {
- Err(no_op_soup_err())
+ ) -> Result<impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>> + Send, SoupErr> {
+ Err::<std::vec::IntoIter<ItemGroupingInfo<SoupPropertiesField>>, _>(no_op_soup_err())
}
async fn caller_tag_sets<'a>(
diff --git a/crates/soup/src/domain/service.rs b/crates/soup/src/domain/service.rs
index 9b398c3ebc..4b228ed3a4 100644
--- a/crates/soup/src/domain/service.rs
+++ b/crates/soup/src/domain/service.rs
@@ -1,8 +1,8 @@
use crate::domain::{
models::{
AdvancedSortParams, EnrichedSoupItem, FrecencyQueryInner, GetCrmCompaniesRequest,
- GroupedSortRequest, GroupedSoupItem, IntoSoupReqAst, SimpleQueryInner, SimpleSortQuery,
- SimpleSortRequest, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest, SoupType,
+ GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner, SimpleSortQuery, SimpleSortRequest,
+ SoupErr, SoupPropertiesField, SoupQuery, SoupRequest, SoupType, grouping::ItemGroupingInfo,
},
ports::{SoupOutput, SoupRepo, SoupService},
};
@@ -180,7 +180,7 @@ where
async fn handle_grouped_soup_request(
&self,
req: GroupedSortRequest<'_>,
- ) -> Result<Vec<GroupedSoupItem<()>>, SoupErr> {
+ ) -> Result<T::GroupedItems, SoupErr> {
self.soup_storage
.expanded_grouped_cursor_soup(req)
.await
@@ -698,28 +698,21 @@ where
async fn populate_grouped_items(
&self,
user_id: MacroUserIdStr<'_>,
- items: Vec<GroupedSoupItem<()>>,
- ) -> Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr> {
- if items.is_empty() {
- return Ok(Vec::new());
- }
-
+ items: impl Iterator<Item = ItemGroupingInfo> + Send,
+ ) -> Result<impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>>, SoupErr> {
let (items, metadata): (Vec<_>, Vec<_>) = items
- .into_iter()
.map(|item| {
(
item.item,
- (
- item.frecency_score,
- item.group_key,
- item.group_total_count,
- item.row_in_group,
- item.group_label,
- item.group_display_order,
- ),
+ (item.key, item.total_group_count, item.index_in_group),
)
})
.unzip();
+
+ if items.is_empty() {
+ return Ok(Vec::new().into_iter());
+ }
+
let items = self
.soup_storage
.populate_properties(user_id, items)
@@ -735,27 +728,15 @@ where
.into_iter()
.zip(metadata)
.map(
- |(
+ |(item, (key, total_group_count, index_in_group))| ItemGroupingInfo {
item,
- (
- frecency_score,
- group_key,
- group_total_count,
- row_in_group,
- group_label,
- group_display_order,
- ),
- )| GroupedSoupItem {
- item,
- frecency_score,
- group_key,
- group_total_count,
- row_in_group,
- group_label,
- group_display_order,
+ key,
+ total_group_count,
+ index_in_group,
},
)
- .collect())
+ .collect::<Vec<_>>()
+ .into_iter())
}
async fn get_user_soup_internal<R>(
@@ -939,7 +920,7 @@ where
async fn get_user_soup_grouped(
&self,
req: GroupedSortRequest<'_>,
- ) -> Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr> {
+ ) -> Result<impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>> + Send, SoupErr> {
let user_id = req.user_id.clone();
let items = self.handle_grouped_soup_request(req).await?;
self.populate_grouped_items(user_id, items).await
diff --git a/crates/soup/src/domain/service/tests.rs b/crates/soup/src/domain/service/tests.rs
index d79634dc0d..9abda60f64 100644
--- a/crates/soup/src/domain/service/tests.rs
+++ b/crates/soup/src/domain/service/tests.rs
@@ -1229,15 +1229,13 @@ async fn grouped_properties_are_populated_by_the_service() {
.times(1)
.returning(|_| {
Box::pin(async move {
- Ok(vec![GroupedSoupItem {
+ Ok(vec![ItemGroupingInfo {
item: SoupItem::Document(soup_document("grouped-document")),
- frecency_score: None,
- group_key: "document".to_string(),
- group_total_count: 1,
- row_in_group: 1,
- group_label: Some("Documents".to_string()),
- group_display_order: Some(1),
- }])
+ key: "document".to_string(),
+ total_group_count: 1,
+ index_in_group: 1,
+ }]
+ .into_iter())
})
});
soup_mock
@@ -1277,11 +1275,13 @@ async fn grouped_properties_are_populated_by_the_service() {
},
})
.await
- .unwrap();
+ .unwrap()
+ .collect::<Vec<_>>();
assert_eq!(items.len(), 1);
- assert_eq!(items[0].group_key, "document");
- assert_eq!(items[0].group_label.as_deref(), Some("Documents"));
+ assert_eq!(items[0].key, "document");
+ assert_eq!(items[0].total_group_count, 1);
+ assert_eq!(items[0].index_in_group, 1);
assert!(match &items[0].item {
SoupItem::Document(document) => document.extra.properties.is_empty(),
_ => false,
diff --git a/crates/soup/src/inbound/axum_router.rs b/crates/soup/src/inbound/axum_router.rs
index be7562bfc9..a55297d774 100644
--- a/crates/soup/src/inbound/axum_router.rs
+++ b/crates/soup/src/inbound/axum_router.rs
@@ -1,8 +1,8 @@
use crate::domain::{
models::{
- EnrichedSoupItem, FrecencyQueryInner, GroupMeta, GroupedSortRequest, IntoSoupReqAst,
- SimpleQueryInner, SoupErr, SoupItemWithProperties, SoupQuery, SoupRequest, SoupType,
- build_grouped_response,
+ EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst, SimpleQueryInner,
+ SoupErr, SoupItemWithProperties, SoupQuery, SoupRequest, SoupType,
+ grouping::{GroupMeta, build_grouped_response},
},
ports::SoupService,
};
diff --git a/crates/soup/src/inbound/axum_router/tests.rs b/crates/soup/src/inbound/axum_router/tests.rs
index 40f2b395fb..159e552656 100644
--- a/crates/soup/src/inbound/axum_router/tests.rs
+++ b/crates/soup/src/inbound/axum_router/tests.rs
@@ -27,9 +27,9 @@ use item_filters::ast::EntityFilterAst;
use crate::{
domain::{
models::{
- EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, GroupedSoupItem,
- IntoSoupReqAst, SimpleQueryInner, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest,
- SoupType,
+ EnrichedSoupItem, FrecencyQueryInner, GroupedSortRequest, IntoSoupReqAst,
+ SimpleQueryInner, SoupErr, SoupPropertiesField, SoupQuery, SoupRequest, SoupType,
+ grouping::ItemGroupingInfo,
},
ports::{SoupOutput, SoupService},
},
@@ -162,8 +162,10 @@ impl SoupService for MockSoup {
async fn get_user_soup_grouped(
&self,
_req: GroupedSortRequest<'_>,
- ) -> Result<Vec<GroupedSoupItem<SoupPropertiesField>>, SoupErr> {
- Err(SoupErr::SoupDbErr(anyhow::anyhow!("Not implemented")))
+ ) -> Result<impl Iterator<Item = ItemGroupingInfo<SoupPropertiesField>> + Send, SoupErr> {
+ Err::<std::vec::IntoIter<ItemGroupingInfo<SoupPropertiesField>>, _>(SoupErr::SoupDbErr(
+ anyhow::anyhow!("Not implemented"),
+ ))
}
async fn caller_tag_sets<'a>(
diff --git a/crates/soup/src/outbound/pg_soup_repo.rs b/crates/soup/src/outbound/pg_soup_repo.rs
index 37e284d0f0..a8a7b86298 100644
--- a/crates/soup/src/outbound/pg_soup_repo.rs
+++ b/crates/soup/src/outbound/pg_soup_repo.rs
@@ -1,8 +1,8 @@
use crate::{
domain::{
models::{
- AdvancedSortParams, GroupedSortRequest, GroupedSoupItem, SimpleSortQuery,
- SimpleSortRequest, SoupPropertiesField,
+ AdvancedSortParams, GroupedSortRequest, SimpleSortQuery, SimpleSortRequest,
+ SoupPropertiesField, grouping::ItemGroupingInfo,
},
ports::SoupRepo,
},
@@ -35,6 +35,7 @@ impl PgSoupRepo {
impl SoupRepo for PgSoupRepo {
type Err = sqlx::Error;
+ type GroupedItems = std::vec::IntoIter<ItemGroupingInfo>;
fn expanded_generic_cursor_soup<'a>(
&self,
@@ -147,10 +148,10 @@ impl SoupRepo for PgSoupRepo {
.map_err(|e| sqlx::Error::Decode(e.into()))
}
- fn expanded_grouped_cursor_soup<'a>(
+ async fn expanded_grouped_cursor_soup<'a>(
&self,
req: GroupedSortRequest<'a>,
- ) -> impl Future<Output = Result<Vec<GroupedSoupItem<()>>, Self::Err>> + Send {
+ ) -> Result<Self::GroupedItems, Self::Err> {
expanded::dynamic::expanded_dynamic_cursor_soup_grouped(
&self.pool.0,
GroupedDynamicCursorArgs {
@@ -161,6 +162,7 @@ impl SoupRepo for PgSoupRepo {
grouping: req.grouping,
},
)
+ .await
}
}
diff --git a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
index 13ae9ba58e..5ceb7e795b 100644
--- a/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/expanded/dynamic.rs
@@ -27,7 +27,7 @@ use sqlx::{PgPool, Postgres, QueryBuilder, Row, postgres::PgRow, prelude::FromRo
use system_properties::{StatusOption, SystemPropertyKey};
use uuid::Uuid;
-use crate::domain::models::GroupedSoupItem;
+use crate::domain::models::grouping::ItemGroupingInfo;
use crate::outbound::pg_soup_repo::grouping::{
GroupJoinClause, group_join_clause, group_select_expr,
};
@@ -1435,6 +1435,7 @@ impl<'a> FromRow<'a, PgRow> for SoupRow {
}
impl SoupRow {
+ #[tracing::instrument(err)]
fn into_soup_item(self) -> Result<SoupItem<()>, sqlx::Error> {
Ok(match self {
SoupRow::Document(DocumentRow {
@@ -1618,37 +1619,22 @@ pub struct GroupedDynamicCursorArgs<'a> {
}
/// Group metadata fields extracted from grouped query rows.
+#[derive(FromRow)]
struct GroupFields {
group_key: String,
group_total_count: i64,
row_in_group: i64,
}
-impl GroupFields {
- fn from_row(row: &PgRow) -> Result<Self, sqlx::Error> {
- Ok(GroupFields {
- group_key: row.try_get("group_key")?,
- group_total_count: row.try_get("group_total_count")?,
- row_in_group: row.try_get("row_in_group")?,
- })
- }
-}
-
/// Grouped row: reuses SoupRow for item data, adds group metadata.
+#[derive(FromRow)]
struct GroupedSoupRow {
+ #[sqlx(flatten)]
item: SoupRow,
+ #[sqlx(flatten)]
group: GroupFields,
}
-impl<'a> FromRow<'a, PgRow> for GroupedSoupRow {
- fn from_row(row: &'a PgRow) -> Result<Self, sqlx::Error> {
- Ok(GroupedSoupRow {
- item: SoupRow::from_row(row)?,
- group: GroupFields::from_row(row)?,
- })
- }
-}
-
/// Per-group limit for initial grouped queries.
/// Each group will return at most this many items initially.
const PER_GROUP_LIMIT: i32 = 10;
@@ -1882,7 +1868,7 @@ fn build_grouped_query<'a>(
pub async fn expanded_dynamic_cursor_soup_grouped(
db: &PgPool,
args: GroupedDynamicCursorArgs<'_>,
-) -> Result<Vec<GroupedSoupItem<()>>, sqlx::Error> {
+) -> Result<std::vec::IntoIter<ItemGroupingInfo>, sqlx::Error> {
let GroupedDynamicCursorArgs {
user_id,
limit,
@@ -1904,7 +1890,7 @@ pub async fn expanded_dynamic_cursor_soup_grouped(
// $9 is bound unconditionally (NULL when not in single-group mode) so $10 stays aligned.
let mut query = query_builder
- .build()
+ .build_query_as::<'_, GroupedSoupRow>()
.bind(user_id.as_ref())
.bind(sort_method_str)
.bind(query_limit)
@@ -1925,34 +1911,22 @@ pub async fn expanded_dynamic_cursor_soup_grouped(
// filter-shaped SQL text gets no reuse from the statement cache but
// does get generic-plan flips.
.persistent(false)
- .try_map(|row| GroupedSoupRow::from_row(&row))
.fetch_all(db)
.await?;
- // Convert rows to (SoupItem, GroupFields) pairs and unzip
- let (soup_items, groups): (Vec<SoupItem<()>>, Vec<GroupFields>) = rows
+ let items = rows
.into_iter()
.map(|row| {
- let item = row.item.into_soup_item()?;
- Ok((item, row.group))
+ Ok(ItemGroupingInfo {
+ item: row.item.into_soup_item()?,
+ key: row.group.group_key,
+ total_group_count: usize::try_from(row.group.group_total_count)
+ .map_err(|error| sqlx::Error::Decode(Box::new(error)))?,
+ index_in_group: usize::try_from(row.group.row_in_group)
+ .map_err(|error| sqlx::Error::Decode(Box::new(error)))?,
+ })
})
- .collect::<Result<Vec<_>, sqlx::Error>>()?
- .into_iter()
- .unzip();
+ .collect::<Result<Vec<_>, sqlx::Error>>()?;
- let items = soup_items
- .into_iter()
- .zip(groups)
- .map(|(item, group)| GroupedSoupItem {
- item,
- frecency_score: None,
- group_key: group.group_key,
- group_total_count: group.group_total_count as u32,
- row_in_group: group.row_in_group as u32,
- group_label: None,
- group_display_order: None,
- })
- .collect();
-
- Ok(items)
+ Ok(items.into_iter())
}
diff --git a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
index 4098fe779f..362b76dd38 100644
--- a/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
+++ b/crates/soup/src/outbound/pg_soup_repo/grouping/test.rs
@@ -83,23 +83,24 @@ async fn test_grouped_by_entity_type(pool: Pool<Postgres>) -> anyhow::Result<()>
grouping,
},
)
- .await?;
+ .await?
+ .collect::<Vec<_>>();
assert!(!items.is_empty(), "Should return some items");
// Check that items have group keys
for item in &items {
assert!(
- ["document", "chat", "project"].contains(&item.group_key.as_str()),
+ ["document", "chat", "project"].contains(&item.key.as_str()),
"Group key should be a valid entity type, got: {}",
- item.group_key
+ item.key
);
}
// Check that group_total_count is populated
for item in &items {
assert!(
- item.group_total_count > 0,
+ item.total_group_count > 0,
"group_total_count should be > 0"
);
}
@@ -135,15 +136,15 @@ async fn test_grouped_by_project(pool: Pool<Postgres>) -> anyhow::Result<()> {
grouping,
},
)
- .await?;
+ .await?
+ .collect::<Vec<_>>();
assert!(!items.is_empty(), "Should return some items");
// Group keys should be UUIDs or empty string (for unassigned)
for item in &items {
- if !item.group_key.is_empty() {
- uuid::Uuid::parse_str(&item.group_key)
- .expect("Non-empty group_key should be a valid UUID");
+ if !item.key.is_empty() {
+ uuid::Uuid::parse_str(&item.key).expect("Non-empty group key should be a valid UUID");
}
}
@@ -178,10 +179,11 @@ async fn test_grouped_single_group_filter(pool: Pool<Postgres>) -> anyhow::Resul
},
},
)
- .await?;
+ .await?
+ .collect::<Vec<_>>();
// Find a group key that has items
- let target_group_key = all_items.first().map(|i| i.group_key.clone());
+ let target_group_key = all_items.first().map(|i| i.key.clone());
let Some(group_key) = target_group_key else {
return Ok(()); // No items to test with
};
@@ -204,12 +206,13 @@ async fn test_grouped_single_group_filter(pool: Pool<Postgres>) -> anyhow::Resul
},
},
)
- .await?;
+ .await?
+ .collect::<Vec<_>>();
// All returned items should have the same group key
for item in &filtered_items {
assert_eq!(
- item.group_key, group_key,
+ item.key, group_key,
"All items should belong to the filtered group"
);
}
diff --git a/static_assets/schema.graphql b/static_assets/schema.graphql
index 5cecd4bd21..62050f8500 100644
--- a/static_assets/schema.graphql
+++ b/static_assets/schema.graphql
@@ -862,6 +862,46 @@ input GraphqlForeignEntityLiteral @oneOf {
notificationSeen: Boolean
}
+"""
+Grouping modes supported by grouped Soup.
+"""
+enum GraphqlGroupByField {
+ """
+ Group into date buckets.
+ """
+ DATE
+ """
+ Group by Soup entity type.
+ """
+ ENTITY_TYPE
+ """
+ Group by containing project.
+ """
+ PROJECT
+ """
+ Group by a property value.
+ """
+ PROPERTY
+}
+
+"""
+GraphQL representation of a field used to group Soup items.
+"""
+input GraphqlGroupByInput {
+ """
+ The kind of grouping to perform.
+ """
+ field: GraphqlGroupByField!
+ """
+ Property definition to group by when `field` is `PROPERTY`.
+ """
+ propertyDefinitionId: ID
+ """
+ Optional property entity type restriction.
+ """
+ entityType: GraphqlPropertyEntityType
+}
+
"""
A link property value.
"""
@@ -1269,6 +1309,24 @@ enum GraphqlSimpleSortMethod {
VIEWED_UPDATED
}
+"""
+GraphQL representation of a Soup group bin.
+"""
+type GraphqlSoupBin {
+ """
+ The grouping key.
+ """
+ key: String!
+ """
+ Total number of items in this group across all pages.
+ """
+ totalCount: Int!
+ """
+ Items in this bin, ordered by their index within the group.
+ """
+ items: [GraphqlSoupItem!]!
+}
+
"""
GraphQL representation of the soup call.
"""
@@ -2313,11 +2371,47 @@ type GraphqlUser {
"""
id: ID!
"""
+ Fetch Soup items nested into grouping bins.
+ """
+ groupSoup(input: GroupedSoupInput!): GroupedSoup!
+ """
Fetch a page of Soup items using the existing Soup filter AST format.
"""
soup(input: SoupInput!): SoupPage!
}
+"""
+GraphQL representation of grouped Soup items.
+"""
+type GroupedSoup {
+ """
+ Bins containing the grouped Soup items.
+ """
+ bins: [GraphqlSoupBin!]!
+}
+
+"""
+Input for `Query.groupSoup`.
+"""
+input GroupedSoupInput {
+ """
+ The field used to divide Soup items into bins.
+ """
+ groupBy: GraphqlGroupByInput!
+ """
+ Maximum number of items to return per bin. Defaults to 20, max 500.
+ """
+ limit: Int
+ """
+ Sort order within each bin. Defaults to `VIEWED_UPDATED`.
+ """
+ sortMethod: GraphqlSimpleSortMethod
+ """
+ AST-shaped filters applied to each Soup entity type.
+ """
+ filters: GraphqlEntityFilterAst
+}
+
"""
A scalar that can represent any JSON value.
"""