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.

feat(soup/gql): connect email thread edge (#4734)

teo886e1483e2bb
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 4a899195b5..53c9a79566 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -943,6 +943,7 @@
"crates/foreign_entity",
"crates/frecency",
"crates/graphql_common",
+ "crates/graphql_email",
"crates/graphql_notification",
"crates/graphql_properties",
"crates/graphql_soup",
@@ -1560,6 +1561,7 @@
"crates/fusionauth",
"crates/github",
"crates/graphql_common",
+ "crates/graphql_email",
"crates/graphql_notification",
"crates/graphql_properties",
"crates/graphql_soup",
@@ -2645,6 +2647,61 @@
"crates/non_empty",
"crates/workspace-hack"
],
+ "graphql_email": [
+ "crates/agent",
+ "crates/ai_toolset",
+ "crates/ai_usage",
+ "crates/attachment",
+ "crates/authentication_service_client",
+ "crates/bot_id",
+ "crates/channel_sender",
+ "crates/cowlike",
+ "crates/crm",
+ "crates/decode_jwt",
+ "crates/document_sub_type",
+ "crates/email",
+ "crates/email_db_client",
+ "crates/email_utils",
+ "crates/entity_access",
+ "crates/entity_access_db_utils",
+ "crates/entity_access_management",
+ "crates/filter_ast",
+ "crates/frecency",
+ "crates/graphql_email",
+ "crates/invite_email",
+ "crates/item_filters",
+ "crates/last_online_tracker",
+ "crates/macro_auth",
+ "crates/macro_env",
+ "crates/macro_env_var",
+ "crates/macro_event_broker",
+ "crates/macro_event_topics",
+ "crates/macro_service_urls",
+ "crates/macro_user_id",
+ "crates/macro_uuid",
+ "crates/mention_utils",
+ "crates/model",
+ "crates/model-entity",
+ "crates/model-error-response",
+ "crates/model_file_type",
+ "crates/model_notifications",
+ "crates/model_user",
+ "crates/models_bulk_upload",
+ "crates/models_email",
+ "crates/models_entity_access_management",
+ "crates/models_pagination",
+ "crates/models_permissions",
+ "crates/models_properties",
+ "crates/non_empty",
+ "crates/notification",
+ "crates/prompt",
+ "crates/properties",
+ "crates/rate_limit",
+ "crates/remote_env_var",
+ "crates/system_properties",
+ "crates/unfurl",
+ "crates/workspace-hack"
+ ],
"graphql_notification": [
"crates/ai_toolset",
"crates/cowlike",
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 1bf29ccf8d..e717a8c3af 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -10,7 +10,7 @@ Open an issue before putting up a PR. This applies to both features and fixes. I
## AI-assisted contributions
-Useful contributions require human effort. You may use whatever tools best
+Useful contributions require human effort. You may use whatever tools best
serve you including AI tools, but if you don't understand the work you're
doing it's probably not useful.
@@ -38,7 +38,7 @@ Common types: `feat`, `fix`, `chore`
## PR bodies
Keep PR bodies concise and write them yourself. A few sentences covering what
-changed and why, a link to the issue, and anything a reviewer needs to know.
+changed and why, a link to the issue, and anything a reviewer needs to know.
No generated boilerplate, no exhaustive file-by-file change lists.
## Development setup
diff --git a/Cargo.lock b/Cargo.lock
index a44c9f8a2c..7c875dafde 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2854,6 +2854,7 @@ dependencies = [
"email",
"entity_access",
"graphql_common",
+ "graphql_email",
"graphql_notification",
"graphql_properties",
"graphql_soup",
@@ -5075,6 +5076,7 @@ dependencies = [
"bot_id",
"cached",
"cool_asserts",
+ "futures",
"macro_db_migrator",
"macro_user_id",
"mockall",
@@ -5956,6 +5958,20 @@ dependencies = [
"workspace-hack",
]
+[[package]]
+name = "graphql_email"
+version = "0.1.0"
+dependencies = [
+ "async-graphql",
+ "chrono",
+ "email",
+ "macro_user_id",
+ "tokio",
+ "tracing",
+ "uuid",
+ "workspace-hack",
+]
+
[[package]]
name = "graphql_notification"
version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
index 88cc3088f0..9aea23bb98 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,7 @@ members = [
"crates/favorites",
"crates/foreign_entity",
"crates/graphql_common",
+ "crates/graphql_email",
"crates/graphql_notification",
"crates/graphql_properties",
"crates/graphql_soup",
diff --git a/crates/complete_graph/AGENTS.md b/crates/complete_graph/AGENTS.md
index 0eded59814..96f29f12b2 100644
--- a/crates/complete_graph/AGENTS.md
+++ b/crates/complete_graph/AGENTS.md
@@ -1,7 +1,7 @@
# complete_graph — schema conventions
This crate composes the domain GraphQL adapter crates (`graphql_soup`,
-`graphql_properties`, `graphql_notification`, with shared plumbing in
+`graphql_properties`, `graphql_notification`, `graphql_email`, with shared plumbing in
`graphql_common`) into the complete GraphQL schema consumed by the client's
**normalized cache** (`crates/client`, design doc:
`apps/web/docs/graphql-normalized-cache-plan.md`). Two field-naming
diff --git a/crates/complete_graph/Cargo.toml b/crates/complete_graph/Cargo.toml
index e1e3fa27fc..08553d5a16 100644
--- a/crates/complete_graph/Cargo.toml
+++ b/crates/complete_graph/Cargo.toml
@@ -18,6 +18,7 @@ entity_access = { path = "../entity_access", default-features = false, features
"inbound",
] }
graphql_common = { path = "../graphql_common" }
+graphql_email = { path = "../graphql_email" }
graphql_notification = { path = "../graphql_notification" }
graphql_properties = { path = "../graphql_properties" }
graphql_soup = { path = "../graphql_soup" }
diff --git a/crates/complete_graph/src/edges.rs b/crates/complete_graph/src/edges.rs
index 66be072323..d169408f9a 100644
--- a/crates/complete_graph/src/edges.rs
+++ b/crates/complete_graph/src/edges.rs
@@ -1,22 +1,27 @@
use std::marker::PhantomData;
use async_graphql::{Context, Object};
+use graphql_email::{
+ EmailContentKey, GraphqlSoupEmailMessage, SoupEmailContentEdgeReader, load_latest_email_message,
+};
use graphql_notification::{
GraphqlSoupNotification, SoupNotificationEdgeReader, load_entity_notifications,
};
use graphql_properties::{EntityPropertyReader, GraphqlProperty, load_entity_properties};
use graphql_soup::SoupEntityEdges;
-/// Notification and property fields attached to property-bearing Soup entities.
+type EdgeReaders<NR, PR, ER> = PhantomData<fn() -> (NR, PR, ER)>;
+
+/// Notification, property, and email-content fields attached to Soup entities.
///
/// This concrete edge shape lives in the composition crate so `graphql_soup`
/// does not know which cross-domain fields are attached to its objects.
-pub struct SoupEdges<NR, PR> {
+pub struct SoupEdges<NR, PR, ER> {
entity: model_entity::Entity<'static>,
- _readers: PhantomData<fn() -> (NR, PR)>,
+ _readers: EdgeReaders<NR, PR, ER>,
}
-impl<NR, PR> Clone for SoupEdges<NR, PR> {
+impl<NR, PR, ER> Clone for SoupEdges<NR, PR, ER> {
fn clone(&self) -> Self {
Self {
entity: self.entity.clone(),
@@ -25,24 +30,35 @@ impl<NR, PR> Clone for SoupEdges<NR, PR> {
}
}
-impl<NR, PR> SoupEntityEdges for SoupEdges<NR, PR>
+impl<NR, PR, ER> SoupEntityEdges for SoupEdges<NR, PR, ER>
where
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
+ type EmailThreadEdges = SoupEmailThreadEdges<ER>;
+
fn from_entity(entity: model_entity::Entity<'static>) -> Self {
Self {
entity,
_readers: PhantomData,
}
}
+
+ fn email_thread_edges(&self) -> Self::EmailThreadEdges {
+ SoupEmailThreadEdges {
+ thread_id: self.entity.entity_id.to_string(),
+ _reader: PhantomData,
+ }
+ }
}
#[Object(name = "SoupEdges")]
-impl<NR, PR> SoupEdges<NR, PR>
+impl<NR, PR, ER> SoupEdges<NR, PR, ER>
where
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
async fn properties(&self, ctx: &Context<'_>) -> async_graphql::Result<Vec<GraphqlProperty>> {
load_entity_properties::<PR>(ctx, self.entity.clone()).await
@@ -55,3 +71,37 @@ where
load_entity_notifications::<NR>(ctx, self.entity.clone()).await
}
}
+
+/// Email-content fields attached only to Soup email-thread entities.
+pub struct SoupEmailThreadEdges<ER> {
+ thread_id: String,
+ _reader: PhantomData<fn() -> ER>,
+}
+
+impl<ER> Clone for SoupEmailThreadEdges<ER> {
+ fn clone(&self) -> Self {
+ Self {
+ thread_id: self.thread_id.clone(),
+ _reader: PhantomData,
+ }
+ }
+}
+
+#[Object]
+impl<ER> SoupEmailThreadEdges<ER>
+where
+ ER: SoupEmailContentEdgeReader,
+{
+ async fn latest_content_message(
+ &self,
+ ctx: &Context<'_>,
+ ) -> async_graphql::Result<Option<GraphqlSoupEmailMessage>> {
+ load_latest_email_message::<ER>(
+ ctx,
+ EmailContentKey {
+ thread_id: self.thread_id.clone(),
+ },
+ )
+ .await
+ }
+}
diff --git a/crates/complete_graph/src/lib.rs b/crates/complete_graph/src/lib.rs
index c5ab2a2f80..becf18a9b2 100644
--- a/crates/complete_graph/src/lib.rs
+++ b/crates/complete_graph/src/lib.rs
@@ -1,5 +1,5 @@
//! Composition of the domain GraphQL adapter crates (`graphql_soup`,
-//! `graphql_properties`, `graphql_notification`) into the complete schema
+//! `graphql_properties`, `graphql_notification`, `graphql_email`) into the complete schema
//! served by `document_storage_service` and exported as SDL.
#![deny(missing_docs)]
@@ -8,8 +8,12 @@ mod schema;
#[cfg(test)]
mod sdl_test;
-pub use edges::SoupEdges;
+pub use edges::{SoupEdges, SoupEmailThreadEdges};
pub use graphql_common::GraphqlSoupRequestParts;
+pub use graphql_email::{
+ EmailContentKey, EmailContentLoad, EmailContentLoader, NoOpSoupEmailContentEdgeReader,
+ SoupEmailContentEdgeReader, email_content_loader,
+};
pub use graphql_notification::{
EntityNotificationsLoader, SoupNotificationEdgeReader, entity_notifications_loader,
};
diff --git a/crates/complete_graph/src/schema.rs b/crates/complete_graph/src/schema.rs
index a307b68cc2..382d8fb607 100644
--- a/crates/complete_graph/src/schema.rs
+++ b/crates/complete_graph/src/schema.rs
@@ -12,6 +12,7 @@ use email::{
};
use entity_access::domain::ports::{EntityAccessService, NoOpEntityAccessService};
use graphql_common::extract_part;
+use graphql_email::{NoOpSoupEmailContentEdgeReader, SoupEmailContentEdgeReader};
use graphql_notification::{NoOpSoupNotificationEdgeReader, SoupNotificationEdgeReader};
use graphql_properties::{
EntityPropertyReader, EntityPropertyWriter, NoOpEntityPropertyReader, NoOpEntityPropertyWriter,
@@ -27,13 +28,14 @@ use crate::SoupEdges;
///
/// `S` is the soup service, `E` the email service, `EAS` the entity access
/// service, `St` the embedding axum router state, `W` the property mutation
-/// writer, `NR` the notification edge reader, and `PR` the property edge
-/// reader.
-pub type SoupSchema<S, E, EAS, St, W, NR, PR> =
- Schema<SoupQueryRoot<S, E, EAS, St, NR, PR>, PropertiesMutationRoot<W>, EmptySubscription>;
+/// writer, `NR` the notification edge reader, `PR` the property edge reader,
+/// and `ER` the email-content edge reader.
+pub type SoupSchema<S, E, EAS, St, W, NR, PR, ER> =
+ Schema<SoupQueryRoot<S, E, EAS, St, NR, PR, ER>, PropertiesMutationRoot<W>, EmptySubscription>;
/// GraphQL Soup schema type backed by a shared soup service.
-pub type SharedSoupSchema<S, E, EAS, St, W, NR, PR> = SoupSchema<Arc<S>, E, EAS, St, W, NR, PR>;
+pub type SharedSoupSchema<S, E, EAS, St, W, NR, PR, ER> =
+ SoupSchema<Arc<S>, E, EAS, St, W, NR, PR, ER>;
/// GraphQL Soup schema type backed by the no-op services, used only for
/// SDL export or introspection.
@@ -45,6 +47,7 @@ pub type SchemaOnlySoupSchema = SoupSchema<
NoOpEntityPropertyWriter,
NoOpSoupNotificationEdgeReader,
NoOpEntityPropertyReader,
+ NoOpSoupEmailContentEdgeReader,
>;
/// Axum-style state used only to construct the GraphQL schema for SDL export.
@@ -65,15 +68,15 @@ impl FromRef<SchemaOnlyState> for Arc<NoOpEntityAccessService> {
/// Zero-sized marker tying the query objects to the email/entity-access/state
/// generics without requiring values of those types.
-type ServicesMarker<E, EAS, St, NR, PR> = PhantomData<fn() -> (E, EAS, St, NR, PR)>;
+type ServicesMarker<E, EAS, St, NR, PR, ER> = PhantomData<fn() -> (E, EAS, St, NR, PR, ER)>;
/// Root GraphQL query object for Soup.
-pub struct SoupQueryRoot<S, E, EAS, St, NR, PR> {
+pub struct SoupQueryRoot<S, E, EAS, St, NR, PR, ER> {
service: S,
- _marker: ServicesMarker<E, EAS, St, NR, PR>,
+ _marker: ServicesMarker<E, EAS, St, NR, PR, ER>,
}
-impl<S, E, EAS, St, NR, PR> SoupQueryRoot<S, E, EAS, St, NR, PR> {
+impl<S, E, EAS, St, NR, PR, ER> SoupQueryRoot<S, E, EAS, St, NR, PR, ER> {
/// Create a root GraphQL query object.
pub fn new(service: S) -> Self {
Self {
@@ -86,9 +89,9 @@ impl<S, E, EAS, St, NR, PR> SoupQueryRoot<S, E, EAS, St, NR, PR> {
/// The authenticated user (viewer). All user-scoped data hangs off this
/// object so clients (and their normalized caches) observe data ownership
/// structurally rather than implicitly through the session.
-pub struct GraphqlUser<S, E, EAS, St, NR, PR> {
+pub struct GraphqlUser<S, E, EAS, St, NR, PR, ER> {
service: S,
- _marker: ServicesMarker<E, EAS, St, NR, PR>,
+ _marker: ServicesMarker<E, EAS, St, NR, PR, ER>,
}
/// Build a GraphQL schema for Soup suitable for SDL export or introspection.
@@ -97,9 +100,9 @@ pub fn build_schema() -> SchemaOnlySoupSchema {
}
/// Build a GraphQL schema for Soup backed by the provided service.
-pub fn build_schema_with_service<S, E, EAS, St, W, NR, PR>(
+pub fn build_schema_with_service<S, E, EAS, St, W, NR, PR, ER>(
service: S,
-) -> SoupSchema<S, E, EAS, St, W, NR, PR>
+) -> SoupSchema<S, E, EAS, St, W, NR, PR, ER>
where
S: SoupService + Clone,
E: EmailService,
@@ -110,6 +113,7 @@ where
W: EntityPropertyWriter,
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
Schema::build(
SoupQueryRoot::new(service),
@@ -120,9 +124,9 @@ where
}
/// Build a GraphQL schema for Soup backed by an `Arc`-shared service.
-pub fn build_schema_from_arc<S, E, EAS, St, W, NR, PR>(
+pub fn build_schema_from_arc<S, E, EAS, St, W, NR, PR, ER>(
service: Arc<S>,
-) -> SharedSoupSchema<S, E, EAS, St, W, NR, PR>
+) -> SharedSoupSchema<S, E, EAS, St, W, NR, PR, ER>
where
S: SoupService,
E: EmailService,
@@ -133,12 +137,13 @@ where
W: EntityPropertyWriter,
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
build_schema_with_service(service)
}
#[Object]
-impl<S, E, EAS, St, NR, PR> SoupQueryRoot<S, E, EAS, St, NR, PR>
+impl<S, E, EAS, St, NR, PR, ER> SoupQueryRoot<S, E, EAS, St, NR, PR, ER>
where
S: SoupService + Clone,
E: EmailService,
@@ -148,9 +153,10 @@ where
Arc<EAS>: FromRef<St>,
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
/// The authenticated user.
- async fn user(&self) -> GraphqlUser<S, E, EAS, St, NR, PR> {
+ async fn user(&self) -> GraphqlUser<S, E, EAS, St, NR, PR, ER> {
GraphqlUser {
service: self.service.clone(),
_marker: PhantomData,
@@ -159,7 +165,7 @@ where
}
#[Object(name = "GraphqlUser")]
-impl<S, E, EAS, St, NR, PR> GraphqlUser<S, E, EAS, St, NR, PR>
+impl<S, E, EAS, St, NR, PR, ER> GraphqlUser<S, E, EAS, St, NR, PR, ER>
where
S: SoupService,
E: EmailService,
@@ -169,6 +175,7 @@ where
Arc<EAS>: FromRef<St>,
NR: SoupNotificationEdgeReader,
PR: EntityPropertyReader,
+ ER: SoupEmailContentEdgeReader,
{
/// Stable id of the authenticated user.
async fn id(&self, ctx: &Context<'_>) -> async_graphql::Result<async_graphql::ID> {
@@ -182,7 +189,7 @@ where
&self,
ctx: &Context<'_>,
input: SoupInput,
- ) -> async_graphql::Result<SoupPage<SoupEdges<NR, PR>>> {
- resolve_soup::<S, E, EAS, St, SoupEdges<NR, PR>>(&self.service, ctx, input).await
+ ) -> async_graphql::Result<SoupPage<SoupEdges<NR, PR, ER>>> {
+ resolve_soup::<S, E, EAS, St, SoupEdges<NR, PR, ER>>(&self.service, ctx, input).await
}
}
diff --git a/crates/complete_graph/src/schema/test.rs b/crates/complete_graph/src/schema/test.rs
index 3c26460f84..b34f644318 100644
--- a/crates/complete_graph/src/schema/test.rs
+++ b/crates/complete_graph/src/schema/test.rs
@@ -370,6 +370,7 @@ struct TestHarness {
NoOpEntityPropertyWriter,
NoOpSoupNotificationEdgeReader,
NoOpEntityPropertyReader,
+ NoOpSoupEmailContentEdgeReader,
>,
state: TestState,
inbox_calls: Arc<AtomicUsize>,
diff --git a/crates/complete_graph/src/sdl_test.rs b/crates/complete_graph/src/sdl_test.rs
index f7e1953006..0ac69b5a4a 100644
--- a/crates/complete_graph/src/sdl_test.rs
+++ b/crates/complete_graph/src/sdl_test.rs
@@ -19,6 +19,11 @@ fn soup_response_schema_exposes_frontend_fields() {
"attachments: [GraphqlSoupEmailAttachment!]!",
"labels: [GraphqlSoupEmailLabel!]!",
"properties: [GraphqlProperty!]!",
+ "latestContentMessage: GraphqlSoupEmailMessage",
+ "type GraphqlSoupEmailMessage {",
+ "bodyParsed: String",
+ "bodyHtmlSanitized: String",
+ "bodyReplyless: String",
"type GraphqlSoupCall {",
"channelName: String",
"customName: String",
diff --git a/crates/email/src/domain/models/parsed_message.rs b/crates/email/src/domain/models/parsed_message.rs
index beaff4c5fe..c6ce582d67 100644
--- a/crates/email/src/domain/models/parsed_message.rs
+++ b/crates/email/src/domain/models/parsed_message.rs
@@ -23,6 +23,8 @@ pub struct ParsedMessage {
pub thread_db_id: Uuid,
/// Message subject.
pub subject: Option<String>,
+ /// Message snippet.
+ pub snippet: Option<String>,
/// Sender contact info.
pub from: Option<ContactInfo>,
/// To recipients.
@@ -35,8 +37,32 @@ pub struct ParsedMessage {
pub labels: Vec<ParsedLabel>,
/// The body parsed into plaintext (from body_replyless, with HTML converted if needed).
pub body_parsed: Option<String>,
+ /// Plain text body.
+ pub body_text: Option<String>,
+ /// Sanitized HTML body.
+ pub body_html_sanitized: Option<String>,
+ /// Macro-format body.
+ pub body_macro: Option<String>,
+ /// Body with reply/forwarded content stripped.
+ pub body_replyless: Option<String>,
/// Internal date timestamp from the provider.
pub internal_date_ts: Option<DateTime<Utc>>,
+ /// When the message was sent.
+ pub sent_at: Option<DateTime<Utc>>,
+ /// Whether the message has been read.
+ pub is_read: bool,
+ /// Whether the message is starred.
+ pub is_starred: bool,
+ /// Whether the message was sent by the user.
+ pub is_sent: bool,
+ /// Whether the message is a draft.
+ pub is_draft: bool,
+ /// Whether the message has attachments.
+ pub has_attachments: bool,
+ /// When the message was created.
+ pub created_at: DateTime<Utc>,
+ /// When the message was last updated.
+ pub updated_at: DateTime<Utc>,
}
/// A thread with lightweight parsed messages.
diff --git a/crates/email/src/domain/ports.rs b/crates/email/src/domain/ports.rs
index 5c3ac80ea9..bd3bc18502 100644
--- a/crates/email/src/domain/ports.rs
+++ b/crates/email/src/domain/ports.rs
@@ -2,9 +2,9 @@ use crate::domain::models::{
Attachment, AttachmentDraft, AttachmentForwarded, Contact, ContactInfo, CreateDraftInput,
CreatedDraft, EmailErr, EmailFilter, EmailThreadPreview, EnrichedEmailThreadPreview,
GetEmailsRequest, Label, Link, LinkLabel, MessageAttachment, MessageLabel, MessageRow,
- ParsedAddresses, ParsedThread, PreviewCursorQuery, RecipientType, ResolvedDraftInput,
- SimpleMessage, SimpleMessageInfo, Thread, ThreadRow, UpdateThreadLabelsResult,
- UpsertEmailFilterInput, UpsertedContacts, UserProvider,
+ ParsedAddresses, ParsedMessage, ParsedThread, PreviewCursorQuery, RecipientType,
+ ResolvedDraftInput, SimpleMessage, SimpleMessageInfo, Thread, ThreadRow,
+ UpdateThreadLabelsResult, UpsertEmailFilterInput, UpsertedContacts, UserProvider,
};
use chrono::{DateTime, Utc};
use entity_access::domain::models::{EditAccessLevel, EntityAccessReceipt, ViewAccessLevel};
@@ -113,6 +113,12 @@ pub trait EmailRepo: Send + Sync + 'static {
limit: i64,
) -> impl Future<Output = Result<Vec<MessageRow>, Self::Err>> + Send;
+ /// Fetch the newest non-draft content message for each requested thread.
+ fn latest_content_message_rows(
+ &self,
+ thread_ids: &[Uuid],
+ ) -> impl Future<Output = Result<Vec<MessageRow>, Self::Err>> + Send;
+
/// Find macro reply drafts (across the given inboxes) that reply to any of
/// `replying_to_ids` but live in a thread other than `exclude_thread_id` —
/// i.e. a reply moved to another inbox by switching the sender.
@@ -327,6 +333,15 @@ pub trait EmailPreviewServiceReadOnly: Send + Sync + 'static {
> + 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.
+ fn get_latest_messages_parsed(
+ &self,
+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+ ) -> impl Future<Output = Result<HashMap<Uuid, ParsedMessage>, EmailErr>> + Send;
+}
+
/// Newtype adapter that restricts a full `EmailService` to read-only preview access.
/// Wrapping is explicit so readonly wiring is intentional — a bare `EmailServiceImpl`
/// will *not* silently satisfy `EmailPreviewServiceReadOnly`.
@@ -658,6 +673,15 @@ impl EmailService for NoOpEmailService {
}
}
+impl EmailContentService for NoOpEmailService {
+ async fn get_latest_messages_parsed(
+ &self,
+ _receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+ ) -> Result<HashMap<Uuid, ParsedMessage>, EmailErr> {
+ Err(no_op_email_err())
+ }
+}
+
/// Outcome of a first-inbox provisioning attempt.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FirstInboxProvisionOutcome {
diff --git a/crates/email/src/domain/service/mod.rs b/crates/email/src/domain/service/mod.rs
index 3bbae220dd..d2329d2217 100644
--- a/crates/email/src/domain/service/mod.rs
+++ b/crates/email/src/domain/service/mod.rs
@@ -9,10 +9,10 @@ use crate::domain::{
events::{EmailMacroEvent, ThreadProjectChangedMetadata},
models::{
CreateDraftInput, CreatedDraft, EmailErr, EmailFilter, EnrichedEmailThreadPreview,
- GetEmailsRequest, Link, LinkLabel, ParsedThread, Thread, UpdateThreadLabelsResult,
- UpsertEmailFilterInput,
+ GetEmailsRequest, Link, LinkLabel, ParsedMessage, ParsedThread, Thread,
+ UpdateThreadLabelsResult, UpsertEmailFilterInput,
},
- ports::{EmailMessageEnqueuer, EmailRepo, EmailService},
+ ports::{EmailContentService, EmailMessageEnqueuer, EmailRepo, EmailService},
};
use crm::domain::service::CrmService;
use entity_access::domain::models::{
@@ -23,6 +23,7 @@ use frecency::domain::ports::FrecencyQueryService;
use macro_event_broker::{MacroEventBroker, NoopMacroEventBroker};
use model_entity::EntityType;
use models_pagination::{PaginatedCursor, SimpleSortMethod};
+use std::collections::HashMap;
use uuid::Uuid;
#[derive(Clone)]
@@ -406,3 +407,20 @@ where
.map_err(|e| EmailErr::RepoErr(e.into()))
}
}
+
+impl<T, U, E, CS, Eam> EmailContentService for EmailServiceImpl<T, U, E, CS, Eam>
+where
+ T: EmailRepo,
+ U: FrecencyQueryService,
+ E: EmailMessageEnqueuer,
+ CS: CrmService,
+ Eam: EntityAccessManagementService,
+ anyhow::Error: From<T::Err>,
+{
+ async fn get_latest_messages_parsed(
+ &self,
+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+ ) -> Result<HashMap<Uuid, ParsedMessage>, EmailErr> {
+ self.get_latest_messages_parsed_impl(receipts).await
+ }
+}
diff --git a/crates/email/src/domain/service/thread.rs b/crates/email/src/domain/service/thread.rs
index b42097f377..5279a18415 100644
--- a/crates/email/src/domain/service/thread.rs
+++ b/crates/email/src/domain/service/thread.rs
@@ -7,7 +7,7 @@ use crate::domain::{
ports::{EmailRepo, RecipientsByMessageId},
};
use entity_access::domain::models::{
- AccessLevel, EntityAccessReceipt, EntityPermission, ViewAccessLevel,
+ AccessLevel, EntityAccessReceipt, EntityPermission, EntityType, ViewAccessLevel,
};
use frecency::domain::ports::FrecencyQueryService;
use std::collections::HashMap;
@@ -259,42 +259,13 @@ where
.into_iter()
.filter(|row| is_owner || !row.is_draft)
.map(|row| {
- let sender = senders.remove(&row.db_id);
- let recipient_list = recipients.remove(&row.db_id).unwrap_or_default();
- let message_labels = labels.remove(&row.db_id).unwrap_or_default();
-
- let (to, cc, bcc) = split_recipients(recipient_list);
-
- let body_replyless = email_utils::body_replyless::compute_body_replyless(
- row.subject.as_deref(),
- row.body_html_sanitized.as_deref(),
- row.body_text.as_deref(),
- );
-
- let body_parsed = email_utils::body_parsed::compute_body_parsed(
- row.body_html_sanitized.is_some(),
- &body_replyless,
- );
-
- ParsedMessage {
- db_id: row.db_id,
- link_id: row.link_id,
- thread_db_id: row.thread_db_id,
- subject: row.subject,
- from: sender,
- to,
- cc,
- bcc,
- labels: message_labels
- .into_iter()
- .map(|l| ParsedLabel {
- provider_id: l.provider_label_id,
- name: l.name.unwrap_or_default(),
- })
- .collect(),
- body_parsed,
- internal_date_ts: row.internal_date_ts,
- }
+ let message_id = row.db_id;
+ parsed_message_from_row(
+ row,
+ senders.remove(&message_id),
+ recipients.remove(&message_id).unwrap_or_default(),
+ labels.remove(&message_id).unwrap_or_default(),
+ )
})
.collect();
@@ -318,4 +289,128 @@ where
labels,
}))
}
+
+ /// Fetch the newest non-draft content message for each authorized thread in one
+ /// repository batch, then assemble senders, recipients, and labels in bulk.
+ pub(crate) async fn get_latest_messages_parsed_impl(
+ &self,
+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+ ) -> Result<HashMap<Uuid, ParsedMessage>, EmailErr> {
+ let thread_ids = email_thread_ids_from_receipts(receipts)?;
+
+ if thread_ids.is_empty() {
+ return Ok(HashMap::new());
+ }
+
+ let rows = self
+ .email_repo
+ .latest_content_message_rows(&thread_ids)
+ .await
+ .map_err(anyhow::Error::from)?;
+ let message_ids = rows.iter().map(|row| row.db_id).collect::<Vec<_>>();
+ let (mut senders, mut recipients, mut labels) = tokio::try_join!(
+ async {
+ self.email_repo
+ .senders_by_message_ids(&message_ids)
+ .await
+ .map_err(anyhow::Error::from)
+ },
+ async {
+ self.email_repo
+ .recipients_by_message_ids(&message_ids)
+ .await
+ .map_err(anyhow::Error::from)
+ },
+ async {
+ self.email_repo
+ .labels_by_message_ids(&message_ids)
+ .await
+ .map_err(anyhow::Error::from)
+ },
+ )?;
+
+ Ok(rows
+ .into_iter()
+ .map(|row| {
+ let message_id = row.db_id;
+ let thread_id = row.thread_db_id;
+ let message = parsed_message_from_row(
+ row,
+ senders.remove(&message_id),
+ recipients.remove(&message_id).unwrap_or_default(),
+ labels.remove(&message_id).unwrap_or_default(),
+ );
+ (thread_id, message)
+ })
+ .collect())
+ }
+}
+
+fn email_thread_ids_from_receipts(
+ receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+) -> Result<Vec<Uuid>, EmailErr> {
+ receipts
+ .into_iter()
+ .map(|receipt| {
+ if receipt.entity().entity_type != EntityType::EmailThread {
+ return Err(EmailErr::Unauthorized);
+ }
+ Uuid::parse_str(&receipt.entity().entity_id)
+ .map_err(|err| EmailErr::RepoErr(anyhow::anyhow!("invalid thread id: {err}")))
+ })
+ .collect()
}
+
+fn parsed_message_from_row(
+ row: MessageRow,
+ sender: Option<ContactInfo>,
+ recipients: Vec<(ContactInfo, crate::domain::models::RecipientType)>,
+ labels: Vec<MessageLabel>,
+) -> ParsedMessage {
+ let (to, cc, bcc) = split_recipients(recipients);
+ let body_replyless = email_utils::body_replyless::compute_body_replyless(
+ row.subject.as_deref(),
+ row.body_html_sanitized.as_deref(),
+ row.body_text.as_deref(),
+ );
+ let body_parsed = email_utils::body_parsed::compute_body_parsed(
+ row.body_html_sanitized.is_some(),
+ &body_replyless,
+ );
+
+ ParsedMessage {
+ db_id: row.db_id,
+ link_id: row.link_id,
+ thread_db_id: row.thread_db_id,
+ subject: row.subject,
+ snippet: row.snippet,
+ from: sender,
+ to,
+ cc,
+ bcc,
+ labels: labels
+ .into_iter()
+ .map(|label| ParsedLabel {
+ provider_id: label.provider_label_id,
+ name: label.name.unwrap_or_default(),
+ })
+ .collect(),
+ body_parsed,
+ body_text: row.body_text,
+ body_html_sanitized: row.body_html_sanitized,
+ body_macro: row.body_macro,
+ body_replyless,
+ internal_date_ts: row.internal_date_ts,
+ sent_at: row.sent_at,
+ is_read: row.is_read,
+ is_starred: row.is_starred,
+ is_sent: row.is_sent,
+ is_draft: row.is_draft,
+ has_attachments: row.has_attachments,
+ created_at: row.created_at,
+ updated_at: row.updated_at,
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/email/src/domain/service/thread/test.rs b/crates/email/src/domain/service/thread/test.rs
new file mode 100644
index 0000000000..9e37f67dfc
--- /dev/null
+++ b/crates/email/src/domain/service/thread/test.rs
@@ -0,0 +1,44 @@
+use super::*;
+use macro_user_id::user_id::MacroUserIdStr;
+
+fn receipt(entity_id: &str, entity_type: EntityType) -> EntityAccessReceipt<ViewAccessLevel> {
+ EntityAccessReceipt::dangerously_assert_authenticated_user(
+ MacroUserIdStr::try_from_email("[email protected]").unwrap(),
+ entity_id,
+ entity_type,
+ )
+}
+
+#[test]
+fn content_receipts_must_be_for_email_threads() {
+ let error = email_thread_ids_from_receipts(vec![receipt(
+ &Uuid::new_v4().to_string(),
+ EntityType::Document,
+ )])
+ .unwrap_err();
+
+ assert!(matches!(error, EmailErr::Unauthorized));
+}
+
+#[test]
+fn content_receipts_must_contain_uuid_thread_ids() {
+ let error =
+ email_thread_ids_from_receipts(vec![receipt("not-a-uuid", EntityType::EmailThread)])
+ .unwrap_err();
+
+ assert!(matches!(error, EmailErr::RepoErr(_)));
+}
+
+#[test]
+fn content_receipts_are_translated_to_a_single_batch() {
+ let first = Uuid::new_v4();
+ let second = Uuid::new_v4();
+
+ let ids = email_thread_ids_from_receipts(vec![
+ receipt(&first.to_string(), EntityType::EmailThread),
+ receipt(&second.to_string(), EntityType::EmailThread),
+ ])
+ .unwrap();
+
+ assert_eq!(ids, vec![first, second]);
+}
diff --git a/crates/email/src/outbound/email_pg_repo/db_types.rs b/crates/email/src/outbound/email_pg_repo/db_types.rs
index a2cf3cd006..80663e42af 100644
--- a/crates/email/src/outbound/email_pg_repo/db_types.rs
+++ b/crates/email/src/outbound/email_pg_repo/db_types.rs
@@ -199,6 +199,7 @@ impl From<DbThreadRow> for ThreadRow {
}
/// DB row for a message record.
+#[derive(sqlx::FromRow)]
pub struct DbMessageRow {
pub id: uuid::Uuid,
pub provider_id: Option<String>,
diff --git a/crates/email/src/outbound/email_pg_repo/mod.rs b/crates/email/src/outbound/email_pg_repo/mod.rs
index f643d7cb73..3c83b6460e 100644
--- a/crates/email/src/outbound/email_pg_repo/mod.rs
+++ b/crates/email/src/outbound/email_pg_repo/mod.rs
@@ -135,6 +135,13 @@ impl EmailRepo for EmailPgRepo {
thread::messages_by_thread_id_paginated(&self.pool, thread_id, offset, limit).await
}
+ async fn latest_content_message_rows(
+ &self,
+ thread_ids: &[Uuid],
+ ) -> Result<Vec<MessageRow>, Self::Err> {
+ thread::latest_content_message_rows(&self.pool, thread_ids).await
+ }
+
async fn cross_inbox_reply_drafts(
&self,
replying_to_ids: &[Uuid],
diff --git a/crates/email/src/outbound/email_pg_repo/test/thread.rs b/crates/email/src/outbound/email_pg_repo/test/thread.rs
index c1e4114e00..164536bca2 100644
--- a/crates/email/src/outbound/email_pg_repo/test/thread.rs
+++ b/crates/email/src/outbound/email_pg_repo/test/thread.rs
@@ -196,6 +196,80 @@ async fn test_messages_by_thread_id_paginated_fields_populated(
Ok(())
}
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_thread"))
+)]
+async fn test_latest_content_messages_are_batched_and_exclude_drafts(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let thread_1 = Uuid::parse_str("11111111-1111-1111-1111-111111111111")?;
+ let thread_2 = Uuid::parse_str("22222222-2222-2222-2222-222222222222")?;
+ let draft_id = Uuid::parse_str("ffffffff-aaaa-0001-aaaa-ffffffffffff")?;
+ sqlx::query(
+ r#"
+ INSERT INTO email_messages
+ (id, thread_id, link_id, internal_date_ts, subject, is_draft, created_at, updated_at)
+ VALUES ($1, $2, 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
+ '2025-03-01 00:00:00+00', 'private draft', true, NOW(), NOW())
+ "#,
+ )
+ .bind(draft_id)
+ .bind(thread_1)
+ .execute(&pool)
+ .await?;
+
+ let repo = EmailPgRepo::new(pool);
+ let rows = repo
+ .latest_content_message_rows(&[thread_1, thread_2])
+ .await?;
+ assert_eq!(rows.len(), 2);
+ assert_eq!(
+ rows.iter()
+ .find(|row| row.thread_db_id == thread_1)
+ .and_then(|row| row.provider_id.as_deref()),
+ Some("msg-1-newest")
+ );
+
+ assert!(rows.iter().all(|row| row.db_id != draft_id));
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../../../fixtures", scripts("email_thread"))
+)]
+async fn test_latest_content_message_uses_id_as_timestamp_tiebreaker(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let thread_id = Uuid::parse_str("22222222-2222-2222-2222-222222222222")?;
+ let lower_id = Uuid::parse_str("22222222-aaaa-0002-aaaa-222222222222")?;
+ let higher_id = Uuid::parse_str("22222222-aaaa-0003-aaaa-222222222222")?;
+ for (id, subject) in [(lower_id, "lower"), (higher_id, "higher")] {
+ sqlx::query(
+ r#"
+ INSERT INTO email_messages
+ (id, thread_id, link_id, internal_date_ts, subject, created_at, updated_at)
+ VALUES ($1, $2, 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
+ '2025-03-01 00:00:00+00', $3, NOW(), NOW())
+ "#,
+ )
+ .bind(id)
+ .bind(thread_id)
+ .bind(subject)
+ .execute(&pool)
+ .await?;
+ }
+
+ let repo = EmailPgRepo::new(pool);
+ let rows = repo.latest_content_message_rows(&[thread_id]).await?;
+
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].db_id, higher_id);
+ Ok(())
+}
+
// ── insert_thread ─────────────────────────────────────────────────
#[sqlx::test(
diff --git a/crates/email/src/outbound/email_pg_repo/thread.rs b/crates/email/src/outbound/email_pg_repo/thread.rs
index 0720fe206f..2c3bbf10e9 100644
--- a/crates/email/src/outbound/email_pg_repo/thread.rs
+++ b/crates/email/src/outbound/email_pg_repo/thread.rs
@@ -60,6 +60,53 @@ pub(super) async fn messages_by_thread_id_paginated(
Ok(rows.into_iter().map(MessageRow::from).collect())
}
+/// Fetch the newest non-draft content message in each thread using an
+/// effective timestamp and deterministic ID tiebreaker.
+#[allow(
+ clippy::disallowed_methods,
+ reason = "runtime lateral query is covered by repository integration tests"
+)]
+pub(super) async fn latest_content_message_rows(
+ pool: &PgPool,
+ thread_ids: &[Uuid],
+) -> Result<Vec<MessageRow>, sqlx::Error> {
+ let rows = sqlx::query_as::<_, DbMessageRow>(
+ r#"
+ SELECT
+ message.id, message.provider_id, message.thread_id,
+ message.provider_thread_id, message.replying_to_id,
+ message.global_id, message.link_id, message.provider_history_id,
+ message.internal_date_ts, message.snippet, message.size_estimate,
+ message.subject, message.sent_at, message.has_attachments,
+ message.is_read, message.is_starred, message.is_sent,
+ message.is_draft, message.body_text, message.body_html_sanitized,
+ message.body_macro, message.headers_jsonb, message.created_at,
+ message.updated_at
+ FROM UNNEST($1::uuid[]) AS requested(thread_id)
+ CROSS JOIN LATERAL (
+ SELECT
+ id, provider_id, thread_id, provider_thread_id, replying_to_id,
+ global_id, link_id, provider_history_id, internal_date_ts,
+ snippet, size_estimate, subject, sent_at, has_attachments,
+ is_read, is_starred, is_sent, is_draft, body_text,
+ body_html_sanitized, body_macro, headers_jsonb, created_at,
+ updated_at
+ FROM email_messages
+ WHERE thread_id = requested.thread_id
+ AND is_draft = false
+ ORDER BY COALESCE(internal_date_ts, sent_at, created_at) DESC,
+ id DESC
+ LIMIT 1
+ ) AS message
+ "#,
+ )
+ .bind(thread_ids)
+ .fetch_all(pool)
+ .await?;
+
+ Ok(rows.into_iter().map(MessageRow::from).collect())
+}
+
/// Find macro drafts that reply to any of the given messages but live in a
/// different thread within one of the accessible inboxes. These are reply drafts
/// the user moved to another of their inboxes by switching the sender; surfacing
diff --git a/crates/entity_access/Cargo.toml b/crates/entity_access/Cargo.toml
index 6d1122e64a..3982910f95 100644
--- a/crates/entity_access/Cargo.toml
+++ b/crates/entity_access/Cargo.toml
@@ -20,6 +20,7 @@ schema = ["dep:utoipa"]
[dependencies]
# Core
anyhow = { workspace = true }
+futures = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
diff --git a/crates/entity_access/src/domain/ports.rs b/crates/entity_access/src/domain/ports.rs
index aebee8229d..c020dcb9e0 100644
--- a/crates/entity_access/src/domain/ports.rs
+++ b/crates/entity_access/src/domain/ports.rs
@@ -5,10 +5,10 @@
use super::models::EntityType;
use crate::domain::models::{
AccessError, AccessLevel, BotId, CallChannelInfo, ChannelRoleResult, CrmEntityAccess,
- EntityAccessReceipt, EntityPermission, RequiredPermission, UserTeamInfo,
+ EntityAccessReceipt, EntityPermission, RequiredPermission, UserTeamInfo, ViewAccessLevel,
};
use macro_user_id::{lowercased::Lowercase, user_id::MacroUserId, user_id::MacroUserIdStr};
-use std::future::Future;
+use std::{collections::HashMap, future::Future};
use uuid::Uuid;
/// Repository for accessing entity permissions from the database.
@@ -44,6 +44,13 @@ pub trait AccessRepository: Clone + Send + Sync + 'static {
user_id: Option<&MacroUserId<Lowercase<'_>>>,
) -> impl Future<Output = Result<Option<AccessLevel>, AccessError>> + Send;
+ /// Return the requested email threads owned by, or inbox-delegated to, a user.
+ fn get_owned_email_thread_ids(
+ &self,
+ thread_ids: &[Uuid],
+ user_id: &MacroUserId<Lowercase<'_>>,
+ ) -> impl Future<Output = Result<Vec<Uuid>, AccessError>> + Send;
+
/// Get the highest access level a user has for a call.
fn get_call_access(
&self,
@@ -192,6 +199,38 @@ pub trait EntityAccessService: Clone + Send + Sync + 'static {
entity_type: EntityType,
) -> impl Future<Output = Result<EntityAccessReceipt<T>, AccessError>> + Send;
+ /// Mint view receipts for a distinct batch of email thread IDs.
+ ///
+ /// Implementations may override this to share authorization work across
+ /// the batch. The default preserves correctness for test and alternate
+ /// adapters by delegating to the single-entity API.
+ fn generate_email_thread_view_access_receipts<'a>(
+ &'a self,
+ user_id: &'a MacroUserId<Lowercase<'_>>,
+ user_org_id: Option<i64>,
+ thread_ids: &'a [String],
+ ) -> impl Future<
+ Output = HashMap<String, Result<EntityAccessReceipt<ViewAccessLevel>, AccessError>>,
+ > + Send
+ + 'a {
+ async move {
+ let mut receipts = HashMap::with_capacity(thread_ids.len());
+ for thread_id in thread_ids {
+ receipts.insert(
+ thread_id.clone(),
+ self.generate_entity_access_receipt::<ViewAccessLevel>(
+ user_id,
+ user_org_id,
+ thread_id,
+ EntityType::EmailThread,
+ )
+ .await,
+ );
+ }
+ receipts
+ }
+ }
+
/// Generates an [`EntityAccessReceipt<T>`] for an authenticated bot.
///
/// Document, chat, project, email-thread, and call permissions are resolved
diff --git a/crates/entity_access/src/domain/service.rs b/crates/entity_access/src/domain/service.rs
index 35f3de91a6..a859cd5246 100644
--- a/crates/entity_access/src/domain/service.rs
+++ b/crates/entity_access/src/domain/service.rs
@@ -1,21 +1,23 @@
//! Entity access service implementation.
-use std::marker::PhantomData;
-use std::str::FromStr;
+use std::{collections::HashMap, marker::PhantomData, str::FromStr};
use crate::domain::{
models::{
AccessError, AccessLevel, BotId, CallChannelInfo, ChannelRoleResult, CrmEntityAccess,
Entity, EntityAccessAuth, EntityAccessReceipt, EntityPermission, EntityType,
- RequiredPermission, UserTeamInfo,
+ RequiredPermission, UserTeamInfo, ViewAccessLevel,
},
ports::{AccessRepository, EntityAccessService},
};
+use futures::{StreamExt, stream};
use macro_user_id::{
cowlike::CowLike, lowercased::Lowercase, user_id::MacroUserId, user_id::MacroUserIdStr,
};
use uuid::Uuid;
+const MAX_CONCURRENT_BATCH_ACCESS_CHECKS: usize = 8;
+
/// Implementation of the [`EntityAccessService`].
///
/// This service orchestrates access checks by:
@@ -163,6 +165,87 @@ where
})
}
+ async fn generate_email_thread_view_access_receipts(
+ &self,
+ user_id: &MacroUserId<Lowercase<'_>>,
+ user_org_id: Option<i64>,
+ thread_ids: &[String],
+ ) -> HashMap<String, Result<EntityAccessReceipt<ViewAccessLevel>, AccessError>> {
+ let mut receipts = HashMap::with_capacity(thread_ids.len());
+ let mut valid_ids = Vec::with_capacity(thread_ids.len());
+
+ for thread_id in thread_ids {
+ match Uuid::parse_str(thread_id) {
+ Ok(id) => valid_ids.push((thread_id.clone(), id)),
+ Err(_) => {
+ receipts.insert(
+ thread_id.clone(),
+ Err(AccessError::BadRequest("Invalid thread ID format")),
+ );
+ }
+ }
+ }
+
+ let owned_ids = match self
+ .repo
+ .get_owned_email_thread_ids(
+ &valid_ids.iter().map(|(_, id)| *id).collect::<Vec<_>>(),
+ user_id,
+ )
+ .await
+ {
+ Ok(ids) => ids.into_iter().collect::<std::collections::HashSet<_>>(),
+ Err(error) => {
+ tracing::error!(?error, "bulk email thread ownership check failed");
+ for (thread_id, _) in valid_ids {
+ receipts.insert(thread_id, Err(AccessError::Internal));
+ }
+ return receipts;
+ }
+ };
+
+ let mut remaining = Vec::new();
+ for (thread_id, id) in valid_ids {
+ if owned_ids.contains(&id) {
+ receipts.insert(
+ thread_id.clone(),
+ Ok(EntityAccessReceipt {
+ auth: EntityAccessAuth::Authenticated(MacroUserIdStr(
+ user_id.clone().into_owned(),
+ )),
+ entity: Entity {
+ entity_id: thread_id,
+ entity_type: EntityType::EmailThread,
+ },
+ entity_permission: EntityPermission::AccessLevel {
+ access_level: AccessLevel::Owner,
+ },
+ _marker: PhantomData,
+ }),
+ );
+ } else {
+ remaining.push(thread_id);
+ }
+ }
+
+ let fallback = stream::iter(remaining.into_iter().map(|thread_id| async move {
+ let result = self
+ .generate_entity_access_receipt::<ViewAccessLevel>(
+ user_id,
+ user_org_id,
+ &thread_id,
+ EntityType::EmailThread,
+ )
+ .await;
+ (thread_id, result)
+ }))
+ .buffer_unordered(MAX_CONCURRENT_BATCH_ACCESS_CHECKS)
+ .collect::<Vec<_>>()
+ .await;
+ receipts.extend(fallback);
+ receipts
+ }
+
#[tracing::instrument(err, skip(self))]
async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
&self,
diff --git a/crates/entity_access/src/domain/service/test.rs b/crates/entity_access/src/domain/service/test.rs
index 706b1f9516..dbab7baec5 100644
--- a/crates/entity_access/src/domain/service/test.rs
+++ b/crates/entity_access/src/domain/service/test.rs
@@ -22,6 +22,8 @@ struct MockRepo {
chat_access: Arc<Mutex<Option<AccessLevel>>>,
project_access: Arc<Mutex<Option<AccessLevel>>>,
thread_access: Arc<Mutex<Option<AccessLevel>>>,
+ thread_access_calls: Arc<AtomicUsize>,
+ owned_email_thread_ids: Arc<Mutex<Vec<Uuid>>>,
call_access: Arc<Mutex<Option<AccessLevel>>>,
bot_entity_access: Arc<Mutex<Option<AccessLevel>>>,
bot_entity_access_calls: Arc<AtomicUsize>,
@@ -48,6 +50,8 @@ impl MockRepo {
chat_access: Arc::new(Mutex::new(None)),
project_access: Arc::new(Mutex::new(None)),
thread_access: Arc::new(Mutex::new(None)),
+ thread_access_calls: Arc::new(AtomicUsize::new(0)),
+ owned_email_thread_ids: Arc::new(Mutex::new(Vec::new())),
call_access: Arc::new(Mutex::new(None)),
bot_entity_access: Arc::new(Mutex::new(None)),
bot_entity_access_calls: Arc::new(AtomicUsize::new(0)),
@@ -88,6 +92,11 @@ impl MockRepo {
self
}
+ fn with_owned_email_thread_ids(mut self, ids: Vec<Uuid>) -> Self {
+ self.owned_email_thread_ids = Arc::new(Mutex::new(ids));
+ self
+ }
+
#[allow(dead_code)]
fn with_call_access(mut self, level: AccessLevel) -> Self {
self.call_access = Arc::new(Mutex::new(Some(level)));
@@ -187,9 +196,23 @@ impl AccessRepository for MockRepo {
_thread_id: &str,
_user_id: Option<&MacroUserId<Lowercase<'_>>>,
) -> Result<Option<AccessLevel>, AccessError> {
+ self.thread_access_calls.fetch_add(1, Ordering::SeqCst);
Ok(*self.thread_access.lock().await)
}
+ async fn get_owned_email_thread_ids(
+ &self,
+ thread_ids: &[Uuid],
+ _user_id: &MacroUserId<Lowercase<'_>>,
+ ) -> Result<Vec<Uuid>, AccessError> {
+ let owned = self.owned_email_thread_ids.lock().await;
+ Ok(thread_ids
+ .iter()
+ .copied()
+ .filter(|id| owned.contains(id))
+ .collect())
+ }
+
async fn check_user_channel_membership(
&self,
_user_id: Option<&MacroUserId<Lowercase<'_>>>,
@@ -745,6 +768,46 @@ async fn test_check_access_with_none_user_id_and_no_access() {
// --- generate_entity_access_receipt tests ---
+#[tokio::test]
+async fn test_batch_email_receipts_use_owned_thread_fast_path() {
+ let thread_id = Uuid::new_v4();
+ let repo = MockRepo::new().with_owned_email_thread_ids(vec![thread_id]);
+ let calls = repo.thread_access_calls.clone();
+ let service = EntityAccessServiceImpl::new(repo);
+ let user_id = test_user_id();
+
+ let mut receipts = service
+ .generate_email_thread_view_access_receipts(&user_id, None, &[thread_id.to_string()])
+ .await;
+ let receipt = receipts.remove(&thread_id.to_string()).unwrap().unwrap();
+
+ assert_eq!(calls.load(Ordering::SeqCst), 0);
+ assert_eq!(receipt.entity().entity_type, EntityType::EmailThread);
+ assert_eq!(receipt.entity().entity_id, thread_id.to_string());
+ assert!(matches!(
+ receipt.entity_permission(),
+ EntityPermission::AccessLevel {
+ access_level: AccessLevel::Owner
+ }
+ ));
+}
+
+#[tokio::test]
+async fn test_batch_email_receipts_fall_back_for_shared_threads() {
+ let thread_id = Uuid::new_v4();
+ let repo = MockRepo::new().with_thread_access(AccessLevel::View);
+ let calls = repo.thread_access_calls.clone();
+ let service = EntityAccessServiceImpl::new(repo);
+ let user_id = test_user_id();
+
+ let receipts = service
+ .generate_email_thread_view_access_receipts(&user_id, None, &[thread_id.to_string()])
+ .await;
+
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+ assert!(receipts[&thread_id.to_string()].is_ok());
+}
+
#[tokio::test]
async fn test_generate_receipt_document_with_access() {
let repo = MockRepo::new().with_document_access(AccessLevel::Edit);
diff --git a/crates/entity_access/src/outbound/pg_access_repo/mod.rs b/crates/entity_access/src/outbound/pg_access_repo/mod.rs
index ef72881e8c..566365b050 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/mod.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/mod.rs
@@ -121,6 +121,18 @@ impl AccessRepository for PgAccessRepository {
.await?)
}
+ #[tracing::instrument(err, skip(self, thread_ids, user_id))]
+ async fn get_owned_email_thread_ids(
+ &self,
+ thread_ids: &[Uuid],
+ user_id: &MacroUserId<Lowercase<'_>>,
+ ) -> Result<Vec<Uuid>, AccessError> {
+ Ok(
+ queries::thread_access::get_owned_email_thread_ids(&self.pool, thread_ids, user_id)
+ .await?,
+ )
+ }
+
#[tracing::instrument(err, skip(self))]
async fn get_call_access(
&self,
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs
index da2a2f258d..fe0c0bf341 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access.rs
@@ -9,6 +9,46 @@ use sqlx::PgPool;
use std::str::FromStr;
use uuid::Uuid;
+#[derive(sqlx::FromRow)]
+struct OwnedEmailThreadRow {
+ id: Uuid,
+}
+
+/// Return the requested threads owned by, or inbox-delegated to, `user_id`.
+#[allow(
+ clippy::disallowed_methods,
+ reason = "runtime query is covered by the repository integration test"
+)]
+pub async fn get_owned_email_thread_ids(
+ pool: &PgPool,
+ thread_ids: &[Uuid],
+ user_id: &MacroUserId<Lowercase<'_>>,
+) -> Result<Vec<Uuid>, sqlx::Error> {
+ let rows = sqlx::query_as::<_, OwnedEmailThreadRow>(
+ r#"
+ SELECT t.id
+ FROM email_threads t
+ JOIN email_links l ON l.id = t.link_id
+ WHERE t.id = ANY($1)
+ AND (
+ l.macro_id = $2
+ OR EXISTS (
+ SELECT 1
+ FROM macro_user_links mul
+ WHERE mul.link_id = l.id
+ AND mul.primary_macro_id = $2
+ )
+ )
+ "#,
+ )
+ .bind(thread_ids)
+ .bind(user_id.as_ref())
+ .fetch_all(pool)
+ .await?;
+
+ Ok(rows.into_iter().map(|row| row.id).collect())
+}
+
/// Get the highest access level a user has for an email thread.
#[tracing::instrument(err, skip(pool, source_ids))]
pub async fn get_thread_access(
diff --git a/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs b/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs
index 740caf7e8a..a147d90178 100644
--- a/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs
+++ b/crates/entity_access/src/outbound/pg_access_repo/queries/thread_access/test.rs
@@ -329,6 +329,21 @@ async fn returns_none_when_thread_does_not_exist(pool: PgPool) -> anyhow::Result
Ok(())
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn batch_owner_lookup_returns_only_owned_threads(pool: PgPool) -> anyhow::Result<()> {
+ insert_user(&pool, REQUESTER, "[email protected]").await;
+ insert_user(&pool, OWNER, "[email protected]").await;
+ let (_, owned_thread_id) = create_link_and_thread(&pool, REQUESTER).await;
+ let (_, other_thread_id) = create_link_and_thread(&pool, OWNER).await;
+ let requester = user(REQUESTER);
+
+ let owned =
+ get_owned_email_thread_ids(&pool, &[owned_thread_id, other_thread_id], &requester).await?;
+
+ assert_eq!(owned, vec![owned_thread_id]);
+ Ok(())
+}
+
// ---------------------------------------------------------------------------
// Delegated / shared inboxes (macro_user_links)
// ---------------------------------------------------------------------------
diff --git a/crates/graphql_email/Cargo.toml b/crates/graphql_email/Cargo.toml
new file mode 100644
index 0000000000..7f1287535e
--- /dev/null
+++ b/crates/graphql_email/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "graphql_email"
+version = "0.1.0"
+edition = "2024"
+publish = false
+
+[dependencies]
+async-graphql = { workspace = true, features = ["dataloader"] }
+chrono = { workspace = true }
+email = { path = "../email", default-features = false, features = ["ports"] }
+macro_user_id = { path = "../macro_user_id" }
+tokio = { workspace = true }
+tracing = { workspace = true }
+uuid = { workspace = true }
+workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/crates/graphql_email/src/lib.rs b/crates/graphql_email/src/lib.rs
new file mode 100644
index 0000000000..09c878592b
--- /dev/null
+++ b/crates/graphql_email/src/lib.rs
@@ -0,0 +1,12 @@
+//! GraphQL inbound adapter for the email domain: email message object types
+//! and the DataLoader-backed Soup email-message edge.
+#![deny(missing_docs)]
+
+mod loaders;
+mod objects;
+
+pub use loaders::{
+ EmailContentKey, EmailContentLoad, EmailContentLoader, NoOpSoupEmailContentEdgeReader,
+ SoupEmailContentEdgeReader, email_content_loader,
+};
+pub use objects::{GraphqlSoupEmailMessage, load_latest_email_message};
diff --git a/crates/graphql_email/src/loaders.rs b/crates/graphql_email/src/loaders.rs
new file mode 100644
index 0000000000..c1e1a93306
--- /dev/null
+++ b/crates/graphql_email/src/loaders.rs
@@ -0,0 +1,195 @@
+use std::{collections::HashMap, sync::Arc};
+
+use async_graphql::dataloader::{DataLoader, Loader};
+use email::domain::models::ParsedMessage;
+use macro_user_id::user_id::MacroUserIdStr;
+
+pub(crate) const MAX_EMAIL_CONTENT_KEYS: usize = 20;
+
+/// Result of loading the newest non-draft content message for one email thread.
+#[derive(Debug, Clone)]
+pub enum EmailContentLoad {
+ /// The thread has a non-draft content message.
+ Found(Box<ParsedMessage>),
+ /// The thread is absent, inaccessible, or has no content messages.
+ Missing,
+ /// An internal failure occurred. Details are logged, never exposed.
+ Failed,
+}
+
+/// A request for the newest non-draft content message belonging to an email thread.
+#[derive(Debug, Clone, Hash, PartialEq, Eq)]
+pub struct EmailContentKey {
+ /// Email thread ID.
+ pub thread_id: String,
+}
+
+/// Reader used by the Soup email-content GraphQL edge.
+pub trait SoupEmailContentEdgeReader: Send + Sync + 'static {
+ /// Load content for authorized threads on behalf of `user_id`.
+ fn get_email_content<'a>(
+ &'a self,
+ user_id: &'a MacroUserIdStr<'static>,
+ keys: Vec<EmailContentKey>,
+ ) -> impl Future<Output = HashMap<EmailContentKey, EmailContentLoad>> + Send + 'a;
+}
+
+/// Schema-only reader that treats every thread as missing.
+#[derive(Debug, Clone, Copy, Default)]
+pub struct NoOpSoupEmailContentEdgeReader;
+
+impl SoupEmailContentEdgeReader for NoOpSoupEmailContentEdgeReader {
+ async fn get_email_content(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ keys: Vec<EmailContentKey>,
+ ) -> HashMap<EmailContentKey, EmailContentLoad> {
+ keys.into_iter()
+ .map(|key| (key, EmailContentLoad::Missing))
+ .collect()
+ }
+}
+
+/// DataLoader for the newest non-draft content message attached to Soup email threads.
+pub struct EmailContentLoader<R> {
+ user_id: MacroUserIdStr<'static>,
+ reader: R,
+}
+
+/// Error returned when a GraphQL operation exceeds the email-content cost cap.
+#[derive(Debug)]
+pub struct EmailContentLoaderError {
+ key_count: usize,
+}
+
+impl std::fmt::Display for EmailContentLoaderError {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(
+ formatter,
+ "email content edge supports at most {MAX_EMAIL_CONTENT_KEYS} threads per operation (received {})",
+ self.key_count
+ )
+ }
+}
+
+impl std::error::Error for EmailContentLoaderError {}
+
+impl<R> EmailContentLoader<R> {
+ /// Create a DataLoader scoped to the requesting user.
+ pub fn new(user_id: MacroUserIdStr<'static>, reader: R) -> Self {
+ Self { user_id, reader }
+ }
+}
+
+impl<R> Loader<EmailContentKey> for EmailContentLoader<R>
+where
+ R: SoupEmailContentEdgeReader,
+{
+ type Value = EmailContentLoad;
+ type Error = Arc<EmailContentLoaderError>;
+
+ async fn load(
+ &self,
+ keys: &[EmailContentKey],
+ ) -> Result<HashMap<EmailContentKey, Self::Value>, Self::Error> {
+ if keys.len() > MAX_EMAIL_CONTENT_KEYS {
+ tracing::warn!(
+ key_count = keys.len(),
+ max_key_count = MAX_EMAIL_CONTENT_KEYS,
+ "rejecting oversized Soup email content batch"
+ );
+ return Err(Arc::new(EmailContentLoaderError {
+ key_count: keys.len(),
+ }));
+ }
+
+ Ok(self
+ .reader
+ .get_email_content(&self.user_id, keys.to_vec())
+ .await)
+ }
+}
+
+/// Build an email-content DataLoader scoped to the requesting user.
+pub fn email_content_loader<R>(
+ user_id: MacroUserIdStr<'static>,
+ reader: R,
+) -> DataLoader<EmailContentLoader<R>>
+where
+ R: SoupEmailContentEdgeReader,
+{
+ DataLoader::new(EmailContentLoader::new(user_id, reader), tokio::spawn)
+}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::{
+ Arc,
+ atomic::{AtomicUsize, Ordering},
+ };
+
+ use macro_user_id::user_id::MacroUserIdStr;
+
+ use super::*;
+
+ #[derive(Clone, Default)]
+ struct RecordingReader {
+ calls: Arc<AtomicUsize>,
+ }
+
+ impl SoupEmailContentEdgeReader for RecordingReader {
+ async fn get_email_content(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ keys: Vec<EmailContentKey>,
+ ) -> HashMap<EmailContentKey, EmailContentLoad> {
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ keys.into_iter()
+ .map(|key| (key, EmailContentLoad::Missing))
+ .collect()
+ }
+ }
+
+ fn key(index: usize) -> EmailContentKey {
+ EmailContentKey {
+ thread_id: format!("00000000-0000-0000-0000-{index:012}"),
+ }
+ }
+
+ #[tokio::test]
+ async fn batches_distinct_threads_in_one_reader_call() {
+ let reader = RecordingReader::default();
+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let loader = email_content_loader(user_id, reader.clone());
+ let first = key(1);
+ let second = key(2);
+
+ let loaded = loader
+ .load_many(vec![first.clone(), second.clone()])
+ .await
+ .unwrap();
+
+ assert_eq!(reader.calls.load(Ordering::SeqCst), 1);
+ assert!(matches!(
+ loaded.get(&first),
+ Some(EmailContentLoad::Missing)
+ ));
+ assert!(matches!(
+ loaded.get(&second),
+ Some(EmailContentLoad::Missing)
+ ));
+ }
+
+ #[tokio::test]
+ async fn rejects_oversized_batches_without_calling_the_reader() {
+ let reader = RecordingReader::default();
+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let loader = EmailContentLoader::new(user_id, reader.clone());
+ let keys = (0..=MAX_EMAIL_CONTENT_KEYS).map(key).collect::<Vec<_>>();
+
+ let error = loader.load(&keys).await.unwrap_err();
+
+ assert_eq!(reader.calls.load(Ordering::SeqCst), 0);
+ assert!(error.to_string().contains("at most 20 threads"));
+ }
+}
diff --git a/crates/graphql_email/src/objects.rs b/crates/graphql_email/src/objects.rs
new file mode 100644
index 0000000000..e7b4757d69
--- /dev/null
+++ b/crates/graphql_email/src/objects.rs
@@ -0,0 +1,268 @@
+use async_graphql::{Context, ID, Object, dataloader::DataLoader};
+use email::domain::models::{ContactInfo, ParsedLabel, ParsedMessage};
+
+use crate::loaders::{
+ EmailContentKey, EmailContentLoad, EmailContentLoader, SoupEmailContentEdgeReader,
+};
+
+/// A lightweight email content projection for Soup queries.
+pub struct GraphqlSoupEmailMessage(ParsedMessage);
+
+#[Object]
+impl GraphqlSoupEmailMessage {
+ async fn id(&self) -> ID {
+ ID(self.0.db_id.to_string())
+ }
+
+ async fn thread_id(&self) -> ID {
+ ID(self.0.thread_db_id.to_string())
+ }
+
+ async fn link_id(&self) -> ID {
+ ID(self.0.link_id.to_string())
+ }
+
+ async fn subject(&self) -> Option<&str> {
+ self.0.subject.as_deref()
+ }
+
+ async fn snippet(&self) -> Option<&str> {
+ self.0.snippet.as_deref()
+ }
+
+ async fn internal_date_ts(&self) -> Option<String> {
+ self.0.internal_date_ts.map(|value| value.to_rfc3339())
+ }
+
+ async fn sent_at(&self) -> Option<String> {
+ self.0.sent_at.map(|value| value.to_rfc3339())
+ }
+
+ async fn is_read(&self) -> bool {
+ self.0.is_read
+ }
+
+ async fn is_starred(&self) -> bool {
+ self.0.is_starred
+ }
+
+ async fn is_sent(&self) -> bool {
+ self.0.is_sent
+ }
+
+ async fn has_attachments(&self) -> bool {
+ self.0.has_attachments
+ }
+
+ async fn from(&self) -> Option<GraphqlSoupEmailContact> {
+ self.0.from.clone().map(GraphqlSoupEmailContact)
+ }
+
+ async fn to(&self) -> Vec<GraphqlSoupEmailContact> {
+ self.0
+ .to
+ .iter()
+ .cloned()
+ .map(GraphqlSoupEmailContact)
+ .collect()
+ }
+
+ async fn cc(&self) -> Vec<GraphqlSoupEmailContact> {
+ self.0
+ .cc
+ .iter()
+ .cloned()
+ .map(GraphqlSoupEmailContact)
+ .collect()
+ }
+
+ async fn bcc(&self) -> Vec<GraphqlSoupEmailContact> {
+ self.0
+ .bcc
+ .iter()
+ .cloned()
+ .map(GraphqlSoupEmailContact)
+ .collect()
+ }
+
+ async fn labels(&self) -> Vec<GraphqlSoupEmailMessageLabel> {
+ self.0
+ .labels
+ .iter()
+ .cloned()
+ .map(GraphqlSoupEmailMessageLabel)
+ .collect()
+ }
+
+ async fn body_parsed(&self) -> Option<&str> {
+ self.0.body_parsed.as_deref()
+ }
+
+ async fn body_text(&self) -> Option<&str> {
+ self.0.body_text.as_deref()
+ }
+
+ async fn body_html_sanitized(&self) -> Option<&str> {
+ self.0.body_html_sanitized.as_deref()
+ }
+
+ async fn body_macro(&self) -> Option<&str> {
+ self.0.body_macro.as_deref()
+ }
+
+ async fn body_replyless(&self) -> Option<&str> {
+ self.0.body_replyless.as_deref()
+ }
+
+ async fn created_at(&self) -> String {
+ self.0.created_at.to_rfc3339()
+ }
+
+ async fn updated_at(&self) -> String {
+ self.0.updated_at.to_rfc3339()
+ }
+}
+
+/// An email sender or recipient embedded in a message.
+pub struct GraphqlSoupEmailContact(ContactInfo);
+
+#[Object]
+impl GraphqlSoupEmailContact {
+ async fn email(&self) -> &str {
+ &self.0.email
+ }
+
+ async fn name(&self) -> Option<&str> {
+ self.0.name.as_deref()
+ }
+
+ async fn photo_url(&self) -> Option<&str> {
+ self.0.photo_url.as_deref()
+ }
+}
+
+/// A lightweight label embedded in an email message.
+pub struct GraphqlSoupEmailMessageLabel(ParsedLabel);
+
+#[Object]
+impl GraphqlSoupEmailMessageLabel {
+ async fn provider_label_id(&self) -> &str {
+ &self.0.provider_id
+ }
+
+ async fn name(&self) -> &str {
+ &self.0.name
+ }
+}
+
+/// Load the newest non-draft content message for an email thread.
+pub async fn load_latest_email_message<R>(
+ ctx: &Context<'_>,
+ key: EmailContentKey,
+) -> async_graphql::Result<Option<GraphqlSoupEmailMessage>>
+where
+ R: SoupEmailContentEdgeReader,
+{
+ let loader = ctx.data::<DataLoader<EmailContentLoader<R>>>()?;
+ let value = loader.load_one(key).await?;
+ Ok(match value {
+ Some(EmailContentLoad::Found(message)) => Some(GraphqlSoupEmailMessage(*message)),
+ Some(EmailContentLoad::Missing | EmailContentLoad::Failed) | None => None,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use std::collections::HashMap;
+
+ use async_graphql::{EmptyMutation, EmptySubscription, Schema};
+ use chrono::Utc;
+ use macro_user_id::user_id::MacroUserIdStr;
+ use uuid::Uuid;
+
+ use super::*;
+ use crate::{SoupEmailContentEdgeReader, email_content_loader};
+
+ struct ContentQuery;
+
+ #[Object]
+ impl ContentQuery {
+ async fn latest_content_message(
+ &self,
+ ctx: &Context<'_>,
+ ) -> async_graphql::Result<Option<GraphqlSoupEmailMessage>> {
+ load_latest_email_message::<ContentReader>(
+ ctx,
+ EmailContentKey {
+ thread_id: Uuid::from_u128(2).to_string(),
+ },
+ )
+ .await
+ }
+ }
+
+ struct ContentReader;
+
+ impl SoupEmailContentEdgeReader for ContentReader {
+ async fn get_email_content(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ keys: Vec<EmailContentKey>,
+ ) -> HashMap<EmailContentKey, EmailContentLoad> {
+ keys.into_iter()
+ .map(|key| {
+ let thread_id = Uuid::parse_str(&key.thread_id).unwrap();
+ (key, EmailContentLoad::Found(Box::new(message(thread_id))))
+ })
+ .collect()
+ }
+ }
+
+ fn message(thread_id: Uuid) -> ParsedMessage {
+ let now = Utc::now();
+ ParsedMessage {
+ db_id: Uuid::from_u128(1),
+ link_id: Uuid::from_u128(3),
+ thread_db_id: thread_id,
+ subject: Some("Subject".to_owned()),
+ snippet: Some("Snippet".to_owned()),
+ from: None,
+ to: Vec::new(),
+ cc: Vec::new(),
+ bcc: Vec::new(),
+ labels: Vec::new(),
+ body_parsed: Some("Hello from the edge".to_owned()),
+ body_text: Some("Hello from the edge".to_owned()),
+ body_html_sanitized: None,
+ body_macro: None,
+ body_replyless: Some("Hello from the edge".to_owned()),
+ internal_date_ts: Some(now),
+ sent_at: Some(now),
+ is_read: true,
+ is_starred: false,
+ is_sent: false,
+ is_draft: false,
+ has_attachments: false,
+ created_at: now,
+ updated_at: now,
+ }
+ }
+
+ #[tokio::test]
+ async fn executes_content_field_with_request_scoped_loader() {
+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let schema = Schema::build(ContentQuery, EmptyMutation, EmptySubscription)
+ .data(email_content_loader(user_id, ContentReader))
+ .finish();
+
+ let response = schema
+ .execute("{ latestContentMessage { id threadId bodyParsed } }")
+ .await;
+
+ assert!(response.errors.is_empty(), "{:?}", response.errors);
+ assert_eq!(
+ response.data.to_string(),
+ r#"{latestContentMessage: {id: "00000000-0000-0000-0000-000000000001", threadId: "00000000-0000-0000-0000-000000000002", bodyParsed: "Hello from the edge"}}"#
+ );
+ }
+}
diff --git a/crates/graphql_soup/src/objects.rs b/crates/graphql_soup/src/objects.rs
index 867d6b523f..fe3a159056 100644
--- a/crates/graphql_soup/src/objects.rs
+++ b/crates/graphql_soup/src/objects.rs
@@ -23,8 +23,14 @@ use soup::domain::models::EnrichedSoupItem;
/// The concrete edge object is supplied by the schema composition crate and
/// flattened into each Soup entity's GraphQL fields.
pub trait SoupEntityEdges: ObjectType + Clone + Send + Sync + 'static {
+ /// Additional fields attached only to email-thread entities.
+ type EmailThreadEdges: ObjectType + Clone + Send + Sync + 'static;
+
/// Construct the edge object for a Soup entity.
fn from_entity(entity: model_entity::Entity<'static>) -> Self;
+
+ /// Construct the email-thread-specific edge object.
+ fn email_thread_edges(&self) -> Self::EmailThreadEdges;
}
/// Page returned by `Query.soup`.
@@ -715,6 +721,11 @@ where
async fn edges(&self) -> E {
self.1.clone()
}
+
+ #[graphql(flatten)]
+ async fn email_thread_edges(&self) -> E::EmailThreadEdges {
+ self.1.email_thread_edges()
+ }
}
/// GraphQL channel participant.
diff --git a/crates/macro_db_client/migrations/20260710150000_email_latest_content_message_index.sql b/crates/macro_db_client/migrations/20260710150000_email_latest_content_message_index.sql
new file mode 100644
index 0000000000..1767d4f02e
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260710150000_email_latest_content_message_index.sql
@@ -0,0 +1,10 @@
+-- no-transaction
+
+-- Supports one indexed latest-content probe per requested email thread.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_email_messages_latest_content
+ ON public.email_messages (
+ thread_id,
+ (COALESCE(internal_date_ts, sent_at, created_at)) DESC,
+ id DESC
+ )
+ WHERE is_draft = FALSE;
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 2c821b92d0..047b06e934 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -147,6 +147,7 @@ pub(crate) type DssGraphqlSoupSchema = complete_graph::SharedSoupSchema<
complete_graph::PropertiesEntityPropertyWriter<PropertiesService, EntityAccessService>,
Arc<ai_tools::ToolNotificationService>,
complete_graph::PropertiesEntityPropertyReader<PropertiesService, EntityAccessService>,
+ crate::api::graphql_soup::DssEmailContentReader<DssEmailService, EntityAccessService>,
>;
type SystemPropertiesService = SystemPropertiesServiceImpl<PgSystemPropertiesRepository>;
diff --git a/services/document_storage_service/src/api/graphql_soup.rs b/services/document_storage_service/src/api/graphql_soup.rs
index 5a6f573cd5..fa77f7719d 100644
--- a/services/document_storage_service/src/api/graphql_soup.rs
+++ b/services/document_storage_service/src/api/graphql_soup.rs
@@ -11,6 +11,10 @@ use axum_extra::extract::Cached;
use complete_graph::GraphqlSoupRequestParts;
use model_user::axum_extractor::OptionalMacroUserExtractor;
+mod email_content;
+
+pub(crate) use email_content::DssEmailContentReader;
+
pub(crate) fn router() -> Router<ApiContext> {
Router::new().route("/soup/graphql", get(graphiql).post(handler))
}
@@ -63,6 +67,10 @@ async fn handler(State(state): State<ApiContext>, req: Request) -> Response {
state.entity_access_service.clone(),
macro_user_id.clone(),
);
+ let email_content_reader = DssEmailContentReader::new(
+ state.soup_router_state.email_service(),
+ state.entity_access_service.clone(),
+ );
let request = request
.data(GraphqlSoupRequestParts::new(parts))
.data(state.clone())
@@ -70,6 +78,10 @@ async fn handler(State(state): State<ApiContext>, req: Request) -> Response {
macro_user_id.clone(),
property_reader,
))
+ .data(complete_graph::email_content_loader(
+ macro_user_id.clone(),
+ email_content_reader,
+ ))
.data(property_writer)
.data(complete_graph::entity_notifications_loader(
macro_user_id,
diff --git a/services/document_storage_service/src/api/graphql_soup/email_content.rs b/services/document_storage_service/src/api/graphql_soup/email_content.rs
new file mode 100644
index 0000000000..b4c00c5a30
--- /dev/null
+++ b/services/document_storage_service/src/api/graphql_soup/email_content.rs
@@ -0,0 +1,114 @@
+#[cfg(test)]
+mod test;
+
+use std::{collections::HashMap, sync::Arc};
+
+use complete_graph::{EmailContentKey, EmailContentLoad, SoupEmailContentEdgeReader};
+use email::domain::ports::EmailContentService;
+use entity_access::domain::{models::AccessError, ports::EntityAccessService};
+use macro_user_id::user_id::MacroUserIdStr;
+use uuid::Uuid;
+
+/// Document-storage adapter for the Soup email-content edge.
+///
+/// The adapter batches authorization through the entity-access domain, then
+/// passes only typed view receipts to the email domain.
+pub(crate) struct DssEmailContentReader<S, EAS> {
+ email_service: Arc<S>,
+ entity_access_service: Arc<EAS>,
+}
+
+impl<S, EAS> DssEmailContentReader<S, EAS> {
+ pub(crate) fn new(email_service: Arc<S>, entity_access_service: Arc<EAS>) -> Self {
+ Self {
+ email_service,
+ entity_access_service,
+ }
+ }
+}
+
+impl<S, EAS> SoupEmailContentEdgeReader for DssEmailContentReader<S, EAS>
+where
+ S: EmailContentService,
+ EAS: EntityAccessService,
+{
+ async fn get_email_content(
+ &self,
+ user_id: &MacroUserIdStr<'static>,
+ keys: Vec<EmailContentKey>,
+ ) -> HashMap<EmailContentKey, EmailContentLoad> {
+ let thread_ids = keys
+ .iter()
+ .map(|key| key.thread_id.clone())
+ .collect::<Vec<_>>();
+ let mut receipts = self
+ .entity_access_service
+ .generate_email_thread_view_access_receipts(user_id, None, &thread_ids)
+ .await;
+ let access_results = keys.into_iter().map(|key| {
+ let result = receipts
+ .remove(&key.thread_id)
+ .unwrap_or(Err(AccessError::Internal));
+ (key, result)
+ });
+
+ let mut loads = HashMap::with_capacity(access_results.len());
+ let mut authorized = Vec::new();
+ let mut keys_by_thread_id = HashMap::new();
+
+ for (key, access_result) in access_results {
+ match access_result {
+ Ok(receipt) => match Uuid::parse_str(&key.thread_id) {
+ Ok(thread_id) => {
+ keys_by_thread_id.insert(thread_id, key);
+ authorized.push(receipt);
+ }
+ Err(error) => {
+ tracing::error!(thread_id = %key.thread_id, ?error, "invalid authorized email thread ID");
+ loads.insert(key, EmailContentLoad::Failed);
+ }
+ },
+ Err(
+ AccessError::Unauthorized
+ | AccessError::UnauthorizedWithMessage(_)
+ | AccessError::NotFound(_),
+ ) => {
+ loads.insert(key, EmailContentLoad::Missing);
+ }
+ Err(error) => {
+ tracing::error!(thread_id = %key.thread_id, ?error, "email content access check failed");
+ loads.insert(key, EmailContentLoad::Failed);
+ }
+ }
+ }
+
+ if authorized.is_empty() {
+ return loads;
+ }
+
+ match self
+ .email_service
+ .get_latest_messages_parsed(authorized)
+ .await
+ {
+ Ok(mut messages) => {
+ for (thread_id, key) in keys_by_thread_id {
+ let load = messages
+ .remove(&thread_id)
+ .map_or(EmailContentLoad::Missing, |message| {
+ EmailContentLoad::Found(Box::new(message))
+ });
+ loads.insert(key, load);
+ }
+ }
+ Err(error) => {
+ tracing::error!(?error, "bulk email content load failed");
+ for key in keys_by_thread_id.into_values() {
+ loads.insert(key, EmailContentLoad::Failed);
+ }
+ }
+ }
+
+ loads
+ }
+}
diff --git a/services/document_storage_service/src/api/graphql_soup/email_content/test.rs b/services/document_storage_service/src/api/graphql_soup/email_content/test.rs
new file mode 100644
index 0000000000..f925acaf12
--- /dev/null
+++ b/services/document_storage_service/src/api/graphql_soup/email_content/test.rs
@@ -0,0 +1,195 @@
+use std::{
+ collections::HashMap,
+ sync::{
+ Arc,
+ atomic::{AtomicUsize, Ordering},
+ },
+};
+
+use email::domain::{
+ models::{EmailErr, ParsedMessage},
+ ports::EmailContentService,
+};
+use entity_access::domain::{
+ models::{
+ AccessLevel, BotId, CallChannelInfo, EntityAccessReceipt, EntityPermission, EntityType,
+ RequiredPermission, UserTeamInfo, ViewAccessLevel,
+ },
+ ports::EntityAccessService,
+};
+use macro_user_id::{
+ lowercased::Lowercase,
+ user_id::{MacroUserId, MacroUserIdStr},
+};
+
+use super::*;
+
+#[derive(Clone)]
+struct TestAccessService {
+ allow: bool,
+}
+
+impl EntityAccessService for TestAccessService {
+ async fn generate_entity_access_receipt<T: RequiredPermission>(
+ &self,
+ _user_id: &MacroUserId<Lowercase<'_>>,
+ _user_org_id: Option<i64>,
+ entity_id: &str,
+ entity_type: EntityType,
+ ) -> Result<EntityAccessReceipt<T>, AccessError> {
+ if !self.allow {
+ return Err(AccessError::Unauthorized);
+ }
+ Ok(EntityAccessReceipt::dangerously_assert_authenticated_user(
+ MacroUserIdStr::try_from_email("[email protected]").unwrap(),
+ entity_id,
+ entity_type,
+ ))
+ }
+
+ async fn generate_bot_entity_access_receipt<T: RequiredPermission>(
+ &self,
+ _bot_id: BotId,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ ) -> Result<EntityAccessReceipt<T>, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_access_level(
+ &self,
+ _user_id: Option<&MacroUserId<Lowercase<'_>>>,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ ) -> Result<Option<AccessLevel>, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn check_access(
+ &self,
+ _user_id: Option<&MacroUserId<Lowercase<'_>>>,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ _required_level: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn check_public_access(
+ &self,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ _required_level: AccessLevel,
+ ) -> Result<AccessLevel, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_entity_permission(
+ &self,
+ _user_id: Option<&MacroUserId<Lowercase<'_>>>,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ _user_org_id: Option<i64>,
+ ) -> Result<EntityPermission, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_crm_entity_permission_with_team(
+ &self,
+ _user_id: Option<&MacroUserId<Lowercase<'_>>>,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ ) -> Result<(EntityPermission, Uuid), AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_users_by_entity(
+ &self,
+ _entity_id: &str,
+ _entity_type: EntityType,
+ ) -> Result<Vec<MacroUserIdStr<'static>>, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_call_channel(
+ &self,
+ _call_id: &Uuid,
+ ) -> Result<Option<CallChannelInfo>, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_call_channel_by_channel_id(
+ &self,
+ _channel_id: &Uuid,
+ ) -> Result<Option<CallChannelInfo>, AccessError> {
+ Err(AccessError::Internal)
+ }
+
+ async fn get_user_team(
+ &self,
+ _user_id: &MacroUserId<Lowercase<'_>>,
+ ) -> Result<Option<UserTeamInfo>, AccessError> {
+ Err(AccessError::Internal)
+ }
+}
+
+#[derive(Default)]
+struct RecordingContentService {
+ calls: AtomicUsize,
+}
+
+impl EmailContentService for RecordingContentService {
+ async fn get_latest_messages_parsed(
+ &self,
+ _receipts: Vec<EntityAccessReceipt<ViewAccessLevel>>,
+ ) -> Result<HashMap<Uuid, ParsedMessage>, EmailErr> {
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ Ok(HashMap::new())
+ }
+}
+
+fn key(index: usize) -> EmailContentKey {
+ EmailContentKey {
+ thread_id: format!("00000000-0000-0000-0000-{index:012}"),
+ }
+}
+
+#[tokio::test]
+async fn authorized_keys_reach_the_email_domain() {
+ let content = Arc::new(RecordingContentService::default());
+ let service =
+ DssEmailContentReader::new(content.clone(), Arc::new(TestAccessService { allow: true }));
+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let requested = key(1);
+
+ let loaded = service
+ .get_email_content(&user_id, vec![requested.clone()])
+ .await;
+
+ assert_eq!(content.calls.load(Ordering::SeqCst), 1);
+ assert!(matches!(
+ loaded.get(&requested),
+ Some(EmailContentLoad::Missing)
+ ));
+}
+
+#[tokio::test]
+async fn unauthorized_keys_do_not_reach_the_email_domain() {
+ let content = Arc::new(RecordingContentService::default());
+ let service = DssEmailContentReader::new(
+ content.clone(),
+ Arc::new(TestAccessService { allow: false }),
+ );
+ let user_id = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let requested = key(1);
+
+ let loaded = service
+ .get_email_content(&user_id, vec![requested.clone()])
+ .await;
+
+ assert_eq!(content.calls.load(Ordering::SeqCst), 0);
+ assert!(matches!(
+ loaded.get(&requested),
+ Some(EmailContentLoad::Missing)
+ ));
+}
diff --git a/static_assets/schema.graphql b/static_assets/schema.graphql
index 713edd2a5e..7aa6d92a4c 100644
--- a/static_assets/schema.graphql
+++ b/static_assets/schema.graphql
@@ -715,6 +715,12 @@ type GraphqlSoupEmailAttachment {
createdAt: String!
}
+type GraphqlSoupEmailContact {
+ email: String!
+ name: String
+ photoUrl: String
+}
+
type GraphqlSoupEmailLabel {
id: ID!
linkId: ID!
@@ -726,6 +732,37 @@ type GraphqlSoupEmailLabel {
type: String!
}
+type GraphqlSoupEmailMessage {
+ id: ID!
+ threadId: ID!
+ linkId: ID!
+ subject: String
+ snippet: String
+ internalDateTs: String
+ sentAt: String
+ isRead: Boolean!
+ isStarred: Boolean!
+ isSent: Boolean!
+ hasAttachments: Boolean!
+ from: GraphqlSoupEmailContact
+ to: [GraphqlSoupEmailContact!]!
+ cc: [GraphqlSoupEmailContact!]!
+ bcc: [GraphqlSoupEmailContact!]!
+ labels: [GraphqlSoupEmailMessageLabel!]!
+ bodyParsed: String
+ bodyText: String
+ bodyHtmlSanitized: String
+ bodyMacro: String
+ bodyReplyless: String
+ createdAt: String!
+ updatedAt: String!
+}
+
+type GraphqlSoupEmailMessageLabel {
+ providerLabelId: String!
+ name: String!
+}
+
type GraphqlSoupEmailParticipant {
id: ID!
linkId: ID!
@@ -760,6 +797,7 @@ type GraphqlSoupEmailThread {
labels: [GraphqlSoupEmailLabel!]!
properties: [GraphqlProperty!]!
notifications: [GraphqlSoupNotification!]!
+ latestContentMessage: GraphqlSoupEmailMessage
}
"""