Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(calendar): resolve email invitations for linked inboxes (#8055)

gbirmand41ac96bb6e0
diff --git a/.sqlx/query-db932610fa3b2640026b23d9d69ee07febe9124cb765bf4cb572a3ac52b9e8a2.json b/.sqlx/query-106022b492e461a3d054a7d2644702cce52d382ca2e6c4026517718550a2ec76.json
similarity index 77%
rename from .sqlx/query-db932610fa3b2640026b23d9d69ee07febe9124cb765bf4cb572a3ac52b9e8a2.json
rename to .sqlx/query-106022b492e461a3d054a7d2644702cce52d382ca2e6c4026517718550a2ec76.json
index 17a93ebf5a..7239ecb060 100644
--- a/.sqlx/query-db932610fa3b2640026b23d9d69ee07febe9124cb765bf4cb572a3ac52b9e8a2.json
+++ b/.sqlx/query-106022b492e461a3d054a7d2644702cce52d382ca2e6c4026517718550a2ec76.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT requested.ord AS \"ord!\", candidate.id, candidate.source_link_id, candidate.email_address, candidate.sequence, candidate.status, candidate.updated_at,\n (SELECT o.occurrence_key FROM calendar_event_occurrences o\n CROSS JOIN LATERAL (SELECT COALESCE(o.starts_at, o.start_date::timestamp AT TIME ZONE 'UTC') AS starts,\n COALESCE(o.ends_at, o.end_date::timestamp AT TIME ZONE 'UTC') AS ends) span\n WHERE o.event_id = candidate.id AND (requested.occurrence_key IS NULL OR o.occurrence_key = requested.occurrence_key OR o.recurrence_id = requested.occurrence_key)\n -- A series resolves to its current or next live occurrence, else its latest.\n ORDER BY o.is_cancelled, (span.ends > now()) DESC,\n CASE WHEN span.ends > now() THEN span.starts END, span.starts DESC, o.occurrence_key\n LIMIT 1) AS occurrence_key\n FROM unnest($2::text[], $3::text[]) WITH ORDINALITY requested(uid, occurrence_key, ord)\n CROSS JOIN LATERAL (\n SELECT e.id, e.source_link_id, l.email_address, e.sequence, e.status, e.updated_at\n FROM calendar_events e JOIN email_links l ON l.id = e.source_link_id\n WHERE e.ical_uid = requested.uid AND e.owner_id = $1 AND l.macro_id = $1\n AND EXISTS (SELECT 1 FROM calendar_event_sources s JOIN calendar_accounts a ON a.id = s.account_id JOIN calendars c ON c.id = s.calendar_id WHERE s.event_id = e.id AND s.source_kind = 'google' AND NOT c.is_deleted AND a.sync_status NOT IN ('disabled', 'reauth_required'))\n ORDER BY e.id LIMIT 16\n ) candidate\n ",
+ "query": "\n SELECT requested.ord AS \"ord!\", candidate.id, candidate.source_link_id, candidate.email_address, candidate.sequence, candidate.status, candidate.updated_at,\n (SELECT o.occurrence_key FROM calendar_event_occurrences o\n CROSS JOIN LATERAL (SELECT COALESCE(o.starts_at, o.start_date::timestamp AT TIME ZONE 'UTC') AS starts,\n COALESCE(o.ends_at, o.end_date::timestamp AT TIME ZONE 'UTC') AS ends) span\n WHERE o.event_id = candidate.id AND (requested.occurrence_key IS NULL OR o.occurrence_key = requested.occurrence_key OR o.recurrence_id = requested.occurrence_key)\n -- A series resolves to its current or next live occurrence, else its latest.\n ORDER BY o.is_cancelled, (span.ends > now()) DESC,\n CASE WHEN span.ends > now() THEN span.starts END, span.starts DESC, o.occurrence_key\n LIMIT 1) AS occurrence_key\n FROM unnest($2::text[], $3::text[]) WITH ORDINALITY requested(uid, occurrence_key, ord)\n CROSS JOIN LATERAL (\n SELECT e.id, e.source_link_id, l.email_address, e.sequence, e.status, e.updated_at\n FROM calendar_events e JOIN email_links l ON l.id = e.source_link_id\n WHERE e.ical_uid = requested.uid\n AND ((e.owner_id = $1 AND l.macro_id = $1)\n OR EXISTS (SELECT 1 FROM macro_user_links delegation\n WHERE delegation.primary_macro_id = $1 AND delegation.link_id = e.source_link_id))\n AND EXISTS (SELECT 1 FROM calendar_event_sources s JOIN calendar_accounts a ON a.id = s.account_id JOIN calendars c ON c.id = s.calendar_id WHERE s.event_id = e.id AND s.source_kind = 'google' AND NOT c.is_deleted AND a.sync_status NOT IN ('disabled', 'reauth_required'))\n ORDER BY e.id LIMIT 16\n ) candidate\n ",
"describe": {
"columns": [
{
@@ -62,5 +62,5 @@
null
]
},
- "hash": "db932610fa3b2640026b23d9d69ee07febe9124cb765bf4cb572a3ac52b9e8a2"
+ "hash": "106022b492e461a3d054a7d2644702cce52d382ca2e6c4026517718550a2ec76"
}
diff --git a/.sqlx/query-23d9f97b2e79c6cb15fc6cf68e74c892f7e0d561597bd7aaac3b7cb8694a4522.json b/.sqlx/query-23d9f97b2e79c6cb15fc6cf68e74c892f7e0d561597bd7aaac3b7cb8694a4522.json
new file mode 100644
index 0000000000..5d0ad6f28b
--- /dev/null
+++ b/.sqlx/query-23d9f97b2e79c6cb15fc6cf68e74c892f7e0d561597bd7aaac3b7cb8694a4522.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)\n VALUES ($1, $2, $3)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "23d9f97b2e79c6cb15fc6cf68e74c892f7e0d561597bd7aaac3b7cb8694a4522"
+}
diff --git a/.sqlx/query-0d1400b19fc1f0678644749a87bc6e7f30c5d5be5fcd5490504ec6c0de375681.json b/.sqlx/query-45a15bd0b9e024f65078cdd068e299d891aa447153c2573dc26707879329690b.json
similarity index 57%
rename from .sqlx/query-0d1400b19fc1f0678644749a87bc6e7f30c5d5be5fcd5490504ec6c0de375681.json
rename to .sqlx/query-45a15bd0b9e024f65078cdd068e299d891aa447153c2573dc26707879329690b.json
index a8b56cae0b..256780fe60 100644
--- a/.sqlx/query-0d1400b19fc1f0678644749a87bc6e7f30c5d5be5fcd5490504ec6c0de375681.json
+++ b/.sqlx/query-45a15bd0b9e024f65078cdd068e299d891aa447153c2573dc26707879329690b.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "SELECT count(*) > 0 AS \"connected!\",\n COALESCE(bool_or(a.sync_status IN ('pending', 'syncing')), false) AS \"syncing!\"\n FROM calendar_accounts a JOIN email_links l ON l.id = a.email_link_id\n WHERE a.owner_id = $1 AND l.macro_id = $1 AND a.sync_status NOT IN ('disabled', 'reauth_required')",
+ "query": "SELECT count(*) > 0 AS \"connected!\",\n COALESCE(bool_or(a.sync_status IN ('pending', 'syncing')), false) AS \"syncing!\"\n FROM calendar_accounts a JOIN email_links l ON l.id = a.email_link_id\n WHERE ((a.owner_id = $1 AND l.macro_id = $1)\n OR EXISTS (SELECT 1 FROM macro_user_links delegation\n WHERE delegation.primary_macro_id = $1 AND delegation.link_id = a.email_link_id))\n AND a.sync_status NOT IN ('disabled', 'reauth_required')",
"describe": {
"columns": [
{
@@ -24,5 +24,5 @@
null
]
},
- "hash": "0d1400b19fc1f0678644749a87bc6e7f30c5d5be5fcd5490504ec6c0de375681"
+ "hash": "45a15bd0b9e024f65078cdd068e299d891aa447153c2573dc26707879329690b"
}
diff --git a/.sqlx/query-6d062758c6388cc69a012ed7b5b508486b4a71177b963cffcf24f223f4e8915f.json b/.sqlx/query-6d062758c6388cc69a012ed7b5b508486b4a71177b963cffcf24f223f4e8915f.json
new file mode 100644
index 0000000000..37f219e2ca
--- /dev/null
+++ b/.sqlx/query-6d062758c6388cc69a012ed7b5b508486b4a71177b963cffcf24f223f4e8915f.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "DELETE FROM macro_user_links WHERE primary_macro_id = $1 AND link_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "6d062758c6388cc69a012ed7b5b508486b4a71177b963cffcf24f223f4e8915f"
+}
diff --git a/.sqlx/query-ba85a555cb8071a8b7861176e6d983a96e4d2d347b6bcf38ab83f34521cc3f78.json b/.sqlx/query-ba85a555cb8071a8b7861176e6d983a96e4d2d347b6bcf38ab83f34521cc3f78.json
new file mode 100644
index 0000000000..cfe0c14383
--- /dev/null
+++ b/.sqlx/query-ba85a555cb8071a8b7861176e6d983a96e4d2d347b6bcf38ab83f34521cc3f78.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE calendar_accounts SET sync_status = $1 WHERE email_link_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ba85a555cb8071a8b7861176e6d983a96e4d2d347b6bcf38ab83f34521cc3f78"
+}
diff --git a/.sqlx/query-341970aa3de5e15886614551d3c08dd4d856c65e8f975a8a7028cba1050726cc.json b/.sqlx/query-bce91baa6d1f4bf7cc4dccf330fcc7433e84d39b43e65d6b5cc6248dc2f77543.json
similarity index 96%
rename from .sqlx/query-341970aa3de5e15886614551d3c08dd4d856c65e8f975a8a7028cba1050726cc.json
rename to .sqlx/query-bce91baa6d1f4bf7cc4dccf330fcc7433e84d39b43e65d6b5cc6248dc2f77543.json
index d683cfd413..d49f980db5 100644
--- a/.sqlx/query-341970aa3de5e15886614551d3c08dd4d856c65e8f975a8a7028cba1050726cc.json
+++ b/.sqlx/query-bce91baa6d1f4bf7cc4dccf330fcc7433e84d39b43e65d6b5cc6248dc2f77543.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": " SELECT\n occurrence.event_id,\n event.source_link_id,\n occurrence.occurrence_key,\n occurrence.recurrence_id,\n occurrence.starts_at AS occurrence_starts_at,\n occurrence.ends_at AS occurrence_ends_at,\n occurrence.start_date AS occurrence_start_date,\n occurrence.end_date AS occurrence_end_date,\n occurrence.is_cancelled,\n override.title AS override_title,\n override.description AS override_description,\n override.location AS override_location,\n override.status AS override_status,\n event.owner_id,\n event.ical_uid,\n event.title,\n event.description,\n event.location,\n event.status,\n event.visibility,\n event.transparency,\n event.event_type,\n event.starts_at,\n event.ends_at,\n event.start_date,\n event.end_date,\n event.time_zone,\n event.recurrence_lines,\n event.organizer_email,\n event.organizer_name,\n event.creator_email,\n event.creator_name,\n event.conference_url,\n event.conference_provider,\n COALESCE(override.sequence, event.sequence) AS \"sequence!\",\n event.is_read_only,\n event.reminders_use_default,\n event.reminder_overrides,\n event.created_at,\n COALESCE(override.source_updated_at, event.updated_at) AS \"updated_at!\"\n FROM unnest($2::uuid[], $3::text[]) requested(event_id, occurrence_key)\n JOIN calendar_events event ON event.id = requested.event_id\n JOIN LATERAL (\n SELECT o.* FROM calendar_event_occurrences o\n WHERE o.event_id = event.id\n AND o.occurrence_key = requested.occurrence_key\n ORDER BY o.occurrence_key LIMIT 1\n ) occurrence ON true\n LEFT JOIN calendar_event_overrides override\n ON override.event_id = occurrence.event_id\n AND override.recurrence_id = occurrence.recurrence_id\n WHERE event.owner_id = $1\n",
+ "query": " SELECT\n occurrence.event_id,\n event.source_link_id,\n occurrence.occurrence_key,\n occurrence.recurrence_id,\n occurrence.starts_at AS occurrence_starts_at,\n occurrence.ends_at AS occurrence_ends_at,\n occurrence.start_date AS occurrence_start_date,\n occurrence.end_date AS occurrence_end_date,\n occurrence.is_cancelled,\n override.title AS override_title,\n override.description AS override_description,\n override.location AS override_location,\n override.status AS override_status,\n event.owner_id,\n event.ical_uid,\n event.title,\n event.description,\n event.location,\n event.status,\n event.visibility,\n event.transparency,\n event.event_type,\n event.starts_at,\n event.ends_at,\n event.start_date,\n event.end_date,\n event.time_zone,\n event.recurrence_lines,\n event.organizer_email,\n event.organizer_name,\n event.creator_email,\n event.creator_name,\n event.conference_url,\n event.conference_provider,\n COALESCE(override.sequence, event.sequence) AS \"sequence!\",\n event.is_read_only,\n event.reminders_use_default,\n event.reminder_overrides,\n event.created_at,\n COALESCE(override.source_updated_at, event.updated_at) AS \"updated_at!\"\n FROM unnest($2::uuid[], $3::text[]) requested(event_id, occurrence_key)\n JOIN calendar_events event ON event.id = requested.event_id\n JOIN LATERAL (\n SELECT o.* FROM calendar_event_occurrences o\n WHERE o.event_id = event.id\n AND o.occurrence_key = requested.occurrence_key\n ORDER BY o.occurrence_key LIMIT 1\n ) occurrence ON true\n LEFT JOIN calendar_event_overrides override\n ON override.event_id = occurrence.event_id\n AND override.recurrence_id = occurrence.recurrence_id\n WHERE event.owner_id = $1\n OR EXISTS (SELECT 1 FROM macro_user_links delegation\n WHERE delegation.primary_macro_id = $1 AND delegation.link_id = event.source_link_id)\n",
"describe": {
"columns": [
{
@@ -254,5 +254,5 @@
null
]
},
- "hash": "341970aa3de5e15886614551d3c08dd4d856c65e8f975a8a7028cba1050726cc"
+ "hash": "bce91baa6d1f4bf7cc4dccf330fcc7433e84d39b43e65d6b5cc6248dc2f77543"
}
diff --git a/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOf.ts b/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOf.ts
index e9cab09b6e..c406b33282 100644
--- a/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOf.ts
+++ b/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOf.ts
@@ -7,7 +7,7 @@
import type { InvitationResolutionOneOfKind } from './invitationResolutionOneOfKind';
/**
- * Viewer has no active owned Google-backed calendar.
+ * Viewer has no active owned or delegated Google-backed calendar.
*/
export type InvitationResolutionOneOf = {
kind: InvitationResolutionOneOfKind;
diff --git a/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOfOneone.ts b/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOfOneone.ts
index 0062c190b3..48ef6602ec 100644
--- a/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOfOneone.ts
+++ b/apps/web/src/lib/service-clients/service-email/generated/schemas/invitationResolutionOneOfOneone.ts
@@ -7,7 +7,7 @@
import type { InvitationResolutionOneOfOneoneKind } from './invitationResolutionOneOfOneoneKind';
/**
- * More than one owned account could respond; never guess.
+ * More than one authorized account could respond; never guess.
*/
export type InvitationResolutionOneOfOneone = {
kind: InvitationResolutionOneOfOneoneKind;
diff --git a/apps/web/src/lib/service-clients/service-email/openapi.json b/apps/web/src/lib/service-clients/service-email/openapi.json
index cab48eb8bc..770718cd9a 100644
--- a/apps/web/src/lib/service-clients/service-email/openapi.json
+++ b/apps/web/src/lib/service-clients/service-email/openapi.json
@@ -5016,7 +5016,7 @@
"oneOf": [
{
"type": "object",
- "description": "Viewer has no active owned Google-backed calendar.",
+ "description": "Viewer has no active owned or delegated Google-backed calendar.",
"required": ["kind"],
"properties": {
"kind": {
@@ -5071,7 +5071,7 @@
},
{
"type": "object",
- "description": "More than one owned account could respond; never guess.",
+ "description": "More than one authorized account could respond; never guess.",
"required": ["kind"],
"properties": {
"kind": {
diff --git a/crates/calendar_events/src/domain/invitations.rs b/crates/calendar_events/src/domain/invitations.rs
index 41341ff39a..61868beb29 100644
--- a/crates/calendar_events/src/domain/invitations.rs
+++ b/crates/calendar_events/src/domain/invitations.rs
@@ -64,7 +64,7 @@ impl InvitationIdentity {
}
}
-/// Facts from an independently authorized owned calendar copy.
+/// Facts from an independently authorized owned or delegated calendar copy.
pub struct InvitationCandidate {
/// Connected inbox of this copy.
pub link_id: Uuid,
@@ -83,12 +83,12 @@ pub struct InvitationCandidate {
}
/// Calendar-owned access and identity lookup boundary.
pub trait CalendarInvitationRepository: Send + Sync {
- /// Whether an independently owned connected account is available or still syncing.
+ /// Whether an owned or delegated connected account is available or still syncing.
fn invitation_availability(
&self,
viewer: &str,
) -> impl Future<Output = Result<InvitationAvailability, Report>> + Send;
- /// Batch query only the viewer's owned calendar copies.
+ /// Batch query only the viewer's owned or inbox-delegated calendar copies.
fn invitation_candidates(
&self,
viewer: &str,
@@ -97,7 +97,7 @@ pub trait CalendarInvitationRepository: Send + Sync {
}
/// Calendar connection/sync facts for explaining lookup misses.
pub struct InvitationAvailability {
- /// At least one active owned calendar account.
+ /// At least one active owned or delegated calendar account.
pub connected: bool,
/// At least one active account still syncing.
pub syncing: bool,
@@ -107,7 +107,7 @@ pub struct InvitationAvailability {
#[serde(tag = "kind", rename_all = "snake_case")]
#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
pub enum InvitationResolution {
- /// Viewer has no active owned Google-backed calendar.
+ /// Viewer has no active owned or delegated Google-backed calendar.
Disconnected,
/// An active calendar is still ingesting this event.
StillSyncing,
@@ -117,7 +117,7 @@ pub enum InvitationResolution {
Unavailable,
/// No independently authorized matching copy has synced.
NoMatch,
- /// More than one owned account could respond; never guess.
+ /// More than one authorized account could respond; never guess.
Ambiguous,
/// Current authorized event and explicit capabilities.
Resolved {
diff --git a/crates/calendar_events/src/outbound/pg/invitations.rs b/crates/calendar_events/src/outbound/pg/invitations.rs
index b52b451fb6..49860dcaa0 100644
--- a/crates/calendar_events/src/outbound/pg/invitations.rs
+++ b/crates/calendar_events/src/outbound/pg/invitations.rs
@@ -12,7 +12,10 @@ impl CalendarInvitationRepository for PgCalendarRepository {
let row = sqlx::query!(r#"SELECT count(*) > 0 AS "connected!",
COALESCE(bool_or(a.sync_status IN ('pending', 'syncing')), false) AS "syncing!"
FROM calendar_accounts a JOIN email_links l ON l.id = a.email_link_id
- WHERE a.owner_id = $1 AND l.macro_id = $1 AND a.sync_status NOT IN ('disabled', 'reauth_required')"#, viewer).fetch_one(&self.pool).await.map_err(report)?;
+ WHERE ((a.owner_id = $1 AND l.macro_id = $1)
+ OR EXISTS (SELECT 1 FROM macro_user_links delegation
+ WHERE delegation.primary_macro_id = $1 AND delegation.link_id = a.email_link_id))
+ AND a.sync_status NOT IN ('disabled', 'reauth_required')"#, viewer).fetch_one(&self.pool).await.map_err(report)?;
Ok(InvitationAvailability {
connected: row.connected,
syncing: row.syncing,
@@ -42,7 +45,10 @@ impl CalendarInvitationRepository for PgCalendarRepository {
CROSS JOIN LATERAL (
SELECT e.id, e.source_link_id, l.email_address, e.sequence, e.status, e.updated_at
FROM calendar_events e JOIN email_links l ON l.id = e.source_link_id
- WHERE e.ical_uid = requested.uid AND e.owner_id = $1 AND l.macro_id = $1
+ WHERE e.ical_uid = requested.uid
+ AND ((e.owner_id = $1 AND l.macro_id = $1)
+ OR EXISTS (SELECT 1 FROM macro_user_links delegation
+ WHERE delegation.primary_macro_id = $1 AND delegation.link_id = e.source_link_id))
AND EXISTS (SELECT 1 FROM calendar_event_sources s JOIN calendar_accounts a ON a.id = s.account_id JOIN calendars c ON c.id = s.calendar_id WHERE s.event_id = e.id AND s.source_kind = 'google' AND NOT c.is_deleted AND a.sync_status NOT IN ('disabled', 'reauth_required'))
ORDER BY e.id LIMIT 16
) candidate
@@ -107,6 +113,8 @@ impl CalendarInvitationRepository for PgCalendarRepository {
ON override.event_id = occurrence.event_id
AND override.recurrence_id = occurrence.recurrence_id
WHERE event.owner_id = $1
+ OR EXISTS (SELECT 1 FROM macro_user_links delegation
+ WHERE delegation.primary_macro_id = $1 AND delegation.link_id = event.source_link_id)
"#,
viewer,
&ids,
diff --git a/crates/calendar_events/src/outbound/pg/test/invitations.rs b/crates/calendar_events/src/outbound/pg/test/invitations.rs
index 55d95d464b..30ce2d9eb5 100644
--- a/crates/calendar_events/src/outbound/pg/test/invitations.rs
+++ b/crates/calendar_events/src/outbound/pg/test/invitations.rs
@@ -1,7 +1,7 @@
use super::*;
use crate::domain::invitations::{
- CalendarInvitationResolver, CalendarInvitationService, InvitationIdentity,
- InvitationResolution, InvitationRevision,
+ CalendarInvitationRepository, CalendarInvitationResolver, CalendarInvitationService,
+ InvitationIdentity, InvitationResolution, InvitationRevision,
};
fn revision(sequence: u32, cancelled: bool) -> InvitationRevision {
@@ -96,6 +96,129 @@ async fn invitation_accounts_are_authorized_and_ambiguity_never_guesses(pool: Pg
));
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn invitation_delegation_is_scoped_to_the_granted_inbox(pool: PgPool) {
+ let viewer = "macro|[email protected]";
+ let owner = "macro|[email protected]";
+ insert_user(&pool, viewer).await;
+ insert_user(&pool, owner).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let delegated_link = insert_link(&pool, owner).await;
+ let private_link = insert_link(&pool, owner).await;
+ let delegated_event = event(&pool, &repo, owner, delegated_link).await;
+ // The same owner and UID must not expose another inbox or cause ambiguity.
+ event(&pool, &repo, owner, private_link).await;
+ sqlx::query!(
+ r#"
+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
+ VALUES ($1, $2, $3)
+ "#,
+ viewer,
+ owner,
+ delegated_link,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let email = sqlx::query_scalar!(
+ "SELECT email_address FROM email_links WHERE id = $1",
+ delegated_link
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ let resolver = CalendarInvitationResolver::new(repo);
+ for preferred_link in [delegated_link, Uuid::nil()] {
+ let result = resolver
+ .resolve(viewer, &[identity(preferred_link)])
+ .await
+ .unwrap();
+ assert!(
+ matches!(&result[0], InvitationResolution::Resolved {
+ event, responding_email, can_respond: true, can_join: true, ..
+ } if event.id == delegated_event && responding_email == &email),
+ "{result:?}"
+ );
+ }
+ assert!(matches!(
+ resolver
+ .resolve("macro|[email protected]", &[identity(delegated_link)])
+ .await
+ .unwrap()[0],
+ InvitationResolution::Disconnected
+ ));
+
+ sqlx::query!(
+ "DELETE FROM macro_user_links WHERE primary_macro_id = $1 AND link_id = $2",
+ viewer,
+ delegated_link,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert!(matches!(
+ resolver
+ .resolve(viewer, &[identity(delegated_link)])
+ .await
+ .unwrap()[0],
+ InvitationResolution::Disconnected
+ ));
+
+ // Having an active account of one's own must not bypass the revoked grant.
+ let own_link = insert_link(&pool, viewer).await;
+ persist_complete_grant(&pool, own_link, 1).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ provider_ids(&repo, own_link).await;
+ let candidates = repo
+ .invitation_candidates(viewer, &[identity(delegated_link)])
+ .await
+ .unwrap();
+ assert!(candidates[0].is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn invitation_delegation_preserves_connection_and_sync_states(pool: PgPool) {
+ let viewer = "macro|[email protected]";
+ let owner = "macro|[email protected]";
+ insert_user(&pool, viewer).await;
+ insert_user(&pool, owner).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let link = insert_link(&pool, owner).await;
+ persist_complete_grant(&pool, link, 1).await;
+ provider_ids(&repo, link).await;
+ sqlx::query!(
+ r#"
+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
+ VALUES ($1, $2, $3)
+ "#,
+ viewer,
+ owner,
+ link,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let resolver = CalendarInvitationResolver::new(repo);
+ for (status, expected) in [
+ ("pending", "still_syncing"),
+ ("syncing", "still_syncing"),
+ ("ready", "no_match"),
+ ("disabled", "disconnected"),
+ ("reauth_required", "disconnected"),
+ ] {
+ sqlx::query!(
+ "UPDATE calendar_accounts SET sync_status = $1 WHERE email_link_id = $2",
+ status,
+ link,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let result = resolver.resolve(viewer, &[identity(link)]).await.unwrap();
+ assert_eq!(serde_json::to_value(&result[0]).unwrap()["kind"], expected);
+ }
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn invitation_moved_instance_uses_original_key_and_instance_revision(pool: PgPool) {
let viewer = "macro|[email protected]";
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index b457ced11a..74eb0a7826 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -2830,6 +2830,9 @@ A newly arrived scheduling update shows RSVP and Join only once its calendar sta
been checked. A series invitation shows its current or next live occurrence.
A connected, resolved invitation shows the responding address and Yes / Maybe / No.
+Calendars connected through a linked inbox also resolve here, using that inbox's
+address for responses. Access is limited to the delegated inbox; another inbox owned
+by the same account stays private, and removing the link removes invitation access.
Local verification requires both email and calendar services: email supplies saved
snapshots and resolves them against the synced calendar, while calendar service handles
the RSVP write. With calendar service's `CALENDAR_SYNC_ENABLED` off, its RSVP route is
diff --git a/packages/sdk/specs/email.json b/packages/sdk/specs/email.json
index cab48eb8bc..770718cd9a 100644
--- a/packages/sdk/specs/email.json
+++ b/packages/sdk/specs/email.json
@@ -5016,7 +5016,7 @@
"oneOf": [
{
"type": "object",
- "description": "Viewer has no active owned Google-backed calendar.",
+ "description": "Viewer has no active owned or delegated Google-backed calendar.",
"required": ["kind"],
"properties": {
"kind": {
@@ -5071,7 +5071,7 @@
},
{
"type": "object",
- "description": "More than one owned account could respond; never guess.",
+ "description": "More than one authorized account could respond; never guess.",
"required": ["kind"],
"properties": {
"kind": {