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): clear the snapshot mark once a real invitation reaches the copy (#8636)

gbirman1864e628eef7
diff --git a/crates/calendar_events/AGENTS.md b/crates/calendar_events/AGENTS.md
index c1a6b48616..2dc9f6b91e 100644
--- a/crates/calendar_events/AGENTS.md
+++ b/crates/calendar_events/AGENTS.md
@@ -93,7 +93,14 @@ reminder dispatch, team availability, and provider/Postgres persistence.
the target calendar, accepted, as the only guest, plus the private
`macroSnapshot` extended property. Sync stores it as `is_snapshot`, which
grants no capability and makes RSVPs and invites fail with `snapshot`,
- since neither would reach the meeting. Its owner changes its details only
+ since neither would reach the meeting. An invitation to the same meeting
+ later lands on the snapshot's Google event (same UID, marker kept), so
+ `import::is_snapshot` counts a marked copy as a snapshot only while it holds
+ what the import wrote: the calendar as its only attendee, answered. Another
+ attendee (a Google meeting with guests lists its organizer) or the
+ calendar's row back at `needsAction` makes it the owner's seat, and the
+ Postgres upsert keeps a source stored as a seat from turning back after
+ they answer. Its owner changes its details only
as a guest without modify rights would (reminders and availability), and
may delete it. The mutation target reads the flag from the addressed
copy's projection, since a snapshot can share its entity with the real
diff --git a/crates/calendar_events/src/outbound/google.rs b/crates/calendar_events/src/outbound/google.rs
index be794324e3..52d7ac0446 100644
--- a/crates/calendar_events/src/outbound/google.rs
+++ b/crates/calendar_events/src/outbound/google.rs
@@ -2340,7 +2340,7 @@ fn map_upsert(
.collect(),
reminders: map_reminders(master.reminders.as_ref()),
guest_policy: guest_policy(&master),
- is_snapshot: import::is_snapshot(&master),
+ is_snapshot: import::is_snapshot(&master, &target.provider_calendar_id),
viewer_can: ViewerCapabilities::NONE,
created_at,
updated_at,
diff --git a/crates/calendar_events/src/outbound/google/import.rs b/crates/calendar_events/src/outbound/google/import.rs
index 90c4874784..75f08b34c1 100644
--- a/crates/calendar_events/src/outbound/google/import.rs
+++ b/crates/calendar_events/src/outbound/google/import.rs
@@ -126,11 +126,34 @@ pub(super) fn import_body(
body
}
-/// Whether the event carries Macro's snapshot marker.
-pub(super) fn is_snapshot(event: &GoogleEvent) -> bool {
- event
+/// Whether the event is still Macro's snapshot: it carries the marker and
+/// holds only what the import wrote, the calendar as its only guest with an
+/// answer.
+///
+/// The snapshot shares the meeting's iCalendar UID, so an invitation to the
+/// meeting that later reaches this calendar lands on the same event, private
+/// marker included. Google then lists the meeting's guests (a Google meeting
+/// with any guest also lists its organizer) and asks the calendar to answer
+/// again, and from then on the copy is the owner's seat at the meeting.
+pub(super) fn is_snapshot(event: &GoogleEvent, provider_calendar_id: &str) -> bool {
+ let marked = event
.extended_properties
.as_ref()
.and_then(|properties| properties.private.get(SNAPSHOT_PROPERTY))
- .is_some_and(|value| value == "true")
+ .is_some_and(|value| value == "true");
+ marked
+ && event.attendees.iter().flatten().all(|attendee| {
+ let is_calendar = attendee.is_self
+ || attendee
+ .email
+ .as_deref()
+ .is_some_and(|email| email.eq_ignore_ascii_case(provider_calendar_id));
+ // Only an explicit answer, as the import wrote one: sync reads a
+ // missing one as awaiting a reply, like `needsAction`.
+ is_calendar
+ && matches!(
+ attendee.response_status.as_deref(),
+ Some("accepted" | "declined" | "tentative")
+ )
+ })
}
diff --git a/crates/calendar_events/src/outbound/google/test/import.rs b/crates/calendar_events/src/outbound/google/test/import.rs
index 8ad639f738..dfb4aaf5ac 100644
--- a/crates/calendar_events/src/outbound/google/test/import.rs
+++ b/crates/calendar_events/src/outbound/google/test/import.rs
@@ -306,3 +306,62 @@ async fn import_reports_a_refusal_apart_from_a_failure_worth_retrying() {
assert_eq!(error.kind(), CalendarProviderErrorKind::Transient);
task.abort();
}
+
+/// Google's copy of the meeting on the member's calendar.
+fn copy_on_calendar(attendees: serde_json::Value, marked: bool) -> GoogleEvent {
+ let mut event = serde_json::json!({
+ "id": "meeting-id",
+ "iCalUID": "[email protected]",
+ "status": "confirmed",
+ "organizer": { "email": "[email protected]" },
+ "attendees": attendees,
+ });
+ if marked {
+ event["extendedProperties"] = serde_json::json!({ "private": { "macroSnapshot": "true" } });
+ }
+ serde_json::from_value(event).unwrap()
+}
+
+#[test]
+fn an_invitation_reaching_a_snapshot_makes_it_the_owners_seat() {
+ let is_snapshot =
+ |event: &GoogleEvent| super::super::import::is_snapshot(event, "[email protected]");
+ let calendar = |status: &str| serde_json::json!({ "email": "[email protected]", "self": true, "responseStatus": status });
+
+ // What the import wrote, including the owner answering it in Google.
+ assert!(is_snapshot(&copy_on_calendar(
+ serde_json::json!([calendar("accepted")]),
+ true
+ )));
+ assert!(is_snapshot(&copy_on_calendar(
+ serde_json::json!([calendar("declined")]),
+ true
+ )));
+ // The import's own echo names the calendar without Google's `self` flag.
+ assert!(is_snapshot(&copy_on_calendar(
+ serde_json::json!([{ "email": "[email protected]", "responseStatus": "accepted" }]),
+ true
+ )));
+
+ // A Google meeting lists its organizer beside the invited calendar.
+ assert!(!is_snapshot(&copy_on_calendar(
+ serde_json::json!([
+ { "email": "[email protected]", "organizer": true, "responseStatus": "accepted" },
+ calendar("needsAction"),
+ ]),
+ true
+ )));
+ // A meeting that lists only the invitee still asks them to answer.
+ assert!(!is_snapshot(&copy_on_calendar(
+ serde_json::json!([calendar("needsAction")]),
+ true
+ )));
+ assert!(!is_snapshot(&copy_on_calendar(
+ serde_json::json!([{ "email": "[email protected]", "self": true }]),
+ true
+ )));
+ assert!(!is_snapshot(&copy_on_calendar(
+ serde_json::json!([calendar("accepted")]),
+ false
+ )));
+}
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index c24f225f8e..ca4d182691 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -801,7 +801,7 @@ impl CalendarRepository for PgCalendarRepository {
let mut tx = self.pool.begin().await.map_err(report)?;
let user_mutation = matches!(write, CalendarEventWrite::UserMutation(_));
let provider_sync = matches!(write, CalendarEventWrite::GoogleBackfill { .. });
- let upsert = match write {
+ let mut upsert = match write {
#[cfg(feature = "outlook")]
CalendarEventWrite::OutlookSync { lease, upsert } => {
outlook::fence(&mut tx, &lease).await?;
@@ -906,7 +906,18 @@ impl CalendarRepository for PgCalendarRepository {
})
})
});
+ let mut became_seat = false;
if let Some(row) = &existing {
+ // A snapshot an invitation has reached is its owner's seat at the
+ // meeting. Answering can leave Google's copy looking like the
+ // import again, as when the meeting lists only them, so a copy
+ // stored as a seat stays one.
+ let stored_snapshot = row
+ .normalized_payload
+ .pointer("/event/isSnapshot")
+ .is_some_and(|value| value == true);
+ became_seat = stored_snapshot && !upsert.event.is_snapshot;
+ upsert.event.is_snapshot &= stored_snapshot;
let incoming =
serde_json::to_value(StoredSourceProjection::from(&upsert)).map_err(report)?;
if canonical_projection(&row.normalized_payload) == canonical_projection(&incoming) {
@@ -1075,6 +1086,12 @@ impl CalendarRepository for PgCalendarRepository {
let mut log = ChangeLogBatch::default();
log.record(source_link_id, CalendarChangeOp::UpsertEvent(event_id));
log.commit(tx).await?;
+ if became_seat {
+ tracing::info!(
+ %event_id,
+ "an invitation reached a snapshot, which is now its owner's seat"
+ );
+ }
Ok(CalendarEventWriteOutcome {
event_id,
owner_id: upsert.event.owner_id.clone(),
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index a42a156748..84ee7bc841 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -1652,6 +1652,55 @@ async fn a_snapshot_stays_marked_on_reads_and_mutation_targets(pool: PgPool) {
);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn a_snapshot_an_invitation_reached_stays_its_owners_seat(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let provider = provider_ids(&repo, link_id).await;
+ let copy = |sequence, is_snapshot| {
+ let mut upsert = timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Shared meeting",
+ sequence,
+ );
+ upsert.event.is_snapshot = is_snapshot;
+ upsert
+ };
+ let is_snapshot = |event_id| {
+ let repo = &repo;
+ async move {
+ repo.get_event_mutation_target(owner_id, event_id, None)
+ .await
+ .unwrap()
+ .expect("the owner may address their copy")
+ .is_snapshot
+ }
+ };
+
+ let event_id = repo.upsert_event_fixture(copy(1, true)).await.unwrap();
+ repo.upsert_event_fixture(copy(2, true)).await.unwrap();
+ assert!(is_snapshot(event_id).await, "a later sync keeps a snapshot");
+
+ // The organizer's invitation lands on the snapshot's Google event.
+ repo.upsert_event_fixture(copy(3, false)).await.unwrap();
+ assert!(!is_snapshot(event_id).await);
+ // Answering can leave Google's copy looking like the import again.
+ repo.upsert_event_fixture(copy(4, true)).await.unwrap();
+ assert!(!is_snapshot(event_id).await, "the seat stays a seat");
+ let entity_is_snapshot = sqlx::query_scalar!(
+ "SELECT is_snapshot FROM calendar_events WHERE id = $1",
+ event_id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert!(!entity_is_snapshot);
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn a_snapshot_beside_the_real_meeting_stays_a_snapshot_when_addressed(pool: PgPool) {
let owner_id = "macro|[email protected]";
diff --git a/docs/agent-guide/calendar.md b/docs/agent-guide/calendar.md
index 5af2808141..9aed0f8020 100644
--- a/docs/agent-guide/calendar.md
+++ b/docs/agent-guide/calendar.md
@@ -561,7 +561,12 @@ invite guests.` line, since none of it would reach the meeting. It offers no `Ed
desktop or in the phone drawer, and the grid does not move or resize it: its details are the
organizer's, and a composer that could change only its notifications would read as editing the
meeting. Its delete button is `Delete your copy`, whose dialog says the shared event stays as it
-is. If Google already holds the meeting on that calendar (an invitation Macro has
+is. If the organizer later invites the viewer (for example by accepting their request to join),
+Google's invitation lands on the same calendar event. Once Google's copy shows it (another guest,
+which every Google meeting with guests has in its organizer, or a request for the viewer's
+answer) the copy becomes an ordinary guest seat: the note goes away, and RSVP and `Edit event`
+follow the organizer's guest permissions. A meeting that lists only the viewer, answered in
+Google before Macro synced the invitation, still reads as a snapshot. If Google already holds the meeting on that calendar (an invitation Macro has
not synced yet), nothing is imported, a sync is scheduled and the card reports that it is
already on their calendar. `Download .ics` saves the event as an iCalendar file and is not
flagged. `Ask to join` asks the person who shared the event to add the viewer as a guest; it