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): add Google and email calendar ingestion (stack 2/3) (#5203)

teod55c52baff36
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index c28c90d407..225ee51b2a 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -349,6 +349,7 @@
"crates/attachment",
"crates/authentication_service_client",
"crates/bot_id",
+ "crates/calendar_events",
"crates/call",
"crates/channel_sender",
"crates/channels",
@@ -2189,6 +2190,7 @@
"crates/attachment",
"crates/authentication_service_client",
"crates/bot_id",
+ "crates/calendar_events",
"crates/channel_sender",
"crates/connection_gateway_client",
"crates/connection_gateway_models",
diff --git a/.sqlx/query-082330c6e5e21b0489318427748337823c860eb9306a9c0a9eddff8ff42c2214.json b/.sqlx/query-082330c6e5e21b0489318427748337823c860eb9306a9c0a9eddff8ff42c2214.json
new file mode 100644
index 0000000000..c21ce6867f
--- /dev/null
+++ b/.sqlx/query-082330c6e5e21b0489318427748337823c860eb9306a9c0a9eddff8ff42c2214.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_backfill_completion_outbox (id, backfill_job_id)\n VALUES ($1, $2)\n ON CONFLICT (backfill_job_id) DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "082330c6e5e21b0489318427748337823c860eb9306a9c0a9eddff8ff42c2214"
+}
diff --git a/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json b/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json
new file mode 100644
index 0000000000..6403eaf45b
--- /dev/null
+++ b/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n status,\n (cursor->>'emailBackfillJobId')::uuid AS email_job_id\n FROM calendar_backfill_jobs\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'email_ics'\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "email_job_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ null
+ ]
+ },
+ "hash": "0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615"
+}
diff --git a/.sqlx/query-0cde968437c593137d949d21a4a2165e1f7e99f446be0eaa325d61340b39ab8a.json b/.sqlx/query-0cde968437c593137d949d21a4a2165e1f7e99f446be0eaa325d61340b39ab8a.json
new file mode 100644
index 0000000000..6d70ffff71
--- /dev/null
+++ b/.sqlx/query-0cde968437c593137d949d21a4a2165e1f7e99f446be0eaa325d61340b39ab8a.json
@@ -0,0 +1,38 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n outbox.id,\n outbox.backfill_job_id,\n job.link_id AS \"email_link_id!\",\n job.threads_requested_limit IS NULL AS \"priority_pass!\"\n FROM email_backfill_init_outbox outbox\n JOIN email_backfill_jobs job ON job.id = outbox.backfill_job_id\n WHERE outbox.published_at IS NULL\n AND job.status = 'InProgress'\n AND job.initialized_at IS NOT NULL\n ORDER BY outbox.created_at, outbox.id\n FOR UPDATE OF outbox SKIP LOCKED\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "backfill_job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "email_link_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "priority_pass!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ null
+ ]
+ },
+ "hash": "0cde968437c593137d949d21a4a2165e1f7e99f446be0eaa325d61340b39ab8a"
+}
diff --git a/.sqlx/query-0f4165b0c4d3a02a206ef06b15745bf64d991ba22687bb0317dc6aa7e081e4d0.json b/.sqlx/query-0f4165b0c4d3a02a206ef06b15745bf64d991ba22687bb0317dc6aa7e081e4d0.json
new file mode 100644
index 0000000000..ac9068d3ad
--- /dev/null
+++ b/.sqlx/query-0f4165b0c4d3a02a206ef06b15745bf64d991ba22687bb0317dc6aa7e081e4d0.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET init_lease_token = $2,\n init_lease_expires_at = now() + interval '2 minutes',\n updated_at = now()\n WHERE id = $1\n AND status = 'Init'\n AND initialized_at IS NULL\n AND (\n init_lease_expires_at IS NULL\n OR init_lease_expires_at < now()\n )\n RETURNING init_lease_token AS \"init_lease_token!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "init_lease_token!",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "0f4165b0c4d3a02a206ef06b15745bf64d991ba22687bb0317dc6aa7e081e4d0"
+}
diff --git a/.sqlx/query-15fc1c11be83237e786fd59c68afd545f75f4f6a7f1091cd7d3d515498787e04.json b/.sqlx/query-15fc1c11be83237e786fd59c68afd545f75f4f6a7f1091cd7d3d515498787e04.json
new file mode 100644
index 0000000000..89d2ae7115
--- /dev/null
+++ b/.sqlx/query-15fc1c11be83237e786fd59c68afd545f75f4f6a7f1091cd7d3d515498787e04.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET published_at = now()\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "15fc1c11be83237e786fd59c68afd545f75f4f6a7f1091cd7d3d515498787e04"
+}
diff --git a/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.json b/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.json
new file mode 100644
index 0000000000..7f9db7ed85
--- /dev/null
+++ b/.sqlx/query-417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT google_grant_version = 0 AS \"unrecorded!\" FROM email_links WHERE id = $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "unrecorded!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "417ce1abfdf12bd1ef05c7c5b67158fa676d4a08db1f56c4c8b1923771e723ef"
+}
diff --git a/.sqlx/query-4430bc3ae386da8a252f0beecd7a412c4ea55c6020f7eadce3c1783f46e38fd2.json b/.sqlx/query-4430bc3ae386da8a252f0beecd7a412c4ea55c6020f7eadce3c1783f46e38fd2.json
new file mode 100644
index 0000000000..b48b887689
--- /dev/null
+++ b/.sqlx/query-4430bc3ae386da8a252f0beecd7a412c4ea55c6020f7eadce3c1783f46e38fd2.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET published_at = NULL\n WHERE backfill_job_id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4430bc3ae386da8a252f0beecd7a412c4ea55c6020f7eadce3c1783f46e38fd2"
+}
diff --git a/.sqlx/query-4817cb0a7e4ce1099fbd3da53d70a243759fb318f45ce25d712b084931770d1e.json b/.sqlx/query-4817cb0a7e4ce1099fbd3da53d70a243759fb318f45ce25d712b084931770d1e.json
new file mode 100644
index 0000000000..b14f7b1453
--- /dev/null
+++ b/.sqlx/query-4817cb0a7e4ce1099fbd3da53d70a243759fb318f45ce25d712b084931770d1e.json
@@ -0,0 +1,38 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n outbox.id,\n outbox.backfill_job_id,\n job.email_link_id,\n job.kind\n FROM calendar_sync_outbox outbox\n JOIN calendar_backfill_jobs job ON job.id = outbox.backfill_job_id\n WHERE outbox.published_at IS NULL\n ORDER BY outbox.created_at, outbox.id\n FOR UPDATE OF outbox SKIP LOCKED\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "backfill_job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "email_link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 3,
+ "name": "kind",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "4817cb0a7e4ce1099fbd3da53d70a243759fb318f45ce25d712b084931770d1e"
+}
diff --git a/.sqlx/query-48d287ff526cf1f6f2dd21d4ac9c2db424b664240192693f5011f04d0fb9977a.json b/.sqlx/query-48d287ff526cf1f6f2dd21d4ac9c2db424b664240192693f5011f04d0fb9977a.json
new file mode 100644
index 0000000000..704289f3cc
--- /dev/null
+++ b/.sqlx/query-48d287ff526cf1f6f2dd21d4ac9c2db424b664240192693f5011f04d0fb9977a.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT status::text AS \"status!\"\n FROM email_backfill_jobs\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "48d287ff526cf1f6f2dd21d4ac9c2db424b664240192693f5011f04d0fb9977a"
+}
diff --git a/.sqlx/query-48dfd3f8e54b94d722d6fe1ffb31d5aa87b44fe2c3cfdd3eac9f513c5f61e29e.json b/.sqlx/query-48dfd3f8e54b94d722d6fe1ffb31d5aa87b44fe2c3cfdd3eac9f513c5f61e29e.json
new file mode 100644
index 0000000000..7c9902d057
--- /dev/null
+++ b/.sqlx/query-48dfd3f8e54b94d722d6fe1ffb31d5aa87b44fe2c3cfdd3eac9f513c5f61e29e.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT account.email_link_id\n FROM calendars calendar\n JOIN calendar_accounts account ON account.id = calendar.account_id\n WHERE calendar.watch_channel_id = $1\n AND calendar.watch_resource_id = $2\n AND NOT calendar.is_deleted\n AND account.sync_status <> 'disabled'\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email_link_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "48dfd3f8e54b94d722d6fe1ffb31d5aa87b44fe2c3cfdd3eac9f513c5f61e29e"
+}
diff --git a/.sqlx/query-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json b/.sqlx/query-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json
new file mode 100644
index 0000000000..15bee76a6b
--- /dev/null
+++ b/.sqlx/query-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'complete',\n completed_at = now(),\n updated_at = now()\n WHERE kind = 'email_ics'\n AND status = 'running'\n AND cursor->>'emailBackfillJobId' = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7"
+}
diff --git a/.sqlx/query-4eaf035a8e3ed828d8108165365a4d1d5534711bf48e8e9f246e61f9b41ca8e5.json b/.sqlx/query-4eaf035a8e3ed828d8108165365a4d1d5534711bf48e8e9f246e61f9b41ca8e5.json
new file mode 100644
index 0000000000..f37ca24d8c
--- /dev/null
+++ b/.sqlx/query-4eaf035a8e3ed828d8108165365a4d1d5534711bf48e8e9f246e61f9b41ca8e5.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET effects_lease_expires_at = now() + interval '2 minutes'\n WHERE backfill_job_id = $1\n AND completed_at IS NULL\n AND effects_lease_token = $2\n AND effects_lease_expires_at > now()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4eaf035a8e3ed828d8108165365a4d1d5534711bf48e8e9f246e61f9b41ca8e5"
+}
diff --git a/.sqlx/query-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json b/.sqlx/query-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json
new file mode 100644
index 0000000000..de1abd019a
--- /dev/null
+++ b/.sqlx/query-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id\n FROM email_backfill_jobs\n WHERE id = $1\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c"
+}
diff --git a/.sqlx/query-508a71633632e814e11f627cf39133c9c6f36e9b18b47065db4a882478fd1b28.json b/.sqlx/query-508a71633632e814e11f627cf39133c9c6f36e9b18b47065db4a882478fd1b28.json
new file mode 100644
index 0000000000..a6fbebeca4
--- /dev/null
+++ b/.sqlx/query-508a71633632e814e11f627cf39133c9c6f36e9b18b47065db4a882478fd1b28.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT status::text AS \"status!\", initialized_at\n FROM email_backfill_jobs\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status!",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 1,
+ "name": "initialized_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null,
+ true
+ ]
+ },
+ "hash": "508a71633632e814e11f627cf39133c9c6f36e9b18b47065db4a882478fd1b28"
+}
diff --git a/.sqlx/query-adb83ca0fd4a69e6129b89df6a740e705b5fa34381265d81403a44a776919439.json b/.sqlx/query-61e3a8b8718918c4e8f7ac025b83db6cb7d2800b924c29803e776f8b56ce7726.json
similarity index 83%
rename from .sqlx/query-adb83ca0fd4a69e6129b89df6a740e705b5fa34381265d81403a44a776919439.json
rename to .sqlx/query-61e3a8b8718918c4e8f7ac025b83db6cb7d2800b924c29803e776f8b56ce7726.json
index deaed3af2e..e3046bd595 100644
--- a/.sqlx/query-adb83ca0fd4a69e6129b89df6a740e705b5fa34381265d81403a44a776919439.json
+++ b/.sqlx/query-61e3a8b8718918c4e8f7ac025b83db6cb7d2800b924c29803e776f8b56ce7726.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n INSERT INTO calendars (\n id, account_id, provider_calendar_id, name, description,\n time_zone, color, access_role, is_primary, is_selected\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ON CONFLICT (account_id, provider_calendar_id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n time_zone = EXCLUDED.time_zone,\n color = EXCLUDED.color,\n access_role = EXCLUDED.access_role,\n is_primary = EXCLUDED.is_primary,\n is_selected = EXCLUDED.is_selected,\n is_deleted = false,\n updated_at = now()\n RETURNING\n id,\n sync_token,\n materialized_starts_at,\n materialized_ends_at,\n materialized_start_date,\n materialized_end_date\n ",
+ "query": "\n INSERT INTO calendars (\n id, account_id, provider_calendar_id, name, description,\n time_zone, color, access_role, is_primary, is_selected\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ON CONFLICT (account_id, provider_calendar_id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n time_zone = EXCLUDED.time_zone,\n color = EXCLUDED.color,\n access_role = EXCLUDED.access_role,\n is_primary = EXCLUDED.is_primary,\n is_selected = EXCLUDED.is_selected,\n is_deleted = false,\n updated_at = now()\n RETURNING\n id,\n sync_token,\n materialized_starts_at,\n materialized_ends_at,\n materialized_start_date,\n materialized_end_date,\n synced_at,\n watch_expires_at\n ",
"describe": {
"columns": [
{
@@ -32,6 +32,16 @@
"ordinal": 5,
"name": "materialized_end_date",
"type_info": "Date"
+ },
+ {
+ "ordinal": 6,
+ "name": "synced_at",
+ "type_info": "Timestamptz"
+ },
+ {
+ "ordinal": 7,
+ "name": "watch_expires_at",
+ "type_info": "Timestamptz"
}
],
"parameters": {
@@ -54,8 +64,10 @@
true,
true,
true,
+ true,
+ true,
true
]
},
- "hash": "adb83ca0fd4a69e6129b89df6a740e705b5fa34381265d81403a44a776919439"
+ "hash": "61e3a8b8718918c4e8f7ac025b83db6cb7d2800b924c29803e776f8b56ce7726"
}
diff --git a/.sqlx/query-62304610537f75b3664e98760be3fab4e0610a0e200b4f66e6b2e7a40edbe725.json b/.sqlx/query-62304610537f75b3664e98760be3fab4e0610a0e200b4f66e6b2e7a40edbe725.json
new file mode 100644
index 0000000000..99707951ec
--- /dev/null
+++ b/.sqlx/query-62304610537f75b3664e98760be3fab4e0610a0e200b4f66e6b2e7a40edbe725.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET init_lease_expires_at = now() + interval '2 minutes',\n updated_at = now()\n WHERE id = $1\n AND status = 'Init'\n AND init_lease_token = $2\n AND init_lease_expires_at > now()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "62304610537f75b3664e98760be3fab4e0610a0e200b4f66e6b2e7a40edbe725"
+}
diff --git a/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json b/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json
new file mode 100644
index 0000000000..2e69b5e53e
--- /dev/null
+++ b/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'failed',\n last_error = 'email backfill failed before calendar extraction completed',\n completed_at = now(),\n updated_at = now()\n WHERE kind = 'email_ics'\n AND status = 'running'\n AND cursor->>'emailBackfillJobId' = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9"
+}
diff --git a/.sqlx/query-727667a1407fbabf0b8447639906e014dfa0b5ceb3839507b7e27646816c16a4.json b/.sqlx/query-727667a1407fbabf0b8447639906e014dfa0b5ceb3839507b7e27646816c16a4.json
new file mode 100644
index 0000000000..819c12689a
--- /dev/null
+++ b/.sqlx/query-727667a1407fbabf0b8447639906e014dfa0b5ceb3839507b7e27646816c16a4.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET status = 'Failed',\n updated_at = now()\n WHERE id = $1\n AND status IN ('Init', 'InProgress')\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "727667a1407fbabf0b8447639906e014dfa0b5ceb3839507b7e27646816c16a4"
+}
diff --git a/.sqlx/query-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json b/.sqlx/query-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json
new file mode 100644
index 0000000000..fe122352df
--- /dev/null
+++ b/.sqlx/query-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n (cursor->>'emailBackfillJobId')::uuid AS email_job_id\n FROM calendar_backfill_jobs\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'email_ics'\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email_job_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6"
+}
diff --git a/.sqlx/query-77cafa228e78eecc9d143dcb5d700e56b098b0d68887864c1f0f955f8bfbeba8.json b/.sqlx/query-77cafa228e78eecc9d143dcb5d700e56b098b0d68887864c1f0f955f8bfbeba8.json
new file mode 100644
index 0000000000..80056505b2
--- /dev/null
+++ b/.sqlx/query-77cafa228e78eecc9d143dcb5d700e56b098b0d68887864c1f0f955f8bfbeba8.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_sync_outbox\n SET published_at = now()\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "77cafa228e78eecc9d143dcb5d700e56b098b0d68887864c1f0f955f8bfbeba8"
+}
diff --git a/.sqlx/query-8585906f3f1b0170c67ac7e8aa1b03e706dffdb73094830f4781526521447195.json b/.sqlx/query-8585906f3f1b0170c67ac7e8aa1b03e706dffdb73094830f4781526521447195.json
new file mode 100644
index 0000000000..7304f65868
--- /dev/null
+++ b/.sqlx/query-8585906f3f1b0170c67ac7e8aa1b03e706dffdb73094830f4781526521447195.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO in_progress_user_link (\n id,\n macro_user_id,\n requested_google_scopes\n )\n VALUES ($1, $2, $3)\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "8585906f3f1b0170c67ac7e8aa1b03e706dffdb73094830f4781526521447195"
+}
diff --git a/.sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.json b/.sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.json
new file mode 100644
index 0000000000..1a26dd2bf2
--- /dev/null
+++ b/.sqlx/query-86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38.json
@@ -0,0 +1,17 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'complete',\n completed_at = now(),\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $3\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $4::text[]\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "86e304676292a96720d06f53478ab0dcb9e6c0bef75322619ef71389188b3d38"
+}
diff --git a/.sqlx/query-89ea8b5ca592db2f4acfef9069e554d0060e6b57776ae1baaf4a3a8b839c1112.json b/.sqlx/query-89ea8b5ca592db2f4acfef9069e554d0060e6b57776ae1baaf4a3a8b839c1112.json
new file mode 100644
index 0000000000..494bda7477
--- /dev/null
+++ b/.sqlx/query-89ea8b5ca592db2f4acfef9069e554d0060e6b57776ae1baaf4a3a8b839c1112.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE in_progress_user_link\n SET linked_email = $1,\n granted_google_scopes = $2\n WHERE id = $3\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Text",
+ "TextArray",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "89ea8b5ca592db2f4acfef9069e554d0060e6b57776ae1baaf4a3a8b839c1112"
+}
diff --git a/.sqlx/query-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json b/.sqlx/query-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json
new file mode 100644
index 0000000000..6250c06e71
--- /dev/null
+++ b/.sqlx/query-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT status::text AS \"status!\"\n FROM email_backfill_jobs\n WHERE id = $1\n AND link_id = $2\n FOR UPDATE\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "status!",
+ "type_info": "Text"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af"
+}
diff --git a/.sqlx/query-95237a5cf0f104663d438e3f5a8336277c4dc8849763c5cedfb6cb8cc2d6f161.json b/.sqlx/query-95237a5cf0f104663d438e3f5a8336277c4dc8849763c5cedfb6cb8cc2d6f161.json
new file mode 100644
index 0000000000..af99b1dba9
--- /dev/null
+++ b/.sqlx/query-95237a5cf0f104663d438e3f5a8336277c4dc8849763c5cedfb6cb8cc2d6f161.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET status = 'Complete',\n initialized_at = COALESCE(initialized_at, now()),\n init_lease_token = NULL,\n init_lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND (\n (\n status = 'Init'\n AND $2::uuid IS NOT NULL\n AND init_lease_token = $2\n AND init_lease_expires_at > now()\n )\n OR (\n status = 'InProgress'\n AND $2::uuid IS NULL\n )\n )\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "95237a5cf0f104663d438e3f5a8336277c4dc8849763c5cedfb6cb8cc2d6f161"
+}
diff --git a/.sqlx/query-96789ed93a8cfa77b5b52ad7ddf3d0dd91ed51285b810f0219a5b16414c6475b.json b/.sqlx/query-96789ed93a8cfa77b5b52ad7ddf3d0dd91ed51285b810f0219a5b16414c6475b.json
new file mode 100644
index 0000000000..c7f208bae4
--- /dev/null
+++ b/.sqlx/query-96789ed93a8cfa77b5b52ad7ddf3d0dd91ed51285b810f0219a5b16414c6475b.json
@@ -0,0 +1,21 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendars\n SET sync_token = $3,\n synced_at = now(),\n materialized_starts_at = CASE\n WHEN $4 THEN $5\n ELSE materialized_starts_at\n END,\n materialized_ends_at = CASE\n WHEN $4 THEN $6\n ELSE materialized_ends_at\n END,\n materialized_start_date = CASE\n WHEN $4 THEN $7\n ELSE materialized_start_date\n END,\n materialized_end_date = CASE\n WHEN $4 THEN $8\n ELSE materialized_end_date\n END,\n updated_at = now()\n WHERE id = $1\n AND account_id = $2\n AND NOT is_deleted\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Bool",
+ "Timestamptz",
+ "Timestamptz",
+ "Date",
+ "Date"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "96789ed93a8cfa77b5b52ad7ddf3d0dd91ed51285b810f0219a5b16414c6475b"
+}
diff --git a/.sqlx/query-9973b072d4ba913af3343333c70806a921d4eb47c55fc17c8d66bb4a388e0e09.json b/.sqlx/query-9973b072d4ba913af3343333c70806a921d4eb47c55fc17c8d66bb4a388e0e09.json
new file mode 100644
index 0000000000..0fa599c7f3
--- /dev/null
+++ b/.sqlx/query-9973b072d4ba913af3343333c70806a921d4eb47c55fc17c8d66bb4a388e0e09.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET completed_at = now()\n WHERE backfill_job_id = $1\n AND completed_at IS NULL\n AND effects_lease_token = $2\n AND effects_lease_expires_at > now()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "9973b072d4ba913af3343333c70806a921d4eb47c55fc17c8d66bb4a388e0e09"
+}
diff --git a/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.json b/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.json
new file mode 100644
index 0000000000..2bb2fcb98a
--- /dev/null
+++ b/.sqlx/query-a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH due AS (\n UPDATE calendar_backfill_jobs job\n SET status = 'pending',\n cursor = '{}',\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n FROM calendar_accounts account, email_links link\n WHERE job.email_link_id = $1\n AND job.email_link_id = account.email_link_id\n AND job.account_id = account.id\n AND link.id = job.email_link_id\n AND job.kind = 'google_calendar'\n AND job.status = 'complete'\n AND job.grant_version = link.google_grant_version\n AND link.google_granted_scopes @> $2::text[]\n AND account.sync_status = 'ready'\n RETURNING job.id\n ),\n republished AS (\n UPDATE calendar_sync_outbox outbox\n SET published_at = NULL\n FROM due\n WHERE outbox.backfill_job_id = due.id\n )\n SELECT count(*) > 0 AS \"scheduled!\" FROM due\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "scheduled!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "TextArray"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "a26e204c2c74cf9608191908dbcd0cbda042bd30e1600dd39baa1ab888f2ebea"
+}
diff --git a/.sqlx/query-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json b/.sqlx/query-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json
new file mode 100644
index 0000000000..2019fa09e7
--- /dev/null
+++ b/.sqlx/query-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'failed',\n last_error = $3,\n completed_at = now(),\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n AND kind = 'email_ics'\n AND status NOT IN ('complete', 'failed')\n RETURNING\n (cursor->>'emailBackfillJobId')::uuid AS email_job_id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "email_job_id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2"
+}
diff --git a/.sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json b/.sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json
new file mode 100644
index 0000000000..f0b682becd
--- /dev/null
+++ b/.sqlx/query-ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d.json
@@ -0,0 +1,30 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT event_id, normalized_payload\n FROM calendar_event_sources\n WHERE source_kind = 'google'\n AND account_id = $1\n AND calendar_id = $2\n AND provider_event_id = $3\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "event_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "normalized_payload",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "ba148316295d3b0aa2b2bcb3803a00e38ae028c72a10d112a707789f495b384d"
+}
diff --git a/.sqlx/query-bd39b17b754c18941a4c634bee1759203f612f75a54cb51e79714ce6e3b43616.json b/.sqlx/query-bd39b17b754c18941a4c634bee1759203f612f75a54cb51e79714ce6e3b43616.json
new file mode 100644
index 0000000000..4a941ec704
--- /dev/null
+++ b/.sqlx/query-bd39b17b754c18941a4c634bee1759203f612f75a54cb51e79714ce6e3b43616.json
@@ -0,0 +1,32 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n outbox.id,\n outbox.backfill_job_id,\n job.link_id AS \"email_link_id?\"\n FROM email_backfill_completion_outbox outbox\n JOIN email_backfill_jobs job ON job.id = outbox.backfill_job_id\n WHERE outbox.published_at IS NULL\n AND job.status = 'Complete'\n ORDER BY outbox.created_at, outbox.id\n FOR UPDATE OF outbox SKIP LOCKED\n LIMIT 1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "backfill_job_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "email_link_id?",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": []
+ },
+ "nullable": [
+ false,
+ false,
+ true
+ ]
+ },
+ "hash": "bd39b17b754c18941a4c634bee1759203f612f75a54cb51e79714ce6e3b43616"
+}
diff --git a/.sqlx/query-be88823aa337869574e673190ccba51519cf29d25e0fcf59b223debc25e15d96.json b/.sqlx/query-be88823aa337869574e673190ccba51519cf29d25e0fcf59b223debc25e15d96.json
new file mode 100644
index 0000000000..36268fb88d
--- /dev/null
+++ b/.sqlx/query-be88823aa337869574e673190ccba51519cf29d25e0fcf59b223debc25e15d96.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET effects_lease_token = NULL,\n effects_lease_expires_at = NULL\n WHERE backfill_job_id = $1\n AND completed_at IS NULL\n AND effects_lease_token = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "be88823aa337869574e673190ccba51519cf29d25e0fcf59b223debc25e15d96"
+}
diff --git a/.sqlx/query-bfe3f888258ac5d7317d1d46590a24b1a7d31bfe8a85b1662e2b19a1cf341f34.json b/.sqlx/query-bfe3f888258ac5d7317d1d46590a24b1a7d31bfe8a85b1662e2b19a1cf341f34.json
new file mode 100644
index 0000000000..2df9af5186
--- /dev/null
+++ b/.sqlx/query-bfe3f888258ac5d7317d1d46590a24b1a7d31bfe8a85b1662e2b19a1cf341f34.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET init_lease_token = NULL,\n init_lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND status = 'Init'\n AND init_lease_token = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "bfe3f888258ac5d7317d1d46590a24b1a7d31bfe8a85b1662e2b19a1cf341f34"
+}
diff --git a/.sqlx/query-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json b/.sqlx/query-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json
new file mode 100644
index 0000000000..04857c37b6
--- /dev/null
+++ b/.sqlx/query-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET status = 'Failed',\n updated_at = now()\n WHERE id = $1\n AND status = 'Init'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa"
+}
diff --git a/.sqlx/query-c160036949a29aab345bf402dee954fc49fc976251530b425520ede12349077a.json b/.sqlx/query-c160036949a29aab345bf402dee954fc49fc976251530b425520ede12349077a.json
new file mode 100644
index 0000000000..27eb4014e8
--- /dev/null
+++ b/.sqlx/query-c160036949a29aab345bf402dee954fc49fc976251530b425520ede12349077a.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_jobs\n SET status = 'InProgress',\n initialized_at = now(),\n init_lease_token = NULL,\n init_lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND status = 'Init'\n AND init_lease_token = $2\n AND init_lease_expires_at > now()\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "c160036949a29aab345bf402dee954fc49fc976251530b425520ede12349077a"
+}
diff --git a/.sqlx/query-c1c5a087a867d385bdbc0788af416a65a1d34ad733f48280e6f817ae6590b57e.json b/.sqlx/query-c1c5a087a867d385bdbc0788af416a65a1d34ad733f48280e6f817ae6590b57e.json
deleted file mode 100644
index 77f4c51245..0000000000
--- a/.sqlx/query-c1c5a087a867d385bdbc0788af416a65a1d34ad733f48280e6f817ae6590b57e.json
+++ /dev/null
@@ -1,18 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'complete',\n extracted_count = $2,\n completed_at = now(),\n lease_token = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $3\n AND kind = 'google_calendar'\n AND status = 'running'\n AND lease_token = $4\n AND lease_expires_at > now()\n AND EXISTS (\n SELECT 1\n FROM email_links link\n WHERE link.id = calendar_backfill_jobs.email_link_id\n AND link.google_grant_version = calendar_backfill_jobs.grant_version\n AND link.google_granted_scopes @> $5::text[]\n )\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Int8",
- "Uuid",
- "Uuid",
- "TextArray"
- ]
- },
- "nullable": []
- },
- "hash": "c1c5a087a867d385bdbc0788af416a65a1d34ad733f48280e6f817ae6590b57e"
-}
diff --git a/.sqlx/query-c7e67578aa4f0140f493efc82b170abf0e14fa343fb22a13cf4ffc54caf27cce.json b/.sqlx/query-c7e67578aa4f0140f493efc82b170abf0e14fa343fb22a13cf4ffc54caf27cce.json
deleted file mode 100644
index 51f56e661c..0000000000
--- a/.sqlx/query-c7e67578aa4f0140f493efc82b170abf0e14fa343fb22a13cf4ffc54caf27cce.json
+++ /dev/null
@@ -1,21 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE calendars\n SET sync_token = $3,\n materialized_starts_at = CASE\n WHEN $4 THEN $5\n ELSE materialized_starts_at\n END,\n materialized_ends_at = CASE\n WHEN $4 THEN $6\n ELSE materialized_ends_at\n END,\n materialized_start_date = CASE\n WHEN $4 THEN $7\n ELSE materialized_start_date\n END,\n materialized_end_date = CASE\n WHEN $4 THEN $8\n ELSE materialized_end_date\n END,\n updated_at = now()\n WHERE id = $1\n AND account_id = $2\n AND NOT is_deleted\n ",
- "describe": {
- "columns": [],
- "parameters": {
- "Left": [
- "Uuid",
- "Uuid",
- "Text",
- "Bool",
- "Timestamptz",
- "Timestamptz",
- "Date",
- "Date"
- ]
- },
- "nullable": []
- },
- "hash": "c7e67578aa4f0140f493efc82b170abf0e14fa343fb22a13cf4ffc54caf27cce"
-}
diff --git a/.sqlx/query-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json b/.sqlx/query-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json
new file mode 100644
index 0000000000..1dfe13ef1f
--- /dev/null
+++ b/.sqlx/query-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json
@@ -0,0 +1,19 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = $2,\n started_at = COALESCE(started_at, now()),\n completed_at = CASE WHEN $3 THEN now() ELSE NULL END,\n cursor = jsonb_build_object('emailBackfillJobId', $4::uuid),\n last_error = $5,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $6\n AND kind = 'email_ics'\n AND status <> 'complete'\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text",
+ "Bool",
+ "Uuid",
+ "Text",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188"
+}
diff --git a/.sqlx/query-cdfcb90c358eca0822811fcd11fe6077144442e8d4724f20ac664d6a90696232.json b/.sqlx/query-cdfcb90c358eca0822811fcd11fe6077144442e8d4724f20ac664d6a90696232.json
new file mode 100644
index 0000000000..800c44342a
--- /dev/null
+++ b/.sqlx/query-cdfcb90c358eca0822811fcd11fe6077144442e8d4724f20ac664d6a90696232.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_backfill_init_outbox (id, backfill_job_id)\n VALUES ($1, $2)\n ON CONFLICT (backfill_job_id) DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "cdfcb90c358eca0822811fcd11fe6077144442e8d4724f20ac664d6a90696232"
+}
diff --git a/.sqlx/query-ce1bad947bee5585d765566587354c2673502e303aeae2bcfa4dfb61fc2c2434.json b/.sqlx/query-ce1bad947bee5585d765566587354c2673502e303aeae2bcfa4dfb61fc2c2434.json
new file mode 100644
index 0000000000..a94c255709
--- /dev/null
+++ b/.sqlx/query-ce1bad947bee5585d765566587354c2673502e303aeae2bcfa4dfb61fc2c2434.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET published_at = COALESCE(published_at, now()),\n completed_at = COALESCE(completed_at, now())\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "ce1bad947bee5585d765566587354c2673502e303aeae2bcfa4dfb61fc2c2434"
+}
diff --git a/.sqlx/query-d4bb53d8249e571abb19cba30d523c1359895c11f8a73fb92337b668a405c42d.json b/.sqlx/query-d4bb53d8249e571abb19cba30d523c1359895c11f8a73fb92337b668a405c42d.json
new file mode 100644
index 0000000000..f8985941ac
--- /dev/null
+++ b/.sqlx/query-d4bb53d8249e571abb19cba30d523c1359895c11f8a73fb92337b668a405c42d.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_init_outbox\n SET published_at = now()\n WHERE id = $1\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "d4bb53d8249e571abb19cba30d523c1359895c11f8a73fb92337b668a405c42d"
+}
diff --git a/.sqlx/query-da139e715598117fe16dabccd90e9949672a2e785882757d00e538a59a40304d.json b/.sqlx/query-da139e715598117fe16dabccd90e9949672a2e785882757d00e538a59a40304d.json
new file mode 100644
index 0000000000..cfe75440e4
--- /dev/null
+++ b/.sqlx/query-da139e715598117fe16dabccd90e9949672a2e785882757d00e538a59a40304d.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_backfill_completion_outbox\n SET effects_lease_token = $2,\n effects_lease_expires_at = now() + interval '2 minutes'\n WHERE backfill_job_id = $1\n AND completed_at IS NULL\n AND (\n effects_lease_token IS NULL\n OR effects_lease_expires_at < now()\n )\n RETURNING effects_lease_token AS \"effects_lease_token!\"\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "effects_lease_token!",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ true
+ ]
+ },
+ "hash": "da139e715598117fe16dabccd90e9949672a2e785882757d00e538a59a40304d"
+}
diff --git a/.sqlx/query-f1107131b8da606d041af545e43099dd73cecd1a3b55c7cc12814db807baf8ed.json b/.sqlx/query-f1107131b8da606d041af545e43099dd73cecd1a3b55c7cc12814db807baf8ed.json
new file mode 100644
index 0000000000..066830d62b
--- /dev/null
+++ b/.sqlx/query-f1107131b8da606d041af545e43099dd73cecd1a3b55c7cc12814db807baf8ed.json
@@ -0,0 +1,18 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendars\n SET watch_channel_id = $3,\n watch_resource_id = $4,\n watch_expires_at = $5,\n updated_at = now()\n WHERE id = $1\n AND account_id = $2\n AND NOT is_deleted\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Text",
+ "Text",
+ "Timestamptz"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f1107131b8da606d041af545e43099dd73cecd1a3b55c7cc12814db807baf8ed"
+}
diff --git a/.sqlx/query-f5782320106bfac840a938ad834491495552ed9c02393230cccb5c827e9ae021.json b/.sqlx/query-f5782320106bfac840a938ad834491495552ed9c02393230cccb5c827e9ae021.json
new file mode 100644
index 0000000000..2e8a8967a6
--- /dev/null
+++ b/.sqlx/query-f5782320106bfac840a938ad834491495552ed9c02393230cccb5c827e9ae021.json
@@ -0,0 +1,40 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT\n macro_user_id,\n linked_email,\n requested_google_scopes,\n granted_google_scopes\n FROM\n in_progress_user_link\n WHERE\n id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "macro_user_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "linked_email",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 2,
+ "name": "requested_google_scopes",
+ "type_info": "TextArray"
+ },
+ {
+ "ordinal": 3,
+ "name": "granted_google_scopes",
+ "type_info": "TextArray"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ true,
+ false,
+ false
+ ]
+ },
+ "hash": "f5782320106bfac840a938ad834491495552ed9c02393230cccb5c827e9ae021"
+}
diff --git a/.sqlx/query-f8d1a3062e3de072fe8ac1e8256125203616db9f235245d8431e712e25a5c102.json b/.sqlx/query-f8d1a3062e3de072fe8ac1e8256125203616db9f235245d8431e712e25a5c102.json
new file mode 100644
index 0000000000..0c4dcf7487
--- /dev/null
+++ b/.sqlx/query-f8d1a3062e3de072fe8ac1e8256125203616db9f235245d8431e712e25a5c102.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT completed_at IS NULL AS \"pending!\"\n FROM email_backfill_completion_outbox\n WHERE backfill_job_id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "pending!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "f8d1a3062e3de072fe8ac1e8256125203616db9f235245d8431e712e25a5c102"
+}
diff --git a/.sqlx/query-fafa84eb636a2855f683f73eba494f9ce25445a642ef58e1f8a59788a7f374b8.json b/.sqlx/query-fafa84eb636a2855f683f73eba494f9ce25445a642ef58e1f8a59788a7f374b8.json
deleted file mode 100644
index 9c9523edbb..0000000000
--- a/.sqlx/query-fafa84eb636a2855f683f73eba494f9ce25445a642ef58e1f8a59788a7f374b8.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n SELECT\n macro_user_id,\n linked_email\n FROM\n in_progress_user_link\n WHERE\n id = $1\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "macro_user_id",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "linked_email",
- "type_info": "Text"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- false,
- true
- ]
- },
- "hash": "fafa84eb636a2855f683f73eba494f9ce25445a642ef58e1f8a59788a7f374b8"
-}
diff --git a/.sqlx/query-feb210dc04a982ac051c02fccec534631e5af01b62070b66ccd7432b9d9a6942.json b/.sqlx/query-feb210dc04a982ac051c02fccec534631e5af01b62070b66ccd7432b9d9a6942.json
new file mode 100644
index 0000000000..863634933a
--- /dev/null
+++ b/.sqlx/query-feb210dc04a982ac051c02fccec534631e5af01b62070b66ccd7432b9d9a6942.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET extracted_count = extracted_count + $3,\n updated_at = now()\n WHERE id = $1\n AND email_link_id = $2\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Int8"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "feb210dc04a982ac051c02fccec534631e5af01b62070b66ccd7432b9d9a6942"
+}
diff --git a/Cargo.lock b/Cargo.lock
index 1645a16898..96e0baf1b1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1086,6 +1086,7 @@ dependencies = [
"aws-sdk-sqs",
"axum",
"base64 0.22.1",
+ "calendar_events",
"channels",
"chrono",
"comms_db_client",
@@ -2520,16 +2521,20 @@ dependencies = [
"axum",
"chrono",
"futures",
+ "icalendar",
"macro_authorization",
"macro_db_migrator",
"models_pagination",
+ "reqwest 0.13.4",
"rootcause",
"serde",
"serde_json",
+ "sha2 0.10.9",
"sqlx",
"thiserror 2.0.18",
"tokio",
"tracing",
+ "urlencoding",
"utoipa",
"uuid",
"workspace-hack",
@@ -5092,6 +5097,7 @@ dependencies = [
"axum-extra",
"base64 0.22.1",
"bytes",
+ "calendar_events",
"chrono",
"cloudfront_sign",
"connection_gateway_client",
@@ -5147,6 +5153,7 @@ dependencies = [
"redis",
"regex",
"reqwest 0.13.4",
+ "rootcause",
"s3_client",
"schemars 1.2.1",
"scraper",
@@ -7162,6 +7169,22 @@ dependencies = [
"cc",
]
+[[package]]
+name = "icalendar"
+version = "0.17.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3478c3b7a2411366daf20457373174bff6cfbc1089333038b1f95594d23d985"
+dependencies = [
+ "chrono",
+ "chrono-tz",
+ "iso8601",
+ "nom 8.0.0",
+ "nom-language",
+ "rrule",
+ "thiserror 2.0.18",
+ "uuid",
+]
+
[[package]]
name = "icu_collections"
version = "2.2.0"
@@ -7662,6 +7685,15 @@ version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
+[[package]]
+name = "iso8601"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "84d00640a3a46595e984f1b124b21d56506a5844dc799f831dde9b05cb2f6c51"
+dependencies = [
+ "nom 8.0.0",
+]
+
[[package]]
name = "item_filters"
version = "0.1.0"
@@ -9877,6 +9909,15 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "nom-language"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2de2bc5b451bfedaef92c90b8939a8fff5770bdcc1fafd6239d086aab8fa6b29"
+dependencies = [
+ "nom 8.0.0",
+]
+
[[package]]
name = "non_empty"
version = "0.1.0"
@@ -12461,6 +12502,19 @@ dependencies = [
"text-size",
]
+[[package]]
+name = "rrule"
+version = "0.14.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "720acfb4980b9d8a6a430f6d7a11933e701ebbeba5eee39cc9d8c5f932aaff74"
+dependencies = [
+ "chrono",
+ "chrono-tz",
+ "log",
+ "regex",
+ "thiserror 2.0.18",
+]
+
[[package]]
name = "rs-libreoffice-bindings"
version = "0.0.7"
@@ -16729,6 +16783,7 @@ dependencies = [
"bzip2-sys",
"cc",
"chrono",
+ "chrono-tz",
"crossbeam-epoch",
"crossbeam-utils",
"crypto-bigint",
@@ -16794,6 +16849,7 @@ dependencies = [
"rand 0.8.6",
"rand 0.9.4",
"redis",
+ "regex",
"regex-automata",
"regex-syntax",
"reqwest 0.13.4",
diff --git a/Cargo.toml b/Cargo.toml
index d33cf816aa..56ef3ee5aa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -156,6 +156,7 @@ hmac = "0.12.1"
http = "1"
http-body-util = "0.1.2"
image = "0.25"
+icalendar = { version = "0.17.12", features = ["chrono-tz", "recurrence"] }
indexmap = "2"
inquire = "0.7"
itertools = "0.14"
diff --git a/crates/calendar_events/Cargo.toml b/crates/calendar_events/Cargo.toml
index 30fdee08eb..6327b01838 100644
--- a/crates/calendar_events/Cargo.toml
+++ b/crates/calendar_events/Cargo.toml
@@ -6,6 +6,8 @@ version = "0.1.0"
[features]
default = []
+google = ["dep:reqwest", "dep:urlencoding"]
+ics = ["dep:icalendar", "dep:sha2"]
inbound = [
"dep:axum",
"dep:macro_authorization",
@@ -19,18 +21,22 @@ schema = ["dep:utoipa"]
axum = { workspace = true, optional = true }
chrono = { workspace = true }
futures = { workspace = true }
+icalendar = { workspace = true, optional = true }
macro_authorization = { path = "../macro_authorization", default-features = false, features = [
"axum",
], optional = true }
models_pagination = { path = "../models_pagination", optional = true }
+reqwest = { workspace = true, optional = true }
rootcause = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
+sha2 = { workspace = true, optional = true }
sqlx = { workspace = true, optional = true }
thiserror = { workspace = true }
tokio = { workspace = true, optional = true }
tracing = { workspace = true }
utoipa = { workspace = true, optional = true }
+urlencoding = { workspace = true, optional = true }
uuid = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }
diff --git a/crates/calendar_events/src/domain/models.rs b/crates/calendar_events/src/domain/models.rs
index 03c7d3959f..c78ea5f5af 100644
--- a/crates/calendar_events/src/domain/models.rs
+++ b/crates/calendar_events/src/domain/models.rs
@@ -546,6 +546,53 @@ pub struct StoredGoogleCalendar {
pub sync_token: Option<String>,
/// Exact recurrence window materialized by the last full snapshot.
pub materialized_range: Option<OccurrenceRange>,
+ /// When this calendar's sync state last committed, if ever.
+ pub synced_at: Option<DateTime<Utc>>,
+ /// Expiry of the active push notification channel, when one exists.
+ pub watch_expires_at: Option<DateTime<Utc>>,
+}
+
+/// How often Google's own read-only system calendars (holidays, birthdays)
+/// are synced. Their content changes on the order of once a year, and Google
+/// chronically resets their sync tokens, turning every poll into a full
+/// snapshot; a daily cadence keeps them fresh without that churn.
+pub const SYSTEM_CALENDAR_SYNC_INTERVAL: chrono::Duration = chrono::Duration::hours(24);
+
+/// Whether a provider calendar is one of Google's shared system calendars
+/// (`en.usa#[email protected]` and friends) rather than a
+/// calendar a person maintains.
+pub fn is_system_calendar(provider_calendar_id: &str) -> bool {
+ provider_calendar_id.ends_with("@group.v.calendar.google.com")
+}
+
+/// Deployment configuration enabling Google push notification channels.
+#[derive(Clone, PartialEq, Eq)]
+pub struct GoogleWatchConfig {
+ /// Public HTTPS address Google delivers notifications to.
+ pub address: String,
+ /// Shared verification token echoed back on every notification.
+ pub token: String,
+}
+
+impl std::fmt::Debug for GoogleWatchConfig {
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ formatter
+ .debug_struct("GoogleWatchConfig")
+ .field("address", &self.address)
+ .field("token", &"<redacted>")
+ .finish()
+ }
+}
+
+/// An active Google push notification channel for one calendar.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct GoogleWatchChannel {
+ /// Client-minted channel identifier.
+ pub channel_id: Uuid,
+ /// Provider-assigned resource identifier required to stop the channel.
+ pub resource_id: String,
+ /// Provider-assigned channel expiry.
+ pub expires_at: DateTime<Utc>,
}
/// How the provider adapter must reconcile one calendar this run.
diff --git a/crates/calendar_events/src/domain/models/test.rs b/crates/calendar_events/src/domain/models/test.rs
index d679419f41..f01b97941a 100644
--- a/crates/calendar_events/src/domain/models/test.rs
+++ b/crates/calendar_events/src/domain/models/test.rs
@@ -45,6 +45,8 @@ fn sync_plan_extends_only_the_uncovered_tail() {
id: Uuid::now_v7(),
sync_token: Some("token".to_string()),
materialized_range: Some(materialized.clone()),
+ synced_at: None,
+ watch_expires_at: None,
};
assert_eq!(
@@ -74,6 +76,8 @@ fn sync_plan_extends_only_the_uncovered_tail() {
id: Uuid::now_v7(),
sync_token: None,
materialized_range: Some(materialized.clone()),
+ synced_at: None,
+ watch_expires_at: None,
};
assert_eq!(
uninitialized.sync_plan(&OccurrenceRange::historical_sync(now)),
@@ -84,6 +88,8 @@ fn sync_plan_extends_only_the_uncovered_tail() {
id: Uuid::now_v7(),
sync_token: Some("token".to_string()),
materialized_range: None,
+ synced_at: None,
+ watch_expires_at: None,
};
assert_eq!(
unmaterialized.sync_plan(&OccurrenceRange::historical_sync(now)),
diff --git a/crates/calendar_events/src/domain/ports.rs b/crates/calendar_events/src/domain/ports.rs
index 704cef1fa4..457aa2fae3 100644
--- a/crates/calendar_events/src/domain/ports.rs
+++ b/crates/calendar_events/src/domain/ports.rs
@@ -11,8 +11,8 @@ use super::models::{
CalendarBackfillFailureOutcome, CalendarBackfillJobKey, CalendarEvent, CalendarEventUpsert,
CalendarOccurrence, CalendarOccurrenceCursor, CalendarSyncStatus, EmailCalendarBackfillState,
EmailCalendarScanAssociation, EmailCalendarScanJob, GoogleCalendarSyncSnapshot,
- GoogleEventSyncBatch, GoogleScopeSet, GoogleSyncPlan, OccurrenceRange, ProviderCalendar,
- StoredGoogleCalendar,
+ GoogleEventSyncBatch, GoogleScopeSet, GoogleSyncPlan, GoogleWatchChannel, GoogleWatchConfig,
+ OccurrenceRange, ProviderCalendar, StoredGoogleCalendar,
};
/// Classification supplied by provider adapters to backfill policy.
@@ -151,16 +151,43 @@ pub trait CalendarRepository: Send + Sync + 'static {
) -> impl Future<Output = Result<StoredGoogleCalendar, Report>> + Send;
/// Durably commit one calendar's poll under the backfill's fencing token:
- /// prune sources a full snapshot no longer observed, then advance the
- /// calendar's continuation token and materialized range.
+ /// prune sources a full snapshot no longer observed, advance the
+ /// calendar's continuation token and materialized range, and add this
+ /// calendar's upserts to the job's running progress counters.
fn commit_google_calendar_sync(
&self,
key: CalendarBackfillJobKey,
lease_token: Uuid,
account_id: Uuid,
sync: GoogleCalendarSyncSnapshot,
+ events_upserted: usize,
) -> impl Future<Output = Result<(), Report>> + Send;
+ /// Record a freshly opened push channel for one calendar under the
+ /// backfill's fencing token.
+ fn record_watch_channel(
+ &self,
+ key: CalendarBackfillJobKey,
+ lease_token: Uuid,
+ account_id: Uuid,
+ calendar_id: Uuid,
+ channel: GoogleWatchChannel,
+ ) -> impl Future<Output = Result<(), Report>> + Send;
+
+ /// Resolve a push notification to the inbox whose calendar it watches.
+ fn find_watch_target(
+ &self,
+ channel_id: &str,
+ resource_id: &str,
+ ) -> impl Future<Output = Result<Option<Uuid>, Report>> + Send;
+
+ /// Re-arm a completed sync job for one inbox, returning whether a run
+ /// was scheduled. Pending or running jobs absorb the notification.
+ fn schedule_google_sync_for_link(
+ &self,
+ email_link_id: Uuid,
+ ) -> impl Future<Output = Result<bool, Report>> + Send;
+
/// Reconcile the provider's calendar list under the backfill's fencing
/// token, removing calendars and sources no longer returned by Google.
fn reconcile_google_calendar_list(
@@ -178,6 +205,7 @@ pub trait GoogleCalendarProvider: Send + Sync + 'static {
fn list_calendars(
&self,
access_token: &str,
+ email_link_id: Uuid,
) -> impl Future<Output = Result<Vec<ProviderCalendar>, GoogleProviderError>> + Send;
/// Poll provider changes and, when needed, rebuild the bounded event snapshot.
@@ -186,6 +214,16 @@ pub trait GoogleCalendarProvider: Send + Sync + 'static {
access_token: &str,
context: GoogleEventSyncContext,
) -> impl Future<Output = Result<GoogleEventSyncBatch, GoogleProviderError>> + Send;
+
+ /// Open a push notification channel for one provider calendar.
+ fn watch_calendar(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ channel_id: Uuid,
+ config: &GoogleWatchConfig,
+ ) -> impl Future<Output = Result<GoogleWatchChannel, GoogleProviderError>> + Send;
}
/// Durable scheduling operations for periodic provider maintenance.
@@ -227,12 +265,12 @@ pub trait CalendarBackfillRepository: Send + Sync + 'static {
lease_token: Uuid,
) -> impl Future<Output = Result<(), Report>> + Send;
- /// Atomically complete the job and mark its account ready.
+ /// Atomically complete the job and mark its account ready; progress
+ /// counters were already accumulated by the per-calendar commits.
fn complete_google_backfill(
&self,
key: CalendarBackfillJobKey,
lease_token: Uuid,
- extracted_count: usize,
) -> impl Future<Output = Result<(), Report>> + Send;
/// Persist a classified failure, releasing or terminating the job.
diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rs
index 34eac6972c..3dfb9e8f6d 100644
--- a/crates/calendar_events/src/domain/service.rs
+++ b/crates/calendar_events/src/domain/service.rs
@@ -245,6 +245,28 @@ where
) -> Result<super::models::CalendarSyncStatus, Report> {
self.repository.sync_status(requester_id).await
}
+
+ /// Re-arm the watched inbox's sync job for a push notification whose
+ /// channel token the adapter already verified. Returns whether the
+ /// notification matched an active channel.
+ #[tracing::instrument(skip(self, channel_id, resource_id), err)]
+ pub async fn handle_watch_notification(
+ &self,
+ channel_id: &str,
+ resource_id: &str,
+ ) -> Result<bool, Report> {
+ let Some(email_link_id) = self
+ .repository
+ .find_watch_target(channel_id, resource_id)
+ .await?
+ else {
+ return Ok(false);
+ };
+ self.repository
+ .schedule_google_sync_for_link(email_link_id)
+ .await?;
+ Ok(true)
+ }
}
impl<R> CalendarOccurrenceService for CalendarService<R>
@@ -281,8 +303,13 @@ where
pub struct GoogleCalendarBackfillService<R, G> {
repository: R,
provider: G,
+ watch: Option<super::models::GoogleWatchConfig>,
}
+/// Renew a channel whenever less than this much lifetime remains, so every
+/// poll cycle has several chances before expiry.
+const WATCH_RENEWAL_THRESHOLD: chrono::Duration = chrono::Duration::hours(12);
+
/// Periodically makes completed provider jobs eligible for another incremental poll.
pub struct GoogleCalendarSyncScheduler<R> {
repository: R,
@@ -375,6 +402,7 @@ pub struct GoogleCalendarBackfillCoordinator<R, G, L> {
repository: R,
provider: G,
lifecycle: L,
+ watch: Option<super::models::GoogleWatchConfig>,
}
impl<R, G, L> GoogleCalendarBackfillCoordinator<R, G, L>
@@ -383,12 +411,19 @@ where
G: GoogleCalendarProvider + Clone,
L: CalendarBackfillRepository,
{
- /// Construct a coordinator from domain ports.
- pub fn new(repository: R, provider: G, lifecycle: L) -> Self {
+ /// Construct a coordinator from domain ports; supplying a watch config
+ /// makes every backfill maintain push notification channels.
+ pub fn new(
+ repository: R,
+ provider: G,
+ lifecycle: L,
+ watch: Option<super::models::GoogleWatchConfig>,
+ ) -> Self {
Self {
repository,
provider,
lifecycle,
+ watch,
}
}
@@ -437,8 +472,11 @@ where
return Err(GoogleCalendarBackfillRunError::Retryable(message));
}
- let backfill =
- GoogleCalendarBackfillService::new(self.repository.clone(), self.provider.clone());
+ let backfill = GoogleCalendarBackfillService::new(
+ self.repository.clone(),
+ self.provider.clone(),
+ self.watch.clone(),
+ );
let work = backfill.backfill(key, lease_token, account_id, owner_id, access_token, range);
let lease = self
.lifecycle
@@ -455,7 +493,7 @@ where
match result {
Ok(count) => {
self.lifecycle
- .complete_google_backfill(key, lease_token, count)
+ .complete_google_backfill(key, lease_token)
.await
.map_err(|_| GoogleCalendarBackfillRunError::LeaseLost)?;
Ok(count)
@@ -520,10 +558,15 @@ where
G: GoogleCalendarProvider,
{
/// Construct the provider backfill service.
- pub fn new(repository: R, provider: G) -> Self {
+ pub fn new(
+ repository: R,
+ provider: G,
+ watch: Option<super::models::GoogleWatchConfig>,
+ ) -> Self {
Self {
repository,
provider,
+ watch,
}
}
@@ -543,7 +586,7 @@ where
}
let calendars = self
.provider
- .list_calendars(access_token)
+ .list_calendars(access_token, key.email_link_id)
.await
.map_err(|error| -> Report { rootcause::report!(error).into() })?;
let mut count = 0;
@@ -551,6 +594,7 @@ where
for provider_calendar in calendars {
let provider_calendar_id = provider_calendar.provider_calendar_id.clone();
+ let watch_provider_calendar_id = provider_calendar.provider_calendar_id.clone();
let is_read_only = !matches!(
provider_calendar.access_role.as_deref(),
Some("owner" | "writer")
@@ -561,6 +605,13 @@ where
.await?;
let calendar_id = stored_calendar.id;
calendar_ids.push(calendar_id);
+ if super::models::is_system_calendar(&provider_calendar_id)
+ && stored_calendar.synced_at.is_some_and(|synced_at| {
+ synced_at > Utc::now() - super::models::SYSTEM_CALENDAR_SYNC_INTERVAL
+ })
+ {
+ continue;
+ }
let plan = stored_calendar.sync_plan(&range);
let batch = self
.provider
@@ -580,6 +631,7 @@ where
)
.await
.map_err(|error| -> Report { rootcause::report!(error).into() })?;
+ let mut calendar_count = 0;
for upsert in batch.upserts {
if let Err(error) = validate_upsert(&upsert) {
tracing::warn!(
@@ -600,8 +652,9 @@ where
upsert,
})
.await?;
- count += 1;
+ calendar_count += 1;
}
+ count += calendar_count;
// Committing per calendar keeps earlier calendars' sync tokens
// durable when a later calendar's poll fails, so the retry only
// re-pulls what never committed.
@@ -617,8 +670,58 @@ where
materialized_range: batch.materialized_range,
cancelled_provider_event_ids: batch.cancelled_provider_event_ids,
},
+ calendar_count,
)
.await?;
+
+ // Channel upkeep is best-effort: the poll remains the backstop,
+ // so a failed watch call must not fail the sync that just
+ // committed durable progress.
+ if let Some(watch) = &self.watch
+ && stored_calendar
+ .watch_expires_at
+ .is_none_or(|expires_at| expires_at < Utc::now() + WATCH_RENEWAL_THRESHOLD)
+ {
+ let channel_id = Uuid::new_v4();
+ match self
+ .provider
+ .watch_calendar(
+ access_token,
+ key.email_link_id,
+ &watch_provider_calendar_id,
+ channel_id,
+ watch,
+ )
+ .await
+ {
+ Ok(channel) => {
+ self.repository
+ .record_watch_channel(
+ key,
+ lease_token,
+ account_id,
+ calendar_id,
+ channel,
+ )
+ .await
+ .inspect_err(|error| {
+ tracing::warn!(
+ error=?error,
+ calendar_id=%calendar_id,
+ "failed to record Google Calendar watch channel"
+ );
+ })
+ .ok();
+ }
+ Err(error) => {
+ tracing::warn!(
+ error=?error,
+ calendar_id=%calendar_id,
+ "failed to open Google Calendar watch channel"
+ );
+ }
+ }
+ }
}
self.repository
diff --git a/crates/calendar_events/src/domain/service/test.rs b/crates/calendar_events/src/domain/service/test.rs
index 46776699bd..02941d3143 100644
--- a/crates/calendar_events/src/domain/service/test.rs
+++ b/crates/calendar_events/src/domain/service/test.rs
@@ -6,8 +6,8 @@ use crate::domain::{
CalendarEventSource, CalendarOccurrence, CalendarSyncStatus, EmailCalendarBackfillState,
EmailCalendarScanAssociation, EmailCalendarScanJob, EmailCalendarScanStatus,
EmailIcsSource, EventStatus, EventTime, EventTransparency, EventVisibility,
- GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSyncBatch, ProviderCalendar,
- StoredGoogleCalendar,
+ GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSyncBatch,
+ GoogleWatchChannel, GoogleWatchConfig, ProviderCalendar, StoredGoogleCalendar,
},
ports::{
CalendarBackfillRepository, CalendarEventWrite, CalendarRepository,
@@ -21,6 +21,7 @@ use std::sync::{Arc, Mutex};
#[derive(Clone, Default)]
struct FakeRepo {
upserts: Arc<Mutex<Vec<CalendarEventUpsert>>>,
+ stored_synced_at: Option<chrono::DateTime<Utc>>,
}
impl CalendarRepository for FakeRepo {
@@ -68,6 +69,8 @@ impl CalendarRepository for FakeRepo {
id: Uuid::nil(),
sync_token: None,
materialized_range: None,
+ synced_at: self.stored_synced_at,
+ watch_expires_at: None,
})
}
@@ -77,6 +80,7 @@ impl CalendarRepository for FakeRepo {
_lease_token: Uuid,
_account_id: Uuid,
_sync: GoogleCalendarSyncSnapshot,
+ _events_upserted: usize,
) -> Result<(), Report> {
Ok(())
}
@@ -90,6 +94,29 @@ impl CalendarRepository for FakeRepo {
) -> Result<(), Report> {
Ok(())
}
+
+ async fn record_watch_channel(
+ &self,
+ _key: CalendarBackfillJobKey,
+ _lease_token: Uuid,
+ _account_id: Uuid,
+ _calendar_id: Uuid,
+ _channel: GoogleWatchChannel,
+ ) -> Result<(), Report> {
+ Ok(())
+ }
+
+ async fn find_watch_target(
+ &self,
+ _channel_id: &str,
+ _resource_id: &str,
+ ) -> Result<Option<Uuid>, Report> {
+ Ok(None)
+ }
+
+ async fn schedule_google_sync_for_link(&self, _email_link_id: Uuid) -> Result<bool, Report> {
+ Ok(false)
+ }
}
fn valid_upsert() -> CalendarEventUpsert {
@@ -196,6 +223,7 @@ impl GoogleCalendarProvider for FakeGoogleProvider {
async fn list_calendars(
&self,
_access_token: &str,
+ _email_link_id: Uuid,
) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {
Ok(Vec::new())
}
@@ -213,6 +241,17 @@ impl GoogleCalendarProvider for FakeGoogleProvider {
cancelled_provider_event_ids: Vec::new(),
})
}
+
+ async fn watch_calendar(
+ &self,
+ _access_token: &str,
+ _email_link_id: Uuid,
+ _provider_calendar_id: &str,
+ _channel_id: Uuid,
+ _config: &GoogleWatchConfig,
+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {
+ unreachable!("watch is disabled in these tests")
+ }
}
#[derive(Clone)]
@@ -222,6 +261,7 @@ impl GoogleCalendarProvider for ReauthGoogleProvider {
async fn list_calendars(
&self,
_access_token: &str,
+ _email_link_id: Uuid,
) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {
Err(GoogleProviderError::new(
GoogleProviderErrorKind::ReauthRequired,
@@ -236,12 +276,23 @@ impl GoogleCalendarProvider for ReauthGoogleProvider {
) -> Result<GoogleEventSyncBatch, GoogleProviderError> {
unreachable!("calendar listing fails first")
}
+
+ async fn watch_calendar(
+ &self,
+ _access_token: &str,
+ _email_link_id: Uuid,
+ _provider_calendar_id: &str,
+ _channel_id: Uuid,
+ _config: &GoogleWatchConfig,
+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {
+ unreachable!("watch is disabled in these tests")
+ }
}
#[derive(Clone)]
struct FakeLifecycle {
claim: CalendarBackfillClaim,
- completions: Arc<Mutex<Vec<usize>>>,
+ completions: Arc<Mutex<Vec<()>>>,
failures: Arc<Mutex<Vec<CalendarBackfillFailureDisposition>>>,
failure_outcome: CalendarBackfillFailureOutcome,
lease_lost: bool,
@@ -312,9 +363,8 @@ impl CalendarBackfillRepository for FakeLifecycle {
&self,
_key: CalendarBackfillJobKey,
_lease_token: Uuid,
- extracted_count: usize,
) -> Result<(), Report> {
- self.completions.lock().unwrap().push(extracted_count);
+ self.completions.lock().unwrap().push(());
Ok(())
}
@@ -337,6 +387,7 @@ async fn google_coordinator_owns_claim_and_completion_lifecycle() {
FakeRepo::default(),
FakeGoogleProvider,
lifecycle.clone(),
+ None,
);
let count = coordinator
@@ -353,7 +404,79 @@ async fn google_coordinator_owns_claim_and_completion_lifecycle() {
.unwrap();
assert_eq!(count, 0);
- assert_eq!(lifecycle.completions.lock().unwrap().as_slice(), &[0]);
+ assert_eq!(lifecycle.completions.lock().unwrap().len(), 1);
+}
+
+#[derive(Clone)]
+struct SystemCalendarProvider;
+
+impl GoogleCalendarProvider for SystemCalendarProvider {
+ async fn list_calendars(
+ &self,
+ _access_token: &str,
+ _email_link_id: Uuid,
+ ) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {
+ Ok(vec![ProviderCalendar {
+ provider_calendar_id: "en.usa#[email protected]".to_string(),
+ name: "Holidays in United States".to_string(),
+ description: None,
+ time_zone: Some("UTC".to_string()),
+ color: None,
+ access_role: Some("reader".to_string()),
+ is_primary: false,
+ is_selected: true,
+ }])
+ }
+
+ async fn sync_events(
+ &self,
+ _access_token: &str,
+ _context: GoogleEventSyncContext,
+ ) -> Result<GoogleEventSyncBatch, GoogleProviderError> {
+ unreachable!("freshly synced system calendars must not sync")
+ }
+
+ async fn watch_calendar(
+ &self,
+ _access_token: &str,
+ _email_link_id: Uuid,
+ _provider_calendar_id: &str,
+ _channel_id: Uuid,
+ _config: &GoogleWatchConfig,
+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {
+ unreachable!("watch is disabled in these tests")
+ }
+}
+
+#[tokio::test]
+async fn freshly_synced_system_calendars_are_skipped() {
+ let lifecycle = FakeLifecycle::claimed();
+ let repo = FakeRepo {
+ stored_synced_at: Some(Utc::now()),
+ ..FakeRepo::default()
+ };
+ let coordinator = GoogleCalendarBackfillCoordinator::new(
+ repo,
+ SystemCalendarProvider,
+ lifecycle.clone(),
+ None,
+ );
+
+ let count = coordinator
+ .run(
+ CalendarBackfillJobKey {
+ job_id: Uuid::now_v7(),
+ email_link_id: Uuid::now_v7(),
+ },
+ "secret",
+ OccurrenceRange::maintenance_horizon(Utc::now()),
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(count, 0);
+ assert_eq!(lifecycle.completions.lock().unwrap().len(), 1);
}
#[derive(Clone)]
@@ -363,6 +486,7 @@ impl GoogleCalendarProvider for HangingGoogleProvider {
async fn list_calendars(
&self,
_access_token: &str,
+ _email_link_id: Uuid,
) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {
std::future::pending().await
}
@@ -374,6 +498,17 @@ impl GoogleCalendarProvider for HangingGoogleProvider {
) -> Result<GoogleEventSyncBatch, GoogleProviderError> {
unreachable!("provider hangs before syncing events")
}
+
+ async fn watch_calendar(
+ &self,
+ _access_token: &str,
+ _email_link_id: Uuid,
+ _provider_calendar_id: &str,
+ _channel_id: Uuid,
+ _config: &GoogleWatchConfig,
+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {
+ unreachable!("watch is disabled in these tests")
+ }
}
#[tokio::test]
@@ -383,6 +518,7 @@ async fn google_coordinator_surfaces_lease_loss_while_work_is_running() {
FakeRepo::default(),
HangingGoogleProvider,
lifecycle.clone(),
+ None,
);
let error = coordinator
@@ -410,6 +546,7 @@ async fn google_coordinator_keeps_calendar_permission_health_separate_from_gmail
FakeRepo::default(),
ReauthGoogleProvider,
lifecycle.clone(),
+ None,
);
let error = coordinator
diff --git a/crates/calendar_events/src/inbound/ics.rs b/crates/calendar_events/src/inbound/ics.rs
new file mode 100644
index 0000000000..3333089b41
--- /dev/null
+++ b/crates/calendar_events/src/inbound/ics.rs
@@ -0,0 +1,575 @@
+//! RFC 5545 email invitation adapter.
+
+use std::collections::BTreeMap;
+
+use chrono::{Duration, Utc};
+use icalendar::{Calendar, CalendarDateTime, Class, Component, DatePerhapsTime, Event, EventLike};
+use rootcause::Report;
+use sha2::{Digest, Sha256};
+use uuid::Uuid;
+
+use crate::domain::models::{
+ AttendeeResponseStatus, CalendarAttendee, CalendarEvent, CalendarEventOverride,
+ CalendarEventSource, CalendarEventUpsert, CalendarOccurrence, EmailIcsSource, EventStart,
+ EventStatus, EventTime, EventTransparency, EventVisibility, OccurrenceRange,
+};
+
+/// Errors returned while adapting an iCalendar message into the domain.
+#[derive(Debug, thiserror::Error)]
+pub enum IcsParseError {
+ /// Input was not UTF-8.
+ #[error("iCalendar input is not valid UTF-8")]
+ InvalidUtf8,
+ /// The calendar document was invalid.
+ #[error("invalid iCalendar document: {0}")]
+ InvalidCalendar(String),
+ /// A VEVENT lacked a stable UID.
+ #[error("iCalendar VEVENT is missing UID")]
+ MissingUid,
+ /// A VEVENT had an unsupported or incomplete time shape.
+ #[error("iCalendar VEVENT {0} has an invalid or unsupported DTSTART/DTEND")]
+ InvalidTime(String),
+ /// Recurrence expansion failed.
+ #[error("iCalendar VEVENT {0} has an invalid recurrence rule: {1}")]
+ InvalidRecurrence(String, String),
+}
+
+#[derive(Debug)]
+struct ParsedEvent {
+ event: CalendarEvent,
+ recurrence_id: Option<EventStart>,
+ occurrences: Vec<CalendarOccurrence>,
+}
+
+/// Return the stable SHA-256 content identity used for idempotent extraction.
+pub fn ics_content_hash(bytes: &[u8]) -> String {
+ let digest = Sha256::digest(bytes);
+ format!("{digest:x}")
+}
+
+/// Parse an iCalendar MIME part into canonical event upserts.
+///
+/// Multiple VEVENTs with the same UID are combined into one master plus
+/// recurrence overrides. Recurrences are materialized only inside `horizon`;
+/// the raw rule remains on the canonical entity for future horizon extension.
+#[tracing::instrument(skip(bytes, source, horizon), err)]
+pub fn parse_email_ics(
+ owner_id: &str,
+ source: EmailIcsSource,
+ bytes: &[u8],
+ horizon: &OccurrenceRange,
+) -> Result<Vec<CalendarEventUpsert>, Report> {
+ let input =
+ std::str::from_utf8(bytes).map_err(|_| rootcause::report!(IcsParseError::InvalidUtf8))?;
+ let calendar: Calendar = input.parse().map_err(|error| {
+ rootcause::report!(IcsParseError::InvalidCalendar(format!("{error:?}")))
+ })?;
+ let method_cancel = calendar
+ .property_value("METHOD")
+ .is_some_and(|method| method.eq_ignore_ascii_case("CANCEL"));
+
+ let mut by_uid: BTreeMap<String, Vec<ParsedEvent>> = BTreeMap::new();
+ for event in calendar.events() {
+ match parse_event(owner_id, event, horizon, method_cancel) {
+ Ok(parsed) => {
+ by_uid
+ .entry(parsed.event.ical_uid.clone())
+ .or_default()
+ .push(parsed);
+ }
+ Err(error) => {
+ tracing::warn!(error = ?error, "skipping malformed VEVENT");
+ }
+ }
+ }
+
+ let mut upserts = Vec::with_capacity(by_uid.len());
+ for (uid, mut components) in by_uid {
+ components.sort_by_key(|component| component.event.sequence);
+ let Some(master_index) = components
+ .iter()
+ .rposition(|component| component.recurrence_id.is_none())
+ else {
+ tracing::warn!(ical_uid = %uid, "skipping iCalendar UID without a master VEVENT");
+ continue;
+ };
+ let master = components.remove(master_index);
+ let event_id = master.event.id;
+ let mut occurrences = master.occurrences;
+ let mut overrides = Vec::with_capacity(components.len());
+
+ for component in components {
+ let Some(original_time) = component.recurrence_id else {
+ continue;
+ };
+ let recurrence_id = original_time.occurrence_key();
+ occurrences.retain(|occurrence| occurrence.occurrence_key != recurrence_id);
+ let replacement = CalendarOccurrence {
+ event_id,
+ occurrence_key: recurrence_id.clone(),
+ recurrence_id: Some(recurrence_id.clone()),
+ time: component.event.time.clone(),
+ is_cancelled: component.event.status == EventStatus::Cancelled,
+ };
+ if replacement.is_cancelled || replacement.time.overlaps(horizon) {
+ occurrences.push(replacement);
+ }
+ overrides.push(CalendarEventOverride {
+ recurrence_id,
+ original_time,
+ time: component.event.time,
+ title: Some(component.event.title),
+ description: component.event.description,
+ location: component.event.location,
+ status: Some(component.event.status),
+ });
+ }
+
+ upserts.push(CalendarEventUpsert {
+ event: master.event,
+ source: CalendarEventSource::EmailIcs(source.clone()),
+ overrides,
+ occurrences,
+ });
+ }
+
+ Ok(upserts)
+}
+
+fn parse_event(
+ owner_id: &str,
+ event: &Event,
+ horizon: &OccurrenceRange,
+ method_cancel: bool,
+) -> Result<ParsedEvent, Report> {
+ let uid = event
+ .get_uid()
+ .filter(|uid| !uid.trim().is_empty())
+ .ok_or_else(|| rootcause::report!(IcsParseError::MissingUid))?
+ .to_string();
+ let time = parse_time(event, &uid)?;
+ let recurrence_id = event
+ .get_recurrence_id()
+ .map(parse_start)
+ .transpose()
+ .map_err(|_| rootcause::report!(IcsParseError::InvalidTime(uid.clone())))?;
+ let recurrence_lines = recurrence_lines(event);
+ let id = Uuid::now_v7();
+ let created_at = event.get_created().unwrap_or_else(Utc::now);
+ let updated_at = event
+ .get_last_modified()
+ .or_else(|| event.get_timestamp())
+ .unwrap_or(created_at);
+ let sequence = event.get_sequence().unwrap_or_default();
+ let organizer = event.properties().get("ORGANIZER");
+ let organizer_email = organizer
+ .map(|property| normalize_calendar_address(property.value()))
+ .filter(|email| !email.is_empty());
+ let organizer_name = organizer
+ .and_then(|property| property.params().get("CN"))
+ .map(|parameter| parameter.value().to_string());
+ let owner_email = owner_id
+ .rsplit_once('|')
+ .map(|(_, email)| email.to_ascii_lowercase());
+ let attendees = parse_attendees(event, organizer_email.as_deref(), owner_email.as_deref());
+
+ let status = if method_cancel {
+ EventStatus::Cancelled
+ } else {
+ parse_status(event.property_value("STATUS"))
+ };
+ let canonical = CalendarEvent {
+ id,
+ owner_id: owner_id.to_string(),
+ ical_uid: uid.clone(),
+ title: event.get_summary().unwrap_or_default().to_string(),
+ description: event.get_description().map(ToOwned::to_owned),
+ location: event.get_location().map(ToOwned::to_owned),
+ status,
+ visibility: parse_visibility(event.get_class()),
+ transparency: parse_transparency(event.property_value("TRANSP")),
+ time: time.clone(),
+ recurrence_lines,
+ organizer_email,
+ organizer_name,
+ conference_url: conference_url(event),
+ sequence,
+ is_read_only: true,
+ attendees,
+ created_at,
+ updated_at,
+ };
+
+ let occurrences = if recurrence_id.is_some() {
+ Vec::new()
+ } else {
+ materialize_occurrences(
+ event,
+ id,
+ &uid,
+ &time,
+ horizon,
+ status == EventStatus::Cancelled,
+ )?
+ };
+
+ Ok(ParsedEvent {
+ event: canonical,
+ recurrence_id,
+ occurrences,
+ })
+}
+
+fn parse_time(event: &Event, uid: &str) -> Result<EventTime, Report> {
+ let start = event
+ .get_start()
+ .ok_or_else(|| rootcause::report!(IcsParseError::InvalidTime(uid.to_string())))?;
+ let end = event.get_end();
+
+ match (start, end) {
+ (DatePerhapsTime::Date(start_date), Some(DatePerhapsTime::Date(end_date)))
+ if end_date > start_date =>
+ {
+ Ok(EventTime::AllDay {
+ start_date,
+ end_date,
+ })
+ }
+ // RFC 5545 gives an all-day VEVENT without DTEND a one-day duration.
+ (DatePerhapsTime::Date(start_date), None) => Ok(EventTime::AllDay {
+ start_date,
+ end_date: start_date + Duration::days(1),
+ }),
+ // RFC 5545 also allows a timed VEVENT to carry DURATION instead of
+ // DTEND; instants with neither stay rejected since a zero-length
+ // span cannot materialize an occurrence.
+ (DatePerhapsTime::DateTime(start), None) => {
+ let duration = event
+ .properties()
+ .get("DURATION")
+ .and_then(|property| parse_ical_duration(property.value()))
+ .ok_or_else(|| rootcause::report!(IcsParseError::InvalidTime(uid.to_string())))?;
+ let time_zone = timezone_id(&start);
+ let starts_at = start
+ .try_into_utc()
+ .ok_or_else(|| rootcause::report!(IcsParseError::InvalidTime(uid.to_string())))?;
+ let ends_at = starts_at + duration;
+ if ends_at <= starts_at {
+ return Err(rootcause::report!(IcsParseError::InvalidTime(uid.to_string())).into());
+ }
+ Ok(EventTime::Timed {
+ starts_at,
+ ends_at,
+ time_zone,
+ })
+ }
+ (DatePerhapsTime::DateTime(start), Some(DatePerhapsTime::DateTime(end))) => {
+ let time_zone = timezone_id(&start);
+ let starts_at = start
+ .try_into_utc()
+ .ok_or_else(|| rootcause::report!(IcsParseError::InvalidTime(uid.to_string())))?;
+ let ends_at = end
+ .try_into_utc()
+ .ok_or_else(|| rootcause::report!(IcsParseError::InvalidTime(uid.to_string())))?;
+ if ends_at <= starts_at {
+ return Err(rootcause::report!(IcsParseError::InvalidTime(uid.to_string())).into());
+ }
+ Ok(EventTime::Timed {
+ starts_at,
+ ends_at,
+ time_zone,
+ })
+ }
+ _ => Err(rootcause::report!(IcsParseError::InvalidTime(uid.to_string())).into()),
+ }
+}
+
+/// Parse an RFC 5545 DURATION value (`P2W`, `P1DT2H30M`, `PT45M`, ...).
+fn parse_ical_duration(value: &str) -> Option<Duration> {
+ let value = value.trim();
+ let (sign, rest) = match value.strip_prefix('-') {
+ Some(rest) => (-1, rest),
+ None => (1, value.strip_prefix('+').unwrap_or(value)),
+ };
+ let rest = rest.strip_prefix('P')?;
+ if let Some(weeks) = rest.strip_suffix('W') {
+ let weeks: i64 = weeks.parse().ok()?;
+ return Some(Duration::seconds(sign * weeks * 7 * 86_400));
+ }
+ let (date_part, time_part) = match rest.split_once('T') {
+ Some((date_part, time_part)) => (date_part, time_part),
+ None => (rest, ""),
+ };
+ let mut seconds: i64 = 0;
+ let mut digits = String::new();
+ for ch in date_part.chars() {
+ if ch.is_ascii_digit() {
+ digits.push(ch);
+ } else if ch == 'D' {
+ seconds += digits.parse::<i64>().ok()? * 86_400;
+ digits.clear();
+ } else {
+ return None;
+ }
+ }
+ if !digits.is_empty() {
+ return None;
+ }
+ for ch in time_part.chars() {
+ if ch.is_ascii_digit() {
+ digits.push(ch);
+ } else {
+ let unit: i64 = match ch {
+ 'H' => 3_600,
+ 'M' => 60,
+ 'S' => 1,
+ _ => return None,
+ };
+ seconds += digits.parse::<i64>().ok()? * unit;
+ digits.clear();
+ }
+ }
+ if !digits.is_empty() {
+ return None;
+ }
+ Some(Duration::seconds(sign * seconds))
+}
+
+fn parse_start(value: DatePerhapsTime) -> Result<EventStart, ()> {
+ match value {
+ DatePerhapsTime::Date(date) => Ok(EventStart::AllDay(date)),
+ DatePerhapsTime::DateTime(date_time) => {
+ date_time.try_into_utc().map(EventStart::Timed).ok_or(())
+ }
+ }
+}
+
+fn timezone_id(value: &CalendarDateTime) -> Option<String> {
+ match value {
+ CalendarDateTime::Utc(_) => Some("UTC".to_string()),
+ CalendarDateTime::WithTimezone { tzid, .. } => Some(tzid.clone()),
+ CalendarDateTime::Floating(_) => None,
+ }
+}
+
+/// Whether the VEVENT carries a recurrence rule or date set in either of
+/// `icalendar`'s property maps — repeated properties land in the multi map,
+/// so checking a single map per key silently drops whole series.
+fn declares_recurrence(event: &Event) -> bool {
+ ["RRULE", "RDATE"].iter().any(|key| {
+ event.properties().contains_key(*key)
+ || event
+ .multi_properties()
+ .get(*key)
+ .is_some_and(|properties| !properties.is_empty())
+ })
+}
+
+fn recurrence_lines(event: &Event) -> Vec<String> {
+ let mut lines = Vec::new();
+ for key in ["RRULE", "RDATE", "EXDATE"] {
+ if let Some(property) = event.properties().get(key) {
+ lines.push(format!("{key}:{}", property.value()));
+ }
+ if let Some(properties) = event.multi_properties().get(key) {
+ lines.extend(
+ properties
+ .iter()
+ .map(|property| format!("{key}:{}", property.value())),
+ );
+ }
+ }
+ lines
+}
+
+fn materialize_occurrences(
+ event: &Event,
+ event_id: Uuid,
+ uid: &str,
+ event_time: &EventTime,
+ horizon: &OccurrenceRange,
+ is_cancelled: bool,
+) -> Result<Vec<CalendarOccurrence>, Report> {
+ if !declares_recurrence(event) {
+ return Ok(event_time
+ .overlaps(horizon)
+ .then(|| CalendarOccurrence {
+ event_id,
+ occurrence_key: event_time.occurrence_key(),
+ recurrence_id: None,
+ time: event_time.clone(),
+ is_cancelled,
+ })
+ .into_iter()
+ .collect());
+ }
+
+ let recurrence = event.get_recurrence().map_err(|error| {
+ rootcause::report!(IcsParseError::InvalidRecurrence(
+ uid.to_string(),
+ error.to_string()
+ ))
+ })?;
+ let duration = event_duration(event_time);
+ let after = horizon
+ .starts_at
+ .checked_sub_signed(duration)
+ .unwrap_or(horizon.starts_at)
+ .with_timezone(&icalendar::rrule::Tz::UTC);
+ let before = horizon.ends_at.with_timezone(&icalendar::rrule::Tz::UTC);
+ const MAX_MATERIALIZED_OCCURRENCES: usize = 20_000;
+ let result = recurrence
+ .after(after)
+ .before(before)
+ .all((MAX_MATERIALIZED_OCCURRENCES + 1) as u16);
+ let mut dates = result.dates;
+ let mut materialized_range = horizon.clone();
+ if result.limited {
+ let first_unmaterialized = dates.pop().ok_or_else(|| {
+ rootcause::report!(IcsParseError::InvalidRecurrence(
+ uid.to_string(),
+ "recurrence expansion reached its safety limit before producing coverage"
+ .to_string()
+ ))
+ })?;
+ dates.truncate(MAX_MATERIALIZED_OCCURRENCES);
+ let first_unmaterialized = first_unmaterialized.with_timezone(&Utc);
+ materialized_range.ends_at = materialized_range.ends_at.min(first_unmaterialized);
+ materialized_range.end_date = materialized_range
+ .end_date
+ .min(first_unmaterialized.date_naive());
+ if !materialized_range.is_valid_for_backfill() {
+ return Err(rootcause::report!(IcsParseError::InvalidRecurrence(
+ uid.to_string(),
+ "recurrence expansion exhausted its safety limit before the requested horizon"
+ .to_string()
+ ))
+ .into());
+ }
+ }
+
+ Ok(dates
+ .into_iter()
+ .filter_map(|starts| {
+ let starts_at = starts.with_timezone(&Utc);
+ let time = match event_time {
+ EventTime::Timed { time_zone, .. } => EventTime::Timed {
+ starts_at,
+ ends_at: starts_at + duration,
+ time_zone: time_zone.clone(),
+ },
+ EventTime::AllDay {
+ start_date,
+ end_date,
+ } => {
+ let days = *end_date - *start_date;
+ let occurrence_start = starts_at.date_naive();
+ EventTime::AllDay {
+ start_date: occurrence_start,
+ end_date: occurrence_start + days,
+ }
+ }
+ };
+ time.overlaps(horizon).then(|| CalendarOccurrence {
+ event_id,
+ occurrence_key: time.occurrence_key(),
+ recurrence_id: None,
+ time,
+ is_cancelled,
+ })
+ })
+ .collect())
+}
+
+fn event_duration(time: &EventTime) -> Duration {
+ match time {
+ EventTime::Timed {
+ starts_at, ends_at, ..
+ } => *ends_at - *starts_at,
+ EventTime::AllDay {
+ start_date,
+ end_date,
+ } => *end_date - *start_date,
+ }
+}
+
+fn normalize_calendar_address(value: &str) -> String {
+ value
+ .trim()
+ .strip_prefix("mailto:")
+ .or_else(|| value.trim().strip_prefix("MAILTO:"))
+ .unwrap_or(value.trim())
+ .to_ascii_lowercase()
+}
+
+fn parse_attendees(
+ event: &Event,
+ organizer_email: Option<&str>,
+ owner_email: Option<&str>,
+) -> Vec<CalendarAttendee> {
+ event
+ .get_attendees()
+ .into_iter()
+ .filter_map(|attendee| {
+ let email = normalize_calendar_address(&attendee.cal_address);
+ (!email.is_empty()).then(|| CalendarAttendee {
+ is_organizer: organizer_email == Some(email.as_str()),
+ is_self: owner_email == Some(email.as_str()),
+ email,
+ display_name: attendee.cn,
+ response_status: match attendee
+ .part_stat
+ .map(|status| format!("{status:?}").to_ascii_lowercase())
+ .as_deref()
+ {
+ Some("accepted") => AttendeeResponseStatus::Accepted,
+ Some("declined") => AttendeeResponseStatus::Declined,
+ Some("tentative") => AttendeeResponseStatus::Tentative,
+ _ => AttendeeResponseStatus::NeedsAction,
+ },
+ is_optional: attendee
+ .role
+ .is_some_and(|role| format!("{role:?}").eq_ignore_ascii_case("OptParticipant")),
+ comment: None,
+ })
+ })
+ .collect()
+}
+
+fn parse_status(value: Option<&str>) -> EventStatus {
+ match value.map(str::to_ascii_lowercase).as_deref() {
+ Some("tentative") => EventStatus::Tentative,
+ Some("cancelled") => EventStatus::Cancelled,
+ _ => EventStatus::Confirmed,
+ }
+}
+
+fn parse_visibility(value: Option<Class>) -> EventVisibility {
+ match value {
+ Some(Class::Public) => EventVisibility::Public,
+ Some(Class::Private) => EventVisibility::Private,
+ Some(Class::Confidential) => EventVisibility::Confidential,
+ _ => EventVisibility::Default,
+ }
+}
+
+fn parse_transparency(value: Option<&str>) -> EventTransparency {
+ if value.is_some_and(|value| value.eq_ignore_ascii_case("TRANSPARENT")) {
+ EventTransparency::Transparent
+ } else {
+ EventTransparency::Opaque
+ }
+}
+
+fn conference_url(event: &Event) -> Option<String> {
+ [
+ "X-GOOGLE-CONFERENCE",
+ "X-MICROSOFT-SKYPETEAMSMEETINGURL",
+ "URL",
+ ]
+ .into_iter()
+ .find_map(|key| event.property_value(key).map(ToOwned::to_owned))
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/calendar_events/src/inbound/ics/test.rs b/crates/calendar_events/src/inbound/ics/test.rs
new file mode 100644
index 0000000000..c8f4029f0f
--- /dev/null
+++ b/crates/calendar_events/src/inbound/ics/test.rs
@@ -0,0 +1,177 @@
+use super::*;
+use chrono::{NaiveDate, TimeZone};
+
+fn range() -> OccurrenceRange {
+ OccurrenceRange {
+ starts_at: Utc.with_ymd_and_hms(2026, 7, 1, 0, 0, 0).unwrap(),
+ ends_at: Utc.with_ymd_and_hms(2026, 8, 1, 0, 0, 0).unwrap(),
+ start_date: NaiveDate::from_ymd_opt(2026, 7, 1).unwrap(),
+ end_date: NaiveDate::from_ymd_opt(2026, 8, 1).unwrap(),
+ }
+}
+
+fn source(bytes: &[u8]) -> EmailIcsSource {
+ EmailIcsSource {
+ email_link_id: Uuid::now_v7(),
+ email_thread_id: Some(Uuid::now_v7()),
+ email_message_id: Uuid::now_v7(),
+ email_attachment_id: Some("invite".to_string()),
+ content_hash: ics_content_hash(bytes),
+ raw_payload: serde_json::json!({"mimeType": "text/calendar"}),
+ }
+}
+
+#[test]
+fn parses_timed_invitation_and_attendees() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART:20260724T140000Z\r\nDTEND:20260724T150000Z\r\nSUMMARY:Design review\r\nORGANIZER;CN=Taylor:mailto:[email protected]\r\nATTENDEE;CN=Sam;PARTSTAT=ACCEPTED:mailto:[email protected]\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].event.ical_uid, "[email protected]");
+ assert_eq!(events[0].event.title, "Design review");
+ assert_eq!(
+ events[0].event.organizer_email.as_deref(),
+ );
+ assert_eq!(events[0].event.attendees[0].email, "[email protected]");
+ assert_eq!(events[0].occurrences.len(), 1);
+}
+
+#[test]
+fn parses_all_day_with_exclusive_end() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART;VALUE=DATE:20260724\r\nDTEND;VALUE=DATE:20260726\r\nSUMMARY:Offsite\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert!(matches!(
+ events[0].event.time,
+ EventTime::AllDay {
+ start_date,
+ end_date
+ } if start_date == NaiveDate::from_ymd_opt(2026, 7, 24).unwrap()
+ && end_date == NaiveDate::from_ymd_opt(2026, 7, 26).unwrap()
+ ));
+}
+
+#[test]
+fn defaults_an_all_day_event_without_dtend_to_one_day() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART;VALUE=DATE:20260724\r\nSUMMARY:Holiday\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert!(matches!(
+ events[0].event.time,
+ EventTime::AllDay {
+ start_date,
+ end_date
+ } if start_date == NaiveDate::from_ymd_opt(2026, 7, 24).unwrap()
+ && end_date == NaiveDate::from_ymd_opt(2026, 7, 25).unwrap()
+ ));
+}
+
+#[test]
+fn folds_override_into_master_entity() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART:20260724T140000Z\r\nDTEND:20260724T143000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\nSUMMARY:Standup\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nRECURRENCE-ID:20260725T140000Z\r\nDTSTART:20260725T150000Z\r\nDTEND:20260725T153000Z\r\nSUMMARY:Late standup\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].overrides.len(), 1);
+ assert_eq!(events[0].occurrences.len(), 3);
+ assert!(
+ events[0]
+ .occurrences
+ .iter()
+ .any(|occurrence| occurrence.recurrence_id.is_some())
+ );
+}
+
+#[test]
+fn recurrence_limit_caps_materialized_occurrences() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T000000Z\r\nDTSTART:20260701T000000Z\r\nDTEND:20260701T000100Z\r\nRRULE:FREQ=MINUTELY\r\nSUMMARY:Dense recurrence\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+ let requested = range();
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &requested).unwrap();
+
+ assert_eq!(events[0].occurrences.len(), 20_000);
+ let last_start = events[0]
+ .occurrences
+ .iter()
+ .map(|occurrence| match occurrence.time {
+ EventTime::Timed { starts_at, .. } => starts_at,
+ EventTime::AllDay { .. } => unreachable!("dense recurrence is timed"),
+ })
+ .max()
+ .unwrap();
+ assert!(last_start < requested.ends_at);
+}
+
+#[test]
+fn calendar_cancel_method_cancels_the_materialized_occurrence() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nMETHOD:CANCEL\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART:20260724T140000Z\r\nDTEND:20260724T150000Z\r\nSUMMARY:Cancelled meeting\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert_eq!(events[0].event.status, EventStatus::Cancelled);
+ assert!(events[0].occurrences[0].is_cancelled);
+}
+
+#[test]
+fn malformed_event_does_not_discard_valid_events_in_the_same_calendar() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nSUMMARY:Missing time\r\nEND:VEVENT\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART:20260724T140000Z\r\nDTEND:20260724T150000Z\r\nSUMMARY:Valid meeting\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert_eq!(events.len(), 1);
+ assert_eq!(events[0].event.ical_uid, "[email protected]");
+}
+
+#[test]
+fn recurrence_is_detected_in_either_property_map() {
+ let mut single = Event::new();
+ single.add_property("RRULE", "FREQ=DAILY");
+ assert!(declares_recurrence(&single));
+
+ let mut multi = Event::new();
+ multi.append_multi_property(icalendar::Property::new("RRULE", "FREQ=WEEKLY"));
+ assert!(
+ declares_recurrence(&multi),
+ "an RRULE stored in multi_properties must still count as recurring"
+ );
+
+ let mut rdate = Event::new();
+ rdate.append_multi_property(icalendar::Property::new("RDATE", "20260801T120000Z"));
+ assert!(declares_recurrence(&rdate));
+
+ assert!(!declares_recurrence(&Event::new()));
+}
+
+#[test]
+fn duration_stands_in_for_a_missing_dtend() {
+ let ics = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:[email protected]\r\nDTSTAMP:20260701T120000Z\r\nDTSTART:20260724T140000Z\r\nDURATION:PT1H30M\r\nSUMMARY:Duration event\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
+
+ let events = parse_email_ics("macro|[email protected]", source(ics), ics, &range()).unwrap();
+
+ assert_eq!(events.len(), 1);
+ let EventTime::Timed {
+ starts_at, ends_at, ..
+ } = events[0].event.time
+ else {
+ panic!("expected a timed event");
+ };
+ assert_eq!((ends_at - starts_at).num_minutes(), 90);
+}
+
+#[test]
+fn ical_durations_parse_per_rfc_5545() {
+ assert_eq!(parse_ical_duration("PT1H"), Some(Duration::hours(1)));
+ assert_eq!(
+ parse_ical_duration("P1DT2H30M"),
+ Some(Duration::minutes(24 * 60 + 150))
+ );
+ assert_eq!(parse_ical_duration("P2W"), Some(Duration::weeks(2)));
+ assert_eq!(parse_ical_duration("-PT15M"), Some(Duration::minutes(-15)));
+ assert_eq!(parse_ical_duration("nonsense"), None);
+ assert_eq!(parse_ical_duration("P1X"), None);
+}
diff --git a/crates/calendar_events/src/inbound/mod.rs b/crates/calendar_events/src/inbound/mod.rs
index a1c6982abb..df41f26f7d 100644
--- a/crates/calendar_events/src/inbound/mod.rs
+++ b/crates/calendar_events/src/inbound/mod.rs
@@ -1,4 +1,9 @@
//! Inbound calendar adapters.
/// Axum routes for authenticated calendar queries.
+#[cfg(feature = "inbound")]
pub mod axum_router;
+
+/// RFC 5545 iCalendar invitation parsing.
+#[cfg(feature = "ics")]
+pub mod ics;
diff --git a/crates/calendar_events/src/lib.rs b/crates/calendar_events/src/lib.rs
index fab2e0bd6e..7ab87ba33a 100644
--- a/crates/calendar_events/src/lib.rs
+++ b/crates/calendar_events/src/lib.rs
@@ -1,5 +1,5 @@
#![deny(missing_docs)]
-//! Calendar event domain and persistence adapter.
+//! Calendar event domain, parsing, and provider persistence adapters.
//!
//! A [`domain::models::CalendarEvent`] is the stable Macro entity. External
//! calendars and email invitations are sources of that entity, while
@@ -7,9 +7,9 @@
/// Calendar business models, ports, and services.
pub mod domain;
-/// Inbound adapters that expose calendar use cases.
-#[cfg(feature = "inbound")]
+/// Inbound adapters that expose calendar use cases and accepted formats.
+#[cfg(any(feature = "ics", feature = "inbound"))]
pub mod inbound;
-/// Database adapters used by service composition roots.
-#[cfg(feature = "postgres")]
+/// Database and provider adapters used by service composition roots.
+#[cfg(any(feature = "google", feature = "postgres"))]
pub mod outbound;
diff --git a/crates/calendar_events/src/outbound/google.rs b/crates/calendar_events/src/outbound/google.rs
new file mode 100644
index 0000000000..7474ab9417
--- /dev/null
+++ b/crates/calendar_events/src/outbound/google.rs
@@ -0,0 +1,1265 @@
+//! Google Calendar API adapter.
+
+use std::collections::{BTreeMap, BTreeSet};
+
+use chrono::{DateTime, NaiveDate, Utc};
+use reqwest::{Client, RequestBuilder, StatusCode};
+use rootcause::Report;
+use serde::{Deserialize, Serialize, de::DeserializeOwned};
+use uuid::Uuid;
+
+use crate::domain::{
+ models::{
+ AttendeeResponseStatus, CalendarAttendee, CalendarEvent, CalendarEventOverride,
+ CalendarEventSource, CalendarEventUpsert, CalendarOccurrence, EventStart, EventStatus,
+ EventTime, EventTransparency, EventVisibility, GoogleEventSource, GoogleEventSyncBatch,
+ GoogleSyncPlan, GoogleWatchChannel, GoogleWatchConfig, OccurrenceRange, ProviderCalendar,
+ },
+ ports::{
+ GoogleCalendarProvider, GoogleEventSyncContext, GoogleProviderError,
+ GoogleProviderErrorKind,
+ },
+};
+
+const GOOGLE_CALENDAR_API: &str = "https://www.googleapis.com/calendar/v3";
+
+/// Consulted before every Google Calendar HTTP request so deployments can
+/// enforce the per-user API quota. Denials surface as transient provider
+/// errors, which the backfill lifecycle retries.
+pub trait GoogleRequestGate: Send + Sync + 'static {
+ /// Admit one provider request on behalf of the connected inbox.
+ fn acquire(
+ &self,
+ email_link_id: Uuid,
+ ) -> impl Future<Output = Result<(), GoogleProviderError>> + Send;
+}
+
+/// Gate that admits every request, for tests and unmetered environments.
+#[derive(Clone, Copy, Debug, Default)]
+pub struct UnmeteredGate;
+
+impl GoogleRequestGate for UnmeteredGate {
+ async fn acquire(&self, _email_link_id: Uuid) -> Result<(), GoogleProviderError> {
+ Ok(())
+ }
+}
+
+/// Google Calendar REST client.
+#[derive(Clone)]
+pub struct GoogleCalendarClient<G = UnmeteredGate> {
+ client: Client,
+ gate: G,
+}
+
+impl GoogleCalendarClient<UnmeteredGate> {
+ /// Construct an unmetered client using the application's HTTP client.
+ pub fn new(client: Client) -> Self {
+ Self {
+ client,
+ gate: UnmeteredGate,
+ }
+ }
+}
+
+impl<G: GoogleRequestGate> GoogleCalendarClient<G> {
+ /// Construct a client whose requests must pass the supplied quota gate.
+ pub fn with_gate(client: Client, gate: G) -> Self {
+ Self { client, gate }
+ }
+
+ async fn calendars(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ ) -> Result<Vec<GoogleCalendar>, GoogleProviderError> {
+ let mut page_token: Option<String> = None;
+ let mut result = Vec::new();
+ loop {
+ self.gate.acquire(email_link_id).await?;
+ let mut request = self
+ .client
+ .get(format!("{GOOGLE_CALENDAR_API}/users/me/calendarList"))
+ .bearer_auth(access_token)
+ .query(&[("maxResults", "250"), ("minAccessRole", "reader")]);
+ if let Some(token) = &page_token {
+ request = request.query(&[("pageToken", token)]);
+ }
+ let page: GoogleCalendarListResponse = send_google(request).await?;
+ result.extend(page.items);
+ page_token = page.next_page_token;
+ if page_token.is_none() {
+ break;
+ }
+ }
+ Ok(result)
+ }
+
+ async fn events(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ range: &OccurrenceRange,
+ single_events: bool,
+ ) -> Result<Vec<GoogleEvent>, GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ let mut page_token: Option<String> = None;
+ let mut result = Vec::new();
+ loop {
+ self.gate.acquire(email_link_id).await?;
+ let mut request = self
+ .client
+ .get(format!("{GOOGLE_CALENDAR_API}/calendars/{calendar}/events"))
+ .bearer_auth(access_token)
+ .query(&[
+ ("maxResults", "2500".to_string()),
+ ("singleEvents", single_events.to_string()),
+ ("showDeleted", "false".to_string()),
+ ("timeMin", range.starts_at.to_rfc3339()),
+ ("timeMax", range.ends_at.to_rfc3339()),
+ ("timeZone", "UTC".to_string()),
+ ]);
+ if single_events {
+ request = request.query(&[("orderBy", "startTime")]);
+ }
+ if let Some(token) = &page_token {
+ request = request.query(&[("pageToken", token)]);
+ }
+ let page: GoogleEventListResponse = send_google(request).await?;
+ result.extend(page.items);
+ page_token = page.next_page_token;
+ if page_token.is_none() {
+ break;
+ }
+ }
+ Ok(result)
+ }
+
+ async fn event_changes(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ sync_token: Option<&str>,
+ window: &OccurrenceRange,
+ ) -> Result<(Vec<GoogleEvent>, String), GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ let mut page_token: Option<String> = None;
+ let mut result = Vec::new();
+ loop {
+ self.gate.acquire(email_link_id).await?;
+ let mut request = self
+ .client
+ .get(format!("{GOOGLE_CALENDAR_API}/calendars/{calendar}/events"))
+ .bearer_auth(access_token)
+ .query(&[
+ ("maxResults", "2500"),
+ ("singleEvents", "false"),
+ ("showDeleted", "true"),
+ ]);
+ if let Some(token) = sync_token {
+ // Google forbids combining a sync token with time bounds; the
+ // token already encodes the ones it was minted with.
+ request = request.query(&[("syncToken", token)]);
+ } else {
+ // The token-earning enumeration otherwise walks the entire
+ // calendar history just to reach the final page. Bound the
+ // past only: a timeMax would be encoded into the token and
+ // silently hide events created beyond it once the maintained
+ // window extends.
+ request = request.query(&[("timeMin", window.starts_at.to_rfc3339())]);
+ }
+ if let Some(token) = &page_token {
+ request = request.query(&[("pageToken", token)]);
+ }
+ let page: GoogleEventListResponse = send_google(request).await?;
+ result.extend(page.items);
+ page_token = page.next_page_token;
+ if page_token.is_none() {
+ return page
+ .next_sync_token
+ .map(|next_sync_token| (result, next_sync_token))
+ .ok_or_else(|| {
+ GoogleProviderError::new(
+ GoogleProviderErrorKind::Transient,
+ "Google Calendar change feed ended without a sync token",
+ )
+ });
+ }
+ }
+ }
+
+ async fn event(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ provider_event_id: &str,
+ ) -> Result<Option<GoogleEvent>, GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ let event = urlencoding::encode(provider_event_id);
+ self.gate.acquire(email_link_id).await?;
+ let response = self
+ .client
+ .get(format!(
+ "{GOOGLE_CALENDAR_API}/calendars/{calendar}/events/{event}"
+ ))
+ .bearer_auth(access_token)
+ .send()
+ .await
+ .map_err(provider_transport_error)?;
+ let status = response.status();
+ if status == StatusCode::NOT_FOUND || status == StatusCode::GONE {
+ return Ok(None);
+ }
+ if !status.is_success() {
+ let body = response.text().await.map_err(provider_transport_error)?;
+ return Err(provider_response_error(status, &body));
+ }
+ response
+ .json()
+ .await
+ .map(Some)
+ .map_err(provider_transport_error)
+ }
+
+ async fn events_by_ical_uid(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ ical_uid: &str,
+ ) -> Result<Vec<GoogleEvent>, GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ let mut page_token: Option<String> = None;
+ let mut result = Vec::new();
+ loop {
+ self.gate.acquire(email_link_id).await?;
+ let mut request = self
+ .client
+ .get(format!("{GOOGLE_CALENDAR_API}/calendars/{calendar}/events"))
+ .bearer_auth(access_token)
+ .query(&[
+ ("maxResults", "2500"),
+ ("singleEvents", "false"),
+ ("showDeleted", "false"),
+ ("iCalUID", ical_uid),
+ ]);
+ if let Some(token) = &page_token {
+ request = request.query(&[("pageToken", token)]);
+ }
+ let page: GoogleEventListResponse = send_google(request).await?;
+ result.extend(page.items);
+ page_token = page.next_page_token;
+ if page_token.is_none() {
+ break;
+ }
+ }
+ Ok(result)
+ }
+
+ async fn instances(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ provider_event_id: &str,
+ range: &OccurrenceRange,
+ ) -> Result<Vec<GoogleEvent>, GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ let event = urlencoding::encode(provider_event_id);
+ let mut page_token: Option<String> = None;
+ let mut result = Vec::new();
+ loop {
+ self.gate.acquire(email_link_id).await?;
+ let mut request = self
+ .client
+ .get(format!(
+ "{GOOGLE_CALENDAR_API}/calendars/{calendar}/events/{event}/instances"
+ ))
+ .bearer_auth(access_token)
+ .query(&[
+ ("maxResults", "2500".to_string()),
+ ("showDeleted", "false".to_string()),
+ ("timeMin", range.starts_at.to_rfc3339()),
+ ("timeMax", range.ends_at.to_rfc3339()),
+ ("timeZone", "UTC".to_string()),
+ ]);
+ if let Some(token) = &page_token {
+ request = request.query(&[("pageToken", token)]);
+ }
+ let page: GoogleEventListResponse = send_google(request).await?;
+ result.extend(page.items);
+ page_token = page.next_page_token;
+ if page_token.is_none() {
+ break;
+ }
+ }
+ Ok(result)
+ }
+}
+
+async fn send_google<T: DeserializeOwned>(
+ request: RequestBuilder,
+) -> Result<T, GoogleProviderError> {
+ let response = request.send().await.map_err(provider_transport_error)?;
+ let status = response.status();
+ if !status.is_success() {
+ let body = response.text().await.map_err(provider_transport_error)?;
+ return Err(provider_response_error(status, &body));
+ }
+ response.json().await.map_err(provider_transport_error)
+}
+
+fn provider_transport_error(error: reqwest::Error) -> GoogleProviderError {
+ GoogleProviderError::new(GoogleProviderErrorKind::Transient, format!("{error:?}"))
+}
+
+fn provider_response_error(status: StatusCode, body: &str) -> GoogleProviderError {
+ let payload = serde_json::from_str::<GoogleErrorResponse>(body).ok();
+ let reasons: Vec<_> = payload
+ .as_ref()
+ .map(|payload| {
+ payload
+ .error
+ .errors
+ .iter()
+ .map(|error| error.reason.as_str())
+ .collect()
+ })
+ .unwrap_or_default();
+ let kind = if status == StatusCode::GONE || reasons.contains(&"fullSyncRequired") {
+ GoogleProviderErrorKind::SyncTokenExpired
+ } else if reasons.contains(&"insufficientPermissions") {
+ GoogleProviderErrorKind::ReauthRequired
+ } else if status == StatusCode::UNAUTHORIZED
+ || status == StatusCode::REQUEST_TIMEOUT
+ || status == StatusCode::TOO_MANY_REQUESTS
+ || status.is_server_error()
+ || reasons.contains(&"authError")
+ || reasons.contains(&"backendError")
+ || reasons.contains(&"dailyLimitExceeded")
+ || reasons.contains(&"quotaExceeded")
+ || reasons.contains(&"rateLimitExceeded")
+ || reasons.contains(&"userRateLimitExceeded")
+ {
+ GoogleProviderErrorKind::Transient
+ } else {
+ GoogleProviderErrorKind::Permanent
+ };
+ let message = payload
+ .map(|payload| payload.error.message)
+ .filter(|message| !message.is_empty())
+ .unwrap_or_else(|| format!("Google Calendar returned HTTP {status}"));
+ GoogleProviderError::new(kind, message)
+}
+
+impl<G: GoogleRequestGate> GoogleCalendarProvider for GoogleCalendarClient<G> {
+ #[tracing::instrument(skip(self, access_token), err)]
+ async fn list_calendars(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ ) -> Result<Vec<ProviderCalendar>, GoogleProviderError> {
+ Ok(self
+ .calendars(access_token, email_link_id)
+ .await?
+ .into_iter()
+ .map(|calendar| ProviderCalendar {
+ provider_calendar_id: calendar.id,
+ name: calendar.summary,
+ description: calendar.description,
+ time_zone: calendar.time_zone,
+ color: calendar.background_color,
+ access_role: calendar.access_role,
+ is_primary: calendar.primary,
+ is_selected: calendar.selected,
+ })
+ .collect())
+ }
+
+ #[tracing::instrument(
+ skip(self, access_token, context),
+ fields(provider_calendar_id = %context.provider_calendar_id),
+ err
+ )]
+ async fn sync_events(
+ &self,
+ access_token: &str,
+ context: GoogleEventSyncContext,
+ ) -> Result<GoogleEventSyncBatch, GoogleProviderError> {
+ let (changes, next_sync_token, token_was_reset) = match self
+ .event_changes(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ context.sync_token.as_deref(),
+ &context.range,
+ )
+ .await
+ {
+ Ok((changes, next_sync_token)) => (changes, next_sync_token, false),
+ Err(error) if error.kind() == GoogleProviderErrorKind::SyncTokenExpired => {
+ let (changes, next_sync_token) = self
+ .event_changes(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ None,
+ &context.range,
+ )
+ .await?;
+ (changes, next_sync_token, true)
+ }
+ Err(error) => return Err(error),
+ };
+ let rebuild_snapshot =
+ needs_full_rebuild(&context.plan, context.sync_token.is_some(), token_was_reset);
+ if rebuild_snapshot {
+ tracing::info!(
+ plan = ?context.plan,
+ had_sync_token = context.sync_token.is_some(),
+ token_was_reset,
+ feed_changes = changes.len(),
+ "rebuilding full Google Calendar snapshot"
+ );
+ let canonical_events = self
+ .events(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ &context.range,
+ false,
+ )
+ .await?;
+ let instances = self
+ .events(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ &context.range,
+ true,
+ )
+ .await?;
+
+ let mapped = map_snapshot(&context, canonical_events, instances);
+
+ return Ok(GoogleEventSyncBatch {
+ upserts: mapped.upserts,
+ observed_provider_event_ids: Some(mapped.observed_provider_event_ids),
+ next_sync_token,
+ materialized_range: Some(context.range.clone()),
+ cancelled_provider_event_ids: Vec::new(),
+ });
+ }
+
+ let mut applied = self
+ .apply_change_feed(access_token, &context, changes)
+ .await?;
+ let materialized_range =
+ if let GoogleSyncPlan::ExtendTail { from, from_date } = context.plan {
+ self.extend_tail(access_token, &context, from, from_date, &mut applied)
+ .await?;
+ Some(context.range.clone())
+ } else {
+ None
+ };
+
+ Ok(GoogleEventSyncBatch {
+ upserts: applied.upserts,
+ observed_provider_event_ids: None,
+ next_sync_token,
+ materialized_range,
+ cancelled_provider_event_ids: applied.cancelled.into_iter().collect(),
+ })
+ }
+
+ #[tracing::instrument(skip(self, access_token, config), err)]
+ async fn watch_calendar(
+ &self,
+ access_token: &str,
+ email_link_id: Uuid,
+ provider_calendar_id: &str,
+ channel_id: Uuid,
+ config: &GoogleWatchConfig,
+ ) -> Result<GoogleWatchChannel, GoogleProviderError> {
+ let calendar = urlencoding::encode(provider_calendar_id);
+ self.gate.acquire(email_link_id).await?;
+ let response: GoogleChannelResponse = send_google(
+ self.client
+ .post(format!(
+ "{GOOGLE_CALENDAR_API}/calendars/{calendar}/events/watch"
+ ))
+ .bearer_auth(access_token)
+ .json(&serde_json::json!({
+ "id": channel_id.to_string(),
+ "type": "web_hook",
+ "address": config.address,
+ "token": config.token,
+ })),
+ )
+ .await?;
+ let expiration_millis: i64 = response.expiration.parse().map_err(|_| {
+ GoogleProviderError::new(
+ GoogleProviderErrorKind::Transient,
+ "Google Calendar watch returned an unparseable expiration",
+ )
+ })?;
+ let expires_at = DateTime::from_timestamp_millis(expiration_millis).ok_or_else(|| {
+ GoogleProviderError::new(
+ GoogleProviderErrorKind::Transient,
+ "Google Calendar watch returned an out-of-range expiration",
+ )
+ })?;
+ Ok(GoogleWatchChannel {
+ channel_id,
+ resource_id: response.resource_id,
+ expires_at,
+ })
+ }
+}
+
+/// Feed application state shared by incremental polls and tail extension.
+#[derive(Default)]
+struct AppliedChangeFeed {
+ upserts: Vec<CalendarEventUpsert>,
+ cancelled: BTreeSet<String>,
+ refreshed_series: BTreeSet<String>,
+ upserted_singles: BTreeSet<String>,
+}
+
+enum SeriesOutcome {
+ Refreshed(Box<CalendarEventUpsert>),
+ Gone,
+ Malformed,
+}
+
+impl<G: GoogleRequestGate> GoogleCalendarClient<G> {
+ async fn apply_change_feed(
+ &self,
+ access_token: &str,
+ context: &GoogleEventSyncContext,
+ changes: Vec<GoogleEvent>,
+ ) -> Result<AppliedChangeFeed, GoogleProviderError> {
+ let classified = classify_changes(changes);
+ let mut applied = AppliedChangeFeed {
+ cancelled: classified.tombstoned_provider_event_ids,
+ ..AppliedChangeFeed::default()
+ };
+
+ for single in classified.single_upserts {
+ let provider_event_id = single.id.clone();
+ match map_upsert(context, single.clone(), Vec::new(), vec![single]) {
+ Ok(upsert) => {
+ applied.upserts.push(upsert);
+ applied.upserted_singles.insert(provider_event_id);
+ }
+ Err(error) => {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed changed Google Calendar event"
+ );
+ }
+ }
+ }
+
+ // One bounded refresh per changed series keeps Google authoritative
+ // for recurrence expansion without re-sweeping the whole window.
+ for (master_id, feed_master) in classified.refresh_masters {
+ match self
+ .refresh_series(access_token, context, &master_id, feed_master)
+ .await?
+ {
+ SeriesOutcome::Refreshed(upsert) => {
+ applied.upserts.push(*upsert);
+ applied.refreshed_series.insert(master_id);
+ }
+ SeriesOutcome::Gone => {
+ applied.cancelled.insert(master_id);
+ }
+ SeriesOutcome::Malformed => {
+ applied.refreshed_series.insert(master_id);
+ }
+ }
+ }
+
+ Ok(applied)
+ }
+
+ async fn refresh_series(
+ &self,
+ access_token: &str,
+ context: &GoogleEventSyncContext,
+ master_id: &str,
+ feed_master: Option<GoogleEvent>,
+ ) -> Result<SeriesOutcome, GoogleProviderError> {
+ let master = match feed_master {
+ Some(master) => Some(master),
+ None => {
+ self.event(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ master_id,
+ )
+ .await?
+ }
+ };
+ let Some(master) = master else {
+ return Ok(SeriesOutcome::Gone);
+ };
+ if master.status.as_deref() == Some("cancelled") {
+ return Ok(SeriesOutcome::Gone);
+ }
+ let exceptions = if master.ical_uid.is_empty() {
+ Vec::new()
+ } else {
+ self.events_by_ical_uid(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ &master.ical_uid,
+ )
+ .await?
+ .into_iter()
+ .filter(|event| event.recurring_event_id.is_some())
+ .collect()
+ };
+ let instances = self
+ .instances(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ &master.id,
+ &context.range,
+ )
+ .await?;
+ let provider_event_id = master.id.clone();
+ match map_upsert(context, master, exceptions, instances) {
+ Ok(upsert) => Ok(SeriesOutcome::Refreshed(Box::new(upsert))),
+ Err(error) => {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed changed Google Calendar series"
+ );
+ Ok(SeriesOutcome::Malformed)
+ }
+ }
+ }
+
+ /// Materialize only the window the stored coverage does not reach yet:
+ /// one bounded expanded sweep of the tail, then a full-window refresh of
+ /// each series that surfaced there and was not already refreshed.
+ async fn extend_tail(
+ &self,
+ access_token: &str,
+ context: &GoogleEventSyncContext,
+ from: DateTime<Utc>,
+ from_date: NaiveDate,
+ applied: &mut AppliedChangeFeed,
+ ) -> Result<(), GoogleProviderError> {
+ let tail = OccurrenceRange {
+ starts_at: from,
+ ends_at: context.range.ends_at,
+ start_date: from_date,
+ end_date: context.range.end_date,
+ };
+ if tail.starts_at >= tail.ends_at {
+ return Ok(());
+ }
+ let tail_events = self
+ .events(
+ access_token,
+ context.email_link_id,
+ &context.provider_calendar_id,
+ &tail,
+ true,
+ )
+ .await?;
+ let (tail_series, tail_singles) = plan_tail_refreshes(tail_events, applied);
+
+ for single in tail_singles {
+ let provider_event_id = single.id.clone();
+ match map_upsert(context, single.clone(), Vec::new(), vec![single]) {
+ Ok(upsert) => {
+ applied.upserts.push(upsert);
+ applied.upserted_singles.insert(provider_event_id);
+ }
+ Err(error) => {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed Google Calendar event in the coverage tail"
+ );
+ }
+ }
+ }
+
+ for master_id in tail_series {
+ match self
+ .refresh_series(access_token, context, &master_id, None)
+ .await?
+ {
+ SeriesOutcome::Refreshed(upsert) => {
+ applied.upserts.push(*upsert);
+ applied.refreshed_series.insert(master_id);
+ }
+ SeriesOutcome::Gone => {
+ applied.cancelled.insert(master_id);
+ }
+ SeriesOutcome::Malformed => {
+ applied.refreshed_series.insert(master_id);
+ }
+ }
+ }
+
+ Ok(())
+ }
+}
+
+/// Whether this run must rebuild the complete bounded snapshot instead of
+/// applying the change feed. ExtendTail deliberately stays out of this set:
+/// it applies the feed and then materializes only the uncovered tail.
+fn needs_full_rebuild(plan: &GoogleSyncPlan, has_sync_token: bool, token_was_reset: bool) -> bool {
+ matches!(plan, GoogleSyncPlan::FullSnapshot) || !has_sync_token || token_was_reset
+}
+
+/// Split a tail sweep into series needing a refresh and standalone events to
+/// upsert, skipping anything the change feed already handled this run.
+fn plan_tail_refreshes(
+ tail_events: Vec<GoogleEvent>,
+ applied: &AppliedChangeFeed,
+) -> (BTreeSet<String>, Vec<GoogleEvent>) {
+ let mut series = BTreeSet::new();
+ let mut singles = Vec::new();
+ let mut seen_singles = BTreeSet::new();
+ for event in tail_events {
+ if let Some(master_id) = &event.recurring_event_id {
+ if !applied.refreshed_series.contains(master_id)
+ && !applied.cancelled.contains(master_id)
+ {
+ series.insert(master_id.clone());
+ }
+ } else if !applied.upserted_singles.contains(&event.id)
+ && !applied.cancelled.contains(&event.id)
+ && seen_singles.insert(event.id.clone())
+ {
+ singles.push(event);
+ }
+ }
+ (series, singles)
+}
+
+#[derive(Default)]
+struct ClassifiedChanges {
+ /// Events the feed reported deleted; a master id also retires its
+ /// expanded instances during the fenced per-calendar commit.
+ tombstoned_provider_event_ids: BTreeSet<String>,
+ /// Recurring series needing a bounded refresh, keyed by master id and
+ /// carrying the master when the feed already delivered it.
+ refresh_masters: BTreeMap<String, Option<GoogleEvent>>,
+ /// Changed standalone events whose feed payload is the whole update.
+ single_upserts: Vec<GoogleEvent>,
+}
+
+fn classify_changes(changes: Vec<GoogleEvent>) -> ClassifiedChanges {
+ let mut classified = ClassifiedChanges::default();
+ for change in changes {
+ let is_cancelled = change.status.as_deref() == Some("cancelled");
+ match (&change.recurring_event_id, is_cancelled) {
+ (Some(master_id), _) => {
+ // Created, modified, or cancelled exceptions all resolve by
+ // refreshing their series from the provider.
+ classified
+ .refresh_masters
+ .entry(master_id.clone())
+ .or_insert(None);
+ }
+ (None, true) => {
+ classified.tombstoned_provider_event_ids.insert(change.id);
+ }
+ (None, false) if !change.recurrence.is_empty() => {
+ classified
+ .refresh_masters
+ .insert(change.id.clone(), Some(change));
+ }
+ (None, false) => classified.single_upserts.push(change),
+ }
+ }
+ classified
+}
+
+struct MappedGoogleSnapshot {
+ upserts: Vec<CalendarEventUpsert>,
+ observed_provider_event_ids: Vec<String>,
+}
+
+fn map_snapshot(
+ context: &GoogleEventSyncContext,
+ canonical_events: Vec<GoogleEvent>,
+ instances: Vec<GoogleEvent>,
+) -> MappedGoogleSnapshot {
+ let mut occurrences: BTreeMap<String, Vec<GoogleEvent>> = BTreeMap::new();
+ for instance in instances {
+ occurrences
+ .entry(instance.ical_uid.clone())
+ .or_default()
+ .push(instance);
+ }
+
+ let mut exceptions: BTreeMap<String, Vec<GoogleEvent>> = BTreeMap::new();
+ let mut masters = BTreeMap::new();
+ for event in canonical_events {
+ if event.recurring_event_id.is_some() {
+ exceptions
+ .entry(event.ical_uid.clone())
+ .or_default()
+ .push(event);
+ } else {
+ masters.insert(event.ical_uid.clone(), event);
+ }
+ }
+
+ // Google can omit a master outside the requested window while still
+ // returning one expanded instance. Preserve identity by using that
+ // instance as a read-only canonical fallback.
+ for (uid, uid_instances) in &occurrences {
+ if !masters.contains_key(uid)
+ && let Some(instance) = uid_instances.first()
+ {
+ masters.insert(uid.clone(), instance.clone());
+ }
+ }
+
+ let observed_provider_event_ids = masters
+ .values()
+ .map(|master| master.id.clone())
+ .collect::<Vec<_>>();
+ let upserts = masters
+ .into_iter()
+ .filter_map(|(uid, master)| {
+ let provider_event_id = master.id.clone();
+ map_upsert(
+ context,
+ master,
+ exceptions.remove(&uid).unwrap_or_default(),
+ occurrences.remove(&uid).unwrap_or_default(),
+ )
+ .inspect_err(|error| {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed Google Calendar master"
+ );
+ })
+ .ok()
+ })
+ .collect();
+ MappedGoogleSnapshot {
+ upserts,
+ observed_provider_event_ids,
+ }
+}
+
+fn map_upsert(
+ context: &GoogleEventSyncContext,
+ master: GoogleEvent,
+ exceptions: Vec<GoogleEvent>,
+ instances: Vec<GoogleEvent>,
+) -> Result<CalendarEventUpsert, Report> {
+ let event_id = Uuid::now_v7();
+ let time = google_time(&master)?;
+ let created_at = parse_datetime(master.created.as_deref()).unwrap_or_else(Utc::now);
+ let updated_at = parse_datetime(master.updated.as_deref()).unwrap_or(created_at);
+ let source = CalendarEventSource::Google(GoogleEventSource {
+ email_link_id: context.email_link_id,
+ account_id: context.account_id,
+ calendar_id: context.calendar_id,
+ provider_event_id: master.id.clone(),
+ provider_recurring_event_id: master.recurring_event_id.clone(),
+ provider_etag: master.etag.clone(),
+ raw_payload: serde_json::to_value(&master).map_err(report)?,
+ });
+ let event = CalendarEvent {
+ id: event_id,
+ owner_id: context.owner_id.clone(),
+ ical_uid: master.ical_uid.clone(),
+ title: master.summary.clone().unwrap_or_default(),
+ description: master.description.clone(),
+ location: master.location.clone(),
+ status: google_status(master.status.as_deref()),
+ visibility: google_visibility(master.visibility.as_deref()),
+ transparency: google_transparency(master.transparency.as_deref()),
+ time,
+ recurrence_lines: master.recurrence.clone(),
+ organizer_email: master
+ .organizer
+ .as_ref()
+ .and_then(|value| value.email.clone()),
+ organizer_name: master
+ .organizer
+ .as_ref()
+ .and_then(|value| value.display_name.clone()),
+ conference_url: master
+ .hangout_link
+ .clone()
+ .or_else(|| conference_url(master.conference_data.as_ref())),
+ sequence: master.sequence.unwrap_or_default(),
+ is_read_only: context.is_read_only,
+ attendees: master
+ .attendees
+ .clone()
+ .unwrap_or_default()
+ .into_iter()
+ .filter_map(map_attendee)
+ .collect(),
+ created_at,
+ updated_at,
+ };
+
+ let overrides = exceptions
+ .into_iter()
+ .filter_map(|exception| {
+ // Cancelled exceptions arrive without times; the occurrence
+ // replace already removes them, so there is no override to keep.
+ if exception.status.as_deref() == Some("cancelled") {
+ return None;
+ }
+ let provider_event_id = exception.id.clone();
+ (|| -> Result<CalendarEventOverride, Report> {
+ let original = exception
+ .original_start_time
+ .as_ref()
+ .and_then(google_start)
+ .ok_or_else(|| {
+ rootcause::report!(
+ "Google recurring exception {} has an invalid original start",
+ exception.id
+ )
+ })?;
+ let time = google_time(&exception)?;
+ Ok(CalendarEventOverride {
+ recurrence_id: original.occurrence_key(),
+ original_time: original,
+ time,
+ title: exception.summary,
+ description: exception.description,
+ location: exception.location,
+ status: Some(google_status(exception.status.as_deref())),
+ })
+ })()
+ .inspect_err(|error| {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed Google Calendar recurrence exception"
+ );
+ })
+ .ok()
+ })
+ .collect();
+
+ let occurrences = instances
+ .into_iter()
+ .filter_map(|instance| {
+ let provider_event_id = instance.id.clone();
+ (|| -> Result<Option<CalendarOccurrence>, Report> {
+ let time = google_time(&instance)?;
+ let original_start = instance
+ .original_start_time
+ .as_ref()
+ .map(|value| {
+ google_start(value).ok_or_else(|| {
+ rootcause::report!(
+ "Google recurring instance {} has an invalid original start",
+ instance.id
+ )
+ })
+ })
+ .transpose()?;
+ Ok(time.overlaps(&context.range).then(|| CalendarOccurrence {
+ event_id,
+ occurrence_key: original_start
+ .as_ref()
+ .map(|start| start.occurrence_key())
+ .unwrap_or_else(|| time.occurrence_key()),
+ recurrence_id: original_start.map(|start| start.occurrence_key()),
+ time,
+ is_cancelled: instance.status.as_deref() == Some("cancelled"),
+ }))
+ })()
+ .inspect_err(|error| {
+ tracing::warn!(
+ error=?error,
+ provider_calendar_id=%context.provider_calendar_id,
+ provider_event_id,
+ "skipping malformed Google Calendar recurrence instance"
+ );
+ })
+ .ok()
+ .flatten()
+ })
+ .collect();
+
+ Ok(CalendarEventUpsert {
+ event,
+ source,
+ overrides,
+ occurrences,
+ })
+}
+
+fn google_time(event: &GoogleEvent) -> Result<EventTime, Report> {
+ let start = event
+ .start
+ .as_ref()
+ .ok_or_else(|| rootcause::report!("Google event {} has no start", event.id))?;
+ let end = event
+ .end
+ .as_ref()
+ .ok_or_else(|| rootcause::report!("Google event {} has no end", event.id))?;
+ match (
+ start.date_time.as_deref(),
+ end.date_time.as_deref(),
+ start.date.as_deref(),
+ end.date.as_deref(),
+ ) {
+ (Some(start_value), Some(end_value), _, _) => {
+ let starts_at = DateTime::parse_from_rfc3339(start_value)
+ .map_err(report)?
+ .with_timezone(&Utc);
+ let ends_at = DateTime::parse_from_rfc3339(end_value)
+ .map_err(report)?
+ .with_timezone(&Utc);
+ Ok(EventTime::Timed {
+ starts_at,
+ ends_at,
+ time_zone: start.time_zone.clone(),
+ })
+ }
+ (_, _, Some(start_value), Some(end_value)) => Ok(EventTime::AllDay {
+ start_date: NaiveDate::parse_from_str(start_value, "%Y-%m-%d").map_err(report)?,
+ end_date: NaiveDate::parse_from_str(end_value, "%Y-%m-%d").map_err(report)?,
+ }),
+ _ => Err(rootcause::report!(
+ "Google event {} has mixed or missing time fields",
+ event.id
+ )),
+ }
+}
+
+fn google_start(value: &GoogleEventDateTime) -> Option<EventStart> {
+ if let Some(date_time) = &value.date_time {
+ DateTime::parse_from_rfc3339(date_time)
+ .ok()
+ .map(|value| EventStart::Timed(value.with_timezone(&Utc)))
+ } else {
+ value.date.as_deref().and_then(|date| {
+ NaiveDate::parse_from_str(date, "%Y-%m-%d")
+ .ok()
+ .map(EventStart::AllDay)
+ })
+ }
+}
+
+fn map_attendee(value: GoogleAttendee) -> Option<CalendarAttendee> {
+ let email = value.email?.to_ascii_lowercase();
+ Some(CalendarAttendee {
+ email,
+ display_name: value.display_name,
+ response_status: match value.response_status.as_deref() {
+ Some("accepted") => AttendeeResponseStatus::Accepted,
+ Some("declined") => AttendeeResponseStatus::Declined,
+ Some("tentative") => AttendeeResponseStatus::Tentative,
+ _ => AttendeeResponseStatus::NeedsAction,
+ },
+ is_organizer: value.organizer,
+ is_optional: value.optional,
+ is_self: value.is_self,
+ comment: value.comment,
+ })
+}
+
+fn conference_url(data: Option<&GoogleConferenceData>) -> Option<String> {
+ data.and_then(|data| {
+ data.entry_points
+ .iter()
+ .find(|entry| entry.entry_point_type.as_deref() == Some("video"))
+ .and_then(|entry| entry.uri.clone())
+ })
+}
+
+fn google_status(value: Option<&str>) -> EventStatus {
+ match value {
+ Some("tentative") => EventStatus::Tentative,
+ Some("cancelled") => EventStatus::Cancelled,
+ _ => EventStatus::Confirmed,
+ }
+}
+
+fn google_visibility(value: Option<&str>) -> EventVisibility {
+ match value {
+ Some("public") => EventVisibility::Public,
+ Some("private") => EventVisibility::Private,
+ Some("confidential") => EventVisibility::Confidential,
+ _ => EventVisibility::Default,
+ }
+}
+
+fn google_transparency(value: Option<&str>) -> EventTransparency {
+ if value == Some("transparent") {
+ EventTransparency::Transparent
+ } else {
+ EventTransparency::Opaque
+ }
+}
+
+fn parse_datetime(value: Option<&str>) -> Option<DateTime<Utc>> {
+ value
+ .and_then(|value| DateTime::parse_from_rfc3339(value).ok())
+ .map(|value| value.with_timezone(&Utc))
+}
+
+fn report(error: impl std::error::Error + Send + Sync + 'static) -> Report {
+ rootcause::report!(error).into()
+}
+
+#[derive(Clone, Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleChannelResponse {
+ resource_id: String,
+ expiration: String,
+}
+
+#[derive(Clone, Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleCalendarListResponse {
+ #[serde(default)]
+ items: Vec<GoogleCalendar>,
+ next_page_token: Option<String>,
+}
+
+#[derive(Debug, Deserialize)]
+struct GoogleErrorResponse {
+ error: GoogleErrorBody,
+}
+
+#[derive(Debug, Deserialize)]
+struct GoogleErrorBody {
+ message: String,
+ #[serde(default)]
+ errors: Vec<GoogleErrorDetail>,
+}
+
+#[derive(Debug, Deserialize)]
+struct GoogleErrorDetail {
+ reason: String,
+}
+
+#[derive(Clone, Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleCalendar {
+ id: String,
+ summary: String,
+ description: Option<String>,
+ time_zone: Option<String>,
+ background_color: Option<String>,
+ access_role: Option<String>,
+ #[serde(default)]
+ primary: bool,
+ #[serde(default)]
+ selected: bool,
+}
+
+#[derive(Clone, Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleEventListResponse {
+ #[serde(default)]
+ items: Vec<GoogleEvent>,
+ next_page_token: Option<String>,
+ next_sync_token: Option<String>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleEvent {
+ id: String,
+ #[serde(rename = "iCalUID")]
+ #[serde(default)]
+ ical_uid: String,
+ etag: Option<String>,
+ status: Option<String>,
+ summary: Option<String>,
+ description: Option<String>,
+ location: Option<String>,
+ visibility: Option<String>,
+ transparency: Option<String>,
+ start: Option<GoogleEventDateTime>,
+ end: Option<GoogleEventDateTime>,
+ #[serde(default)]
+ recurrence: Vec<String>,
+ recurring_event_id: Option<String>,
+ original_start_time: Option<GoogleEventDateTime>,
+ organizer: Option<GooglePerson>,
+ #[serde(default)]
+ attendees: Option<Vec<GoogleAttendee>>,
+ hangout_link: Option<String>,
+ conference_data: Option<GoogleConferenceData>,
+ sequence: Option<u32>,
+ created: Option<String>,
+ updated: Option<String>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleEventDateTime {
+ date: Option<String>,
+ date_time: Option<String>,
+ time_zone: Option<String>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GooglePerson {
+ email: Option<String>,
+ display_name: Option<String>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleAttendee {
+ email: Option<String>,
+ display_name: Option<String>,
+ response_status: Option<String>,
+ #[serde(default)]
+ organizer: bool,
+ #[serde(default)]
+ optional: bool,
+ #[serde(default, rename = "self")]
+ is_self: bool,
+ comment: Option<String>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleConferenceData {
+ #[serde(default)]
+ entry_points: Vec<GoogleEntryPoint>,
+}
+
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+struct GoogleEntryPoint {
+ entry_point_type: Option<String>,
+ uri: Option<String>,
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/calendar_events/src/outbound/google/test.rs b/crates/calendar_events/src/outbound/google/test.rs
new file mode 100644
index 0000000000..2df11ddfec
--- /dev/null
+++ b/crates/calendar_events/src/outbound/google/test.rs
@@ -0,0 +1,383 @@
+use super::*;
+
+#[test]
+fn calendar_access_role_is_reflected_on_mapped_events() {
+ let master: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "provider-event",
+ "iCalUID": "[email protected]",
+ "summary": "Read-only calendar",
+ "start": {"dateTime": "2026-07-24T14:00:00Z", "timeZone": "UTC"},
+ "end": {"dateTime": "2026-07-24T15:00:00Z", "timeZone": "UTC"},
+ "created": "2026-07-20T14:00:00Z",
+ "updated": "2026-07-21T14:00:00Z"
+ }))
+ .unwrap();
+ let range = OccurrenceRange {
+ starts_at: DateTime::parse_from_rfc3339("2026-07-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc),
+ ends_at: DateTime::parse_from_rfc3339("2026-08-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc),
+ start_date: NaiveDate::from_ymd_opt(2026, 7, 1).unwrap(),
+ end_date: NaiveDate::from_ymd_opt(2026, 8, 1).unwrap(),
+ };
+
+ let context = GoogleEventSyncContext {
+ owner_id: "macro|[email protected]".to_string(),
+ email_link_id: Uuid::now_v7(),
+ account_id: Uuid::now_v7(),
+ calendar_id: Uuid::now_v7(),
+ provider_calendar_id: "primary".to_string(),
+ is_read_only: true,
+ range,
+ sync_token: None,
+ plan: GoogleSyncPlan::FullSnapshot,
+ };
+ let upsert = map_upsert(&context, master, Vec::new(), Vec::new()).unwrap();
+
+ assert!(upsert.event.is_read_only);
+}
+
+#[test]
+fn malformed_recurring_instance_does_not_overstate_snapshot_coverage() {
+ let master: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "provider-master",
+ "iCalUID": "[email protected]",
+ "summary": "Recurring calendar event",
+ "start": {"dateTime": "2026-07-24T14:00:00Z", "timeZone": "UTC"},
+ "end": {"dateTime": "2026-07-24T15:00:00Z", "timeZone": "UTC"},
+ "recurrence": ["RRULE:FREQ=DAILY"],
+ "created": "2026-07-20T14:00:00Z",
+ "updated": "2026-07-21T14:00:00Z"
+ }))
+ .unwrap();
+ let malformed_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "provider-instance",
+ "iCalUID": "[email protected]",
+ "recurringEventId": "provider-master",
+ "originalStartTime": {"dateTime": "2026-07-24T14:00:00Z"},
+ "start": {"dateTime": "2026-07-24T14:00:00Z"}
+ }))
+ .unwrap();
+ let range = OccurrenceRange {
+ starts_at: DateTime::parse_from_rfc3339("2026-07-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc),
+ ends_at: DateTime::parse_from_rfc3339("2026-08-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc),
+ start_date: NaiveDate::from_ymd_opt(2026, 7, 1).unwrap(),
+ end_date: NaiveDate::from_ymd_opt(2026, 8, 1).unwrap(),
+ };
+ let context = GoogleEventSyncContext {
+ owner_id: "macro|[email protected]".to_string(),
+ email_link_id: Uuid::now_v7(),
+ account_id: Uuid::now_v7(),
+ calendar_id: Uuid::now_v7(),
+ provider_calendar_id: "primary".to_string(),
+ is_read_only: false,
+ range,
+ sync_token: None,
+ plan: GoogleSyncPlan::FullSnapshot,
+ };
+
+ let upsert = map_upsert(&context, master, Vec::new(), vec![malformed_instance]).unwrap();
+ assert!(upsert.occurrences.is_empty());
+}
+
+#[test]
+fn malformed_master_is_quarantined_without_deleting_its_provider_identity() {
+ let valid: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "valid-provider-event",
+ "iCalUID": "[email protected]",
+ "start": {"dateTime": "2026-07-24T14:00:00Z"},
+ "end": {"dateTime": "2026-07-24T15:00:00Z"}
+ }))
+ .unwrap();
+ let malformed: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "malformed-provider-event",
+ "iCalUID": "[email protected]",
+ "start": {"dateTime": "2026-07-24T14:00:00Z"}
+ }))
+ .unwrap();
+ let starts_at = DateTime::parse_from_rfc3339("2026-07-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc);
+ let ends_at = DateTime::parse_from_rfc3339("2026-08-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc);
+ let context = GoogleEventSyncContext {
+ owner_id: "macro|[email protected]".to_string(),
+ email_link_id: Uuid::now_v7(),
+ account_id: Uuid::now_v7(),
+ calendar_id: Uuid::now_v7(),
+ provider_calendar_id: "primary".to_string(),
+ is_read_only: false,
+ range: OccurrenceRange {
+ starts_at,
+ ends_at,
+ start_date: starts_at.date_naive(),
+ end_date: ends_at.date_naive(),
+ },
+ sync_token: Some("token".to_string()),
+ plan: GoogleSyncPlan::FullSnapshot,
+ };
+
+ let mapped = map_snapshot(&context, vec![valid, malformed], Vec::new());
+
+ assert_eq!(mapped.upserts.len(), 1);
+ assert_eq!(
+ mapped.observed_provider_event_ids,
+ vec![
+ "malformed-provider-event".to_string(),
+ "valid-provider-event".to_string()
+ ]
+ );
+}
+
+#[test]
+fn quota_forbidden_response_is_retryable() {
+ let error = provider_response_error(
+ StatusCode::FORBIDDEN,
+ r#"{"error":{"message":"Quota exceeded","errors":[{"reason":"userRateLimitExceeded"}]}}"#,
+ );
+
+ assert_eq!(error.kind(), GoogleProviderErrorKind::Transient);
+}
+
+#[test]
+fn insufficient_permissions_require_reauthorization() {
+ let error = provider_response_error(
+ StatusCode::FORBIDDEN,
+ r#"{"error":{"message":"Insufficient Permission","errors":[{"reason":"insufficientPermissions"}]}}"#,
+ );
+
+ assert_eq!(error.kind(), GoogleProviderErrorKind::ReauthRequired);
+}
+
+#[test]
+fn expired_sync_token_requests_a_full_resync() {
+ let error = provider_response_error(
+ StatusCode::GONE,
+ r#"{"error":{"message":"Sync token is no longer valid","errors":[{"reason":"fullSyncRequired"}]}}"#,
+ );
+
+ assert_eq!(error.kind(), GoogleProviderErrorKind::SyncTokenExpired);
+}
+
+#[test]
+fn rejected_access_token_is_retryable_with_a_fresh_token() {
+ let error = provider_response_error(
+ StatusCode::UNAUTHORIZED,
+ r#"{"error":{"message":"Invalid Credentials","errors":[{"reason":"authError"}]}}"#,
+ );
+
+ assert_eq!(error.kind(), GoogleProviderErrorKind::Transient);
+}
+
+#[test]
+fn unrelated_forbidden_response_is_permanent() {
+ let error = provider_response_error(
+ StatusCode::FORBIDDEN,
+ r#"{"error":{"message":"Forbidden","errors":[{"reason":"forbidden"}]}}"#,
+ );
+
+ assert_eq!(error.kind(), GoogleProviderErrorKind::Permanent);
+}
+
+#[test]
+fn cancelled_single_events_become_tombstones() {
+ let cancelled: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "gone-event",
+ "status": "cancelled"
+ }))
+ .unwrap();
+
+ let classified = classify_changes(vec![cancelled]);
+
+ assert!(
+ classified
+ .tombstoned_provider_event_ids
+ .contains("gone-event")
+ );
+ assert!(classified.refresh_masters.is_empty());
+ assert!(classified.single_upserts.is_empty());
+}
+
+#[test]
+fn exception_changes_refresh_their_series() {
+ let cancelled_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "master_20260724T140000Z",
+ "status": "cancelled",
+ "recurringEventId": "master",
+ "originalStartTime": {"dateTime": "2026-07-24T14:00:00Z"}
+ }))
+ .unwrap();
+ let modified_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "other_20260725T140000Z",
+ "iCalUID": "[email protected]",
+ "recurringEventId": "other",
+ "originalStartTime": {"dateTime": "2026-07-25T14:00:00Z"},
+ "start": {"dateTime": "2026-07-25T15:00:00Z"},
+ "end": {"dateTime": "2026-07-25T16:00:00Z"}
+ }))
+ .unwrap();
+
+ let classified = classify_changes(vec![cancelled_instance, modified_instance]);
+
+ assert!(classified.tombstoned_provider_event_ids.is_empty());
+ assert_eq!(classified.refresh_masters.len(), 2);
+ assert!(matches!(
+ classified.refresh_masters.get("master"),
+ Some(None)
+ ));
+ assert!(matches!(
+ classified.refresh_masters.get("other"),
+ Some(None)
+ ));
+}
+
+#[test]
+fn feed_master_payload_survives_exception_placeholders_in_any_order() {
+ let master = serde_json::json!({
+ "id": "master",
+ "iCalUID": "[email protected]",
+ "recurrence": ["RRULE:FREQ=DAILY"],
+ "start": {"dateTime": "2026-07-24T14:00:00Z"},
+ "end": {"dateTime": "2026-07-24T15:00:00Z"}
+ });
+ let exception = serde_json::json!({
+ "id": "master_20260724T140000Z",
+ "status": "cancelled",
+ "recurringEventId": "master"
+ });
+
+ for changes in [
+ vec![master.clone(), exception.clone()],
+ vec![exception, master],
+ ] {
+ let changes: Vec<GoogleEvent> = changes
+ .into_iter()
+ .map(|value| serde_json::from_value(value).unwrap())
+ .collect();
+ let classified = classify_changes(changes);
+ assert!(matches!(
+ classified.refresh_masters.get("master"),
+ Some(Some(payload)) if payload.ical_uid == "[email protected]"
+ ));
+ }
+}
+
+#[test]
+fn plain_changed_events_map_directly_from_the_feed() {
+ let singleton: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "plain-event",
+ "iCalUID": "[email protected]",
+ "summary": "Moved meeting",
+ "start": {"dateTime": "2026-07-24T14:00:00Z"},
+ "end": {"dateTime": "2026-07-24T15:00:00Z"}
+ }))
+ .unwrap();
+
+ let classified = classify_changes(vec![singleton]);
+
+ assert!(classified.tombstoned_provider_event_ids.is_empty());
+ assert!(classified.refresh_masters.is_empty());
+ assert_eq!(classified.single_upserts.len(), 1);
+ assert_eq!(classified.single_upserts[0].id, "plain-event");
+}
+
+#[test]
+fn tail_planning_skips_series_and_singles_the_feed_already_handled() {
+ let refreshed_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "refreshed_20280801T140000Z",
+ "recurringEventId": "refreshed",
+ "start": {"dateTime": "2028-08-01T14:00:00Z"},
+ "end": {"dateTime": "2028-08-01T15:00:00Z"}
+ }))
+ .unwrap();
+ let new_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "fresh_20280801T090000Z",
+ "recurringEventId": "fresh",
+ "start": {"dateTime": "2028-08-01T09:00:00Z"},
+ "end": {"dateTime": "2028-08-01T10:00:00Z"}
+ }))
+ .unwrap();
+ let tombstoned_instance: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "gone_20280801T090000Z",
+ "recurringEventId": "gone",
+ "start": {"dateTime": "2028-08-01T09:00:00Z"},
+ "end": {"dateTime": "2028-08-01T10:00:00Z"}
+ }))
+ .unwrap();
+ let known_single: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "known-single",
+ "start": {"dateTime": "2028-08-02T09:00:00Z"},
+ "end": {"dateTime": "2028-08-02T10:00:00Z"}
+ }))
+ .unwrap();
+ let new_single: GoogleEvent = serde_json::from_value(serde_json::json!({
+ "id": "new-single",
+ "start": {"dateTime": "2028-08-03T09:00:00Z"},
+ "end": {"dateTime": "2028-08-03T10:00:00Z"}
+ }))
+ .unwrap();
+
+ let mut applied = AppliedChangeFeed::default();
+ applied.refreshed_series.insert("refreshed".to_string());
+ applied.cancelled.insert("gone".to_string());
+ applied.upserted_singles.insert("known-single".to_string());
+
+ let (series, singles) = plan_tail_refreshes(
+ vec![
+ refreshed_instance,
+ new_instance.clone(),
+ new_instance,
+ tombstoned_instance,
+ known_single,
+ new_single,
+ ],
+ &applied,
+ );
+
+ assert_eq!(series.into_iter().collect::<Vec<_>>(), vec!["fresh"]);
+ assert_eq!(
+ singles
+ .iter()
+ .map(|event| event.id.as_str())
+ .collect::<Vec<_>>(),
+ vec!["new-single"]
+ );
+}
+
+#[test]
+fn only_the_snapshot_plan_or_a_reset_token_forces_a_full_rebuild() {
+ let tail = GoogleSyncPlan::ExtendTail {
+ from: DateTime::parse_from_rfc3339("2028-08-01T00:00:00Z")
+ .unwrap()
+ .with_timezone(&Utc),
+ from_date: NaiveDate::from_ymd_opt(2028, 8, 1).unwrap(),
+ };
+
+ assert!(needs_full_rebuild(
+ &GoogleSyncPlan::FullSnapshot,
+ true,
+ false
+ ));
+ assert!(needs_full_rebuild(
+ &GoogleSyncPlan::Incremental,
+ false,
+ false
+ ));
+ assert!(needs_full_rebuild(&GoogleSyncPlan::Incremental, true, true));
+ assert!(!needs_full_rebuild(
+ &GoogleSyncPlan::Incremental,
+ true,
+ false
+ ));
+ assert!(
+ !needs_full_rebuild(&tail, true, false),
+ "ExtendTail must reach the tail path, not the full rebuild"
+ );
+}
diff --git a/crates/calendar_events/src/outbound/mod.rs b/crates/calendar_events/src/outbound/mod.rs
index a0424b0e7e..e622cf04cd 100644
--- a/crates/calendar_events/src/outbound/mod.rs
+++ b/crates/calendar_events/src/outbound/mod.rs
@@ -1,5 +1,8 @@
//! Outbound calendar adapters.
+/// Google Calendar API adapter.
+#[cfg(feature = "google")]
+pub mod google;
/// PostgreSQL calendar repository.
#[cfg(feature = "postgres")]
pub mod pg;
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index 61c5305039..d1e7fc761f 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -15,8 +15,8 @@ use crate::domain::{
CalendarBackfillJobKey, CalendarBackfillKind, CalendarEvent, CalendarEventOverride,
CalendarEventSource, CalendarEventUpsert, CalendarOccurrence, CalendarOccurrenceCursor,
CalendarSyncStatus, EventStart, EventStatus, EventTime, EventTransparency, EventVisibility,
- GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleScopeSet, OccurrenceRange,
- ProviderCalendar, StoredGoogleCalendar,
+ GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleScopeSet, GoogleWatchChannel,
+ OccurrenceRange, ProviderCalendar, StoredGoogleCalendar,
},
ports::{
CalendarBackfillRepository, CalendarEventWrite, CalendarRepository,
@@ -258,6 +258,8 @@ struct StoredCalendarRow {
materialized_ends_at: Option<DateTime<Utc>>,
materialized_start_date: Option<NaiveDate>,
materialized_end_date: Option<NaiveDate>,
+ synced_at: Option<DateTime<Utc>>,
+ watch_expires_at: Option<DateTime<Utc>>,
}
struct OccurrenceJoinRow {
@@ -500,6 +502,38 @@ impl CalendarRepository for PgCalendarRepository {
.fetch_one(&mut *tx)
.await
.map_err(report)?;
+ // Full snapshots re-upsert every event they observed, and a provider
+ // sync-token reset makes that happen wholesale. When the incoming
+ // Google projection is identical to the stored one, skip the write
+ // path entirely so rebuilds cost reads, not occurrence churn.
+ if let CalendarEventSource::Google(source) = &upsert.source {
+ let existing = sqlx::query!(
+ r#"
+ SELECT event_id, normalized_payload
+ FROM calendar_event_sources
+ WHERE source_kind = 'google'
+ AND account_id = $1
+ AND calendar_id = $2
+ AND provider_event_id = $3
+ "#,
+ source.account_id,
+ source.calendar_id,
+ &source.provider_event_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(report)?;
+ if let Some(row) = existing {
+ let incoming =
+ serde_json::to_value(StoredSourceProjection::from(&upsert)).map_err(report)?;
+ if canonical_projection(&row.normalized_payload) == canonical_projection(&incoming)
+ {
+ tx.commit().await.map_err(report)?;
+ return Ok(row.event_id);
+ }
+ }
+ }
+
let (starts_at, ends_at, start_date, end_date, time_zone) = split_time(&upsert.event.time);
let proposed_id = upsert.event.id;
@@ -800,10 +834,33 @@ impl CalendarRepository for PgCalendarRepository {
lease_token: Uuid,
account_id: Uuid,
sync: GoogleCalendarSyncSnapshot,
+ events_upserted: usize,
) -> 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?;
+ if events_upserted > 0 {
+ sqlx::query!(
+ r#"
+ UPDATE calendar_backfill_jobs
+ SET extracted_count = extracted_count + $3,
+ updated_at = now()
+ WHERE id = $1
+ AND email_link_id = $2
+ "#,
+ key.job_id,
+ key.email_link_id,
+ i64::try_from(events_upserted).map_err(|_| {
+ rootcause::report!(
+ "calendar backfill extracted count overflows the database representation"
+ )
+ })?,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(report)?;
+ }
+
if !sync.cancelled_provider_event_ids.is_empty() {
// A cancelled recurring master retires its expanded instances via
// provider_recurring_event_id, matching Google's tombstone shape.
@@ -876,6 +933,7 @@ impl CalendarRepository for PgCalendarRepository {
r#"
UPDATE calendars
SET sync_token = $3,
+ synced_at = now(),
materialized_starts_at = CASE
WHEN $4 THEN $5
ELSE materialized_starts_at
@@ -918,6 +976,108 @@ impl CalendarRepository for PgCalendarRepository {
tx.commit().await.map_err(report)
}
+ #[tracing::instrument(skip(self, channel), fields(job_id = %key.job_id), err)]
+ async fn record_watch_channel(
+ &self,
+ key: CalendarBackfillJobKey,
+ lease_token: Uuid,
+ account_id: Uuid,
+ calendar_id: Uuid,
+ channel: GoogleWatchChannel,
+ ) -> 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!(
+ r#"
+ UPDATE calendars
+ SET watch_channel_id = $3,
+ watch_resource_id = $4,
+ watch_expires_at = $5,
+ updated_at = now()
+ WHERE id = $1
+ AND account_id = $2
+ AND NOT is_deleted
+ "#,
+ calendar_id,
+ account_id,
+ channel.channel_id.to_string(),
+ channel.resource_id,
+ channel.expires_at,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(report)?;
+ tx.commit().await.map_err(report)
+ }
+
+ #[tracing::instrument(skip(self, channel_id, resource_id), err)]
+ async fn find_watch_target(
+ &self,
+ channel_id: &str,
+ resource_id: &str,
+ ) -> Result<Option<Uuid>, Report> {
+ sqlx::query_scalar!(
+ r#"
+ SELECT account.email_link_id
+ FROM calendars calendar
+ JOIN calendar_accounts account ON account.id = calendar.account_id
+ WHERE calendar.watch_channel_id = $1
+ AND calendar.watch_resource_id = $2
+ AND NOT calendar.is_deleted
+ AND account.sync_status <> 'disabled'
+ "#,
+ channel_id,
+ resource_id,
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(report)
+ }
+
+ #[tracing::instrument(skip(self), err)]
+ async fn schedule_google_sync_for_link(&self, email_link_id: Uuid) -> Result<bool, Report> {
+ let required_scopes = GOOGLE_CALENDAR_SCOPES.map(str::to_owned);
+ let scheduled = sqlx::query_scalar!(
+ r#"
+ WITH due AS (
+ UPDATE calendar_backfill_jobs job
+ SET status = 'pending',
+ cursor = '{}',
+ last_error = NULL,
+ started_at = NULL,
+ completed_at = NULL,
+ lease_token = NULL,
+ lease_expires_at = NULL,
+ updated_at = now()
+ FROM calendar_accounts account, email_links link
+ WHERE job.email_link_id = $1
+ AND job.email_link_id = account.email_link_id
+ AND job.account_id = account.id
+ AND link.id = job.email_link_id
+ AND job.kind = 'google_calendar'
+ AND job.status = 'complete'
+ AND job.grant_version = link.google_grant_version
+ AND link.google_granted_scopes @> $2::text[]
+ AND account.sync_status = 'ready'
+ RETURNING job.id
+ ),
+ republished AS (
+ UPDATE calendar_sync_outbox outbox
+ SET published_at = NULL
+ FROM due
+ WHERE outbox.backfill_job_id = due.id
+ )
+ SELECT count(*) > 0 AS "scheduled!" FROM due
+ "#,
+ email_link_id,
+ &required_scopes,
+ )
+ .fetch_one(&self.pool)
+ .await
+ .map_err(report)?;
+ Ok(scheduled)
+ }
+
#[tracing::instrument(skip(self, calendar_ids), fields(job_id = %key.job_id), err)]
async fn reconcile_google_calendar_list(
&self,
@@ -1121,7 +1281,9 @@ async fn upsert_calendar_tx(
materialized_starts_at,
materialized_ends_at,
materialized_start_date,
- materialized_end_date
+ materialized_end_date,
+ synced_at,
+ watch_expires_at
"#,
Uuid::now_v7(),
account_id,
@@ -1165,6 +1327,8 @@ fn stored_google_calendar(row: StoredCalendarRow) -> Result<StoredGoogleCalendar
id: row.id,
sync_token: row.sync_token,
materialized_range,
+ synced_at: row.synced_at,
+ watch_expires_at: row.watch_expires_at,
})
}
@@ -1350,7 +1514,6 @@ impl CalendarBackfillRepository for PgCalendarRepository {
&self,
key: CalendarBackfillJobKey,
lease_token: Uuid,
- extracted_count: usize,
) -> Result<(), Report> {
let required_scopes = GOOGLE_CALENDAR_SCOPES.map(str::to_owned);
let mut tx = self.pool.begin().await.map_err(report)?;
@@ -1358,31 +1521,25 @@ impl CalendarBackfillRepository for PgCalendarRepository {
r#"
UPDATE calendar_backfill_jobs
SET status = 'complete',
- extracted_count = $2,
completed_at = now(),
lease_token = NULL,
lease_expires_at = NULL,
updated_at = now()
WHERE id = $1
- AND email_link_id = $3
+ AND email_link_id = $2
AND kind = 'google_calendar'
AND status = 'running'
- AND lease_token = $4
+ AND lease_token = $3
AND lease_expires_at > now()
AND EXISTS (
SELECT 1
FROM email_links link
WHERE link.id = calendar_backfill_jobs.email_link_id
AND link.google_grant_version = calendar_backfill_jobs.grant_version
- AND link.google_granted_scopes @> $5::text[]
+ AND link.google_granted_scopes @> $4::text[]
)
"#,
key.job_id,
- i64::try_from(extracted_count).map_err(|_| {
- rootcause::report!(
- "calendar backfill extracted count overflows the database representation"
- )
- })?,
key.email_link_id,
lease_token,
&required_scopes,
@@ -2160,6 +2317,24 @@ fn attendee_status(value: &str) -> AttendeeResponseStatus {
}
}
+/// Strip the volatile identifiers a fresh normalization mints (the proposed
+/// entity id and its occurrence back-references) so two projections of the
+/// same provider state compare equal.
+fn canonical_projection(value: &serde_json::Value) -> serde_json::Value {
+ let mut value = value.clone();
+ if let Some(event) = value.get_mut("event").and_then(|event| event.get_mut("id")) {
+ *event = serde_json::Value::Null;
+ }
+ if let Some(occurrences) = value.get_mut("occurrences").and_then(|v| v.as_array_mut()) {
+ for occurrence in occurrences {
+ if let Some(id) = occurrence.get_mut("eventId") {
+ *id = serde_json::Value::Null;
+ }
+ }
+ }
+ value
+}
+
fn report(error: impl std::error::Error + Send + Sync + 'static) -> Report {
rootcause::report!(error).into()
}
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index e2afa8867c..cd7556ecc2 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -1,6 +1,7 @@
use super::*;
use crate::domain::models::{
EmailIcsSource, GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSource,
+ GoogleWatchChannel,
};
use crate::domain::ports::GoogleCalendarSyncRepository;
use crate::domain::service::GoogleCalendarBackfillFailureService;
@@ -422,13 +423,24 @@ async fn completed_google_job_is_rearmed_and_reuses_calendar_sync_state(pool: Pg
materialized_range: Some(range.clone()),
cancelled_provider_event_ids: Vec::new(),
},
+ 3,
)
.await
.unwrap();
+ assert_eq!(
+ sqlx::query_scalar!(
+ r#"SELECT extracted_count FROM calendar_backfill_jobs WHERE id = $1"#,
+ key.job_id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap(),
+ 3
+ );
repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![calendar_id])
.await
.unwrap();
- repo.complete_google_backfill(key, lease_token, 0)
+ repo.complete_google_backfill(key, lease_token)
.await
.unwrap();
sqlx::query!(
@@ -906,7 +918,7 @@ async fn expired_google_worker_cannot_resurrect_reconciled_provider_data(pool: P
repo.reconcile_google_calendar_list(key, current_lease, account_id, Vec::new())
.await
.unwrap();
- repo.complete_google_backfill(key, current_lease, 0)
+ repo.complete_google_backfill(key, current_lease)
.await
.unwrap();
@@ -1042,6 +1054,7 @@ async fn google_snapshot_deletion_restores_the_surviving_email_source(pool: PgPo
materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
cancelled_provider_event_ids: Vec::new(),
},
+ 0,
)
.await
.unwrap();
@@ -1163,6 +1176,7 @@ async fn incremental_cancellation_tombstones_retire_sources_without_a_snapshot(p
"cancelled-master".to_string(),
],
},
+ 0,
)
.await
.unwrap();
@@ -1282,6 +1296,133 @@ async fn occurrence_cursor_is_stable_when_occurrences_share_a_start(pool: PgPool
assert_eq!(event_ids.len(), 3);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn watch_channels_round_trip_from_recording_to_targeted_rearm(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ let enabled = repo
+ .apply_google_grant(link_id, complete_grant())
+ .await
+ .unwrap();
+ let account_id = repo.upsert_google_account(link_id).await.unwrap();
+ let google_job = enabled
+ .jobs
+ .into_iter()
+ .find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
+ .unwrap();
+ let key = CalendarBackfillJobKey {
+ job_id: google_job.id,
+ email_link_id: link_id,
+ };
+ let CalendarBackfillClaim::Claimed { lease_token, .. } =
+ repo.claim_google_backfill(key).await.unwrap()
+ else {
+ panic!("Google job should be claimable");
+ };
+ let provider_calendar = ProviderCalendar {
+ provider_calendar_id: "primary".to_string(),
+ name: "Primary".to_string(),
+ description: None,
+ time_zone: Some("UTC".to_string()),
+ color: None,
+ access_role: Some("owner".to_string()),
+ is_primary: true,
+ is_selected: true,
+ };
+ let calendar_id = repo
+ .upsert_google_calendar(key, lease_token, account_id, provider_calendar.clone())
+ .await
+ .unwrap()
+ .id;
+
+ let channel = GoogleWatchChannel {
+ channel_id: Uuid::new_v4(),
+ resource_id: "resource-1".to_string(),
+ expires_at: (Utc::now() + Duration::days(6)).trunc_subsecs(6),
+ };
+ assert!(
+ repo.record_watch_channel(
+ key,
+ Uuid::new_v4(),
+ account_id,
+ calendar_id,
+ channel.clone()
+ )
+ .await
+ .is_err(),
+ "a stale lease must not record channels"
+ );
+ repo.record_watch_channel(key, lease_token, account_id, calendar_id, channel.clone())
+ .await
+ .unwrap();
+
+ // The stored expiry feeds the renewal decision on the next run.
+ let stored = repo
+ .upsert_google_calendar(key, lease_token, account_id, provider_calendar)
+ .await
+ .unwrap();
+ assert_eq!(stored.watch_expires_at, Some(channel.expires_at));
+
+ // Notifications resolve the channel to its inbox; unknown or mismatched
+ // identifiers resolve to nothing.
+ assert_eq!(
+ repo.find_watch_target(&channel.channel_id.to_string(), "resource-1")
+ .await
+ .unwrap(),
+ Some(link_id)
+ );
+ assert_eq!(
+ repo.find_watch_target(&channel.channel_id.to_string(), "other-resource")
+ .await
+ .unwrap(),
+ None
+ );
+
+ // A completed job re-arms exactly once per notification burst.
+ repo.commit_google_calendar_sync(
+ key,
+ lease_token,
+ account_id,
+ GoogleCalendarSyncSnapshot {
+ calendar_id,
+ next_sync_token: "next".to_string(),
+ observed_provider_event_ids: Some(Vec::new()),
+ materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
+ cancelled_provider_event_ids: Vec::new(),
+ },
+ 0,
+ )
+ .await
+ .unwrap();
+ repo.reconcile_google_calendar_list(key, lease_token, account_id, vec![calendar_id])
+ .await
+ .unwrap();
+ repo.complete_google_backfill(key, lease_token)
+ .await
+ .unwrap();
+ assert!(repo.schedule_google_sync_for_link(link_id).await.unwrap());
+ assert!(
+ !repo.schedule_google_sync_for_link(link_id).await.unwrap(),
+ "a pending job absorbs further notifications"
+ );
+
+ // Disabled accounts stop resolving notifications.
+ sqlx::query!(
+ "UPDATE calendar_accounts SET sync_status = 'disabled' WHERE id = $1",
+ account_id,
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ assert_eq!(
+ repo.find_watch_target(&channel.channel_id.to_string(), "resource-1")
+ .await
+ .unwrap(),
+ None
+ );
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn sync_status_reflects_visible_account_ingestion_state(pool: PgPool) {
let owner_id = "macro|[email protected]";
@@ -1314,6 +1455,83 @@ async fn sync_status_reflects_visible_account_ingestion_state(pool: PgPool) {
);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn unchanged_google_projection_skips_the_write_path(pool: PgPool) {
+ let owner_id = "macro|[email protected]";
+ let link_id = insert_link(&pool, owner_id).await;
+ let repo = PgCalendarRepository::new(pool.clone());
+ repo.apply_google_grant(link_id, complete_grant())
+ .await
+ .unwrap();
+ let account_id = repo.upsert_google_account(link_id).await.unwrap();
+ let calendar_id = repo
+ .upsert_calendar_fixture(
+ account_id,
+ ProviderCalendar {
+ provider_calendar_id: "primary".to_string(),
+ name: "Primary".to_string(),
+ description: None,
+ time_zone: Some("UTC".to_string()),
+ color: None,
+ access_role: Some("owner".to_string()),
+ is_primary: true,
+ is_selected: true,
+ },
+ )
+ .await
+ .unwrap();
+
+ let build = |title: &str| {
+ let mut upsert = timed_upsert(owner_id, link_id, "[email protected]", title, 1);
+ upsert.source = CalendarEventSource::Google(GoogleEventSource {
+ email_link_id: link_id,
+ account_id,
+ calendar_id,
+ provider_event_id: "stable-event".to_string(),
+ provider_recurring_event_id: None,
+ provider_etag: Some("\"etag\"".to_string()),
+ raw_payload: serde_json::json!({}),
+ });
+ upsert
+ };
+
+ let first_id = repo
+ .upsert_event_fixture(build("Same title"))
+ .await
+ .unwrap();
+ let generated_before = sqlx::query_scalar!(
+ r#"SELECT max(generated_at) AS "max!" FROM calendar_event_occurrences WHERE event_id = $1"#,
+ first_id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+
+ // A rebuild re-mints the proposed entity id, so a fresh mapping of the
+ // same provider state must be recognized as unchanged and skipped.
+ let second_id = repo
+ .upsert_event_fixture(build("Same title"))
+ .await
+ .unwrap();
+ assert_eq!(second_id, first_id);
+ let generated_after = sqlx::query_scalar!(
+ r#"SELECT max(generated_at) AS "max!" FROM calendar_event_occurrences WHERE event_id = $1"#,
+ first_id,
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(generated_after, generated_before);
+
+ // A real change still writes through.
+ repo.upsert_event_fixture(build("New title")).await.unwrap();
+ let title = sqlx::query_scalar!("SELECT title FROM calendar_events WHERE id = $1", first_id,)
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+ assert_eq!(title, "New title");
+}
+
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
async fn delegated_inbox_source_grants_calendar_visibility(pool: PgPool) {
let child_id = "macro|[email protected]";
@@ -1585,6 +1803,7 @@ async fn stale_google_source_projection_cannot_resurface_during_reconciliation(p
materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
cancelled_provider_event_ids: Vec::new(),
},
+ 0,
)
.await
.unwrap();
@@ -1599,6 +1818,7 @@ async fn stale_google_source_projection_cannot_resurface_during_reconciliation(p
materialized_range: Some(OccurrenceRange::historical_sync(Utc::now())),
cancelled_provider_event_ids: Vec::new(),
},
+ 0,
)
.await
.unwrap();
diff --git a/crates/email_db_client/src/backfill/job/update.rs b/crates/email_db_client/src/backfill/job/update.rs
index 04044cf85f..91e439e5cd 100644
--- a/crates/email_db_client/src/backfill/job/update.rs
+++ b/crates/email_db_client/src/backfill/job/update.rs
@@ -3,6 +3,45 @@ use models_email::email::service;
use sqlx::PgPool;
use sqlx::types::Uuid;
+/// Result of attempting to claim one email-backfill initialization lease.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum InitLeaseClaim {
+ /// This caller owns the returned lease token.
+ Claimed(Uuid),
+ /// Initialization already reached a later durable state.
+ Complete,
+ /// Another worker currently owns initialization.
+ Busy,
+ /// The requested backfill job does not exist.
+ NotFound,
+}
+
+/// Result of attempting to renew an email-backfill initialization lease.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum InitLeaseRenewal {
+ /// The lease was extended.
+ Renewed,
+ /// Initialization already reached a later durable state.
+ Complete,
+ /// The lease is no longer owned by this worker.
+ Lost,
+ /// The requested backfill job does not exist.
+ NotFound,
+}
+
+/// Result of attempting to atomically complete an email backfill.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum BackfillCompletion {
+ /// This caller completed the active job and published its completion effects.
+ Completed,
+ /// The job had already reached a terminal state.
+ AlreadyTerminal,
+ /// The job is active, but this caller does not hold the required lease.
+ LeaseLost,
+ /// The requested backfill job does not exist.
+ NotFound,
+}
+
#[tracing::instrument(skip(executor), err)]
pub async fn update_backfill_job_status<'e, E>(
executor: E,
@@ -32,6 +71,510 @@ where
Ok(())
}
+/// Claim initialization for an email backfill, allowing expired work to resume.
+#[tracing::instrument(skip(pool), err)]
+pub async fn claim_init_lease(pool: &PgPool, job_id: Uuid) -> anyhow::Result<InitLeaseClaim> {
+ let lease_token = Uuid::now_v7();
+ let claimed = sqlx::query_scalar!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET init_lease_token = $2,
+ init_lease_expires_at = now() + interval '2 minutes',
+ updated_at = now()
+ WHERE id = $1
+ AND status = 'Init'
+ AND initialized_at IS NULL
+ AND (
+ init_lease_expires_at IS NULL
+ OR init_lease_expires_at < now()
+ )
+ RETURNING init_lease_token AS "init_lease_token!"
+ "#,
+ job_id,
+ lease_token,
+ )
+ .fetch_optional(pool)
+ .await?;
+ if let Some(lease_token) = claimed {
+ return Ok(InitLeaseClaim::Claimed(lease_token));
+ }
+
+ let state = sqlx::query!(
+ r#"
+ SELECT status::text AS "status!", initialized_at
+ FROM email_backfill_jobs
+ WHERE id = $1
+ "#,
+ job_id,
+ )
+ .fetch_optional(pool)
+ .await?;
+ Ok(match state {
+ Some(state) if state.initialized_at.is_some() || state.status != "Init" => {
+ InitLeaseClaim::Complete
+ }
+ Some(_) => InitLeaseClaim::Busy,
+ None => InitLeaseClaim::NotFound,
+ })
+}
+
+/// Renew initialization while the caller still owns the lease.
+#[tracing::instrument(skip(pool), err)]
+pub async fn renew_init_lease(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<InitLeaseRenewal> {
+ let updated = sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET init_lease_expires_at = now() + interval '2 minutes',
+ updated_at = now()
+ WHERE id = $1
+ AND status = 'Init'
+ AND init_lease_token = $2
+ AND init_lease_expires_at > now()
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(pool)
+ .await?;
+ if updated.rows_affected() == 1 {
+ return Ok(InitLeaseRenewal::Renewed);
+ }
+
+ let state = sqlx::query!(
+ r#"
+ SELECT status::text AS "status!", initialized_at
+ FROM email_backfill_jobs
+ WHERE id = $1
+ "#,
+ job_id,
+ )
+ .fetch_optional(pool)
+ .await?;
+ Ok(match state {
+ Some(state) if state.initialized_at.is_some() || state.status != "Init" => {
+ InitLeaseRenewal::Complete
+ }
+ Some(_) => InitLeaseRenewal::Lost,
+ None => InitLeaseRenewal::NotFound,
+ })
+}
+
+/// Release an initialization lease still owned by the caller.
+#[tracing::instrument(skip(pool), err)]
+pub async fn release_init_lease(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<bool> {
+ let released = sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET init_lease_token = NULL,
+ init_lease_expires_at = NULL,
+ updated_at = now()
+ WHERE id = $1
+ AND status = 'Init'
+ AND init_lease_token = $2
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(pool)
+ .await?;
+ Ok(released.rows_affected() == 1)
+}
+
+/// Atomically publish initialization and transition the leased job in progress.
+#[tracing::instrument(skip(pool), err)]
+pub async fn finalize_initialization(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<bool> {
+ let mut tx = pool.begin().await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_init_outbox (id, backfill_job_id)
+ VALUES ($1, $2)
+ ON CONFLICT (backfill_job_id) DO NOTHING
+ "#,
+ Uuid::now_v7(),
+ job_id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ let finalized = sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET status = 'InProgress',
+ initialized_at = now(),
+ init_lease_token = NULL,
+ init_lease_expires_at = NULL,
+ updated_at = now()
+ WHERE id = $1
+ AND status = 'Init'
+ AND init_lease_token = $2
+ AND init_lease_expires_at > now()
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(&mut *tx)
+ .await?;
+ if finalized.rows_affected() == 0 {
+ tx.rollback().await?;
+ return Ok(false);
+ }
+ tx.commit().await?;
+ Ok(true)
+}
+
+/// Atomically fails an email backfill job and any running email-calendar
+/// extraction job associated with that scan.
+#[tracing::instrument(skip(pool), err)]
+pub async fn fail_backfill_job_and_calendar_extraction(
+ pool: &PgPool,
+ job_id: Uuid,
+) -> anyhow::Result<bool> {
+ let mut tx = pool.begin().await?;
+ let failed = sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET status = 'Failed',
+ updated_at = now()
+ WHERE id = $1
+ AND status IN ('Init', 'InProgress')
+ "#,
+ job_id,
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ if failed.rows_affected() == 0 {
+ tx.commit().await?;
+ return Ok(false);
+ }
+
+ sqlx::query!(
+ r#"
+ UPDATE calendar_backfill_jobs
+ SET status = 'failed',
+ last_error = 'email backfill failed before calendar extraction completed',
+ completed_at = now(),
+ updated_at = now()
+ WHERE kind = 'email_ics'
+ AND status = 'running'
+ AND cursor->>'emailBackfillJobId' = $1
+ "#,
+ job_id.to_string(),
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ tx.commit().await?;
+ Ok(true)
+}
+
+/// Atomically completes an active email backfill and its associated email-ICS
+/// extraction job, returning a durable outcome for idempotency and fencing.
+#[tracing::instrument(skip(pool), err)]
+pub async fn complete_backfill_job_and_calendar_extraction(
+ pool: &PgPool,
+ job_id: Uuid,
+ init_lease_token: Option<Uuid>,
+) -> anyhow::Result<BackfillCompletion> {
+ let mut tx = pool.begin().await?;
+ let completed = sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET status = 'Complete',
+ initialized_at = COALESCE(initialized_at, now()),
+ init_lease_token = NULL,
+ init_lease_expires_at = NULL,
+ updated_at = now()
+ WHERE id = $1
+ AND (
+ (
+ status = 'Init'
+ AND $2::uuid IS NOT NULL
+ AND init_lease_token = $2
+ AND init_lease_expires_at > now()
+ )
+ OR (
+ status = 'InProgress'
+ AND $2::uuid IS NULL
+ )
+ )
+ "#,
+ job_id,
+ init_lease_token,
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ if completed.rows_affected() == 0 {
+ let status = sqlx::query_scalar!(
+ r#"
+ SELECT status::text AS "status!"
+ FROM email_backfill_jobs
+ WHERE id = $1
+ "#,
+ job_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ return Ok(match status.as_deref() {
+ Some("Complete" | "Failed" | "Cancelled") => BackfillCompletion::AlreadyTerminal,
+ Some(_) => BackfillCompletion::LeaseLost,
+ None => BackfillCompletion::NotFound,
+ });
+ }
+
+ sqlx::query!(
+ r#"
+ UPDATE calendar_backfill_jobs
+ SET status = 'complete',
+ completed_at = now(),
+ updated_at = now()
+ WHERE kind = 'email_ics'
+ AND status = 'running'
+ AND cursor->>'emailBackfillJobId' = $1
+ "#,
+ job_id.to_string(),
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_completion_outbox (id, backfill_job_id)
+ VALUES ($1, $2)
+ ON CONFLICT (backfill_job_id) DO NOTHING
+ "#,
+ Uuid::now_v7(),
+ job_id,
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ tx.commit().await?;
+ Ok(BackfillCompletion::Completed)
+}
+
+/// Returns whether durable post-completion effects still need to run.
+#[tracing::instrument(skip(pool), err)]
+pub async fn completion_effects_pending(pool: &PgPool, job_id: Uuid) -> anyhow::Result<bool> {
+ let pending = sqlx::query_scalar!(
+ r#"
+ SELECT completed_at IS NULL AS "pending!"
+ FROM email_backfill_completion_outbox
+ WHERE backfill_job_id = $1
+ "#,
+ job_id,
+ )
+ .fetch_optional(pool)
+ .await?;
+ Ok(pending.unwrap_or(false))
+}
+
+/// Claims durable post-completion effects for one worker.
+///
+/// The lease prevents duplicate queue deliveries from running the same effects
+/// concurrently. An expired lease can be reclaimed after a worker crash.
+#[tracing::instrument(skip(pool), err)]
+pub async fn claim_completion_effects(pool: &PgPool, job_id: Uuid) -> anyhow::Result<Option<Uuid>> {
+ let lease_token = Uuid::now_v7();
+ let claimed = sqlx::query_scalar!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET effects_lease_token = $2,
+ effects_lease_expires_at = now() + interval '2 minutes'
+ WHERE backfill_job_id = $1
+ AND completed_at IS NULL
+ AND (
+ effects_lease_token IS NULL
+ OR effects_lease_expires_at < now()
+ )
+ RETURNING effects_lease_token AS "effects_lease_token!"
+ "#,
+ job_id,
+ lease_token,
+ )
+ .fetch_optional(pool)
+ .await?;
+ Ok(claimed)
+}
+
+/// Extends a completion-effects lease owned by `lease_token`.
+#[tracing::instrument(skip(pool), err)]
+pub async fn renew_completion_effects(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<bool> {
+ let renewed = sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET effects_lease_expires_at = now() + interval '2 minutes'
+ WHERE backfill_job_id = $1
+ AND completed_at IS NULL
+ AND effects_lease_token = $2
+ AND effects_lease_expires_at > now()
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(pool)
+ .await?;
+ Ok(renewed.rows_affected() == 1)
+}
+
+/// Releases a completion-effects lease owned by `lease_token`.
+#[tracing::instrument(skip(pool), err)]
+pub async fn release_completion_effects(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<bool> {
+ let released = sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET effects_lease_token = NULL,
+ effects_lease_expires_at = NULL
+ WHERE backfill_job_id = $1
+ AND completed_at IS NULL
+ AND effects_lease_token = $2
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(pool)
+ .await?;
+ Ok(released.rows_affected() == 1)
+}
+
+/// Marks durable post-completion effects complete after every effect succeeds.
+///
+/// The lease token fences out stale workers whose lease expired and was
+/// reclaimed while they were still running.
+#[tracing::instrument(skip(pool), err)]
+pub async fn mark_completion_effects_complete(
+ pool: &PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> anyhow::Result<()> {
+ let result = sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET completed_at = now()
+ WHERE backfill_job_id = $1
+ AND completed_at IS NULL
+ AND effects_lease_token = $2
+ AND effects_lease_expires_at > now()
+ "#,
+ job_id,
+ lease_token,
+ )
+ .execute(pool)
+ .await?;
+ if result.rows_affected() == 0 {
+ anyhow::bail!("no pending completion effects for backfill job {job_id}");
+ }
+ Ok(())
+}
+
+/// Atomically fails an active email-ICS calendar backfill and any unpublished
+/// email scan it owns.
+#[tracing::instrument(skip(pool, message), err)]
+pub async fn fail_email_ics_calendar_backfill_job(
+ pool: &PgPool,
+ calendar_job_id: Uuid,
+ email_link_id: Uuid,
+ message: &str,
+) -> anyhow::Result<bool> {
+ let mut tx = pool.begin().await?;
+ let job_identity = sqlx::query!(
+ r#"
+ SELECT
+ (cursor->>'emailBackfillJobId')::uuid AS email_job_id
+ FROM calendar_backfill_jobs
+ WHERE id = $1
+ AND email_link_id = $2
+ AND kind = 'email_ics'
+ "#,
+ calendar_job_id,
+ email_link_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(job_identity) = job_identity else {
+ tx.commit().await?;
+ return Ok(false);
+ };
+
+ if let Some(email_job_id) = job_identity.email_job_id {
+ sqlx::query_scalar!(
+ r#"
+ SELECT id
+ FROM email_backfill_jobs
+ WHERE id = $1
+ FOR UPDATE
+ "#,
+ email_job_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ }
+
+ let failed_job = sqlx::query!(
+ r#"
+ UPDATE calendar_backfill_jobs
+ SET status = 'failed',
+ last_error = $3,
+ completed_at = now(),
+ lease_token = NULL,
+ lease_expires_at = NULL,
+ updated_at = now()
+ WHERE id = $1
+ AND email_link_id = $2
+ AND kind = 'email_ics'
+ AND status NOT IN ('complete', 'failed')
+ RETURNING
+ (cursor->>'emailBackfillJobId')::uuid AS email_job_id
+ "#,
+ calendar_job_id,
+ email_link_id,
+ message,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+ let Some(failed_job) = failed_job else {
+ tx.commit().await?;
+ return Ok(false);
+ };
+
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET status = 'Failed',
+ updated_at = now()
+ WHERE id = $1
+ AND status = 'Init'
+ "#,
+ failed_job.email_job_id,
+ )
+ .execute(&mut *tx)
+ .await?;
+
+ tx.commit().await?;
+ Ok(true)
+}
+
#[tracing::instrument(skip(pool), err)]
pub async fn cancel_active_jobs_by_link_id(pool: &PgPool, link_id: Uuid) -> anyhow::Result<usize> {
let db_status = db::backfill::BackfillJobStatus::Cancelled;
@@ -102,3 +645,6 @@ pub async fn update_job_threads_retrieved_count(
Ok(())
}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/email_db_client/src/backfill/job/update/test.rs b/crates/email_db_client/src/backfill/job/update/test.rs
new file mode 100644
index 0000000000..33063b7b6c
--- /dev/null
+++ b/crates/email_db_client/src/backfill/job/update/test.rs
@@ -0,0 +1,487 @@
+use super::{
+ BackfillCompletion, InitLeaseClaim, InitLeaseRenewal, claim_completion_effects,
+ claim_init_lease, complete_backfill_job_and_calendar_extraction, completion_effects_pending,
+ fail_backfill_job_and_calendar_extraction, fail_email_ics_calendar_backfill_job,
+ finalize_initialization, mark_completion_effects_complete, release_completion_effects,
+ release_init_lease, renew_completion_effects, renew_init_lease,
+};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use serde_json::json;
+use sqlx::{PgPool, types::Uuid};
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn initialization_lease_is_fenced_reclaimable_and_publishes_once(
+ pool: PgPool,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (id, link_id, fusionauth_user_id, status)
+ VALUES ($1, $2, $3, 'Init')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-init-lease",
+ )
+ .execute(&pool)
+ .await?;
+
+ let InitLeaseClaim::Claimed(first_lease) = claim_init_lease(&pool, email_job_id).await? else {
+ panic!("initialization should be claimable");
+ };
+ assert_eq!(
+ claim_init_lease(&pool, email_job_id).await?,
+ InitLeaseClaim::Busy
+ );
+ assert_eq!(
+ renew_init_lease(&pool, email_job_id, first_lease).await?,
+ InitLeaseRenewal::Renewed
+ );
+ assert!(!release_init_lease(&pool, email_job_id, Uuid::new_v4()).await?);
+ assert!(release_init_lease(&pool, email_job_id, first_lease).await?);
+
+ let InitLeaseClaim::Claimed(current_lease) = claim_init_lease(&pool, email_job_id).await?
+ else {
+ panic!("released initialization should be reclaimable");
+ };
+ assert!(!finalize_initialization(&pool, email_job_id, Uuid::new_v4()).await?);
+ assert!(finalize_initialization(&pool, email_job_id, current_lease).await?);
+ assert_eq!(
+ claim_init_lease(&pool, email_job_id).await?,
+ InitLeaseClaim::Complete
+ );
+ assert_eq!(
+ claim_init_lease(&pool, Uuid::new_v4()).await?,
+ InitLeaseClaim::NotFound
+ );
+ assert_eq!(
+ sqlx::query_scalar!(
+ r#"
+ SELECT count(*) AS "count!"
+ FROM email_backfill_init_outbox
+ WHERE backfill_job_id = $1
+ "#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?,
+ 1
+ );
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn failing_email_scan_also_fails_associated_calendar_job(pool: PgPool) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ let calendar_job_id = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (id, link_id, fusionauth_user_id, status)
+ VALUES ($1, $2, $3, 'Init')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-cleanup",
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO calendar_backfill_jobs (
+ id, email_link_id, kind, grant_version, status, cursor
+ )
+ VALUES ($1, $2, 'email_ics', 1, 'running', $3)
+ "#,
+ calendar_job_id,
+ link_id,
+ json!({ "emailBackfillJobId": email_job_id }),
+ )
+ .execute(&pool)
+ .await?;
+
+ assert!(fail_backfill_job_and_calendar_extraction(&pool, email_job_id).await?);
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ BackfillCompletion::AlreadyTerminal
+ );
+
+ let email_status = sqlx::query_scalar!(
+ r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ let calendar_job = sqlx::query!(
+ r#"
+ SELECT status, last_error, completed_at
+ FROM calendar_backfill_jobs
+ WHERE id = $1
+ "#,
+ calendar_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+
+ assert_eq!(email_status, "Failed");
+ assert_eq!(calendar_job.status, "failed");
+ assert_eq!(
+ calendar_job.last_error.as_deref(),
+ Some("email backfill failed before calendar extraction completed")
+ );
+ assert!(calendar_job.completed_at.is_some());
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn completing_active_email_scan_also_completes_calendar_job(
+ pool: PgPool,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ let calendar_job_id = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (id, link_id, fusionauth_user_id, status)
+ VALUES ($1, $2, $3, 'InProgress')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-completion",
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO calendar_backfill_jobs (
+ id, email_link_id, kind, grant_version, status, cursor
+ )
+ VALUES ($1, $2, 'email_ics', 1, 'running', $3)
+ "#,
+ calendar_job_id,
+ link_id,
+ json!({ "emailBackfillJobId": email_job_id }),
+ )
+ .execute(&pool)
+ .await?;
+
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ BackfillCompletion::Completed
+ );
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ BackfillCompletion::AlreadyTerminal
+ );
+
+ let email_status = sqlx::query_scalar!(
+ r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ let calendar = sqlx::query!(
+ "SELECT status, completed_at FROM calendar_backfill_jobs WHERE id = $1",
+ calendar_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(email_status, "Complete");
+ assert_eq!(calendar.status, "complete");
+ assert!(calendar.completed_at.is_some());
+ assert!(completion_effects_pending(&pool, email_job_id).await?);
+ let lease_token = claim_completion_effects(&pool, email_job_id)
+ .await?
+ .expect("completion effects should be claimable");
+ assert!(
+ claim_completion_effects(&pool, email_job_id)
+ .await?
+ .is_none()
+ );
+ assert!(renew_completion_effects(&pool, email_job_id, lease_token).await?);
+ assert!(!release_completion_effects(&pool, email_job_id, Uuid::new_v4()).await?);
+ assert!(release_completion_effects(&pool, email_job_id, lease_token).await?);
+ let lease_token = claim_completion_effects(&pool, email_job_id)
+ .await?
+ .expect("released completion effects should be reclaimable");
+ assert!(
+ mark_completion_effects_complete(&pool, email_job_id, Uuid::new_v4())
+ .await
+ .is_err()
+ );
+ mark_completion_effects_complete(&pool, email_job_id, lease_token).await?;
+ assert!(!completion_effects_pending(&pool, email_job_id).await?);
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn zero_thread_completion_is_fenced_by_init_lease(pool: PgPool) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ let current_lease = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (
+ id, link_id, fusionauth_user_id, status,
+ init_lease_token, init_lease_expires_at
+ )
+ VALUES ($1, $2, $3, 'Init', $4, now() + interval '2 minutes')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-fenced-zero",
+ current_lease,
+ )
+ .execute(&pool)
+ .await?;
+
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ BackfillCompletion::LeaseLost
+ );
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, Some(Uuid::new_v4()))
+ .await?,
+ BackfillCompletion::LeaseLost
+ );
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_jobs
+ SET init_lease_expires_at = now() - interval '1 second'
+ WHERE id = $1
+ "#,
+ email_job_id,
+ )
+ .execute(&pool)
+ .await?;
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, Some(current_lease))
+ .await?,
+ BackfillCompletion::LeaseLost
+ );
+ assert_eq!(
+ sqlx::query_scalar!(
+ r#"
+ SELECT count(*) AS "count!"
+ FROM email_backfill_completion_outbox
+ WHERE backfill_job_id = $1
+ "#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?,
+ 0
+ );
+ let InitLeaseClaim::Claimed(reclaimed_lease) = claim_init_lease(&pool, email_job_id).await?
+ else {
+ panic!("expired zero-thread initialization should be reclaimable");
+ };
+ assert_eq!(
+ complete_backfill_job_and_calendar_extraction(&pool, email_job_id, Some(reclaimed_lease))
+ .await?,
+ BackfillCompletion::Completed
+ );
+
+ let status = sqlx::query_scalar!(
+ r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(status, "Complete");
+ assert!(completion_effects_pending(&pool, email_job_id).await?);
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ let calendar_job_id = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (id, link_id, fusionauth_user_id, status)
+ VALUES ($1, $2, $3, 'Complete')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-terminal",
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO calendar_backfill_jobs (
+ id, email_link_id, kind, grant_version, status, cursor, completed_at
+ )
+ VALUES ($1, $2, 'email_ics', 1, 'complete', $3, now())
+ "#,
+ calendar_job_id,
+ link_id,
+ json!({ "emailBackfillJobId": email_job_id }),
+ )
+ .execute(&pool)
+ .await?;
+
+ assert!(!fail_backfill_job_and_calendar_extraction(&pool, email_job_id).await?);
+ assert!(!fail_backfill_job_and_calendar_extraction(&pool, Uuid::new_v4()).await?);
+
+ let email_status = sqlx::query_scalar!(
+ r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ let calendar_status = sqlx::query_scalar!(
+ "SELECT status FROM calendar_backfill_jobs WHERE id = $1",
+ calendar_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+
+ assert_eq!(email_status, "Complete");
+ assert_eq!(calendar_status, "complete");
+ Ok(())
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn failing_email_calendar_job_releases_unpublished_scan(pool: PgPool) -> anyhow::Result<()> {
+ let link_id = Uuid::new_v4();
+ let email_job_id = Uuid::new_v4();
+ let calendar_job_id = Uuid::new_v4();
+ sqlx::query!(
+ r#"
+ INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider)
+ VALUES ($1, $2, $2, $3, 'GMAIL')
+ "#,
+ link_id,
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO email_backfill_jobs (id, link_id, fusionauth_user_id, status)
+ VALUES ($1, $2, $3, 'Init')
+ "#,
+ email_job_id,
+ link_id,
+ "fusion-calendar-terminal-cleanup",
+ )
+ .execute(&pool)
+ .await?;
+ sqlx::query!(
+ r#"
+ INSERT INTO calendar_backfill_jobs (
+ id, email_link_id, kind, grant_version, status, cursor
+ )
+ VALUES ($1, $2, 'email_ics', 1, 'running', $3)
+ "#,
+ calendar_job_id,
+ link_id,
+ json!({ "emailBackfillJobId": email_job_id }),
+ )
+ .execute(&pool)
+ .await?;
+
+ assert!(
+ !fail_email_ics_calendar_backfill_job(
+ &pool,
+ calendar_job_id,
+ Uuid::new_v4(),
+ "wrong inbox",
+ )
+ .await?
+ );
+ assert!(
+ fail_email_ics_calendar_backfill_job(
+ &pool,
+ calendar_job_id,
+ link_id,
+ "invalid calendar data",
+ )
+ .await?
+ );
+
+ let email_status = sqlx::query_scalar!(
+ r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
+ email_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+ let calendar_job = sqlx::query!(
+ "SELECT status, last_error FROM calendar_backfill_jobs WHERE id = $1",
+ calendar_job_id,
+ )
+ .fetch_one(&pool)
+ .await?;
+
+ assert_eq!(email_status, "Failed");
+ assert_eq!(calendar_job.status, "failed");
+ assert_eq!(
+ calendar_job.last_error.as_deref(),
+ Some("invalid calendar data")
+ );
+ assert!(
+ !fail_email_ics_calendar_backfill_job(&pool, calendar_job_id, link_id, "duplicate",)
+ .await?
+ );
+ Ok(())
+}
diff --git a/crates/fusionauth/src/google/oauth.rs b/crates/fusionauth/src/google/oauth.rs
index d695ae25b5..67eb056b13 100644
--- a/crates/fusionauth/src/google/oauth.rs
+++ b/crates/fusionauth/src/google/oauth.rs
@@ -109,9 +109,13 @@ pub(crate) async fn refresh_google_token(
/// Response from exchanging a Google authorization code for tokens.
pub struct GoogleExchangeTokenResponse {
/// The refresh token.
+ #[serde(default)]
pub refresh_token: String,
/// The ID token.
pub id_token: String,
+ /// The actual scopes granted by the user.
+ #[serde(default)]
+ pub scope: String,
}
#[derive(Debug, serde::Serialize)]
diff --git a/crates/macro_db_client/migrations/20260803192043_calendar_synced_at.sql b/crates/macro_db_client/migrations/20260803192043_calendar_synced_at.sql
new file mode 100644
index 0000000000..1dad1e7869
--- /dev/null
+++ b/crates/macro_db_client/migrations/20260803192043_calendar_synced_at.sql
@@ -0,0 +1,5 @@
+-- Tracks when each provider calendar's sync state last committed, so the
+-- backfill loop can hold Google's system calendars to a daily cadence.
+-- Added separately because the calendar_entities migration had already been
+-- applied to deployed databases when this column was introduced.
+ALTER TABLE calendars ADD COLUMN IF NOT EXISTS synced_at timestamptz;
diff --git a/crates/macro_db_client/src/in_progress_user_link.rs b/crates/macro_db_client/src/in_progress_user_link.rs
index b8044614ce..0cf2720bee 100644
--- a/crates/macro_db_client/src/in_progress_user_link.rs
+++ b/crates/macro_db_client/src/in_progress_user_link.rs
@@ -55,6 +55,34 @@ pub async fn create_in_progress_user_link(
Ok(link_id)
}
+/// Create an in-progress Google link and record the exact scopes requested.
+pub async fn create_in_progress_google_link(
+ db: &sqlx::Pool<sqlx::Postgres>,
+ macro_user_id: &str,
+ requested_google_scopes: &[String],
+) -> anyhow::Result<Uuid> {
+ let macro_user_id = macro_uuid::string_to_uuid(macro_user_id)?;
+ let link_id = macro_uuid::generate_uuid_v7();
+
+ sqlx::query!(
+ r#"
+ INSERT INTO in_progress_user_link (
+ id,
+ macro_user_id,
+ requested_google_scopes
+ )
+ VALUES ($1, $2, $3)
+ "#,
+ &link_id,
+ &macro_user_id,
+ requested_google_scopes
+ )
+ .execute(db)
+ .await?;
+
+ Ok(link_id)
+}
+
pub async fn delete_in_progress_user_link<'e, E>(db: E, link_id: &uuid::Uuid) -> anyhow::Result<()>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
@@ -121,6 +149,10 @@ pub async fn get_macro_user_id_by_link_id(
pub struct InProgressUserLink {
pub macro_user_id: Uuid,
pub linked_email: Option<String>,
+ /// Scopes Macro placed on the authorization request.
+ pub requested_google_scopes: Vec<String>,
+ /// Actual scopes returned by Google's token exchange.
+ pub granted_google_scopes: Vec<String>,
}
pub async fn get_in_progress_user_link(
@@ -131,7 +163,9 @@ pub async fn get_in_progress_user_link(
r#"
SELECT
macro_user_id,
- linked_email
+ linked_email,
+ requested_google_scopes,
+ granted_google_scopes
FROM
in_progress_user_link
WHERE
@@ -145,9 +179,39 @@ pub async fn get_in_progress_user_link(
Ok(InProgressUserLink {
macro_user_id: row.macro_user_id,
linked_email: row.linked_email,
+ requested_google_scopes: row.requested_google_scopes,
+ granted_google_scopes: row.granted_google_scopes,
})
}
+/// Record the identity and actual scopes returned by Google's OAuth callback.
+pub async fn set_linked_google_grant(
+ db: &sqlx::Pool<sqlx::Postgres>,
+ link_id: &uuid::Uuid,
+ linked_email: &str,
+ granted_google_scopes: &[String],
+) -> anyhow::Result<()> {
+ let result = sqlx::query!(
+ r#"
+ UPDATE in_progress_user_link
+ SET linked_email = $1,
+ granted_google_scopes = $2
+ WHERE id = $3
+ "#,
+ linked_email,
+ granted_google_scopes,
+ link_id
+ )
+ .execute(db)
+ .await?;
+
+ if result.rows_affected() == 0 {
+ anyhow::bail!("in_progress_user_link not found for link_id={link_id}");
+ }
+
+ Ok(())
+}
+
pub async fn set_linked_email(
db: &sqlx::Pool<sqlx::Postgres>,
link_id: &uuid::Uuid,
diff --git a/crates/macro_db_client/src/in_progress_user_link/test.rs b/crates/macro_db_client/src/in_progress_user_link/test.rs
index b63a16afce..6ae3053ebf 100644
--- a/crates/macro_db_client/src/in_progress_user_link/test.rs
+++ b/crates/macro_db_client/src/in_progress_user_link/test.rs
@@ -24,6 +24,8 @@ async fn set_linked_email_then_get(pool: Pool<Postgres>) -> anyhow::Result<()> {
let pre = get_in_progress_user_link(&pool, &link_id).await?;
assert_eq!(pre.macro_user_id, macro_user_id);
assert!(pre.linked_email.is_none());
+ assert!(pre.requested_google_scopes.is_empty());
+ assert!(pre.granted_google_scopes.is_empty());
set_linked_email(&pool, &link_id, "[email protected]").await?;
@@ -34,6 +36,27 @@ async fn set_linked_email_then_get(pool: Pool<Postgres>) -> anyhow::Result<()> {
Ok(())
}
+#[sqlx::test]
+async fn google_link_records_requested_and_granted_scopes(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let macro_user_id = macro_uuid::generate_uuid_v7();
+ insert_macro_user(&pool, macro_user_id).await?;
+ let requested = vec!["gmail".to_string(), "calendar".to_string()];
+ let granted = vec!["gmail".to_string()];
+
+ let link_id =
+ create_in_progress_google_link(&pool, &macro_user_id.to_string(), &requested).await?;
+ set_linked_google_grant(&pool, &link_id, "[email protected]", &granted).await?;
+
+ let link = get_in_progress_user_link(&pool, &link_id).await?;
+ assert_eq!(link.requested_google_scopes, requested);
+ assert_eq!(link.granted_google_scopes, granted);
+ assert_eq!(link.linked_email.as_deref(), Some("[email protected]"));
+
+ Ok(())
+}
+
#[sqlx::test]
async fn count_excludes_expired_links(pool: Pool<Postgres>) -> anyhow::Result<()> {
let macro_user_id = macro_uuid::generate_uuid_v7();
diff --git a/crates/models_email/src/email/service/backfill.rs b/crates/models_email/src/email/service/backfill.rs
index b46d2fe7c2..044d31a88c 100644
--- a/crates/models_email/src/email/service/backfill.rs
+++ b/crates/models_email/src/email/service/backfill.rs
@@ -21,10 +21,17 @@ pub struct BackfillMessagePayload {
pub message_provider_id: String,
}
+/// Dispatch payload for a durable calendar backfill job.
+#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
+pub struct CalendarBackfillPayload {
+ /// Calendar-domain job identifier.
+ pub calendar_job_id: Uuid,
+}
+
/// Scope envelope for backfill operations that belong to a tracked backfill
/// job (Init, ListThreads, BackfillThread, BackfillMessage,
-/// UpdateThreadMetadata, BackfillAttachment). Carries the `link_id` and
-/// `job_id` the operation needs alongside its variant-specific payload.
+/// UpdateThreadMetadata, BackfillAttachment, FinalizeBackfill). Carries the
+/// `link_id` and `job_id` the operation needs alongside its variant-specific payload.
/// The inner payload is `#[serde(flatten)]`-ed so the JSON shape stays the
/// same as if `link_id`/`job_id` and the payload fields were siblings.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
@@ -50,6 +57,10 @@ pub struct LinkScopedPayload<P> {
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default)]
pub struct InitPayload {}
+/// Empty payload for [`BackfillOperation::FinalizeBackfill`].
+#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, Default)]
+pub struct FinalizeBackfillPayload {}
+
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum BackfillOperation {
@@ -74,6 +85,14 @@ pub enum BackfillOperation {
UpdateThreadMetadata(JobScopedPayload<UpdateMetadataPayload>),
// Uploads the message attachment as a Macro document.
BackfillAttachment(JobScopedPayload<BackfillAttachmentPayload>),
+ /// Runs durable post-completion attachment, contact, and client-refresh effects.
+ FinalizeBackfill(JobScopedPayload<FinalizeBackfillPayload>),
+ /// Fetch calendars, canonical events, and bounded occurrence projections
+ /// from Google Calendar.
+ CalendarGoogleBackfill(LinkScopedPayload<CalendarBackfillPayload>),
+ /// Start an email re-scan; the normal BackfillMessage path performs the
+ /// same iCalendar extraction used for live ingestion.
+ CalendarEmailIcsBackfill(LinkScopedPayload<CalendarBackfillPayload>),
// Seeds the contacts service from one recent sent message. Fanned out
// one-per-message by the priority pass of ListThreads (which lists the
// user's last 200 sent messages). The consumer fetches the message and
@@ -118,6 +137,9 @@ impl BackfillOperation {
BackfillOperation::BackfillMessage(s) => Some(s.link_id),
BackfillOperation::UpdateThreadMetadata(s) => Some(s.link_id),
BackfillOperation::BackfillAttachment(s) => Some(s.link_id),
+ BackfillOperation::FinalizeBackfill(s) => Some(s.link_id),
+ BackfillOperation::CalendarGoogleBackfill(s) => Some(s.link_id),
+ BackfillOperation::CalendarEmailIcsBackfill(s) => Some(s.link_id),
BackfillOperation::SeedSentContact(s) => Some(s.link_id),
BackfillOperation::PopulateCrmContact(s) => Some(s.link_id),
BackfillOperation::DepopulateCrmContact(s) => Some(s.link_id),
@@ -136,7 +158,10 @@ impl BackfillOperation {
BackfillOperation::BackfillMessage(s) => Some(s.job_id),
BackfillOperation::UpdateThreadMetadata(s) => Some(s.job_id),
BackfillOperation::BackfillAttachment(s) => Some(s.job_id),
+ BackfillOperation::FinalizeBackfill(s) => Some(s.job_id),
BackfillOperation::SeedSentContact(s) => Some(s.job_id),
+ BackfillOperation::CalendarGoogleBackfill(_)
+ | BackfillOperation::CalendarEmailIcsBackfill(_) => None,
BackfillOperation::PopulateCrmContact(_)
| BackfillOperation::DepopulateCrmContact(_)
| BackfillOperation::PopulateCrmForUser(_)
diff --git a/crates/models_email/src/email/service/pubsub.rs b/crates/models_email/src/email/service/pubsub.rs
index 0675c9aac2..3f3e7dcc90 100644
--- a/crates/models_email/src/email/service/pubsub.rs
+++ b/crates/models_email/src/email/service/pubsub.rs
@@ -31,6 +31,8 @@ pub enum FailureReason {
MessageNotFoundInDatabase,
LinkNotFound,
BackfillJobNotFound,
+ EmailBackfillInitBusy,
+ CalendarBackfillBusy,
GmailApiFailed,
GmailApiRateLimited,
OutdatedHistoryId,
diff --git a/crates/workspace-hack/Cargo.toml b/crates/workspace-hack/Cargo.toml
index 5f7c2bc155..c1b7c87001 100644
--- a/crates/workspace-hack/Cargo.toml
+++ b/crates/workspace-hack/Cargo.toml
@@ -36,6 +36,7 @@ bytes = { version = "1", features = ["serde"] }
bzip2 = { version = "0.5", features = ["libbz2-rs-sys"] }
bzip2-sys = { version = "0.1", default-features = false, features = ["__disabled"] }
chrono = { version = "0.4", features = ["serde"] }
+chrono-tz = { version = "0.10", features = ["serde"] }
crossbeam-utils = { version = "0.8" }
crypto-bigint = { version = "0.5", features = ["generic-array", "zeroize"] }
crypto-common = { version = "0.1", default-features = false, features = ["getrandom", "std"] }
@@ -90,6 +91,7 @@ prost = { version = "0.12", features = ["prost-derive"] }
rand-274715c4dabd11b0 = { package = "rand", version = "0.9" }
rand-c38e5c1d305a1b54 = { package = "rand", version = "0.8", features = ["small_rng"] }
redis = { version = "1", features = ["cluster-async", "tokio-rustls-comp"] }
+regex = { version = "1" }
regex-automata = { version = "0.4", default-features = false, features = ["dfa-build", "dfa-onepass", "hybrid", "meta", "nfa-backtrack", "perf-inline", "perf-literal", "std", "unicode"] }
regex-syntax = { version = "0.8" }
reqwest = { version = "0.13", features = ["form", "gzip", "hickory-dns", "json", "multipart", "query", "stream"] }
@@ -161,6 +163,7 @@ bzip2 = { version = "0.5", features = ["libbz2-rs-sys"] }
bzip2-sys = { version = "0.1", default-features = false, features = ["__disabled"] }
cc = { version = "1", default-features = false, features = ["parallel"] }
chrono = { version = "0.4", features = ["serde"] }
+chrono-tz = { version = "0.10", features = ["serde"] }
crossbeam-utils = { version = "0.8" }
crypto-bigint = { version = "0.5", features = ["generic-array", "zeroize"] }
crypto-common = { version = "0.1", default-features = false, features = ["getrandom", "std"] }
@@ -218,6 +221,7 @@ quote = { version = "1" }
rand-274715c4dabd11b0 = { package = "rand", version = "0.9" }
rand-c38e5c1d305a1b54 = { package = "rand", version = "0.8", features = ["small_rng"] }
redis = { version = "1", features = ["cluster-async", "tokio-rustls-comp"] }
+regex = { version = "1" }
regex-automata = { version = "0.4", default-features = false, features = ["dfa-build", "dfa-onepass", "hybrid", "meta", "nfa-backtrack", "perf-inline", "perf-literal", "std", "unicode"] }
regex-syntax = { version = "0.8" }
reqwest = { version = "0.13", features = ["form", "gzip", "hickory-dns", "json", "multipart", "query", "stream"] }
diff --git a/services/authentication_service/Cargo.toml b/services/authentication_service/Cargo.toml
index 8e7743065e..283d561939 100644
--- a/services/authentication_service/Cargo.toml
+++ b/services/authentication_service/Cargo.toml
@@ -28,6 +28,7 @@ rate_limit = []
return_passwordless_code = []
[dependencies]
+calendar_events = { path = "../../crates/calendar_events" }
analytics_client = { path = "../../crates/analytics_client" }
anyhow = { workspace = true }
async-stripe = { workspace = true, default-features = false, features = [
diff --git a/services/authentication_service/src/api/link/gmail.rs b/services/authentication_service/src/api/link/gmail.rs
index 6de5ea6993..8a87146497 100644
--- a/services/authentication_service/src/api/link/gmail.rs
+++ b/services/authentication_service/src/api/link/gmail.rs
@@ -5,6 +5,7 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
+use calendar_events::domain::models::google_calendar_scope_parameter;
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use macro_middleware::tracking::ClientIp;
use macro_user_id::user_id::MacroUserIdStr;
@@ -127,9 +128,15 @@ pub async fn init_gmail_link_handler(
return Err(InitGmailLinkError::TooManyInProgressLinks);
}
- let link_id = macro_db_client::in_progress_user_link::create_in_progress_user_link(
+ let authorization_scopes = format!("{GMAIL_SCOPES} {}", google_calendar_scope_parameter());
+ let requested_google_scopes: Vec<String> = authorization_scopes
+ .split_ascii_whitespace()
+ .map(ToOwned::to_owned)
+ .collect();
+ let link_id = macro_db_client::in_progress_user_link::create_in_progress_google_link(
&ctx.db,
&authorization.authorization.user.user_context.fusion_user_id,
+ &requested_google_scopes,
)
.await?;
@@ -149,22 +156,38 @@ pub async fn init_gmail_link_handler(
let redirect_uri = crate::api::oauth2::format_redirect_uri("google");
let state_str = serde_json::to_string(&state).context("failed to serialize OAuth state")?;
+ let authorization_url = google_authorization_url(
+ ctx.auth_client.google_client_id(),
+ &redirect_uri,
+ &authorization_scopes,
+ &state_str,
+ )?;
+
+ Ok(Json(InitGmailLinkResponse {
+ authorization_url: authorization_url.to_string(),
+ link_id,
+ }))
+}
+
+fn google_authorization_url(
+ client_id: &str,
+ redirect_uri: &str,
+ scopes: &str,
+ state: &str,
+) -> anyhow::Result<Url> {
let mut authorization_url =
Url::parse(GOOGLE_AUTHORIZATION_URL).context("invalid Google authorization URL")?;
authorization_url
.query_pairs_mut()
- .append_pair("client_id", ctx.auth_client.google_client_id())
- .append_pair("redirect_uri", &redirect_uri)
+ .append_pair("client_id", client_id)
+ .append_pair("redirect_uri", redirect_uri)
.append_pair("response_type", "code")
- .append_pair("scope", GMAIL_SCOPES)
- .append_pair("state", &state_str)
+ .append_pair("scope", scopes)
+ .append_pair("state", state)
.append_pair("access_type", "offline")
+ .append_pair("include_granted_scopes", "true")
.append_pair("prompt", "consent");
-
- Ok(Json(InitGmailLinkResponse {
- authorization_url: authorization_url.to_string(),
- link_id,
- }))
+ Ok(authorization_url)
}
#[tracing::instrument(skip(db, macro_user_id), err)]
diff --git a/services/authentication_service/src/api/link/gmail/test.rs b/services/authentication_service/src/api/link/gmail/test.rs
index 4567a99889..67db87bba8 100644
--- a/services/authentication_service/src/api/link/gmail/test.rs
+++ b/services/authentication_service/src/api/link/gmail/test.rs
@@ -78,3 +78,41 @@ async fn professional_features_skip_existing_inbox_check() {
);
assert!(result.is_ok());
}
+
+#[test]
+fn authorization_url_requests_incremental_calendar_access() {
+ let scopes = format!("{GMAIL_SCOPES} {}", google_calendar_scope_parameter());
+ let url = google_authorization_url(
+ "client-id",
+ "https://auth.example.com/oauth2/callback",
+ &scopes,
+ "state",
+ )
+ .unwrap();
+ let params = url
+ .query_pairs()
+ .collect::<std::collections::HashMap<_, _>>();
+
+ assert_eq!(
+ params
+ .get("include_granted_scopes")
+ .map(|value| value.as_ref()),
+ Some("true")
+ );
+ assert_eq!(
+ params.get("access_type").map(|value| value.as_ref()),
+ Some("offline")
+ );
+ assert_eq!(
+ params.get("prompt").map(|value| value.as_ref()),
+ Some("consent")
+ );
+ let granted = params.get("scope").unwrap();
+ for calendar_scope in calendar_events::domain::models::GOOGLE_CALENDAR_SCOPES {
+ assert!(
+ granted
+ .split_ascii_whitespace()
+ .any(|scope| scope == calendar_scope)
+ );
+ }
+}
diff --git a/services/authentication_service/src/api/oauth2/google.rs b/services/authentication_service/src/api/oauth2/google.rs
index 4955de75a1..da847d9b21 100644
--- a/services/authentication_service/src/api/oauth2/google.rs
+++ b/services/authentication_service/src/api/oauth2/google.rs
@@ -21,16 +21,20 @@ use crate::api::{
use fusionauth::error::FusionAuthClientError;
use fusionauth::identity_provider::{IdentityProviderLink, LinkUserRequest};
+#[cfg(test)]
+mod test;
+
async fn link_user(
ctx: &ApiContext,
identity_provider_id: &str,
code: &str,
link_id: &uuid::Uuid,
) -> Result<(), (StatusCode, String)> {
- let macro_user_id =
- macro_db_client::in_progress_user_link::get_macro_user_id_by_link_id(&ctx.db, link_id)
+ let in_progress =
+ macro_db_client::in_progress_user_link::get_in_progress_user_link(&ctx.db, link_id)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
+ let macro_user_id = in_progress.macro_user_id;
let token_response = ctx
.auth_client
@@ -131,20 +135,36 @@ async fn link_user(
}
}
- // Stash the linked email on the in_progress_user_link row so /email/init can pick it up.
- // The row is consumed and deleted by /email/init once the email_links record is created.
- macro_db_client::in_progress_user_link::set_linked_email(&ctx.db, link_id, &user_info_email)
- .await
- .map_err(|e| {
- (
- StatusCode::INTERNAL_SERVER_ERROR,
- format!("unable to record linked email on in_progress_user_link {e}"),
- )
- })?;
+ // Stash the linked identity and Google's actual grant (which may be a
+ // subset of what was requested). /email/init applies these capabilities
+ // atomically to the durable link and schedules any newly unlocked work.
+ let granted_scopes =
+ resolved_granted_scopes(&token_response.scope, in_progress.requested_google_scopes);
+ macro_db_client::in_progress_user_link::set_linked_google_grant(
+ &ctx.db,
+ link_id,
+ &user_info_email,
+ &granted_scopes,
+ )
+ .await
+ .map_err(|e| {
+ (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ format!("unable to record linked Google grant on in_progress_user_link {e}"),
+ )
+ })?;
Ok(())
}
+fn resolved_granted_scopes(returned: &str, requested: Vec<String>) -> Vec<String> {
+ if returned.trim().is_empty() {
+ requested
+ } else {
+ calendar_events::domain::models::GoogleScopeSet::parse(returned).into_vec()
+ }
+}
+
/// Replaces a stale Google grant on an existing IdP link with a freshly minted
/// refresh token. Used on reconnect, when the mailbox's FusionAuth link already
/// exists but its stored token has gone dead — a plain `link_user` no-ops and
diff --git a/services/authentication_service/src/api/oauth2/google/test.rs b/services/authentication_service/src/api/oauth2/google/test.rs
new file mode 100644
index 0000000000..d7e9feb795
--- /dev/null
+++ b/services/authentication_service/src/api/oauth2/google/test.rs
@@ -0,0 +1,21 @@
+use super::resolved_granted_scopes;
+
+#[test]
+fn uses_requested_scopes_when_google_omits_an_unchanged_scope_field() {
+ let requested = vec![
+ "https://www.googleapis.com/auth/gmail.modify".to_string(),
+ "https://www.googleapis.com/auth/calendar.events".to_string(),
+ ];
+
+ assert_eq!(resolved_granted_scopes("", requested.clone()), requested);
+}
+
+#[test]
+fn uses_google_scope_field_when_the_grant_differs_from_the_request() {
+ let requested = vec!["requested".to_string()];
+
+ assert_eq!(
+ resolved_granted_scopes("calendar gmail", requested),
+ vec!["calendar".to_string(), "gmail".to_string()]
+ );
+}
diff --git a/services/email_service/Cargo.toml b/services/email_service/Cargo.toml
index a92a19f379..a2cda81b6e 100644
--- a/services/email_service/Cargo.toml
+++ b/services/email_service/Cargo.toml
@@ -76,6 +76,7 @@ sfs_map = []
sfs_delete = []
[dependencies]
+calendar_events = { path = "../../crates/calendar_events", features = ["google", "ics", "postgres"] }
ammonia = "4.1.4"
anyhow = { workspace = true }
authentication_service_client = { path = "../../crates/authentication_service_client" }
@@ -144,6 +145,7 @@ redis = { workspace = true, features = [
] }
regex = { workspace = true }
reqwest = { workspace = true, features = ["stream"] }
+rootcause = { workspace = true }
s3_client = { path = "../../crates/s3_client" }
schemars = { workspace = true }
scraper = { workspace = true }
diff --git a/services/email_service/src/api/calendar_watch.rs b/services/email_service/src/api/calendar_watch.rs
new file mode 100644
index 0000000000..13fde2573e
--- /dev/null
+++ b/services/email_service/src/api/calendar_watch.rs
@@ -0,0 +1,61 @@
+//! Google Calendar push notification webhook.
+//!
+//! Google delivers content-free notifications here for every calendar with
+//! an open `events.watch` channel. The handler verifies the shared channel
+//! token minted at channel creation, then re-arms the watched inbox's sync
+//! job; the regular poll remains the backstop for dropped notifications, so
+//! unmatched or failed notifications are acknowledged rather than retried.
+
+use axum::Router;
+use axum::extract::State;
+use axum::http::{HeaderMap, StatusCode};
+use axum::routing::post;
+
+use crate::api::context::ApiContext;
+use email_service::pubsub::context::calendar_watch_config;
+
+/// Build the unauthenticated watch notification router.
+pub fn router() -> Router<ApiContext> {
+ Router::new().route("/notifications", post(handler))
+}
+
+fn header<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {
+ headers.get(name).and_then(|value| value.to_str().ok())
+}
+
+#[tracing::instrument(skip_all)]
+async fn handler(State(ctx): State<ApiContext>, headers: HeaderMap) -> StatusCode {
+ let Some(config) = calendar_watch_config() else {
+ return StatusCode::NOT_FOUND;
+ };
+ if header(&headers, "x-goog-channel-token") != Some(config.token.as_str()) {
+ return StatusCode::FORBIDDEN;
+ }
+ if header(&headers, "x-goog-resource-state") == Some("sync") {
+ return StatusCode::OK;
+ }
+ let (Some(channel_id), Some(resource_id)) = (
+ header(&headers, "x-goog-channel-id"),
+ header(&headers, "x-goog-resource-id"),
+ ) else {
+ return StatusCode::BAD_REQUEST;
+ };
+ match ctx
+ .calendar_service
+ .handle_watch_notification(channel_id, resource_id)
+ .await
+ {
+ Ok(matched) => {
+ if !matched {
+ tracing::debug!(
+ channel_id,
+ "calendar notification matched no active channel"
+ );
+ }
+ }
+ Err(error) => {
+ tracing::warn!(error = ?error, channel_id, "failed to apply calendar watch notification");
+ }
+ }
+ StatusCode::OK
+}
diff --git a/services/email_service/src/api/context.rs b/services/email_service/src/api/context.rs
index 767e11fedf..ef068f00c1 100644
--- a/services/email_service/src/api/context.rs
+++ b/services/email_service/src/api/context.rs
@@ -1,4 +1,5 @@
use axum::extract::FromRef;
+use calendar_events::{domain::service::CalendarService, outbound::pg::PgCalendarRepository};
use document_storage_service_client::DocumentStorageServiceClient;
use email::{
domain::service::EmailServiceImpl,
@@ -26,6 +27,7 @@ use system_properties::{PgSystemPropertiesRepository, SystemPropertiesServiceImp
use tokio_util::task::TaskTracker;
pub(crate) type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+pub(crate) type CalendarGrantService = CalendarService<PgCalendarRepository>;
pub(crate) type EmailEntityAccessService = EntityAccessServiceImpl<PgAccessRepository>;
pub(crate) type EmailEntityAccessManagementService =
EntityAccessManagementServiceImpl<entity_access_management::outbound::PgRepository>;
@@ -63,4 +65,5 @@ pub(crate) struct ApiContext {
EmailThreadRouterState<EmailSvc, EmailEntityAccessService, AuthorizationService>,
pub gmail_token_state: GmailTokenState<GmailTokenProviderImpl>,
pub macro_event_broker: Arc<EmailEventBroker>,
+ pub calendar_service: Arc<CalendarGrantService>,
}
diff --git a/services/email_service/src/api/email/init.rs b/services/email_service/src/api/email/init.rs
index 645bcb1e01..b78a71fae9 100644
--- a/services/email_service/src/api/email/init.rs
+++ b/services/email_service/src/api/email/init.rs
@@ -1,5 +1,5 @@
use crate::api::ApiContext;
-use crate::api::context::AuthorizationService;
+use crate::api::context::{AuthorizationService, CalendarGrantService};
use crate::utils::extract_email_with_response;
use anyhow::Context;
use authentication_service_client::error::AuthServiceClientError;
@@ -8,6 +8,7 @@ use axum::{
http::StatusCode,
response::{IntoResponse, Json, Response},
};
+use calendar_events::domain::models::GoogleScopeSet;
use email::domain::events::{EmailMacroEvent, LinkConnectedMetadata};
use email::domain::models::UserProvider;
use email::domain::ports::EmailRepo;
@@ -19,7 +20,7 @@ use macro_user_id::email::EmailStr;
use macro_user_id::user_id::MacroUserIdStr;
use model::response::ErrorResponse;
use models_email::email::service::backfill::{
- BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
+ BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
};
use models_email::gmail::error::GmailError;
use models_email::service::link;
@@ -249,6 +250,7 @@ async fn init_user(
) -> Result<Response, InitError> {
let macro_user_id = authorization.authorization.user.macro_user_id.clone();
let user_context = authorization.authorization.user.user_context.clone();
+ let mut completed_google_grant: Option<Vec<String>> = None;
tracing::info!(user_id = %user_context.user_id, ?link_id, "Init called");
let pg_repo = EmailPgRepo::new(ctx.db.clone());
@@ -258,6 +260,7 @@ async fn init_user(
macro_db_client::in_progress_user_link::get_in_progress_user_link(&ctx.db, &link_id)
.await
.context("Failed to fetch in_progress_user_link")?;
+ completed_google_grant = Some(in_progress.granted_google_scopes.clone());
if in_progress.macro_user_id.to_string() != user_context.fusion_user_id {
return Err(InitError::BadRequest(
@@ -307,9 +310,8 @@ async fn init_user(
if let Some(child_link) = existing_child_link {
// Cross-user delegation: the child already connected their own inbox under
// their own fusion id. Link primary (caller) → child so primary can read it.
- // The edge insert and in_progress consumption are a coupled pair, so commit
- // them atomically: any failure rolls back, leaving the in_progress row for a
- // retry.
+ // Keep the in-progress grant until the calendar grant and its outbox work
+ // are durable. The edge insert is idempotent if a retry reaches this path.
let mut tx = ctx
.db
.begin()
@@ -325,16 +327,18 @@ async fn init_user(
.await
.context("Failed to insert macro_user_links edge")?;
- macro_db_client::in_progress_user_link::delete_in_progress_user_link(
- &mut *tx, &link_id,
- )
- .await
- .context("Failed to delete in_progress_user_link after graph delegation")?;
-
tx.commit()
.await
.context("Failed to commit graph delegation transaction")?;
+ apply_and_consume_calendar_grant(
+ &ctx,
+ child_link.id,
+ link_id,
+ &in_progress.granted_google_scopes,
+ )
+ .await?;
+
return Ok((
StatusCode::OK,
Json(InitResponse {
@@ -367,8 +371,8 @@ async fn init_user(
fetch_gmail_token_for_email(&ctx, &child_fusion_id, &linked_email).await?;
let watch_response = register_watch_recovering(&ctx.gmail_client, &gmail_token).await?;
- // All writes commit atomically so a partial failure leaves no dangling link,
- // edge, or consumed in_progress row.
+ // The link and delegation edge commit atomically. The in-progress grant is
+ // consumed only after the calendar grant and outbox work are durable.
let mut tx = ctx
.db
.begin()
@@ -393,12 +397,6 @@ async fn init_user(
.await
.context("Failed to insert macro_user_links edge")?;
- macro_db_client::in_progress_user_link::delete_in_progress_user_link(
- &mut *tx, &link_id,
- )
- .await
- .context("Failed to delete in_progress_user_link after self-link bootstrap")?;
-
tx.commit()
.await
.context("Failed to commit self-link bootstrap transaction")?;
@@ -408,7 +406,7 @@ async fn init_user(
(link, linked_email)
} else {
// Data-source path (same-user re-link or brand-new email with no prior signup).
- if pg_repo
+ if let Some(existing_link) = pg_repo
.link_by_fusionauth_email_provider(
&user_context.fusion_user_id,
&linked_email,
@@ -416,9 +414,29 @@ async fn init_user(
)
.await
.context("Failed to check existing link by email")?
- .is_some()
{
- return Err(InitError::AlreadyInitialized);
+ let applied = apply_and_consume_calendar_grant(
+ &ctx,
+ existing_link.id,
+ link_id,
+ &in_progress.granted_google_scopes,
+ )
+ .await?;
+ tracing::info!(
+ link_id = %existing_link.id,
+ grant_version = applied.grant_version,
+ changed = applied.changed,
+ calendar_jobs = applied.jobs.len(),
+ "Applied Google permission upgrade to existing inbox"
+ );
+ return Ok((
+ StatusCode::OK,
+ Json(InitResponse {
+ link_id: existing_link.id,
+ backfill_job_id: None,
+ }),
+ )
+ .into_response());
}
// The linked email is not itself a macro user, but another macro user may
@@ -470,12 +488,6 @@ async fn init_user(
.await
.context("Failed to promote link to shared inbox")?;
- macro_db_client::in_progress_user_link::delete_in_progress_user_link(
- &mut *tx, &link_id,
- )
- .await
- .context("Failed to delete in_progress_user_link after shared-inbox promotion")?;
-
tx.commit()
.await
.context("Failed to commit shared-inbox promotion transaction")?;
@@ -529,6 +541,14 @@ async fn init_user(
}
}
+ apply_and_consume_calendar_grant(
+ &ctx,
+ promoted.link_id,
+ link_id,
+ &in_progress.granted_google_scopes,
+ )
+ .await?;
+
return Ok((
StatusCode::OK,
Json(InitResponse {
@@ -563,13 +583,6 @@ async fn init_user(
.await
.context("Failed to commit link transaction")?;
- macro_db_client::in_progress_user_link::delete_in_progress_user_link(&ctx.db, &link_id)
- .await
- .inspect_err(|e| {
- tracing::error!(error=?e, ?link_id, "Failed to delete in_progress_user_link after init");
- })
- .ok();
-
(link, linked_email)
}
} else {
@@ -616,6 +629,12 @@ async fn init_user(
(link, email)
};
+ if let (Some(scopes), Some(link_id)) = (completed_google_grant.as_deref(), link_id) {
+ apply_and_consume_calendar_grant(&ctx, link.id, link_id, scopes).await?;
+ } else if completed_google_grant.is_none() {
+ apply_grant_discovered_from_token(&ctx, &link).await;
+ }
+
// Concurrent /email/init calls for the same inbox upsert the same link (ON CONFLICT)
// and would each enqueue a backfill. Reuse an in-flight backfill if one already exists
// for this link rather than starting a duplicate. This cheap pre-check covers the common
@@ -705,24 +724,12 @@ async fn init_user(
{
tracing::error!(error = ?e, backfill_id = %backfill_job.id, "Failed to enqueue backfill message");
- let db_pool = ctx.db.clone();
- let job_id = backfill_job.id;
- tokio::spawn(async move {
- if let Err(update_err) =
- email_db_client::backfill::job::update::update_backfill_job_status(
- &db_pool,
- job_id,
- BackfillJobStatus::Failed,
- )
- .await
- {
- tracing::error!(
- error = ?update_err,
- backfill_id = %job_id,
- "Failed to update backfill job status to Failed"
- );
- }
- });
+ email_db_client::backfill::job::update::fail_backfill_job_and_calendar_extraction(
+ &ctx.db,
+ backfill_job.id,
+ )
+ .await
+ .context("Failed to persist initial backfill publication failure")?;
return Err(InitError::EnqueueError);
}
@@ -737,6 +744,118 @@ async fn init_user(
.into_response())
}
+/// Init runs on every authentication, including first-login SSO where
+/// FusionAuth performs the token exchange and the granted scopes never reach
+/// the link flow. When the link has no recorded grant generation, discover
+/// the scopes from Google's tokeninfo endpoint using the link's own token so
+/// SSO-only users still receive calendar sync. Best-effort: a failure here
+/// must never fail authentication, and the next init retries it.
+async fn apply_grant_discovered_from_token(ctx: &ApiContext, link: &link::Link) {
+ let unrecorded = sqlx::query_scalar!(
+ r#"SELECT google_grant_version = 0 AS "unrecorded!" FROM email_links WHERE id = $1"#,
+ link.id,
+ )
+ .fetch_optional(&ctx.db)
+ .await
+ .ok()
+ .flatten()
+ .unwrap_or(false);
+ if !unrecorded {
+ return;
+ }
+ let token = match fetch_gmail_token_for_email(
+ ctx,
+ &link.fusionauth_user_id,
+ link.email_address.0.as_ref(),
+ )
+ .await
+ {
+ Ok(token) => token,
+ Err(error) => {
+ tracing::warn!(error = ?error, link_id = %link.id, "skipping grant discovery: no Google token");
+ return;
+ }
+ };
+ match fetch_token_scopes(&token).await {
+ Ok(scopes) => {
+ apply_calendar_grant(ctx.calendar_service.as_ref(), link.id, &scopes)
+ .await
+ .inspect_err(|error| {
+ tracing::warn!(error = ?error, link_id = %link.id, "failed to apply discovered Google grant");
+ })
+ .ok();
+ }
+ Err(error) => {
+ tracing::warn!(error = ?error, link_id = %link.id, "failed to discover Google token scopes");
+ }
+ }
+}
+
+/// Ask Google which scopes an access token actually carries.
+async fn fetch_token_scopes(access_token: &str) -> anyhow::Result<Vec<String>> {
+ #[derive(serde::Deserialize)]
+ struct TokenInfo {
+ scope: String,
+ }
+ // One shared client with a hard timeout: discovery runs on the
+ // authentication path, so a hung Google response must not stall login.
+ static TOKENINFO_CLIENT: std::sync::LazyLock<reqwest::Client> =
+ std::sync::LazyLock::new(|| {
+ reqwest::Client::builder()
+ .timeout(std::time::Duration::from_secs(10))
+ .build()
+ .expect("static reqwest client configuration is valid")
+ });
+ let info: TokenInfo = TOKENINFO_CLIENT
+ .post("https://www.googleapis.com/oauth2/v3/tokeninfo")
+ .form(&[("access_token", access_token)])
+ .send()
+ .await?
+ .error_for_status()?
+ .json()
+ .await?;
+ Ok(info
+ .scope
+ .split_ascii_whitespace()
+ .map(ToOwned::to_owned)
+ .collect())
+}
+
+async fn apply_calendar_grant(
+ calendar_service: &CalendarGrantService,
+ email_link_id: Uuid,
+ granted_scopes: &[String],
+) -> Result<calendar_events::domain::models::AppliedGoogleGrant, InitError> {
+ calendar_service
+ .apply_google_grant(
+ email_link_id,
+ GoogleScopeSet::from_scopes(granted_scopes.iter().cloned()),
+ )
+ .await
+ .map_err(|error| {
+ InitError::DatabaseError(anyhow::anyhow!(
+ "failed to apply Google grant to email link: {error:?}"
+ ))
+ })
+}
+
+async fn apply_and_consume_calendar_grant(
+ ctx: &ApiContext,
+ email_link_id: Uuid,
+ in_progress_link_id: Uuid,
+ granted_scopes: &[String],
+) -> Result<calendar_events::domain::models::AppliedGoogleGrant, InitError> {
+ let applied =
+ apply_calendar_grant(ctx.calendar_service.as_ref(), email_link_id, granted_scopes).await?;
+ macro_db_client::in_progress_user_link::delete_in_progress_user_link(
+ &ctx.db,
+ &in_progress_link_id,
+ )
+ .await
+ .context("Failed to consume applied Google grant")?;
+ Ok(applied)
+}
+
/// Registers a Gmail watch, recovering from the one-watch-per-mailbox limit. A watch
/// left over from a prior connect (e.g. a disconnect that could not reach Gmail to stop
/// it) makes a fresh registration fail with 400 "Only one user push notification client
diff --git a/services/email_service/src/api/email/links/resync.rs b/services/email_service/src/api/email/links/resync.rs
index f2499d2b2a..a4066b988b 100644
--- a/services/email_service/src/api/email/links/resync.rs
+++ b/services/email_service/src/api/email/links/resync.rs
@@ -6,7 +6,7 @@ use axum::response::{IntoResponse, Json, Response};
use macro_authorization::{MacroAuthorizationExtractor, UserOrInternal};
use model::response::ErrorResponse;
use models_email::email::service::backfill::{
- BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
+ BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
};
use utoipa::ToSchema;
use uuid::Uuid;
@@ -100,15 +100,12 @@ pub async fn resync_link_handler(
.enqueue_email_backfill_message(ps_message)
.await
{
- if let Err(update_err) = email_db_client::backfill::job::update::update_backfill_job_status(
+ email_db_client::backfill::job::update::fail_backfill_job_and_calendar_extraction(
&ctx.db,
backfill_job.id,
- BackfillJobStatus::Failed,
)
.await
- {
- tracing::error!(error=?update_err, backfill_id=%backfill_job.id, "failed to mark backfill job failed");
- }
+ .context("failed to persist backfill publication failure")?;
return Err(e.context("failed to enqueue backfill message").into());
}
diff --git a/services/email_service/src/api/internal/gmail/create.rs b/services/email_service/src/api/internal/gmail/create.rs
index 63d7ed0961..78b75957c3 100644
--- a/services/email_service/src/api/internal/gmail/create.rs
+++ b/services/email_service/src/api/internal/gmail/create.rs
@@ -7,7 +7,7 @@ use axum::{
use macro_authorization::{InternalOnly, MacroAuthorizationExtractor};
use model::response::ErrorResponse;
use models_email::email::service::backfill::{
- BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
+ BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
};
use sqlx::types::Uuid;
use utoipa::ToSchema;
@@ -142,37 +142,44 @@ pub async fn handler(
}),
};
- ctx.sqs_client.enqueue_email_backfill_message(ps_message)
+ if let Err(error) = ctx
+ .sqs_client
+ .enqueue_email_backfill_message(ps_message)
.await
- .map_err(|e| {
- // Log the error
- tracing::error!(error = ?e, backfill_id = %backfill_job.id, "Failed to enqueue backfill message");
-
- // Update the job status to Failed
- let db_pool = ctx.db.clone();
- let job_id = backfill_job.id;
- tokio::spawn(async move {
- if let Err(update_err) = email_db_client::backfill::job::update::update_backfill_job_status(
- &db_pool,
- job_id,
- BackfillJobStatus::Failed,
- ).await {
- tracing::error!(
- error = ?update_err,
- backfill_id = %job_id,
- "Failed to update backfill job status to Failed"
- );
- }
- });
-
+ {
+ tracing::error!(error = ?error, backfill_id = %backfill_job.id, "Failed to enqueue backfill message");
+ email_db_client::backfill::job::update::fail_backfill_job_and_calendar_extraction(
+ &ctx.db,
+ backfill_job.id,
+ )
+ .await
+ .inspect_err(|update_error| {
+ tracing::error!(
+ error = ?update_error,
+ backfill_id = %backfill_job.id,
+ "Failed to persist backfill publication failure"
+ );
+ })
+ .map_err(|_| {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
- message: format!("Failed to enqueue backfill message for link {}", link_id).into(),
+ message: format!("Failed to clean up backfill job for link {}", link_id)
+ .into(),
}),
)
.into_response()
})?;
+
+ return Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(ErrorResponse {
+ message: format!("Failed to enqueue backfill message for link {}", link_id)
+ .into(),
+ }),
+ )
+ .into_response());
+ }
}
Ok((
StatusCode::OK,
diff --git a/services/email_service/src/api/mod.rs b/services/email_service/src/api/mod.rs
index c638bc0be2..f139ef057c 100644
--- a/services/email_service/src/api/mod.rs
+++ b/services/email_service/src/api/mod.rs
@@ -9,6 +9,7 @@ use utoipa_swagger_ui::SwaggerUi;
// Routes
mod health;
+mod calendar_watch;
mod email;
// Misc
@@ -52,4 +53,5 @@ fn api_router(state: ApiContext) -> Router<ApiContext> {
.nest("/email", email::router(state))
.nest("/gmail", gmail::router())
.nest("/internal", internal::router())
+ .nest("/calendar", calendar_watch::router())
}
diff --git a/services/email_service/src/backfill_completion_service.rs b/services/email_service/src/backfill_completion_service.rs
new file mode 100644
index 0000000000..c08b9e3af4
--- /dev/null
+++ b/services/email_service/src/backfill_completion_service.rs
@@ -0,0 +1,596 @@
+use crate::pubsub::context::PubSubContext;
+use crate::pubsub::util::cg_refresh_email;
+use contacts::domain::models::messages::ContactConnection;
+use contacts::domain::ports::ContactsIngress;
+use email_db_client::backfill::job::update::BackfillCompletion;
+use macro_user_id::user_id::MacroUserIdStr;
+use models_email::api::refresh::{BackfillStatus, RefreshEmailEvent};
+use models_email::db::address::EmailRecipientType;
+use models_email::service::attachment::{
+ AttachmentUploadArgs, AttachmentUploadDestination, AttachmentUploadMetadata,
+};
+use models_email::service::backfill::{
+ BackfillAttachmentPayload, BackfillMessagePayload, BackfillOperation, BackfillPubsubMessage,
+ JobScopedPayload, UpdateMetadataPayload,
+};
+use models_email::service::link::Link;
+use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
+use uuid::Uuid;
+
+/// Emit a `refresh_email` event once per this many completed threads, plus on
+/// the first completed thread and at job completion, rather than per-thread.
+/// The first-thread emit ensures backfills smaller than this interval still
+/// surface a progress event before completing.
+const REFRESH_EMAIL_THREAD_INTERVAL: i32 = 50;
+
+#[cfg(test)]
+mod test;
+
+/// called when a thread has completed processing. checks if it is the last thread to be processed
+/// for the job, and if so, performs the necessary actions for job completion.
+#[tracing::instrument(skip(ctx))]
+pub async fn incr_completed_threads(
+ ctx: &PubSubContext,
+ link: &Link,
+ job_id: Uuid,
+) -> Result<(), ProcessingError> {
+ let progress = ctx
+ .redis_client
+ .incr_completed_threads(job_id)
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::RedisQueryFailed,
+ source: e.context("Failed to increment completed thread count"),
+ })
+ })?;
+
+ if progress.job_complete {
+ tracing::info!(
+ job_id = job_id.to_string(),
+ "All threads for job have been processed"
+ );
+ handle_job_completed(ctx, job_id, None).await?;
+ } else if progress.completed_threads == 1
+ || progress.completed_threads % REFRESH_EMAIL_THREAD_INTERVAL == 0
+ {
+ cg_refresh_email(
+ &ctx.connection_gateway_client,
+ link.macro_id.as_ref(),
+ RefreshEmailEvent::BackfillProgress {
+ link_id: link.id,
+ status: BackfillStatus::Progress,
+ completed_threads: progress.completed_threads,
+ total_threads: progress.total_threads,
+ },
+ )
+ .await;
+ }
+
+ Ok(())
+}
+
+/// If this message was the last one in the thread to be processed, proceed to next stage of backfill for thread
+#[tracing::instrument(skip(ctx))]
+pub async fn incr_completed_messages(
+ ctx: &PubSubContext,
+ link: &Link,
+ job_id: Uuid,
+ p: &BackfillMessagePayload,
+) -> Result<(), ProcessingError> {
+ let thread_backfill_complete = ctx
+ .redis_client
+ .incr_completed_messages(job_id, &p.thread_provider_id)
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::RedisQueryFailed,
+ source: e.context(format!(
+ "Handle completed message for {} failed",
+ p.message_provider_id
+ )),
+ })
+ })?;
+
+ if thread_backfill_complete {
+ handle_thread_completed(ctx, link, job_id, p).await?;
+ }
+
+ Ok(())
+}
+
+/// performs actions when all threads and messages have been backfilled for the user.
+#[tracing::instrument(skip(ctx))]
+pub(crate) async fn handle_job_completed(
+ ctx: &PubSubContext,
+ job_id: Uuid,
+ init_lease_token: Option<Uuid>,
+) -> Result<(), ProcessingError> {
+ tracing::info!("Backfill complete for job {}", job_id);
+ let outcome =
+ email_db_client::backfill::job::update::complete_backfill_job_and_calendar_extraction(
+ &ctx.db,
+ job_id,
+ init_lease_token,
+ )
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to complete email and calendar backfill jobs"),
+ })
+ })?;
+
+ completion_result(job_id, outcome)
+}
+
+fn completion_result(job_id: Uuid, outcome: BackfillCompletion) -> Result<(), ProcessingError> {
+ match outcome {
+ BackfillCompletion::Completed | BackfillCompletion::AlreadyTerminal => Ok(()),
+ BackfillCompletion::LeaseLost => Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::EmailBackfillInitBusy,
+ source: anyhow::anyhow!("email backfill completion lease was lost for job {job_id}"),
+ })),
+ BackfillCompletion::NotFound => Err(ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::BackfillJobNotFound,
+ source: anyhow::anyhow!("email backfill job {job_id} was not found"),
+ })),
+ }
+}
+
+/// Runs durable completion effects published from the completion outbox.
+#[tracing::instrument(skip(ctx))]
+pub(crate) async fn finalize_backfill(
+ ctx: &PubSubContext,
+ link_id: Uuid,
+ job_id: Uuid,
+) -> Result<(), ProcessingError> {
+ let Some(lease_token) =
+ email_db_client::backfill::job::update::claim_completion_effects(&ctx.db, job_id)
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to claim backfill completion effects"),
+ })
+ })?
+ else {
+ let pending =
+ email_db_client::backfill::job::update::completion_effects_pending(&ctx.db, job_id)
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to load backfill completion effects"),
+ })
+ })?;
+ if !pending {
+ return Ok(());
+ }
+ return Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: anyhow::anyhow!("backfill completion effects are already leased"),
+ }));
+ };
+
+ let result = finalize_claimed_backfill(ctx, link_id, job_id, lease_token).await;
+ if result.is_err() {
+ email_db_client::backfill::job::update::release_completion_effects(
+ &ctx.db,
+ job_id,
+ lease_token,
+ )
+ .await
+ .inspect_err(|error| {
+ tracing::error!(
+ error = ?error,
+ job_id = %job_id,
+ "failed to release backfill completion-effects lease"
+ );
+ })
+ .ok();
+ }
+ result
+}
+
+async fn finalize_claimed_backfill(
+ ctx: &PubSubContext,
+ link_id: Uuid,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> Result<(), ProcessingError> {
+ let Some(job) = email_db_client::backfill::job::get::get_backfill_job(&ctx.db, job_id)
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to load completed backfill job"),
+ })
+ })?
+ else {
+ return Ok(());
+ };
+ match job.status {
+ models_email::service::backfill::BackfillJobStatus::Complete => {}
+ // A job that failed or was cancelled after its completion message
+ // published will never become Complete; retrying such a delivery
+ // only burns receives, so retire its effects instead.
+ models_email::service::backfill::BackfillJobStatus::Failed
+ | models_email::service::backfill::BackfillJobStatus::Cancelled => {
+ tracing::warn!(%job_id, status = ?job.status, "retiring completion effects for a terminal, non-complete job");
+ return retire_completion_effects(ctx, job_id, lease_token).await;
+ }
+ models_email::service::backfill::BackfillJobStatus::Init
+ | models_email::service::backfill::BackfillJobStatus::InProgress => {
+ return Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: anyhow::anyhow!(
+ "backfill completion effects published before job completion"
+ ),
+ }));
+ }
+ }
+ let Some(canonical_link_id) = job.link_id else {
+ return retire_completion_effects(ctx, job_id, lease_token).await;
+ };
+ if canonical_link_id != link_id {
+ tracing::warn!(
+ message_link_id = %link_id,
+ canonical_link_id = %canonical_link_id,
+ "using the completed backfill job's canonical link"
+ );
+ }
+ let Some(link) = email_db_client::links::get::fetch_link_by_id(&ctx.db, canonical_link_id)
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to load link for backfill completion"),
+ })
+ })?
+ else {
+ return retire_completion_effects(ctx, job_id, lease_token).await;
+ };
+
+ let effects = async {
+ handle_attachment_upload(ctx, &link, job_id)
+ .await
+ .map_err(completion_effect_retry)?;
+ handle_contacts_sync(ctx, &link)
+ .await
+ .map_err(completion_effect_retry)?;
+ cg_refresh_email(
+ &ctx.connection_gateway_client,
+ link.macro_id.as_ref(),
+ RefreshEmailEvent::BackfillProgress {
+ link_id: canonical_link_id,
+ status: BackfillStatus::Complete,
+ completed_threads: job.total_threads,
+ total_threads: job.total_threads,
+ },
+ )
+ .await;
+ Ok(())
+ };
+ tokio::pin!(effects);
+ let lease_heartbeat = maintain_completion_effects_lease(ctx.db.clone(), job_id, lease_token);
+ tokio::pin!(lease_heartbeat);
+ tokio::select! {
+ biased;
+ lease_result = &mut lease_heartbeat => lease_result?,
+ effects_result = &mut effects => effects_result?,
+ }
+
+ retire_completion_effects(ctx, job_id, lease_token).await
+}
+
+async fn maintain_completion_effects_lease(
+ db: sqlx::PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> Result<(), ProcessingError> {
+ loop {
+ tokio::time::sleep(std::time::Duration::from_secs(30)).await;
+ let renewed = email_db_client::backfill::job::update::renew_completion_effects(
+ &db,
+ job_id,
+ lease_token,
+ )
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to renew backfill completion-effects lease"),
+ })
+ })?;
+ if !renewed {
+ return Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: anyhow::anyhow!("backfill completion-effects lease was lost"),
+ }));
+ }
+ }
+}
+
+async fn retire_completion_effects(
+ ctx: &PubSubContext,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> Result<(), ProcessingError> {
+ email_db_client::backfill::job::update::mark_completion_effects_complete(
+ &ctx.db,
+ job_id,
+ lease_token,
+ )
+ .await
+ .map_err(|error| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error.context("Failed to finish backfill completion effects"),
+ })
+ })
+}
+
+fn completion_effect_retry(error: ProcessingError) -> ProcessingError {
+ match error {
+ ProcessingError::NonRetryable(detail) => ProcessingError::Retryable(detail),
+ error => error,
+ }
+}
+
+#[tracing::instrument(skip(ctx))]
+async fn handle_attachment_upload(
+ ctx: &PubSubContext,
+ link: &Link,
+ job_id: Uuid,
+) -> Result<(), ProcessingError> {
+ if cfg!(not(feature = "attachment_upload")) {
+ return Ok(());
+ }
+
+ let attachments =
+ email_db_client::attachments::provider::upload::fetch_job_attachments_for_backfill(
+ &ctx.db, link.id,
+ )
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to fetch job attachment backfill metadata".to_string()),
+ })
+ })?;
+
+ if !attachments.is_empty() {
+ tracing::debug!(
+ "Found {} condition 5 attachments to backfill for job {}",
+ attachments.len(),
+ job_id
+ );
+
+ send_attachment_backfill_messages(ctx, link.id, job_id, attachments).await?;
+ }
+
+ Ok(())
+}
+
+#[tracing::instrument(skip(ctx))]
+async fn handle_contacts_sync(ctx: &PubSubContext, link: &Link) -> Result<(), ProcessingError> {
+ if cfg!(not(feature = "contacts_sync")) {
+ return Ok(());
+ }
+
+ let email_addresses =
+ email_db_client::contacts::get::fetch_contacts_emails_by_link_id(&ctx.db, link.id)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to fetch contact email addresses".to_string()),
+ })
+ })?;
+
+ let length = email_addresses.len();
+
+ tracing::info!(
+ "Populating {} contacts for macro email {}",
+ length,
+ link.macro_id
+ );
+
+ let connections = email_addresses
+ .iter()
+ .map(|email| {
+ MacroUserIdStr::try_from_email(email)
+ .map(|contact| ContactConnection::new(link.macro_id.clone(), contact))
+ })
+ .collect::<Result<Vec<_>, _>>()
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::SqsEnqueueFailed,
+ source: anyhow::anyhow!(e).context("invalid user email for contacts"),
+ })
+ })?;
+
+ ctx.contacts_ingress
+ .enqueue_contact_connections(connections)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::SqsEnqueueFailed,
+ source: anyhow::anyhow!("{e:?}").context(format!(
+ "Failed to enqueue contacts message for {}",
+ email_addresses.join(", ")
+ )),
+ })
+ })?;
+
+ tracing::info!(
+ "Successfully populated {} contacts for macro email {}",
+ length,
+ link.macro_id
+ );
+
+ Ok(())
+}
+
+/// when a thread is done being backfilled, update its metadata and backfill its attachments.
+#[tracing::instrument(skip(ctx))]
+async fn handle_thread_completed(
+ ctx: &PubSubContext,
+ link: &Link,
+ job_id: Uuid,
+ p: &BackfillMessagePayload,
+) -> Result<(), ProcessingError> {
+ let new_payload = UpdateMetadataPayload {
+ thread_provider_id: p.thread_provider_id.clone(),
+ thread_db_id: p.thread_db_id,
+ };
+
+ let ps_message = BackfillPubsubMessage {
+ backfill_operation: BackfillOperation::UpdateThreadMetadata(JobScopedPayload {
+ link_id: link.id,
+ job_id,
+ payload: new_payload,
+ }),
+ };
+
+ ctx.sqs_client
+ .enqueue_email_backfill_message(ps_message)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::SqsEnqueueFailed,
+ source: e.context("Failed to enqueue metadata message".to_string()),
+ })
+ })?;
+
+ handle_thread_attachment_upload(ctx, link, job_id, p.thread_db_id).await?;
+
+ Ok(())
+}
+
+/// Creates BackfillAttachmentPayload messages and enqueues them via SQS for the given attachments
+#[tracing::instrument(skip(ctx, attachments))]
+async fn send_attachment_backfill_messages(
+ ctx: &PubSubContext,
+ link_id: Uuid,
+ job_id: Uuid,
+ attachments: Vec<AttachmentUploadMetadata>,
+) -> Result<(), ProcessingError> {
+ if attachments.is_empty() {
+ return Ok(());
+ }
+
+ let message_ids = attachments
+ .iter()
+ .map(|a| a.message_db_id)
+ .collect::<Vec<_>>();
+
+ let message_recipients =
+ email_db_client::contacts::get::fetch_db_recipients_in_bulk(&ctx.db, &message_ids)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context(
+ "Failed to fetch db recipients for thread attachment backfill".to_string(),
+ ),
+ })
+ })?;
+
+ for attachment in attachments {
+ // get the email addresses of the recipients of the message
+ let recipient_emails: Vec<String> = message_recipients
+ .get(&attachment.message_db_id)
+ .map(|v| v.as_slice())
+ .unwrap_or(&[])
+ .iter()
+ .filter(|(_, recipient_type)| *recipient_type == EmailRecipientType::To)
+ .filter_map(|(contact, _)| contact.email_address.clone())
+ .collect();
+
+ let upload_destination = if matches!(
+ attachment.mime_type.split('/').next(),
+ Some("image" | "video")
+ ) {
+ AttachmentUploadDestination::Sfs
+ } else {
+ AttachmentUploadDestination::Dss
+ };
+
+ let attachment_upload_args = AttachmentUploadArgs {
+ recipient_emails,
+ attachment_metadata: attachment,
+ backfill: true,
+ upload_destination,
+ };
+
+ let new_payload = BackfillAttachmentPayload {
+ metadata: attachment_upload_args,
+ };
+
+ let ps_message = BackfillPubsubMessage {
+ backfill_operation: BackfillOperation::BackfillAttachment(JobScopedPayload {
+ link_id,
+ job_id,
+ payload: new_payload,
+ }),
+ };
+
+ ctx.sqs_client
+ .enqueue_email_backfill_message(ps_message)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::SqsEnqueueFailed,
+ source: e.context("Failed to enqueue attachment backfill message".to_string()),
+ })
+ })?;
+ }
+
+ Ok(())
+}
+
+#[tracing::instrument(skip(ctx))]
+async fn handle_thread_attachment_upload(
+ ctx: &PubSubContext,
+ link: &Link,
+ job_id: Uuid,
+ thread_db_id: Uuid,
+) -> Result<(), ProcessingError> {
+ if cfg!(not(feature = "attachment_upload")) {
+ return Ok(());
+ }
+
+ let (attachments, attachments2) = tokio::try_join!(
+ email_db_client::attachments::provider::upload::thread_document_atts_for_backfill(
+ &ctx.db,
+ thread_db_id,
+ ),
+ email_db_client::attachments::provider::upload::thread_media_atts_for_backfill(
+ &ctx.db,
+ thread_db_id,
+ )
+ )
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to fetch thread attachment backfill metadata".to_string()),
+ })
+ })?;
+
+ let mut all_attachments = attachments;
+ all_attachments.extend(attachments2);
+
+ if !all_attachments.is_empty() {
+ tracing::debug!(
+ "Found {} attachments to backfill for thread {}",
+ all_attachments.len(),
+ thread_db_id
+ );
+
+ send_attachment_backfill_messages(ctx, link.id, job_id, all_attachments).await?;
+ }
+
+ Ok(())
+}
diff --git a/services/email_service/src/backfill_completion_service/test.rs b/services/email_service/src/backfill_completion_service/test.rs
new file mode 100644
index 0000000000..9391b00f54
--- /dev/null
+++ b/services/email_service/src/backfill_completion_service/test.rs
@@ -0,0 +1,23 @@
+use super::*;
+
+#[test]
+fn lease_loss_is_retryable_and_terminal_completion_is_idempotent() {
+ let job_id = Uuid::new_v4();
+
+ assert!(completion_result(job_id, BackfillCompletion::Completed).is_ok());
+ assert!(completion_result(job_id, BackfillCompletion::AlreadyTerminal).is_ok());
+ assert!(matches!(
+ completion_result(job_id, BackfillCompletion::LeaseLost),
+ Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::EmailBackfillInitBusy,
+ ..
+ }))
+ ));
+ assert!(matches!(
+ completion_result(job_id, BackfillCompletion::NotFound),
+ Err(ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::BackfillJobNotFound,
+ ..
+ }))
+ ));
+}
diff --git a/services/email_service/src/backfill_init_service.rs b/services/email_service/src/backfill_init_service.rs
new file mode 100644
index 0000000000..d2bb1be3a3
--- /dev/null
+++ b/services/email_service/src/backfill_init_service.rs
@@ -0,0 +1,226 @@
+use crate::backfill_completion_service;
+use crate::pubsub::context::PubSubContext;
+use crate::pubsub::util::{CheckGmailRateLimitArgs, check_gmail_rate_limit};
+use crate::util::process_pre_insert::sync_labels::sync_labels;
+use crate::util::sync_contacts::sync_contacts;
+use email_db_client::backfill::job::update::{InitLeaseClaim, InitLeaseRenewal};
+use models_email::email::service::backfill::{InitPayload, JobScopedPayload};
+use models_email::email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
+use models_email::email::service::{backfill, link};
+use models_email::gmail::operations::GmailApiOperation;
+use std::cmp::min;
+use uuid::Uuid;
+
+/// This step is invoked via the API when a new job is created.
+/// Populates total_threads value in backfill_job row, and sends the first ListThreads message.
+#[tracing::instrument(skip(ctx, access_token))]
+pub async fn init_backfill(
+ ctx: &PubSubContext,
+ access_token: &str,
+ scope: &JobScopedPayload<InitPayload>,
+ link: &link::Link,
+ backfill_job: &backfill::BackfillJob,
+) -> Result<(), ProcessingError> {
+ let lease_token =
+ match email_db_client::backfill::job::update::claim_init_lease(&ctx.db, scope.job_id)
+ .await
+ .map_err(database_error)?
+ {
+ InitLeaseClaim::Claimed(lease_token) => lease_token,
+ InitLeaseClaim::Complete => return Ok(()),
+ InitLeaseClaim::Busy => return Err(init_lease_busy()),
+ InitLeaseClaim::NotFound => {
+ return Err(ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::BackfillJobNotFound,
+ source: anyhow::anyhow!("email backfill job was not found"),
+ }));
+ }
+ };
+
+ let initialization =
+ initialize_backfill(ctx, access_token, scope, link, backfill_job, lease_token);
+ tokio::pin!(initialization);
+ let heartbeat = maintain_init_lease(ctx.db.clone(), scope.job_id, lease_token);
+ tokio::pin!(heartbeat);
+ let result = tokio::select! {
+ biased;
+ lease_result = &mut heartbeat => lease_result,
+ initialization_result = &mut initialization => initialization_result,
+ };
+ if result.is_err() {
+ email_db_client::backfill::job::update::release_init_lease(
+ &ctx.db,
+ scope.job_id,
+ lease_token,
+ )
+ .await
+ .inspect_err(|error| {
+ tracing::error!(
+ error = ?error,
+ job_id = %scope.job_id,
+ "failed to release email backfill init lease"
+ );
+ })
+ .ok();
+ }
+ result
+}
+
+async fn initialize_backfill(
+ ctx: &PubSubContext,
+ access_token: &str,
+ scope: &JobScopedPayload<InitPayload>,
+ link: &link::Link,
+ backfill_job: &backfill::BackfillJob,
+ lease_token: Uuid,
+) -> Result<(), ProcessingError> {
+ tracing::info!("Initializing backfill job");
+
+ // ensure we have the user's labels in the db
+ sync_labels(&ctx.db, &ctx.gmail_client, access_token, link.id)
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to sync labels"),
+ })
+ })?;
+
+ if let Err(e) = sync_contacts(
+ link,
+ &ctx.db,
+ &ctx.gmail_client,
+ &ctx.sqs_client,
+ access_token,
+ )
+ .await
+ {
+ tracing::error!(error = ?e, "Failed to sync contacts");
+ }
+
+ let threads_requested_limit = backfill_job.threads_requested_limit;
+
+ check_gmail_rate_limit(CheckGmailRateLimitArgs {
+ redis_client: &ctx.redis_client,
+ link_id: link.id,
+ gmail_operation: GmailApiOperation::UsersGetProfile,
+ retryable: true,
+ is_backfill: true,
+ })
+ .await?;
+ // get the total number of threads the user has in their account
+ let total_threads = match ctx
+ .gmail_client
+ .get_profile_threads_total(access_token)
+ .await
+ {
+ Ok(list) => list,
+ Err(e) => {
+ // Construct the structured Retryable error and return immediately.
+ return Err(ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::GmailApiFailed,
+ source: e.context("Failed to get total threads from Gmail API"),
+ }));
+ }
+ };
+
+ // If thread count is not specified, populate all available threads. Otherwise,
+ // populate up to the requested number or total available, whichever is smaller
+ let total_threads = match threads_requested_limit {
+ Some(requested) => min(total_threads, requested),
+ None => total_threads,
+ };
+
+ email_db_client::backfill::job::update::update_job_total_threads(
+ &ctx.db,
+ scope.job_id,
+ total_threads,
+ )
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to update total_threads"),
+ })
+ })?;
+
+ // With no threads to list, no per-thread message is ever enqueued, so the
+ // per-thread completion path that finalizes the job never runs. Complete
+ // the job directly instead. Completion atomically clears this init lease;
+ // if it fails, the outer error path releases the lease so a retry can
+ // finish both the email and associated calendar jobs.
+ if total_threads == 0 {
+ backfill_completion_service::handle_job_completed(ctx, scope.job_id, Some(lease_token))
+ .await?;
+ return Ok(());
+ }
+
+ ctx.redis_client
+ .init_backfill_job_progress(scope.job_id, total_threads)
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::RedisQueryFailed,
+ source: e.context("Failed to create entry in redis for job"),
+ })
+ })?;
+
+ if !email_db_client::backfill::job::update::finalize_initialization(
+ &ctx.db,
+ scope.job_id,
+ lease_token,
+ )
+ .await
+ .map_err(database_error)?
+ {
+ return Err(init_lease_busy());
+ }
+
+ Ok(())
+}
+
+async fn maintain_init_lease(
+ db: sqlx::PgPool,
+ job_id: Uuid,
+ lease_token: Uuid,
+) -> Result<(), ProcessingError> {
+ loop {
+ tokio::time::sleep(std::time::Duration::from_secs(30)).await;
+ let renewal =
+ email_db_client::backfill::job::update::renew_init_lease(&db, job_id, lease_token)
+ .await;
+ match renewal {
+ Ok(InitLeaseRenewal::Renewed) => {}
+ Ok(InitLeaseRenewal::Complete) => return Ok(()),
+ Ok(InitLeaseRenewal::Lost) => return Err(init_lease_busy()),
+ Ok(InitLeaseRenewal::NotFound) => {
+ return Err(ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::BackfillJobNotFound,
+ source: anyhow::anyhow!("email backfill job was not found"),
+ }));
+ }
+ Err(error) => {
+ tracing::error!(
+ error = ?error,
+ job_id = %job_id,
+ "failed to extend email backfill init lease"
+ );
+ return Err(database_error(error));
+ }
+ }
+ }
+}
+
+fn init_lease_busy() -> ProcessingError {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::EmailBackfillInitBusy,
+ source: anyhow::anyhow!("email backfill init is already leased"),
+ })
+}
+
+fn database_error(error: anyhow::Error) -> ProcessingError {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: error,
+ })
+}
diff --git a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
index 4706b7d5f2..773a7f0e2d 100644
--- a/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
+++ b/services/email_service/src/bin/pubsub_workers/pubsub_workers.rs
@@ -101,6 +101,15 @@ async fn main() -> anyhow::Result<()> {
let worker_cancellation_token = CancellationToken::new();
let worker_tracker = TaskTracker::new();
let event_broker_tracker = TaskTracker::new();
+
+ worker_tracker.spawn(email_service::calendar_outbox::run(
+ db.clone(),
+ sqs_client.clone(),
+ calendar_events::domain::service::GoogleCalendarSyncScheduler::new(
+ calendar_events::outbound::pg::PgCalendarRepository::new(db.clone()),
+ ),
+ worker_cancellation_token.clone(),
+ ));
let macro_event_broker = MacroEventBrokerService::new(
KafkaEventPublisher::new(config.kafka_brokers.as_ref())
.context("failed to create kafka event publisher")?,
diff --git a/services/email_service/src/calendar_ingest.rs b/services/email_service/src/calendar_ingest.rs
new file mode 100644
index 0000000000..a1849aa990
--- /dev/null
+++ b/services/email_service/src/calendar_ingest.rs
@@ -0,0 +1,198 @@
+//! Shared live-ingest and historical-backfill extraction of calendar MIME parts.
+
+use base64::{
+ Engine as _,
+ engine::general_purpose::{URL_SAFE, URL_SAFE_NO_PAD},
+};
+use calendar_events::{
+ domain::{
+ models::{EmailIcsSource, OccurrenceRange},
+ service::CalendarService,
+ },
+ inbound::ics::{ics_content_hash, parse_email_ics},
+ outbound::pg::PgCalendarRepository,
+};
+use chrono::Utc;
+use models_email::gmail::MessagePart;
+use uuid::Uuid;
+
+const MAX_ICS_BYTES: usize = 10 * 1024 * 1024;
+
+struct CalendarPart<'a> {
+ part: &'a MessagePart,
+ attachment_id: Option<&'a str>,
+}
+
+/// Identifiers and provider data needed to extract invitations from one email.
+pub struct CalendarIngestInput<'a> {
+ /// OAuth access token used to fetch non-inline Gmail attachments.
+ pub access_token: &'a str,
+ /// Whether the caller is a backfill worker, for quota accounting.
+ pub is_backfill: bool,
+ /// Macro owner of the linked inbox.
+ pub owner_id: &'a str,
+ /// Persisted email link containing the message.
+ pub email_link_id: Uuid,
+ /// Persisted email thread containing the message.
+ pub email_thread_id: Uuid,
+ /// Persisted email message being inspected.
+ pub email_message_id: Uuid,
+ /// Gmail message identifier used by the attachment API.
+ pub provider_message_id: &'a str,
+ /// Gmail MIME tree to inspect recursively.
+ pub payload: &'a MessagePart,
+}
+
+/// Extract and reconcile every `text/calendar`, `application/ics`, or
+/// filename-identified `.ics` part from one Gmail message.
+///
+/// Invalid individual invitations are logged and skipped so malformed mail
+/// cannot block email ingestion. Gmail fetch and database failures are
+/// returned, allowing the surrounding queue operation to retry.
+#[tracing::instrument(
+ skip(db, gmail_client, redis_client, input),
+ fields(
+ email_link_id = %input.email_link_id,
+ email_message_id = %input.email_message_id
+ ),
+ err
+)]
+pub async fn ingest_calendar_parts(
+ db: &sqlx::PgPool,
+ gmail_client: &gmail_client::GmailClient,
+ redis_client: &crate::util::redis::RedisClient,
+ input: CalendarIngestInput<'_>,
+) -> anyhow::Result<usize> {
+ let parts = calendar_parts(input.payload);
+ if parts.is_empty() {
+ return Ok(0);
+ }
+
+ let horizon = OccurrenceRange::maintenance_horizon(Utc::now());
+ let service = CalendarService::new(PgCalendarRepository::new(db.clone()));
+ let mut extracted = 0;
+
+ for calendar_part in parts {
+ let bytes = if let Some(encoded) = calendar_part
+ .part
+ .body
+ .as_ref()
+ .and_then(|body| body.data_base64.as_deref())
+ {
+ match decode_base64url(encoded) {
+ Ok(bytes) => bytes,
+ Err(error) => {
+ tracing::warn!(
+ error = ?error,
+ part_id = %calendar_part.part.part_id,
+ "skipping calendar MIME part with invalid inline base64"
+ );
+ continue;
+ }
+ }
+ } else if let Some(attachment_id) = calendar_part.attachment_id {
+ // Callers already passed a message-level gate; attachment fetches
+ // are additional Gmail requests and must respect the same quota.
+ if redis_client
+ .is_rate_limited(crate::util::redis::rate_limit::RateLimitArgs {
+ user_id: input.email_link_id,
+ operation:
+ models_email::gmail::operations::GmailApiOperation::MessagesAttachmentsGet,
+ is_backfill: input.is_backfill,
+ })
+ .await
+ {
+ anyhow::bail!("Gmail rate limit reached while fetching calendar attachments");
+ }
+ gmail_client
+ .get_attachment_data(input.access_token, input.provider_message_id, attachment_id)
+ .await?
+ } else {
+ tracing::warn!(
+ part_id = %calendar_part.part.part_id,
+ "calendar MIME part has neither inline data nor an attachment id"
+ );
+ continue;
+ };
+
+ if bytes.len() > MAX_ICS_BYTES {
+ tracing::warn!(
+ part_id = %calendar_part.part.part_id,
+ bytes = bytes.len(),
+ "calendar MIME part exceeds extraction size limit"
+ );
+ continue;
+ }
+
+ let source = EmailIcsSource {
+ email_link_id: input.email_link_id,
+ email_thread_id: Some(input.email_thread_id),
+ email_message_id: input.email_message_id,
+ email_attachment_id: calendar_part
+ .attachment_id
+ .map(ToOwned::to_owned)
+ .or_else(|| Some(calendar_part.part.part_id.clone()).filter(|id| !id.is_empty())),
+ content_hash: ics_content_hash(&bytes),
+ raw_payload: serde_json::json!({
+ "providerMessageId": input.provider_message_id,
+ "partId": calendar_part.part.part_id,
+ "filename": calendar_part.part.filename,
+ "mimeType": calendar_part.part.mime_type,
+ }),
+ };
+ let upserts = match parse_email_ics(input.owner_id, source, &bytes, &horizon) {
+ Ok(upserts) => upserts,
+ Err(error) => {
+ tracing::warn!(
+ error=?error,
+ part_id=%calendar_part.part.part_id,
+ "skipping malformed calendar invitation"
+ );
+ continue;
+ }
+ };
+ for upsert in upserts {
+ service
+ .upsert_email_event(upsert)
+ .await
+ .map_err(|error| anyhow::anyhow!("{error:?}"))?;
+ extracted += 1;
+ }
+ }
+
+ Ok(extracted)
+}
+
+fn calendar_parts(root: &MessagePart) -> Vec<CalendarPart<'_>> {
+ let mut result = Vec::new();
+ let mut stack = vec![root];
+ while let Some(part) = stack.pop() {
+ let mime_type = part.mime_type.split(';').next().unwrap_or_default().trim();
+ let is_calendar = mime_type.eq_ignore_ascii_case("text/calendar")
+ || mime_type.eq_ignore_ascii_case("application/ics")
+ || part.filename.to_ascii_lowercase().ends_with(".ics");
+ if is_calendar {
+ result.push(CalendarPart {
+ attachment_id: part
+ .body
+ .as_ref()
+ .and_then(|body| body.attachment_id.as_deref()),
+ part,
+ });
+ }
+ if let Some(children) = &part.parts {
+ stack.extend(children);
+ }
+ }
+ result
+}
+
+fn decode_base64url(value: &str) -> anyhow::Result<Vec<u8>> {
+ URL_SAFE_NO_PAD
+ .decode(value)
+ .or_else(|_| URL_SAFE.decode(value))
+ .map_err(Into::into)
+}
+
+#[cfg(test)]
+mod test;
diff --git a/services/email_service/src/calendar_ingest/test.rs b/services/email_service/src/calendar_ingest/test.rs
new file mode 100644
index 0000000000..d6fc5db161
--- /dev/null
+++ b/services/email_service/src/calendar_ingest/test.rs
@@ -0,0 +1,53 @@
+use super::*;
+use models_email::gmail::MessagePartBody;
+
+#[test]
+fn discovers_inline_and_attachment_calendar_parts() {
+ let root = MessagePart {
+ part_id: String::new(),
+ mime_type: "multipart/mixed".to_string(),
+ filename: String::new(),
+ headers: Vec::new(),
+ body: None,
+ parts: Some(vec![
+ MessagePart {
+ part_id: "inline".to_string(),
+ mime_type: "text/calendar; method=REQUEST".to_string(),
+ filename: String::new(),
+ headers: Vec::new(),
+ body: Some(MessagePartBody {
+ attachment_id: None,
+ size: 10,
+ data_base64: Some("aGVsbG8".to_string()),
+ }),
+ parts: None,
+ },
+ MessagePart {
+ part_id: "attachment".to_string(),
+ mime_type: "application/octet-stream".to_string(),
+ filename: "invite.ICS".to_string(),
+ headers: Vec::new(),
+ body: Some(MessagePartBody {
+ attachment_id: Some("gmail-id".to_string()),
+ size: 10,
+ data_base64: None,
+ }),
+ parts: None,
+ },
+ ]),
+ };
+
+ let parts = calendar_parts(&root);
+ assert_eq!(parts.len(), 2);
+ assert!(
+ parts
+ .iter()
+ .any(|part| part.attachment_id == Some("gmail-id"))
+ );
+}
+
+#[test]
+fn accepts_padded_and_unpadded_base64url() {
+ assert_eq!(decode_base64url("aGVsbG8").unwrap(), b"hello");
+ assert_eq!(decode_base64url("aGVsbG8=").unwrap(), b"hello");
+}
diff --git a/services/email_service/src/calendar_outbox.rs b/services/email_service/src/calendar_outbox.rs
new file mode 100644
index 0000000000..1612133f2c
--- /dev/null
+++ b/services/email_service/src/calendar_outbox.rs
@@ -0,0 +1,289 @@
+//! Durable calendar backfill publication.
+
+use models_email::email::service::backfill::{
+ BackfillOperation, BackfillPubsubMessage, CalendarBackfillPayload, FinalizeBackfillPayload,
+ JobScopedPayload, LinkScopedPayload,
+};
+use models_email::email::service::thread::ListThreadsPayload;
+use sqlx::PgPool;
+use sqs_client::SQS;
+use uuid::Uuid;
+
+const BATCH_SIZE: usize = 50;
+
+struct OutboxRow {
+ id: Uuid,
+ backfill_job_id: Uuid,
+ email_link_id: Uuid,
+ kind: String,
+}
+
+struct EmailInitOutboxRow {
+ id: Uuid,
+ backfill_job_id: Uuid,
+ email_link_id: Uuid,
+ priority_pass: bool,
+}
+
+struct EmailCompletionOutboxRow {
+ id: Uuid,
+ backfill_job_id: Uuid,
+ email_link_id: Option<Uuid>,
+}
+
+/// Continuously publish calendar outbox rows to the established email
+/// backfill queue.
+///
+/// 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, sqs, scheduler))]
+pub async fn run<R>(
+ db: PgPool,
+ sqs: SQS,
+ scheduler: GoogleCalendarSyncScheduler<R>,
+ cancellation_token: tokio_util::sync::CancellationToken,
+) where
+ R: GoogleCalendarSyncRepository,
+{
+ loop {
+ if cancellation_token.is_cancelled() {
+ return;
+ }
+ scheduler
+ .run_once(Utc::now())
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error=?error, "failed to schedule due Google Calendar syncs");
+ })
+ .ok();
+ drain_calendar(&db, &sqs)
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error = ?error, "failed to publish calendar backfill outbox");
+ })
+ .ok();
+ drain_email_init(&db, &sqs)
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error = ?error, "failed to publish email backfill init outbox");
+ })
+ .ok();
+ drain_email_completion(&db, &sqs)
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error = ?error, "failed to publish email completion outbox");
+ })
+ .ok();
+ tokio::select! {
+ _ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {}
+ _ = cancellation_token.cancelled() => return,
+ }
+ }
+}
+
+#[tracing::instrument(skip(db, sqs), err)]
+async fn drain_email_completion(db: &PgPool, sqs: &SQS) -> anyhow::Result<usize> {
+ let mut published = 0;
+ for _ in 0..BATCH_SIZE {
+ let mut tx = db.begin().await?;
+ let row = sqlx::query_as!(
+ EmailCompletionOutboxRow,
+ r#"
+ SELECT
+ outbox.id,
+ outbox.backfill_job_id,
+ job.link_id AS "email_link_id?"
+ FROM email_backfill_completion_outbox outbox
+ JOIN email_backfill_jobs job ON job.id = outbox.backfill_job_id
+ WHERE outbox.published_at IS NULL
+ AND job.status = 'Complete'
+ ORDER BY outbox.created_at, outbox.id
+ FOR UPDATE OF outbox SKIP LOCKED
+ LIMIT 1
+ "#,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+
+ let Some(row) = row else {
+ tx.commit().await?;
+ break;
+ };
+ let Some(message) = to_email_completion_message(&row) else {
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET published_at = COALESCE(published_at, now()),
+ completed_at = COALESCE(completed_at, now())
+ WHERE id = $1
+ "#,
+ row.id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ published += 1;
+ continue;
+ };
+ sqs.enqueue_email_backfill_message(message).await?;
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET published_at = now()
+ WHERE id = $1
+ "#,
+ row.id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ published += 1;
+ }
+ Ok(published)
+}
+
+fn to_email_completion_message(row: &EmailCompletionOutboxRow) -> Option<BackfillPubsubMessage> {
+ Some(BackfillPubsubMessage {
+ backfill_operation: BackfillOperation::FinalizeBackfill(JobScopedPayload {
+ link_id: row.email_link_id?,
+ job_id: row.backfill_job_id,
+ payload: FinalizeBackfillPayload {},
+ }),
+ })
+}
+
+#[tracing::instrument(skip(db, sqs), err)]
+async fn drain_calendar(db: &PgPool, sqs: &SQS) -> anyhow::Result<usize> {
+ let mut published = 0;
+ for _ in 0..BATCH_SIZE {
+ let mut tx = db.begin().await?;
+ let row = sqlx::query_as!(
+ OutboxRow,
+ r#"
+ SELECT
+ outbox.id,
+ outbox.backfill_job_id,
+ job.email_link_id,
+ job.kind
+ FROM calendar_sync_outbox outbox
+ JOIN calendar_backfill_jobs job ON job.id = outbox.backfill_job_id
+ WHERE outbox.published_at IS NULL
+ ORDER BY outbox.created_at, outbox.id
+ FOR UPDATE OF outbox SKIP LOCKED
+ LIMIT 1
+ "#,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+
+ let Some(row) = row else {
+ tx.commit().await?;
+ break;
+ };
+
+ match to_queue_message(&row) {
+ Ok(message) => {
+ sqs.enqueue_email_backfill_message(message).await?;
+ published += 1;
+ }
+ Err(error) => {
+ // An unmappable row must still be marked published: the drain
+ // always selects the oldest unpublished row, so leaving it
+ // would wedge every calendar row behind it forever.
+ tracing::error!(error = ?error, outbox_id = %row.id, "skipping unmappable calendar outbox row");
+ }
+ }
+ sqlx::query!(
+ r#"
+ UPDATE calendar_sync_outbox
+ SET published_at = now()
+ WHERE id = $1
+ "#,
+ row.id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ }
+ Ok(published)
+}
+
+#[tracing::instrument(skip(db, sqs), err)]
+async fn drain_email_init(db: &PgPool, sqs: &SQS) -> anyhow::Result<usize> {
+ let mut published = 0;
+ for _ in 0..BATCH_SIZE {
+ let mut tx = db.begin().await?;
+ let row = sqlx::query_as!(
+ EmailInitOutboxRow,
+ r#"
+ SELECT
+ outbox.id,
+ outbox.backfill_job_id,
+ job.link_id AS "email_link_id!",
+ job.threads_requested_limit IS NULL AS "priority_pass!"
+ FROM email_backfill_init_outbox outbox
+ JOIN email_backfill_jobs job ON job.id = outbox.backfill_job_id
+ WHERE outbox.published_at IS NULL
+ AND job.status = 'InProgress'
+ AND job.initialized_at IS NOT NULL
+ ORDER BY outbox.created_at, outbox.id
+ FOR UPDATE OF outbox SKIP LOCKED
+ LIMIT 1
+ "#,
+ )
+ .fetch_optional(&mut *tx)
+ .await?;
+
+ let Some(row) = row else {
+ tx.commit().await?;
+ break;
+ };
+ let message = BackfillPubsubMessage {
+ backfill_operation: BackfillOperation::ListThreads(JobScopedPayload {
+ link_id: row.email_link_id,
+ job_id: row.backfill_job_id,
+ payload: ListThreadsPayload {
+ next_page_token: None,
+ priority_pass: row.priority_pass,
+ },
+ }),
+ };
+ sqs.enqueue_email_backfill_message(message).await?;
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_init_outbox
+ SET published_at = now()
+ WHERE id = $1
+ "#,
+ row.id,
+ )
+ .execute(&mut *tx)
+ .await?;
+ tx.commit().await?;
+ published += 1;
+ }
+ Ok(published)
+}
+
+fn to_queue_message(row: &OutboxRow) -> anyhow::Result<BackfillPubsubMessage> {
+ let scope = LinkScopedPayload {
+ link_id: row.email_link_id,
+ payload: CalendarBackfillPayload {
+ calendar_job_id: row.backfill_job_id,
+ },
+ };
+ let backfill_operation = match row.kind.as_str() {
+ "google_calendar" => BackfillOperation::CalendarGoogleBackfill(scope),
+ "email_ics" => BackfillOperation::CalendarEmailIcsBackfill(scope),
+ kind => anyhow::bail!("unsupported calendar backfill kind: {kind}"),
+ };
+ Ok(BackfillPubsubMessage { backfill_operation })
+}
+
+#[cfg(test)]
+mod test;
+use calendar_events::domain::{
+ ports::GoogleCalendarSyncRepository, service::GoogleCalendarSyncScheduler,
+};
+use chrono::Utc;
diff --git a/services/email_service/src/calendar_outbox/test.rs b/services/email_service/src/calendar_outbox/test.rs
new file mode 100644
index 0000000000..cd2a856639
--- /dev/null
+++ b/services/email_service/src/calendar_outbox/test.rs
@@ -0,0 +1,54 @@
+use super::*;
+
+fn row(kind: &str) -> OutboxRow {
+ OutboxRow {
+ id: Uuid::now_v7(),
+ backfill_job_id: Uuid::now_v7(),
+ email_link_id: Uuid::now_v7(),
+ kind: kind.to_string(),
+ }
+}
+
+#[test]
+fn maps_google_calendar_kind() {
+ assert!(matches!(
+ to_queue_message(&row("google_calendar"))
+ .unwrap()
+ .backfill_operation,
+ BackfillOperation::CalendarGoogleBackfill(_)
+ ));
+}
+
+#[test]
+fn rejects_unknown_kind() {
+ assert!(to_queue_message(&row("unknown")).is_err());
+}
+
+#[test]
+fn maps_email_completion_outbox() {
+ let row = EmailCompletionOutboxRow {
+ id: Uuid::now_v7(),
+ backfill_job_id: Uuid::now_v7(),
+ email_link_id: Some(Uuid::now_v7()),
+ };
+
+ let BackfillOperation::FinalizeBackfill(scope) = to_email_completion_message(&row)
+ .unwrap()
+ .backfill_operation
+ else {
+ panic!("expected finalize-backfill operation");
+ };
+ assert_eq!(scope.job_id, row.backfill_job_id);
+ assert_eq!(Some(scope.link_id), row.email_link_id);
+}
+
+#[test]
+fn skips_email_completion_when_link_was_deleted() {
+ let row = EmailCompletionOutboxRow {
+ id: Uuid::now_v7(),
+ backfill_job_id: Uuid::now_v7(),
+ email_link_id: None,
+ };
+
+ assert!(to_email_completion_message(&row).is_none());
+}
diff --git a/services/email_service/src/lib.rs b/services/email_service/src/lib.rs
index ac60a74d68..e87bbd0fdb 100644
--- a/services/email_service/src/lib.rs
+++ b/services/email_service/src/lib.rs
@@ -1,3 +1,10 @@
+/// Durable email-backfill completion orchestration.
+pub mod backfill_completion_service;
+/// Fenced email-backfill initialization orchestration.
+pub mod backfill_init_service;
+pub mod calendar_ingest;
+/// Durable publication of grant-triggered calendar work.
+pub mod calendar_outbox;
pub mod config;
pub mod convert;
pub mod pubsub;
diff --git a/services/email_service/src/main.rs b/services/email_service/src/main.rs
index 1ff7c65314..98a3486f5a 100644
--- a/services/email_service/src/main.rs
+++ b/services/email_service/src/main.rs
@@ -1,6 +1,7 @@
#![recursion_limit = "256"]
use crate::api::context::{ApiContext, AuthorizationService};
use anyhow::Context;
+use calendar_events::{domain::service::CalendarService, outbound::pg::PgCalendarRepository};
use document_storage_service_client::DocumentStorageServiceClient;
use email::{
domain::service::EmailServiceImpl,
@@ -182,6 +183,7 @@ async fn main() -> anyhow::Result<()> {
redis_conn,
auth_service_client.clone(),
));
+ let calendar_service = Arc::new(CalendarService::new(PgCalendarRepository::new(db.clone())));
let api_result = api::setup_and_serve(ApiContext {
db,
internal_api_key: config.internal_api_key.clone(),
@@ -201,6 +203,7 @@ async fn main() -> anyhow::Result<()> {
email_thread_state,
gmail_token_state,
macro_event_broker: Arc::new(macro_event_broker),
+ calendar_service,
})
.await;
diff --git a/services/email_service/src/openapi.rs b/services/email_service/src/openapi.rs
index b3d4782ce3..591ac02713 100644
--- a/services/email_service/src/openapi.rs
+++ b/services/email_service/src/openapi.rs
@@ -1,6 +1,9 @@
#![allow(unused)]
mod api;
+mod backfill_completion_service;
+mod backfill_init_service;
+mod calendar_ingest;
mod config;
mod convert;
mod pubsub;
diff --git a/services/email_service/src/pubsub/backfill/backfill_message.rs b/services/email_service/src/pubsub/backfill/backfill_message.rs
index c40cb8f27a..186ffc173d 100644
--- a/services/email_service/src/pubsub/backfill/backfill_message.rs
+++ b/services/email_service/src/pubsub/backfill/backfill_message.rs
@@ -53,6 +53,8 @@ pub async fn backfill_message(
}
};
+ let calendar_payload = message_resource.payload.clone();
+
// Map Gmail resource to service model (IDs are generated in the parse function)
let mut message = map_message_resource_to_service(message_resource, link.id)
.context("Failed to map message resource to service")
@@ -81,6 +83,43 @@ pub async fn backfill_message(
source: e.context("Failed to insert final message into database"),
})
})?;
+ let (message_db_id, persisted_thread_id) =
+ email_db_client::messages::get::get_message_and_thread_id_by_provider_id(
+ &ctx.db,
+ link.id,
+ &p.message_provider_id,
+ )
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to resolve persisted calendar invitation message"),
+ })
+ })?;
+
+ crate::calendar_ingest::ingest_calendar_parts(
+ &ctx.db,
+ &ctx.gmail_client,
+ &ctx.redis_client,
+ crate::calendar_ingest::CalendarIngestInput {
+ is_backfill: true,
+ access_token,
+ owner_id: link.macro_id.as_ref(),
+ email_link_id: link.id,
+ email_thread_id: persisted_thread_id,
+ email_message_id: message_db_id,
+ provider_message_id: &p.message_provider_id,
+ payload: &calendar_payload,
+ },
+ )
+ .await
+ .inspect_err(|error| {
+ // Calendar extraction is best-effort: a Gmail or database failure
+ // here must not block the message's core side effects, and the
+ // durable email-ICS backfill re-extracts anything missed.
+ tracing::warn!(error = ?error, "failed to extract calendar invitation");
+ })
+ .ok();
// Fan out a PopulateCrmContact job per address involved in the
// message — every non-draft message contributes, in both
diff --git a/services/email_service/src/pubsub/backfill/calendar_email_ics_backfill.rs b/services/email_service/src/pubsub/backfill/calendar_email_ics_backfill.rs
new file mode 100644
index 0000000000..ec1e0f3e5a
--- /dev/null
+++ b/services/email_service/src/pubsub/backfill/calendar_email_ics_backfill.rs
@@ -0,0 +1,51 @@
+use crate::pubsub::context::PubSubContext;
+use calendar_events::domain::{
+ models::CalendarBackfillJobKey, service::EmailCalendarBackfillRunError,
+};
+use models_email::email::service::{
+ backfill::CalendarBackfillPayload,
+ link::Link,
+ pubsub::{DetailedError, FailureReason, ProcessingError},
+};
+
+/// Invoke the calendar application service for one email-ICS backfill delivery.
+#[tracing::instrument(skip(ctx, link), fields(calendar_job_id = %payload.calendar_job_id))]
+pub async fn calendar_email_ics_backfill(
+ ctx: &PubSubContext,
+ link: &Link,
+ payload: &CalendarBackfillPayload,
+) -> Result<(), ProcessingError> {
+ ctx.calendar_backfills
+ .email_ics
+ .run(
+ CalendarBackfillJobKey {
+ job_id: payload.calendar_job_id,
+ email_link_id: link.id,
+ },
+ link.fusionauth_user_id.as_str(),
+ )
+ .await
+ .map_err(map_run_error)
+}
+
+fn map_run_error(error: EmailCalendarBackfillRunError) -> ProcessingError {
+ let retryable = matches!(
+ error,
+ EmailCalendarBackfillRunError::Busy | EmailCalendarBackfillRunError::Retryable(_)
+ );
+ let reason = match &error {
+ EmailCalendarBackfillRunError::Busy => FailureReason::CalendarBackfillBusy,
+ EmailCalendarBackfillRunError::NotFound => FailureReason::BackfillJobNotFound,
+ EmailCalendarBackfillRunError::ScanFailed => FailureReason::InvalidData,
+ EmailCalendarBackfillRunError::Retryable(_) => FailureReason::DatabaseQueryFailed,
+ };
+ let detailed = DetailedError {
+ reason,
+ source: anyhow::Error::new(error),
+ };
+ if retryable {
+ ProcessingError::Retryable(detailed)
+ } else {
+ ProcessingError::NonRetryable(detailed)
+ }
+}
diff --git a/services/email_service/src/pubsub/backfill/calendar_google_backfill.rs b/services/email_service/src/pubsub/backfill/calendar_google_backfill.rs
new file mode 100644
index 0000000000..fd9bc53d55
--- /dev/null
+++ b/services/email_service/src/pubsub/backfill/calendar_google_backfill.rs
@@ -0,0 +1,65 @@
+use crate::pubsub::context::PubSubContext;
+use calendar_events::domain::{
+ models::{CalendarBackfillJobKey, OccurrenceRange},
+ service::GoogleCalendarBackfillRunError,
+};
+use chrono::Utc;
+use models_email::email::service::{
+ backfill::CalendarBackfillPayload,
+ link::Link,
+ pubsub::{DetailedError, FailureReason, ProcessingError},
+};
+
+/// Invoke the calendar application service for one Google backfill delivery.
+#[tracing::instrument(skip(ctx, access_token, link), fields(calendar_job_id = %payload.calendar_job_id))]
+pub async fn calendar_google_backfill(
+ ctx: &PubSubContext,
+ access_token: &str,
+ link: &Link,
+ payload: &CalendarBackfillPayload,
+) -> Result<(), ProcessingError> {
+ ctx.calendar_backfills
+ .google
+ .run(
+ CalendarBackfillJobKey {
+ job_id: payload.calendar_job_id,
+ email_link_id: link.id,
+ },
+ link.macro_id.as_ref(),
+ access_token,
+ OccurrenceRange::maintenance_horizon(Utc::now()),
+ )
+ .await
+ .map(|_| ())
+ .map_err(map_run_error)
+}
+
+fn map_run_error(error: GoogleCalendarBackfillRunError) -> ProcessingError {
+ let retryable = matches!(
+ error,
+ GoogleCalendarBackfillRunError::Busy
+ | GoogleCalendarBackfillRunError::LeaseLost
+ | GoogleCalendarBackfillRunError::Retryable(_)
+ );
+ let reason = match &error {
+ GoogleCalendarBackfillRunError::Busy | GoogleCalendarBackfillRunError::LeaseLost => {
+ FailureReason::CalendarBackfillBusy
+ }
+ GoogleCalendarBackfillRunError::NotFound => FailureReason::BackfillJobNotFound,
+ GoogleCalendarBackfillRunError::ReauthRequired { .. } => {
+ FailureReason::AccessTokenFetchFailed
+ }
+ GoogleCalendarBackfillRunError::AlreadyFailed
+ | GoogleCalendarBackfillRunError::Permanent(_) => FailureReason::InvalidData,
+ GoogleCalendarBackfillRunError::Retryable(_) => FailureReason::GmailApiFailed,
+ };
+ let detailed = DetailedError {
+ reason,
+ source: anyhow::Error::new(error),
+ };
+ if retryable {
+ ProcessingError::Retryable(detailed)
+ } else {
+ ProcessingError::NonRetryable(detailed)
+ }
+}
diff --git a/services/email_service/src/pubsub/backfill/error_handlers.rs b/services/email_service/src/pubsub/backfill/error_handlers.rs
index 8b73a2d145..21fd5dc43a 100644
--- a/services/email_service/src/pubsub/backfill/error_handlers.rs
+++ b/services/email_service/src/pubsub/backfill/error_handlers.rs
@@ -3,12 +3,15 @@ use crate::pubsub::backfill::increment_counters::{
};
use crate::pubsub::context::PubSubContext;
use crate::pubsub::util::cg_refresh_email;
+use calendar_events::domain::{
+ models::{CalendarBackfillFailureDisposition, CalendarBackfillJobKey},
+ service::GoogleCalendarBackfillRunError,
+};
use models_email::api::refresh::{BackfillStatus, RefreshEmailEvent};
use models_email::email::service::backfill::{
- BackfillJobStatus, BackfillMessagePayload, BackfillOperation, BackfillPubsubMessage,
- JobScopedPayload,
+ BackfillMessagePayload, BackfillOperation, BackfillPubsubMessage, JobScopedPayload,
};
-use models_email::email::service::pubsub::DetailedError;
+use models_email::email::service::pubsub::{DetailedError, FailureReason, LinkManagerMessage};
use sqs_worker::cleanup_message;
use uuid::Uuid;
@@ -27,18 +30,91 @@ pub async fn handle_non_retryable_error(
);
match &data.backfill_operation {
- BackfillOperation::Init(scope) => mark_job_failed(ctx, scope.job_id).await,
- BackfillOperation::ListThreads(scope) => mark_job_failed(ctx, scope.job_id).await,
+ BackfillOperation::Init(scope) => mark_job_failed(ctx, scope.job_id).await?,
+ BackfillOperation::ListThreads(scope) => mark_job_failed(ctx, scope.job_id).await?,
BackfillOperation::BackfillThread(scope) => {
- handle_thread_failure(ctx, scope.link_id, scope.job_id).await;
+ handle_thread_failure(ctx, scope.link_id, scope.job_id).await?;
}
BackfillOperation::UpdateThreadMetadata(scope) => {
- handle_thread_failure(ctx, scope.link_id, scope.job_id).await;
+ handle_thread_failure(ctx, scope.link_id, scope.job_id).await?;
}
BackfillOperation::BackfillMessage(scope) => {
- handle_message_failure(ctx, scope).await;
+ handle_message_failure(ctx, scope).await?;
}
BackfillOperation::BackfillAttachment(_) => {}
+ BackfillOperation::FinalizeBackfill(scope) => {
+ // The message is about to be deleted; without republishing the
+ // completion-outbox row, the backfill would finish silently
+ // missing attachment fan-out, contacts sync, and the final
+ // refresh event. Deterministic failures resurface via the DLQ.
+ sqlx::query!(
+ r#"
+ UPDATE email_backfill_completion_outbox
+ SET published_at = NULL
+ WHERE backfill_job_id = $1
+ "#,
+ scope.job_id,
+ )
+ .execute(&ctx.db)
+ .await
+ .inspect_err(|error| {
+ tracing::error!(error = ?error, job_id = %scope.job_id, "failed to republish completion outbox after terminal finalize failure");
+ })
+ .ok();
+ }
+ BackfillOperation::CalendarGoogleBackfill(scope) => {
+ let coordinator_reauth_transitioned = coordinator_reauth_edge(&e.source);
+ let prelease_reauth_transitioned = if coordinator_reauth_transitioned.is_none() {
+ let disposition = if e.reason == FailureReason::AccessTokenFetchFailed {
+ CalendarBackfillFailureDisposition::ReauthRequired
+ } else {
+ CalendarBackfillFailureDisposition::Permanent
+ };
+ ctx.calendar_backfills
+ .google_failure
+ .fail_unclaimed(
+ CalendarBackfillJobKey {
+ job_id: scope.payload.calendar_job_id,
+ email_link_id: scope.link_id,
+ },
+ disposition,
+ &format!("{:#}", e.source),
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!("{error:?}"))?
+ .link_reauth_transitioned
+ } else {
+ false
+ };
+ if coordinator_reauth_transitioned == Some(true) || prelease_reauth_transitioned {
+ ctx.sqs_client
+ .enqueue_link_manager_notification(LinkManagerMessage::NotifyReauthRequired {
+ link_id: scope.link_id,
+ })
+ .await
+ .inspect_err(|error| {
+ tracing::error!(
+ error=?error,
+ link_id=%scope.link_id,
+ "Failed to enqueue reauth notification after calendar backfill failure"
+ );
+ })
+ .ok();
+ }
+ }
+ BackfillOperation::CalendarEmailIcsBackfill(scope) => {
+ ctx.calendar_backfills
+ .email_ics
+ .fail_terminal(
+ CalendarBackfillJobKey {
+ job_id: scope.payload.calendar_job_id,
+ email_link_id: scope.link_id,
+ },
+ &format!("{:#}", e.source),
+ )
+ .await
+ .map_err(|error| anyhow::anyhow!("{error:?}"))?;
+ }
// Best-effort side seed — a failure must not fail the backfill job.
BackfillOperation::SeedSentContact(_) => {}
BackfillOperation::PopulateCrmContact(_) => {}
@@ -51,23 +127,34 @@ pub async fn handle_non_retryable_error(
Ok(())
}
-async fn mark_job_failed(ctx: &PubSubContext, job_id: Uuid) {
- if let Err(db_err) = email_db_client::backfill::job::update::update_backfill_job_status(
- &ctx.db,
- job_id,
- BackfillJobStatus::Failed,
- )
- .await
- {
- tracing::error!(
- error = %db_err,
- job_id = %job_id,
- "Failed to update backfill job status to Failed"
- );
- return;
- }
+fn coordinator_reauth_edge(error: &anyhow::Error) -> Option<bool> {
+ error.chain().find_map(|cause| {
+ cause
+ .downcast_ref::<GoogleCalendarBackfillRunError>()
+ .map(|error| {
+ matches!(
+ error,
+ GoogleCalendarBackfillRunError::ReauthRequired {
+ link_reauth_transitioned: true,
+ ..
+ }
+ )
+ })
+ })
+}
+
+#[tracing::instrument(skip(ctx), err)]
+async fn mark_job_failed(ctx: &PubSubContext, job_id: Uuid) -> anyhow::Result<()> {
+ let transitioned =
+ email_db_client::backfill::job::update::fail_backfill_job_and_calendar_extraction(
+ &ctx.db, job_id,
+ )
+ .await?;
- notify_job_failed(ctx, job_id).await;
+ if transitioned {
+ notify_job_failed(ctx, job_id).await;
+ }
+ Ok(())
}
/// Resolves the failed job's link and signals the failure over the connection
@@ -96,6 +183,9 @@ async fn notify_job_failed(ctx: &PubSubContext, job_id: Uuid) {
}
}
+#[cfg(test)]
+mod test;
+
/// Handles retryable errors by updating status to InProgress and adding the error message
#[tracing::instrument(
skip(data, _e),
@@ -140,6 +230,24 @@ pub async fn handle_retryable_error(
"Retryable error backfilling attachment"
)
}
+ BackfillOperation::FinalizeBackfill(scope) => {
+ tracing::debug!(
+ job_id = %scope.job_id,
+ "Retryable error finalizing completed backfill"
+ )
+ }
+ BackfillOperation::CalendarGoogleBackfill(scope) => {
+ tracing::debug!(
+ calendar_job_id = %scope.payload.calendar_job_id,
+ "Retryable error backfilling Google Calendar"
+ )
+ }
+ BackfillOperation::CalendarEmailIcsBackfill(scope) => {
+ tracing::debug!(
+ calendar_job_id = %scope.payload.calendar_job_id,
+ "Retryable error scheduling email calendar extraction"
+ )
+ }
BackfillOperation::SeedSentContact(scope) => {
tracing::debug!(
message_id = %scope.payload.message_provider_id,
@@ -175,7 +283,11 @@ pub async fn handle_retryable_error(
}
#[tracing::instrument(skip(ctx))]
-async fn handle_thread_failure(ctx: &PubSubContext, link_id: Uuid, job_id: Uuid) {
+async fn handle_thread_failure(
+ ctx: &PubSubContext,
+ link_id: Uuid,
+ job_id: Uuid,
+) -> anyhow::Result<()> {
let link = match email_db_client::links::get::fetch_link_by_id(&ctx.db, link_id).await {
Ok(Some(link)) => link,
Ok(None) => {
@@ -189,8 +301,8 @@ async fn handle_thread_failure(ctx: &PubSubContext, link_id: Uuid, job_id: Uuid)
job_id = job_id.to_string(),
"Link not found in handle_thread_failure; marking backfill job failed"
);
- mark_job_failed(ctx, job_id).await;
- return;
+ mark_job_failed(ctx, job_id).await?;
+ return Ok(());
}
Err(db_err) => {
tracing::error!(
@@ -198,8 +310,8 @@ async fn handle_thread_failure(ctx: &PubSubContext, link_id: Uuid, job_id: Uuid)
job_id = job_id.to_string(),
"Failed to fetch link in handle_thread_failure; marking backfill job failed"
);
- mark_job_failed(ctx, job_id).await;
- return;
+ mark_job_failed(ctx, job_id).await?;
+ return Ok(());
}
};
@@ -210,13 +322,14 @@ async fn handle_thread_failure(ctx: &PubSubContext, link_id: Uuid, job_id: Uuid)
"Failed to check if job is completed in handle thread failure"
);
}
+ Ok(())
}
#[tracing::instrument(skip(ctx))]
pub async fn handle_message_failure(
ctx: &PubSubContext,
scope: &JobScopedPayload<BackfillMessagePayload>,
-) {
+) -> anyhow::Result<()> {
let link = match email_db_client::links::get::fetch_link_by_id(&ctx.db, scope.link_id).await {
Ok(Some(link)) => link,
Ok(None) => {
@@ -229,8 +342,8 @@ pub async fn handle_message_failure(
job_id = scope.job_id.to_string(),
"Link not found in handle_message_failure; marking backfill job failed"
);
- mark_job_failed(ctx, scope.job_id).await;
- return;
+ mark_job_failed(ctx, scope.job_id).await?;
+ return Ok(());
}
Err(db_err) => {
tracing::error!(
@@ -238,8 +351,8 @@ pub async fn handle_message_failure(
job_id = scope.job_id.to_string(),
"Failed to fetch link in handle_message_failure; marking backfill job failed"
);
- mark_job_failed(ctx, scope.job_id).await;
- return;
+ mark_job_failed(ctx, scope.job_id).await?;
+ return Ok(());
}
};
@@ -250,4 +363,5 @@ pub async fn handle_message_failure(
"Failed to check if thread is completed in handle message failure"
);
}
+ Ok(())
}
diff --git a/services/email_service/src/pubsub/backfill/error_handlers/test.rs b/services/email_service/src/pubsub/backfill/error_handlers/test.rs
new file mode 100644
index 0000000000..63040192cd
--- /dev/null
+++ b/services/email_service/src/pubsub/backfill/error_handlers/test.rs
@@ -0,0 +1,27 @@
+use super::*;
+
+#[test]
+fn detects_coordinator_failure_without_replaying_unclaimed_transition() {
+ let permanent = anyhow::Error::new(GoogleCalendarBackfillRunError::Permanent(
+ "invalid event".into(),
+ ));
+ assert_eq!(coordinator_reauth_edge(&permanent), Some(false));
+}
+
+#[test]
+fn preserves_coordinator_reauth_notification_edge_through_context() {
+ let reauth = anyhow::Error::new(GoogleCalendarBackfillRunError::ReauthRequired {
+ message: "insufficient permissions".into(),
+ link_reauth_transitioned: true,
+ })
+ .context("calendar worker failed");
+ assert_eq!(coordinator_reauth_edge(&reauth), Some(true));
+}
+
+#[test]
+fn prelease_failure_has_no_coordinator_marker() {
+ assert_eq!(
+ coordinator_reauth_edge(&anyhow::anyhow!("token refresh failed")),
+ None
+ );
+}
diff --git a/services/email_service/src/pubsub/backfill/increment_counters.rs b/services/email_service/src/pubsub/backfill/increment_counters.rs
index 18dd918a1e..fdb7bf634f 100644
--- a/services/email_service/src/pubsub/backfill/increment_counters.rs
+++ b/services/email_service/src/pubsub/backfill/increment_counters.rs
@@ -1,387 +1,44 @@
-use crate::pubsub::context::PubSubContext;
-use crate::pubsub::util::cg_refresh_email;
-use contacts::domain::models::messages::ContactConnection;
-use contacts::domain::ports::ContactsIngress;
-use macro_user_id::user_id::MacroUserIdStr;
-use models_email::api::refresh::{BackfillStatus, RefreshEmailEvent};
-use models_email::db::address::EmailRecipientType;
-use models_email::service::attachment::{
- AttachmentUploadArgs, AttachmentUploadDestination, AttachmentUploadMetadata,
+use crate::{backfill_completion_service, pubsub::context::PubSubContext};
+use models_email::service::{
+ backfill::BackfillMessagePayload, link::Link, pubsub::ProcessingError,
};
-use models_email::service::backfill::{
- BackfillAttachmentPayload, BackfillJobStatus, BackfillMessagePayload, BackfillOperation,
- BackfillPubsubMessage, JobScopedPayload, UpdateMetadataPayload,
-};
-use models_email::service::link::Link;
-use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
use uuid::Uuid;
-/// Emit a `refresh_email` event once per this many completed threads, plus on
-/// the first completed thread and at job completion, rather than per-thread.
-/// The first-thread emit ensures backfills smaller than this interval still
-/// surface a progress event before completing.
-const REFRESH_EMAIL_THREAD_INTERVAL: i32 = 50;
-
-/// called when a thread has completed processing. checks if it is the last thread to be processed
-/// for the job, and if so, performs the necessary actions for job completion.
+/// Map a completed thread delivery into the backfill application service.
#[tracing::instrument(skip(ctx))]
pub async fn incr_completed_threads(
ctx: &PubSubContext,
link: &Link,
job_id: Uuid,
) -> Result<(), ProcessingError> {
- let progress = ctx
- .redis_client
- .incr_completed_threads(job_id)
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::RedisQueryFailed,
- source: e.context("Failed to increment completed thread count"),
- })
- })?;
-
- if progress.job_complete
- || progress.completed_threads == 1
- || progress.completed_threads % REFRESH_EMAIL_THREAD_INTERVAL == 0
- {
- cg_refresh_email(
- &ctx.connection_gateway_client,
- link.macro_id.as_ref(),
- RefreshEmailEvent::BackfillProgress {
- link_id: link.id,
- status: if progress.job_complete {
- BackfillStatus::Complete
- } else {
- BackfillStatus::Progress
- },
- completed_threads: progress.completed_threads,
- total_threads: progress.total_threads,
- },
- )
- .await;
- }
-
- if progress.job_complete {
- tracing::info!(
- job_id = job_id.to_string(),
- "All threads for job have been processed"
- );
- handle_job_completed(ctx, link, job_id).await?;
- }
-
- Ok(())
+ backfill_completion_service::incr_completed_threads(ctx, link, job_id).await
}
-/// If this message was the last one in the thread to be processed, proceed to next stage of backfill for thread
-#[tracing::instrument(skip(ctx))]
+/// Map a completed message delivery into the backfill application service.
+#[tracing::instrument(skip(ctx, payload))]
pub async fn incr_completed_messages(
ctx: &PubSubContext,
link: &Link,
job_id: Uuid,
- p: &BackfillMessagePayload,
+ payload: &BackfillMessagePayload,
) -> Result<(), ProcessingError> {
- let thread_backfill_complete = ctx
- .redis_client
- .incr_completed_messages(job_id, &p.thread_provider_id)
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::RedisQueryFailed,
- source: e.context(format!(
- "Handle completed message for {} failed",
- p.message_provider_id
- )),
- })
- })?;
-
- if thread_backfill_complete {
- handle_thread_completed(ctx, link, job_id, p).await?;
- }
-
- Ok(())
+ backfill_completion_service::incr_completed_messages(ctx, link, job_id, payload).await
}
-/// performs actions when all threads and messages have been backfilled for the user.
-#[tracing::instrument(skip(ctx))]
+/// Complete an email scan and its associated calendar extraction.
pub(super) async fn handle_job_completed(
ctx: &PubSubContext,
- link: &Link,
job_id: Uuid,
+ init_lease_token: Option<Uuid>,
) -> Result<(), ProcessingError> {
- tracing::info!("Backfill complete for job {}", job_id);
- email_db_client::backfill::job::update::update_backfill_job_status(
- &ctx.db,
- job_id,
- BackfillJobStatus::Complete,
- )
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to update thread status to complete"),
- })
- })?;
-
- handle_attachment_upload(ctx, link, job_id).await?;
- handle_contacts_sync(ctx, link).await?;
-
- Ok(())
+ backfill_completion_service::handle_job_completed(ctx, job_id, init_lease_token).await
}
-#[tracing::instrument(skip(ctx))]
-async fn handle_attachment_upload(
- ctx: &PubSubContext,
- link: &Link,
- job_id: Uuid,
-) -> Result<(), ProcessingError> {
- if cfg!(not(feature = "attachment_upload")) {
- return Ok(());
- }
-
- let attachments =
- email_db_client::attachments::provider::upload::fetch_job_attachments_for_backfill(
- &ctx.db, link.id,
- )
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to fetch job attachment backfill metadata".to_string()),
- })
- })?;
-
- if !attachments.is_empty() {
- tracing::debug!(
- "Found {} condition 5 attachments to backfill for job {}",
- attachments.len(),
- job_id
- );
-
- send_attachment_backfill_messages(ctx, link.id, job_id, attachments).await?;
- }
-
- Ok(())
-}
-
-#[tracing::instrument(skip(ctx))]
-async fn handle_contacts_sync(ctx: &PubSubContext, link: &Link) -> Result<(), ProcessingError> {
- if cfg!(not(feature = "contacts_sync")) {
- return Ok(());
- }
-
- let email_addresses =
- email_db_client::contacts::get::fetch_contacts_emails_by_link_id(&ctx.db, link.id)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to fetch contact email addresses".to_string()),
- })
- })?;
-
- let length = email_addresses.len();
-
- tracing::info!(
- "Populating {} contacts for macro email {}",
- length,
- link.macro_id
- );
-
- let connections = email_addresses
- .iter()
- .map(|email| {
- MacroUserIdStr::try_from_email(email)
- .map(|contact| ContactConnection::new(link.macro_id.clone(), contact))
- })
- .collect::<Result<Vec<_>, _>>()
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::SqsEnqueueFailed,
- source: anyhow::anyhow!(e).context("invalid user email for contacts"),
- })
- })?;
-
- ctx.contacts_ingress
- .enqueue_contact_connections(connections)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::SqsEnqueueFailed,
- source: anyhow::anyhow!("{e:?}").context(format!(
- "Failed to enqueue contacts message for {}",
- email_addresses.join(", ")
- )),
- })
- })?;
-
- tracing::info!(
- "Successfully populated {} contacts for macro email {}",
- length,
- link.macro_id
- );
-
- Ok(())
-}
-
-/// when a thread is done being backfilled, update its metadata and backfill its attachments.
-#[tracing::instrument(skip(ctx))]
-async fn handle_thread_completed(
- ctx: &PubSubContext,
- link: &Link,
- job_id: Uuid,
- p: &BackfillMessagePayload,
-) -> Result<(), ProcessingError> {
- let new_payload = UpdateMetadataPayload {
- thread_provider_id: p.thread_provider_id.clone(),
- thread_db_id: p.thread_db_id,
- };
-
- let ps_message = BackfillPubsubMessage {
- backfill_operation: BackfillOperation::UpdateThreadMetadata(JobScopedPayload {
- link_id: link.id,
- job_id,
- payload: new_payload,
- }),
- };
-
- ctx.sqs_client
- .enqueue_email_backfill_message(ps_message)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::SqsEnqueueFailed,
- source: e.context("Failed to enqueue metadata message".to_string()),
- })
- })?;
-
- handle_thread_attachment_upload(ctx, link, job_id, p.thread_db_id).await?;
-
- Ok(())
-}
-
-/// Creates BackfillAttachmentPayload messages and enqueues them via SQS for the given attachments
-#[tracing::instrument(skip(ctx, attachments))]
-async fn send_attachment_backfill_messages(
+/// Map one completion-outbox delivery into the backfill application service.
+pub(super) async fn finalize_backfill(
ctx: &PubSubContext,
link_id: Uuid,
job_id: Uuid,
- attachments: Vec<AttachmentUploadMetadata>,
-) -> Result<(), ProcessingError> {
- if attachments.is_empty() {
- return Ok(());
- }
-
- let message_ids = attachments
- .iter()
- .map(|a| a.message_db_id)
- .collect::<Vec<_>>();
-
- let message_recipients =
- email_db_client::contacts::get::fetch_db_recipients_in_bulk(&ctx.db, &message_ids)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context(
- "Failed to fetch db recipients for thread attachment backfill".to_string(),
- ),
- })
- })?;
-
- for attachment in attachments {
- // get the email addresses of the recipients of the message
- let recipient_emails: Vec<String> = message_recipients
- .get(&attachment.message_db_id)
- .map(|v| v.as_slice())
- .unwrap_or(&[])
- .iter()
- .filter(|(_, recipient_type)| *recipient_type == EmailRecipientType::To)
- .filter_map(|(contact, _)| contact.email_address.clone())
- .collect();
-
- let upload_destination = if matches!(
- attachment.mime_type.split('/').next(),
- Some("image" | "video")
- ) {
- AttachmentUploadDestination::Sfs
- } else {
- AttachmentUploadDestination::Dss
- };
-
- let attachment_upload_args = AttachmentUploadArgs {
- recipient_emails,
- attachment_metadata: attachment,
- backfill: true,
- upload_destination,
- };
-
- let new_payload = BackfillAttachmentPayload {
- metadata: attachment_upload_args,
- };
-
- let ps_message = BackfillPubsubMessage {
- backfill_operation: BackfillOperation::BackfillAttachment(JobScopedPayload {
- link_id,
- job_id,
- payload: new_payload,
- }),
- };
-
- ctx.sqs_client
- .enqueue_email_backfill_message(ps_message)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::SqsEnqueueFailed,
- source: e.context("Failed to enqueue attachment backfill message".to_string()),
- })
- })?;
- }
-
- Ok(())
-}
-
-#[tracing::instrument(skip(ctx))]
-async fn handle_thread_attachment_upload(
- ctx: &PubSubContext,
- link: &Link,
- job_id: Uuid,
- thread_db_id: Uuid,
) -> Result<(), ProcessingError> {
- if cfg!(not(feature = "attachment_upload")) {
- return Ok(());
- }
-
- let (attachments, attachments2) = tokio::try_join!(
- email_db_client::attachments::provider::upload::thread_document_atts_for_backfill(
- &ctx.db,
- thread_db_id,
- ),
- email_db_client::attachments::provider::upload::thread_media_atts_for_backfill(
- &ctx.db,
- thread_db_id,
- )
- )
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to fetch thread attachment backfill metadata".to_string()),
- })
- })?;
-
- let mut all_attachments = attachments;
- all_attachments.extend(attachments2);
-
- if !all_attachments.is_empty() {
- tracing::debug!(
- "Found {} attachments to backfill for thread {}",
- all_attachments.len(),
- thread_db_id
- );
-
- send_attachment_backfill_messages(ctx, link.id, job_id, all_attachments).await?;
- }
-
- Ok(())
+ backfill_completion_service::finalize_backfill(ctx, link_id, job_id).await
}
diff --git a/services/email_service/src/pubsub/backfill/init.rs b/services/email_service/src/pubsub/backfill/init.rs
index 022136a0c3..b839f598aa 100644
--- a/services/email_service/src/pubsub/backfill/init.rs
+++ b/services/email_service/src/pubsub/backfill/init.rs
@@ -1,154 +1,22 @@
-use super::increment_counters;
-use crate::pubsub::context::PubSubContext;
-use crate::pubsub::util::{CheckGmailRateLimitArgs, check_gmail_rate_limit};
-use crate::util::process_pre_insert::sync_labels::sync_labels;
-use crate::util::sync_contacts::sync_contacts;
-use models_email::email::service::backfill::{
- BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
+use crate::{backfill_init_service, pubsub::context::PubSubContext};
+use models_email::email::service::{
+ backfill::{BackfillJob, InitPayload, JobScopedPayload},
+ link::Link,
+ pubsub::ProcessingError,
};
-use models_email::email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
-use models_email::email::service::thread::ListThreadsPayload;
-use models_email::email::service::{backfill, link};
-use models_email::gmail::operations::GmailApiOperation;
-use std::cmp::min;
-/// This step is invoked via the API when a new job is created.
-/// Populates total_threads value in backfill_job row, and sends the first ListThreads message.
-#[tracing::instrument(skip(ctx, access_token))]
+/// Map one initialization queue delivery into the email-backfill application service.
+#[tracing::instrument(
+ skip(ctx, access_token, link, backfill_job),
+ fields(link_id = %link.id, job_id = %backfill_job.id),
+ err
+)]
pub async fn init_backfill(
ctx: &PubSubContext,
access_token: &str,
scope: &JobScopedPayload<InitPayload>,
- link: &link::Link,
- backfill_job: &backfill::BackfillJob,
+ link: &Link,
+ backfill_job: &BackfillJob,
) -> Result<(), ProcessingError> {
- tracing::info!("Initializing backfill job");
-
- // ensure we have the user's labels in the db
- sync_labels(&ctx.db, &ctx.gmail_client, access_token, link.id)
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to sync labels"),
- })
- })?;
-
- if let Err(e) = sync_contacts(
- link,
- &ctx.db,
- &ctx.gmail_client,
- &ctx.sqs_client,
- access_token,
- )
- .await
- {
- tracing::error!(error = ?e, "Failed to sync contacts");
- }
-
- let threads_requested_limit = backfill_job.threads_requested_limit;
-
- check_gmail_rate_limit(CheckGmailRateLimitArgs {
- redis_client: &ctx.redis_client,
- link_id: link.id,
- gmail_operation: GmailApiOperation::UsersGetProfile,
- retryable: true,
- is_backfill: true,
- })
- .await?;
- // get the total number of threads the user has in their account
- let total_threads = match ctx
- .gmail_client
- .get_profile_threads_total(access_token)
- .await
- {
- Ok(list) => list,
- Err(e) => {
- // Construct the structured Retryable error and return immediately.
- return Err(ProcessingError::Retryable(DetailedError {
- reason: FailureReason::GmailApiFailed,
- source: e.context("Failed to get total threads from Gmail API"),
- }));
- }
- };
-
- // If thread count is not specified, populate all available threads. Otherwise,
- // populate up to the requested number or total available, whichever is smaller
- let total_threads = match threads_requested_limit {
- Some(requested) => min(total_threads, requested),
- None => total_threads,
- };
-
- email_db_client::backfill::job::update::update_job_total_threads(
- &ctx.db,
- scope.job_id,
- total_threads,
- )
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to update total_threads"),
- })
- })?;
-
- // With no threads to list, no per-thread message is ever enqueued, so the
- // per-thread completion path that finalizes the job never runs. Complete
- // the job directly instead.
- if total_threads == 0 {
- increment_counters::handle_job_completed(ctx, link, scope.job_id).await?;
- return Ok(());
- }
-
- ctx.redis_client
- .init_backfill_job_progress(scope.job_id, total_threads)
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::RedisQueryFailed,
- source: e.context("Failed to create entry in redis for job"),
- })
- })?;
-
- let thread_sqs_msg = BackfillPubsubMessage {
- backfill_operation: BackfillOperation::ListThreads(JobScopedPayload {
- link_id: scope.link_id,
- job_id: scope.job_id,
- payload: ListThreadsPayload {
- next_page_token: None, // a value of None tells the process to start from the beginning
- // Seed the user's most important threads first; this pass then
- // hands off to the normal most-recent-to-least sweep. Only for
- // full backfills: a bounded backfill (threads_requested_limit set)
- // must process an exact count, but the priority pass and the
- // newest-first sweep can each enqueue up to the limit, so their
- // non-overlapping threads would push the unique total past it.
- priority_pass: threads_requested_limit.is_none(),
- },
- }),
- };
-
- ctx.sqs_client
- .enqueue_email_backfill_message(thread_sqs_msg)
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::SqsEnqueueFailed,
- source: e.context("Failed to enqueue initial ListThreads message".to_string()),
- })
- })?;
-
- email_db_client::backfill::job::update::update_backfill_job_status(
- &ctx.db,
- scope.job_id,
- BackfillJobStatus::InProgress,
- )
- .await
- .map_err(|e| {
- ProcessingError::Retryable(DetailedError {
- reason: FailureReason::DatabaseQueryFailed,
- source: e.context("Failed to update backfill job status to InProgress"),
- })
- })?;
-
- Ok(())
+ backfill_init_service::init_backfill(ctx, access_token, scope, link, backfill_job).await
}
diff --git a/services/email_service/src/pubsub/backfill/list_threads.rs b/services/email_service/src/pubsub/backfill/list_threads.rs
index 3cd0b2401f..4ebebf74db 100644
--- a/services/email_service/src/pubsub/backfill/list_threads.rs
+++ b/services/email_service/src/pubsub/backfill/list_threads.rs
@@ -150,7 +150,7 @@ pub async fn list_threads(
// the listing returned none, so no per-thread message was ever
// enqueued and the per-thread completion path will never finalize the
// job. Complete it here.
- increment_counters::handle_job_completed(ctx, link, scope.job_id).await?;
+ increment_counters::handle_job_completed(ctx, scope.job_id, None).await?;
let _ = ctx
.redis_client
.delete_backfill_job_progress(scope.job_id)
diff --git a/services/email_service/src/pubsub/backfill/mod.rs b/services/email_service/src/pubsub/backfill/mod.rs
index d878985904..d17e28b051 100644
--- a/services/email_service/src/pubsub/backfill/mod.rs
+++ b/services/email_service/src/pubsub/backfill/mod.rs
@@ -1,6 +1,8 @@
mod backfill_attachment;
mod backfill_message;
mod backfill_thread;
+mod calendar_email_ics_backfill;
+mod calendar_google_backfill;
mod depopulate_crm_contact;
mod depopulate_crm_for_user;
mod error_handlers;
diff --git a/services/email_service/src/pubsub/backfill/process.rs b/services/email_service/src/pubsub/backfill/process.rs
index c524963408..1d975536e4 100644
--- a/services/email_service/src/pubsub/backfill/process.rs
+++ b/services/email_service/src/pubsub/backfill/process.rs
@@ -1,10 +1,13 @@
use crate::pubsub::backfill::{
- backfill_attachment, backfill_message, backfill_thread, depopulate_crm_contact,
- depopulate_crm_for_user, error_handlers, init, list_threads, populate_crm_contact,
- populate_crm_for_user, seed_sent_contact, update_metadata,
+ backfill_attachment, backfill_message, backfill_thread, calendar_email_ics_backfill,
+ calendar_google_backfill, depopulate_crm_contact, depopulate_crm_for_user, error_handlers,
+ increment_counters, init, list_threads, populate_crm_contact, populate_crm_for_user,
+ seed_sent_contact, update_metadata,
};
use crate::pubsub::context::PubSubContext;
-use crate::util::gmail::auth::fetch_token_or_mark_reauth;
+use crate::util::gmail::auth::{
+ fetch_token_or_mark_reauth, fetch_token_or_mark_reauth_no_cache, is_reauth_required_error,
+};
use anyhow::Context;
use models_email::email::service::backfill::{
BackfillJob, BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, JobScopedPayload,
@@ -63,7 +66,7 @@ async fn inner_process_message(
link,
backfill_job,
access_token,
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, false).await?
else {
return Ok(());
};
@@ -74,7 +77,7 @@ async fn inner_process_message(
link,
backfill_job,
access_token,
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, false).await?
else {
return Ok(());
};
@@ -83,7 +86,7 @@ async fn inner_process_message(
BackfillOperation::BackfillThread(scope) => {
let Some(JobContext {
link, access_token, ..
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, false).await?
else {
return Ok(());
};
@@ -92,7 +95,7 @@ async fn inner_process_message(
BackfillOperation::BackfillMessage(scope) => {
let Some(JobContext {
link, access_token, ..
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, false).await?
else {
// BackfillMessage owns per-thread progress in addition to
// the job-level progress that fetch_job_context already
@@ -114,7 +117,7 @@ async fn inner_process_message(
// fetch so a revoked token can't fail this handler with
// AccessTokenFetchFailed.
let Some(JobContextNoToken { link, .. }) =
- fetch_job_context_no_token(ctx, scope).await?
+ fetch_job_context_no_token(ctx, scope, false).await?
else {
return Ok(());
};
@@ -123,17 +126,57 @@ async fn inner_process_message(
BackfillOperation::BackfillAttachment(scope) => {
let Some(JobContext {
link, access_token, ..
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, true).await?
else {
return Ok(());
};
backfill_attachment::backfill_attachment(ctx, &access_token, &link, &scope.payload)
.await
}
+ BackfillOperation::CalendarGoogleBackfill(scope) => {
+ let link = fetch_link(ctx, scope.link_id).await?;
+ // Calendar jobs can be created immediately after an incremental
+ // Google scope grant. Bypass the Gmail token cache so this job does
+ // not reuse a pre-consent access token that lacks calendar scopes.
+ let access_token = fetch_token_or_mark_reauth_no_cache(
+ &link,
+ &ctx.db,
+ &ctx.redis_client,
+ &ctx.auth_service_client,
+ &ctx.sqs_client,
+ )
+ .await
+ .map_err(|e| {
+ let detail = DetailedError {
+ reason: FailureReason::AccessTokenFetchFailed,
+ source: e.context("failed to fetch token for Google Calendar backfill"),
+ };
+ if is_reauth_required_error(&detail.source) {
+ ProcessingError::NonRetryable(detail)
+ } else {
+ ProcessingError::Retryable(detail)
+ }
+ })?;
+ calendar_google_backfill::calendar_google_backfill(
+ ctx,
+ &access_token,
+ &link,
+ &scope.payload,
+ )
+ .await
+ }
+ BackfillOperation::CalendarEmailIcsBackfill(scope) => {
+ let link = fetch_link(ctx, scope.link_id).await?;
+ calendar_email_ics_backfill::calendar_email_ics_backfill(ctx, &link, &scope.payload)
+ .await
+ }
+ BackfillOperation::FinalizeBackfill(scope) => {
+ increment_counters::finalize_backfill(ctx, scope.link_id, scope.job_id).await
+ }
BackfillOperation::SeedSentContact(scope) => {
let Some(JobContext {
link, access_token, ..
- }) = fetch_job_context(ctx, scope).await?
+ }) = fetch_job_context(ctx, scope, false).await?
else {
return Ok(());
};
@@ -178,13 +221,14 @@ struct JobContextNoToken {
/// Shared prefetch for DB-only job-scoped handlers: loads the backfill
/// job, short-circuits on cancellation (also cleaning up the job-level
/// redis progress key), and loads the link. Returns `Ok(None)` when the
-/// parent backfill job has been cancelled — the caller should ack the
-/// message without running the handler. Variant-specific cancellation
-/// cleanup (e.g. per-thread progress keys) belongs at the call site, not
-/// here.
+/// parent backfill job is failed or cancelled, and normally when it is
+/// complete. Completion follow-up operations may opt into running after the
+/// terminal transition. Variant-specific cancellation cleanup (e.g.
+/// per-thread progress keys) belongs at the call site, not here.
async fn fetch_job_context_no_token<P>(
ctx: &PubSubContext,
scope: &JobScopedPayload<P>,
+ allow_complete: bool,
) -> Result<Option<JobContextNoToken>, ProcessingError> {
let backfill_job = email_db_client::backfill::job::get::get_backfill_job(&ctx.db, scope.job_id)
.await
@@ -201,7 +245,11 @@ async fn fetch_job_context_no_token<P>(
})
})?;
- if backfill_job.status == BackfillJobStatus::Cancelled {
+ if matches!(
+ backfill_job.status,
+ BackfillJobStatus::Cancelled | BackfillJobStatus::Failed
+ ) || (backfill_job.status == BackfillJobStatus::Complete && !allow_complete)
+ {
let _ = ctx
.redis_client
.delete_backfill_job_progress(scope.job_id)
@@ -216,13 +264,16 @@ async fn fetch_job_context_no_token<P>(
/// As [`fetch_job_context_no_token`], plus a fresh Gmail access token for
/// the link. For handlers that need to talk to Gmail (Init, ListThreads,
-/// BackfillThread, BackfillMessage, BackfillAttachment).
+/// BackfillThread, BackfillMessage, BackfillAttachment). The attachment
+/// finalization path opts into completed jobs because those messages are
+/// intentionally produced by [`BackfillOperation::FinalizeBackfill`].
async fn fetch_job_context<P>(
ctx: &PubSubContext,
scope: &JobScopedPayload<P>,
+ allow_complete: bool,
) -> Result<Option<JobContext>, ProcessingError> {
let Some(JobContextNoToken { link, backfill_job }) =
- fetch_job_context_no_token(ctx, scope).await?
+ fetch_job_context_no_token(ctx, scope, allow_complete).await?
else {
return Ok(None);
};
diff --git a/services/email_service/src/pubsub/backfill/worker.rs b/services/email_service/src/pubsub/backfill/worker.rs
index 57472939ac..88055f8677 100644
--- a/services/email_service/src/pubsub/backfill/worker.rs
+++ b/services/email_service/src/pubsub/backfill/worker.rs
@@ -1,6 +1,7 @@
use crate::pubsub::backfill::process;
use crate::pubsub::context::{
- CrmServiceType, NotificationIngressType, PubSubContext, PubSubEventBroker,
+ CalendarBackfillServices, CrmServiceType, NotificationIngressType, PubSubContext,
+ PubSubEventBroker,
};
use crate::pubsub::worker_lifecycle::run_until_cancelled;
use crate::util::redis::RedisClient;
@@ -77,6 +78,8 @@ pub async fn run_worker_with_cancellation(
notifications_enabled: bool,
cancellation_token: CancellationToken,
) {
+ let calendar_backfills =
+ CalendarBackfillServices::new(db.clone(), sqs_client.clone(), redis_client.clone());
let ctx = PubSubContext {
db,
sqs_worker: worker.clone(),
@@ -94,6 +97,7 @@ pub async fn run_worker_with_cancellation(
macro_event_broker,
notifications_enabled,
retry_worker: false,
+ calendar_backfills,
};
loop {
diff --git a/services/email_service/src/pubsub/calendar_backfill_adapters.rs b/services/email_service/src/pubsub/calendar_backfill_adapters.rs
new file mode 100644
index 0000000000..aa147973b9
--- /dev/null
+++ b/services/email_service/src/pubsub/calendar_backfill_adapters.rs
@@ -0,0 +1,293 @@
+//! Database and queue adapters for email-based calendar extraction.
+
+use calendar_events::domain::{
+ models::{
+ CalendarBackfillJobKey, EmailCalendarBackfillState, EmailCalendarScanAssociation,
+ EmailCalendarScanJob, EmailCalendarScanStatus,
+ },
+ ports::{
+ EmailCalendarBackfillPublisher, EmailCalendarBackfillRepository, GoogleProviderError,
+ GoogleProviderErrorKind,
+ },
+};
+use calendar_events::outbound::google::GoogleRequestGate;
+
+use crate::util::redis::RedisClient;
+
+/// Enforces the per-inbox Google Calendar API quota before each request.
+#[derive(Clone)]
+pub struct RedisCalendarRequestGate {
+ redis: RedisClient,
+}
+
+impl RedisCalendarRequestGate {
+ /// Construct the gate over the process-level Redis client.
+ pub fn new(redis: RedisClient) -> Self {
+ Self { redis }
+ }
+}
+
+impl GoogleRequestGate for RedisCalendarRequestGate {
+ async fn acquire(&self, email_link_id: Uuid) -> Result<(), GoogleProviderError> {
+ if self.redis.is_calendar_rate_limited(email_link_id).await {
+ return Err(GoogleProviderError::new(
+ GoogleProviderErrorKind::Transient,
+ "Google Calendar API rate limit reached for this inbox",
+ ));
+ }
+ Ok(())
+ }
+}
+use models_email::email::service::backfill::{
+ BackfillJobStatus, BackfillOperation, BackfillPubsubMessage, InitPayload, JobScopedPayload,
+};
+use rootcause::Report;
+use sqlx::PgPool;
+use sqs_client::SQS;
+use uuid::Uuid;
+
+/// MacroDB implementation of the email calendar-backfill repository.
+#[derive(Clone)]
+pub struct PgEmailCalendarBackfillRepository {
+ pool: PgPool,
+}
+
+impl PgEmailCalendarBackfillRepository {
+ /// Construct the repository from the shared MacroDB pool.
+ pub fn new(pool: PgPool) -> Self {
+ Self { pool }
+ }
+}
+
+impl EmailCalendarBackfillRepository for PgEmailCalendarBackfillRepository {
+ async fn get_email_calendar_backfill_state(
+ &self,
+ key: CalendarBackfillJobKey,
+ ) -> Result<EmailCalendarBackfillState, Report> {
+ let row = sqlx::query!(
+ r#"
+ SELECT
+ status,
+ (cursor->>'emailBackfillJobId')::uuid AS email_job_id
+ FROM calendar_backfill_jobs
+ WHERE id = $1
+ AND email_link_id = $2
+ AND kind = 'email_ics'
+ "#,
+ key.job_id,
+ key.email_link_id,
+ )
+ .fetch_optional(&self.pool)
+ .await
+ .map_err(report)?;
+ Ok(match row {
+ None => EmailCalendarBackfillState::NotFound,
+ Some(row) if row.status == "complete" => EmailCalendarBackfillState::Complete,
+ Some(row) => row
+ .email_job_id
+ .map(|email_job_id| EmailCalendarBackfillState::Associated { email_job_id })
+ .unwrap_or(EmailCalendarBackfillState::Unassociated),
+ })
+ }
+
+ async fn get_email_scan_job(
+ &self,
+ email_link_id: Uuid,
+ email_job_id: Uuid,
+ ) -> Result<Option<EmailCalendarScanJob>, Report> {
+ email_db_client::backfill::job::get::get_backfill_job_with_link_id(
+ &self.pool,
+ email_job_id,
+ email_link_id,
+ )
+ .await
+ .map(|job| job.map(to_scan_job))
+ .map_err(report)
+ }
+
+ async fn get_active_email_scan_job(
+ &self,
+ email_link_id: Uuid,
+ ) -> Result<Option<EmailCalendarScanJob>, Report> {
+ email_db_client::backfill::job::get::get_active_backfill_job(&self.pool, email_link_id)
+ .await
+ .map(|job| job.map(to_scan_job))
+ .map_err(report)
+ }
+
+ async fn create_email_scan_job(
+ &self,
+ email_link_id: Uuid,
+ fusionauth_user_id: &str,
+ ) -> Result<EmailCalendarScanJob, Report> {
+ if let Some(job) = email_db_client::backfill::job::insert::create_backfill_job(
+ &self.pool,
+ email_link_id,
+ fusionauth_user_id,
+ None,
+ )
+ .await
+ .map_err(report)?
+ {
+ return Ok(to_scan_job(job));
+ }
+ email_db_client::backfill::job::get::get_active_backfill_job(&self.pool, email_link_id)
+ .await
+ .map_err(report)?
+ .map(to_scan_job)
+ .ok_or_else(|| {
+ rootcause::report!("email backfill insert conflicted without an active job")
+ })
+ }
+
+ async fn associate_email_scan(
+ &self,
+ key: CalendarBackfillJobKey,
+ email_job_id: Uuid,
+ allow_in_progress: bool,
+ ) -> Result<EmailCalendarScanAssociation, Report> {
+ let mut tx = self.pool.begin().await.map_err(report)?;
+ let email_status = sqlx::query_scalar!(
+ r#"
+ SELECT status::text AS "status!"
+ FROM email_backfill_jobs
+ WHERE id = $1
+ AND link_id = $2
+ FOR UPDATE
+ "#,
+ email_job_id,
+ key.email_link_id,
+ )
+ .fetch_optional(&mut *tx)
+ .await
+ .map_err(report)?;
+ let Some(email_status) = email_status else {
+ tx.commit().await.map_err(report)?;
+ return Ok(EmailCalendarScanAssociation::NotFound);
+ };
+ let scan_status = parse_scan_status(&email_status)?;
+ if scan_status == EmailCalendarScanStatus::InProgress && !allow_in_progress {
+ tx.commit().await.map_err(report)?;
+ return Ok(EmailCalendarScanAssociation::Busy);
+ }
+ let (calendar_status, completed, last_error) = match scan_status {
+ EmailCalendarScanStatus::Init | EmailCalendarScanStatus::InProgress => {
+ ("running", false, None)
+ }
+ EmailCalendarScanStatus::Complete => ("complete", true, None),
+ EmailCalendarScanStatus::Failed => (
+ "failed",
+ true,
+ Some("associated email backfill ended before extraction completed"),
+ ),
+ };
+ let updated = sqlx::query!(
+ r#"
+ UPDATE calendar_backfill_jobs
+ SET status = $2,
+ started_at = COALESCE(started_at, now()),
+ completed_at = CASE WHEN $3 THEN now() ELSE NULL END,
+ cursor = jsonb_build_object('emailBackfillJobId', $4::uuid),
+ last_error = $5,
+ updated_at = now()
+ WHERE id = $1
+ AND email_link_id = $6
+ AND kind = 'email_ics'
+ AND status <> 'complete'
+ "#,
+ key.job_id,
+ calendar_status,
+ completed,
+ email_job_id,
+ last_error,
+ key.email_link_id,
+ )
+ .execute(&mut *tx)
+ .await
+ .map_err(report)?;
+ if updated.rows_affected() == 0 && calendar_status != "complete" {
+ tx.commit().await.map_err(report)?;
+ return Ok(EmailCalendarScanAssociation::NotFound);
+ }
+ tx.commit().await.map_err(report)?;
+ Ok(EmailCalendarScanAssociation::Associated(scan_status))
+ }
+
+ async fn fail_email_calendar_backfill(
+ &self,
+ key: CalendarBackfillJobKey,
+ message: &str,
+ ) -> Result<bool, Report> {
+ email_db_client::backfill::job::update::fail_email_ics_calendar_backfill_job(
+ &self.pool,
+ key.job_id,
+ key.email_link_id,
+ message,
+ )
+ .await
+ .map_err(report)
+ }
+}
+
+/// SQS implementation of email scan initialization publication.
+#[derive(Clone)]
+pub struct SqsEmailCalendarBackfillPublisher {
+ sqs: SQS,
+}
+
+impl SqsEmailCalendarBackfillPublisher {
+ /// Construct a publisher for the established email backfill queue.
+ pub fn new(sqs: SQS) -> Self {
+ Self { sqs }
+ }
+}
+
+impl EmailCalendarBackfillPublisher for SqsEmailCalendarBackfillPublisher {
+ async fn publish_email_scan_init(
+ &self,
+ email_link_id: Uuid,
+ email_job_id: Uuid,
+ ) -> Result<(), Report> {
+ self.sqs
+ .enqueue_email_backfill_message(BackfillPubsubMessage {
+ backfill_operation: BackfillOperation::Init(JobScopedPayload {
+ link_id: email_link_id,
+ job_id: email_job_id,
+ payload: InitPayload {},
+ }),
+ })
+ .await
+ .map_err(report)
+ }
+}
+
+fn to_scan_job(job: models_email::email::service::backfill::BackfillJob) -> EmailCalendarScanJob {
+ EmailCalendarScanJob {
+ id: job.id,
+ status: match job.status {
+ BackfillJobStatus::Init => EmailCalendarScanStatus::Init,
+ BackfillJobStatus::InProgress => EmailCalendarScanStatus::InProgress,
+ BackfillJobStatus::Complete => EmailCalendarScanStatus::Complete,
+ BackfillJobStatus::Cancelled | BackfillJobStatus::Failed => {
+ EmailCalendarScanStatus::Failed
+ }
+ },
+ is_full_scan: job.threads_requested_limit.is_none(),
+ }
+}
+
+fn parse_scan_status(status: &str) -> Result<EmailCalendarScanStatus, Report> {
+ match status {
+ "Init" => Ok(EmailCalendarScanStatus::Init),
+ "InProgress" => Ok(EmailCalendarScanStatus::InProgress),
+ "Complete" => Ok(EmailCalendarScanStatus::Complete),
+ "Cancelled" | "Failed" => Ok(EmailCalendarScanStatus::Failed),
+ _ => Err(rootcause::report!(
+ "unsupported email backfill status: {status}"
+ )),
+ }
+}
+
+fn report(error: impl std::fmt::Debug) -> Report {
+ rootcause::report!("{error:?}")
+}
diff --git a/services/email_service/src/pubsub/context.rs b/services/email_service/src/pubsub/context.rs
index 7b23e5ac51..249c0fdba9 100644
--- a/services/email_service/src/pubsub/context.rs
+++ b/services/email_service/src/pubsub/context.rs
@@ -1,5 +1,16 @@
+use crate::pubsub::calendar_backfill_adapters::{
+ PgEmailCalendarBackfillRepository, RedisCalendarRequestGate, SqsEmailCalendarBackfillPublisher,
+};
use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
+use calendar_events::{
+ domain::models::GoogleWatchConfig,
+ domain::service::{
+ EmailCalendarBackfillCoordinator, GoogleCalendarBackfillCoordinator,
+ GoogleCalendarBackfillFailureService,
+ },
+ outbound::{google::GoogleCalendarClient, pg::PgCalendarRepository},
+};
use connection_gateway_client::client::ConnectionGatewayClient;
use contacts::domain::service::SqsContactsIngress;
use contacts::outbound::ingress::SqsContactsQueue;
@@ -26,6 +37,76 @@ pub type PubSubEventBroker = MacroEventBrokerService<KafkaEventPublisher, TaskTr
/// The concrete notification ingress service type.
pub type NotificationIngressType = SqsNotificationIngress<SqsQueue>;
+/// Concrete Google Calendar backfill application service.
+pub type GoogleCalendarBackfillService = GoogleCalendarBackfillCoordinator<
+ PgCalendarRepository,
+ GoogleCalendarClient<RedisCalendarRequestGate>,
+ PgCalendarRepository,
+>;
+
+/// Concrete email-ICS backfill application service.
+pub type EmailIcsBackfillService = EmailCalendarBackfillCoordinator<
+ PgEmailCalendarBackfillRepository,
+ SqsEmailCalendarBackfillPublisher,
+>;
+
+/// Concrete pre-lease Google Calendar failure application service.
+pub type GoogleCalendarBackfillFailureHandler =
+ GoogleCalendarBackfillFailureService<PgCalendarRepository>;
+
+/// Calendar application services composed once when a worker starts.
+#[derive(Clone)]
+pub struct CalendarBackfillServices {
+ /// Google provider backfill coordinator.
+ pub google: Arc<GoogleCalendarBackfillService>,
+ /// Email-ICS backfill coordinator.
+ pub email_ics: Arc<EmailIcsBackfillService>,
+ /// Applies terminal provider failures that happen before a lease is claimed.
+ pub google_failure: Arc<GoogleCalendarBackfillFailureHandler>,
+}
+
+impl CalendarBackfillServices {
+ /// Compose calendar application services from process-level adapters.
+ pub fn new(db: PgPool, sqs_client: sqs_client::SQS, redis_client: RedisClient) -> Self {
+ let repository = PgCalendarRepository::new(db.clone());
+ Self {
+ google: Arc::new(GoogleCalendarBackfillCoordinator::new(
+ repository.clone(),
+ GoogleCalendarClient::with_gate(
+ reqwest::Client::builder()
+ .timeout(std::time::Duration::from_secs(30))
+ .build()
+ .expect("calendar client configuration is valid"),
+ RedisCalendarRequestGate::new(redis_client),
+ ),
+ repository.clone(),
+ calendar_watch_config(),
+ )),
+ email_ics: Arc::new(EmailCalendarBackfillCoordinator::new(
+ PgEmailCalendarBackfillRepository::new(db),
+ SqsEmailCalendarBackfillPublisher::new(sqs_client),
+ )),
+ google_failure: Arc::new(GoogleCalendarBackfillFailureService::new(repository)),
+ }
+ }
+}
+
+/// Push notification channels are opened only when both optional watch
+/// variables are configured; without them the 5-minute poll is the sole
+/// freshness mechanism.
+pub fn calendar_watch_config() -> Option<GoogleWatchConfig> {
+ // A variable set to an empty string must count as unset: a blank token
+ // would verify blank-header webhook requests.
+ let read = |name| {
+ macro_env_var::maybe_read_env(name)
+ .map(|value| value.trim().to_owned())
+ .filter(|value| !value.is_empty())
+ };
+ let address = read("CALENDAR_WATCH_WEBHOOK_URL")?;
+ let token = read("CALENDAR_WATCH_TOKEN")?;
+ Some(GoogleWatchConfig { address, token })
+}
+
/// The unfurl-backed resolver used when Apollo enrichment is disabled.
type UnfurlResolver = UnfurlCompanyMetadataResolver<
unfurl::domain::service::UnfurlServiceImpl<unfurl::outbound::ReqwestUnfurlFetcher>,
@@ -77,4 +158,5 @@ pub struct PubSubContext {
pub macro_event_broker: PubSubEventBroker,
pub notifications_enabled: bool,
pub retry_worker: bool,
+ pub calendar_backfills: CalendarBackfillServices,
}
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
index 3d02e86b31..4e26a21a10 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/upsert_message.rs
@@ -82,6 +82,8 @@ pub async fn upsert_message(
}
};
+ let calendar_payload = message_resource.payload.clone();
+
// Map Gmail resource to service model (IDs are generated in the parse function)
let message = map_message_resource_to_service(message_resource, link.id).map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
@@ -276,6 +278,30 @@ pub async fn upsert_message(
publish_email_event(&ctx.macro_event_broker, &event);
}
+ crate::calendar_ingest::ingest_calendar_parts(
+ &ctx.db,
+ &ctx.gmail_client,
+ &ctx.redis_client,
+ crate::calendar_ingest::CalendarIngestInput {
+ is_backfill: false,
+ access_token: &gmail_access_token,
+ owner_id: link.macro_id.as_ref(),
+ email_link_id: link.id,
+ email_thread_id: thread_db_id,
+ email_message_id: message_db_id,
+ provider_message_id: &payload.provider_message_id,
+ payload: &calendar_payload,
+ },
+ )
+ .await
+ .inspect_err(|error| {
+ // Calendar extraction is best-effort: a Gmail or database failure
+ // here must not block the message's core side effects, and the
+ // durable email-ICS backfill re-extracts anything missed.
+ tracing::warn!(error = ?error, "failed to extract calendar invitation");
+ })
+ .ok();
+
handle_attachment_upload(
ctx,
&gmail_access_token,
diff --git a/services/email_service/src/pubsub/inbox_sync/worker.rs b/services/email_service/src/pubsub/inbox_sync/worker.rs
index 302020a089..8c0ae90eae 100644
--- a/services/email_service/src/pubsub/inbox_sync/worker.rs
+++ b/services/email_service/src/pubsub/inbox_sync/worker.rs
@@ -1,5 +1,6 @@
use crate::pubsub::context::{
- CrmServiceType, NotificationIngressType, PubSubContext, PubSubEventBroker,
+ CalendarBackfillServices, CrmServiceType, NotificationIngressType, PubSubContext,
+ PubSubEventBroker,
};
use crate::pubsub::inbox_sync::process;
use crate::pubsub::worker_lifecycle::run_until_cancelled;
@@ -80,6 +81,8 @@ pub async fn run_worker_with_cancellation(
retry_worker: bool,
cancellation_token: CancellationToken,
) {
+ let calendar_backfills =
+ CalendarBackfillServices::new(db.clone(), sqs_client.clone(), redis_client.clone());
let ctx = PubSubContext {
db,
sqs_worker: worker.clone(),
@@ -97,6 +100,7 @@ pub async fn run_worker_with_cancellation(
macro_event_broker,
notifications_enabled,
retry_worker,
+ calendar_backfills,
};
loop {
diff --git a/services/email_service/src/pubsub/mod.rs b/services/email_service/src/pubsub/mod.rs
index fd70404efe..e34bbceeef 100644
--- a/services/email_service/src/pubsub/mod.rs
+++ b/services/email_service/src/pubsub/mod.rs
@@ -1,5 +1,7 @@
pub mod backfill;
-pub(crate) mod context;
+/// Outbound adapters used by calendar backfill application services.
+pub mod calendar_backfill_adapters;
+pub mod context;
/// The flag-selected CRM metadata resolver, re-exported for the
/// pubsub_workers binary to construct.
pub use context::CrmMetadataResolver;
diff --git a/services/email_service/src/util/redis/rate_limit.rs b/services/email_service/src/util/redis/rate_limit.rs
index fb8ae5c15f..47ccf044ff 100644
--- a/services/email_service/src/util/redis/rate_limit.rs
+++ b/services/email_service/src/util/redis/rate_limit.rs
@@ -87,6 +87,61 @@ impl RedisClient {
}
}
+/// Google Calendar's per-user quota is 600 queries per minute; cap our
+/// background sync at half of it so interactive calendar features keep room.
+const CALENDAR_RATE_LIMIT_QUERIES: u32 = 300;
+
+impl RedisClient {
+ /// Checks if a Google Calendar API request is rate-limited, using the
+ /// same atomic sliding-window Lua script as the Gmail limiter but with a
+ /// calendar-specific key and Calendar's flat one-query-per-request cost.
+ pub async fn is_calendar_rate_limited(&self, email_link_id: Uuid) -> bool {
+ let mut con = match self.inner.get_multiplexed_async_connection().await {
+ Ok(conn) => conn,
+ Err(e) => {
+ tracing::warn!(
+ "Failed to get Redis connection for calendar rate limiting link {}: {}",
+ email_link_id,
+ e
+ );
+ return false;
+ }
+ };
+
+ let redis_key = format!("calendar-ratelimit:log:{}", email_link_id);
+ let now_micros = SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .expect("Time went backwards")
+ .as_micros() as u64;
+ let cost = 1u32;
+ let member_id = format!("{}:{}", cost, Uuid::new_v4());
+ let script = Script::new(get_rate_limit_script_with_usage());
+
+ let (is_limited, _current_units): (i32, u32) = match script
+ .key(&redis_key)
+ .arg(CALENDAR_RATE_LIMIT_QUERIES)
+ .arg(self.rate_limit_secs * 1_000_000)
+ .arg(now_micros)
+ .arg(cost)
+ .arg(&member_id)
+ .invoke_async(&mut con)
+ .await
+ {
+ Ok(res) => res,
+ Err(e) => {
+ tracing::warn!(
+ "Failed to execute calendar rate limit script for link {}: {}",
+ email_link_id,
+ e
+ );
+ return false;
+ }
+ };
+
+ is_limited == 1
+ }
+}
+
/// Returns the raw Lua script for an atomic, cost-based sliding window rate limiter.
///
/// The script is designed to be executed atomically on the Redis server. It tracks the
diff --git a/tooling/just/rust.just b/tooling/just/rust.just
index 05ffcac20a..2f444a7018 100644
--- a/tooling/just/rust.just
+++ b/tooling/just/rust.just
@@ -118,6 +118,7 @@ setup_test_envs:
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./services/ai_projections_refresh_handler/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/webhook/.env
echo "DATABASE_URL={{ DATABASE_URL }}" >> ./crates/calendar_events/.env
+ echo "DATABASE_URL={{ DATABASE_URL }}" >> ./services/email_service/.env
setup_macrodb:
just crates/macro_db_client/create_db