Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(calendar): import Outlook series whose meetings moved (#9915)
gbirmanca8921ed11a4
diff --git a/.sqlx/query-af941174afa6b6693f428cc0626d9502c9875b0ed8b45ce9f64680459e6f33eb.json b/.sqlx/query-af941174afa6b6693f428cc0626d9502c9875b0ed8b45ce9f64680459e6f33eb.jsonnew file mode 100644index 0000000000..d9e120fc64--- /dev/null+++ b/.sqlx/query-af941174afa6b6693f428cc0626d9502c9875b0ed8b45ce9f64680459e6f33eb.json@@ -0,0 +1,16 @@+{+ "db_name": "PostgreSQL",+ "query": "UPDATE calendar_outlook_members SET reader_revision=$3 WHERE work_id=$1 AND master_id=$2 AND reader_revision IS DISTINCT FROM $3",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Text",+ "Int2"+ ]+ },+ "nullable": []+ },+ "hash": "af941174afa6b6693f428cc0626d9502c9875b0ed8b45ce9f64680459e6f33eb"+}diff --git a/.sqlx/query-c0415ed6a6b9adb805cbdc0d1e080254672d43907b79f5f23e22d3c8e47e0009.json b/.sqlx/query-c0415ed6a6b9adb805cbdc0d1e080254672d43907b79f5f23e22d3c8e47e0009.jsonnew file mode 100644index 0000000000..2ff236a1b3--- /dev/null+++ b/.sqlx/query-c0415ed6a6b9adb805cbdc0d1e080254672d43907b79f5f23e22d3c8e47e0009.json@@ -0,0 +1,25 @@+{+ "db_name": "PostgreSQL",+ "query": "WITH unchecked AS(SELECT m.provider_id,m.master_id,EXISTS(SELECT 1 FROM calendar_event_sources s\n WHERE s.source_kind='outlook' AND s.account_id=$3 AND s.calendar_id=$4 AND s.provider_event_id=m.master_id) AS stored\n FROM calendar_outlook_members m WHERE m.work_id=$1 AND(m.reader_revision IS NULL OR m.reader_revision<$2)),\n stamped AS(UPDATE calendar_outlook_members m SET reader_revision=$2 FROM unchecked u\n WHERE m.work_id=$1 AND m.provider_id=u.provider_id AND u.stored)\n SELECT DISTINCT master_id AS \"master_id!\" FROM unchecked WHERE NOT stored",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "master_id!",+ "type_info": "Text"+ }+ ],+ "parameters": {+ "Left": [+ "Uuid",+ "Int2",+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": [+ false+ ]+ },+ "hash": "c0415ed6a6b9adb805cbdc0d1e080254672d43907b79f5f23e22d3c8e47e0009"+}diff --git a/crates/calendar_events/AGENTS.md b/crates/calendar_events/AGENTS.mdindex 6713e21eb5..c2594e3dbd 100644--- a/crates/calendar_events/AGENTS.md+++ b/crates/calendar_events/AGENTS.md@@ -376,7 +376,17 @@ reminder dispatch, team availability, and provider/Postgres persistence.page membership stays so later tombstones resolve. The round still finishesas a clean sync. The warning `skipping an Outlook event Macro cannot read`and the `calendar.outlook_sync` span carry `calendar_id`,- `provider_event_id` and `rejected_field`. Discovery does not fail on a+ `provider_event_id` and `rejected_field`. A series imports without an+ occurrence the adapter cannot read (`OutlookEventRefresh::Refreshed`'s+ `skipped`), warning `skipping an Outlook occurrence Macro cannot read` with+ its `occurrence_id`, and the span adds `skipped_occurrences`. A secondary+ calendar has no v1.0 delta, so each of its rounds lists and reads every+ event again. A delta round lists only changes, so it also reads again the+ members with no stored copy that an older `READER_REVISION` read (stamped+ in `calendar_outlook_members.reader_revision`, NULL from builds before+ it; a member found with a stored copy is stamped instead): a parser fix brings skipped events back without anyone touching+ them, and a skip costs no read until the event changes or the revision+ rises. Discovery does not fail on amailbox time zone Macro cannot map to IANA either: `OutlookDiscovery`stores the account's calendars with no `time_zone`, which every readertreats as unknown, as it does a Google calendar's zone it cannot readdiff --git a/crates/calendar_events/src/domain/outlook.rs b/crates/calendar_events/src/domain/outlook.rsindex 54d4c1a266..5063f261c9 100644--- a/crates/calendar_events/src/domain/outlook.rs+++ b/crates/calendar_events/src/domain/outlook.rs@@ -34,6 +34,9 @@ pub struct OutlookCalendarLease {pub page_loaded: bool,/// Opaque current-page URL.pub page_cursor: Option<String>,+ /// [`OutlookCalendarReader::READER_REVISION`] of the worker holding the+ /// lease, which every event it reads is stamped with.+ pub reader_revision: i16,}/// Complete discovery result for a provider calendar.@@ -79,7 +82,8 @@ pub enum OutlookCalendarProgress {},/// The event exists but Macro cannot read it. Retire any copy stored/// before, which would show a version Microsoft no longer has, but keep- /// the event's page membership so later tombstones still resolve.+ /// the event's page membership so later tombstones still resolve. A+ /// reader of a later revision reads it again on its next delta round.Unreadable {/// Master/single ID from this lease's pending work.id: String,@@ -92,8 +96,15 @@ pub enum OutlookCalendarProgress {/// One provider event read back for synchronization.pub enum OutlookEventRefresh {- /// The current event or complete series, normalized.- Refreshed(Box<CalendarEventUpsert>),+ /// The current event or series, normalized.+ Refreshed {+ /// What the calendar stores.+ upsert: Box<CalendarEventUpsert>,+ /// Occurrences of the series Macro could not read and left out, as+ /// Google sync never shows what it cannot normalize. The rest of the+ /// series is still imported.+ skipped: Vec<SkippedOccurrence>,+ },/// Microsoft no longer has the event on this calendar.Gone,/// Microsoft has the event, but Macro cannot represent it. The sync skips@@ -106,8 +117,22 @@ pub enum OutlookEventRefresh {},}+/// An occurrence left out of an imported series.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SkippedOccurrence {+ /// Graph's id of the occurrence.+ pub provider_id: String,+ /// Path of the Graph property Macro rejected, such as+ /// `instances.originalStart.missing`.+ pub field: String,+}+/// Provider reads with normalized results and no persistence policy.pub trait OutlookCalendarReader: Send + Sync + 'static {+ /// Revision of the normalization. Raise it with a change that lets Macro+ /// read events an older revision skipped: each delta calendar then reads+ /// those events again once, on its next round.+ const READER_REVISION: i16;/// Discover all visible calendars, following every page.fn calendars(&self,@@ -133,11 +158,13 @@ pub trait OutlookCalendarReader: Send + Sync + 'static {/// Calendar-owned persistence and exact-generation leases.pub trait OutlookCalendarRepository: CalendarRepository {- /// Reconcile grant capability and claim one due job atomically.+ /// Reconcile grant capability and claim one due job atomically for a+ /// worker reading at `reader_revision`.fn claim_outlook_calendar(&self,lease_id: Uuid,range: OccurrenceRange,+ reader_revision: i16,) -> impl Future<Output = Result<Option<OutlookCalendarLease>, Report>> + Send;/// Extend a live lease, failing after disconnect or custody change.fn renew_outlook_calendar(@@ -210,6 +237,7 @@ impl<.claim_outlook_calendar(Uuid::now_v7(),OccurrenceRange::maintenance_horizon(chrono::Utc::now()),+ P::READER_REVISION,).await?else {@@ -240,6 +268,7 @@ impl<provider_event_id = tracing::field::Empty,outcome = tracing::field::Empty,rejected_field = tracing::field::Empty,+ skipped_occurrences = tracing::field::Empty,unmapped_time_zone = tracing::field::Empty,error_kind = tracing::field::Empty,),@@ -274,8 +303,21 @@ impl<.refresh_calendar_event(&token, target, &id).await?{- OutlookEventRefresh::Refreshed(upsert) => {+ OutlookEventRefresh::Refreshed { upsert, skipped } => {span.record("outcome", "refreshed");+ if let Some(first) = skipped.first() {+ span.record("rejected_field", first.field.as_str());+ span.record("skipped_occurrences", skipped.len() as i64);+ }+ for occurrence in &skipped {+ tracing::warn!(+ calendar_id = %target.calendar_id,+ provider_event_id = %id,+ occurrence_id = %occurrence.provider_id,+ rejected_field = %occurrence.field,+ "skipping an Outlook occurrence Macro cannot read"+ );+ }self.repository.upsert_event(CalendarEventWrite::OutlookSync {lease: Box::new(lease.clone()),diff --git a/crates/calendar_events/src/outbound/pg/outlook.rs b/crates/calendar_events/src/outbound/pg/outlook.rsindex ddf69e0359..b62b1dce5d 100644--- a/crates/calendar_events/src/outbound/pg/outlook.rs+++ b/crates/calendar_events/src/outbound/pg/outlook.rs@@ -144,20 +144,47 @@ impl OutlookCalendarRepository for PgCalendarRepository {SELECT $1,rows.id,rows.master,$4 FROM unnest($2::text[],$3::text[]) rows(id,master)ON CONFLICT(work_id,provider_id) DO UPDATE SET master_id=EXCLUDED.master_id,scan_id=EXCLUDED.scan_id"#,lease.id,&ids,&masters,state.scan_id).execute(&mut *tx).await.map_err(report)?;+ sqlx::query!("DELETE FROM calendar_outlook_members WHERE work_id=$1 AND(provider_id=ANY($2::text[]) OR master_id=ANY($2::text[]))",lease.id,&page.removed).execute(&mut *tx).await.map_err(report)?;+ // A full scan reads every listed event anyway. A delta+ // round lists only changes, so it also reads the events+ // an older reader skipped, once per reader revision. One+ // with a stored copy was read, so it is stamped instead+ // and later pages do not check it again.+ let skipped_before = if state.full_scan {+ vec![]+ } else {+ sqlx::query_scalar!(+ r#"WITH unchecked AS(SELECT m.provider_id,m.master_id,EXISTS(SELECT 1 FROM calendar_event_sources s+ WHERE s.source_kind='outlook' AND s.account_id=$3 AND s.calendar_id=$4 AND s.provider_event_id=m.master_id) AS stored+ FROM calendar_outlook_members m WHERE m.work_id=$1 AND(m.reader_revision IS NULL OR m.reader_revision<$2)),+ stamped AS(UPDATE calendar_outlook_members m SET reader_revision=$2 FROM unchecked u+ WHERE m.work_id=$1 AND m.provider_id=u.provider_id AND u.stored)+ SELECT DISTINCT master_id AS "master_id!" FROM unchecked WHERE NOT stored"#,+ lease.id,+ lease.reader_revision,+ lease.account_id,+ target.calendar_id+ )+ .fetch_all(&mut *tx)+ .await+ .map_err(report)?+ };let pending: Vec<_> = masters.into_iter().chain(removed.into_iter().filter_map(|r| r.master_id))+ .chain(skipped_before).collect::<std::collections::BTreeSet<_>>().into_iter().collect();- sqlx::query!("DELETE FROM calendar_outlook_members WHERE work_id=$1 AND(provider_id=ANY($2::text[]) OR master_id=ANY($2::text[]))",lease.id,&page.removed).execute(&mut *tx).await.map_err(report)?;sqlx::query!(r#"UPDATE calendar_outlook_work SET page_loaded=true,pending_ids=$2,next_page=$3,terminal_cursor=$4,visited_pages=array_append(visited_pages,COALESCE(page_cursor,'initial')) WHERE id=$1"#,lease.id,&pending,page.next,page.delta).execute(&mut *tx).await.map_err(report)?;}}OutlookCalendarProgress::Event { id, exists } => {acknowledge_event(&state.pending_ids, state.page_loaded, &id)?;- if !exists {+ if exists {+ stamp_reader(&mut tx, lease, &id).await?;+ } else {retire_provider_event(&mut tx, &mut log, target.calendar_id, &id).await?;sqlx::query!("DELETE FROM calendar_outlook_members WHERE work_id=$1 AND master_id=$2",@@ -172,6 +199,7 @@ impl OutlookCalendarRepository for PgCalendarRepository {}OutlookCalendarProgress::Unreadable { id } => {acknowledge_event(&state.pending_ids, state.page_loaded, &id)?;+ stamp_reader(&mut tx, lease, &id).await?;retire_provider_event(&mut tx, &mut log, target.calendar_id, &id).await?;remove_pending(&mut tx, lease.id, &id).await?;}@@ -228,6 +256,7 @@ impl OutlookCalendarRepository for PgCalendarRepository {&self,lease_id: Uuid,range: OccurrenceRange,+ reader_revision: i16,) -> Result<Option<OutlookCalendarLease>, Report> {let mut tx = self.pool.begin().await.map_err(report)?;let mut log = ChangeLogBatch::default();@@ -335,6 +364,7 @@ impl OutlookCalendarRepository for PgCalendarRepository {pending_ids: row.pending_ids,page_loaded: row.page_loaded,page_cursor: row.page_cursor,+ reader_revision,}))}async fn renew_outlook_calendar(&self, lease: &OutlookCalendarLease) -> Result<(), Report> {@@ -491,6 +521,23 @@ async fn retire_provider_event(Ok(())}+async fn stamp_reader(+ tx: &mut Transaction<'_, Postgres>,+ lease: &OutlookCalendarLease,+ id: &str,+) -> Result<(), Report> {+ sqlx::query!(+ "UPDATE calendar_outlook_members SET reader_revision=$3 WHERE work_id=$1 AND master_id=$2 AND reader_revision IS DISTINCT FROM $3",+ lease.id,+ id,+ lease.reader_revision+ )+ .execute(&mut **tx)+ .await+ .map_err(report)?;+ Ok(())+}+async fn remove_pending(tx: &mut Transaction<'_, Postgres>,work_id: Uuid,diff --git a/crates/calendar_events/src/outbound/pg/test/outlook.rs b/crates/calendar_events/src/outbound/pg/test/outlook.rsindex 5c14bf9983..9ff64f30ae 100644--- a/crates/calendar_events/src/outbound/pg/test/outlook.rs+++ b/crates/calendar_events/src/outbound/pg/test/outlook.rs@@ -15,10 +15,17 @@ async fn mailbox(pool: &PgPool) -> (PgCalendarRepository, Uuid, String) {sqlx::query!("INSERT INTO email_link_microsoft_scopes(link_id,grant_generation,granted_scopes) VALUES($1,1,ARRAY['Calendars.ReadWrite'])",link).execute(pool).await.unwrap();(PgCalendarRepository::new(pool.clone()), link, owner)}+/// The reader revision of the tests' worker.+const READER_REVISION: i16 = 1;+async fn claim(repo: &PgCalendarRepository) -> Option<OutlookCalendarLease> {+ claim_at(repo, READER_REVISION).await+}+async fn claim_at(repo: &PgCalendarRepository, revision: i16) -> Option<OutlookCalendarLease> {repo.claim_outlook_calendar(Uuid::now_v7(),OccurrenceRange::maintenance_horizon(Utc::now()),+ revision,).await.unwrap()@@ -683,6 +690,139 @@ async fn tombstones_refresh_known_series_and_reset_unknown_history(pool: PgPool)assert!(!reset.page_loaded);}+fn delta_page(cursor: &str) -> OutlookCalendarProgress {+ OutlookCalendarProgress::Page(OutlookCalendarPage {+ members: vec![],+ removed: vec![],+ next: None,+ delta: Some(cursor.into()),+ })+}++async fn finish_round(repo: &PgCalendarRepository, pool: &PgPool, revision: i16) {+ let lease = claim_at(repo, revision).await.unwrap();+ assert!(lease.pending_ids.is_empty());+ repo.checkpoint_outlook_calendar(&lease, OutlookCalendarProgress::Finish)+ .await+ .unwrap();+ sqlx::query!(+ "UPDATE calendar_outlook_work SET next_run_at=now() WHERE id=$1",+ lease.id+ )+ .execute(pool)+ .await+ .unwrap();+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_delta_round_reads_again_only_events_an_older_reader_skipped(pool: PgPool) {+ let (repo, _, _) = mailbox(&pool).await;+ let lease = calendar(&repo).await;+ repo.checkpoint_outlook_calendar(+ &lease,+ OutlookCalendarProgress::Page(OutlookCalendarPage {+ members: vec![+ ("kept".into(), "provider-kept".into()),+ ("skipped-occurrence".into(), "provider-skipped".into()),+ ("unstamped-occurrence".into(), "provider-unstamped".into()),+ ],+ removed: vec![],+ next: None,+ delta: Some("baseline".into()),+ }),+ )+ .await+ .unwrap();+ let event = claim(&repo).await.unwrap();+ store(&repo, &event, "kept").await;+ repo.checkpoint_outlook_calendar(+ &event,+ OutlookCalendarProgress::Event {+ id: "provider-kept".into(),+ exists: true,+ },+ )+ .await+ .unwrap();+ for id in ["provider-skipped", "provider-unstamped"] {+ let event = claim(&repo).await.unwrap();+ repo.checkpoint_outlook_calendar(+ &event,+ OutlookCalendarProgress::Unreadable { id: id.into() },+ )+ .await+ .unwrap();+ }+ finish_round(&repo, &pool, READER_REVISION).await;+ // What a build before reader revisions left behind.+ sqlx::query!("UPDATE calendar_outlook_members SET reader_revision=NULL WHERE master_id IN ('provider-kept','provider-unstamped')")+ .execute(&pool)+ .await+ .unwrap();++ let delta = claim(&repo).await.unwrap();+ repo.checkpoint_outlook_calendar(&delta, delta_page("second"))+ .await+ .unwrap();+ let reread = claim(&repo).await.unwrap();+ assert_eq!(+ reread.pending_ids,+ ["provider-unstamped"],+ "a stored event and one this revision skipped are not read again"+ );+ let kept = sqlx::query_scalar!(+ "SELECT reader_revision FROM calendar_outlook_members WHERE master_id='provider-kept'"+ )+ .fetch_one(&pool)+ .await+ .unwrap();+ assert_eq!(+ kept,+ Some(READER_REVISION),+ "a stored event is stamped as it is checked, so later pages skip it"+ );+ repo.checkpoint_outlook_calendar(+ &reread,+ OutlookCalendarProgress::Unreadable {+ id: "provider-unstamped".into(),+ },+ )+ .await+ .unwrap();+ finish_round(&repo, &pool, READER_REVISION).await;++ let delta = claim(&repo).await.unwrap();+ repo.checkpoint_outlook_calendar(&delta, delta_page("third"))+ .await+ .unwrap();+ finish_round(&repo, &pool, READER_REVISION).await;++ let later = READER_REVISION + 1;+ let delta = claim_at(&repo, later).await.unwrap();+ repo.checkpoint_outlook_calendar(&delta, delta_page("fourth"))+ .await+ .unwrap();+ let reread = claim_at(&repo, later).await.unwrap();+ assert_eq!(+ reread.pending_ids,+ ["provider-skipped", "provider-unstamped"],+ "a later revision reads again what older ones skipped"+ );++ repo.checkpoint_outlook_calendar(&reread, OutlookCalendarProgress::Reset)+ .await+ .unwrap();+ let scan = claim_at(&repo, later).await.unwrap();+ repo.checkpoint_outlook_calendar(&scan, delta_page("rescanned"))+ .await+ .unwrap();+ let listed = claim_at(&repo, later).await.unwrap();+ assert!(+ listed.page_loaded && listed.pending_ids.is_empty(),+ "a full scan reads only what it lists"+ );+}+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]async fn reconnect_clears_only_the_replaced_credentials_reauth_state(pool: PgPool) {let (repo, link, _) = mailbox(&pool).await;diff --git a/crates/email_api_client/AGENTS.md b/crates/email_api_client/AGENTS.mdindex cc53396d2b..ab31f269f1 100644--- a/crates/email_api_client/AGENTS.md+++ b/crates/email_api_client/AGENTS.md@@ -109,7 +109,16 @@ Outlook (Microsoft Graph) outbound adapters.`recurrence.rs`) fails with `Rejected`, which names the Graph propertypath (`recurrence.range.endDate`, `instances.start.dateTime`), built onlyfrom literals so its log carries no event content, so the sync can skip and- log that one event. Give a new check a `Rejected`, not `invalid()`. Graph names UTC `tzone://Microsoft/Utc` on meetings Exchange+ log that one event. Give a new check a `Rejected`, not `invalid()`. Graph+ leaves `originalStart` out of `instances` unless it is selected, so the+ request selects `INSTANCE_PROPERTIES` (Graph's default `event` properties+ plus `originalStart`); a new read of an instance property must be added+ there, since a `$select` returns nothing else. An exception still without+ one is left out of its series and reported as a `SkippedOccurrence`+ (`instances.originalStart.missing`); one Macro cannot parse rejects the+ series at `instances.originalStart`. Raise `READER_REVISION` with a change+ that lets Macro read events it skipped before, so delta calendars read+ them again. Graph names UTC `tzone://Microsoft/Utc` on meetings Exchangeimported from another system's invitation, which `zones::zone` maps; aseries end date in a legacy custom zone (`tzone://Microsoft/Custom`) isread in the zone the event's time is kept in. Calendar discovery reads thediff --git a/crates/email_api_client/src/outbound/outlook/calendar.rs b/crates/email_api_client/src/outbound/outlook/calendar.rsindex 97a3e42756..1bccdd4955 100644--- a/crates/email_api_client/src/outbound/outlook/calendar.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar.rs@@ -28,6 +28,55 @@ const CREATION_ID: &str =const APPOINTMENT_SEQUENCE_ID: &str = "Integer {00062002-0000-0000-C000-000000000046} Id 0x8201";const PREFERENCES_ID: &str ="String {81f532e3-7ace-4e0b-bae9-312ce0d50c8c} Name MacroCalendarPreferences";+/// What `instances` selects: every property of Graph's v1.0 `event` that it+/// returns by default, plus `originalStart`, which it leaves out of the+/// default set although its documentation says otherwise. Without it an+/// exception cannot be paired with the occurrence it replaces, nor an+/// occurrence addressed by its original start. A `$select` returns only what+/// it names, so a new read of an instance property must be listed here.+const INSTANCE_PROPERTIES: &[&str] = &[+ "allowNewTimeProposals",+ "attendees",+ "body",+ "bodyPreview",+ "categories",+ "changeKey",+ "createdDateTime",+ "end",+ "hasAttachments",+ "hideAttendees",+ "iCalUId",+ "id",+ "importance",+ "isAllDay",+ "isCancelled",+ "isDraft",+ "isOnlineMeeting",+ "isOrganizer",+ "isReminderOn",+ "lastModifiedDateTime",+ "location",+ "locations",+ "onlineMeeting",+ "onlineMeetingProvider",+ "onlineMeetingUrl",+ "organizer",+ "originalEndTimeZone",+ "originalStart",+ "originalStartTimeZone",+ "recurrence",+ "reminderMinutesBeforeStart",+ "responseRequested",+ "responseStatus",+ "sensitivity",+ "seriesMasterId",+ "showAs",+ "start",+ "subject",+ "transactionId",+ "type",+ "webLink",+];fn invalid() -> CalendarProviderError {CalendarProviderError::new(@@ -230,6 +279,7 @@ impl OutlookApiClientRepository {.append_pair("startDateTime", &target.range.starts_at.to_rfc3339()).append_pair("endDateTime", &target.range.ends_at.to_rfc3339()).append_pair("$top", "1000")+ .append_pair("$select", &INSTANCE_PROPERTIES.join(",")).append_pair("$expand",&format!(@@ -258,7 +308,7 @@ impl OutlookApiClientRepository {master: Value,) -> Result<CalendarEventUpsert, CalendarProviderError> {let instances = self.calendar_occurrences(token, target, &master).await?;- Ok(normalize::projection(target, master, instances)?)+ Ok(normalize::projection(target, master, instances)?.upsert)}async fn calendar_refresh(&self,@@ -332,6 +382,9 @@ impl OutlookApiClientRepository {}impl OutlookCalendarReader for OutlookApiClientRepository {+ // 1: instances select `originalStart`, so exceptions pair with their+ // occurrences instead of hiding their series.+ const READER_REVISION: i16 = 1;async fn calendars(&self,token: &str,@@ -466,7 +519,10 @@ impl OutlookCalendarReader for OutlookApiClientRepository {};let instances = api.calendar_occurrences(&token, target, &master).await?;Ok(match normalize::projection(target, master, instances) {- Ok(upsert) => OutlookEventRefresh::Refreshed(Box::new(upsert)),+ Ok(normalize::Projection { upsert, skipped }) => OutlookEventRefresh::Refreshed {+ upsert: Box::new(upsert),+ skipped,+ },Err(Rejected(field)) => OutlookEventRefresh::Unreadable { field },})}@@ -929,7 +985,7 @@ impl CalendarMutationProvider for OutlookApiClientRepository {}// Outlook may remove a declined event from the attendee's calendar.// The successful response must not be reported as a failed RSVP.- let mut echo = normalize::projection(target, master, vec![])?;+ let mut echo = normalize::projection(target, master, vec![])?.upsert;for attendee in &mut echo.event.attendees {if attendee.email.eq_ignore_ascii_case(address) {attendee.response_status = response;diff --git a/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs b/crates/email_api_client/src/outbound/outlook/calendar/normalize.rsindex 0d7b637d57..b621fe7a33 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/normalize.rs@@ -3,6 +3,15 @@ use chrono::{DateTime, NaiveDate, NaiveDateTime, TimeZone, Utc};const DECLINE_FIELD: &str = "singleValueExtendedProperties.automaticDecline";const INSTANCE_DECLINE_FIELD: &str = "instances.singleValueExtendedProperties.automaticDecline";+/// An exception without the start of the occurrence it replaces. An+/// `originalStart` Macro cannot parse is `instances.originalStart` instead.+pub(super) const UNPAIRED_EXCEPTION_FIELD: &str = "instances.originalStart.missing";++/// A normalized event, and the occurrences of its series left out of it.+pub(super) struct Projection {+ pub(super) upsert: CalendarEventUpsert,+ pub(super) skipped: Vec<SkippedOccurrence>,+}#[cfg(test)]mod test;@@ -178,7 +187,7 @@ pub(super) fn projection(target: &ProviderCalendarTarget,master: Value,instances: Vec<Value>,-) -> Result<CalendarEventUpsert, Rejected> {+) -> Result<Projection, Rejected> {let event_id = Uuid::now_v7();let time = event_time(&master)?;let updated = master["lastModifiedDateTime"]@@ -258,6 +267,7 @@ pub(super) fn projection(};let mut occurrences = std::collections::BTreeMap::new();let mut overrides = vec![];+ let mut skipped = vec![];for instance in instances {if instance["isCancelled"].as_bool() == Some(true) {continue;@@ -266,7 +276,19 @@ pub(super) fn projection(let original = original(&instance).map_err(|e| e.under("instances"))?;let recurrence_id = original.as_ref().map(EventStart::occurrence_key);if instance["type"].as_str() == Some("exception") {- let original_time = original.ok_or_else(|| Rejected::at("instances.originalStart"))?;+ // Neither its slot nor its own details can be told apart from+ // the series, so it is left out, as Google sync never shows an+ // exception without its original start, and the rest of the+ // series still imports.+ let Some(original_time) = original else {+ skipped.push(SkippedOccurrence {+ provider_id: field(&instance, "id")+ .map_err(|e| e.under("instances"))?+ .into(),+ field: UNPAIRED_EXCEPTION_FIELD.into(),+ });+ continue;+ };let instance_preferences = preferences(&instance);// Graph may omit inherited extended properties. Preserve the series'// extra reminders unless this instance carries its own settings or@@ -319,7 +341,7 @@ pub(super) fn projection();}}- Ok(CalendarEventUpsert {+ let upsert = CalendarEventUpsert {event,source: CalendarEventSource::Outlook(ProviderEventSource {automatic_decline: if master["showAs"].as_str() == Some("oof") {@@ -344,7 +366,8 @@ pub(super) fn projection(}),overrides,occurrences: occurrences.into_values().collect(),- })+ };+ Ok(Projection { upsert, skipped })}pub(super) fn preferences(event: &Value) -> Value {diff --git a/crates/email_api_client/src/outbound/outlook/calendar/normalize/test.rs b/crates/email_api_client/src/outbound/outlook/calendar/normalize/test.rsindex 9b5d8b4285..b65f6e797c 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/normalize/test.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/normalize/test.rs@@ -18,7 +18,9 @@ fn sensitivity_maps_consistently_for_series_and_exceptions() {] {let mut master = event("series");master["sensitivity"] = json!(sensitivity);- let series = projection(&target(), master.clone(), vec![]).unwrap();+ let series = projection(&target(), master.clone(), vec![])+ .unwrap()+ .upsert;assert_eq!(series.event.visibility, expected);// Explicit normal must replace a private series; private exceptions@@ -30,7 +32,9 @@ fn sensitivity_maps_consistently_for_series_and_exceptions() {};let mut instance = exception();instance["sensitivity"] = json!(sensitivity);- let mut upsert = projection(&target(), master, vec![instance]).unwrap();+ let mut upsert = projection(&target(), master, vec![instance])+ .unwrap()+ .upsert;assert_eq!(upsert.overrides[0].visibility, Some(expected));upsert.overrides[0].apply_to(&mut upsert.event);assert_eq!(upsert.event.visibility, expected);@@ -49,7 +53,9 @@ fn show_as_maps_consistently_for_series_and_exceptions() {] {let mut master = event("series");master["showAs"] = json!(show_as);- let series = projection(&target(), master.clone(), vec![]).unwrap();+ let series = projection(&target(), master.clone(), vec![])+ .unwrap()+ .upsert;assert_eq!(series.event.transparency, expected);master["showAs"] = if expected == EventTransparency::Opaque {@@ -59,7 +65,9 @@ fn show_as_maps_consistently_for_series_and_exceptions() {};let mut instance = exception();instance["showAs"] = json!(show_as);- let mut upsert = projection(&target(), master, vec![instance]).unwrap();+ let mut upsert = projection(&target(), master, vec![instance])+ .unwrap()+ .upsert;assert_eq!(upsert.overrides[0].transparency, Some(expected));upsert.overrides[0].apply_to(&mut upsert.event);assert_eq!(upsert.event.transparency, expected);@@ -73,7 +81,9 @@ fn omitted_exception_fields_inherit_series_privacy_and_availability() {let mut master = event("series");master["sensitivity"] = json!(privacy);master["showAs"] = json!(show_as);- let mut upsert = projection(&target(), master, vec![exception()]).unwrap();+ let mut upsert = projection(&target(), master, vec![exception()])+ .unwrap()+ .upsert;let visibility = upsert.event.visibility;let transparency = upsert.event.transparency;assert_eq!(upsert.overrides[0].visibility, None);@@ -92,7 +102,7 @@ fn source_retains_the_observed_role_without_inferring_it_from_editability() {let mut target = target();target.observed_access_role = role.map(str::to_owned);target.is_read_only = is_read_only;- let upsert = projection(&target, event("event"), vec![]).unwrap();+ let upsert = projection(&target, event("event"), vec![]).unwrap().upsert;assert_eq!(upsert.source.details().observed_access_role,target.observed_access_rolediff --git a/crates/email_api_client/src/outbound/outlook/calendar/test.rs b/crates/email_api_client/src/outbound/outlook/calendar/test.rsindex 6c65557bc3..ad1b039514 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/test.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/test.rs@@ -35,6 +35,183 @@ pub(super) fn target() -> ProviderCalendarTarget {pub(super) fn event(id: &str) -> Value {json!({"id":id,"@odata.etag":"W/\"1\"","iCalUId":format!("{id}@outlook"),"subject":"Meeting","type":"singleInstance","createdDateTime":"2026-01-01T10:00:00Z","lastModifiedDateTime":"2026-10-01T10:00:00Z","originalStartTimeZone":"Pacific Standard Time","start":{"dateTime":"2026-11-02T17:00:00.0000000","timeZone":"UTC"},"end":{"dateTime":"2026-11-02T18:00:00.0000000","timeZone":"UTC"},"isAllDay":false,"attendees":[]})}+/// One instance as `GET /me/events/{id}/instances` returns it for `request`.+/// With a `$select`, Graph returns only the named properties, besides `id`,+/// annotations and what `$expand` names. Without one it returns its default+/// properties, which leave out `originalStart`: every stored occurrence of+/// the dev Outlook mailbox's series has no original start, which only+/// happens when Graph sends none.+pub(super) fn as_graph_returns(instance: &Value, request: &wiremock::Request) -> Value {+ let query: Vec<(String, String)> = request.url.query_pairs().into_owned().collect();+ let select: Option<Vec<&str>> = query+ .iter()+ .find(|(key, _)| key == "$select")+ .map(|(_, value)| value.split(',').collect());+ let expanded = query+ .iter()+ .any(|(key, value)| key == "$expand" && value.starts_with("singleValueExtendedProperties"));+ let returned = instance+ .as_object()+ .unwrap()+ .iter()+ .filter(|(key, _)| match key.as_str() {+ "id" => true,+ "singleValueExtendedProperties" => expanded,+ key if key.starts_with("@odata.") => true,+ key => match &select {+ Some(names) => names.contains(&key),+ None => key != "originalStart",+ },+ })+ .map(|(key, value)| (key.clone(), value.clone()))+ .collect();+ Value::Object(returned)+}++/// A series' `instances` endpoint that answers as Graph does (see+/// [`as_graph_returns`]).+pub(super) struct Instances(pub(super) Vec<Value>);++impl wiremock::Respond for Instances {+ fn respond(&self, request: &wiremock::Request) -> ResponseTemplate {+ let value: Vec<_> = self+ .0+ .iter()+ .map(|instance| as_graph_returns(instance, request))+ .collect();+ ResponseTemplate::new(200).set_body_json(json!({ "value": value }))+ }+}++/// An instance of Graph's documented weekly "Review strategy for Q3" series+/// (List instances, v1.0), with every default property of a v1.0 `event`.+/// `day` is its date, `original_day` the date of the slot it fills.+pub(super) fn documented_instance(id: &str, kind: &str, day: &str, original_day: &str) -> Value {+ let mut value = json!({+ "@odata.etag":"W/\"x3IAvB5fbUWf4XNcBFLNUwAAKuA3yQ==\"",+ "id":id,+ "createdDateTime":"2019-03-28T17:08:14.3474405Z",+ "lastModifiedDateTime":"2019-04-02T20:17:49.6339342Z",+ "changeKey":"x3IAvB5fbUWf4XNcBFLNUwAAKuA3yQ==",+ "categories":[],+ "transactionId":null,+ "originalStartTimeZone":"Pacific Standard Time",+ "originalEndTimeZone":"Pacific Standard Time",+ "iCalUId":"040000008200E00074C5B7101A82E00807E3040F2C5F84B3B5E5D401000000000000000010000000F4A8D0BB2AA6DC4FA5B57C6A0DBF2E3A",+ "reminderMinutesBeforeStart":15,+ "isReminderOn":true,+ "hasAttachments":false,+ "subject":"Review strategy for Q3",+ "bodyPreview":"",+ "importance":"normal",+ "sensitivity":"normal",+ });+ let rest = json!({+ "originalStart":format!("{original_day}T20:30:00Z"),+ "isAllDay":false,+ "isCancelled":false,+ "isOrganizer":false,+ "responseRequested":true,+ "seriesMasterId":"AAMkAGUzYRgWAAA=",+ "showAs":"busy",+ "type":kind,+ "webLink":format!("https://outlook.office365.com/owa/?itemid={id}&exvsurl=1&path=/calendar/item"),+ "onlineMeetingUrl":null,+ "isOnlineMeeting":false,+ "onlineMeetingProvider":"unknown",+ "allowNewTimeProposals":true,+ "isDraft":false,+ "hideAttendees":false,+ "recurrence":null,+ "onlineMeeting":null,+ "responseStatus":{"response":"accepted","time":"2019-04-02T20:20:00Z"},+ "body":{"contentType":"html","content":"<html><body><p>Quarterly review</p></body></html>"},+ "start":{"dateTime":format!("{day}T20:30:00.0000000"),"timeZone":"UTC"},+ "end":{"dateTime":format!("{day}T21:00:00.0000000"),"timeZone":"UTC"},+ "location":{"displayName":"Conference room","locationType":"default","uniqueIdType":"unknown"},+ "locations":[{"displayName":"Conference room","locationType":"default","uniqueIdType":"unknown"}],+ "attendees":[{+ "type":"required",+ "status":{"response":"accepted","time":"2019-04-02T20:20:00Z"},+ "emailAddress":{"name":"Ada","address":"[email protected]"}+ }],+ "organizer":{"emailAddress":{"name":"Olivia","address":"[email protected]"}}+ });+ for (key, property) in rest.as_object().unwrap() {+ value[key.as_str()] = property.clone();+ }+ value+}++/// The series of Graph's documented List instances example, with the+/// occurrences and the exception that response lists: its 15 April meeting+/// moved to the 16th, with its own details.+pub(super) fn documented_series() -> (Value, Vec<Value>) {+ let mut master = documented_instance(+ "AAMkAGUzYRgWAAA=",+ "seriesMaster",+ "2019-04-08",+ "2019-04-08",+ );+ for property in ["originalStart", "seriesMasterId"] {+ master.as_object_mut().unwrap().remove(property);+ }+ master["recurrence"] = json!({+ "pattern":{"type":"weekly","interval":1,"month":0,"dayOfMonth":0,"daysOfWeek":["monday"],"firstDayOfWeek":"sunday","index":"first"},+ "range":{"type":"endDate","startDate":"2019-04-08","endDate":"2019-04-29","recurrenceTimeZone":"Pacific Standard Time","numberOfOccurrences":0}+ });+ let mut moved = documented_instance(+ "AAMkAGUzYAgI1sE1TatAAEYAAAAAlNFb2CNPe0ucP9you",+ "exception",+ "2019-04-16",+ "2019-04-15",+ );+ moved["subject"] = json!("Review strategy for Q3 (moved)");+ moved["bodyPreview"] = json!("Changing meeting from 4/15 to 4/16.");+ moved["body"]["content"] =+ json!("<html><body><p>Changing meeting from 4/15 to 4/16.</p></body></html>");+ moved["location"]["displayName"] = json!("Board room");+ moved["sensitivity"] = json!("private");+ moved["showAs"] = json!("tentative");+ moved["isReminderOn"] = json!(false);+ moved["lastModifiedDateTime"] = json!("2019-04-03T09:12:00.1234567Z");+ moved["attendees"][0]["emailAddress"] = json!({"name":"Grace","address":"[email protected]"});+ let instances = vec![+ moved,+ documented_instance(+ "AAMkAGUzYAgI1ru1JMcAAEYAAAAAlNFb2CNPe0ucP9you",+ "occurrence",+ "2019-04-08",+ "2019-04-08",+ ),+ documented_instance(+ "AAMkAGUzYAgI1sa1do_AAEYAAAAAlNFb2CNPe0ucP9you",+ "occurrence",+ "2019-04-22",+ "2019-04-22",+ ),+ documented_instance(+ "AAMkAGUzYAgI1sw1n3PAAEYAAAAAlNFb2CNPe0ucP9you",+ "occurrence",+ "2019-04-29",+ "2019-04-29",+ ),+ ];+ (master, instances)+}++/// The window of Graph's documented List instances request.+pub(super) fn april_2019() -> ProviderCalendarTarget {+ let mut target = target();+ target.range = OccurrenceRange {+ starts_at: Utc.with_ymd_and_hms(2019, 4, 8, 9, 0, 0).unwrap(),+ ends_at: Utc.with_ymd_and_hms(2019, 4, 30, 9, 0, 0).unwrap(),+ start_date: "2019-04-08".parse().unwrap(),+ end_date: "2019-04-30".parse().unwrap(),+ };+ target+}+/// `GET /me/mailboxSettings` as Graph documents it, with `time_zone` as the/// mailbox's zone, or without one when it is `None`. Its working hours keep a/// zone of their own.@@ -238,7 +415,9 @@ fn windows_zones_and_moved_exceptions_preserve_the_original_occurrence() {exception["subject"] = json!("Moved meeting");exception["start"]["dateTime"] = json!("2026-11-03T18:00:00");exception["end"]["dateTime"] = json!("2026-11-03T19:00:00");- let upsert = normalize::projection(&target(), master, vec![exception]).unwrap();+ let upsert = normalize::projection(&target(), master, vec![exception])+ .unwrap()+ .upsert;assert_eq!(upsert.occurrences[0].recurrence_id.as_deref(),Some("2026-11-02T17:00:00+00:00")@@ -366,6 +545,7 @@ fn provider_restrictions_are_explicit_and_extra_popup_reminders_round_trip() {assert_eq!(normalize::projection(&target(), current, vec![]).unwrap()+ .upsert.event.reminders,reminders@@ -390,7 +570,9 @@ fn provider_restrictions_are_explicit_and_extra_popup_reminders_round_trip() {email_event["isReminderOn"] = email_reminder["isReminderOn"].clone();email_event["singleValueExtendedProperties"] =email_reminder["singleValueExtendedProperties"].clone();- let restored = normalize::projection(&target(), email_event, vec![]).unwrap();+ let restored = normalize::projection(&target(), email_event, vec![])+ .unwrap()+ .upsert;assert_eq!(restored.event.reminders.overrides[0].method, "email");}@@ -498,6 +680,7 @@ fn native_reminder_edits_supersede_macro_extra_reminders() {current["singleValueExtendedProperties"] = json!([{"id":PREFERENCES_ID,"value":json!({"nativeReminderOn":true,"nativeReminderMinutes":10,"reminders":{"useDefault":false,"overrides":[{"method":"popup","minutes":10},{"method":"popup","minutes":30}]}}).to_string()}]);let reminders = normalize::projection(&target(), current, vec![]).unwrap()+ .upsert.event.reminders;assert_eq!(@@ -514,7 +697,9 @@ fn custom_zones_do_not_poison_timed_calendar_pages_and_sequence_tracks_invitatiolet mut value = event("custom");value["originalStartTimeZone"] = json!("tzone://Microsoft/Custom");value["singleValueExtendedProperties"] = json!([{"id":APPOINTMENT_SEQUENCE_ID,"value":"3"}]);- let projection = normalize::projection(&target(), value, vec![]).unwrap();+ let projection = normalize::projection(&target(), value, vec![])+ .unwrap()+ .upsert;assert_eq!(projection.event.sequence, 3);assert!(matches!(projection.event.time,EventTime::Timed {time_zone:Some(ref zone),..} if zone=="Etc/UTC")@@ -538,7 +723,9 @@ fn the_organizer_of_a_created_event_holds_it_while_guests_have_not_answered() {}]);- let projection = normalize::projection(&target(), value, vec![]).unwrap();+ let projection = normalize::projection(&target(), value, vec![])+ .unwrap()+ .upsert;assert_eq!(projection@@ -637,6 +824,7 @@ fn automatic_decline_preferences_survive_reminder_edits_and_identical_retries()current["singleValueExtendedProperties"] = body["singleValueExtendedProperties"].clone();let policy = normalize::projection(&target(), current.clone(), vec![]).unwrap()+ .upsert.source.details().automatic_decline@@ -664,6 +852,7 @@ fn automatic_decline_preferences_survive_reminder_edits_and_identical_retries()edited_reply["singleValueExtendedProperties"].clone();let edited_policy = normalize::projection(&target(), updated, vec![]).unwrap()+ .upsert.source.details().automatic_decline@@ -688,6 +877,7 @@ fn automatic_decline_preferences_survive_reminder_edits_and_identical_retries()assert_eq!(normalize::projection(&target(), current.clone(), vec![]).unwrap()+ .upsert.source.details().automatic_decline@@ -698,6 +888,7 @@ fn automatic_decline_preferences_survive_reminder_edits_and_identical_retries()assert!(normalize::projection(&target(), current.clone(), vec![]).unwrap()+ .upsert.source.details().automatic_decline@@ -709,6 +900,7 @@ fn automatic_decline_preferences_survive_reminder_edits_and_identical_retries()enabled_again["singleValueExtendedProperties"].clone();let new_policy = normalize::projection(&target(), current, vec![]).unwrap()+ .upsert.source.details().automatic_decline@@ -802,7 +994,9 @@ fn an_imported_series_ended_by_its_organizer_reads_graphs_utc_alias() {instance["end"]["dateTime"] = json!(format!("{day}T11:30:00.0000000"));instance});- let upsert = normalize::projection(&target(), series, instances.to_vec()).unwrap();+ let upsert = normalize::projection(&target(), series, instances.to_vec())+ .unwrap()+ .upsert;assert_eq!(upsert.occurrences.len(), 2);}@@ -854,6 +1048,176 @@ async fn an_event_macro_cannot_read_names_the_rejected_field() {}}+/// A daily meeting Graph holds at 10:00 in Los Angeles on each of the next+/// three days, as `instances` lists it, organized by `organizer`. Its third+/// meeting moved to 11:00 with a title of its own.+pub(super) fn upcoming_series(+ id: &str,+ organizer: &str,+ is_organizer: bool,+) -> (Value, Vec<Value>) {+ let days: Vec<_> = (1..=3)+ .map(|n| Utc::now().date_naive() + chrono::Days::new(n))+ .collect();+ let at = |day: chrono::NaiveDate, hour: u32| {+ day.and_hms_opt(hour, 0, 0)+ .unwrap()+ .and_local_timezone(chrono_tz::America::Los_Angeles)+ .unwrap()+ .to_utc()+ };+ let graph_time = |instant: chrono::DateTime<Utc>| json!({"dateTime":instant.format("%Y-%m-%dT%H:%M:%S.0000000").to_string(),"timeZone":"UTC"});+ let instance = |instance_id: String, kind: &str, day: chrono::NaiveDate, hour: u32| {+ let mut value = documented_instance(&instance_id, kind, "2019-04-08", "2019-04-08");+ value["subject"] = json!("Daily standup");+ value["iCalUId"] = json!(format!("{id}-uid"));+ value["seriesMasterId"] = json!(id);+ value["isOrganizer"] = json!(is_organizer);+ value["organizer"] = json!({"emailAddress":{"name":"Organizer","address":organizer}});+ value["start"] = graph_time(at(day, hour));+ value["end"] = graph_time(at(day, hour) + chrono::Duration::minutes(30));+ value["originalStart"] =+ json!(at(day, 10).to_rfc3339_opts(chrono::SecondsFormat::Secs, true));+ value+ };+ let mut master = instance(id.into(), "seriesMaster", days[0], 10);+ for property in ["originalStart", "seriesMasterId"] {+ master.as_object_mut().unwrap().remove(property);+ }+ master["recurrence"] = json!({+ "pattern":{"type":"daily","interval":1,"month":0,"dayOfMonth":0,"firstDayOfWeek":"sunday","index":"first"},+ "range":{"type":"numbered","startDate":days[0].to_string(),"endDate":"0001-01-01","recurrenceTimeZone":"Pacific Standard Time","numberOfOccurrences":3}+ });+ let mut instances: Vec<_> = days[..2]+ .iter()+ .map(|day| instance(format!("{id}-{day}"), "occurrence", *day, 10))+ .collect();+ let mut moved = instance(format!("{id}-{}", days[2]), "exception", days[2], 11);+ moved["subject"] = json!("Daily standup, an hour later");+ instances.push(moved);+ (master, instances)+}++fn without_ids(mut upsert: CalendarEventUpsert) -> CalendarEventUpsert {+ upsert.event.id = Uuid::nil();+ for occurrence in &mut upsert.occurrences {+ occurrence.event_id = Uuid::nil();+ }+ upsert+}++async fn serve_series(server: &MockServer, master: &Value, instances: Vec<Value>) {+ let id = master["id"].as_str().unwrap();+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/calendars/calendar/events/{id}")))+ .respond_with(ResponseTemplate::new(200).set_body_json(master))+ .mount(server)+ .await;+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/events/{id}/instances")))+ .respond_with(Instances(instances))+ .mount(server)+ .await;+}++#[test]+fn instances_select_every_default_event_property_and_the_original_start() {+ let instance = documented_instance("instance", "occurrence", "2019-04-08", "2019-04-08");+ let documented: std::collections::BTreeSet<_> = instance+ .as_object()+ .unwrap()+ .keys()+ .filter(|key| !key.starts_with("@odata."))+ .map(String::as_str)+ .collect();+ let selected: std::collections::BTreeSet<_> = INSTANCE_PROPERTIES.iter().copied().collect();+ assert_eq!(+ selected, documented,+ "instances select what Graph returns by default, plus originalStart, and nothing it does not document"+ );+}++#[tokio::test]+async fn a_series_reads_the_same_through_its_selected_instances_as_from_complete_ones() {+ let (server, api) = setup().await;+ let (master, instances) = documented_series();+ serve_series(&server, &master, instances.clone()).await;+ let target = april_2019();+ let Ok(OutlookEventRefresh::Refreshed { upsert, skipped }) = api+ .refresh_calendar_event("token", &target, "AAMkAGUzYRgWAAA=")+ .await+ else {+ panic!("the documented series should be readable");+ };+ assert!(skipped.is_empty());+ let complete = normalize::projection(&target, master, instances)+ .unwrap()+ .upsert;+ assert_eq!(+ without_ids(*upsert.clone()),+ without_ids(complete),+ "no property the normalizer reads is missing from the selection"+ );+ let moved = &upsert.overrides[0];+ assert_eq!(moved.recurrence_id, "2019-04-15T20:30:00+00:00");+ assert_eq!(+ moved.title.as_deref(),+ Some("Review strategy for Q3 (moved)")+ );+ assert_eq!(moved.location.as_deref(), Some("Board room"));+ assert!(+ upsert+ .occurrences+ .iter()+ .all(|occurrence| occurrence.recurrence_id.as_ref() == Some(&occurrence.occurrence_key)),+ "every occurrence is addressed by its original start"+ );+}++#[test]+fn an_exception_without_its_original_start_is_left_out_of_an_imported_series() {+ let (master, mut instances) = documented_series();+ // Graph's documented response, which selects no originalStart.+ instances[0]+ .as_object_mut()+ .unwrap()+ .remove("originalStart");+ let projection = normalize::projection(&april_2019(), master, instances).unwrap();+ assert_eq!(+ projection.skipped,+ vec![SkippedOccurrence {+ provider_id: "AAMkAGUzYAgI1sE1TatAAEYAAAAAlNFb2CNPe0ucP9you".into(),+ field: "instances.originalStart.missing".into(),+ }]+ );+ assert!(projection.upsert.overrides.is_empty());+ let days: Vec<_> = projection+ .upsert+ .occurrences+ .iter()+ .map(|occurrence| occurrence.occurrence_key.as_str())+ .collect();+ assert_eq!(+ days,+ [+ "2019-04-08T20:30:00+00:00",+ "2019-04-22T20:30:00+00:00",+ "2019-04-29T20:30:00+00:00"+ ],+ "the rest of the series is imported"+ );+}++#[test]+fn an_original_start_macro_cannot_parse_rejects_the_series_at_its_own_field() {+ let (master, mut instances) = documented_series();+ instances[0]["originalStart"] = json!("2019-04-15");+ assert_eq!(+ normalize::projection(&april_2019(), master, instances).err(),+ Some(Rejected::at("instances.originalStart"))+ );+}+#[test]fn occurrence_preferences_override_or_inherit_series_reminders_and_decline_policy() {let policy = AutomaticDeclinePolicy {@@ -875,8 +1239,9 @@ fn occurrence_preferences_override_or_inherit_series_reminders_and_decline_policinstance["type"] = json!("exception");instance["originalStart"] = json!("2026-11-02T17:00:00Z");instance["singleValueExtendedProperties"] = json!([]);- let inherited =- normalize::projection(&target(), master.clone(), vec![instance.clone()]).unwrap();+ let inherited = normalize::projection(&target(), master.clone(), vec![instance.clone()])+ .unwrap()+ .upsert;assert_eq!(inherited.overrides[0].reminders.as_ref().unwrap(),&inherited.event.reminders@@ -887,7 +1252,9 @@ fn occurrence_preferences_override_or_inherit_series_reminders_and_decline_policinstance["singleValueExtendedProperties"] = json!([{"id":PREFERENCES_ID,"value":json!({"nativeReminderOn":false,"reminders":{"useDefault":false,"overrides":[]},"automaticDecline":disabled,}).to_string()}]);- let overridden = normalize::projection(&target(), master, vec![instance]).unwrap();+ let overridden = normalize::projection(&target(), master, vec![instance])+ .unwrap()+ .upsert;assert!(overridden.overrides[0].remindersdiff --git a/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs b/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rsindex 7c2e367718..b7b143c1c3 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/test/mutations.rs@@ -151,6 +151,7 @@ pub(super) async fn connected_mailbox(.claim_outlook_calendar(Uuid::now_v7(),OccurrenceRange::maintenance_horizon(Utc::now()),+ OutlookApiClientRepository::READER_REVISION,).await.unwrap()@@ -167,17 +168,19 @@ async fn sync_event(repo: &PgCalendarRepository, api: &OutlookApiClientRepositor.claim_outlook_calendar(Uuid::now_v7(),OccurrenceRange::maintenance_horizon(Utc::now()),+ OutlookApiClientRepository::READER_REVISION,).await.unwrap().unwrap();- let OutlookEventRefresh::Refreshed(upsert) = api+ let OutlookEventRefresh::Refreshed { upsert, skipped } = api.refresh_calendar_event(TOKEN, lease.target.as_ref().unwrap(), graph_id).await.unwrap()else {panic!("{graph_id} should be readable");};+ assert!(skipped.is_empty(), "{graph_id} is read whole");repo.upsert_event(CalendarEventWrite::OutlookSync {lease: Box::new(lease),upsert: *upsert,@@ -621,3 +624,357 @@ async fn deleting_a_synced_outlook_event_deletes_it_in_graph_and_drops_it_from_t.is_empty());}++/// One series in Graph: its master, and its instances as `instances`+/// returns them, which Macro patches, deletes or answers one at a time.+#[derive(Clone)]+struct GraphSeries(Arc<Mutex<(Value, Vec<Value>)>>);++impl Respond for GraphSeries {+ fn respond(&self, request: &Request) -> ResponseTemplate {+ let mut series = self.0.lock().unwrap();+ let (master, instances) = &mut *series;+ let segments: Vec<String> = request+ .url+ .path_segments()+ .unwrap()+ .skip(2)+ .map(str::to_owned)+ .collect();+ let segments: Vec<&str> = segments.iter().map(String::as_str).collect();+ let is_master = |id: &str| master["id"].as_str() == Some(id);+ match (request.method.as_str(), segments.as_slice()) {+ ("GET", ["events", id, "instances"]) if is_master(id) => {+ let value: Vec<_> = instances+ .iter()+ .map(|instance| as_graph_returns(instance, request))+ .collect();+ ResponseTemplate::new(200).set_body_json(json!({ "value": value }))+ }+ ("GET", ["calendars", _, "events", id] | ["events", id]) if is_master(id) => {+ ResponseTemplate::new(200).set_body_json(master.clone())+ }+ ("PATCH", ["events", id]) => {+ let patch: serde_json::Map<String, Value> =+ serde_json::from_slice(&request.body).unwrap();+ let series_patch = is_master(id);+ let event = if series_patch {+ &mut *master+ } else {+ let Some(instance) = instances.iter_mut().find(|i| i["id"] == *id) else {+ return ResponseTemplate::new(404);+ };+ instance["type"] = json!("exception");+ instance+ };+ for (key, value) in patch {+ event[key.as_str()] = value;+ }+ event["@odata.etag"] = json!("W/\"2\"");+ let patched = event.clone();+ // A series given an end date no longer lists the meetings after it.+ if series_patch && master["recurrence"]["range"]["type"] == "endDate" {+ let end: chrono::NaiveDate = master["recurrence"]["range"]["endDate"]+ .as_str()+ .unwrap()+ .parse()+ .unwrap();+ instances.retain(|instance| {+ let start: chrono::DateTime<Utc> =+ instance["originalStart"].as_str().unwrap().parse().unwrap();+ start.with_timezone(&Los_Angeles).date_naive() <= end+ });+ }+ ResponseTemplate::new(200).set_body_json(patched)+ }+ ("DELETE", ["events", id]) => {+ instances.retain(|instance| instance["id"] != *id);+ ResponseTemplate::new(204)+ }+ ("POST", ["events", id, action]) => {+ let Some(instance) = instances.iter_mut().find(|i| i["id"] == *id) else {+ return ResponseTemplate::new(404);+ };+ instance["type"] = json!("exception");+ instance["responseStatus"]["response"] = json!(match *action {+ "tentativelyAccept" => "tentativelyAccepted",+ "decline" => "declined",+ _ => "accepted",+ });+ ResponseTemplate::new(202)+ }+ _ => ResponseTemplate::new(404),+ }+ }+}++/// A synced daily series of two meetings the mailbox organizes, or attends+/// as a guest, and the original-start key of its second meeting. Its meetings+/// are occurrences only, so it reads the same before and after Graph's+/// `originalStart` is selected, and only finding one meeting depends on it.+async fn synced_series(+ pool: &PgPool,+ is_organizer: bool,+) -> (+ MockServer,+ OutlookApiClientRepository,+ PgCalendarRepository,+ Uuid,+ String,+) {+ let (server, api) = setup().await;+ let repo = connected_mailbox(+ pool,+ &server,+ &api,+ json!([{"id":"AAMkCalendar","name":"Calendar","canEdit":true,"isDefaultCalendar":true}]),+ )+ .await;+ let organizer = if is_organizer {+ INBOX+ } else {+ };+ let (mut master, mut instances) = upcoming_series("AAMkDaily", organizer, is_organizer);+ instances.truncate(2);+ let guest = if is_organizer {+ } else {+ INBOX+ };+ for event in std::iter::once(&mut master).chain(instances.iter_mut()) {+ event["attendees"] = json!([{+ "type":"required",+ "status":{"response":"none","time":"0001-01-01T00:00:00Z"},+ "emailAddress":{"name":"Guest","address":guest}+ }]);+ }+ Mock::given(path_regex(r"^/v1\.0/me/(calendars/[^/]+/)?events/"))+ .respond_with(GraphSeries(Arc::new(Mutex::new((master, instances)))))+ .mount(&server)+ .await;+ Mock::given(method("GET"))+ .and(path("/v1.0/me"))+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({"mail":INBOX})))+ .mount(&server)+ .await;+ sync_event(&repo, &api, "AAMkDaily").await;+ let listed = CalendarService::new(repo.clone())+ .list_occurrences(OWNER, two_weeks(), None, 100)+ .await+ .unwrap();+ assert_eq!(listed.len(), 2);+ let event_id = listed[1].event.id;+ let key = listed[1].occurrence.occurrence_key.clone();+ (server, api, repo, event_id, key)+}++/// The id Graph gives the series' meeting whose original start is `key`.+fn instance_id(key: &str) -> String {+ let start: chrono::DateTime<Utc> = key.parse().unwrap();+ format!(+ "AAMkDaily-{}",+ start.with_timezone(&Los_Angeles).date_naive()+ )+}++async fn writes(server: &MockServer) -> Vec<(String, String, Value)> {+ server+ .received_requests()+ .await+ .unwrap()+ .into_iter()+ .filter(|r| r.method.as_str() != "GET")+ .map(|r| {+ (+ r.method.to_string(),+ r.url.path().to_owned(),+ serde_json::from_slice(&r.body).unwrap_or(Value::Null),+ )+ })+ .collect()+}++fn mutations(+ repo: PgCalendarRepository,+ api: OutlookApiClientRepository,+) -> CalendarMutationServiceImpl<+ PgCalendarRepository,+ OutlookApiClientRepository,+ IssuedToken,+ NoopMacroEventBroker,+ NoOpenClients,+> {+ CalendarMutationServiceImpl::new(repo, api, IssuedToken, NoopMacroEventBroker, NoOpenClients)+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn editing_one_meeting_of_a_synced_outlook_series_patches_only_that_meeting(pool: PgPool) {+ let (server, api, repo, event_id, key) = synced_series(&pool, true).await;+ mutations(repo, api)+ .update_event(+ OWNER,+ event_id,+ None,+ CalendarEventPatch {+ title: Some("Standup with the new hire".into()),+ ..Default::default()+ },+ CalendarUpdateScope::ThisEvent {+ recurrence_id: key.clone(),+ },+ )+ .await+ .unwrap();+ assert_eq!(+ writes(&server).await,+ [(+ "PATCH".into(),+ format!("/v1.0/me/events/{}", instance_id(&key)),+ json!({"subject":"Standup with the new hire"})+ )]+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn answering_one_meeting_of_an_outlook_series_answers_only_that_meeting(pool: PgPool) {+ let (server, api, repo, event_id, key) = synced_series(&pool, false).await;+ mutations(repo, api)+ .respond_to_event(+ OWNER,+ event_id,+ None,+ AttendeeResponseStatus::Accepted,+ CalendarRsvpScope::ThisEvent {+ recurrence_id: key.clone(),+ },+ None,+ )+ .await+ .unwrap();+ assert_eq!(+ writes(&server).await,+ [(+ "POST".into(),+ format!("/v1.0/me/events/{}/accept", instance_id(&key)),+ json!({"sendResponse":true})+ )]+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn deleting_one_meeting_of_an_outlook_series_deletes_only_that_meeting(pool: PgPool) {+ let (server, api, repo, event_id, key) = synced_series(&pool, true).await;+ mutations(repo.clone(), api)+ .delete_event(+ OWNER,+ event_id,+ None,+ CalendarDeletionScope::ThisEvent {+ recurrence_id: key.clone(),+ },+ )+ .await+ .unwrap();+ assert_eq!(+ writes(&server).await,+ [(+ "DELETE".into(),+ format!("/v1.0/me/events/{}", instance_id(&key)),+ Value::Null+ )]+ );+ let left: Vec<_> = CalendarService::new(repo)+ .list_occurrences(OWNER, two_weeks(), None, 100)+ .await+ .unwrap()+ .into_iter()+ .map(|listed| listed.occurrence.occurrence_key)+ .collect();+ assert_eq!(left.len(), 1);+ assert!(!left.contains(&key));+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn ending_an_outlook_series_at_one_meeting_ends_it_the_day_before(pool: PgPool) {+ let (server, api, repo, event_id, key) = synced_series(&pool, true).await;+ let reads = CalendarService::new(repo.clone());+ let first = reads+ .list_occurrences(OWNER, two_weeks(), None, 100)+ .await+ .unwrap()[0]+ .occurrence+ .occurrence_key+ .clone();+ mutations(repo, api)+ .delete_event(+ OWNER,+ event_id,+ None,+ CalendarDeletionScope::ThisAndFollowing {+ recurrence_id: key.clone(),+ },+ )+ .await+ .unwrap();+ let writes = writes(&server).await;+ assert_eq!(writes.len(), 1);+ let (method, path, body) = &writes[0];+ assert_eq!(+ (method.as_str(), path.as_str()),+ ("PATCH", "/v1.0/me/events/AAMkDaily")+ );+ let start: chrono::DateTime<Utc> = key.parse().unwrap();+ assert_eq!(+ body["recurrence"]["range"]["endDate"],+ (start.with_timezone(&Los_Angeles).date_naive() - Days::new(1)).to_string()+ );+ let left: Vec<_> = reads+ .list_occurrences(OWNER, two_weeks(), None, 100)+ .await+ .unwrap()+ .into_iter()+ .map(|listed| listed.occurrence.occurrence_key)+ .collect();+ assert_eq!(left, [first], "only the meeting before it is left");+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn inviting_a_guest_to_one_meeting_of_an_outlook_series_writes_only_that_meeting(+ pool: PgPool,+) {+ let (server, api, repo, event_id, key) = synced_series(&pool, true).await;+ mutations(repo, api)+ .invite_guests(+ OWNER,+ event_id,+ None,+ vec![CalendarAttendeeInput {+ email: "[email protected]".into(),+ is_optional: false,+ response_status: None,+ }],+ CalendarUpdateScope::ThisEvent {+ recurrence_id: key.clone(),+ },+ )+ .await+ .unwrap();+ let writes = writes(&server).await;+ assert_eq!(writes.len(), 1);+ let (method, path, body) = &writes[0];+ assert_eq!(+ (method.as_str(), path.as_str()),+ (+ "PATCH",+ format!("/v1.0/me/events/{}", instance_id(&key)).as_str()+ )+ );+ let invited: Vec<_> = body["attendees"]+ .as_array()+ .unwrap()+ .iter()+ .map(|attendee| attendee["emailAddress"]["address"].as_str().unwrap())+ .collect();+ assert_eq!(invited, ["[email protected]", "[email protected]"]);+}diff --git a/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rs b/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rsindex cd11fe35cb..649fd12787 100644--- a/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rs+++ b/crates/email_api_client/src/outbound/outlook/calendar/test/sync.rs@@ -248,3 +248,125 @@ async fn a_mailbox_in_a_time_zone_macro_cannot_map_still_discovers_and_syncs(poo.unwrap();assert_eq!(time_zone, None, "the calendar's zone is unknown, not UTC");}++async fn serve_series(server: &MockServer, master: Value, instances: Vec<Value>) {+ let id = master["id"].as_str().unwrap().to_owned();+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/calendars/AAMkCalendar/events/{id}")))+ .respond_with(ResponseTemplate::new(200).set_body_json(master))+ .mount(server)+ .await;+ Mock::given(method("GET"))+ .and(path(format!("/v1.0/me/events/{id}/instances")))+ .respond_with(Instances(instances))+ .mount(server)+ .await;+}++/// How many times the round read the event `id` from Graph.+async fn reads_of(server: &MockServer, id: &str) -> usize {+ let event = format!("/v1.0/me/calendars/AAMkCalendar/events/{id}");+ server+ .received_requests()+ .await+ .unwrap()+ .iter()+ .filter(|request| request.url.path() == event)+ .count()+}++async fn next_round(pool: &PgPool) {+ sqlx::query!(+ "UPDATE calendar_outlook_work SET next_run_at = now() WHERE calendar_id IS NOT NULL"+ )+ .execute(pool)+ .await+ .unwrap();+}++/// A series whose decline policy this build cannot read, standing in for a+/// series an older build could not read.+fn unreadable(mut master: Value) -> Value {+ master["showAs"] = json!("oof");+ master["singleValueExtendedProperties"] = json!([{+ "id":PREFERENCES_ID,+ "value":json!({"automaticDecline":{"mode":"declineWhenAway"}}).to_string(),+ }]);+ master+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_series_with_a_moved_meeting_is_imported_with_it(pool: PgPool) {+ let (server, sync) = connected(&pool).await;+ serve_delta(&server, None, &[occurrence("AAMkDaily")], "complete").await;+ let (master, instances) = upcoming_series("AAMkDaily", "[email protected]", false);+ let moved = instances[2]["originalStart"].as_str().unwrap().to_owned();+ serve_series(&server, master, instances).await;+ sync_until_idle(&sync).await;++ assert_synced(&pool).await;+ assert_eq!(imported(&pool).await, vec!["AAMkDaily"]);+ let overrides = sqlx::query!(+ "SELECT o.recurrence_id, o.title FROM calendar_event_overrides o JOIN calendar_events e ON e.id = o.event_id WHERE e.owner_id = $1",+ OWNER+ )+ .fetch_all(&pool)+ .await+ .unwrap();+ assert_eq!(overrides.len(), 1);+ assert_eq!(+ overrides[0]+ .recurrence_id+ .parse::<chrono::DateTime<Utc>>()+ .unwrap(),+ moved.parse::<chrono::DateTime<Utc>>().unwrap(),+ "the moved meeting replaces its original slot"+ );+ assert_eq!(+ overrides[0].title.as_deref(),+ Some("Daily standup, an hour later")+ );+}++#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]+async fn a_series_an_older_build_skipped_is_read_again_once_on_the_next_delta_round(pool: PgPool) {+ let (server, sync) = connected(&pool).await;+ let (master, instances) = upcoming_series("AAMkDaily", "[email protected]", false);+ serve_delta(&server, None, &[occurrence("AAMkDaily")], "first").await;+ serve_series(&server, unreadable(master.clone()), instances.clone()).await;+ sync_until_idle(&sync).await;+ assert!(imported(&pool).await.is_empty());++ // Nothing changed in Outlook, so a series this build skipped costs no read.+ server.reset().await;+ serve_delta(&server, Some("first"), &[], "second").await;+ serve_series(&server, master.clone(), instances.clone()).await;+ next_round(&pool).await;+ sync_until_idle(&sync).await;+ assert_eq!(reads_of(&server, "AAMkDaily").await, 0);++ // What a build before reader revisions left behind when it skipped one.+ sqlx::query!("UPDATE calendar_outlook_members SET reader_revision = NULL")+ .execute(&pool)+ .await+ .unwrap();+ server.reset().await;+ serve_delta(&server, Some("second"), &[], "third").await;+ serve_series(&server, master.clone(), instances.clone()).await;+ next_round(&pool).await;+ sync_until_idle(&sync).await;+ assert_synced(&pool).await;+ assert_eq!(reads_of(&server, "AAMkDaily").await, 1);+ assert_eq!(+ imported(&pool).await,+ vec!["AAMkDaily"],+ "the series comes back without changing in Outlook"+ );++ server.reset().await;+ serve_delta(&server, Some("third"), &[], "fourth").await;+ serve_series(&server, master, instances).await;+ next_round(&pool).await;+ sync_until_idle(&sync).await;+ assert_eq!(reads_of(&server, "AAMkDaily").await, 0, "it is read once");+}diff --git a/crates/macro_db_client/migrations/20261105222458_outlook_members_reader_revision.sql b/crates/macro_db_client/migrations/20261105222458_outlook_members_reader_revision.sqlnew file mode 100644index 0000000000..d3ee48f276--- /dev/null+++ b/crates/macro_db_client/migrations/20261105222458_outlook_members_reader_revision.sql@@ -0,0 +1,9 @@+-- The Outlook reader revision that last read each listed event, or found its+-- stored copy. A delta calendar reads an event again when it has no stored+-- copy and was last read by an older revision, so events an older build+-- skipped as unreadable come back once a build can read them. Rows written+-- before this column, and by builds that do not set it, stay NULL, which+-- counts as older than any revision.+SET LOCAL lock_timeout = '5s';++ALTER TABLE calendar_outlook_members ADD COLUMN reader_revision smallint;