Skip to content

armgit / macro

Public Imported from GitHub
0

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

feat(calendar): log calendar changes per email link in every write transaction (#7673)

gbirmanb23b0516a409
diff --git a/.sqlx/query-08aad90f120dedde3db3693cf2d624724e7312e39babebc8397af88515716efa.json b/.sqlx/query-08aad90f120dedde3db3693cf2d624724e7312e39babebc8397af88515716efa.json
new file mode 100644
index 0000000000..7cf83be626
--- /dev/null
+++ b/.sqlx/query-08aad90f120dedde3db3693cf2d624724e7312e39babebc8397af88515716efa.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH visible AS (\n SELECT id AS link_id FROM email_links WHERE macro_id = $1\n UNION\n SELECT link_id FROM macro_user_links WHERE primary_macro_id = $1\n )\n SELECT visible.link_id AS \"link_id!\", COALESCE(counter.seq, 0) AS \"seq!\"\n FROM visible\n LEFT JOIN calendar_change_counters counter ON counter.link_id = visible.link_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "link_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "seq!",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ null,
+ null
+ ]
+ },
+ "hash": "08aad90f120dedde3db3693cf2d624724e7312e39babebc8397af88515716efa"
+}
diff --git a/.sqlx/query-fd5ef49ce49df5ac1fbec8580a60a212664a563f1eb0bc4d61062b696cf9c320.json b/.sqlx/query-100aae2dcbdee28bf29600b581d32e14c71ad209e0e74191349555aea5822902.json
similarity index 75%
rename from .sqlx/query-fd5ef49ce49df5ac1fbec8580a60a212664a563f1eb0bc4d61062b696cf9c320.json
rename to .sqlx/query-100aae2dcbdee28bf29600b581d32e14c71ad209e0e74191349555aea5822902.json
index ea1f2bdf08..97c8f43340 100644
--- a/.sqlx/query-fd5ef49ce49df5ac1fbec8580a60a212664a563f1eb0bc4d61062b696cf9c320.json
+++ b/.sqlx/query-100aae2dcbdee28bf29600b581d32e14c71ad209e0e74191349555aea5822902.json
@@ -1,15 +1,29 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendars\n SET is_deleted = NOT (id = ANY($2::uuid[])),\n sync_token = CASE\n WHEN id = ANY($2::uuid[]) THEN sync_token\n ELSE NULL\n END,\n materialized_starts_at = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_starts_at\n ELSE NULL\n END,\n materialized_ends_at = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_ends_at\n ELSE NULL\n END,\n materialized_start_date = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_start_date\n ELSE NULL\n END,\n materialized_end_date = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_end_date\n ELSE NULL\n END,\n updated_at = now()\n WHERE account_id = $1\n AND is_deleted IS DISTINCT FROM NOT (id = ANY($2::uuid[]))\n ",
+ "query": "\n UPDATE calendars\n SET is_deleted = NOT (id = ANY($2::uuid[])),\n sync_token = CASE\n WHEN id = ANY($2::uuid[]) THEN sync_token\n ELSE NULL\n END,\n materialized_starts_at = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_starts_at\n ELSE NULL\n END,\n materialized_ends_at = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_ends_at\n ELSE NULL\n END,\n materialized_start_date = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_start_date\n ELSE NULL\n END,\n materialized_end_date = CASE\n WHEN id = ANY($2::uuid[]) THEN materialized_end_date\n ELSE NULL\n END,\n updated_at = now()\n WHERE account_id = $1\n AND is_deleted IS DISTINCT FROM NOT (id = ANY($2::uuid[]))\n RETURNING id, is_deleted\n ",
"describe": {
- "columns": [],
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "is_deleted",
+ "type_info": "Bool"
+ }
+ ],
"parameters": {
"Left": [
"Uuid",
"UuidArray"
]
},
- "nullable": []
+ "nullable": [
+ false,
+ false
+ ]
},
- "hash": "fd5ef49ce49df5ac1fbec8580a60a212664a563f1eb0bc4d61062b696cf9c320"
+ "hash": "100aae2dcbdee28bf29600b581d32e14c71ad209e0e74191349555aea5822902"
}
diff --git a/.sqlx/query-4e3d0cf0fce5c9c7621a9797671543dce476c9d0f6a313961eed66309702b412.json b/.sqlx/query-4e3d0cf0fce5c9c7621a9797671543dce476c9d0f6a313961eed66309702b412.json
new file mode 100644
index 0000000000..529f0e2c52
--- /dev/null
+++ b/.sqlx/query-4e3d0cf0fce5c9c7621a9797671543dce476c9d0f6a313961eed66309702b412.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO calendar_change_counters (link_id, seq)\n VALUES ($1, $2)\n ON CONFLICT (link_id) DO UPDATE\n SET seq = calendar_change_counters.seq + EXCLUDED.seq,\n updated_at = now()\n RETURNING seq\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "seq",
+ "type_info": "Int8"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "4e3d0cf0fce5c9c7621a9797671543dce476c9d0f6a313961eed66309702b412"
+}
diff --git a/.sqlx/query-7394aac245a454138793742b5c5d4658a910f257f65278dbc237412d7fa76831.json b/.sqlx/query-7394aac245a454138793742b5c5d4658a910f257f65278dbc237412d7fa76831.json
new file mode 100644
index 0000000000..cf7ddd0073
--- /dev/null
+++ b/.sqlx/query-7394aac245a454138793742b5c5d4658a910f257f65278dbc237412d7fa76831.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO calendar_change_log (link_id, seq, kind, event_id, calendar_id)\n SELECT $1, change.seq, change.kind, change.event_id, change.calendar_id\n FROM unnest($2::bigint[], $3::smallint[], $4::uuid[], $5::uuid[])\n AS change(seq, kind, event_id, calendar_id)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Int8Array",
+ "Int2Array",
+ "UuidArray",
+ "UuidArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "7394aac245a454138793742b5c5d4658a910f257f65278dbc237412d7fa76831"
+}
diff --git a/.sqlx/query-707e3644cb7cc2292b4b3415aee2802d5ea1c34c434b854b8bccd877e57cdcd4.json b/.sqlx/query-7c7762d3f6eec9f0cc867fd3be9f6224c323ab0cfc40b4caf0ce8ca47c4a0da7.json
similarity index 58%
rename from .sqlx/query-707e3644cb7cc2292b4b3415aee2802d5ea1c34c434b854b8bccd877e57cdcd4.json
rename to .sqlx/query-7c7762d3f6eec9f0cc867fd3be9f6224c323ab0cfc40b4caf0ce8ca47c4a0da7.json
index 842ce6e005..d500363c8f 100644
--- a/.sqlx/query-707e3644cb7cc2292b4b3415aee2802d5ea1c34c434b854b8bccd877e57cdcd4.json
+++ b/.sqlx/query-7c7762d3f6eec9f0cc867fd3be9f6224c323ab0cfc40b4caf0ce8ca47c4a0da7.json
@@ -1,8 +1,14 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendars\n SET last_sync_error = $3,\n last_sync_error_at = now(),\n consecutive_sync_failures = consecutive_sync_failures + 1,\n updated_at = now()\n WHERE id = $1\n AND account_id = $2\n AND NOT is_deleted\n ",
+ "query": "\n UPDATE calendars\n SET last_sync_error = $3,\n last_sync_error_at = now(),\n consecutive_sync_failures = consecutive_sync_failures + 1,\n updated_at = now()\n WHERE id = $1\n AND account_id = $2\n AND NOT is_deleted\n RETURNING consecutive_sync_failures\n ",
"describe": {
- "columns": [],
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "consecutive_sync_failures",
+ "type_info": "Int4"
+ }
+ ],
"parameters": {
"Left": [
"Uuid",
@@ -10,7 +16,9 @@
"Text"
]
},
- "nullable": []
+ "nullable": [
+ false
+ ]
},
- "hash": "707e3644cb7cc2292b4b3415aee2802d5ea1c34c434b854b8bccd877e57cdcd4"
+ "hash": "7c7762d3f6eec9f0cc867fd3be9f6224c323ab0cfc40b4caf0ce8ca47c4a0da7"
}
diff --git a/.sqlx/query-7e7b5bd9dd136f1a124abd11ebc502ea3b5fd9cb93131a36a6a57f6b26ddfa67.json b/.sqlx/query-7e7b5bd9dd136f1a124abd11ebc502ea3b5fd9cb93131a36a6a57f6b26ddfa67.json
new file mode 100644
index 0000000000..8346260765
--- /dev/null
+++ b/.sqlx/query-7e7b5bd9dd136f1a124abd11ebc502ea3b5fd9cb93131a36a6a57f6b26ddfa67.json
@@ -0,0 +1,61 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH visible AS (\n SELECT id AS link_id FROM email_links WHERE macro_id = $1\n UNION\n SELECT link_id FROM macro_user_links WHERE primary_macro_id = $1\n ),\n since AS (\n SELECT position.link_id, position.seq\n FROM unnest($2::uuid[], $3::bigint[]) AS position(link_id, seq)\n )\n SELECT\n visible.link_id AS \"link_id!\",\n COALESCE(counter.seq, 0) AS \"counter!\",\n COALESCE(\n (\n SELECT min(retained.seq) - 1\n FROM calendar_change_log retained\n WHERE retained.link_id = visible.link_id\n ),\n counter.seq,\n 0\n ) AS \"floor!\",\n change.seq AS \"seq?\",\n change.kind AS \"kind?\",\n change.event_id AS \"event_id?\",\n change.calendar_id AS \"calendar_id?\"\n FROM visible\n LEFT JOIN calendar_change_counters counter ON counter.link_id = visible.link_id\n LEFT JOIN since ON since.link_id = visible.link_id\n LEFT JOIN LATERAL (\n SELECT log.seq, log.kind, log.event_id, log.calendar_id\n FROM calendar_change_log log\n WHERE log.link_id = visible.link_id\n AND since.seq IS NOT NULL\n AND log.seq > since.seq\n ORDER BY log.seq\n LIMIT $4\n ) change ON true\n ORDER BY visible.link_id, change.seq\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "link_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "counter!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 2,
+ "name": "floor!",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 3,
+ "name": "seq?",
+ "type_info": "Int8"
+ },
+ {
+ "ordinal": 4,
+ "name": "kind?",
+ "type_info": "Int2"
+ },
+ {
+ "ordinal": 5,
+ "name": "event_id?",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 6,
+ "name": "calendar_id?",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray",
+ "Int8Array",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null,
+ null,
+ null,
+ false,
+ false,
+ true,
+ true
+ ]
+ },
+ "hash": "7e7b5bd9dd136f1a124abd11ebc502ea3b5fd9cb93131a36a6a57f6b26ddfa67"
+}
diff --git a/.sqlx/query-7352a4e38f94ff4966822f67d54e7199a7b089264a0d632501aa73e13807df87.json b/.sqlx/query-7f0b4a6ce456c7b58194e6a73f6badebb1e38c13bc83855ce8d8c2bbf9684976.json
similarity index 61%
rename from .sqlx/query-7352a4e38f94ff4966822f67d54e7199a7b089264a0d632501aa73e13807df87.json
rename to .sqlx/query-7f0b4a6ce456c7b58194e6a73f6badebb1e38c13bc83855ce8d8c2bbf9684976.json
index 658f935476..3a3012aae0 100644
--- a/.sqlx/query-7352a4e38f94ff4966822f67d54e7199a7b089264a0d632501aa73e13807df87.json
+++ b/.sqlx/query-7f0b4a6ce456c7b58194e6a73f6badebb1e38c13bc83855ce8d8c2bbf9684976.json
@@ -1,14 +1,22 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendars\n SET is_deleted = true,\n sync_token = NULL,\n materialized_starts_at = NULL,\n materialized_ends_at = NULL,\n materialized_start_date = NULL,\n materialized_end_date = NULL,\n updated_at = now()\n WHERE account_id = $1\n ",
+ "query": "\n UPDATE calendars\n SET is_deleted = true,\n sync_token = NULL,\n materialized_starts_at = NULL,\n materialized_ends_at = NULL,\n materialized_start_date = NULL,\n materialized_end_date = NULL,\n updated_at = now()\n WHERE account_id = $1\n RETURNING id\n ",
"describe": {
- "columns": [],
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
"parameters": {
"Left": [
"Uuid"
]
},
- "nullable": []
+ "nullable": [
+ false
+ ]
},
- "hash": "7352a4e38f94ff4966822f67d54e7199a7b089264a0d632501aa73e13807df87"
+ "hash": "7f0b4a6ce456c7b58194e6a73f6badebb1e38c13bc83855ce8d8c2bbf9684976"
}
diff --git a/.sqlx/query-94a36ec06a520a7049eee24ce8e37038ef4d055ed10f63d9196a476983a15304.json b/.sqlx/query-94a36ec06a520a7049eee24ce8e37038ef4d055ed10f63d9196a476983a15304.json
new file mode 100644
index 0000000000..f059e166c2
--- /dev/null
+++ b/.sqlx/query-94a36ec06a520a7049eee24ce8e37038ef4d055ed10f63d9196a476983a15304.json
@@ -0,0 +1,83 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n calendar.id,\n link.id AS email_link_id,\n link.email_address,\n calendar.name,\n calendar.color,\n calendar.is_primary,\n calendar.access_role,\n calendar.provider_calendar_id,\n calendar.default_reminders,\n calendar.last_sync_error,\n calendar.consecutive_sync_failures\n FROM email_links link\n JOIN calendar_accounts account ON account.email_link_id = link.id\n JOIN calendars calendar ON calendar.account_id = account.id\n WHERE calendar.id = ANY($2::uuid[])\n AND NOT calendar.is_deleted\n AND account.sync_status <> 'disabled'\n AND (\n link.macro_id = $1\n OR EXISTS (\n SELECT 1\n FROM macro_user_links delegation\n WHERE delegation.link_id = link.id\n AND delegation.primary_macro_id = $1\n )\n )\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "email_link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "email_address",
+ "type_info": "Varchar"
+ },
+ {
+ "ordinal": 3,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "color",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "is_primary",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 6,
+ "name": "access_role",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 7,
+ "name": "provider_calendar_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 8,
+ "name": "default_reminders",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 9,
+ "name": "last_sync_error",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 10,
+ "name": "consecutive_sync_failures",
+ "type_info": "Int4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false,
+ true,
+ false,
+ true,
+ false,
+ false,
+ true,
+ false
+ ]
+ },
+ "hash": "94a36ec06a520a7049eee24ce8e37038ef4d055ed10f63d9196a476983a15304"
+}
diff --git a/.sqlx/query-9e4cd9614b7cfa330ad6c1f702804f1304b52e0b61ea13d6d136fb218d6984ee.json b/.sqlx/query-9e4cd9614b7cfa330ad6c1f702804f1304b52e0b61ea13d6d136fb218d6984ee.json
new file mode 100644
index 0000000000..f30ac5891a
--- /dev/null
+++ b/.sqlx/query-9e4cd9614b7cfa330ad6c1f702804f1304b52e0b61ea13d6d136fb218d6984ee.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH doomed AS (\n SELECT link_id, seq\n FROM calendar_change_log\n WHERE created_at < $1\n ORDER BY created_at, link_id, seq\n LIMIT $2\n )\n DELETE FROM calendar_change_log log\n USING doomed\n WHERE log.link_id = doomed.link_id\n AND log.seq = doomed.seq\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Timestamptz",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9e4cd9614b7cfa330ad6c1f702804f1304b52e0b61ea13d6d136fb218d6984ee"
+}
diff --git a/.sqlx/query-5d684a90e538f05eb71cb4ec127da7f8698082e6a36f7e53dd3ec09900fd5684.json b/.sqlx/query-a3569aa00880382627378a7865053dd865a9ace73d64b8c62ed9148ea2d57d41.json
similarity index 66%
rename from .sqlx/query-5d684a90e538f05eb71cb4ec127da7f8698082e6a36f7e53dd3ec09900fd5684.json
rename to .sqlx/query-a3569aa00880382627378a7865053dd865a9ace73d64b8c62ed9148ea2d57d41.json
index f2c5350ba1..16ee6ee721 100644
--- a/.sqlx/query-5d684a90e538f05eb71cb4ec127da7f8698082e6a36f7e53dd3ec09900fd5684.json
+++ b/.sqlx/query-a3569aa00880382627378a7865053dd865a9ace73d64b8c62ed9148ea2d57d41.json
@@ -1,14 +1,28 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendar_event_sources source\n SET is_read_only = live.is_read_only,\n normalized_payload = jsonb_set(\n source.normalized_payload,\n '{event,isReadOnly}',\n to_jsonb(live.is_read_only)\n )\n FROM (\n SELECT\n calendar.id AS calendar_id,\n NOT COALESCE(calendar.access_role IN ('owner', 'writer'), false)\n AS is_read_only\n FROM calendars calendar\n WHERE calendar.account_id = $1\n ) live\n WHERE source.calendar_id = live.calendar_id\n AND source.is_read_only IS DISTINCT FROM live.is_read_only\n ",
+ "query": "\n UPDATE calendar_event_sources source\n SET is_read_only = live.is_read_only,\n normalized_payload = jsonb_set(\n source.normalized_payload,\n '{event,isReadOnly}',\n to_jsonb(live.is_read_only)\n )\n FROM (\n SELECT\n calendar.id AS calendar_id,\n NOT COALESCE(calendar.access_role IN ('owner', 'writer'), false)\n AS is_read_only\n FROM calendars calendar\n WHERE calendar.account_id = $1\n ) live\n WHERE source.calendar_id = live.calendar_id\n AND source.is_read_only IS DISTINCT FROM live.is_read_only\n RETURNING source.event_id, source.source_link_id\n ",
"describe": {
- "columns": [],
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "event_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "source_link_id",
+ "type_info": "Uuid"
+ }
+ ],
"parameters": {
"Left": [
"Uuid"
]
},
- "nullable": []
+ "nullable": [
+ false,
+ false
+ ]
},
- "hash": "5d684a90e538f05eb71cb4ec127da7f8698082e6a36f7e53dd3ec09900fd5684"
+ "hash": "a3569aa00880382627378a7865053dd865a9ace73d64b8c62ed9148ea2d57d41"
}
diff --git a/.sqlx/query-a8296d31c2ffecb9d60db37e04c062c83e529c91b5b540c4b534e38bfad4190f.json b/.sqlx/query-a8296d31c2ffecb9d60db37e04c062c83e529c91b5b540c4b534e38bfad4190f.json
new file mode 100644
index 0000000000..1b11951e14
--- /dev/null
+++ b/.sqlx/query-a8296d31c2ffecb9d60db37e04c062c83e529c91b5b540c4b534e38bfad4190f.json
@@ -0,0 +1,12 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n DELETE FROM calendar_change_counters counter\n WHERE NOT EXISTS (\n SELECT 1 FROM email_links link WHERE link.id = counter.link_id\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": []
+ },
+ "hash": "a8296d31c2ffecb9d60db37e04c062c83e529c91b5b540c4b534e38bfad4190f"
+}
diff --git a/.sqlx/query-af512a0ac6ba5bcc85a9d9e0c886fa86cd23d43f38629ef2981399f86c270286.json b/.sqlx/query-af512a0ac6ba5bcc85a9d9e0c886fa86cd23d43f38629ef2981399f86c270286.json
deleted file mode 100644
index 16157641c9..0000000000
--- a/.sqlx/query-af512a0ac6ba5bcc85a9d9e0c886fa86cd23d43f38629ef2981399f86c270286.json
+++ /dev/null
@@ -1,29 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT time_zone, default_reminders\n FROM calendars\n WHERE account_id = $1 AND provider_calendar_id = $2\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "time_zone",
- "type_info": "Text"
- },
- {
- "ordinal": 1,
- "name": "default_reminders",
- "type_info": "Jsonb"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- true,
- false
- ]
- },
- "hash": "af512a0ac6ba5bcc85a9d9e0c886fa86cd23d43f38629ef2981399f86c270286"
-}
diff --git a/.sqlx/query-c5af5b736c7193501447485280aa38b2bf3ff8aab9c763811e32826d00c58dee.json b/.sqlx/query-c5af5b736c7193501447485280aa38b2bf3ff8aab9c763811e32826d00c58dee.json
new file mode 100644
index 0000000000..efada0be4d
--- /dev/null
+++ b/.sqlx/query-c5af5b736c7193501447485280aa38b2bf3ff8aab9c763811e32826d00c58dee.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT totals.event_id AS \"event_id!\"\n FROM (\n SELECT\n requested.event_id,\n requested.ord,\n (sum(count(occurrence.event_id)) OVER (ORDER BY requested.ord))::bigint AS running\n FROM unnest($1::uuid[]) WITH ORDINALITY AS requested(event_id, ord)\n LEFT JOIN calendar_event_occurrences occurrence\n ON occurrence.event_id = requested.event_id\n AND NOT occurrence.is_cancelled\n GROUP BY requested.event_id, requested.ord\n ) totals\n WHERE totals.ord = 1 OR totals.running <= $2::bigint\n ORDER BY totals.ord\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "event_id!",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "c5af5b736c7193501447485280aa38b2bf3ff8aab9c763811e32826d00c58dee"
+}
diff --git a/.sqlx/query-cd53c7cf9b8aac6edf83e11fde6db0b391c0c3fa30c76a581c9b6a891220f49a.json b/.sqlx/query-cd53c7cf9b8aac6edf83e11fde6db0b391c0c3fa30c76a581c9b6a891220f49a.json
new file mode 100644
index 0000000000..6fdcf5c48b
--- /dev/null
+++ b/.sqlx/query-cd53c7cf9b8aac6edf83e11fde6db0b391c0c3fa30c76a581c9b6a891220f49a.json
@@ -0,0 +1,59 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n time_zone, default_reminders, name, color, access_role, is_primary, is_deleted\n FROM calendars\n WHERE account_id = $1 AND provider_calendar_id = $2\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "time_zone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "default_reminders",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 2,
+ "name": "name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "color",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "access_role",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 5,
+ "name": "is_primary",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 6,
+ "name": "is_deleted",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ true,
+ false,
+ false,
+ true,
+ true,
+ false,
+ false
+ ]
+ },
+ "hash": "cd53c7cf9b8aac6edf83e11fde6db0b391c0c3fa30c76a581c9b6a891220f49a"
+}
diff --git a/.sqlx/query-d86d248ba47e80aeda63aadc9b7051e423b86e08b7c97e3006daf4f5aa2a9ec1.json b/.sqlx/query-d86d248ba47e80aeda63aadc9b7051e423b86e08b7c97e3006daf4f5aa2a9ec1.json
new file mode 100644
index 0000000000..d02e7f7718
--- /dev/null
+++ b/.sqlx/query-d86d248ba47e80aeda63aadc9b7051e423b86e08b7c97e3006daf4f5aa2a9ec1.json
@@ -0,0 +1,257 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n 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 event.sequence,\n event.is_read_only,\n event.reminders_use_default,\n event.reminder_overrides,\n event.created_at,\n event.updated_at\n FROM calendar_event_occurrences occurrence\n JOIN calendar_events event ON event.id = occurrence.event_id\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 occurrence.event_id = ANY($2::uuid[])\n AND occurrence.owner_id IN (\n SELECT $1::text\n UNION\n SELECT link.child_macro_id\n FROM macro_user_links link\n WHERE link.primary_macro_id = $1\n )\n AND event.status <> 'cancelled'\n AND NOT occurrence.is_cancelled\n AND (\n event.owner_id = $1\n OR EXISTS (\n SELECT 1\n FROM macro_user_links link\n WHERE link.link_id = event.source_link_id\n AND link.primary_macro_id = $1\n )\n )\n ORDER BY\n occurrence.event_id,\n COALESCE(occurrence.starts_at, occurrence.start_date::timestamp AT TIME ZONE 'UTC'),\n occurrence.occurrence_key\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "event_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "source_link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "occurrence_key",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "recurrence_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "occurrence_starts_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 5,
+ "name": "occurrence_ends_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 6,
+ "name": "occurrence_start_date",
+ "type_info": "Date"
+ },
+ {
+ "ordinal": 7,
+ "name": "occurrence_end_date",
+ "type_info": "Date"
+ },
+ {
+ "ordinal": 8,
+ "name": "is_cancelled",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 9,
+ "name": "override_title",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 10,
+ "name": "override_description",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 11,
+ "name": "override_location",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 12,
+ "name": "override_status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 13,
+ "name": "owner_id",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 14,
+ "name": "ical_uid",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 15,
+ "name": "title",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 16,
+ "name": "description",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 17,
+ "name": "location",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 18,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 19,
+ "name": "visibility",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 20,
+ "name": "transparency",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 21,
+ "name": "event_type",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 22,
+ "name": "starts_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 23,
+ "name": "ends_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 24,
+ "name": "start_date",
+ "type_info": "Date"
+ },
+ {
+ "ordinal": 25,
+ "name": "end_date",
+ "type_info": "Date"
+ },
+ {
+ "ordinal": 26,
+ "name": "time_zone",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 27,
+ "name": "recurrence_lines",
+ "type_info": "TextArray"
+ },
+ {
+ "ordinal": 28,
+ "name": "organizer_email",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 29,
+ "name": "organizer_name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 30,
+ "name": "creator_email",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 31,
+ "name": "creator_name",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 32,
+ "name": "conference_url",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 33,
+ "name": "conference_provider",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 34,
+ "name": "sequence",
+ "type_info": "Int4"
+ },
+ {
+ "ordinal": 35,
+ "name": "is_read_only",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 36,
+ "name": "reminders_use_default",
+ "type_info": "Bool"
+ },
+ {
+ "ordinal": 37,
+ "name": "reminder_overrides",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 38,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 39,
+ "name": "updated_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "UuidArray"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ true,
+ true,
+ true,
+ true,
+ true,
+ false,
+ true,
+ true,
+ true,
+ true,
+ false,
+ false,
+ false,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ true,
+ true,
+ true,
+ true,
+ true,
+ false,
+ true,
+ true,
+ true,
+ true,
+ true,
+ true,
+ false,
+ false,
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "d86d248ba47e80aeda63aadc9b7051e423b86e08b7c97e3006daf4f5aa2a9ec1"
+}
diff --git a/.sqlx/query-76f024f53145b7aaebc22382820fccaacafd9247a97a6e95391615d488f40bdf.json b/.sqlx/query-d8bb6417c6e8302ef00911f574f832417392d8266412f0eecdc51beede401974.json
similarity index 70%
rename from .sqlx/query-76f024f53145b7aaebc22382820fccaacafd9247a97a6e95391615d488f40bdf.json
rename to .sqlx/query-d8bb6417c6e8302ef00911f574f832417392d8266412f0eecdc51beede401974.json
index 499aea49f8..7a75e2dc84 100644
--- a/.sqlx/query-76f024f53145b7aaebc22382820fccaacafd9247a97a6e95391615d488f40bdf.json
+++ b/.sqlx/query-d8bb6417c6e8302ef00911f574f832417392d8266412f0eecdc51beede401974.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendar_events event\n SET is_read_only = source.is_read_only\n FROM calendar_event_sources source\n WHERE source.id = event.content_source_id\n AND source.account_id = $1\n AND event.is_read_only IS DISTINCT FROM source.is_read_only\n RETURNING event.id, event.owner_id\n ",
+ "query": "\n UPDATE calendar_events event\n SET is_read_only = source.is_read_only\n FROM calendar_event_sources source\n WHERE source.id = event.content_source_id\n AND source.account_id = $1\n AND event.is_read_only IS DISTINCT FROM source.is_read_only\n RETURNING event.id, event.owner_id, event.source_link_id\n ",
"describe": {
"columns": [
{
@@ -12,6 +12,11 @@
"ordinal": 1,
"name": "owner_id",
"type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "source_link_id",
+ "type_info": "Uuid"
}
],
"parameters": {
@@ -20,9 +25,10 @@
]
},
"nullable": [
+ false,
false,
false
]
},
- "hash": "76f024f53145b7aaebc22382820fccaacafd9247a97a6e95391615d488f40bdf"
+ "hash": "d8bb6417c6e8302ef00911f574f832417392d8266412f0eecdc51beede401974"
}
diff --git a/.sqlx/query-fbbc03499afaa971d0a1a290c734e93f14d7d169e96ed7950345fd386d7e5480.json b/.sqlx/query-fbbc03499afaa971d0a1a290c734e93f14d7d169e96ed7950345fd386d7e5480.json
new file mode 100644
index 0000000000..05aed6dbca
--- /dev/null
+++ b/.sqlx/query-fbbc03499afaa971d0a1a290c734e93f14d7d169e96ed7950345fd386d7e5480.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT last_sync_error, consecutive_sync_failures\n FROM calendars\n WHERE id = $1\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "last_sync_error",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "consecutive_sync_failures",
+ "type_info": "Int4"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true,
+ false
+ ]
+ },
+ "hash": "fbbc03499afaa971d0a1a290c734e93f14d7d169e96ed7950345fd386d7e5480"
+}
diff --git a/crates/calendar_events/src/domain/changes.rs b/crates/calendar_events/src/domain/changes.rs
new file mode 100644
index 0000000000..70f78abe20
--- /dev/null
+++ b/crates/calendar_events/src/domain/changes.rs
@@ -0,0 +1,511 @@
+//! The per-email-link calendar change log: watermarks, delta pages, and the
+//! policy that turns raw log rows into a bounded, gap-free page.
+//!
+//! Every calendar write transaction appends its changes to the log of the
+//! email link the changed rows sync through, under a per-link sequence. A
+//! client remembers the last sequence it applied for each visible link and
+//! asks for everything after it; visibility is resolved at read time from the
+//! links the viewer owns or is delegated.
+
+use std::{
+ collections::{BTreeMap, HashMap, HashSet},
+ future::Future,
+};
+
+use chrono::{DateTime, Utc};
+use rootcause::Report;
+use uuid::Uuid;
+
+use super::models::{
+ ActorInboxes, CalendarEvent, CalendarOccurrence, OccurrenceException, VisibleCalendar,
+};
+
+#[cfg(test)]
+mod test;
+
+/// Most distinct events one delta page hydrates.
+pub const MAX_DELTA_EVENTS: usize = 500;
+/// Most occurrences one delta page carries. A page always carries at least
+/// one event so a single very long series still makes progress.
+pub const MAX_DELTA_OCCURRENCES: usize = 5000;
+/// Most log rows read for one link per page.
+pub const MAX_DELTA_ROWS_PER_LINK: usize = 2000;
+/// How long log rows are kept. A client whose watermark is older must reset.
+pub const CHANGE_LOG_RETENTION: chrono::Duration = chrono::Duration::days(30);
+/// Most log rows one retention batch removes.
+pub const CHANGE_LOG_PRUNE_BATCH: usize = 10_000;
+
+/// One logged change to an event or a calendar.
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub enum CalendarChangeOp {
+ /// The event was created or rewritten.
+ UpsertEvent(Uuid),
+ /// The event no longer exists.
+ DeleteEvent(Uuid),
+ /// The calendar was created, restored, or changed a visible field.
+ UpsertCalendar(Uuid),
+ /// The calendar was removed.
+ DeleteCalendar(Uuid),
+}
+
+/// One row of a link's change log.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct CalendarChange {
+ /// Position in the link's log, starting at 1.
+ pub seq: i64,
+ /// What changed.
+ pub op: CalendarChangeOp,
+}
+
+/// How far a client has applied one link's log.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct CalendarLinkWatermark {
+ /// Email link identifier.
+ pub link_id: Uuid,
+ /// Last applied sequence; 0 before anything was logged.
+ pub seq: i64,
+}
+
+/// A client's position in every visible link's log.
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub struct CalendarWatermark(BTreeMap<Uuid, i64>);
+
+impl CalendarWatermark {
+ /// Build a watermark from per-link positions. A link listed twice keeps
+ /// its lowest position, so nothing between the two is skipped.
+ pub fn from_links(links: impl IntoIterator<Item = CalendarLinkWatermark>) -> Self {
+ let mut positions = BTreeMap::new();
+ for link in links {
+ positions
+ .entry(link.link_id)
+ .and_modify(|seq: &mut i64| *seq = (*seq).min(link.seq))
+ .or_insert(link.seq);
+ }
+ Self(positions)
+ }
+
+ /// Whether no link is tracked.
+ pub fn is_empty(&self) -> bool {
+ self.0.is_empty()
+ }
+
+ /// The position for `link_id`, when tracked.
+ pub fn seq(&self, link_id: Uuid) -> Option<i64> {
+ self.0.get(&link_id).copied()
+ }
+
+ /// Per-link positions ordered by link.
+ pub fn links(&self) -> impl Iterator<Item = CalendarLinkWatermark> + '_ {
+ self.0
+ .iter()
+ .map(|(&link_id, &seq)| CalendarLinkWatermark { link_id, seq })
+ }
+}
+
+/// One visible link's log, read in a single snapshot.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct CalendarLinkLog {
+ /// Email link identifier.
+ pub link_id: Uuid,
+ /// Last sequence written for the link; 0 when nothing was ever logged.
+ pub counter: i64,
+ /// Lowest position a client may resume from. Rows at or below it were
+ /// pruned, so a client behind it has lost changes.
+ pub floor: i64,
+ /// Rows after the requested position in sequence order, at most the
+ /// requested limit. Empty when the link was not requested.
+ pub changes: Vec<CalendarChange>,
+}
+
+/// One instance of a changed event.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct EventOccurrence {
+ /// The materialized instance.
+ pub occurrence: CalendarOccurrence,
+ /// The instance's exception content, empty when it has none.
+ pub exception: OccurrenceException,
+}
+
+/// A changed event with its complete current set of visible occurrences.
+/// Clients replace everything they hold for the event with this.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct CalendarEventChange {
+ /// The series event.
+ pub event: CalendarEvent,
+ /// Email link the event syncs through.
+ pub link_id: Uuid,
+ /// Every non-cancelled materialized occurrence, ordered by start.
+ pub occurrences: Vec<EventOccurrence>,
+}
+
+/// The changed events a page could afford to load.
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub struct EventChangeLoad {
+ /// Considered events that are still visible with a visible occurrence.
+ pub events: Vec<CalendarEventChange>,
+ /// How many requested ids, from the front, were considered. A considered
+ /// id missing from `events` is gone; later ids were not read.
+ pub considered: usize,
+}
+
+/// One bounded page of changes after a watermark.
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub struct CalendarChangesPage {
+ /// Events created or changed, with their full occurrence sets.
+ pub events: Vec<CalendarEventChange>,
+ /// Events that no longer exist or no longer have a visible occurrence.
+ pub deleted_event_ids: Vec<Uuid>,
+ /// Calendars created or changed.
+ pub calendars: Vec<VisibleCalendar>,
+ /// Calendars that were removed.
+ pub deleted_calendar_ids: Vec<Uuid>,
+ /// The position after this page, one entry per currently visible link.
+ /// A link missing here is no longer visible.
+ pub new_watermark: CalendarWatermark,
+ /// Whether more changes follow `new_watermark`.
+ pub has_more: bool,
+ /// Whether the client must drop everything it holds and refetch: its
+ /// watermark is older than retention, or a link became visible that it
+ /// never tracked.
+ pub reset_required: bool,
+}
+
+/// Persistence reads behind the change log.
+pub trait CalendarChangeRepository: Send + Sync + 'static {
+ /// Every link the viewer can see with its current counter.
+ fn current_watermark(
+ &self,
+ viewer: &str,
+ ) -> impl Future<Output = Result<CalendarWatermark, Report>> + Send;
+
+ /// Every link the viewer can see with its counter, floor, and up to
+ /// `limit` rows after the position `since` holds for it, read in one
+ /// snapshot. Links `since` does not track carry no rows.
+ fn link_logs(
+ &self,
+ viewer: &str,
+ since: &CalendarWatermark,
+ limit: usize,
+ ) -> impl Future<Output = Result<Vec<CalendarLinkLog>, Report>> + Send;
+
+ /// The current state of the longest prefix of `event_ids` whose
+ /// materialized occurrences total at most `max_occurrences`, and always
+ /// at least the first id. Within that prefix, events the viewer cannot
+ /// see, that were deleted or cancelled, or that lost every visible
+ /// occurrence are left out.
+ fn load_event_changes(
+ &self,
+ viewer: &str,
+ event_ids: &[Uuid],
+ max_occurrences: usize,
+ ) -> impl Future<Output = Result<EventChangeLoad, Report>> + Send;
+
+ /// The calendars among `calendar_ids` that are still visible.
+ fn list_calendars_by_ids(
+ &self,
+ viewer: &str,
+ calendar_ids: &[Uuid],
+ ) -> impl Future<Output = Result<Vec<VisibleCalendar>, Report>> + Send;
+
+ /// Addresses of every connected inbox the viewer owns.
+ fn owned_inbox_emails(
+ &self,
+ viewer: &str,
+ ) -> impl Future<Output = Result<Vec<String>, Report>> + Send;
+}
+
+/// Persistence port for change-log retention.
+pub trait CalendarChangeLogPruner: Send + Sync + 'static {
+ /// Remove up to `batch` log rows created before `cutoff`, oldest first,
+ /// and the counters of links that no longer exist. Returns the number of
+ /// log rows removed.
+ fn prune_change_log(
+ &self,
+ cutoff: DateTime<Utc>,
+ batch: usize,
+ ) -> impl Future<Output = Result<usize, Report>> + Send;
+}
+
+/// Inbound service port for reading the change log.
+pub trait CalendarChangeQueryService: Send + Sync + 'static {
+ /// The viewer's current position in every visible link's log.
+ fn current_watermark(
+ &self,
+ viewer: &str,
+ ) -> impl Future<Output = Result<CalendarWatermark, Report>> + Send;
+
+ /// One bounded page of changes after `since`. An empty `since` returns
+ /// only the current watermark.
+ fn changes_since(
+ &self,
+ viewer: &str,
+ since: CalendarWatermark,
+ ) -> impl Future<Output = Result<CalendarChangesPage, Report>> + Send;
+}
+
+/// Change-log reads with the delta policy applied.
+pub struct CalendarChangeService<R> {
+ repository: R,
+}
+
+impl<R: CalendarChangeRepository> CalendarChangeService<R> {
+ /// Construct the service.
+ pub fn new(repository: R) -> Self {
+ Self { repository }
+ }
+
+ /// The viewer's current position in every visible link's log.
+ #[tracing::instrument(skip(self, viewer), err)]
+ pub async fn current_watermark(&self, viewer: &str) -> Result<CalendarWatermark, Report> {
+ self.repository.current_watermark(viewer).await
+ }
+
+ /// One bounded page of changes after `since`.
+ #[tracing::instrument(skip(self, viewer, since), err)]
+ pub async fn changes_since(
+ &self,
+ viewer: &str,
+ since: CalendarWatermark,
+ ) -> Result<CalendarChangesPage, Report> {
+ if since.is_empty() {
+ return Ok(CalendarChangesPage {
+ new_watermark: self.repository.current_watermark(viewer).await?,
+ ..CalendarChangesPage::default()
+ });
+ }
+ let logs = self
+ .repository
+ .link_logs(viewer, &since, MAX_DELTA_ROWS_PER_LINK + 1)
+ .await?;
+ if requires_reset(&logs, &since) {
+ return Ok(CalendarChangesPage {
+ new_watermark: counters(&logs),
+ reset_required: true,
+ ..CalendarChangesPage::default()
+ });
+ }
+
+ let walk = walk_logs(&logs, &since, MAX_DELTA_EVENTS);
+ let upserted: Vec<Uuid> = walk.upserted_events().collect();
+ let load = if upserted.is_empty() {
+ EventChangeLoad::default()
+ } else {
+ self.repository
+ .load_event_changes(viewer, &upserted, MAX_DELTA_OCCURRENCES)
+ .await?
+ };
+ // Events past the loaded prefix wait for the next page: walking again
+ // with a smaller budget stops each link right before the first of them.
+ let walk = match upserted.get(load.considered) {
+ Some(&unread) => walk_logs(&logs, &since, walk.event_position(unread)),
+ None => walk,
+ };
+ let mut hydrated: HashMap<Uuid, CalendarEventChange> = load
+ .events
+ .into_iter()
+ .map(|change| (change.event.id, change))
+ .collect();
+
+ let upserted_calendars: Vec<Uuid> = walk.upserted_calendars().collect();
+ let calendars = if upserted_calendars.is_empty() {
+ Vec::new()
+ } else {
+ self.repository
+ .list_calendars_by_ids(viewer, &upserted_calendars)
+ .await?
+ };
+ let listed_calendars: HashSet<Uuid> =
+ calendars.iter().map(|calendar| calendar.id).collect();
+
+ let mut page = CalendarChangesPage {
+ calendars,
+ new_watermark: walk.watermark,
+ has_more: walk.truncated,
+ ..CalendarChangesPage::default()
+ };
+ for op in walk.ops {
+ match op {
+ CalendarChangeOp::UpsertEvent(id) => match hydrated.remove(&id) {
+ Some(change) => page.events.push(change),
+ None => page.deleted_event_ids.push(id),
+ },
+ CalendarChangeOp::DeleteEvent(id) => page.deleted_event_ids.push(id),
+ CalendarChangeOp::UpsertCalendar(id) if !listed_calendars.contains(&id) => {
+ page.deleted_calendar_ids.push(id)
+ }
+ CalendarChangeOp::UpsertCalendar(_) => {}
+ CalendarChangeOp::DeleteCalendar(id) => page.deleted_calendar_ids.push(id),
+ }
+ }
+ if let Some(viewer) =
+ ActorInboxes::from_owned(self.repository.owned_inbox_emails(viewer).await?)
+ {
+ for change in &mut page.events {
+ viewer.mark_attendees(&mut change.event.attendees);
+ for instance in &mut change.occurrences {
+ if let Some(attendees) = &mut instance.exception.attendees {
+ viewer.mark_attendees(attendees);
+ }
+ }
+ }
+ }
+ Ok(page)
+ }
+}
+
+impl<R: CalendarChangeRepository> CalendarChangeQueryService for CalendarChangeService<R> {
+ fn current_watermark(
+ &self,
+ viewer: &str,
+ ) -> impl Future<Output = Result<CalendarWatermark, Report>> + Send {
+ CalendarChangeService::current_watermark(self, viewer)
+ }
+
+ fn changes_since(
+ &self,
+ viewer: &str,
+ since: CalendarWatermark,
+ ) -> impl Future<Output = Result<CalendarChangesPage, Report>> + Send {
+ CalendarChangeService::changes_since(self, viewer, since)
+ }
+}
+
+/// A client must reset when it fell behind retention on any link, or when a
+/// link became visible that it never tracked: that link's data predates its
+/// log, so replaying the log cannot reproduce it.
+fn requires_reset(logs: &[CalendarLinkLog], since: &CalendarWatermark) -> bool {
+ logs.iter().any(|log| match since.seq(log.link_id) {
+ None => true,
+ Some(seq) => seq < log.floor,
+ })
+}
+
+fn counters(logs: &[CalendarLinkLog]) -> CalendarWatermark {
+ CalendarWatermark::from_links(logs.iter().map(|log| CalendarLinkWatermark {
+ link_id: log.link_id,
+ seq: log.counter,
+ }))
+}
+
+/// The coalesced changes a page covers and where it stops.
+#[derive(Debug, PartialEq, Eq)]
+struct LogWalk {
+ /// The latest change per target, in first-seen order.
+ ops: Vec<CalendarChangeOp>,
+ watermark: CalendarWatermark,
+ truncated: bool,
+}
+
+impl LogWalk {
+ fn upserted_events(&self) -> impl Iterator<Item = Uuid> + '_ {
+ self.ops.iter().filter_map(|op| match op {
+ CalendarChangeOp::UpsertEvent(id) => Some(*id),
+ _ => None,
+ })
+ }
+
+ /// How many distinct events the walk covers before `event_id`.
+ fn event_position(&self, event_id: Uuid) -> usize {
+ self.ops
+ .iter()
+ .filter(|op| matches!(target(**op), Target::Event(_)))
+ .take_while(|op| target(**op) != Target::Event(event_id))
+ .count()
+ }
+
+ fn upserted_calendars(&self) -> impl Iterator<Item = Uuid> + '_ {
+ self.ops.iter().filter_map(|op| match op {
+ CalendarChangeOp::UpsertCalendar(id) => Some(*id),
+ _ => None,
+ })
+ }
+}
+
+#[derive(Clone, Copy, PartialEq, Eq, Hash)]
+enum Target {
+ Event(Uuid),
+ Calendar(Uuid),
+}
+
+fn target(op: CalendarChangeOp) -> Target {
+ match op {
+ CalendarChangeOp::UpsertEvent(id) | CalendarChangeOp::DeleteEvent(id) => Target::Event(id),
+ CalendarChangeOp::UpsertCalendar(id) | CalendarChangeOp::DeleteCalendar(id) => {
+ Target::Calendar(id)
+ }
+ }
+}
+
+/// Walk each link's rows in sequence order, links in id order, until
+/// `max_events` distinct events are covered. A link stops on a sequence
+/// boundary: its watermark advances only past rows the page covers, so a
+/// truncated page resumes exactly where it stopped.
+fn walk_logs(logs: &[CalendarLinkLog], since: &CalendarWatermark, max_events: usize) -> LogWalk {
+ let mut sorted: Vec<&CalendarLinkLog> = logs.iter().collect();
+ sorted.sort_by_key(|log| log.link_id);
+ let mut order: Vec<Target> = Vec::new();
+ let mut latest: HashMap<Target, CalendarChangeOp> = HashMap::new();
+ let mut events = 0;
+ let mut truncated = false;
+ let mut watermark = Vec::with_capacity(sorted.len());
+ for log in sorted {
+ let start = since.seq(log.link_id).unwrap_or(log.counter);
+ let mut position = start;
+ let mut stopped = log.changes.len() > MAX_DELTA_ROWS_PER_LINK;
+ for change in log.changes.iter().take(MAX_DELTA_ROWS_PER_LINK) {
+ let key = target(change.op);
+ let new_event = matches!(key, Target::Event(_)) && !latest.contains_key(&key);
+ if new_event && events == max_events {
+ stopped = true;
+ break;
+ }
+ if new_event {
+ events += 1;
+ }
+ if latest.insert(key, change.op).is_none() {
+ order.push(key);
+ }
+ position = change.seq;
+ }
+ truncated |= stopped;
+ watermark.push(CalendarLinkWatermark {
+ link_id: log.link_id,
+ seq: if stopped { position } else { log.counter },
+ });
+ }
+ LogWalk {
+ ops: order.into_iter().map(|key| latest[&key]).collect(),
+ watermark: CalendarWatermark::from_links(watermark),
+ truncated,
+ }
+}
+
+/// Removes change-log rows older than [`CHANGE_LOG_RETENTION`].
+pub struct CalendarChangeLogRetention<R> {
+ repository: R,
+}
+
+impl<R: CalendarChangeLogPruner> CalendarChangeLogRetention<R> {
+ /// Construct the retention job from its persistence port.
+ pub fn new(repository: R) -> Self {
+ Self { repository }
+ }
+
+ /// Prune everything older than retention as of `now`, in batches.
+ /// Returns the number of rows removed.
+ #[tracing::instrument(skip(self), err)]
+ pub async fn run_once(&self, now: DateTime<Utc>) -> Result<usize, Report> {
+ let cutoff = now - CHANGE_LOG_RETENTION;
+ let mut removed = 0;
+ loop {
+ let batch = self
+ .repository
+ .prune_change_log(cutoff, CHANGE_LOG_PRUNE_BATCH)
+ .await?;
+ removed += batch;
+ if batch < CHANGE_LOG_PRUNE_BATCH {
+ return Ok(removed);
+ }
+ }
+ }
+}
diff --git a/crates/calendar_events/src/domain/changes/test.rs b/crates/calendar_events/src/domain/changes/test.rs
new file mode 100644
index 0000000000..7174b744c1
--- /dev/null
+++ b/crates/calendar_events/src/domain/changes/test.rs
@@ -0,0 +1,528 @@
+use std::sync::Mutex;
+
+use chrono::{TimeZone, Utc};
+
+use super::*;
+use crate::domain::models::{
+ AttendeeResponseStatus, CalendarAttendee, EventReminders, EventStatus, EventTime,
+ EventTransparency, EventType, EventVisibility,
+};
+
+const VIEWER: &str = "macro|[email protected]";
+const LINK_A: Uuid = Uuid::from_u128(0xa);
+const LINK_B: Uuid = Uuid::from_u128(0xb);
+
+#[derive(Default)]
+struct FakeChanges {
+ logs: Vec<CalendarLinkLog>,
+ events: HashMap<Uuid, CalendarEventChange>,
+ calendars: Vec<VisibleCalendar>,
+ occurrence_cap_events: Option<usize>,
+ owned_inboxes: Vec<String>,
+ loads: Mutex<Vec<Vec<Uuid>>>,
+}
+
+impl CalendarChangeRepository for FakeChanges {
+ async fn current_watermark(&self, _viewer: &str) -> Result<CalendarWatermark, Report> {
+ Ok(CalendarWatermark::from_links(self.logs.iter().map(|log| {
+ CalendarLinkWatermark {
+ link_id: log.link_id,
+ seq: log.counter,
+ }
+ })))
+ }
+
+ async fn link_logs(
+ &self,
+ _viewer: &str,
+ since: &CalendarWatermark,
+ limit: usize,
+ ) -> Result<Vec<CalendarLinkLog>, Report> {
+ Ok(self
+ .logs
+ .iter()
+ .map(|log| CalendarLinkLog {
+ changes: match since.seq(log.link_id) {
+ Some(seq) => log
+ .changes
+ .iter()
+ .filter(|change| change.seq > seq)
+ .take(limit)
+ .copied()
+ .collect(),
+ None => Vec::new(),
+ },
+ ..log.clone()
+ })
+ .collect())
+ }
+
+ async fn load_event_changes(
+ &self,
+ _viewer: &str,
+ event_ids: &[Uuid],
+ _max_occurrences: usize,
+ ) -> Result<EventChangeLoad, Report> {
+ self.loads.lock().unwrap().push(event_ids.to_vec());
+ let considered = self
+ .occurrence_cap_events
+ .unwrap_or(event_ids.len())
+ .min(event_ids.len());
+ Ok(EventChangeLoad {
+ events: event_ids[..considered]
+ .iter()
+ .filter_map(|id| self.events.get(id).cloned())
+ .collect(),
+ considered,
+ })
+ }
+
+ async fn list_calendars_by_ids(
+ &self,
+ _viewer: &str,
+ calendar_ids: &[Uuid],
+ ) -> Result<Vec<VisibleCalendar>, Report> {
+ Ok(self
+ .calendars
+ .iter()
+ .filter(|calendar| calendar_ids.contains(&calendar.id))
+ .cloned()
+ .collect())
+ }
+
+ async fn owned_inbox_emails(&self, _viewer: &str) -> Result<Vec<String>, Report> {
+ Ok(self.owned_inboxes.clone())
+ }
+}
+
+fn log(link_id: Uuid, floor: i64, ops: Vec<CalendarChangeOp>) -> CalendarLinkLog {
+ let changes: Vec<CalendarChange> = ops
+ .into_iter()
+ .zip(floor + 1..)
+ .map(|(op, seq)| CalendarChange { seq, op })
+ .collect();
+ CalendarLinkLog {
+ link_id,
+ counter: changes.last().map_or(floor, |change| change.seq),
+ floor,
+ changes,
+ }
+}
+
+fn since(positions: &[(Uuid, i64)]) -> CalendarWatermark {
+ CalendarWatermark::from_links(
+ positions
+ .iter()
+ .map(|&(link_id, seq)| CalendarLinkWatermark { link_id, seq }),
+ )
+}
+
+fn event_change(id: Uuid, link_id: Uuid, occurrences: i64) -> CalendarEventChange {
+ let starts_at = Utc.with_ymd_and_hms(2026, 10, 6, 15, 0, 0).unwrap();
+ let time = EventTime::Timed {
+ starts_at,
+ ends_at: starts_at + chrono::Duration::hours(1),
+ time_zone: None,
+ };
+ CalendarEventChange {
+ event: CalendarEvent {
+ id,
+ owner_id: VIEWER.to_owned(),
+ ical_uid: format!("{id}@example.com"),
+ calendar_id: None,
+ sources: Vec::new(),
+ title: "Event".to_owned(),
+ description: None,
+ location: None,
+ status: EventStatus::Confirmed,
+ visibility: EventVisibility::Default,
+ transparency: EventTransparency::Opaque,
+ event_type: EventType::Default,
+ time,
+ recurrence_lines: Vec::new(),
+ organizer_email: None,
+ organizer_name: None,
+ creator_email: None,
+ creator_name: None,
+ conference_url: None,
+ conference_provider: None,
+ sequence: 0,
+ is_read_only: false,
+ attendees: vec![CalendarAttendee {
+ email: "[email protected]".to_owned(),
+ display_name: None,
+ response_status: AttendeeResponseStatus::Accepted,
+ is_organizer: false,
+ is_optional: false,
+ is_self: false,
+ comment: None,
+ }],
+ reminders: EventReminders::default(),
+ created_at: starts_at,
+ updated_at: starts_at,
+ },
+ link_id,
+ occurrences: (0..occurrences)
+ .map(|day| {
+ let starts_at = starts_at + chrono::Duration::days(day);
+ let time = EventTime::Timed {
+ starts_at,
+ ends_at: starts_at + chrono::Duration::hours(1),
+ time_zone: None,
+ };
+ EventOccurrence {
+ occurrence: CalendarOccurrence {
+ event_id: id,
+ occurrence_key: time.occurrence_key(),
+ recurrence_id: None,
+ time,
+ is_cancelled: false,
+ },
+ exception: OccurrenceException::default(),
+ }
+ })
+ .collect(),
+ }
+}
+
+fn calendar(id: Uuid, link_id: Uuid) -> VisibleCalendar {
+ VisibleCalendar {
+ id,
+ email_link_id: link_id,
+ email_address: "[email protected]".to_owned(),
+ name: "Work".to_owned(),
+ color: None,
+ is_primary: true,
+ is_writable: true,
+ is_subscription: false,
+ sync_error: None,
+ default_reminders: Vec::new(),
+ }
+}
+
+fn id(n: u128) -> Uuid {
+ Uuid::from_u128(0x1000 + n)
+}
+
+fn budget() -> i64 {
+ i64::try_from(MAX_DELTA_EVENTS).unwrap()
+}
+
+#[tokio::test]
+async fn an_empty_watermark_bootstraps_with_the_current_positions_only() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![
+ log(LINK_A, 0, vec![CalendarChangeOp::UpsertEvent(id(1))]),
+ log(LINK_B, 4, Vec::new()),
+ ],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, CalendarWatermark::default())
+ .await
+ .unwrap();
+
+ assert_eq!(
+ page,
+ CalendarChangesPage {
+ new_watermark: since(&[(LINK_A, 1), (LINK_B, 4)]),
+ ..Default::default()
+ }
+ );
+}
+
+#[tokio::test]
+async fn a_watermark_behind_retention_requires_a_reset() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_A, 10, vec![CalendarChangeOp::UpsertEvent(id(1))])],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 9)]))
+ .await
+ .unwrap();
+
+ assert!(page.reset_required);
+ assert!(page.events.is_empty() && page.deleted_event_ids.is_empty());
+ assert_eq!(page.new_watermark, since(&[(LINK_A, 11)]));
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 10)]))
+ .await
+ .unwrap();
+ assert!(!page.reset_required);
+}
+
+#[tokio::test]
+async fn a_newly_visible_link_requires_a_reset_and_a_removed_one_drops_out() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_B, 0, Vec::new())],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 3)]))
+ .await
+ .unwrap();
+
+ assert!(page.reset_required);
+ assert_eq!(page.new_watermark, since(&[(LINK_B, 0)]));
+}
+
+#[tokio::test]
+async fn changes_coalesce_to_each_targets_latest_state() {
+ let calendar_kept = Uuid::from_u128(0xc1);
+ let calendar_gone = Uuid::from_u128(0xc2);
+ let calendar_deleted = Uuid::from_u128(0xc3);
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(
+ LINK_A,
+ 0,
+ vec![
+ CalendarChangeOp::UpsertEvent(id(1)),
+ CalendarChangeOp::DeleteEvent(id(1)),
+ CalendarChangeOp::DeleteEvent(id(2)),
+ CalendarChangeOp::UpsertEvent(id(2)),
+ CalendarChangeOp::UpsertEvent(id(3)),
+ CalendarChangeOp::UpsertCalendar(calendar_kept),
+ CalendarChangeOp::UpsertCalendar(calendar_gone),
+ CalendarChangeOp::DeleteCalendar(calendar_deleted),
+ ],
+ )],
+ events: HashMap::from([(id(2), event_change(id(2), LINK_A, 2))]),
+ calendars: vec![calendar(calendar_kept, LINK_A)],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0)]))
+ .await
+ .unwrap();
+
+ assert_eq!(page.events.len(), 1);
+ assert_eq!(page.events[0].event.id, id(2));
+ assert_eq!(page.events[0].occurrences.len(), 2);
+ assert_eq!(page.deleted_event_ids, vec![id(1), id(3)]);
+ assert_eq!(page.calendars, vec![calendar(calendar_kept, LINK_A)]);
+ assert_eq!(
+ page.deleted_calendar_ids,
+ vec![calendar_gone, calendar_deleted]
+ );
+ assert_eq!(page.new_watermark, since(&[(LINK_A, 8)]));
+ assert!(!page.has_more && !page.reset_required);
+}
+
+#[tokio::test]
+async fn a_client_ahead_of_a_lagging_replica_gets_nothing_and_the_counter() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_A, 0, vec![CalendarChangeOp::UpsertEvent(id(1))])],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 5)]))
+ .await
+ .unwrap();
+
+ assert!(page.events.is_empty() && page.deleted_event_ids.is_empty());
+ assert_eq!(page.new_watermark, since(&[(LINK_A, 1)]));
+ assert!(!page.has_more && !page.reset_required);
+}
+
+#[tokio::test]
+async fn untouched_links_keep_their_counter() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![
+ log(LINK_A, 0, vec![CalendarChangeOp::DeleteEvent(id(1))]),
+ log(LINK_B, 0, vec![CalendarChangeOp::DeleteEvent(id(2))]),
+ ],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0), (LINK_B, 1)]))
+ .await
+ .unwrap();
+
+ assert_eq!(page.deleted_event_ids, vec![id(1)]);
+ assert_eq!(page.new_watermark, since(&[(LINK_A, 1), (LINK_B, 1)]));
+}
+
+#[tokio::test]
+async fn the_event_budget_cuts_on_a_sequence_boundary() {
+ let ops: Vec<CalendarChangeOp> = (0..MAX_DELTA_EVENTS as u128 + 5)
+ .map(|n| CalendarChangeOp::DeleteEvent(id(n)))
+ .collect();
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![
+ log(LINK_A, 0, ops),
+ log(LINK_B, 0, vec![CalendarChangeOp::DeleteEvent(id(9999))]),
+ ],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0), (LINK_B, 0)]))
+ .await
+ .unwrap();
+
+ assert_eq!(page.deleted_event_ids.len(), MAX_DELTA_EVENTS);
+ assert!(page.has_more);
+ assert_eq!(
+ page.new_watermark,
+ since(&[(LINK_A, budget()), (LINK_B, 0)])
+ );
+
+ let rest = service
+ .changes_since(VIEWER, page.new_watermark)
+ .await
+ .unwrap();
+ assert_eq!(rest.deleted_event_ids.len(), 6);
+ assert!(!rest.has_more);
+ assert_eq!(
+ rest.new_watermark,
+ since(&[(LINK_A, budget() + 5), (LINK_B, 1)])
+ );
+}
+
+#[tokio::test]
+async fn repeated_changes_to_one_event_count_once_against_the_budget() {
+ let ops: Vec<CalendarChangeOp> = (0..MAX_DELTA_ROWS_PER_LINK + 3)
+ .map(|_| CalendarChangeOp::DeleteEvent(id(1)))
+ .collect();
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_A, 0, ops)],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0)]))
+ .await
+ .unwrap();
+
+ assert_eq!(page.deleted_event_ids, vec![id(1)]);
+ assert!(page.has_more, "the per-link row cap truncates the page");
+ assert_eq!(
+ page.new_watermark,
+ since(&[(LINK_A, i64::try_from(MAX_DELTA_ROWS_PER_LINK).unwrap())])
+ );
+}
+
+#[tokio::test]
+async fn events_past_the_occurrence_cap_wait_for_the_next_page() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(
+ LINK_A,
+ 0,
+ vec![
+ CalendarChangeOp::UpsertEvent(id(1)),
+ CalendarChangeOp::DeleteEvent(id(2)),
+ CalendarChangeOp::UpsertEvent(id(3)),
+ CalendarChangeOp::UpsertEvent(id(4)),
+ ],
+ )],
+ events: HashMap::from([
+ (id(1), event_change(id(1), LINK_A, 3)),
+ (id(3), event_change(id(3), LINK_A, 3)),
+ (id(4), event_change(id(4), LINK_A, 3)),
+ ]),
+ occurrence_cap_events: Some(1),
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0)]))
+ .await
+ .unwrap();
+
+ assert_eq!(page.events.len(), 1);
+ assert_eq!(page.events[0].event.id, id(1));
+ assert_eq!(
+ page.deleted_event_ids,
+ vec![id(2)],
+ "a delete before the first unread event still lands"
+ );
+ assert!(page.has_more);
+ assert_eq!(page.new_watermark, since(&[(LINK_A, 2)]));
+}
+
+#[tokio::test]
+async fn attendee_self_flags_follow_the_viewers_inboxes() {
+ let mut change = event_change(id(1), LINK_A, 1);
+ change.occurrences[0].exception.attendees = Some(change.event.attendees.clone());
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_A, 0, vec![CalendarChangeOp::UpsertEvent(id(1))])],
+ events: HashMap::from([(id(1), change)]),
+ owned_inboxes: vec!["[email protected]".to_owned()],
+ ..Default::default()
+ });
+
+ let page = service
+ .changes_since(VIEWER, since(&[(LINK_A, 0)]))
+ .await
+ .unwrap();
+
+ let change = &page.events[0];
+ assert!(change.event.attendees[0].is_self);
+ assert!(change.occurrences[0].exception.attendees.as_ref().unwrap()[0].is_self);
+}
+
+#[tokio::test]
+async fn nothing_is_hydrated_when_no_event_was_upserted() {
+ let service = CalendarChangeService::new(FakeChanges {
+ logs: vec![log(LINK_A, 0, vec![CalendarChangeOp::DeleteEvent(id(1))])],
+ ..Default::default()
+ });
+
+ service
+ .changes_since(VIEWER, since(&[(LINK_A, 0)]))
+ .await
+ .unwrap();
+
+ assert!(service.repository.loads.lock().unwrap().is_empty());
+}
+
+#[test]
+fn a_duplicated_link_keeps_its_lowest_position() {
+ let watermark = since(&[(LINK_A, 7), (LINK_A, 3)]);
+
+ assert_eq!(watermark.seq(LINK_A), Some(3));
+}
+
+struct FakePruner {
+ batches: Mutex<Vec<usize>>,
+ cutoffs: Mutex<Vec<DateTime<Utc>>>,
+}
+
+impl CalendarChangeLogPruner for FakePruner {
+ async fn prune_change_log(
+ &self,
+ cutoff: DateTime<Utc>,
+ _batch: usize,
+ ) -> Result<usize, Report> {
+ self.cutoffs.lock().unwrap().push(cutoff);
+ Ok(self.batches.lock().unwrap().remove(0))
+ }
+}
+
+#[tokio::test]
+async fn retention_prunes_in_batches_until_a_short_one() {
+ let retention = CalendarChangeLogRetention::new(FakePruner {
+ batches: Mutex::new(vec![CHANGE_LOG_PRUNE_BATCH, CHANGE_LOG_PRUNE_BATCH, 7]),
+ cutoffs: Mutex::new(Vec::new()),
+ });
+ let now = Utc.with_ymd_and_hms(2026, 10, 6, 0, 0, 0).unwrap();
+
+ let removed = retention.run_once(now).await.unwrap();
+
+ assert_eq!(removed, 2 * CHANGE_LOG_PRUNE_BATCH + 7);
+ let cutoffs = retention.repository.cutoffs.lock().unwrap();
+ assert_eq!(cutoffs.len(), 3);
+ assert!(
+ cutoffs
+ .iter()
+ .all(|cutoff| *cutoff == now - CHANGE_LOG_RETENTION)
+ );
+}
diff --git a/crates/calendar_events/src/domain/mod.rs b/crates/calendar_events/src/domain/mod.rs
index 000832d5a4..fde8589540 100644
--- a/crates/calendar_events/src/domain/mod.rs
+++ b/crates/calendar_events/src/domain/mod.rs
@@ -4,6 +4,8 @@ pub mod invitations;
/// The clicker's owned inboxes.
pub mod acting;
+/// The per-email-link calendar change log.
+pub mod changes;
/// Kafka event models for the calendar topic.
pub mod events;
/// Domain models.
diff --git a/crates/calendar_events/src/domain/models.rs b/crates/calendar_events/src/domain/models.rs
index a1f36e5fd5..a5e2274ee2 100644
--- a/crates/calendar_events/src/domain/models.rs
+++ b/crates/calendar_events/src/domain/models.rs
@@ -1498,13 +1498,17 @@ pub struct GoogleBackfillRunReport {
pub events_upserted: usize,
/// Cancellation tombstones the change feed reported this run.
pub cancellations_observed: usize,
+ /// Events removed or rewritten because a source they relied on was
+ /// retired: unobserved by a full snapshot, or on a calendar the account
+ /// no longer lists.
+ pub events_retired: usize,
}
impl GoogleBackfillRunReport {
/// Whether the run plausibly changed the local projection. Quiet
/// token-only polls report nothing and skip client notifications.
pub fn changed(&self) -> bool {
- self.events_upserted > 0 || self.cancellations_observed > 0
+ self.events_upserted > 0 || self.cancellations_observed > 0 || self.events_retired > 0
}
}
diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rs
index 3a6dbad103..e8dec5b778 100644
--- a/crates/calendar_events/src/domain/service.rs
+++ b/crates/calendar_events/src/domain/service.rs
@@ -841,6 +841,7 @@ where
// A provider-side deletion reaches search only here: the row is
// gone once the commit lands, so nothing downstream can rediscover
// it by re-reading Postgres.
+ report.events_retired += retired.len();
self.publish_retirements(retired);
// Tombstones only apply inside the snapshot commit, so they
// count once it succeeds.
@@ -946,6 +947,7 @@ where
.repository
.reconcile_google_calendar_list(key, lease_token, account_id, calendar_ids)
.await?;
+ report.events_retired += retired.len();
self.publish_retirements(retired);
Ok(())
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index d4991a93b6..1782835aa4 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -1,6 +1,10 @@
//! PostgreSQL implementation of the calendar repository port.
+mod change_log;
+mod changes;
mod invitations;
+use change_log::ChangeLogBatch;
+
use std::collections::{HashMap, HashSet};
use chrono::{DateTime, NaiveDate, Utc};
@@ -11,6 +15,7 @@ use sqlx::{PgPool, Postgres, Transaction};
use uuid::Uuid;
use crate::domain::{
+ changes::CalendarChangeOp,
models::{
ActorInboxes, AppliedGoogleGrant, AttendeeResponseStatus,
CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD, CalendarAttendee, CalendarBackfillClaim,
@@ -223,9 +228,19 @@ impl PgCalendarRepository {
account_id: Uuid,
calendar: ProviderCalendar,
) -> Result<Uuid, Report> {
+ let email_link_id = sqlx::query_scalar!(
+ "SELECT email_link_id FROM calendar_accounts WHERE id = $1",
+ account_id,
+ )
+ .fetch_one(&self.pool)
+ .await
+ .map_err(report)?;
let mut tx = self.pool.begin().await.map_err(report)?;
- let id = upsert_calendar_tx(&mut tx, account_id, calendar).await?.id;
- tx.commit().await.map_err(report)?;
+ let mut log = ChangeLogBatch::default();
+ let id = upsert_calendar_tx(&mut tx, &mut log, email_link_id, account_id, calendar)
+ .await?
+ .id;
+ log.commit(tx).await?;
Ok(id)
}
@@ -278,6 +293,41 @@ struct StoredCalendarRow {
watch_unsupported_at: Option<DateTime<Utc>>,
}
+struct VisibleCalendarRow {
+ id: Uuid,
+ email_link_id: Uuid,
+ email_address: String,
+ name: String,
+ color: Option<String>,
+ is_primary: bool,
+ access_role: Option<String>,
+ provider_calendar_id: String,
+ default_reminders: serde_json::Value,
+ last_sync_error: Option<String>,
+ consecutive_sync_failures: i32,
+}
+
+fn visible_calendar(row: VisibleCalendarRow) -> VisibleCalendar {
+ VisibleCalendar {
+ id: row.id,
+ email_link_id: row.email_link_id,
+ email_address: row.email_address,
+ name: row.name,
+ color: row.color,
+ is_primary: row.is_primary,
+ is_writable: matches!(row.access_role.as_deref(), Some("owner" | "writer")),
+ is_subscription: is_system_calendar(&row.provider_calendar_id),
+ sync_error: row
+ .last_sync_error
+ .filter(|_| row.consecutive_sync_failures >= CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD),
+ default_reminders: serde_json::from_value(row.default_reminders)
+ .inspect_err(|e| {
+ tracing::error!(error = ?e, calendar_id = %row.id, "malformed calendar default_reminders json");
+ })
+ .unwrap_or_default(),
+ }
+}
+
struct OccurrenceJoinRow {
event_id: Uuid,
source_link_id: Uuid,
@@ -479,9 +529,10 @@ impl CalendarRepository for PgCalendarRepository {
invalidate_stale_google_jobs_tx(&mut tx, email_link_id, grant_version).await?;
let mut jobs = Vec::new();
+ let mut log = ChangeLogBatch::default();
let has_calendar_capability = scopes.has_calendar_capability();
if had_calendar_capability && !has_calendar_capability {
- disable_google_calendar_capability_tx(&mut tx, email_link_id).await?;
+ disable_google_calendar_capability_tx(&mut tx, &mut log, email_link_id).await?;
}
if has_calendar_capability {
let account_id =
@@ -534,7 +585,7 @@ impl CalendarRepository for PgCalendarRepository {
}
}
- tx.commit().await.map_err(report)?;
+ log.commit(tx).await?;
Ok(AppliedGoogleGrant {
grant_version,
changed,
@@ -627,7 +678,8 @@ impl CalendarRepository for PgCalendarRepository {
// the local projection down and drop the account itself, which
// cascades its calendars and backfill jobs.
invalidate_stale_google_jobs_tx(&mut tx, email_link_id, grant_version).await?;
- disable_google_calendar_capability_tx(&mut tx, email_link_id).await?;
+ let mut log = ChangeLogBatch::default();
+ disable_google_calendar_capability_tx(&mut tx, &mut log, email_link_id).await?;
sqlx::query!(
"DELETE FROM calendar_accounts WHERE email_link_id = $1",
email_link_id,
@@ -636,7 +688,7 @@ impl CalendarRepository for PgCalendarRepository {
.await
.map_err(report)?;
- tx.commit().await.map_err(report)?;
+ log.commit(tx).await?;
Ok(Some(DisconnectedGoogleCalendar {
token_identity: CalendarLinkTokenIdentity {
fusionauth_user_id: row.fusionauth_user_id,
@@ -857,7 +909,9 @@ impl CalendarRepository for PgCalendarRepository {
.await?;
}
- tx.commit().await.map_err(report)?;
+ let mut log = ChangeLogBatch::default();
+ log.record(source_link_id, CalendarChangeOp::UpsertEvent(event_id));
+ log.commit(tx).await?;
Ok(CalendarEventWriteOutcome {
event_id,
owner_id: upsert.event.owner_id.clone(),
@@ -1003,9 +1057,10 @@ impl CalendarRepository for PgCalendarRepository {
});
let series_attendees = attendees.get(&event_id).cloned().unwrap_or_default();
let sources = source_contents.get(&event_id).cloned().unwrap_or_default();
- let (event, mut exception) =
- series_event_from_join(row, series_attendees, sources)?;
+ let mut row = row;
+ let mut exception = take_exception(&mut row);
exception.attendees = exception_attendees;
+ let event = series_event_from_join(row, series_attendees, sources)?;
Ok(OccurrenceListing {
event,
occurrence,
@@ -1347,8 +1402,10 @@ impl CalendarRepository for PgCalendarRepository {
) -> Result<StoredGoogleCalendar, Report> {
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;
- let calendar = upsert_calendar_tx(&mut tx, account_id, calendar).await?;
- tx.commit().await.map_err(report)?;
+ let mut log = ChangeLogBatch::default();
+ let calendar =
+ upsert_calendar_tx(&mut tx, &mut log, key.email_link_id, account_id, calendar).await?;
+ log.commit(tx).await?;
stored_google_calendar(calendar)
}
@@ -1362,6 +1419,7 @@ impl CalendarRepository for PgCalendarRepository {
events_upserted: usize,
) -> Result<Vec<RetiredCalendarEvent>, Report> {
let mut retired = Vec::new();
+ let mut log = ChangeLogBatch::default();
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;
@@ -1414,7 +1472,9 @@ impl CalendarRepository for PgCalendarRepository {
.await
.map_err(report)?;
for event_id in affected_event_ids {
- if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {
+ if let Some(outcome) =
+ restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
+ {
retired.push(outcome);
}
}
@@ -1442,7 +1502,9 @@ impl CalendarRepository for PgCalendarRepository {
.await
.map_err(report)?;
for event_id in affected_event_ids {
- if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {
+ if let Some(outcome) =
+ restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
+ {
retired.push(outcome);
}
}
@@ -1459,6 +1521,7 @@ impl CalendarRepository for PgCalendarRepository {
.as_ref()
.map(|range| range.start_date);
let materialized_end_date = sync.materialized_range.as_ref().map(|range| range.end_date);
+ let badged = visible_calendar_sync_error_tx(&mut tx, sync.calendar_id).await?;
let updated = sqlx::query!(
r#"
UPDATE calendars
@@ -1505,8 +1568,14 @@ impl CalendarRepository for PgCalendarRepository {
"provider calendar disappeared before sync state was committed"
));
}
+ if badged.is_some() {
+ log.record(
+ key.email_link_id,
+ CalendarChangeOp::UpsertCalendar(sync.calendar_id),
+ );
+ }
- tx.commit().await.map_err(report)?;
+ log.commit(tx).await?;
Ok(retired)
}
@@ -1521,7 +1590,8 @@ impl CalendarRepository for PgCalendarRepository {
) -> Result<(), Report> {
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;
- sqlx::query!(
+ let badged_before = visible_calendar_sync_error_tx(&mut tx, calendar_id).await?;
+ let failures = sqlx::query_scalar!(
r#"
UPDATE calendars
SET last_sync_error = $3,
@@ -1531,15 +1601,27 @@ impl CalendarRepository for PgCalendarRepository {
WHERE id = $1
AND account_id = $2
AND NOT is_deleted
+ RETURNING consecutive_sync_failures
"#,
calendar_id,
account_id,
message,
)
- .execute(&mut *tx)
+ .fetch_optional(&mut *tx)
.await
.map_err(report)?;
- tx.commit().await.map_err(report)
+ // Clients see a calendar's error only once it crosses the badge
+ // threshold, so only a newly shown or reworded badge is a change.
+ let mut log = ChangeLogBatch::default();
+ let badged_after =
+ failures.is_some_and(|failures| failures >= CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD);
+ if badged_after && badged_before.as_deref() != Some(message) {
+ log.record(
+ key.email_link_id,
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ );
+ }
+ log.commit(tx).await
}
#[tracing::instrument(skip(self, channel), fields(job_id = %key.job_id), err)]
@@ -1682,6 +1764,7 @@ impl CalendarRepository for PgCalendarRepository {
calendar_ids: Vec<Uuid>,
) -> Result<Vec<RetiredCalendarEvent>, Report> {
let mut retired = Vec::new();
+ let mut log = ChangeLogBatch::default();
let mut tx = self.pool.begin().await.map_err(report)?;
fence_google_mutation_tx(&mut tx, key, lease_token, Some(account_id)).await?;
@@ -1707,12 +1790,14 @@ impl CalendarRepository for PgCalendarRepository {
.await
.map_err(report)?;
for event_id in affected_event_ids {
- if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {
+ if let Some(outcome) =
+ restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
+ {
retired.push(outcome);
}
}
- sqlx::query!(
+ let flipped_calendars = sqlx::query!(
r#"
UPDATE calendars
SET is_deleted = NOT (id = ANY($2::uuid[])),
@@ -1739,13 +1824,24 @@ impl CalendarRepository for PgCalendarRepository {
updated_at = now()
WHERE account_id = $1
AND is_deleted IS DISTINCT FROM NOT (id = ANY($2::uuid[]))
+ RETURNING id, is_deleted
"#,
account_id,
&calendar_ids,
)
- .execute(&mut *tx)
+ .fetch_all(&mut *tx)
.await
.map_err(report)?;
+ for calendar in flipped_calendars {
+ log.record(
+ key.email_link_id,
+ if calendar.is_deleted {
+ CalendarChangeOp::DeleteCalendar(calendar.id)
+ } else {
+ CalendarChangeOp::UpsertCalendar(calendar.id)
+ },
+ );
+ }
// A copy's read-only flag mirrors its calendar's access role as of the
// event's last sync, and Google only resends events that changed, so a
@@ -1753,7 +1849,7 @@ impl CalendarRepository for PgCalendarRepository {
// that calendar stale. Refresh the copies from their calendars' live
// roles, including the stored projection a restore would replay, and
// the entities that mirror them.
- sqlx::query!(
+ let refreshed_copies = sqlx::query!(
r#"
UPDATE calendar_event_sources source
SET is_read_only = live.is_read_only,
@@ -1772,12 +1868,19 @@ impl CalendarRepository for PgCalendarRepository {
) live
WHERE source.calendar_id = live.calendar_id
AND source.is_read_only IS DISTINCT FROM live.is_read_only
+ RETURNING source.event_id, source.source_link_id
"#,
account_id,
)
- .execute(&mut *tx)
+ .fetch_all(&mut *tx)
.await
.map_err(report)?;
+ for copy in refreshed_copies {
+ log.record(
+ copy.source_link_id,
+ CalendarChangeOp::UpsertEvent(copy.event_id),
+ );
+ }
let refreshed_events = sqlx::query!(
r#"
UPDATE calendar_events event
@@ -1786,7 +1889,7 @@ impl CalendarRepository for PgCalendarRepository {
WHERE source.id = event.content_source_id
AND source.account_id = $1
AND event.is_read_only IS DISTINCT FROM source.is_read_only
- RETURNING event.id, event.owner_id
+ RETURNING event.id, event.owner_id, event.source_link_id
"#,
account_id,
)
@@ -1817,23 +1920,24 @@ impl CalendarRepository for PgCalendarRepository {
.await
.map_err(report)?;
for event_id in reranked_event_ids {
- if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {
+ if let Some(outcome) =
+ restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
+ {
retired.push(outcome);
}
}
- retired.extend(
- refreshed_events
- .into_iter()
- .map(|row| RetiredCalendarEvent {
- event_id: row.id,
- owner_id: row.owner_id,
- deleted: false,
- }),
- );
+ retired.extend(refreshed_events.into_iter().map(|row| {
+ log.record(row.source_link_id, CalendarChangeOp::UpsertEvent(row.id));
+ RetiredCalendarEvent {
+ event_id: row.id,
+ owner_id: row.owner_id,
+ deleted: false,
+ }
+ }));
let mut announced = HashSet::new();
retired.retain(|outcome| announced.insert(outcome.event_id));
- tx.commit().await.map_err(report)?;
+ log.commit(tx).await?;
Ok(retired)
}
@@ -2055,7 +2159,8 @@ impl CalendarRepository for PgCalendarRepository {
&self,
requester_id: &str,
) -> Result<Vec<VisibleCalendar>, Report> {
- let rows = sqlx::query!(
+ let rows = sqlx::query_as!(
+ VisibleCalendarRow,
r#"
SELECT
calendar.id,
@@ -2096,27 +2201,7 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_all(&self.pool)
.await
.map_err(report)?;
- Ok(rows
- .into_iter()
- .map(|row| VisibleCalendar {
- id: row.id,
- email_link_id: row.email_link_id,
- email_address: row.email_address,
- name: row.name,
- color: row.color,
- is_primary: row.is_primary,
- is_writable: matches!(row.access_role.as_deref(), Some("owner" | "writer")),
- is_subscription: is_system_calendar(&row.provider_calendar_id),
- sync_error: row.last_sync_error.filter(|_| {
- row.consecutive_sync_failures >= CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD
- }),
- default_reminders: serde_json::from_value(row.default_reminders)
- .inspect_err(|e| {
- tracing::error!(error = ?e, calendar_id = %row.id, "malformed calendar default_reminders json");
- })
- .unwrap_or_default(),
- })
- .collect())
+ Ok(rows.into_iter().map(visible_calendar).collect())
}
#[tracing::instrument(skip(self, requester_id), err)]
@@ -2142,6 +2227,7 @@ impl CalendarRepository for PgCalendarRepository {
provider_event_id: &str,
) -> Result<Vec<RetiredCalendarEvent>, Report> {
let mut tx = self.pool.begin().await.map_err(report)?;
+ let mut log = ChangeLogBatch::default();
let cancelled = [provider_event_id.to_string()];
// A deleted recurring master retires its expanded instances via
// provider_recurring_event_id, matching Google's tombstone shape.
@@ -2170,11 +2256,13 @@ impl CalendarRepository for PgCalendarRepository {
.map_err(report)?;
let mut retired = Vec::new();
for event_id in affected_event_ids {
- if let Some(outcome) = restore_best_source_or_delete(&mut tx, event_id).await? {
+ if let Some(outcome) =
+ restore_best_source_or_delete(&mut tx, &mut log, event_id).await?
+ {
retired.push(outcome);
}
}
- tx.commit().await.map_err(report)?;
+ log.commit(tx).await?;
Ok(retired)
}
}
@@ -2345,6 +2433,8 @@ async fn fence_google_mutation_tx(
async fn upsert_calendar_tx(
tx: &mut Transaction<'_, Postgres>,
+ log: &mut ChangeLogBatch,
+ email_link_id: Uuid,
account_id: Uuid,
calendar: ProviderCalendar,
) -> Result<StoredCalendarRow, Report> {
@@ -2353,7 +2443,8 @@ async fn upsert_calendar_tx(
// the firing schedule of every event that follows this calendar.
let previous = sqlx::query!(
r#"
- SELECT time_zone, default_reminders
+ SELECT
+ time_zone, default_reminders, name, color, access_role, is_primary, is_deleted
FROM calendars
WHERE account_id = $1 AND provider_calendar_id = $2
"#,
@@ -2364,9 +2455,17 @@ async fn upsert_calendar_tx(
.await
.map_err(report)?;
let default_reminders = serde_json::to_value(&calendar.default_reminders).map_err(report)?;
- let reminders_invalidated = previous.is_some_and(|previous| {
+ let reminders_invalidated = previous.as_ref().is_some_and(|previous| {
previous.default_reminders != default_reminders || previous.time_zone != calendar.time_zone
});
+ let visibly_changed = previous.as_ref().is_none_or(|previous| {
+ previous.is_deleted
+ || previous.name != calendar.name
+ || previous.color != calendar.color
+ || previous.access_role != calendar.access_role
+ || previous.is_primary != calendar.is_primary
+ || previous.default_reminders != default_reminders
+ });
let anchor_zone = calendar.time_zone.clone();
let row = sqlx::query_as!(
StoredCalendarRow,
@@ -2430,9 +2529,36 @@ async fn upsert_calendar_tx(
rebuild_calendar_reminder_firings(tx, row.id, anchor_zone.as_deref(), &default_reminders)
.await?;
}
+ if visibly_changed {
+ log.record(email_link_id, CalendarChangeOp::UpsertCalendar(row.id));
+ }
Ok(row)
}
+/// The sync error a calendar shows clients, if any, locking its row for the
+/// rest of the transaction.
+async fn visible_calendar_sync_error_tx(
+ tx: &mut Transaction<'_, Postgres>,
+ calendar_id: Uuid,
+) -> Result<Option<String>, Report> {
+ let row = sqlx::query!(
+ r#"
+ SELECT last_sync_error, consecutive_sync_failures
+ FROM calendars
+ WHERE id = $1
+ FOR UPDATE
+ "#,
+ calendar_id,
+ )
+ .fetch_optional(&mut **tx)
+ .await
+ .map_err(report)?;
+ Ok(row.and_then(|row| {
+ row.last_sync_error
+ .filter(|_| row.consecutive_sync_failures >= CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD)
+ }))
+}
+
fn stored_google_calendar(row: StoredCalendarRow) -> Result<StoredGoogleCalendar, Report> {
let materialized_range = match (
row.materialized_starts_at,
@@ -2781,6 +2907,7 @@ async fn clear_calendar_opt_out_tx(
/// purge-by-owner operation, not this path.
async fn disable_google_calendar_capability_tx(
tx: &mut Transaction<'_, Postgres>,
+ log: &mut ChangeLogBatch,
email_link_id: Uuid,
) -> Result<(), Report> {
let account_id = sqlx::query_scalar!(
@@ -2801,7 +2928,7 @@ async fn disable_google_calendar_capability_tx(
return Ok(());
};
- sqlx::query!(
+ let removed_calendars = sqlx::query_scalar!(
r#"
UPDATE calendars
SET is_deleted = true,
@@ -2812,12 +2939,16 @@ async fn disable_google_calendar_capability_tx(
materialized_end_date = NULL,
updated_at = now()
WHERE account_id = $1
+ RETURNING id
"#,
account_id,
)
- .execute(&mut **tx)
+ .fetch_all(&mut **tx)
.await
.map_err(report)?;
+ for calendar_id in removed_calendars {
+ log.record(email_link_id, CalendarChangeOp::DeleteCalendar(calendar_id));
+ }
let affected_event_ids = sqlx::query_scalar!(
r#"
@@ -2836,7 +2967,7 @@ async fn disable_google_calendar_capability_tx(
.await
.map_err(report)?;
for event_id in &affected_event_ids {
- restore_best_source_or_delete(tx, *event_id).await?;
+ restore_best_source_or_delete(tx, log, *event_id).await?;
}
// Reminder delivery claims are deliberately not foreign-keyed to
@@ -3338,6 +3469,7 @@ async fn apply_canonical_projection(
/// `None` means the row was already gone before this call.
async fn restore_best_source_or_delete(
tx: &mut Transaction<'_, Postgres>,
+ log: &mut ChangeLogBatch,
event_id: Uuid,
) -> Result<Option<RetiredCalendarEvent>, Report> {
let identity = sqlx::query!(
@@ -3384,6 +3516,10 @@ async fn restore_best_source_or_delete(
.execute(&mut **tx)
.await
.map_err(report)?;
+ log.record(
+ identity.source_link_id,
+ CalendarChangeOp::DeleteEvent(event_id),
+ );
return Ok(Some(RetiredCalendarEvent {
event_id,
owner_id: identity.owner_id,
@@ -3402,6 +3538,10 @@ async fn restore_best_source_or_delete(
source.calendar_id,
)
.await?;
+ log.record(
+ identity.source_link_id,
+ CalendarChangeOp::UpsertEvent(event_id),
+ );
Ok(Some(RetiredCalendarEvent {
event_id,
owner_id: identity.owner_id,
@@ -4079,33 +4219,38 @@ fn mention_preview_from_row(row: MentionPreviewRow) -> Result<CalendarMentionPre
}
fn event_from_join(
- row: OccurrenceJoinRow,
+ mut row: OccurrenceJoinRow,
attendees: Vec<CalendarAttendee>,
sources: Vec<CalendarEventSourceContent>,
) -> Result<CalendarEvent, Report> {
- let (mut event, exception) = series_event_from_join(row, attendees, sources)?;
+ let exception = take_exception(&mut row);
+ let mut event = series_event_from_join(row, attendees, sources)?;
// An exception's content replaces the series content for that occurrence
// alone, the same way its attendee list shadows the series list.
event.apply_occurrence_content(exception.content());
Ok(event)
}
-/// Split a joined occurrence row into its series event and the occurrence's
-/// exception content. The exception's attendee list is left to the caller,
-/// which loads attendees in bulk.
-fn series_event_from_join(
- mut row: OccurrenceJoinRow,
- attendees: Vec<CalendarAttendee>,
- sources: Vec<CalendarEventSourceContent>,
-) -> Result<(CalendarEvent, OccurrenceException), Report> {
- let exception = OccurrenceException {
+/// Move a joined occurrence row's exception content out of the row. The
+/// exception's attendee list is left to the caller, which loads attendees in
+/// bulk.
+fn take_exception(row: &mut OccurrenceJoinRow) -> OccurrenceException {
+ OccurrenceException {
title: row.override_title.take(),
description: row.override_description.take(),
location: row.override_location.take(),
status: row.override_status.take().as_deref().map(event_status),
attendees: None,
- };
- let event = CalendarEvent {
+ }
+}
+
+/// The series event of a joined occurrence row, ignoring its exception.
+fn series_event_from_join(
+ row: OccurrenceJoinRow,
+ attendees: Vec<CalendarAttendee>,
+ sources: Vec<CalendarEventSourceContent>,
+) -> Result<CalendarEvent, Report> {
+ Ok(CalendarEvent {
id: row.event_id,
owner_id: row.owner_id,
ical_uid: row.ical_uid,
@@ -4145,8 +4290,7 @@ fn series_event_from_join(
attendees,
created_at: row.created_at,
updated_at: row.updated_at,
- };
- Ok((event, exception))
+ })
}
fn occurrence_from_join(row: &OccurrenceJoinRow) -> Result<CalendarOccurrence, Report> {
diff --git a/crates/calendar_events/src/outbound/pg/change_log.rs b/crates/calendar_events/src/outbound/pg/change_log.rs
new file mode 100644
index 0000000000..acbad29781
--- /dev/null
+++ b/crates/calendar_events/src/outbound/pg/change_log.rs
@@ -0,0 +1,187 @@
+//! Appending to the per-link change log inside calendar write transactions.
+
+use std::collections::{BTreeMap, HashMap};
+
+use chrono::{DateTime, Utc};
+use rootcause::Report;
+use sqlx::{Postgres, Transaction};
+use uuid::Uuid;
+
+use super::{PgCalendarRepository, report};
+use crate::domain::changes::{CalendarChangeLogPruner, CalendarChangeOp};
+
+#[cfg(test)]
+mod test;
+
+/// The changes one transaction made, appended to each link's log as one
+/// contiguous block immediately before the transaction commits.
+#[derive(Debug, Default)]
+pub(super) struct ChangeLogBatch {
+ links: BTreeMap<Uuid, Vec<CalendarChangeOp>>,
+}
+
+impl ChangeLogBatch {
+ pub(super) fn record(&mut self, link_id: Uuid, op: CalendarChangeOp) {
+ self.links.entry(link_id).or_default().push(op);
+ }
+
+ /// Append the batch and commit. Counter rows are the last locks the
+ /// transaction takes, in link order, so writers that touch several links
+ /// cannot deadlock on them, and no other lock is requested while one is
+ /// held.
+ pub(super) async fn commit(self, mut tx: Transaction<'_, Postgres>) -> Result<(), Report> {
+ for (link_id, ops) in self.links {
+ append(&mut tx, link_id, latest_per_target(ops)).await?;
+ }
+ tx.commit().await.map_err(report)
+ }
+}
+
+/// Keep each target's last change, ordered by when that change was recorded.
+fn latest_per_target(ops: Vec<CalendarChangeOp>) -> Vec<CalendarChangeOp> {
+ let last_index: HashMap<(bool, Uuid), usize> = ops
+ .iter()
+ .enumerate()
+ .map(|(index, op)| (target_key(*op), index))
+ .collect();
+ ops.into_iter()
+ .enumerate()
+ .filter(|(index, op)| last_index[&target_key(*op)] == *index)
+ .map(|(_, op)| op)
+ .collect()
+}
+
+fn target_key(op: CalendarChangeOp) -> (bool, Uuid) {
+ match op {
+ CalendarChangeOp::UpsertEvent(id) | CalendarChangeOp::DeleteEvent(id) => (true, id),
+ CalendarChangeOp::UpsertCalendar(id) | CalendarChangeOp::DeleteCalendar(id) => (false, id),
+ }
+}
+
+pub(super) fn kind_code(op: CalendarChangeOp) -> i16 {
+ match op {
+ CalendarChangeOp::UpsertEvent(_) => 1,
+ CalendarChangeOp::DeleteEvent(_) => 2,
+ CalendarChangeOp::UpsertCalendar(_) => 3,
+ CalendarChangeOp::DeleteCalendar(_) => 4,
+ }
+}
+
+pub(super) fn op_from_row(
+ kind: i16,
+ event_id: Option<Uuid>,
+ calendar_id: Option<Uuid>,
+) -> Result<CalendarChangeOp, Report> {
+ match (kind, event_id, calendar_id) {
+ (1, Some(id), None) => Ok(CalendarChangeOp::UpsertEvent(id)),
+ (2, Some(id), None) => Ok(CalendarChangeOp::DeleteEvent(id)),
+ (3, None, Some(id)) => Ok(CalendarChangeOp::UpsertCalendar(id)),
+ (4, None, Some(id)) => Ok(CalendarChangeOp::DeleteCalendar(id)),
+ _ => Err(rootcause::report!(
+ "calendar change log row has an unknown kind or target"
+ )),
+ }
+}
+
+async fn append(
+ tx: &mut Transaction<'_, Postgres>,
+ link_id: Uuid,
+ ops: Vec<CalendarChangeOp>,
+) -> Result<(), Report> {
+ let count = i64::try_from(ops.len())
+ .map_err(|_| rootcause::report!("calendar change batch is too large"))?;
+ if count == 0 {
+ return Ok(());
+ }
+ let last = sqlx::query_scalar!(
+ r#"
+ INSERT INTO calendar_change_counters (link_id, seq)
+ VALUES ($1, $2)
+ ON CONFLICT (link_id) DO UPDATE
+ SET seq = calendar_change_counters.seq + EXCLUDED.seq,
+ updated_at = now()
+ RETURNING seq
+ "#,
+ link_id,
+ count,
+ )
+ .fetch_one(&mut **tx)
+ .await
+ .map_err(report)?;
+ let seqs: Vec<i64> = (last - count + 1..=last).collect();
+ let kinds: Vec<i16> = ops.iter().map(|op| kind_code(*op)).collect();
+ let event_ids: Vec<Option<Uuid>> = ops
+ .iter()
+ .map(|op| match op {
+ CalendarChangeOp::UpsertEvent(id) | CalendarChangeOp::DeleteEvent(id) => Some(*id),
+ _ => None,
+ })
+ .collect();
+ let calendar_ids: Vec<Option<Uuid>> = ops
+ .iter()
+ .map(|op| match op {
+ CalendarChangeOp::UpsertCalendar(id) | CalendarChangeOp::DeleteCalendar(id) => {
+ Some(*id)
+ }
+ _ => None,
+ })
+ .collect();
+ sqlx::query!(
+ r#"
+ INSERT INTO calendar_change_log (link_id, seq, kind, event_id, calendar_id)
+ SELECT $1, change.seq, change.kind, change.event_id, change.calendar_id
+ FROM unnest($2::bigint[], $3::smallint[], $4::uuid[], $5::uuid[])
+ AS change(seq, kind, event_id, calendar_id)
+ "#,
+ link_id,
+ &seqs,
+ &kinds,
+ &event_ids as &[Option<Uuid>],
+ &calendar_ids as &[Option<Uuid>],
+ )
+ .execute(&mut **tx)
+ .await
+ .map_err(report)?;
+ Ok(())
+}
+
+impl CalendarChangeLogPruner for PgCalendarRepository {
+ #[tracing::instrument(skip(self), err)]
+ async fn prune_change_log(&self, cutoff: DateTime<Utc>, batch: usize) -> Result<usize, Report> {
+ let limit = i64::try_from(batch)
+ .map_err(|_| rootcause::report!("calendar change prune batch is too large"))?;
+ let removed = sqlx::query!(
+ r#"
+ WITH doomed AS (
+ SELECT link_id, seq
+ FROM calendar_change_log
+ WHERE created_at < $1
+ ORDER BY created_at, link_id, seq
+ LIMIT $2
+ )
+ DELETE FROM calendar_change_log log
+ USING doomed
+ WHERE log.link_id = doomed.link_id
+ AND log.seq = doomed.seq
+ "#,
+ cutoff,
+ limit,
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(report)?
+ .rows_affected();
+ sqlx::query!(
+ r#"
+ DELETE FROM calendar_change_counters counter
+ WHERE NOT EXISTS (
+ SELECT 1 FROM email_links link WHERE link.id = counter.link_id
+ )
+ "#,
+ )
+ .execute(&self.pool)
+ .await
+ .map_err(report)?;
+ usize::try_from(removed).map_err(|_| rootcause::report!("pruned row count overflows"))
+ }
+}
diff --git a/crates/calendar_events/src/outbound/pg/change_log/test.rs b/crates/calendar_events/src/outbound/pg/change_log/test.rs
new file mode 100644
index 0000000000..770b381a02
--- /dev/null
+++ b/crates/calendar_events/src/outbound/pg/change_log/test.rs
@@ -0,0 +1,62 @@
+use uuid::Uuid;
+
+use super::*;
+
+#[test]
+fn a_batch_keeps_each_targets_last_change_in_recording_order() {
+ let event = Uuid::from_u128(1);
+ let calendar = Uuid::from_u128(2);
+ let other = Uuid::from_u128(3);
+
+ let ops = latest_per_target(vec![
+ CalendarChangeOp::UpsertEvent(event),
+ CalendarChangeOp::UpsertCalendar(calendar),
+ CalendarChangeOp::UpsertEvent(other),
+ CalendarChangeOp::DeleteEvent(event),
+ ]);
+
+ assert_eq!(
+ ops,
+ vec![
+ CalendarChangeOp::UpsertCalendar(calendar),
+ CalendarChangeOp::UpsertEvent(other),
+ CalendarChangeOp::DeleteEvent(event),
+ ]
+ );
+}
+
+#[test]
+fn an_event_and_a_calendar_sharing_an_id_stay_distinct() {
+ let id = Uuid::from_u128(7);
+
+ let ops = latest_per_target(vec![
+ CalendarChangeOp::UpsertEvent(id),
+ CalendarChangeOp::DeleteCalendar(id),
+ ]);
+
+ assert_eq!(ops.len(), 2);
+}
+
+#[test]
+fn kinds_round_trip_through_their_storage_codes() {
+ let id = Uuid::from_u128(9);
+ for op in [
+ CalendarChangeOp::UpsertEvent(id),
+ CalendarChangeOp::DeleteEvent(id),
+ CalendarChangeOp::UpsertCalendar(id),
+ CalendarChangeOp::DeleteCalendar(id),
+ ] {
+ let (event_id, calendar_id) = match op {
+ CalendarChangeOp::UpsertEvent(id) | CalendarChangeOp::DeleteEvent(id) => {
+ (Some(id), None)
+ }
+ _ => (None, Some(id)),
+ };
+ assert_eq!(
+ op_from_row(kind_code(op), event_id, calendar_id).unwrap(),
+ op
+ );
+ }
+ assert!(op_from_row(1, None, Some(id)).is_err());
+ assert!(op_from_row(5, Some(id), None).is_err());
+}
diff --git a/crates/calendar_events/src/outbound/pg/changes.rs b/crates/calendar_events/src/outbound/pg/changes.rs
new file mode 100644
index 0000000000..3a07881e9f
--- /dev/null
+++ b/crates/calendar_events/src/outbound/pg/changes.rs
@@ -0,0 +1,348 @@
+//! Reading the per-link change log and hydrating its targets.
+
+use std::collections::HashMap;
+
+use futures::try_join;
+use rootcause::Report;
+use uuid::Uuid;
+
+use super::{
+ OccurrenceJoinRow, PgCalendarRepository, VisibleCalendarRow, change_log::op_from_row,
+ fetch_attendees, fetch_override_attendees, fetch_source_contents, occurrence_from_join, report,
+ series_event_from_join, take_exception, visible_calendar,
+};
+use crate::domain::{
+ changes::{
+ CalendarChange, CalendarChangeRepository, CalendarEventChange, CalendarLinkLog,
+ CalendarLinkWatermark, CalendarWatermark, EventChangeLoad, EventOccurrence,
+ },
+ models::VisibleCalendar,
+ ports::CalendarRepository,
+};
+
+impl CalendarChangeRepository for PgCalendarRepository {
+ #[tracing::instrument(skip(self, viewer), err)]
+ async fn current_watermark(&self, viewer: &str) -> Result<CalendarWatermark, Report> {
+ let rows = sqlx::query!(
+ r#"
+ WITH visible AS (
+ SELECT id AS link_id FROM email_links WHERE macro_id = $1
+ UNION
+ SELECT link_id FROM macro_user_links WHERE primary_macro_id = $1
+ )
+ SELECT visible.link_id AS "link_id!", COALESCE(counter.seq, 0) AS "seq!"
+ FROM visible
+ LEFT JOIN calendar_change_counters counter ON counter.link_id = visible.link_id
+ "#,
+ viewer,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(report)?;
+ Ok(CalendarWatermark::from_links(rows.into_iter().map(|row| {
+ CalendarLinkWatermark {
+ link_id: row.link_id,
+ seq: row.seq,
+ }
+ })))
+ }
+
+ #[tracing::instrument(skip(self, viewer, since), err)]
+ async fn link_logs(
+ &self,
+ viewer: &str,
+ since: &CalendarWatermark,
+ limit: usize,
+ ) -> Result<Vec<CalendarLinkLog>, Report> {
+ let (since_links, since_seqs): (Vec<Uuid>, Vec<i64>) =
+ since.links().map(|link| (link.link_id, link.seq)).unzip();
+ let limit = i64::try_from(limit)
+ .map_err(|_| rootcause::report!("calendar change page limit is too large"))?;
+ // One statement, so each link's counter and rows come from the same
+ // snapshot and the counter never trails the rows read.
+ let rows = sqlx::query!(
+ r#"
+ WITH visible AS (
+ SELECT id AS link_id FROM email_links WHERE macro_id = $1
+ UNION
+ SELECT link_id FROM macro_user_links WHERE primary_macro_id = $1
+ ),
+ since AS (
+ SELECT position.link_id, position.seq
+ FROM unnest($2::uuid[], $3::bigint[]) AS position(link_id, seq)
+ )
+ SELECT
+ visible.link_id AS "link_id!",
+ COALESCE(counter.seq, 0) AS "counter!",
+ COALESCE(
+ (
+ SELECT min(retained.seq) - 1
+ FROM calendar_change_log retained
+ WHERE retained.link_id = visible.link_id
+ ),
+ counter.seq,
+ 0
+ ) AS "floor!",
+ change.seq AS "seq?",
+ change.kind AS "kind?",
+ change.event_id AS "event_id?",
+ change.calendar_id AS "calendar_id?"
+ FROM visible
+ LEFT JOIN calendar_change_counters counter ON counter.link_id = visible.link_id
+ LEFT JOIN since ON since.link_id = visible.link_id
+ LEFT JOIN LATERAL (
+ SELECT log.seq, log.kind, log.event_id, log.calendar_id
+ FROM calendar_change_log log
+ WHERE log.link_id = visible.link_id
+ AND since.seq IS NOT NULL
+ AND log.seq > since.seq
+ ORDER BY log.seq
+ LIMIT $4
+ ) change ON true
+ ORDER BY visible.link_id, change.seq
+ "#,
+ viewer,
+ &since_links,
+ &since_seqs,
+ limit,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(report)?;
+
+ let mut logs: Vec<CalendarLinkLog> = Vec::new();
+ for row in rows {
+ if logs.last().is_none_or(|log| log.link_id != row.link_id) {
+ logs.push(CalendarLinkLog {
+ link_id: row.link_id,
+ counter: row.counter,
+ floor: row.floor,
+ changes: Vec::new(),
+ });
+ }
+ if let (Some(seq), Some(kind)) = (row.seq, row.kind) {
+ let op = op_from_row(kind, row.event_id, row.calendar_id)?;
+ if let Some(log) = logs.last_mut() {
+ log.changes.push(CalendarChange { seq, op });
+ }
+ }
+ }
+ Ok(logs)
+ }
+
+ #[tracing::instrument(skip(self, viewer, event_ids), err)]
+ async fn load_event_changes(
+ &self,
+ viewer: &str,
+ event_ids: &[Uuid],
+ max_occurrences: usize,
+ ) -> Result<EventChangeLoad, Report> {
+ if event_ids.is_empty() {
+ return Ok(EventChangeLoad::default());
+ }
+ let max_occurrences = i64::try_from(max_occurrences)
+ .map_err(|_| rootcause::report!("calendar occurrence cap is too large"))?;
+ // The running total over the requested order only grows, so the
+ // admitted ids are always a prefix of the request.
+ let admitted = sqlx::query_scalar!(
+ r#"
+ SELECT totals.event_id AS "event_id!"
+ FROM (
+ SELECT
+ requested.event_id,
+ requested.ord,
+ (sum(count(occurrence.event_id)) OVER (ORDER BY requested.ord))::bigint AS running
+ FROM unnest($1::uuid[]) WITH ORDINALITY AS requested(event_id, ord)
+ LEFT JOIN calendar_event_occurrences occurrence
+ ON occurrence.event_id = requested.event_id
+ AND NOT occurrence.is_cancelled
+ GROUP BY requested.event_id, requested.ord
+ ) totals
+ WHERE totals.ord = 1 OR totals.running <= $2::bigint
+ ORDER BY totals.ord
+ "#,
+ event_ids,
+ max_occurrences,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(report)?;
+
+ let rows = sqlx::query_as!(
+ OccurrenceJoinRow,
+ r#"
+ SELECT
+ occurrence.event_id,
+ event.source_link_id,
+ occurrence.occurrence_key,
+ occurrence.recurrence_id,
+ occurrence.starts_at AS occurrence_starts_at,
+ occurrence.ends_at AS occurrence_ends_at,
+ occurrence.start_date AS occurrence_start_date,
+ occurrence.end_date AS occurrence_end_date,
+ occurrence.is_cancelled,
+ override.title AS override_title,
+ override.description AS override_description,
+ override.location AS override_location,
+ override.status AS override_status,
+ event.owner_id,
+ event.ical_uid,
+ event.title,
+ event.description,
+ event.location,
+ event.status,
+ event.visibility,
+ event.transparency,
+ event.event_type,
+ event.starts_at,
+ event.ends_at,
+ event.start_date,
+ event.end_date,
+ event.time_zone,
+ event.recurrence_lines,
+ event.organizer_email,
+ event.organizer_name,
+ event.creator_email,
+ event.creator_name,
+ event.conference_url,
+ event.conference_provider,
+ event.sequence,
+ event.is_read_only,
+ event.reminders_use_default,
+ event.reminder_overrides,
+ event.created_at,
+ event.updated_at
+ FROM calendar_event_occurrences occurrence
+ JOIN calendar_events event ON event.id = occurrence.event_id
+ LEFT JOIN calendar_event_overrides override
+ ON override.event_id = occurrence.event_id
+ AND override.recurrence_id = occurrence.recurrence_id
+ WHERE occurrence.event_id = ANY($2::uuid[])
+ AND occurrence.owner_id IN (
+ SELECT $1::text
+ UNION
+ SELECT link.child_macro_id
+ FROM macro_user_links link
+ WHERE link.primary_macro_id = $1
+ )
+ AND event.status <> 'cancelled'
+ AND NOT occurrence.is_cancelled
+ AND (
+ event.owner_id = $1
+ OR EXISTS (
+ SELECT 1
+ FROM macro_user_links link
+ WHERE link.link_id = event.source_link_id
+ AND link.primary_macro_id = $1
+ )
+ )
+ ORDER BY
+ occurrence.event_id,
+ COALESCE(occurrence.starts_at, occurrence.start_date::timestamp AT TIME ZONE 'UTC'),
+ occurrence.occurrence_key
+ "#,
+ viewer,
+ &admitted,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(report)?;
+ let (attendees, override_attendees, source_contents) = try_join!(
+ fetch_attendees(&self.pool, &admitted),
+ fetch_override_attendees(&self.pool, &admitted),
+ fetch_source_contents(&self.pool, &admitted),
+ )?;
+
+ let mut changes: HashMap<Uuid, CalendarEventChange> = HashMap::new();
+ for mut row in rows {
+ let event_id = row.event_id;
+ let occurrence = occurrence_from_join(&row)?;
+ let mut exception = take_exception(&mut row);
+ exception.attendees = occurrence.recurrence_id.as_ref().and_then(|recurrence_id| {
+ override_attendees
+ .get(&(event_id, recurrence_id.clone()))
+ .cloned()
+ });
+ let instance = EventOccurrence {
+ occurrence,
+ exception,
+ };
+ match changes.get_mut(&event_id) {
+ Some(change) => change.occurrences.push(instance),
+ None => {
+ let link_id = row.source_link_id;
+ let event = series_event_from_join(
+ row,
+ attendees.get(&event_id).cloned().unwrap_or_default(),
+ source_contents.get(&event_id).cloned().unwrap_or_default(),
+ )?;
+ changes.insert(
+ event_id,
+ CalendarEventChange {
+ event,
+ link_id,
+ occurrences: vec![instance],
+ },
+ );
+ }
+ }
+ }
+ Ok(EventChangeLoad {
+ events: admitted
+ .iter()
+ .filter_map(|event_id| changes.remove(event_id))
+ .collect(),
+ considered: admitted.len(),
+ })
+ }
+
+ #[tracing::instrument(skip(self, viewer, calendar_ids), err)]
+ async fn list_calendars_by_ids(
+ &self,
+ viewer: &str,
+ calendar_ids: &[Uuid],
+ ) -> Result<Vec<VisibleCalendar>, Report> {
+ let rows = sqlx::query_as!(
+ VisibleCalendarRow,
+ r#"
+ SELECT
+ calendar.id,
+ link.id AS email_link_id,
+ link.email_address,
+ calendar.name,
+ calendar.color,
+ calendar.is_primary,
+ calendar.access_role,
+ calendar.provider_calendar_id,
+ calendar.default_reminders,
+ calendar.last_sync_error,
+ calendar.consecutive_sync_failures
+ FROM email_links link
+ JOIN calendar_accounts account ON account.email_link_id = link.id
+ JOIN calendars calendar ON calendar.account_id = account.id
+ WHERE calendar.id = ANY($2::uuid[])
+ AND NOT calendar.is_deleted
+ AND account.sync_status <> 'disabled'
+ AND (
+ link.macro_id = $1
+ OR EXISTS (
+ SELECT 1
+ FROM macro_user_links delegation
+ WHERE delegation.link_id = link.id
+ AND delegation.primary_macro_id = $1
+ )
+ )
+ "#,
+ viewer,
+ calendar_ids,
+ )
+ .fetch_all(&self.pool)
+ .await
+ .map_err(report)?;
+ Ok(rows.into_iter().map(visible_calendar).collect())
+ }
+
+ async fn owned_inbox_emails(&self, viewer: &str) -> Result<Vec<String>, Report> {
+ CalendarRepository::owned_inbox_emails(self, viewer).await
+ }
+}
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index 3fb601ea03..c7db88bb24 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -6715,4 +6715,5 @@ async fn retiring_an_unrelated_copy_keeps_a_fresher_schedule_written_through_ano
assert_primary_content(&entity_content(&pool, event_id).await);
}
+mod changes;
mod invitations;
diff --git a/crates/calendar_events/src/outbound/pg/test/changes.rs b/crates/calendar_events/src/outbound/pg/test/changes.rs
new file mode 100644
index 0000000000..4e2d26d3d6
--- /dev/null
+++ b/crates/calendar_events/src/outbound/pg/test/changes.rs
@@ -0,0 +1,812 @@
+use std::collections::{BTreeMap, BTreeSet, HashSet};
+
+use super::*;
+use crate::domain::changes::{
+ CalendarChangeLogPruner, CalendarChangeOp, CalendarChangeRepository, CalendarChangeService,
+ CalendarLinkWatermark, CalendarWatermark, EventOccurrence,
+};
+use crate::outbound::pg::change_log::op_from_row;
+
+async fn logged(pool: &PgPool, link_id: Uuid) -> Vec<(i64, CalendarChangeOp)> {
+ sqlx::query!(
+ r#"
+ SELECT seq, kind, event_id, calendar_id
+ FROM calendar_change_log
+ WHERE link_id = $1
+ ORDER BY seq
+ "#,
+ link_id,
+ )
+ .fetch_all(pool)
+ .await
+ .unwrap()
+ .into_iter()
+ .map(|row| {
+ (
+ row.seq,
+ op_from_row(row.kind, row.event_id, row.calendar_id).unwrap(),
+ )
+ })
+ .collect()
+}
+
+async fn ops_after(pool: &PgPool, link_id: Uuid, after: i64) -> Vec<CalendarChangeOp> {
+ logged(pool, link_id)
+ .await
+ .into_iter()
+ .filter(|(seq, _)| *seq > after)
+ .map(|(_, op)| op)
+ .collect()
+}
+
+async fn counter(pool: &PgPool, link_id: Uuid) -> i64 {
+ sqlx::query_scalar!(
+ "SELECT seq FROM calendar_change_counters WHERE link_id = $1",
+ link_id,
+ )
+ .fetch_optional(pool)
+ .await
+ .unwrap()
+ .unwrap_or(0)
+}
+
+fn primary_calendar(provider_calendar_id: &str, name: &str) -> ProviderCalendar {
+ ProviderCalendar {
+ provider_calendar_id: provider_calendar_id.to_string(),
+ name: name.to_string(),
+ description: None,
+ time_zone: Some("UTC".to_string()),
+ color: None,
+ access_role: Some("owner".to_string()),
+ is_primary: provider_calendar_id == "primary",
+ is_selected: true,
+ default_reminders: Vec::new(),
+ }
+}
+
+/// A claimed Google job for `link_id`, the precondition for every fenced
+/// provider write.
+async fn claimed_job(
+ repo: &PgCalendarRepository,
+ link_id: Uuid,
+) -> (CalendarBackfillJobKey, Uuid, Uuid) {
+ let enabled = repo
+ .apply_google_grant(
+ link_id,
+ complete_grant(),
+ CalendarGrantIntent::CalendarRequested,
+ )
+ .await
+ .unwrap();
+ let job = enabled
+ .jobs
+ .iter()
+ .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
+ .unwrap();
+ let key = CalendarBackfillJobKey {
+ job_id: job.id,
+ email_link_id: link_id,
+ };
+ let CalendarBackfillClaim::Claimed {
+ lease_token,
+ account_id,
+ } = repo.claim_google_backfill(key).await.unwrap()
+ else {
+ panic!("Google job should be claimable");
+ };
+ (key, lease_token, account_id)
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn event_writes_log_one_row_per_change_and_skip_unchanged_echoes(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 upsert = timed_upsert(owner_id, link_id, provider, "[email protected]", "One", 1);
+ let event_id = repo.upsert_event_fixture(upsert.clone()).await.unwrap();
+
+ repo.upsert_event_fixture(upsert.clone()).await.unwrap();
+ let mut edited = upsert;
+ edited.event.title = "Two".to_string();
+ edited.event.sequence = 2;
+ edited.event.updated_at += Duration::minutes(5);
+ repo.upsert_event_fixture(edited).await.unwrap();
+
+ assert_eq!(
+ logged(&pool, link_id).await,
+ vec![
+ (1, CalendarChangeOp::UpsertCalendar(provider.1)),
+ (2, CalendarChangeOp::UpsertEvent(event_id)),
+ (3, CalendarChangeOp::UpsertEvent(event_id)),
+ ],
+ "the identical re-upsert in between logged nothing"
+ );
+ assert_eq!(counter(&pool, link_id).await, 3);
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn calendar_upserts_log_only_visible_changes(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let account_id = repo.upsert_google_account(link_id).await.unwrap();
+ let calendar = primary_calendar("primary", "Primary");
+ let calendar_id = repo
+ .upsert_calendar_fixture(account_id, calendar.clone())
+ .await
+ .unwrap();
+
+ repo.upsert_calendar_fixture(account_id, calendar.clone())
+ .await
+ .unwrap();
+ repo.upsert_calendar_fixture(
+ account_id,
+ ProviderCalendar {
+ name: "Renamed".to_string(),
+ ..calendar
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ ops_after(&pool, link_id, 0).await,
+ vec![
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ ]
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn a_calendar_list_reconcile_logs_its_removals_contiguously(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let (key, lease_token, account_id) = claimed_job(&repo, link_id).await;
+ let kept = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("primary", "Primary"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let dropped = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("team", "Team"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let mut first = timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, dropped),
+ "First",
+ 1,
+ );
+ first.event.calendar_id = Some(dropped);
+ let mut second = timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, dropped),
+ "Second",
+ 1,
+ );
+ second.event.calendar_id = Some(dropped);
+ let first = repo.upsert_event_fixture(first).await.unwrap();
+ let second = repo.upsert_event_fixture(second).await.unwrap();
+ let before = counter(&pool, link_id).await;
+
+ repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![kept])
+ .await
+ .unwrap();
+
+ let rows: Vec<(i64, CalendarChangeOp)> = logged(&pool, link_id)
+ .await
+ .into_iter()
+ .filter(|(seq, _)| *seq > before)
+ .collect();
+ assert_eq!(
+ rows.iter().map(|(seq, _)| *seq).collect::<Vec<_>>(),
+ vec![before + 1, before + 2, before + 3],
+ "one transaction appends one contiguous block"
+ );
+ assert_eq!(
+ rows.into_iter().map(|(_, op)| op).collect::<HashSet<_>>(),
+ HashSet::from([
+ CalendarChangeOp::DeleteEvent(first),
+ CalendarChangeOp::DeleteEvent(second),
+ CalendarChangeOp::DeleteCalendar(dropped),
+ ])
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn snapshot_tombstones_and_source_removal_log_deletions(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let (key, lease_token, account_id) = claimed_job(&repo, link_id).await;
+ let calendar_id = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("primary", "Primary"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let provider = (account_id, calendar_id);
+ let unobserved = repo
+ .upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Unobserved",
+ 1,
+ ))
+ .await
+ .unwrap();
+ let removed = repo
+ .upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ "Removed",
+ 1,
+ ))
+ .await
+ .unwrap();
+ let before = counter(&pool, link_id).await;
+
+ repo.commit_google_calendar_sync(
+ key,
+ lease_token,
+ account_id,
+ GoogleCalendarSyncSnapshot {
+ calendar_id,
+ next_sync_token: "token".to_string(),
+ observed_provider_event_ids: Some(vec!["[email protected]".to_string()]),
+ materialized_range: None,
+ cancelled_provider_event_ids: Vec::new(),
+ },
+ 0,
+ )
+ .await
+ .unwrap();
+ repo.remove_google_source(account_id, calendar_id, "[email protected]")
+ .await
+ .unwrap();
+
+ assert_eq!(
+ ops_after(&pool, link_id, before).await,
+ vec![
+ CalendarChangeOp::DeleteEvent(unobserved),
+ CalendarChangeOp::DeleteEvent(removed),
+ ]
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn a_sync_error_badge_logs_when_it_appears_changes_and_clears(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let (key, lease_token, account_id) = claimed_job(&repo, link_id).await;
+ let calendar_id = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("primary", "Primary"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let before = counter(&pool, link_id).await;
+ let threshold = usize::try_from(CALENDAR_SYNC_FAILURE_BADGE_THRESHOLD).unwrap();
+
+ for _ in 0..threshold + 1 {
+ repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, "boom")
+ .await
+ .unwrap();
+ }
+ assert_eq!(
+ ops_after(&pool, link_id, before).await,
+ vec![CalendarChangeOp::UpsertCalendar(calendar_id)],
+ "only the failure that crosses the threshold shows a badge"
+ );
+ repo.record_google_calendar_sync_error(key, lease_token, account_id, calendar_id, "worse")
+ .await
+ .unwrap();
+ repo.commit_google_calendar_sync(
+ key,
+ lease_token,
+ account_id,
+ GoogleCalendarSyncSnapshot {
+ calendar_id,
+ next_sync_token: "token".to_string(),
+ observed_provider_event_ids: None,
+ materialized_range: None,
+ cancelled_provider_event_ids: Vec::new(),
+ },
+ 0,
+ )
+ .await
+ .unwrap();
+ repo.commit_google_calendar_sync(
+ key,
+ lease_token,
+ account_id,
+ GoogleCalendarSyncSnapshot {
+ calendar_id,
+ next_sync_token: "token-2".to_string(),
+ observed_provider_event_ids: None,
+ materialized_range: None,
+ cancelled_provider_event_ids: Vec::new(),
+ },
+ 0,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ ops_after(&pool, link_id, before).await,
+ vec![
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ CalendarChangeOp::UpsertCalendar(calendar_id),
+ ],
+ "a reworded badge and its clearing log; a quiet commit does not"
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn disconnecting_logs_every_calendar_and_event_removal(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let (_account_id, calendar_id, event_id, _channel_id) =
+ connected_calendar(&pool, &repo, owner_id, link_id).await;
+ let before = counter(&pool, link_id).await;
+
+ repo.disconnect_google_calendar(owner_id, link_id)
+ .await
+ .unwrap()
+ .expect("the owner's inbox is disconnectable");
+
+ assert_eq!(
+ ops_after(&pool, link_id, before)
+ .await
+ .into_iter()
+ .collect::<HashSet<_>>(),
+ HashSet::from([
+ CalendarChangeOp::DeleteCalendar(calendar_id),
+ CalendarChangeOp::DeleteEvent(event_id),
+ ])
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn watermarks_cover_owned_and_delegated_links_only(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let delegate_id = "macro|[email protected]";
+ insert_user(&pool, owner_id).await;
+ insert_user(&pool, delegate_id).await;
+ let shared = insert_link(&pool, owner_id).await;
+ let private = insert_link(&pool, owner_id).await;
+ let own = insert_link(&pool, delegate_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ provider_ids(&repo, shared).await;
+ provider_ids(&repo, private).await;
+ sqlx::query!(
+ r#"
+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
+ VALUES ($1, $2, $3)
+ "#,
+ delegate_id,
+ owner_id,
+ shared,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ let watermark = repo.current_watermark(delegate_id).await.unwrap();
+
+ assert_eq!(
+ watermark.links().collect::<Vec<_>>(),
+ {
+ let mut links = vec![
+ CalendarLinkWatermark {
+ link_id: shared,
+ seq: 1,
+ },
+ CalendarLinkWatermark {
+ link_id: own,
+ seq: 0,
+ },
+ ];
+ links.sort_by_key(|link| link.link_id);
+ links
+ },
+ "the owner's undelegated inbox stays invisible"
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn pruning_raises_the_floor_and_forces_stale_clients_to_reset(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;
+ for n in 0..3 {
+ repo.upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ &format!("prune-{n}@example.com"),
+ "Pruned",
+ 1,
+ ))
+ .await
+ .unwrap();
+ }
+ sqlx::query!(
+ "UPDATE calendar_change_log SET created_at = now() - interval '40 days' WHERE link_id = $1 AND seq <= 2",
+ link_id,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ let removed = repo
+ .prune_change_log(Utc::now() - Duration::days(30), 100)
+ .await
+ .unwrap();
+
+ assert_eq!(removed, 2);
+ let at = |seq| CalendarWatermark::from_links([CalendarLinkWatermark { link_id, seq }]);
+ let logs = repo.link_logs(owner_id, &at(2), 10).await.unwrap();
+ assert_eq!((logs[0].counter, logs[0].floor), (4, 2));
+ let service = CalendarChangeService::new(repo.clone());
+ assert!(
+ service
+ .changes_since(owner_id, at(1))
+ .await
+ .unwrap()
+ .reset_required
+ );
+ let resumed = service.changes_since(owner_id, at(2)).await.unwrap();
+ assert!(!resumed.reset_required);
+ assert_eq!(resumed.events.len(), 2);
+ assert_eq!(resumed.new_watermark, at(4));
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn a_prune_batch_inside_a_timestamp_tie_removes_the_lowest_sequences(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;
+ for n in 0..3 {
+ repo.upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ provider,
+ &format!("prune-tie-{n}@example.com"),
+ "Pruned",
+ 1,
+ ))
+ .await
+ .unwrap();
+ }
+ sqlx::query!(
+ "UPDATE calendar_change_log SET created_at = now() - interval '40 days' WHERE link_id = $1",
+ link_id,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ let removed = repo
+ .prune_change_log(Utc::now() - Duration::days(30), 2)
+ .await
+ .unwrap();
+
+ assert_eq!(removed, 2);
+ assert_eq!(
+ logged(&pool, link_id)
+ .await
+ .into_iter()
+ .map(|(seq, _)| seq)
+ .collect::<Vec<_>>(),
+ vec![3, 4],
+ "the retained log stays a contiguous suffix"
+ );
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn pruning_drops_counters_of_deleted_links(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ provider_ids(&repo, link_id).await;
+ sqlx::query!("DELETE FROM email_links WHERE id = $1", link_id)
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ repo.prune_change_log(Utc::now() - Duration::days(30), 100)
+ .await
+ .unwrap();
+
+ assert_eq!(counter(&pool, link_id).await, 0);
+}
+
+/// What a client holds after folding delta pages: each event's series and
+/// complete occurrence set, and every calendar.
+#[derive(Default)]
+struct ClientModel {
+ events: BTreeMap<Uuid, (CalendarEvent, Vec<EventOccurrence>)>,
+ calendars: BTreeMap<Uuid, VisibleCalendar>,
+}
+
+impl ClientModel {
+ async fn replay(
+ service: &CalendarChangeService<PgCalendarRepository>,
+ viewer: &str,
+ links: &[Uuid],
+ ) -> Self {
+ let mut model = Self::default();
+ let mut since = CalendarWatermark::from_links(
+ links
+ .iter()
+ .map(|&link_id| CalendarLinkWatermark { link_id, seq: 0 }),
+ );
+ loop {
+ let page = service.changes_since(viewer, since).await.unwrap();
+ assert!(!page.reset_required);
+ for change in page.events {
+ model
+ .events
+ .insert(change.event.id, (change.event, change.occurrences));
+ }
+ for id in page.deleted_event_ids {
+ model.events.remove(&id);
+ }
+ for calendar in page.calendars {
+ model.calendars.insert(calendar.id, calendar);
+ }
+ for id in page.deleted_calendar_ids {
+ model.calendars.remove(&id);
+ }
+ since = page.new_watermark;
+ if !page.has_more {
+ return model;
+ }
+ }
+ }
+
+ fn listings(&self, link_of: &BTreeMap<Uuid, Uuid>) -> BTreeSet<String> {
+ self.events
+ .values()
+ .flat_map(|(event, occurrences)| {
+ occurrences.iter().map(move |instance| {
+ listing_key(&OccurrenceListing {
+ event: event.clone(),
+ occurrence: instance.occurrence.clone(),
+ link_id: link_of[&event.id],
+ exception: instance.exception.clone(),
+ })
+ })
+ })
+ .collect()
+ }
+}
+
+fn listing_key(listing: &OccurrenceListing) -> String {
+ let (event, occurrence) = listing.clone().into_occurrence_event();
+ format!(
+ "{} {} {} {} {:?} {:?}",
+ listing.link_id,
+ occurrence.event_id,
+ occurrence.occurrence_key,
+ event.title,
+ event.status,
+ event
+ .attendees
+ .iter()
+ .map(|attendee| (attendee.email.clone(), attendee.response_status))
+ .collect::<Vec<_>>(),
+ )
+}
+
+async fn assert_replay_matches_reads(repo: &PgCalendarRepository, viewer: &str, links: &[Uuid]) {
+ let service = CalendarChangeService::new(repo.clone());
+ let model = ClientModel::replay(&service, viewer, links).await;
+ let starts_at = Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap();
+ let ends_at = Utc.with_ymd_and_hms(2028, 1, 1, 0, 0, 0).unwrap();
+ let listings = repo
+ .list_occurrences(
+ viewer,
+ OccurrenceRange {
+ starts_at,
+ ends_at,
+ start_date: starts_at.date_naive(),
+ end_date: ends_at.date_naive(),
+ },
+ None,
+ 2000,
+ )
+ .await
+ .unwrap();
+ let link_of: BTreeMap<Uuid, Uuid> = model
+ .events
+ .keys()
+ .map(|&id| {
+ let link = listings
+ .iter()
+ .find(|listing| listing.event.id == id)
+ .map(|listing| listing.link_id)
+ .expect("every replayed event is listed");
+ (id, link)
+ })
+ .collect();
+
+ assert_eq!(
+ model.listings(&link_of),
+ listings.iter().map(listing_key).collect::<BTreeSet<_>>()
+ );
+ assert_eq!(model.calendars.into_values().collect::<Vec<_>>(), {
+ let mut calendars = repo.list_visible_calendars(viewer).await.unwrap();
+ calendars.sort_by_key(|calendar| calendar.id);
+ calendars
+ });
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn replaying_the_log_reproduces_what_the_viewer_reads(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let delegate_id = "macro|[email protected]";
+ insert_user(&pool, owner_id).await;
+ insert_user(&pool, delegate_id).await;
+ let link_id = insert_link(&pool, owner_id).await;
+ let other_link = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let (key, lease_token, account_id) = claimed_job(&repo, link_id).await;
+ let primary = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("primary", "Primary"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let team = repo
+ .upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ primary_calendar("team", "Team"),
+ )
+ .await
+ .unwrap()
+ .id;
+ let other_provider = provider_ids(&repo, other_link).await;
+
+ // A recurring series whose second occurrence declines for one instance.
+ let mut series = timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, primary),
+ "Series",
+ 1,
+ );
+ let declined_start = Utc.with_ymd_and_hms(2026, 7, 25, 14, 0, 0).unwrap();
+ series.occurrences[1].recurrence_id = Some(declined_start.to_rfc3339());
+ series.overrides = vec![CalendarEventOverride {
+ sequence: None,
+ source_updated_at: None,
+ recurrence_id: declined_start.to_rfc3339(),
+ original_time: EventStart::Timed(declined_start),
+ time: EventTime::Timed {
+ starts_at: declined_start,
+ ends_at: declined_start + Duration::hours(1),
+ time_zone: Some("UTC".to_string()),
+ },
+ title: Some("Moved".to_string()),
+ description: None,
+ location: None,
+ status: None,
+ attendees: Some(vec![CalendarAttendee {
+ response_status: AttendeeResponseStatus::Declined,
+ ..series.event.attendees[0].clone()
+ }]),
+ }];
+ repo.upsert_event_fixture(series.clone()).await.unwrap();
+ let mut edited = series;
+ edited.event.title = "Series, renamed".to_string();
+ edited.event.sequence = 2;
+ edited.event.updated_at += Duration::minutes(5);
+ repo.upsert_event_fixture(edited).await.unwrap();
+
+ let mut doomed = timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, team),
+ "Team only",
+ 1,
+ );
+ doomed.event.calendar_id = Some(team);
+ repo.upsert_event_fixture(doomed).await.unwrap();
+ repo.upsert_event_fixture(timed_upsert(
+ owner_id,
+ link_id,
+ (account_id, primary),
+ "Removed",
+ 1,
+ ))
+ .await
+ .unwrap();
+ repo.remove_google_source(account_id, primary, "[email protected]")
+ .await
+ .unwrap();
+ repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![primary])
+ .await
+ .unwrap();
+ repo.upsert_google_calendar(
+ key,
+ lease_token,
+ account_id,
+ ProviderCalendar {
+ color: Some("#0b8043".to_string()),
+ ..primary_calendar("primary", "Primary")
+ },
+ )
+ .await
+ .unwrap();
+ repo.upsert_event_fixture(timed_upsert(
+ owner_id,
+ other_link,
+ other_provider,
+ "Other inbox",
+ 1,
+ ))
+ .await
+ .unwrap();
+ sqlx::query!(
+ r#"
+ INSERT INTO macro_user_links (primary_macro_id, child_macro_id, link_id)
+ VALUES ($1, $2, $3)
+ "#,
+ delegate_id,
+ owner_id,
+ link_id,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+
+ assert_replay_matches_reads(&repo, owner_id, &[link_id, other_link]).await;
+ assert_replay_matches_reads(&repo, delegate_id, &[link_id]).await;
+}
diff --git a/crates/macro_db_client/migrations/20261006230258_calendar_change_log.sql b/crates/macro_db_client/migrations/20261006230258_calendar_change_log.sql
new file mode 100644
index 0000000000..c29dd9efda
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261006230258_calendar_change_log.sql
@@ -0,0 +1,32 @@
+-- Per-email-link calendar change log. Every calendar write transaction bumps
+-- its link's counter once and appends one row per changed event or calendar,
+-- so clients holding a per-link watermark can replay exactly what they missed.
+--
+-- Neither table references email_links: the counter row is locked last in
+-- every write transaction, and a foreign key check would make that flush wait
+-- on a link row a concurrent disconnect holds FOR UPDATE. Rows for a deleted
+-- link are invisible (visibility is resolved through email_links at read
+-- time) and the retention job removes them.
+CREATE TABLE calendar_change_counters (
+ link_id uuid PRIMARY KEY,
+ seq bigint NOT NULL CHECK (seq >= 0),
+ updated_at timestamptz NOT NULL DEFAULT now()
+);
+
+CREATE TABLE calendar_change_log (
+ link_id uuid NOT NULL,
+ seq bigint NOT NULL CHECK (seq > 0),
+ -- 1 upsert_event, 2 delete_event, 3 upsert_calendar, 4 delete_calendar
+ kind smallint NOT NULL CHECK (kind BETWEEN 1 AND 4),
+ event_id uuid,
+ calendar_id uuid,
+ -- clock_timestamp, not now(): rows are inserted after the counter lock is
+ -- taken, so creation time follows seq within a link and retention always
+ -- removes a prefix of each link's log.
+ created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
+ PRIMARY KEY (link_id, seq),
+ CHECK ((kind IN (1, 2)) = (event_id IS NOT NULL)),
+ CHECK ((kind IN (3, 4)) = (calendar_id IS NOT NULL))
+);
+
+CREATE INDEX calendar_change_log_created_idx ON calendar_change_log (created_at);
diff --git a/services/calendar_service/src/calendar_outbox.rs b/services/calendar_service/src/calendar_outbox.rs
index bcc8533b48..a4b1996d38 100644
--- a/services/calendar_service/src/calendar_outbox.rs
+++ b/services/calendar_service/src/calendar_outbox.rs
@@ -2,13 +2,16 @@
use crate::backfill_queue::{CalendarBackfillMessage, CalendarBackfillQueueClient};
use calendar_events::domain::{
- ports::GoogleCalendarSyncRepository, service::GoogleCalendarSyncScheduler,
+ changes::{CalendarChangeLogPruner, CalendarChangeLogRetention},
+ ports::GoogleCalendarSyncRepository,
+ service::GoogleCalendarSyncScheduler,
};
use chrono::Utc;
use sqlx::PgPool;
use uuid::Uuid;
const BATCH_SIZE: usize = 50;
+const CHANGE_LOG_RETENTION_INTERVAL: std::time::Duration = std::time::Duration::from_secs(60 * 60);
struct OutboxRow {
id: Uuid,
@@ -17,22 +20,26 @@ struct OutboxRow {
kind: String,
}
-/// Continuously schedule due syncs, reap wedged jobs, and publish calendar
-/// outbox rows to calendar's own backfill queue.
+/// Continuously schedule due syncs, reap wedged jobs, publish calendar outbox
+/// rows to calendar's own backfill queue, and hourly prune the calendar
+/// change log.
///
/// A row lock is held through each SQS publish. A crash after publish but
/// before commit can duplicate a message, so every consumer remains
/// idempotent by calendar job id.
-#[tracing::instrument(skip(db, queue, scheduler))]
-pub async fn run<R>(
+#[tracing::instrument(skip(db, queue, scheduler, retention))]
+pub async fn run<R, P>(
db: PgPool,
queue: CalendarBackfillQueueClient,
scheduler: GoogleCalendarSyncScheduler<R>,
+ retention: CalendarChangeLogRetention<P>,
calendar_sync_enabled: bool,
cancellation_token: tokio_util::sync::CancellationToken,
) where
R: GoogleCalendarSyncRepository,
+ P: CalendarChangeLogPruner,
{
+ let mut next_retention = tokio::time::Instant::now();
loop {
if cancellation_token.is_cancelled() {
return;
@@ -61,6 +68,16 @@ pub async fn run<R>(
tracing::error!(error = ?error, "failed to publish calendar backfill outbox");
})
.ok();
+ if tokio::time::Instant::now() >= next_retention {
+ retention
+ .run_once(Utc::now())
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error = ?error, "failed to prune the calendar change log");
+ })
+ .ok();
+ next_retention = tokio::time::Instant::now() + CHANGE_LOG_RETENTION_INTERVAL;
+ }
}
tokio::select! {
_ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {}
diff --git a/services/calendar_service/src/main.rs b/services/calendar_service/src/main.rs
index 31f41c4c7e..b1a9a3d9b1 100644
--- a/services/calendar_service/src/main.rs
+++ b/services/calendar_service/src/main.rs
@@ -128,6 +128,9 @@ async fn main() -> anyhow::Result<()> {
calendar_events::domain::service::GoogleCalendarSyncScheduler::new(
PgCalendarRepository::new(db.clone()),
),
+ calendar_events::domain::changes::CalendarChangeLogRetention::new(
+ PgCalendarRepository::new(db.clone()),
+ ),
config.calendar_sync_enabled,
worker_cancellation_token.clone(),
));