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
fix(email): open CRM-shared threads from the GraphQL thread lookup (#8179)
gbirman12795d719b0c
diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rsindex 662bf18be3..5fe18aedd1 100644--- a/crates/complete_graph/src/schema/test.rs+++ b/crates/complete_graph/src/schema/test.rs@@ -346,6 +346,29 @@ struct CountingEmailService {seen_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid)>>>,unread_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid)>>>,label_mutation_calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Uuid, Uuid, bool)>>>,+ thread_preview_receipts: Arc<Mutex<Vec<Vec<Uuid>>>>,+}++impl email::domain::ports::EmailThreadPreviewService for CountingEmailService {+ async fn get_email_thread_previews_by_receipts(+ &self,+ user_id: MacroUserIdStr<'static>,+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> Result<Vec<EnrichedEmailThreadPreview>, EmailErr> {+ assert_eq!(user_id.as_ref(), VALID_USER_ID);+ let mut thread_ids = receipts+ .iter()+ .map(|receipt| Uuid::parse_str(&receipt.entity().entity_id).unwrap())+ .collect::<Vec<_>>();+ thread_ids.sort_unstable();+ self.thread_preview_receipts+ .lock()+ .unwrap()+ .push(thread_ids);+ // Only the email crate constructs previews; this fake observes which+ // receipts reached the read.+ Ok(Vec::new())+ }}impl graphql_soup::EmailMutationThreadReader for CountingEmailService {@@ -933,10 +956,13 @@ fn full_message(thread_id: Uuid) -> Message {}}-/// Entity access service whose team lookups are counted and return a team.+/// Entity access service whose team lookups are counted and return a team,+/// and which grants View on the configured email threads only.#[derive(Clone, Default)]struct CountingEntityAccessService {team_calls: Arc<AtomicUsize>,+ viewable_email_threads: Arc<Mutex<std::collections::HashSet<Uuid>>>,+ email_thread_checks: Arc<Mutex<Vec<Uuid>>>,}impl EntityAccessService for CountingEntityAccessService {@@ -955,6 +981,15 @@ impl EntityAccessService for CountingEntityAccessService {| (EntityType::Form, form_activity::RESPONDED_FORM_ID) => Some(AccessLevel::View),(EntityType::Form, form_activity::EDITABLE_FORM_ID) => Some(AccessLevel::Edit),(EntityType::Database | EntityType::Form, _) => None,+ (EntityType::EmailThread, _) => {+ let thread_id = Uuid::parse_str(entity_id).unwrap();+ self.email_thread_checks.lock().unwrap().push(thread_id);+ self.viewable_email_threads+ .lock()+ .unwrap()+ .contains(&thread_id)+ .then_some(AccessLevel::View)+ }_ => return Err(AccessError::internal("test access failure")),};if let Some(access_level) = grant {@@ -967,7 +1002,10 @@ impl EntityAccessService for CountingEntityAccessService {EntityPermission::AccessLevel { access_level },);}- if matches!(entity_type, EntityType::Database | EntityType::Form) {+ if matches!(+ entity_type,+ EntityType::Database | EntityType::Form | EntityType::EmailThread+ ) {return Err(AccessError::Unauthorized);}Err(AccessError::internal("test access failure"))@@ -1129,6 +1167,42 @@ impl FromRef<TestState> for Arc<CountingEntityAccessService> {}}+/// Soup email thread reader standing in for the replica-backed email reader.+#[derive(Clone, Default)]+struct RecordingThreadReader {+ reads: Arc<AtomicUsize>,+ threads: Arc<Mutex<Vec<SoupItem<()>>>>,+}++impl RecordingThreadReader {+ fn set_threads(&self, threads: Vec<SoupItem<()>>) {+ *self.threads.lock().unwrap() = threads;+ }+}++impl graphql_soup::SoupEmailThreadReader for RecordingThreadReader {+ async fn read_threads(+ &self,+ user_id: MacroUserIdStr<'static>,+ thread_ids: Vec<Uuid>,+ ) -> Result<Vec<SoupEnrichedEmailThreadPreview<()>>, graphql_soup::SoupItemLoaderError> {+ assert_eq!(user_id.as_ref(), VALID_USER_ID);+ self.reads.fetch_add(1, Ordering::SeqCst);+ Ok(self+ .threads+ .lock()+ .unwrap()+ .iter()+ .filter_map(|item| match item {+ SoupItem::EmailThread(thread) if thread_ids.contains(&thread.thread.id) => {+ Some(thread.clone())+ }+ _ => None,+ })+ .collect())+ }+}+struct TestHarness {schema: SoupSchema<CountingSoupService,@@ -1157,9 +1231,11 @@ struct TestHarness {primary_soup_service: CountingSoupService,email_service: CountingEmailService,email_content_reader: RecordingEmailContentReader,+ thread_reader: RecordingThreadReader,activity_reader: RecordingActivityReader,authorization_calls: Arc<AtomicUsize>,inbox_calls: Arc<AtomicUsize>,+ thread_reads: Arc<AtomicUsize>,user_label_calls: Arc<AtomicUsize>,user_link_calls: Arc<AtomicUsize>,user_catalog_identities: Arc<Mutex<Vec<MacroUserIdStr<'static>>>>,@@ -1177,9 +1253,11 @@ fn harness() -> TestHarness {let authorization = FakeAuthorizationService::default();let soup = CountingSoupService::default();let email_content_reader = RecordingEmailContentReader::default();+ let thread_reader = RecordingThreadReader::default();let activity_reader = RecordingActivityReader::default();let authorization_calls = Arc::clone(&authorization.authorization_calls);let inbox_calls = Arc::clone(&email.inbox_calls);+ let thread_reads = Arc::clone(&thread_reader.reads);let user_label_calls = Arc::clone(&email.user_label_calls);let user_link_calls = Arc::clone(&email.user_link_calls);let user_catalog_identities = Arc::clone(&email.user_catalog_identities);@@ -1200,9 +1278,11 @@ fn harness() -> TestHarness {primary_soup_service: CountingSoupService::default(),email_service: email,email_content_reader,+ thread_reader,activity_reader,authorization_calls,inbox_calls,+ thread_reads,user_label_calls,user_link_calls,user_catalog_identities,@@ -1265,14 +1345,17 @@ impl TestHarness {.data(graphql_soup::EmailMutationThreadLoader::new(self.email_service.clone(),))- .data(graphql_soup::soup_item_loader(- self.soup_service.clone(),- Arc::new(self.email_service.clone()),+ .data(graphql_soup::SoupItemDataLoader::new(+ graphql_soup::SoupItemLoader::new(+ self.soup_service.clone(),+ self.thread_reader.clone(),+ ),)).data(graphql_soup::AgentSessionEntityLoader(graphql_soup::soup_item_loader(self.primary_soup_service.clone(),Arc::new(self.email_service.clone()),+ self.state.entity_access.as_ref().clone(),),)).data(graphql_properties::entity_properties_loader(@@ -1318,6 +1401,7 @@ async fn soup_updates_subscribes_as_the_authenticated_user() {let loader = graphql_soup::SoupUpdateHydrationLoader(graphql_soup::soup_item_loader(soup_service.clone(),Arc::new(NoOpEmailService),+ NoOpEntityAccessService,));let schema: SoupSchema<CountingSoupService,@@ -1459,6 +1543,7 @@ async fn agent_session_log_appended_streams_runs_for_an_accessible_session() {let loader = graphql_soup::AgentSessionEntityLoader(graphql_soup::soup_item_loader(soup_service.clone(),Arc::new(NoOpEmailService),+ NoOpEntityAccessService,));let schema = build_schema_with_service::<CountingSoupService,@@ -2041,8 +2126,8 @@ async fn email_thread_returns_the_canonical_soup_type_and_forwards_message_paginlet harness = harness();let thread_id = Uuid::from_u128(42);harness- .soup_service- .set_raw_response(vec![soup_email_thread(thread_id)]);+ .thread_reader+ .set_threads(vec![soup_email_thread(thread_id)]);let response = harness.execute(&format!(@@ -2061,8 +2146,9 @@ async fn email_thread_returns_the_canonical_soup_type_and_forwards_message_paginassert_eq!(thread["isRead"], false);assert_eq!(thread["messages"][0]["threadId"], thread_id.to_string());assert_eq!(thread["messages"][0]["bodyParsed"], "Direct thread body");- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);- assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 1);+ assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);+ assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);assert_eq!(*harness.email_content_reader@@ -2398,8 +2484,8 @@ async fn email_message_full_fields_request_the_full_edge_payload() {let harness = harness();let thread_id = Uuid::from_u128(43);harness- .soup_service- .set_raw_response(vec![soup_email_thread(thread_id)]);+ .thread_reader+ .set_threads(vec![soup_email_thread(thread_id)]);let response = harness.execute(&format!(@@ -2440,8 +2526,8 @@ async fn latest_email_message_full_fields_request_the_full_edge_payload() {let harness = harness();let thread_id = Uuid::from_u128(44);harness- .soup_service- .set_raw_response(vec![soup_email_thread(thread_id)]);+ .thread_reader+ .set_threads(vec![soup_email_thread(thread_id)]);let response = harness.execute(&format!(@@ -2549,10 +2635,9 @@ async fn email_mutations_return_primary_state_even_when_the_soup_replica_is_stal}#[tokio::test]-async fn email_thread_returns_null_when_soup_cannot_load_the_thread() {+async fn email_thread_returns_null_when_the_viewer_cannot_read_the_thread() {let harness = harness();let thread_id = Uuid::from_u128(43);- harness.soup_service.set_raw_response(Vec::new());let response = harness.execute(&format!(@@ -2563,8 +2648,9 @@ async fn email_thread_returns_null_when_soup_cannot_load_the_thread() {assert!(response.errors.is_empty(), "{:?}", response.errors);let data = response.data.into_json().unwrap();assert!(data["user"]["emailThread"].is_null());- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);- assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 1);+ assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);+ assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);assert!(harness.email_content_reader@@ -2575,6 +2661,61 @@ async fn email_thread_returns_null_when_soup_cannot_load_the_thread() {);}+/// The lookup mints a view receipt through entity access, the rule the REST+/// thread route applies, so a thread shared through a team's CRM or a project+/// reaches the email read while an inaccessible one never does.+#[tokio::test]+async fn email_thread_lookups_settle_access_before_reading_the_email_service() {+ let harness = harness();+ let viewable_id = Uuid::from_u128(45);+ let hidden_id = Uuid::from_u128(46);+ harness+ .state+ .entity_access+ .viewable_email_threads+ .lock()+ .unwrap()+ .insert(viewable_id);+ let loader = graphql_soup::soup_item_loader(+ harness.soup_service.clone(),+ Arc::new(harness.email_service.clone()),+ harness.state.entity_access.as_ref().clone(),+ );++ for thread_id in [viewable_id, hidden_id] {+ let request = harness+ .request(+ &format!(+ r#"{{ user {{ emailThread(input: {{threadId: "{thread_id}"}}) {{ id }} }} }}"#+ ),+ authenticated_parts(),+ )+ .data(loader.clone());+ let response = harness.schema.execute(request).await;+ assert!(response.errors.is_empty(), "{:?}", response.errors);+ assert!(response.data.into_json().unwrap()["user"]["emailThread"].is_null());+ }++ assert_eq!(+ *harness+ .state+ .entity_access+ .email_thread_checks+ .lock()+ .unwrap(),+ vec![viewable_id, hidden_id]+ );+ assert_eq!(+ *harness+ .email_service+ .thread_preview_receipts+ .lock()+ .unwrap(),+ vec![vec![viewable_id]]+ );+ assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);+}+#[tokio::test]async fn email_thread_rejects_an_invalid_thread_id_before_loading_soup() {let harness = harness();@@ -2590,6 +2731,7 @@ async fn email_thread_rejects_an_invalid_thread_id_before_loading_soup() {.starts_with("invalid threadId UUID"));assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 0);assert_eq!(harness.inbox_calls.load(Ordering::SeqCst), 0);}diff --git a/crates/complete_graph/src/schema/test/email_archive.rs b/crates/complete_graph/src/schema/test/email_archive.rsindex 3662975b79..c74255af1e 100644--- a/crates/complete_graph/src/schema/test/email_archive.rs+++ b/crates/complete_graph/src/schema/test/email_archive.rs@@ -5,8 +5,8 @@ async fn ordinary_email_queries_use_the_replica_while_mutations_reload_from_primlet harness = harness();let id = Uuid::from_u128(44);harness- .soup_service- .set_raw_response(vec![soup_email_thread(id)]);+ .thread_reader+ .set_threads(vec![soup_email_thread(id)]);let query = format!(r#"{{ user {{ emailThread(input: {{threadId: "{id}"}}) {{isRead inboxVisible}} }} }}"#);@@ -15,7 +15,7 @@ async fn ordinary_email_queries_use_the_replica_while_mutations_reload_from_primlet before = before.data.into_json().unwrap();assert_eq!(before["user"]["emailThread"]["isRead"], false);assert_eq!(before["user"]["emailThread"]["inboxVisible"], true);- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);let mutation = harness.execute_authenticated_mutation(&format!(r#"mutation {{@@ -28,13 +28,13 @@ async fn ordinary_email_queries_use_the_replica_while_mutations_reload_from_primassert_eq!(mutation["seen"]["isRead"], true);assert_eq!(mutation["archived"]["isRead"], true);assert_eq!(mutation["archived"]["inboxVisible"], false);- // Neither mutation consulted the lagging Soup reader.- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 1);+ // Neither mutation consulted the lagging replica reader.+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 1);let after = harness.execute(&query).await;assert!(after.errors.is_empty(), "{:?}", after.errors);assert_eq!(after.data.into_json().unwrap(), before);- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 2);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 2);// Normal reads catch up only when the replica does, not by silently// escalating their pool after a mutation.@@ -43,8 +43,8 @@ async fn ordinary_email_queries_use_the_replica_while_mutations_reload_from_prim};replica.thread.inbox_visible = false;harness- .soup_service- .set_raw_response(vec![SoupItem::EmailThread(replica)]);+ .thread_reader+ .set_threads(vec![SoupItem::EmailThread(replica)]);let caught_up = harness.execute(&query).await;assert!(caught_up.errors.is_empty(), "{:?}", caught_up.errors);let caught_up = caught_up.data.into_json().unwrap();@@ -75,8 +75,8 @@ async fn archive_undo_and_redo_return_primary_state_without_reading_the_soup_replet thread_id = Uuid::from_u128(44);// The replica never changes during these writes.harness- .soup_service- .set_raw_response(vec![soup_email_thread(thread_id)]);+ .thread_reader+ .set_threads(vec![soup_email_thread(thread_id)]);for archived in [true, false, true] {let response = harness.execute_authenticated_mutation(&format!(r#"mutation {{ setEmailThreadArchived(input: {{threadId: "{thread_id}", archived: {archived}}}) {{__typename id inboxVisible}} }}"#@@ -88,7 +88,7 @@ async fn archive_undo_and_redo_return_primary_state_without_reading_the_soup_repassert_eq!(thread["id"], thread_id.to_string());assert_eq!(thread["inboxVisible"], !archived);}- assert_eq!(harness.raw_soup_calls.load(Ordering::SeqCst), 0);+ assert_eq!(harness.thread_reads.load(Ordering::SeqCst), 0);assert_eq!(*harness.email_service.archive_mutation_calls.lock().unwrap(),[true, false, true].map(|archived| (diff --git a/crates/complete_graph/src/schema/test/initiative.rs b/crates/complete_graph/src/schema/test/initiative.rsindex e67cc50691..e7d5616ed0 100644--- a/crates/complete_graph/src/schema/test/initiative.rs+++ b/crates/complete_graph/src/schema/test/initiative.rs@@ -12,6 +12,7 @@ async fn execute_initiative_query(harness: &TestHarness, query: &str) -> async_ggraphql_soup::soup_item_loader(harness.soup_service.clone(),Arc::new(harness.email_service.clone()),+ harness.state.entity_access.as_ref().clone(),),)),)diff --git a/crates/complete_graph/src/schema/test/soup_patches.rs b/crates/complete_graph/src/schema/test/soup_patches.rsindex 0f3608e793..87ddf31b97 100644--- a/crates/complete_graph/src/schema/test/soup_patches.rs+++ b/crates/complete_graph/src/schema/test/soup_patches.rs@@ -66,10 +66,15 @@ async fn subscription_responses_from(sender.send(patch).await.unwrap();}drop(sender);- let list_loader = graphql_soup::soup_item_loader(replica.clone(), Arc::new(NoOpEmailService));+ let list_loader = graphql_soup::soup_item_loader(+ replica.clone(),+ Arc::new(NoOpEmailService),+ NoOpEntityAccessService,+ );let hydration_loader = graphql_soup::SoupUpdateHydrationLoader(graphql_soup::soup_item_loader(primary,Arc::new(NoOpEmailService),+ NoOpEntityAccessService,));let schema: RealtimeSchema = build_schema_with_services(replica,@@ -368,7 +373,11 @@ impl MutationEffectsQuery {async fn mutation_effects_share_non_nullable_hydration_and_omit_misses() {let soup = CountingSoupService::default();soup.set_raw_response(vec![soup_document(Uuid::from_u128(1))]);- let loader = graphql_soup::soup_item_loader(soup.clone(), Arc::new(NoOpEmailService));+ let loader = graphql_soup::soup_item_loader(+ soup.clone(),+ Arc::new(NoOpEmailService),+ NoOpEntityAccessService,+ );let schema = Schema::build(MutationEffectsQuery, EmptyMutation, EmptySubscription).data(loader).data(entity_mutation::EntityMutationActor {diff --git a/crates/email/src/domain/ports.rs b/crates/email/src/domain/ports.rsindex 7796a0c605..642f98ff33 100644--- a/crates/email/src/domain/ports.rs+++ b/crates/email/src/domain/ports.rs@@ -508,6 +508,17 @@ pub trait EmailThreadMailProjectionService: Send + Sync + 'static {) -> impl Future<Output = Result<HashMap<Uuid, EmailThreadMailProjection>, EmailErr>> + Send;}+/// Read-only domain service used to hydrate Soup previews of authorized threads.+pub trait EmailThreadPreviewService: Send + Sync + 'static {+ /// Fetch the Soup preview of each authorized thread in one batch, as the+ /// thread's own inbox renders it. A thread without a row is absent.+ fn get_email_thread_previews_by_receipts(+ &self,+ viewer: MacroUserIdStr<'static>,+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> impl Future<Output = Result<Vec<EnrichedEmailThreadPreview>, EmailErr>> + Send;+}+/// Read-only domain service used to hydrate lightweight email content edges.pub trait EmailContentService: Send + Sync + 'static {/// Fetch the newest non-draft parsed content message for each authorized thread.@@ -543,6 +554,7 @@ pub trait EmailContentService: Send + Sync + 'static {/// This narrows capabilities, not consistency: the wrapped service may use a/// primary or replica pool. A bare `EmailServiceImpl` will not silently satisfy/// `EmailPreviewServiceReadOnly`.+#[derive(Clone)]pub struct ReadonlyEmailPreviewAdapter<T>(pub T);impl<T: EmailService> EmailPreviewServiceReadOnly for ReadonlyEmailPreviewAdapter<T> {@@ -559,6 +571,17 @@ impl<T: EmailService> EmailPreviewServiceReadOnly for ReadonlyEmailPreviewAdapte}}+impl<T: EmailThreadPreviewService> EmailThreadPreviewService for ReadonlyEmailPreviewAdapter<T> {+ fn get_email_thread_previews_by_receipts(+ &self,+ viewer: MacroUserIdStr<'static>,+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> impl Future<Output = Result<Vec<EnrichedEmailThreadPreview>, EmailErr>> + Send {+ self.0+ .get_email_thread_previews_by_receipts(viewer, receipts)+ }+}+/// User-scoped read operations consumed by email inbound adapters.pub trait EmailUserService: Send + Sync + 'static {/// Verified senders for an owned/delegated inbox. Authorization precedes@@ -1256,6 +1279,16 @@ impl EmailThreadMailProjectionService for NoOpEmailService {}}+impl EmailThreadPreviewService for NoOpEmailService {+ async fn get_email_thread_previews_by_receipts(+ &self,+ _viewer: MacroUserIdStr<'static>,+ _receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> Result<Vec<EnrichedEmailThreadPreview>, EmailErr> {+ Err(no_op_email_err())+ }+}+impl EmailContentService for NoOpEmailService {async fn get_latest_messages_parsed(&self,diff --git a/crates/email/src/domain/service.rs b/crates/email/src/domain/service.rsindex a545d78afd..1142136801 100644--- a/crates/email/src/domain/service.rs+++ b/crates/email/src/domain/service.rs@@ -25,7 +25,7 @@ use crate::domain::{},ports::{EmailContentService, EmailMessageEnqueuer, EmailRepo, EmailService,- EmailThreadMailProjectionService, EmailThreadMetadataService,+ EmailThreadMailProjectionService, EmailThreadMetadataService, EmailThreadPreviewService,},};use crm::domain::service::CrmService;@@ -611,6 +611,26 @@ where}}+impl<T, U, E, CS, Eam, B> EmailThreadPreviewService for EmailServiceImpl<T, U, E, CS, Eam, B>+where+ T: EmailRepo,+ U: FrecencyQueryService,+ E: EmailMessageEnqueuer,+ CS: CrmService,+ Eam: EntityAccessManagementService,+ B: MacroEventBroker,+ anyhow::Error: From<T::Err>,+{+ async fn get_email_thread_previews_by_receipts(+ &self,+ viewer: macro_user_id::user_id::MacroUserIdStr<'static>,+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> Result<Vec<EnrichedEmailThreadPreview>, EmailErr> {+ self.get_email_thread_previews_by_receipts_impl(viewer, receipts)+ .await+ }+}+impl<T, U, E, CS, Eam, B> EmailContentService for EmailServiceImpl<T, U, E, CS, Eam, B>whereT: EmailRepo,diff --git a/crates/email/src/domain/service/previews.rs b/crates/email/src/domain/service/previews.rsindex e300e5c607..a7eb6e95af 100644--- a/crates/email/src/domain/service/previews.rs+++ b/crates/email/src/domain/service/previews.rs@@ -1,23 +1,30 @@use entity_access::domain::models::EntityAccessReceipt;use entity_access::domain::models::MemberTeamRole;+use entity_access::domain::models::ViewAccessLevel;use crate::domain::{models::{- EmailErr, EnrichedEmailThreadPreview, GetEmailsRequest, PreviewCursorQuery, UserProvider,+ EmailErr, EnrichedEmailThreadPreview, GetEmailsRequest, PreviewCursorQuery, PreviewView,+ PreviewViewStandardLabel, UserProvider,},ports::EmailRepo,};+use filter_ast::Expr;use frecency::domain::{models::{AggregateId, FrecencyByIdsRequest, FrecencyData},ports::FrecencyQueryService,};+use item_filters::ast::email::EmailLiteral;use macro_user_id::cowlike::CowLike;+use macro_user_id::user_id::MacroUserIdStr;use model_entity::EntityType;-use models_pagination::{CollectBy, PaginateOn, PaginatedCursor, SimpleSortMethod};+use models_pagination::{CollectBy, PaginateOn, PaginatedCursor, Query, SimpleSortMethod};use std::collections::HashMap;+use std::sync::Arc;use uuid::Uuid;use super::EmailServiceImpl;+use super::thread::email_thread_ids_from_receipts;#[cfg(test)]mod test;@@ -147,6 +154,42 @@ where.into_page())}+ /// Hydrate the Soup preview of each receipt-authorized thread. The+ /// receipts settled who may read the threads; the preview query only+ /// renders them, scoped to the inboxes that own them.+ #[tracing::instrument(err, skip(self, receipts), fields(thread_count = receipts.len()))]+ pub(crate) async fn get_email_thread_previews_by_receipts_impl(+ &self,+ viewer: MacroUserIdStr<'static>,+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,+ ) -> Result<Vec<EnrichedEmailThreadPreview>, EmailErr> {+ let thread_ids = email_thread_ids_from_receipts(receipts)?;+ if thread_ids.is_empty() {+ return Ok(Vec::new());+ }++ let mut link_ids: Vec<Uuid> = self+ .email_repo+ .thread_metadata_by_ids(&thread_ids)+ .await+ .map_err(anyhow::Error::from)?+ .into_iter()+ .map(|metadata| metadata.link_id)+ .collect();+ link_ids.sort_unstable();+ link_ids.dedup();+ if link_ids.is_empty() {+ return Ok(Vec::new());+ }++ Ok(self+ .get_email_thread_previews_impl(authorized_threads_request(+ viewer, thread_ids, link_ids,+ ))+ .await?+ .items)+ }+pub(crate) async fn get_link_by_auth_id_and_macro_id_impl(&self,auth_id: &str,@@ -224,3 +267,30 @@ whereOk(Some(team_id))}}++/// The preview request for threads whose receipts already authorized the+/// read. Each thread renders in the view of the inbox that owns it, as the+/// CRM list renders teammates' threads, and the exact id filter keeps the+/// widened inbox scope from admitting any other thread.+fn authorized_threads_request(+ viewer: MacroUserIdStr<'static>,+ thread_ids: Vec<Uuid>,+ link_ids: Vec<Uuid>,+) -> GetEmailsRequest {+ let limit = u32::try_from(thread_ids.len()).unwrap_or(u32::MAX);+ let thread_filter = thread_ids+ .into_iter()+ .map(|id| Expr::val(EmailLiteral::ThreadId(id)))+ .reduce(Expr::or)+ .map(Arc::new);+ GetEmailsRequest {+ view: PreviewView::StandardLabel(PreviewViewStandardLabel::All),+ link_ids,+ macro_id: viewer,+ limit: Some(limit),+ query: Query::Sort(SimpleSortMethod::UpdatedAt, thread_filter),+ include_frecency: false,+ team_receipt: None,+ crm_scope: None,+ }+}diff --git a/crates/email/src/domain/service/previews/test.rs b/crates/email/src/domain/service/previews/test.rsindex d81770cddf..aa4129eace 100644--- a/crates/email/src/domain/service/previews/test.rs+++ b/crates/email/src/domain/service/previews/test.rs@@ -32,3 +32,47 @@ async fn skips_frecency_lookup_when_not_requested() {assert!(scores.is_empty());}++#[test]+fn authorized_threads_render_in_their_owning_inboxes_with_an_exact_id_filter() {+ let viewer = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();+ let first = Uuid::from_u128(1);+ let second = Uuid::from_u128(2);+ let own_link = Uuid::from_u128(10);+ let teammate_link = Uuid::from_u128(11);++ let request = authorized_threads_request(+ viewer.clone(),+ vec![first, second],+ vec![own_link, teammate_link],+ );++ assert_eq!(request.link_ids, vec![own_link, teammate_link]);+ assert_eq!(request.macro_id, viewer);+ assert_eq!(request.limit, Some(2));+ assert!(matches!(+ request.view,+ PreviewView::StandardLabel(PreviewViewStandardLabel::All)+ ));+ assert!(!request.include_frecency);+ assert!(request.team_receipt.is_none());+ assert!(request.crm_scope.is_none());+ let Query::Sort(SimpleSortMethod::UpdatedAt, Some(filter)) = request.query else {+ panic!("expected an updated-at sort over the thread filter");+ };+ assert_eq!(ored_thread_ids(&filter), vec![first, second]);+}++fn ored_thread_ids(expr: &Expr<EmailLiteral>) -> Vec<Uuid> {+ match expr {+ Expr::Literal(EmailLiteral::ThreadId(id)) => vec![*id],+ Expr::Or(left, right) => {+ let mut ids = ored_thread_ids(left);+ ids.extend(ored_thread_ids(right));+ ids+ }+ Expr::And(..) | Expr::Not(..) | Expr::Literal(_) => {+ panic!("the thread filter must only OR thread ids")+ }+ }+}diff --git a/crates/email/src/domain/service/thread.rs b/crates/email/src/domain/service/thread.rsindex 42c93d3f3d..6e6187f5e4 100644--- a/crates/email/src/domain/service/thread.rs+++ b/crates/email/src/domain/service/thread.rs@@ -462,7 +462,7 @@ where}}-fn email_thread_ids_from_receipts(+pub(super) fn email_thread_ids_from_receipts(receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,) -> Result<Vec<Uuid>, EmailErr> {receiptsdiff --git a/crates/graphql_initiative/src/test/soup.rs b/crates/graphql_initiative/src/test/soup.rsindex 7a2e686ebf..61c8463121 100644--- a/crates/graphql_initiative/src/test/soup.rs+++ b/crates/graphql_initiative/src/test/soup.rs@@ -4,7 +4,9 @@ use std::sync::{Arc, Mutex};use chrono::{DateTime, Utc};use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};-use graphql_soup::{SoupInboxReader, SoupItemDataLoader, SoupItemLoader, SoupItemLoaderError};+use graphql_soup::{+ SoupEmailThreadReader, SoupItemDataLoader, SoupItemLoader, SoupItemLoaderError,+};use macro_user_id::user_id::MacroUserIdStr;use model_owner::Owner;use models_pagination::{Paginated, PaginatedCursor, SimpleSortMethod};@@ -59,7 +61,7 @@ impl RecordingSoupService {}pub(super) fn loader(&self) -> SoupItemDataLoader {- SoupItemDataLoader::new(SoupItemLoader::new(self.clone(), NoInboxes))+ SoupItemDataLoader::new(SoupItemLoader::new(self.clone(), NoThreads))}}@@ -160,13 +162,17 @@ impl SoupService for RecordingSoupService {}}-struct NoInboxes;+struct NoThreads;-impl SoupInboxReader for NoInboxes {- async fn get_inbox_ids(+impl SoupEmailThreadReader for NoThreads {+ async fn read_threads(&self,_user_id: MacroUserIdStr<'static>,- ) -> Result<Vec<Uuid>, SoupItemLoaderError> {- unreachable!("initiative hydration must not request email inboxes")+ _thread_ids: Vec<Uuid>,+ ) -> Result<+ Vec<models_soup::email_thread::SoupEnrichedEmailThreadPreview<()>>,+ SoupItemLoaderError,+ > {+ unreachable!("initiative hydration must not request email threads")}}diff --git a/crates/graphql_soup/AGENTS.md b/crates/graphql_soup/AGENTS.mdindex 836bde3000..e1e896f768 100644--- a/crates/graphql_soup/AGENTS.md+++ b/crates/graphql_soup/AGENTS.md@@ -7,3 +7,12 @@ soup-domain GraphQL wrapper types. The composed schema (root query, viewer,SDL export) lives in `complete_graph` — see `../complete_graph/AGENTS.md` forthe schema conventions (presence-of-`id` normalized-cache rules and theviewer pattern), which apply to every type defined here.++## Gotchas++- `SoupItemLoader` (single-entity lookups, `soupUpdates` hydration) resolves+ email threads through entity-access view receipts, the rule the REST thread+ route applies, not through the viewer's inbox scope; Soup's email leg only+ serves lists. A thread shared through a team's CRM, a project, or a share+ therefore opens through `emailThread` even though it lives in a teammate's+ inbox.diff --git a/crates/graphql_soup/src/lib.rs b/crates/graphql_soup/src/lib.rsindex 0a105c746f..505878de06 100644--- a/crates/graphql_soup/src/lib.rs+++ b/crates/graphql_soup/src/lib.rs@@ -25,9 +25,9 @@ pub use folders::{pub use graphql_common::{GraphqlRequestParts, GraphqlSoupEntityType};pub use inputs::{GraphqlSimpleSortMethod, GroupedSoupInput, SoupInput};pub use loaders::{- AgentSessionEntityLoader, EmailServiceInboxReader, SoupInboxReader, SoupItemDataLoader,- SoupItemLoader, SoupItemLoaderError, SoupItemLoaderKey, SoupUpdateHydrationLoader,- soup_item_loader, soup_item_loader_with_team_access,+ AgentSessionEntityLoader, EntityAccessEmailThreadReader, SoupEmailThreadReader,+ SoupItemDataLoader, SoupItemLoader, SoupItemLoaderError, SoupItemLoaderKey,+ SoupUpdateHydrationLoader, soup_item_loader, soup_item_loader_with_team_access,};pub use mutation_thread::{EmailMutationThreadLoader, EmailMutationThreadReader, email_mutation_thread_loader,diff --git a/crates/graphql_soup/src/loaders.rs b/crates/graphql_soup/src/loaders.rsindex 35f6eb6289..6dddbb8ff0 100644--- a/crates/graphql_soup/src/loaders.rs+++ b/crates/graphql_soup/src/loaders.rs@@ -1,4 +1,9 @@-//! DataLoader-backed hydration of lightweight realtime Soup patches.+//! DataLoader-backed hydration of lightweight realtime Soup patches and+//! single-entity lookups. Email threads hydrate through entity access+//! receipts, the rule the REST thread route applies, so a thread shared by a+//! team's CRM or a project resolves for the viewer even though it lives in a+//! teammate's inbox; every other entity hydrates through one filtered Soup+//! request per user.#[cfg(test)]mod test;@@ -12,36 +17,37 @@ use std::{use async_graphql::dataloader::{DataLoader, Loader};use email::domain::{models::{PreviewView, PreviewViewStandardLabel},- ports::EmailService,+ ports::EmailThreadPreviewService,};use entity_access::domain::{- models::{EntityAccessReceipt, MemberTeamRole},+ models::{AccessError, EntityAccessReceipt, MemberTeamRole},ports::EntityAccessService,};use filter_ast::Expr;-use futures::{future::BoxFuture, future::try_join_all};-use item_filters::{- SharedEmailFilter,- ast::{- EmailFilterAst, EntityFilterAst,- agent_session::AgentSessionLiteral,- calendar_event::CalendarEventLiteral,- call::CallLiteral,- channel::{ChannelLiteral, ChannelThreadLiteral},- chat::ChatLiteral,- crm_company::CrmCompanyLiteral,- crm_contact::CrmContactLiteral,- database_row::DatabaseRowLiteral,- document::DocumentLiteral,- email::EmailLiteral,- foreign_entity::ForeignEntityLiteral,- initiative::InitiativeLiteral,- project::ProjectLiteral,- },+use futures::{+ future::BoxFuture,+ future::{try_join, try_join_all},+};+use item_filters::ast::{+ EmailFilterAst, EntityFilterAst,+ agent_session::AgentSessionLiteral,+ calendar_event::CalendarEventLiteral,+ call::CallLiteral,+ channel::{ChannelLiteral, ChannelThreadLiteral},+ chat::ChatLiteral,+ crm_company::CrmCompanyLiteral,+ crm_contact::CrmContactLiteral,+ database_row::DatabaseRowLiteral,+ document::DocumentLiteral,+ email::EmailLiteral,+ foreign_entity::ForeignEntityLiteral,+ initiative::InitiativeLiteral,+ project::ProjectLiteral,};use macro_user_id::user_id::MacroUserIdStr;use model_entity::{Entity, EntityType};use models_pagination::SimpleSortMethod;+use models_soup::{email_thread::SoupEnrichedEmailThreadPreview, item::SoupItem};use rootcause::{Report,markers::{Cloneable, Dynamic},@@ -61,41 +67,82 @@ pub type SoupItemLoaderKey = (MacroUserIdStr<'static>, Entity<'static>);/// Cloneable error returned by the realtime Soup item loader.pub type SoupItemLoaderError = Report<Dynamic, Cloneable>;-/// Resolves the inboxes visible to a user for email-thread Soup hydration.-pub trait SoupInboxReader: Send + Sync + 'static {- /// Return the IDs of every inbox visible to `user_id`.- fn get_inbox_ids(+/// Hydrates the email threads a viewer may read.+pub trait SoupEmailThreadReader: Send + Sync + 'static {+ /// Return the preview of each requested thread that `user_id` may view.+ /// A thread the viewer cannot read, or that does not exist, is absent.+ fn read_threads(&self,user_id: MacroUserIdStr<'static>,- ) -> impl Future<Output = Result<Vec<Uuid>, SoupItemLoaderError>> + Send;+ thread_ids: Vec<Uuid>,+ ) -> impl Future<Output = Result<Vec<SoupEnrichedEmailThreadPreview<()>>, SoupItemLoaderError>> + Send;}-/// Inbox reader backed by the email domain service.-pub struct EmailServiceInboxReader<E> {- /// Shared email service used to resolve visible inboxes.- service: Arc<E>,+/// Thread reader that settles access through the entity access service, the+/// rule the REST thread route applies, and hydrates through the email domain.+pub struct EntityAccessEmailThreadReader<E, A> {+ /// Shared email service that renders the authorized threads.+ email_service: Arc<E>,+ /// Access service that mints the view receipts.+ access: A,}-impl<E> EmailServiceInboxReader<E> {- /// Construct an inbox reader from the shared email service.- pub fn new(service: Arc<E>) -> Self {- Self { service }+impl<E, A> EntityAccessEmailThreadReader<E, A> {+ /// Construct a thread reader from the shared email and access services.+ pub fn new(email_service: Arc<E>, access: A) -> Self {+ Self {+ email_service,+ access,+ }}}-impl<E> SoupInboxReader for EmailServiceInboxReader<E>+impl<E, A> SoupEmailThreadReader for EntityAccessEmailThreadReader<E, A>where- E: EmailService,+ E: EmailThreadPreviewService,+ A: EntityAccessService,{- async fn get_inbox_ids(+ #[tracing::instrument(+ name = "soup.hydrate_email_threads",+ skip_all,+ fields(thread_count = thread_ids.len())+ )]+ async fn read_threads(&self,user_id: MacroUserIdStr<'static>,- ) -> Result<Vec<Uuid>, SoupItemLoaderError> {- self.service- .get_inboxes_for_macro_id(user_id)+ thread_ids: Vec<Uuid>,+ ) -> Result<Vec<SoupEnrichedEmailThreadPreview<()>>, SoupItemLoaderError> {+ let ids = thread_ids.iter().map(Uuid::to_string).collect::<Vec<_>>();+ let results = self+ .access+ .generate_email_thread_view_access_receipts(&user_id, None, &ids)+ .await;+ let mut receipts = Vec::with_capacity(results.len());+ for (thread_id, result) in results {+ match result {+ Ok(receipt) => receipts.push(receipt),+ Err(+ AccessError::Unauthorized+ | AccessError::UnauthorizedWithMessage(_)+ | AccessError::NotFound(_),+ ) => {}+ Err(error) => {+ return Err(rootcause::report!(+ "email thread {thread_id} access check failed: {error}"+ )+ .into_cloneable());+ }+ }+ }+ if receipts.is_empty() {+ return Ok(Vec::new());+ }+ let previews = self+ .email_service+ .get_email_thread_previews_by_receipts(user_id, receipts).await- .map(|links| links.into_iter().map(|link| link.id).collect())- .map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())+ .map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())?;+ Ok(previews.into_iter().map(Into::into).collect())}}@@ -139,47 +186,108 @@ impl<A: EntityAccessService> SoupTeamReader for EntityAccessTeamReader<A> {}}-/// Batches lightweight entity patches into one filtered Soup request per user.-pub struct SoupItemLoader<S, I> {+/// Batches lightweight entity patches per user: email threads through the+/// access-checked thread reader, every other entity through one filtered Soup+/// request.+pub struct SoupItemLoader<S, R> {/// Existing Soup query service.soup_service: S,/// Reader used only when a batch requests email threads.- inbox_reader: I,+ thread_reader: R,/// Optional reader for current team access during PR and session hydration.team_reader: Option<Arc<dyn SoupTeamReader>>,}-impl<S, I> SoupItemLoader<S, I> {+impl<S, R> SoupItemLoader<S, R> {/// Construct a Soup item loader from its existing domain services.- pub fn new(soup_service: S, inbox_reader: I) -> Self {+ pub fn new(soup_service: S, thread_reader: R) -> Self {Self {soup_service,- inbox_reader,+ thread_reader,team_reader: None,}}}-impl<S, I> SoupItemLoader<S, I>+impl<S, R> SoupItemLoader<S, R>whereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{- /// Execute one filtered Soup request for one user's portion of a batch.+ /// Hydrate one user's portion of a batch.async fn load_user(&self,user_id: MacroUserIdStr<'static>,entities: Vec<Entity<'static>>,) -> Result<HashMap<SoupItemLoaderKey, SoupProjectionHydration>, SoupItemLoaderError> {let requested = entities.iter().cloned().collect::<HashSet<_>>();- let needs_inboxes = entities- .iter()- .any(|entity| entity.entity_type == EntityType::EmailThread);- let link_ids = if needs_inboxes {- self.inbox_reader.get_inbox_ids(user_id.clone()).await?- } else {- Vec::new()+ let mut thread_ids = Vec::new();+ let mut soup_entities = Vec::new();+ for entity in entities {+ if entity.entity_type == EntityType::EmailThread {+ thread_ids.push(entity_uuid(&entity)?);+ } else {+ soup_entities.push(entity);+ }+ }++ let threads = async {+ if thread_ids.is_empty() {+ return Ok(Vec::new());+ }+ self.thread_reader+ .read_threads(user_id.clone(), thread_ids)+ .await};+ let (threads, items) = try_join(+ threads,+ self.load_soup_items(user_id.clone(), soup_entities),+ )+ .await?;++ let hydrations = threads+ .into_iter()+ .map(|thread| SoupProjectionHydration {+ item: SoupItem::EmailThread(thread),+ document_server_facts: None,+ })+ .chain(items);+ let mut loaded = HashMap::with_capacity(requested.len());+ for hydration in hydrations {+ let entity = hydration.item.entity();+ if !requested.contains(&entity) {+ return Err(rootcause::report!(+ "filtered Soup request returned unrequested entity {} {}",+ entity.entity_type,+ entity.entity_id+ )+ .into_cloneable());+ }+ if loaded+ .insert((user_id.clone(), entity.clone()), hydration)+ .is_some()+ {+ return Err(rootcause::report!(+ "filtered Soup request returned duplicate entity {} {}",+ entity.entity_type,+ entity.entity_id+ )+ .into_cloneable());+ }+ }++ Ok(loaded)+ }++ /// Execute one filtered Soup request for the non-email entities of a batch.+ async fn load_soup_items(+ &self,+ user_id: MacroUserIdStr<'static>,+ entities: Vec<Entity<'static>>,+ ) -> Result<Vec<SoupProjectionHydration>, SoupItemLoaderError> {+ if entities.is_empty() {+ return Ok(Vec::new());+ }// CRM records are team-scoped: Soup skips them without the viewer's team.let needs_team = entities.iter().any(|entity| {matches!(@@ -204,52 +312,28 @@ wherecursor: SoupQuery::new_sort_simple(SimpleSortMethod::UpdatedAt, filter),// Batch entity load — order is irrelevant, the caller re-keys by id.sort_direction: SoupSortDirection::default(),- user: user_id.clone(),+ user: user_id,email_preview_view: PreviewView::StandardLabel(PreviewViewStandardLabel::All),- link_ids,+ // Email threads hydrate through the thread reader, so the request+ // carries no inbox scope and Soup's email leg stays off.+ link_ids: Vec::new(),};- let items = self+ Ok(self.soup_service.get_user_soup_with_projection(request, team).await.map_err(|error| rootcause::report!(error).into_dynamic().into_cloneable())?- .into_items();-- let mut loaded = HashMap::with_capacity(items.len());- for hydration in items {- let entity = hydration.item.entity();- if !requested.contains(&entity) {- return Err(rootcause::report!(- "filtered Soup request returned unrequested entity {} {}",- entity.entity_type,- entity.entity_id- )- .into_cloneable());- }- if loaded- .insert((user_id.clone(), entity.clone()), hydration)- .is_some()- {- return Err(rootcause::report!(- "filtered Soup request returned duplicate entity {} {}",- entity.entity_type,- entity.entity_id- )- .into_cloneable());- }- }-- Ok(loaded)+ .into_items())}}-impl<S, I> SoupItemLoader<S, I>+impl<S, R> SoupItemLoader<S, R>whereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{- /// Partition keys by user and execute one Soup request for each user.+ /// Partition keys by user and hydrate each user's keys in one pass.async fn load_keys(&self,keys: Vec<SoupItemLoaderKey>,@@ -267,10 +351,10 @@ where}}-impl<S, I> Loader<SoupItemLoaderKey> for SoupItemLoader<S, I>+impl<S, R> Loader<SoupItemLoaderKey> for SoupItemLoader<S, R>whereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{type Value = SoupProjectionHydration;type Error = SoupItemLoaderError;@@ -292,10 +376,10 @@ struct OwnedSoupItemLoaderKey {entity: Entity<'static>,}-impl<S, I> Loader<OwnedSoupItemLoaderKey> for SoupItemLoader<S, I>+impl<S, R> Loader<OwnedSoupItemLoaderKey> for SoupItemLoader<S, R>whereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{type Value = SoupProjectionHydration;type Error = SoupItemLoaderError;@@ -324,13 +408,13 @@ type LoadOne = dyn Fn(+ Sync;/// Load one statically owned key through a concrete DataLoader.-async fn load_one_owned<S, I>(- loader: Arc<DataLoader<SoupItemLoader<S, I>>>,+async fn load_one_owned<S, R>(+ loader: Arc<DataLoader<SoupItemLoader<S, R>>>,key: OwnedSoupItemLoaderKey,) -> Result<Option<SoupProjectionHydration>, SoupItemLoaderError>whereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{loader.load_one::<OwnedSoupItemLoaderKey>(key).await}@@ -344,10 +428,10 @@ pub struct SoupItemDataLoader {impl SoupItemDataLoader {/// Construct a type-erased DataLoader from a concrete loader implementation.- pub fn new<S, I>(loader: SoupItemLoader<S, I>) -> Self+ pub fn new<S, R>(loader: SoupItemLoader<S, R>) -> SelfwhereS: SoupService,- I: SoupInboxReader,+ R: SoupEmailThreadReader,{let loader = Arc::new(DataLoader::new(loader, tokio::spawn).max_batch_size(MAX_BATCH_SIZE));// A WebSocket may receive repeated updates for the same entity. Only@@ -385,15 +469,21 @@ pub struct AgentSessionEntityLoader(pub SoupItemDataLoader);#[derive(Clone)]pub struct SoupUpdateHydrationLoader(pub SoupItemDataLoader);-/// Build the realtime Soup DataLoader from the existing Soup and email services.-pub fn soup_item_loader<S, E>(soup_service: S, email_service: Arc<E>) -> SoupItemDataLoader+/// Build the Soup DataLoader from the Soup service and the access-checked+/// email thread reader.+pub fn soup_item_loader<S, E, A>(+ soup_service: S,+ email_service: Arc<E>,+ access: A,+) -> SoupItemDataLoaderwhereS: SoupService,- E: EmailService,+ E: EmailThreadPreviewService,+ A: EntityAccessService,{SoupItemDataLoader::new(SoupItemLoader::new(soup_service,- EmailServiceInboxReader::new(email_service),+ EntityAccessEmailThreadReader::new(email_service, access),))}@@ -405,10 +495,13 @@ pub fn soup_item_loader_with_team_access<S, E, A>() -> SoupItemDataLoaderwhereS: SoupService,- E: EmailService,+ E: EmailThreadPreviewService,A: EntityAccessService,{- let mut loader = SoupItemLoader::new(soup_service, EmailServiceInboxReader::new(email_service));+ let mut loader = SoupItemLoader::new(+ soup_service,+ EntityAccessEmailThreadReader::new(email_service, access.clone()),+ );loader.team_reader = Some(Arc::new(EntityAccessTeamReader(access)));SoupItemDataLoader::new(loader)}@@ -424,13 +517,25 @@ fn literal_tree<T>(literals: Vec<T>, impossible: T) -> Arc<Expr<T>> {)}+/// Parse an entity's id, which every Soup-hydrated entity carries as a UUID.+fn entity_uuid(entity: &Entity<'static>) -> Result<Uuid, SoupItemLoaderError> {+ Uuid::parse_str(entity.entity_id.as_ref()).map_err(|error| {+ rootcause::report!(+ "invalid {} entity id {}: {error}",+ entity.entity_type,+ entity.entity_id+ )+ .into_cloneable()+ })+}+/// Encode an exact entity set into all branches of the Soup filter AST.+/// Email threads hydrate through the thread reader and are not accepted here.fn entity_filter_ast(entities: &[Entity<'static>]) -> Result<EntityFilterAst, SoupItemLoaderError> {let mut documents = Vec::new();let mut chats = Vec::new();let mut projects = Vec::new();let mut initiatives = Vec::new();- let mut email_threads = Vec::new();let mut channels = Vec::new();let mut channel_threads = Vec::new();let mut calls = Vec::new();@@ -442,20 +547,18 @@ fn entity_filter_ast(entities: &[Entity<'static>]) -> Result<EntityFilterAst, Solet mut database_rows = Vec::new();for entity in entities {- let id = Uuid::parse_str(entity.entity_id.as_ref()).map_err(|error| {- rootcause::report!(- "invalid {} entity id {}: {error}",- entity.entity_type,- entity.entity_id- )- .into_cloneable()- })?;+ let id = entity_uuid(entity)?;match entity.entity_type {EntityType::Document => documents.push(DocumentLiteral::Id(id)),EntityType::Chat => chats.push(ChatLiteral::ChatId(id)),EntityType::Project => projects.push(ProjectLiteral::ProjectIdSelf(id)),EntityType::Initiative => initiatives.push(InitiativeLiteral::Id(id)),- EntityType::EmailThread => email_threads.push(EmailLiteral::ThreadId(id)),+ EntityType::EmailThread => {+ return Err(rootcause::report!(+ "email threads hydrate through the thread reader, not the Soup request"+ )+ .into_cloneable());+ }EntityType::Channel => channels.push(ChannelLiteral::ChannelId(id)),EntityType::ChannelMessage => {channel_threads.push(ChannelThreadLiteral::ThreadId(id));@@ -493,13 +596,10 @@ fn entity_filter_ast(entities: &[Entity<'static>]) -> Result<EntityFilterAst, Sodocument_filter: Some(literal_tree(documents, DocumentLiteral::Id(nil))),project_filter: Some(literal_tree(projects, ProjectLiteral::ProjectIdSelf(nil))),chat_filter: Some(literal_tree(chats, ChatLiteral::ChatId(nil))),+ // Email threads never ride this request; the impossible id rules+ // Soup's email leg out like every other unrequested branch.email_filter: EmailFilterAst {- tree: Some(Arc::new(Expr::and(- literal_tree(email_threads, EmailLiteral::ThreadId(nil))- .as_ref()- .clone(),- Expr::val(EmailLiteral::Shared(SharedEmailFilter::Include)),- ))),+ tree: Some(Arc::new(Expr::val(EmailLiteral::ThreadId(nil)))),crm_scope: None,},channel_filter: Some(literal_tree(channels, ChannelLiteral::ChannelId(nil))),diff --git a/crates/graphql_soup/src/loaders/test.rs b/crates/graphql_soup/src/loaders/test.rsindex fbe2e29793..30b1db6521 100644--- a/crates/graphql_soup/src/loaders/test.rs+++ b/crates/graphql_soup/src/loaders/test.rs@@ -5,23 +5,20 @@ use std::{use chrono::{DateTime, Utc};use entity_access::domain::models::{EntityAccessReceipt, MemberTeamRole};-use item_filters::{- SharedEmailFilter,- ast::{- call::CallLiteral,- channel::{ChannelLiteral, ChannelThreadLiteral},- chat::ChatLiteral,- crm_company::CrmCompanyLiteral,- document::DocumentLiteral,- email::EmailLiteral,- foreign_entity::ForeignEntityLiteral,- project::ProjectLiteral,- },+use item_filters::ast::{+ call::CallLiteral,+ channel::{ChannelLiteral, ChannelThreadLiteral},+ chat::ChatLiteral,+ crm_company::CrmCompanyLiteral,+ document::DocumentLiteral,+ email::EmailLiteral,+ foreign_entity::ForeignEntityLiteral,+ project::ProjectLiteral,};use model_owner::Owner;use models_pagination::{Paginated, PaginatedCursor};use models_properties::service::property_definition_with_options::PropertyDefinitionWithOptions;-use models_soup::{document::SoupDocument, item::SoupItem};+use models_soup::{document::SoupDocument, email_thread::SoupEmailThreadPreview};use soup::domain::{models::{EnrichedSoupItem, GroupedSortRequest, IntoSoupReqAst, SoupErr, SoupPropertiesField,@@ -148,24 +145,25 @@ impl SoupService for RecordingSoupService {}}-/// Inbox reader that records users and returns configured inbox IDs.+/// Thread reader that records requests and returns configured previews by user.#[derive(Clone, Default)]-struct RecordingInboxReader {- /// Users whose inboxes were requested.- calls: Arc<Mutex<Vec<MacroUserIdStr<'static>>>>,- /// Inbox IDs returned for each user.- responses: Arc<HashMap<MacroUserIdStr<'static>, Vec<Uuid>>>,+struct RecordingThreadReader {+ /// Users and thread ids whose previews were requested.+ calls: Arc<Mutex<Vec<(MacroUserIdStr<'static>, Vec<Uuid>)>>>,+ /// Previews returned for each user.+ responses: Arc<HashMap<MacroUserIdStr<'static>, Vec<SoupEnrichedEmailThreadPreview<()>>>>,}-impl SoupInboxReader for RecordingInboxReader {- async fn get_inbox_ids(+impl SoupEmailThreadReader for RecordingThreadReader {+ async fn read_threads(&self,user_id: MacroUserIdStr<'static>,- ) -> Result<Vec<Uuid>, SoupItemLoaderError> {+ thread_ids: Vec<Uuid>,+ ) -> Result<Vec<SoupEnrichedEmailThreadPreview<()>>, SoupItemLoaderError> {self.calls.lock()- .expect("inbox calls lock")- .push(user_id.clone());+ .expect("thread calls lock")+ .push((user_id.clone(), thread_ids));Ok(self.responses.get(&user_id).cloned().unwrap_or_default())}}@@ -202,6 +200,36 @@ fn document_named(id: Uuid, name: impl Into<String>) -> SoupItem<()> {})}+/// Build an email thread preview as the access-checked reader returns it.+fn email_thread(id: Uuid) -> SoupEnrichedEmailThreadPreview<()> {+ SoupEnrichedEmailThreadPreview {+ thread: SoupEmailThreadPreview {+ id,+ provider_id: None,+ owner_id: user("macro|[email protected]"),+ inbox_visible: true,+ is_read: false,+ is_draft: false,+ is_important: false,+ is_signal: false,+ name: Some("Shared thread".to_owned()),+ snippet: None,+ sender_email: None,+ sender_name: None,+ sender_photo_url: None,+ sort_ts: DateTime::<Utc>::default(),+ created_at: DateTime::<Utc>::default(),+ updated_at: DateTime::<Utc>::default(),+ viewed_at: None,+ project_id: None,+ },+ attachments: Vec::new(),+ participants: Vec::new(),+ labels: Vec::new(),+ extra: (),+ }+}+/// Collect literal UUIDs from a binary expression tree.fn collect_ids<T>(expr: &Expr<T>, literal_id: impl Copy + Fn(&T) -> Option<Uuid>) -> HashSet<Uuid> {match expr {@@ -215,40 +243,6 @@ fn collect_ids<T>(expr: &Expr<T>, literal_id: impl Copy + Fn(&T) -> Option<Uuid>}}-/// Collect thread IDs from an expression that contains only positive thread-ID literals.-fn positive_email_thread_ids(expr: &Expr<EmailLiteral>) -> Option<HashSet<Uuid>> {- match expr {- Expr::And(left, right) | Expr::Or(left, right) => {- let mut ids = positive_email_thread_ids(left)?;- ids.extend(positive_email_thread_ids(right)?);- Some(ids)- }- Expr::Not(_) => None,- Expr::Literal(EmailLiteral::ThreadId(id)) => Some(HashSet::from([*id])),- Expr::Literal(_) => None,- }-}--/// Return whether positive thread IDs are combined with shared-thread inclusion via `And`.-fn includes_shared_threads(expr: &Expr<EmailLiteral>, expected_ids: &HashSet<Uuid>) -> bool {- let is_shared_include = |expr: &Expr<EmailLiteral>| {- matches!(- expr,- Expr::Literal(EmailLiteral::Shared(SharedEmailFilter::Include))- )- };- let has_expected_ids =- |expr: &Expr<EmailLiteral>| positive_email_thread_ids(expr).as_ref() == Some(expected_ids);-- match expr {- Expr::And(left, right) => {- (has_expected_ids(left) && is_shared_include(right))- || (is_shared_include(left) && has_expected_ids(right))- }- Expr::Or(_, _) | Expr::Not(_) | Expr::Literal(_) => false,- }-}-#[tokio::test]async fn batches_all_keys_for_one_user_into_one_soup_request() {let user_id = user("macro|[email protected]");@@ -259,7 +253,7 @@ async fn batches_all_keys_for_one_user_into_one_soup_request() {vec![document(first_id), document(second_id)],);let calls = Arc::clone(&service.calls);- let loader = SoupItemLoader::new(service, RecordingInboxReader::default());+ let loader = SoupItemLoader::new(service, RecordingThreadReader::default());let keys = vec![(user_id.clone(),@@ -292,7 +286,7 @@ async fn separates_one_soup_request_per_unique_user() {.with_response(first_user.clone(), vec![document(first_id)]).with_response(second_user.clone(), vec![document(second_id)]);let calls = Arc::clone(&service.calls);- let loader = SoupItemLoader::new(service, RecordingInboxReader::default());+ let loader = SoupItemLoader::new(service, RecordingThreadReader::default());let keys = vec![(first_user.clone(),@@ -318,20 +312,25 @@ async fn separates_one_soup_request_per_unique_user() {assert_eq!(users, HashSet::from([first_user, second_user]));}+/// A thread outside the viewer's inboxes (a teammate's thread reached through+/// the team's CRM) hydrates when entity access grants it, without any inbox+/// scope and without a Soup request.#[tokio::test]-async fn resolves_inboxes_once_for_a_users_email_entities() {+async fn email_threads_hydrate_through_the_access_checked_reader_not_the_viewer_inboxes() {let user_id = user("macro|[email protected]");let first_id = Uuid::from_u128(11);let second_id = Uuid::from_u128(12);- let inbox_id = Uuid::from_u128(13);let service = RecordingSoupService::default();let soup_calls = Arc::clone(&service.calls);- let inbox_calls = Arc::new(Mutex::new(Vec::new()));- let inbox_reader = RecordingInboxReader {- calls: Arc::clone(&inbox_calls),- responses: Arc::new(HashMap::from([(user_id.clone(), vec![inbox_id])])),+ let thread_calls = Arc::new(Mutex::new(Vec::new()));+ let thread_reader = RecordingThreadReader {+ calls: Arc::clone(&thread_calls),+ responses: Arc::new(HashMap::from([(+ user_id.clone(),+ vec![email_thread(first_id), email_thread(second_id)],+ )])),};- let loader = SoupItemLoader::new(service, inbox_reader);+ let loader = SoupItemLoader::new(service, thread_reader);let keys = vec![(user_id.clone(),@@ -343,15 +342,79 @@ async fn resolves_inboxes_once_for_a_users_email_entities() {),];- Loader::<SoupItemLoaderKey>::load(&loader, &keys)+ let loaded = Loader::<SoupItemLoaderKey>::load(&loader, &keys).await.expect("batch loads");- assert_eq!(inbox_calls.lock().expect("inbox calls lock").len(), 1);+ assert_eq!(loaded.len(), 2);+ assert!(matches!(+ &loaded[&keys[0]].item,+ SoupItem::EmailThread(thread) if thread.thread.id == first_id+ ));+ assert!(matches!(+ &loaded[&keys[1]].item,+ SoupItem::EmailThread(thread) if thread.thread.id == second_id+ ));assert_eq!(- soup_calls.lock().expect("Soup calls lock")[0].link_ids,- vec![inbox_id]+ *thread_calls.lock().expect("thread calls lock"),+ vec![(user_id, vec![first_id, second_id])]);+ assert!(soup_calls.lock().expect("Soup calls lock").is_empty());+}++#[tokio::test]+async fn email_threads_and_other_entities_hydrate_side_by_side() {+ let user_id = user("macro|[email protected]");+ let document_id = Uuid::from_u128(14);+ let thread_id = Uuid::from_u128(15);+ let service =+ RecordingSoupService::default().with_response(user_id.clone(), vec![document(document_id)]);+ let soup_calls = Arc::clone(&service.calls);+ let thread_reader = RecordingThreadReader {+ calls: Arc::default(),+ responses: Arc::new(HashMap::from([(+ user_id.clone(),+ vec![email_thread(thread_id)],+ )])),+ };+ let loader = SoupItemLoader::new(service, thread_reader);+ let keys = vec![+ (+ user_id.clone(),+ EntityType::Document.with_entity_string(document_id.to_string()),+ ),+ (+ user_id.clone(),+ EntityType::EmailThread.with_entity_string(thread_id.to_string()),+ ),+ ];++ let loaded = Loader::<SoupItemLoaderKey>::load(&loader, &keys)+ .await+ .expect("batch loads");++ assert_eq!(loaded.len(), 2);+ let soup_calls = soup_calls.lock().expect("Soup calls lock");+ assert_eq!(soup_calls.len(), 1);+ assert_eq!(soup_calls[0].limit, 1);+ assert!(soup_calls[0].link_ids.is_empty());+}++#[tokio::test]+async fn a_thread_the_reader_withholds_is_absent() {+ let user_id = user("macro|[email protected]");+ let entity = EntityType::EmailThread.with_entity_string(Uuid::from_u128(16).to_string());+ let loader = SoupItemDataLoader::new(SoupItemLoader::new(+ RecordingSoupService::default(),+ RecordingThreadReader::default(),+ ));++ let item = loader+ .load_one((user_id, entity))+ .await+ .expect("an inaccessible thread is not a loader failure");++ assert!(item.is_none());}#[tokio::test]@@ -365,7 +428,7 @@ async fn context_loader_does_not_cache_across_realtime_updates() {let responses = Arc::clone(&service.responses);let loader = SoupItemDataLoader::new(SoupItemLoader::new(service,- RecordingInboxReader::default(),+ RecordingThreadReader::default(),));let first = loader@@ -394,7 +457,7 @@ async fn context_loader_returns_none_for_a_missing_item() {let entity = EntityType::Document.with_entity_string(Uuid::from_u128(21).to_string());let loader = SoupItemDataLoader::new(SoupItemLoader::new(RecordingSoupService::default(),- RecordingInboxReader::default(),+ RecordingThreadReader::default(),));let item = loader@@ -406,17 +469,13 @@ async fn context_loader_returns_none_for_a_missing_item() {}#[test]-fn email_entity_filter_includes_shared_threads() {+fn email_threads_are_not_encoded_into_the_soup_request() {let thread_id = Uuid::from_u128(20);let entities = vec![EntityType::EmailThread.with_entity_string(thread_id.to_string())];- let ast = entity_filter_ast(&entities).expect("valid AST");- let email_filter = ast.email_filter.tree.as_deref().expect("email filter");+ let error = entity_filter_ast(&entities).expect_err("email threads bypass Soup");- assert!(includes_shared_threads(- email_filter,- &HashSet::from([thread_id])- ));+ assert!(error.to_string().contains("thread reader"));}#[test]@@ -477,9 +536,9 @@ fn encodes_requested_ids_and_disables_unrequested_entity_branches() {),HashSet::from([nil]));- assert!(includes_shared_threads(- ast.email_filter.tree.as_deref().expect("email filter"),- &HashSet::from([nil])+ assert!(matches!(+ ast.email_filter.tree.as_deref(),+ Some(Expr::Literal(EmailLiteral::ThreadId(id))) if id.is_nil()));assert_eq!(collect_ids(@@ -594,7 +653,7 @@ async fn pr_and_session_hydration_recheck_team_on_each_batch() {.unwrap(),))));let soup = RecordingSoupService::default();- let mut loader = SoupItemLoader::new(soup.clone(), RecordingInboxReader::default());+ let mut loader = SoupItemLoader::new(soup.clone(), RecordingThreadReader::default());loader.team_reader = Some(team.clone());let keys = vec![(@@ -635,7 +694,7 @@ async fn crm_record_hydration_carries_the_viewer_team() {.unwrap(),))));let soup = RecordingSoupService::default();- let mut loader = SoupItemLoader::new(soup.clone(), RecordingInboxReader::default());+ let mut loader = SoupItemLoader::new(soup.clone(), RecordingThreadReader::default());loader.team_reader = Some(team);for entity_type in [EntityType::CrmCompany, EntityType::CrmContact] {let keys = vec![(diff --git a/crates/graphql_soup/src/resolvers.rs b/crates/graphql_soup/src/resolvers.rsindex 4c02c0121b..0790298485 100644--- a/crates/graphql_soup/src/resolvers.rs+++ b/crates/graphql_soup/src/resolvers.rs@@ -101,10 +101,18 @@ where))}-/// Resolve one email thread through the existing user-scoped Soup item loader.+/// Resolve one email thread through the Soup item loader, whose email+/// hydration settles access with entity access receipts: the rule the REST+/// thread route applies, so a thread reached through a team's CRM, a project,+/// or a share opens even though it lives in a teammate's inbox.///-/// A missing or inaccessible thread is represented as `None` by the loader's-/// filtered Soup request.+/// A missing or inaccessible thread is represented as `None`.+#[tracing::instrument(+ name = "soup.resolve_email_thread",+ skip_all,+ fields(thread_id = %thread_id),+ err(Debug)+)]pub async fn resolve_soup_email_thread<Edges>(ctx: &Context<'_>,user_id: MacroUserIdStr<'static>,diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rsindex 41d520ed6a..b7d22c4864 100644--- a/services/document_storage_service/src/api/context.rs+++ b/services/document_storage_service/src/api/context.rs@@ -857,6 +857,7 @@ pub(crate) struct ApiContext {pub graphql_initiative_entity_loader: graphql_initiative::InitiativeEntityLoader,pub graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader,pub graphql_soup_update_hydration_loader: graphql_soup::SoupUpdateHydrationLoader,+ pub graphql_soup_item_loader: graphql_soup::SoupItemDataLoader,pub databases_state: DssDatabasesState,pub database_webhooks_state: DssDatabaseWebhooksState,pub forms_state: DssFormsState,diff --git a/services/document_storage_service/src/api/graphql_soup.rs b/services/document_storage_service/src/api/graphql_soup.rsindex 5afa99d1c7..08380f4830 100644--- a/services/document_storage_service/src/api/graphql_soup.rs+++ b/services/document_storage_service/src/api/graphql_soup.rs@@ -15,7 +15,7 @@ use bots::outbound::pg_bots_repo::PgBotsRepo;use complete_graph::GraphqlRequestParts;use entity_registry::OwnerGrantPolicy;use entity_registry_db_utils::OwnedEntityRegistrar;-use graphql_soup::{email_mutation_thread_loader, soup_item_loader_with_team_access};+use graphql_soup::email_mutation_thread_loader;use macro_authorization::{OptionalMacroAuthorizationExtractor, UserOrInternalService, UserOrInternalServiceAuthorization,};@@ -143,13 +143,6 @@ fn insert_graphql_context_data(state.soup_router_state.email_service(),state.entity_access_service.clone(),);- // Ordinary queries use the replica-backed Soup reader. Subscription- // hydration and the mutation-only loader below read the primary.- let soup_item_loader = soup_item_loader_with_team_access(- state.soup_router_state.service(),- state.soup_router_state.email_service(),- state.entity_access_service.as_ref().clone(),- );data.insert(macro_user_id.clone());data.insert(entity_mutation::EntityMutationActor {user_id: macro_user_id.clone(),@@ -189,7 +182,7 @@ fn insert_graphql_context_data(data.insert(state.graphql_notification_reader.clone());data.insert(state.soup_router_state.email_service());data.insert(state.entity_access_service.clone());- data.insert(soup_item_loader);+ data.insert(state.graphql_soup_item_loader.clone());data.insert(state.agent_session_log_subscriptions.clone());// Mutation replies must read their committed state from the primary email// service. Ordinary Soup lists and subscriptions retain their own reader.diff --git a/services/document_storage_service/src/server.rs b/services/document_storage_service/src/server.rsindex c6bb54fadc..4c07ebb327 100644--- a/services/document_storage_service/src/server.rs+++ b/services/document_storage_service/src/server.rs@@ -1594,7 +1594,7 @@ pub async fn run() -> anyhow::Result<()> {SoupImpl::new(PgSoupRepo::new(readonly_pool::ReadOnlyPool(readonly_db.clone())),frecency_service,- readonly_email_service,+ readonly_email_service.clone(),ChannelListServiceImpl::new(PgChannelsRepo::new(readonly_db.clone()),PgChannelsRepo::new(readonly_db.clone()),@@ -2094,12 +2094,14 @@ pub async fn run() -> anyhow::Result<()> {graphql_soup::soup_item_loader(primary_entity_soup.clone(),Arc::new(email_service.clone()),+ entity_access_service.as_ref().clone(),),),graphql_agent_session_entity_loader: graphql_soup::AgentSessionEntityLoader(graphql_soup::soup_item_loader(primary_entity_soup.clone(),Arc::new(email_service.clone()),+ entity_access_service.as_ref().clone(),),),graphql_soup_update_hydration_loader: graphql_soup::SoupUpdateHydrationLoader(@@ -2109,6 +2111,14 @@ pub async fn run() -> anyhow::Result<()> {entity_access_service.as_ref().clone(),),),+ // Ordinary single-entity GraphQL reads use the replica-backed Soup+ // and email readers. Subscription hydration and mutation replies+ // above read the primary.+ graphql_soup_item_loader: graphql_soup::soup_item_loader_with_team_access(+ soup_service.clone(),+ Arc::new(readonly_email_service),+ entity_access_service.as_ref().clone(),+ ),graphql_crm_context: graphql_crm::CrmGraphqlContext::new(pipeline_service.clone()),graphql_initiative_context: graphql_initiative::InitiativeGraphqlContext::new(initiative_service.clone(),