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.

refactor(calendar): remove email ICS calendar ingestion (#5477)

gbirmana7c280ed1457
diff --git a/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json b/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json
deleted file mode 100644
index 6403eaf45b..0000000000
--- a/.sqlx/query-0cc568c6fb4a226b9bfb63d32743b87e8aad56d5bb50e510a15938f9dbe7c615.json
+++ /dev/null
@@ -1,29 +0,0 @@
-{
- "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-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json b/.sqlx/query-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json
deleted file mode 100644
index 15bee76a6b..0000000000
--- a/.sqlx/query-4a3e403985e8dec8c2e47996424b7b81af6f5461e760f67c03e0ee879ed47cb7.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "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-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json b/.sqlx/query-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json
deleted file mode 100644
index de1abd019a..0000000000
--- a/.sqlx/query-503ae4083485cb55f9ce567f127bd35d3a18148cecaf4661893accbeb0bda78c.json
+++ /dev/null
@@ -1,22 +0,0 @@
-{
- "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-271758757a6ddfc04a2f5491ce9c33009589c65ec82f5b0714c7cb62da10a31b.json b/.sqlx/query-659c672b08652a8f9d9fcb5bafa65b00a318a92755abc921d43f8be1692f9ccd.json
similarity index 84%
rename from .sqlx/query-271758757a6ddfc04a2f5491ce9c33009589c65ec82f5b0714c7cb62da10a31b.json
rename to .sqlx/query-659c672b08652a8f9d9fcb5bafa65b00a318a92755abc921d43f8be1692f9ccd.json
index 2641ceef1b..70c5a6b411 100644
--- a/.sqlx/query-271758757a6ddfc04a2f5491ce9c33009589c65ec82f5b0714c7cb62da10a31b.json
+++ b/.sqlx/query-659c672b08652a8f9d9fcb5bafa65b00a318a92755abc921d43f8be1692f9ccd.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'pending',\n cursor = '{}',\n scanned_count = 0,\n extracted_count = 0,\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE email_link_id = $1\n AND grant_version = $2\n AND status = 'failed'\n RETURNING id, account_id, kind\n ",
+ "query": "\n UPDATE calendar_backfill_jobs\n SET status = 'pending',\n cursor = '{}',\n scanned_count = 0,\n extracted_count = 0,\n last_error = NULL,\n started_at = NULL,\n completed_at = NULL,\n lease_expires_at = NULL,\n updated_at = now()\n WHERE email_link_id = $1\n AND grant_version = $2\n AND status = 'failed'\n AND kind = 'google_calendar'\n RETURNING id, account_id, kind\n ",
"describe": {
"columns": [
{
@@ -31,5 +31,5 @@
false
]
},
- "hash": "271758757a6ddfc04a2f5491ce9c33009589c65ec82f5b0714c7cb62da10a31b"
+ "hash": "659c672b08652a8f9d9fcb5bafa65b00a318a92755abc921d43f8be1692f9ccd"
}
diff --git a/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json b/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json
deleted file mode 100644
index 2e69b5e53e..0000000000
--- a/.sqlx/query-693bf48cdf57ee72fed1f97f82753e5205058160fe3e63acb40fc47cee875cf9.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "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-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json b/.sqlx/query-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json
deleted file mode 100644
index fe122352df..0000000000
--- a/.sqlx/query-769cd5edd2ad881b84447bc7dc503e008c1f1835a7e9359c1075cb5837ae5ae6.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json b/.sqlx/query-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json
deleted file mode 100644
index 6250c06e71..0000000000
--- a/.sqlx/query-920e2e8f9bfb55ce11b023c87c69e7d56019cc971f3efd2cbcec434fe58752af.json
+++ /dev/null
@@ -1,23 +0,0 @@
-{
- "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-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json b/.sqlx/query-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json
deleted file mode 100644
index 2019fa09e7..0000000000
--- a/.sqlx/query-b9cf703df1bab99513dbe4ba2ff25119ff126e96b0997dc43db8ae1546c917a2.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "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-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json b/.sqlx/query-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json
deleted file mode 100644
index 04857c37b6..0000000000
--- a/.sqlx/query-c0f012ba1124fbe7e0a2113e59dc24440b56b43b2b7c27c69401354e29c34eaa.json
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "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-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json b/.sqlx/query-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json
deleted file mode 100644
index 1dfe13ef1f..0000000000
--- a/.sqlx/query-cdabfcccb9a0416688755fa9668a4eb40987e36172f0ce8637b9fd44b0214188.json
+++ /dev/null
@@ -1,19 +0,0 @@
-{
- "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/Cargo.lock b/Cargo.lock
index 682cb1e41c..0143ec4775 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2522,7 +2522,6 @@ dependencies = [
"axum",
"chrono",
"futures",
- "icalendar",
"macro_authorization",
"macro_db_migrator",
"models_pagination",
@@ -2530,7 +2529,6 @@ dependencies = [
"rootcause",
"serde",
"serde_json",
- "sha2 0.10.9",
"sqlx",
"thiserror 2.0.18",
"tokio",
@@ -7177,22 +7175,6 @@ 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"
@@ -7693,15 +7675,6 @@ 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"
@@ -9918,15 +9891,6 @@ 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"
@@ -12514,19 +12478,6 @@ 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"
@@ -16796,7 +16747,6 @@ dependencies = [
"bzip2-sys",
"cc",
"chrono",
- "chrono-tz",
"crossbeam-epoch",
"crossbeam-utils",
"crypto-bigint",
@@ -16862,7 +16812,6 @@ 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 56ef3ee5aa..d33cf816aa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -156,7 +156,6 @@ 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 6327b01838..8cfc421cb5 100644
--- a/crates/calendar_events/Cargo.toml
+++ b/crates/calendar_events/Cargo.toml
@@ -7,7 +7,6 @@ version = "0.1.0"
[features]
default = []
google = ["dep:reqwest", "dep:urlencoding"]
-ics = ["dep:icalendar", "dep:sha2"]
inbound = [
"dep:axum",
"dep:macro_authorization",
@@ -21,7 +20,6 @@ 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 }
@@ -30,7 +28,6 @@ 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 }
diff --git a/crates/calendar_events/src/domain/models.rs b/crates/calendar_events/src/domain/models.rs
index c78ea5f5af..698e39e077 100644
--- a/crates/calendar_events/src/domain/models.rs
+++ b/crates/calendar_events/src/domain/models.rs
@@ -679,8 +679,6 @@ pub struct GoogleCalendarSyncSnapshot {
pub enum CalendarBackfillKind {
/// Fetch calendars and canonical provider events.
GoogleCalendar,
- /// Re-scan existing email for iCalendar MIME parts.
- EmailIcs,
}
impl CalendarBackfillKind {
@@ -688,7 +686,6 @@ impl CalendarBackfillKind {
pub fn as_str(self) -> &'static str {
match self {
Self::GoogleCalendar => "google_calendar",
- Self::EmailIcs => "email_ics",
}
}
}
@@ -760,57 +757,6 @@ pub struct CalendarBackfillFailureOutcome {
pub link_reauth_transitioned: bool,
}
-/// Durable state of the email scan associated with calendar extraction.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum EmailCalendarScanStatus {
- /// The email scan is created but has not begun listing threads.
- Init,
- /// The email scan is actively processing messages.
- InProgress,
- /// The scan and calendar extraction completed.
- Complete,
- /// The scan ended before calendar extraction completed.
- Failed,
-}
-
-/// Minimal email scan identity needed by calendar backfill policy.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub struct EmailCalendarScanJob {
- /// Durable email backfill job identifier.
- pub id: Uuid,
- /// Current scan lifecycle state.
- pub status: EmailCalendarScanStatus,
- /// Whether the scan covers the entire mailbox rather than a bounded subset.
- pub is_full_scan: bool,
-}
-
-/// Durable state of an email-ICS calendar backfill.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum EmailCalendarBackfillState {
- /// No email scan has been associated yet.
- Unassociated,
- /// A scan is already durably associated.
- Associated {
- /// Durable email scan identifier.
- email_job_id: Uuid,
- },
- /// Calendar extraction already completed.
- Complete,
- /// No matching email-ICS calendar job exists.
- NotFound,
-}
-
-/// Result of atomically associating an email scan with calendar extraction.
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum EmailCalendarScanAssociation {
- /// The scan was associated at its current safe state.
- Associated(EmailCalendarScanStatus),
- /// An unrelated scan is already in progress and cannot provide a full rescan.
- Busy,
- /// The email scan no longer exists.
- NotFound,
-}
-
/// Result of applying an OAuth grant.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppliedGoogleGrant {
diff --git a/crates/calendar_events/src/domain/ports.rs b/crates/calendar_events/src/domain/ports.rs
index 457aa2fae3..e74befb08b 100644
--- a/crates/calendar_events/src/domain/ports.rs
+++ b/crates/calendar_events/src/domain/ports.rs
@@ -9,8 +9,7 @@ use uuid::Uuid;
use super::models::{
AppliedGoogleGrant, CalendarBackfillClaim, CalendarBackfillFailureDisposition,
CalendarBackfillFailureOutcome, CalendarBackfillJobKey, CalendarEvent, CalendarEventUpsert,
- CalendarOccurrence, CalendarOccurrenceCursor, CalendarSyncStatus, EmailCalendarBackfillState,
- EmailCalendarScanAssociation, EmailCalendarScanJob, GoogleCalendarSyncSnapshot,
+ CalendarOccurrence, CalendarOccurrenceCursor, CalendarSyncStatus, GoogleCalendarSyncSnapshot,
GoogleEventSyncBatch, GoogleScopeSet, GoogleSyncPlan, GoogleWatchChannel, GoogleWatchConfig,
OccurrenceRange, ProviderCalendar, StoredGoogleCalendar,
};
@@ -282,58 +281,3 @@ pub trait CalendarBackfillRepository: Send + Sync + 'static {
message: &str,
) -> impl Future<Output = Result<CalendarBackfillFailureOutcome, Report>> + Send;
}
-
-/// Durable email-scan operations used by calendar extraction policy.
-pub trait EmailCalendarBackfillRepository: Send + Sync + 'static {
- /// Load the calendar job and any scan already associated with it.
- fn get_email_calendar_backfill_state(
- &self,
- key: CalendarBackfillJobKey,
- ) -> impl Future<Output = Result<EmailCalendarBackfillState, Report>> + Send;
-
- /// Load one associated email scan.
- fn get_email_scan_job(
- &self,
- email_link_id: Uuid,
- email_job_id: Uuid,
- ) -> impl Future<Output = Result<Option<EmailCalendarScanJob>, Report>> + Send;
-
- /// Return the active email scan for an inbox, if one exists.
- fn get_active_email_scan_job(
- &self,
- email_link_id: Uuid,
- ) -> impl Future<Output = Result<Option<EmailCalendarScanJob>, Report>> + Send;
-
- /// Create a full email scan, returning the winner of any concurrent insert.
- fn create_email_scan_job(
- &self,
- email_link_id: Uuid,
- fusionauth_user_id: &str,
- ) -> impl Future<Output = Result<EmailCalendarScanJob, Report>> + Send;
-
- /// Atomically associate a scan, optionally accepting an already-started
- /// scan only when it was associated by an earlier delivery.
- fn associate_email_scan(
- &self,
- key: CalendarBackfillJobKey,
- email_job_id: Uuid,
- allow_in_progress: bool,
- ) -> impl Future<Output = Result<EmailCalendarScanAssociation, Report>> + Send;
-
- /// Atomically terminate an active email-ICS job and its unpublished scan.
- fn fail_email_calendar_backfill(
- &self,
- key: CalendarBackfillJobKey,
- message: &str,
- ) -> impl Future<Output = Result<bool, Report>> + Send;
-}
-
-/// Queue publication required to begin a newly associated email scan.
-pub trait EmailCalendarBackfillPublisher: Send + Sync + 'static {
- /// Publish the idempotent email scan initialization message.
- fn publish_email_scan_init(
- &self,
- email_link_id: Uuid,
- email_job_id: Uuid,
- ) -> impl Future<Output = Result<(), Report>> + Send;
-}
diff --git a/crates/calendar_events/src/domain/service.rs b/crates/calendar_events/src/domain/service.rs
index 3dfb9e8f6d..cf8dfa7410 100644
--- a/crates/calendar_events/src/domain/service.rs
+++ b/crates/calendar_events/src/domain/service.rs
@@ -9,14 +9,12 @@ use super::{
models::{
AppliedGoogleGrant, CalendarBackfillClaim, CalendarBackfillFailureDisposition,
CalendarBackfillFailureOutcome, CalendarBackfillJobKey, CalendarEventUpsert,
- CalendarOccurrenceCursor, EmailCalendarBackfillState, EmailCalendarScanAssociation,
- EmailCalendarScanStatus, GoogleCalendarSyncSnapshot, GoogleScopeSet, OccurrenceRange,
+ CalendarOccurrenceCursor, GoogleCalendarSyncSnapshot, GoogleScopeSet, OccurrenceRange,
},
ports::{
CalendarBackfillRepository, CalendarEventWrite, CalendarOccurrenceService,
- CalendarRepository, EmailCalendarBackfillPublisher, EmailCalendarBackfillRepository,
- GoogleCalendarProvider, GoogleCalendarSyncRepository, GoogleEventSyncContext,
- GoogleProviderError, GoogleProviderErrorKind,
+ CalendarRepository, GoogleCalendarProvider, GoogleCalendarSyncRepository,
+ GoogleEventSyncContext, GoogleProviderError, GoogleProviderErrorKind,
},
};
@@ -39,137 +37,6 @@ pub enum CalendarValidationError {
InvalidLimit,
}
-/// Queue-visible outcome of scheduling a full email rescan for ICS extraction.
-#[derive(Debug, thiserror::Error)]
-pub enum EmailCalendarBackfillRunError {
- /// An unrelated email scan is already in progress.
- #[error("email calendar extraction is waiting for the active email backfill")]
- Busy,
- /// The calendar job or its associated email scan was not found.
- #[error("email calendar extraction job was not found")]
- NotFound,
- /// The associated email scan already failed.
- #[error("email calendar extraction scan failed")]
- ScanFailed,
- /// Persistence or queue publication can be retried.
- #[error("email calendar extraction failed transiently: {0}")]
- Retryable(String),
-}
-
-/// Application service that associates a complete email scan with ICS extraction.
-pub struct EmailCalendarBackfillCoordinator<R, P> {
- repository: R,
- publisher: P,
-}
-
-impl<R, P> EmailCalendarBackfillCoordinator<R, P>
-where
- R: EmailCalendarBackfillRepository,
- P: EmailCalendarBackfillPublisher,
-{
- /// Construct the coordinator from persistence and queue ports.
- pub fn new(repository: R, publisher: P) -> Self {
- Self {
- repository,
- publisher,
- }
- }
-
- /// Start or resume one durable email-ICS calendar backfill.
- #[tracing::instrument(skip(self, fusionauth_user_id), fields(job_id = %key.job_id), err)]
- pub async fn run(
- &self,
- key: CalendarBackfillJobKey,
- fusionauth_user_id: &str,
- ) -> Result<(), EmailCalendarBackfillRunError> {
- let state = self
- .repository
- .get_email_calendar_backfill_state(key)
- .await
- .map_err(retryable_email_backfill)?;
- let (email_job, allow_in_progress) = match state {
- EmailCalendarBackfillState::Complete => return Ok(()),
- EmailCalendarBackfillState::NotFound => {
- return Err(EmailCalendarBackfillRunError::NotFound);
- }
- EmailCalendarBackfillState::Associated { email_job_id } => {
- let job = self
- .repository
- .get_email_scan_job(key.email_link_id, email_job_id)
- .await
- .map_err(retryable_email_backfill)?
- .ok_or(EmailCalendarBackfillRunError::NotFound)?;
- if !job.is_full_scan {
- return Err(EmailCalendarBackfillRunError::ScanFailed);
- }
- (job, true)
- }
- EmailCalendarBackfillState::Unassociated => {
- let job = match self
- .repository
- .get_active_email_scan_job(key.email_link_id)
- .await
- .map_err(retryable_email_backfill)?
- {
- Some(job) => job,
- None => self
- .repository
- .create_email_scan_job(key.email_link_id, fusionauth_user_id)
- .await
- .map_err(retryable_email_backfill)?,
- };
- if !job.is_full_scan || job.status == EmailCalendarScanStatus::InProgress {
- return Err(EmailCalendarBackfillRunError::Busy);
- }
- (job, false)
- }
- };
-
- let association = self
- .repository
- .associate_email_scan(key, email_job.id, allow_in_progress)
- .await
- .map_err(retryable_email_backfill)?;
- let status = match association {
- EmailCalendarScanAssociation::Associated(status) => status,
- EmailCalendarScanAssociation::Busy => {
- return Err(EmailCalendarBackfillRunError::Busy);
- }
- EmailCalendarScanAssociation::NotFound => {
- return Err(EmailCalendarBackfillRunError::NotFound);
- }
- };
- match status {
- EmailCalendarScanStatus::Complete => Ok(()),
- EmailCalendarScanStatus::Failed => Err(EmailCalendarBackfillRunError::ScanFailed),
- EmailCalendarScanStatus::InProgress => Ok(()),
- EmailCalendarScanStatus::Init => self
- .publisher
- .publish_email_scan_init(key.email_link_id, email_job.id)
- .await
- .map_err(retryable_email_backfill),
- }
- }
-
- /// Apply a terminal queue failure through the calendar lifecycle port.
- #[tracing::instrument(skip(self, message), fields(job_id = %key.job_id), err)]
- pub async fn fail_terminal(
- &self,
- key: CalendarBackfillJobKey,
- message: &str,
- ) -> Result<(), EmailCalendarBackfillRunError> {
- self.repository
- .fail_email_calendar_backfill(key, message)
- .await
- .map_err(retryable_email_backfill)?;
- Ok(())
- }
-}
-
-fn retryable_email_backfill(error: Report) -> EmailCalendarBackfillRunError {
- EmailCalendarBackfillRunError::Retryable(format!("{error:?}"))
-}
-
/// Calendar use cases with provider and persistence details behind ports.
pub struct CalendarService<R> {
repository: R,
diff --git a/crates/calendar_events/src/domain/service/test.rs b/crates/calendar_events/src/domain/service/test.rs
index 02941d3143..870ca00478 100644
--- a/crates/calendar_events/src/domain/service/test.rs
+++ b/crates/calendar_events/src/domain/service/test.rs
@@ -3,15 +3,13 @@ use crate::domain::{
models::{
AttendeeResponseStatus, CalendarAttendee, CalendarBackfillClaim,
CalendarBackfillFailureDisposition, CalendarBackfillJobKey, CalendarEvent,
- CalendarEventSource, CalendarOccurrence, CalendarSyncStatus, EmailCalendarBackfillState,
- EmailCalendarScanAssociation, EmailCalendarScanJob, EmailCalendarScanStatus,
- EmailIcsSource, EventStatus, EventTime, EventTransparency, EventVisibility,
- GOOGLE_CALENDAR_SCOPES, GoogleCalendarSyncSnapshot, GoogleEventSyncBatch,
- GoogleWatchChannel, GoogleWatchConfig, ProviderCalendar, StoredGoogleCalendar,
+ CalendarEventSource, CalendarOccurrence, CalendarSyncStatus, EmailIcsSource, EventStatus,
+ EventTime, EventTransparency, EventVisibility, GOOGLE_CALENDAR_SCOPES,
+ GoogleCalendarSyncSnapshot, GoogleEventSyncBatch, GoogleWatchChannel, GoogleWatchConfig,
+ ProviderCalendar, StoredGoogleCalendar,
},
ports::{
- CalendarBackfillRepository, CalendarEventWrite, CalendarRepository,
- EmailCalendarBackfillPublisher, EmailCalendarBackfillRepository, GoogleCalendarProvider,
+ CalendarBackfillRepository, CalendarEventWrite, CalendarRepository, GoogleCalendarProvider,
GoogleEventSyncContext, GoogleProviderError,
},
};
@@ -574,168 +572,3 @@ async fn google_coordinator_keeps_calendar_permission_health_separate_from_gmail
&[CalendarBackfillFailureDisposition::CalendarPermissionRequired]
);
}
-
-#[derive(Clone)]
-struct FakeEmailLifecycle {
- state: EmailCalendarBackfillState,
- active: Option<EmailCalendarScanJob>,
- associations: Arc<Mutex<usize>>,
- failures: Arc<Mutex<usize>>,
-}
-
-impl EmailCalendarBackfillRepository for FakeEmailLifecycle {
- async fn get_email_calendar_backfill_state(
- &self,
- _key: CalendarBackfillJobKey,
- ) -> Result<EmailCalendarBackfillState, Report> {
- Ok(self.state)
- }
-
- async fn get_email_scan_job(
- &self,
- _email_link_id: Uuid,
- _email_job_id: Uuid,
- ) -> Result<Option<EmailCalendarScanJob>, Report> {
- Ok(self.active)
- }
-
- async fn get_active_email_scan_job(
- &self,
- _email_link_id: Uuid,
- ) -> Result<Option<EmailCalendarScanJob>, Report> {
- Ok(self.active)
- }
-
- async fn create_email_scan_job(
- &self,
- _email_link_id: Uuid,
- _fusionauth_user_id: &str,
- ) -> Result<EmailCalendarScanJob, Report> {
- self.active
- .ok_or_else(|| rootcause::report!("test scan was not configured"))
- }
-
- async fn associate_email_scan(
- &self,
- _key: CalendarBackfillJobKey,
- _email_job_id: Uuid,
- _allow_in_progress: bool,
- ) -> Result<EmailCalendarScanAssociation, Report> {
- *self.associations.lock().unwrap() += 1;
- Ok(EmailCalendarScanAssociation::Associated(
- self.active.expect("configured scan").status,
- ))
- }
-
- async fn fail_email_calendar_backfill(
- &self,
- _key: CalendarBackfillJobKey,
- _message: &str,
- ) -> Result<bool, Report> {
- *self.failures.lock().unwrap() += 1;
- Ok(true)
- }
-}
-
-#[derive(Clone, Default)]
-struct FakeEmailPublisher {
- publications: Arc<Mutex<usize>>,
-}
-
-impl EmailCalendarBackfillPublisher for FakeEmailPublisher {
- async fn publish_email_scan_init(
- &self,
- _email_link_id: Uuid,
- _email_job_id: Uuid,
- ) -> Result<(), Report> {
- *self.publications.lock().unwrap() += 1;
- Ok(())
- }
-}
-
-#[tokio::test]
-async fn email_coordinator_waits_instead_of_joining_a_partial_scan() {
- let repository = FakeEmailLifecycle {
- state: EmailCalendarBackfillState::Unassociated,
- active: Some(EmailCalendarScanJob {
- id: Uuid::now_v7(),
- status: EmailCalendarScanStatus::InProgress,
- is_full_scan: true,
- }),
- associations: Arc::new(Mutex::new(0)),
- failures: Arc::new(Mutex::new(0)),
- };
- let publisher = FakeEmailPublisher::default();
- let coordinator = EmailCalendarBackfillCoordinator::new(repository.clone(), publisher.clone());
-
- let error = coordinator
- .run(
- CalendarBackfillJobKey {
- job_id: Uuid::now_v7(),
- email_link_id: Uuid::now_v7(),
- },
- "fusion-user",
- )
- .await
- .unwrap_err();
-
- assert!(matches!(error, EmailCalendarBackfillRunError::Busy));
- assert_eq!(*repository.associations.lock().unwrap(), 0);
- assert_eq!(*publisher.publications.lock().unwrap(), 0);
-}
-
-#[tokio::test]
-async fn email_coordinator_rejects_a_bounded_scan() {
- let repository = FakeEmailLifecycle {
- state: EmailCalendarBackfillState::Unassociated,
- active: Some(EmailCalendarScanJob {
- id: Uuid::now_v7(),
- status: EmailCalendarScanStatus::Init,
- is_full_scan: false,
- }),
- associations: Arc::new(Mutex::new(0)),
- failures: Arc::new(Mutex::new(0)),
- };
- let publisher = FakeEmailPublisher::default();
- let coordinator = EmailCalendarBackfillCoordinator::new(repository.clone(), publisher.clone());
-
- let error = coordinator
- .run(
- CalendarBackfillJobKey {
- job_id: Uuid::now_v7(),
- email_link_id: Uuid::now_v7(),
- },
- "fusion-user",
- )
- .await
- .unwrap_err();
-
- assert!(matches!(error, EmailCalendarBackfillRunError::Busy));
- assert_eq!(*repository.associations.lock().unwrap(), 0);
- assert_eq!(*publisher.publications.lock().unwrap(), 0);
-}
-
-#[tokio::test]
-async fn email_coordinator_applies_terminal_failure_through_its_repository() {
- let repository = FakeEmailLifecycle {
- state: EmailCalendarBackfillState::NotFound,
- active: None,
- associations: Arc::new(Mutex::new(0)),
- failures: Arc::new(Mutex::new(0)),
- };
- let coordinator =
- EmailCalendarBackfillCoordinator::new(repository.clone(), FakeEmailPublisher::default());
-
- coordinator
- .fail_terminal(
- CalendarBackfillJobKey {
- job_id: Uuid::now_v7(),
- email_link_id: Uuid::now_v7(),
- },
- "invalid calendar data",
- )
- .await
- .unwrap();
-
- assert_eq!(*repository.failures.lock().unwrap(), 1);
-}
diff --git a/crates/calendar_events/src/inbound/ics.rs b/crates/calendar_events/src/inbound/ics.rs
deleted file mode 100644
index 3333089b41..0000000000
--- a/crates/calendar_events/src/inbound/ics.rs
+++ /dev/null
@@ -1,575 +0,0 @@
-//! 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
deleted file mode 100644
index c8f4029f0f..0000000000
--- a/crates/calendar_events/src/inbound/ics/test.rs
+++ /dev/null
@@ -1,177 +0,0 @@
-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 df41f26f7d..0eb86f5ada 100644
--- a/crates/calendar_events/src/inbound/mod.rs
+++ b/crates/calendar_events/src/inbound/mod.rs
@@ -3,7 +3,3 @@
/// 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 7ab87ba33a..d4f5bdd0de 100644
--- a/crates/calendar_events/src/lib.rs
+++ b/crates/calendar_events/src/lib.rs
@@ -8,7 +8,7 @@
/// Calendar business models, ports, and services.
pub mod domain;
/// Inbound adapters that expose calendar use cases and accepted formats.
-#[cfg(any(feature = "ics", feature = "inbound"))]
+#[cfg(feature = "inbound")]
pub mod inbound;
/// Database and provider adapters used by service composition roots.
#[cfg(any(feature = "google", feature = "postgres"))]
diff --git a/crates/calendar_events/src/outbound/pg.rs b/crates/calendar_events/src/outbound/pg.rs
index 0c758140f2..25b30701e3 100644
--- a/crates/calendar_events/src/outbound/pg.rs
+++ b/crates/calendar_events/src/outbound/pg.rs
@@ -399,10 +399,7 @@ impl CalendarRepository for PgCalendarRepository {
let account_id =
upsert_google_account_tx(&mut tx, email_link_id, &row.macro_id, &row.email_address)
.await?;
- for kind in [
- CalendarBackfillKind::GoogleCalendar,
- CalendarBackfillKind::EmailIcs,
- ] {
+ for kind in [CalendarBackfillKind::GoogleCalendar] {
let job_id = Uuid::now_v7();
let inserted = sqlx::query_scalar!(
r#"
@@ -1748,6 +1745,7 @@ async fn retry_failed_backfills_tx(
WHERE email_link_id = $1
AND grant_version = $2
AND status = 'failed'
+ AND kind = 'google_calendar'
RETURNING id, account_id, kind
"#,
email_link_id,
@@ -1773,7 +1771,6 @@ async fn retry_failed_backfills_tx(
let kind = match row.kind.as_str() {
"google_calendar" => CalendarBackfillKind::GoogleCalendar,
- "email_ics" => CalendarBackfillKind::EmailIcs,
_ => return Err(rootcause::report!("invalid calendar backfill kind")),
};
jobs.push(CalendarBackfillJob {
diff --git a/crates/calendar_events/src/outbound/pg/test.rs b/crates/calendar_events/src/outbound/pg/test.rs
index 77f99e5a12..fd9664b02f 100644
--- a/crates/calendar_events/src/outbound/pg/test.rs
+++ b/crates/calendar_events/src/outbound/pg/test.rs
@@ -235,7 +235,7 @@ async fn calendar_capability_transition_schedules_once_and_failed_jobs_can_retry
.await
.unwrap();
assert!(enabled.changed);
- assert_eq!(enabled.jobs.len(), 2);
+ assert_eq!(enabled.jobs.len(), 1);
let duplicate = repo
.apply_google_grant(link_id, complete_grant())
@@ -300,11 +300,6 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(poo
.iter()
.find(|job| job.kind == CalendarBackfillKind::GoogleCalendar)
.unwrap();
- let email_job = enabled
- .jobs
- .iter()
- .find(|job| job.kind == CalendarBackfillKind::EmailIcs)
- .unwrap();
let account_id = google_job.account_id.unwrap();
let key = CalendarBackfillJobKey {
job_id: google_job.id,
@@ -376,18 +371,15 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(poo
calendar.is_deleted,
google_job.status AS google_job_status,
google_job.lease_token,
- email_job.status AS email_job_status,
- (SELECT count(*) FROM calendar_event_sources WHERE event_id = $4) AS "source_count!",
- (SELECT count(*) FROM calendar_events WHERE id = $4) AS "event_count!"
+ (SELECT count(*) FROM calendar_event_sources WHERE event_id = $3) AS "source_count!",
+ (SELECT count(*) FROM calendar_events WHERE id = $3) AS "event_count!"
FROM calendar_accounts account
JOIN calendars calendar ON calendar.account_id = account.id
JOIN calendar_backfill_jobs google_job ON google_job.id = $2
- JOIN calendar_backfill_jobs email_job ON email_job.id = $3
WHERE account.id = $1
"#,
account_id,
google_job.id,
- email_job.id,
event_id,
)
.fetch_one(&pool)
@@ -397,7 +389,6 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(poo
assert!(state.is_deleted);
assert_eq!(state.google_job_status, "failed");
assert!(state.lease_token.is_none());
- assert_eq!(state.email_job_status, "pending");
assert_eq!(state.source_count, 0);
assert_eq!(state.event_count, 0);
assert!(matches!(
@@ -409,7 +400,7 @@ async fn removing_calendar_scope_disables_sources_and_fences_the_running_job(poo
.apply_google_grant(link_id, complete_grant())
.await
.unwrap();
- assert_eq!(reenabled.jobs.len(), 2);
+ assert_eq!(reenabled.jobs.len(), 1);
assert_eq!(
sqlx::query_scalar!(
r#"SELECT sync_status FROM calendar_accounts WHERE id = $1"#,
diff --git a/crates/email_db_client/src/backfill/job/update.rs b/crates/email_db_client/src/backfill/job/update.rs
index 91e439e5cd..98690be96f 100644
--- a/crates/email_db_client/src/backfill/job/update.rs
+++ b/crates/email_db_client/src/backfill/job/update.rs
@@ -233,13 +233,9 @@ pub async fn finalize_initialization(
Ok(true)
}
-/// Atomically fails an email backfill job and any running email-calendar
-/// extraction job associated with that scan.
+/// Fails an active email backfill job.
#[tracing::instrument(skip(pool), err)]
-pub async fn fail_backfill_job_and_calendar_extraction(
- pool: &PgPool,
- job_id: Uuid,
-) -> anyhow::Result<bool> {
+pub async fn fail_backfill_job(pool: &PgPool, job_id: Uuid) -> anyhow::Result<bool> {
let mut tx = pool.begin().await?;
let failed = sqlx::query!(
r#"
@@ -259,30 +255,14 @@ pub async fn fail_backfill_job_and_calendar_extraction(
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.
+/// Completes an active email backfill, returning a durable outcome for
+/// idempotency and fencing.
#[tracing::instrument(skip(pool), err)]
-pub async fn complete_backfill_job_and_calendar_extraction(
+pub async fn complete_backfill_job(
pool: &PgPool,
job_id: Uuid,
init_lease_token: Option<Uuid>,
@@ -335,21 +315,6 @@ pub async fn complete_backfill_job_and_calendar_extraction(
});
}
- 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)
@@ -488,93 +453,6 @@ pub async fn mark_completion_effects_complete(
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;
diff --git a/crates/email_db_client/src/backfill/job/update/test.rs b/crates/email_db_client/src/backfill/job/update/test.rs
index 33063b7b6c..220a9e7d14 100644
--- a/crates/email_db_client/src/backfill/job/update/test.rs
+++ b/crates/email_db_client/src/backfill/job/update/test.rs
@@ -1,7 +1,6 @@
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,
+ claim_init_lease, complete_backfill_job, completion_effects_pending, fail_backfill_job,
finalize_initialization, mark_completion_effects_complete, release_completion_effects,
release_init_lease, renew_completion_effects, renew_init_lease,
};
@@ -83,10 +82,9 @@ async fn initialization_lease_is_fenced_reclaimable_and_publishes_once(
}
#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
-async fn failing_email_scan_also_fails_associated_calendar_job(pool: PgPool) -> anyhow::Result<()> {
+async fn failing_an_active_scan_marks_it_terminal(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)
@@ -109,23 +107,10 @@ async fn failing_email_scan_also_fails_associated_calendar_job(pool: PgPool) ->
)
.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!(fail_backfill_job(&pool, email_job_id).await?);
assert_eq!(
- complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ complete_backfill_job(&pool, email_job_id, None).await?,
BackfillCompletion::AlreadyTerminal
);
@@ -135,34 +120,17 @@ async fn failing_email_scan_also_fails_associated_calendar_job(pool: PgPool) ->
)
.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(
+async fn completing_an_active_scan_runs_completion_effects_once(
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)
@@ -185,26 +153,13 @@ async fn completing_active_email_scan_also_completes_calendar_job(
)
.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?,
+ complete_backfill_job(&pool, email_job_id, None).await?,
BackfillCompletion::Completed
);
assert_eq!(
- complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ complete_backfill_job(&pool, email_job_id, None).await?,
BackfillCompletion::AlreadyTerminal
);
@@ -214,15 +169,7 @@ async fn completing_active_email_scan_also_completes_calendar_job(
)
.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?
@@ -281,12 +228,11 @@ async fn zero_thread_completion_is_fenced_by_init_lease(pool: PgPool) -> anyhow:
.await?;
assert_eq!(
- complete_backfill_job_and_calendar_extraction(&pool, email_job_id, None).await?,
+ complete_backfill_job(&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?,
+ complete_backfill_job(&pool, email_job_id, Some(Uuid::new_v4())).await?,
BackfillCompletion::LeaseLost
);
sqlx::query!(
@@ -300,8 +246,7 @@ async fn zero_thread_completion_is_fenced_by_init_lease(pool: PgPool) -> anyhow:
.execute(&pool)
.await?;
assert_eq!(
- complete_backfill_job_and_calendar_extraction(&pool, email_job_id, Some(current_lease))
- .await?,
+ complete_backfill_job(&pool, email_job_id, Some(current_lease)).await?,
BackfillCompletion::LeaseLost
);
assert_eq!(
@@ -322,8 +267,7 @@ async fn zero_thread_completion_is_fenced_by_init_lease(pool: PgPool) -> anyhow:
panic!("expired zero-thread initialization should be reclaimable");
};
assert_eq!(
- complete_backfill_job_and_calendar_extraction(&pool, email_job_id, Some(reclaimed_lease))
- .await?,
+ complete_backfill_job(&pool, email_job_id, Some(reclaimed_lease)).await?,
BackfillCompletion::Completed
);
@@ -379,8 +323,8 @@ async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::
.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?);
+ assert!(!fail_backfill_job(&pool, email_job_id).await?);
+ assert!(!fail_backfill_job(&pool, Uuid::new_v4()).await?);
let email_status = sqlx::query_scalar!(
r#"SELECT status::text AS "status!" FROM email_backfill_jobs WHERE id = $1"#,
@@ -399,89 +343,3 @@ async fn failing_a_terminal_scan_is_an_idempotent_noop(pool: PgPool) -> anyhow::
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/models_email/src/email/service/backfill.rs b/crates/models_email/src/email/service/backfill.rs
index 044d31a88c..8c0222c4df 100644
--- a/crates/models_email/src/email/service/backfill.rs
+++ b/crates/models_email/src/email/service/backfill.rs
@@ -90,9 +90,6 @@ pub enum BackfillOperation {
/// 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
@@ -139,7 +136,6 @@ impl BackfillOperation {
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),
@@ -160,8 +156,7 @@ impl BackfillOperation {
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::CalendarGoogleBackfill(_) => None,
BackfillOperation::PopulateCrmContact(_)
| BackfillOperation::DepopulateCrmContact(_)
| BackfillOperation::PopulateCrmForUser(_)
diff --git a/crates/workspace-hack/Cargo.toml b/crates/workspace-hack/Cargo.toml
index c1b7c87001..5f7c2bc155 100644
--- a/crates/workspace-hack/Cargo.toml
+++ b/crates/workspace-hack/Cargo.toml
@@ -36,7 +36,6 @@ 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"] }
@@ -91,7 +90,6 @@ 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"] }
@@ -163,7 +161,6 @@ 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"] }
@@ -221,7 +218,6 @@ 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/email_service/Cargo.toml b/services/email_service/Cargo.toml
index 62b60ed9af..93414704e4 100644
--- a/services/email_service/Cargo.toml
+++ b/services/email_service/Cargo.toml
@@ -76,7 +76,7 @@ sfs_map = []
sfs_delete = []
[dependencies]
-calendar_events = { path = "../../crates/calendar_events", features = ["google", "ics", "postgres"] }
+calendar_events = { path = "../../crates/calendar_events", features = ["google", "postgres"] }
ammonia = "4.1.4"
anyhow = { workspace = true }
authentication_service_client = { path = "../../crates/authentication_service_client" }
diff --git a/services/email_service/src/api/email/init.rs b/services/email_service/src/api/email/init.rs
index 2352d0d4cc..c987688da0 100644
--- a/services/email_service/src/api/email/init.rs
+++ b/services/email_service/src/api/email/init.rs
@@ -724,12 +724,9 @@ async fn init_user(
{
tracing::error!(error = ?e, 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
- .context("Failed to persist initial backfill publication failure")?;
+ email_db_client::backfill::job::update::fail_backfill_job(&ctx.db, backfill_job.id)
+ .await
+ .context("Failed to persist initial backfill publication failure")?;
return Err(InitError::EnqueueError);
}
diff --git a/services/email_service/src/api/email/links/resync.rs b/services/email_service/src/api/email/links/resync.rs
index a4066b988b..9f0ac326be 100644
--- a/services/email_service/src/api/email/links/resync.rs
+++ b/services/email_service/src/api/email/links/resync.rs
@@ -100,12 +100,9 @@ pub async fn resync_link_handler(
.enqueue_email_backfill_message(ps_message)
.await
{
- email_db_client::backfill::job::update::fail_backfill_job_and_calendar_extraction(
- &ctx.db,
- backfill_job.id,
- )
- .await
- .context("failed to persist backfill publication failure")?;
+ email_db_client::backfill::job::update::fail_backfill_job(&ctx.db, backfill_job.id)
+ .await
+ .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 78b75957c3..843a0e5327 100644
--- a/services/email_service/src/api/internal/gmail/create.rs
+++ b/services/email_service/src/api/internal/gmail/create.rs
@@ -148,28 +148,28 @@ pub async fn handler(
.await
{
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 clean up backfill job for link {}", link_id)
+ email_db_client::backfill::job::update::fail_backfill_job(&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 clean up backfill job for link {}",
+ link_id
+ )
.into(),
- }),
- )
- .into_response()
- })?;
+ }),
+ )
+ .into_response()
+ })?;
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
diff --git a/services/email_service/src/backfill_completion_service.rs b/services/email_service/src/backfill_completion_service.rs
index c08b9e3af4..010c0033a3 100644
--- a/services/email_service/src/backfill_completion_service.rs
+++ b/services/email_service/src/backfill_completion_service.rs
@@ -107,19 +107,18 @@ pub(crate) async fn handle_job_completed(
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"),
- })
- })?;
+ let outcome = email_db_client::backfill::job::update::complete_backfill_job(
+ &ctx.db,
+ job_id,
+ init_lease_token,
+ )
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e.context("Failed to complete email backfill job"),
+ })
+ })?;
completion_result(job_id, outcome)
}
diff --git a/services/email_service/src/calendar_ingest.rs b/services/email_service/src/calendar_ingest.rs
deleted file mode 100644
index a1849aa990..0000000000
--- a/services/email_service/src/calendar_ingest.rs
+++ /dev/null
@@ -1,198 +0,0 @@
-//! 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
deleted file mode 100644
index d6fc5db161..0000000000
--- a/services/email_service/src/calendar_ingest/test.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-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
index af574917ed..843fd05df7 100644
--- a/services/email_service/src/calendar_outbox.rs
+++ b/services/email_service/src/calendar_outbox.rs
@@ -299,7 +299,6 @@ fn to_queue_message(row: &OutboxRow) -> anyhow::Result<BackfillPubsubMessage> {
};
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 })
diff --git a/services/email_service/src/lib.rs b/services/email_service/src/lib.rs
index e87bbd0fdb..a0ecf5aae0 100644
--- a/services/email_service/src/lib.rs
+++ b/services/email_service/src/lib.rs
@@ -2,7 +2,6 @@
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;
diff --git a/services/email_service/src/openapi.rs b/services/email_service/src/openapi.rs
index 8ee6895179..29b528694e 100644
--- a/services/email_service/src/openapi.rs
+++ b/services/email_service/src/openapi.rs
@@ -3,7 +3,6 @@
mod api;
mod backfill_completion_service;
mod backfill_init_service;
-mod calendar_ingest;
mod calendar_outbox;
mod config;
mod convert;
diff --git a/services/email_service/src/pubsub/backfill/backfill_message.rs b/services/email_service/src/pubsub/backfill/backfill_message.rs
index e8a63b3d90..c40cb8f27a 100644
--- a/services/email_service/src/pubsub/backfill/backfill_message.rs
+++ b/services/email_service/src/pubsub/backfill/backfill_message.rs
@@ -53,8 +53,6 @@ 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")
@@ -83,45 +81,6 @@ 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"),
- })
- })?;
-
- if ctx.calendar_sync_enabled {
- 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
deleted file mode 100644
index ec1e0f3e5a..0000000000
--- a/services/email_service/src/pubsub/backfill/calendar_email_ics_backfill.rs
+++ /dev/null
@@ -1,51 +0,0 @@
-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/error_handlers.rs b/services/email_service/src/pubsub/backfill/error_handlers.rs
index 21fd5dc43a..50ad419454 100644
--- a/services/email_service/src/pubsub/backfill/error_handlers.rs
+++ b/services/email_service/src/pubsub/backfill/error_handlers.rs
@@ -102,19 +102,6 @@ pub async fn handle_non_retryable_error(
.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(_) => {}
@@ -146,10 +133,7 @@ fn coordinator_reauth_edge(error: &anyhow::Error) -> Option<bool> {
#[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?;
+ email_db_client::backfill::job::update::fail_backfill_job(&ctx.db, job_id).await?;
if transitioned {
notify_job_failed(ctx, job_id).await;
@@ -242,12 +226,6 @@ pub async fn handle_retryable_error(
"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,
diff --git a/services/email_service/src/pubsub/backfill/mod.rs b/services/email_service/src/pubsub/backfill/mod.rs
index d17e28b051..6967e0a6a8 100644
--- a/services/email_service/src/pubsub/backfill/mod.rs
+++ b/services/email_service/src/pubsub/backfill/mod.rs
@@ -1,7 +1,6 @@
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;
diff --git a/services/email_service/src/pubsub/backfill/process.rs b/services/email_service/src/pubsub/backfill/process.rs
index e3de175b6c..55698956db 100644
--- a/services/email_service/src/pubsub/backfill/process.rs
+++ b/services/email_service/src/pubsub/backfill/process.rs
@@ -1,8 +1,7 @@
use crate::pubsub::backfill::{
- 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,
+ backfill_attachment, backfill_message, backfill_thread, 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::{
@@ -192,14 +191,6 @@ async fn inner_process_message(
)
.await
}
- BackfillOperation::CalendarEmailIcsBackfill(scope) => {
- if let Some(parked) = park_calendar_delivery(ctx, scope.payload.calendar_job_id).await {
- return parked;
- }
- 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
}
diff --git a/services/email_service/src/pubsub/backfill/worker.rs b/services/email_service/src/pubsub/backfill/worker.rs
index 01414e4503..5d678fb546 100644
--- a/services/email_service/src/pubsub/backfill/worker.rs
+++ b/services/email_service/src/pubsub/backfill/worker.rs
@@ -81,8 +81,7 @@ pub async fn run_worker_with_cancellation(
calendar_sync_enabled: bool,
cancellation_token: CancellationToken,
) {
- let calendar_backfills =
- CalendarBackfillServices::new(db.clone(), sqs_client.clone(), redis_client.clone());
+ let calendar_backfills = CalendarBackfillServices::new(db.clone(), redis_client.clone());
let ctx = PubSubContext {
db,
sqs_worker: worker.clone(),
diff --git a/services/email_service/src/pubsub/calendar_backfill_adapters.rs b/services/email_service/src/pubsub/calendar_backfill_adapters.rs
index aa147973b9..d2c1f66c7e 100644
--- a/services/email_service/src/pubsub/calendar_backfill_adapters.rs
+++ b/services/email_service/src/pubsub/calendar_backfill_adapters.rs
@@ -1,16 +1,8 @@
-//! Database and queue adapters for email-based calendar extraction.
+//! Process-level adapters backing the calendar application services.
-use calendar_events::domain::{
- models::{
- CalendarBackfillJobKey, EmailCalendarBackfillState, EmailCalendarScanAssociation,
- EmailCalendarScanJob, EmailCalendarScanStatus,
- },
- ports::{
- EmailCalendarBackfillPublisher, EmailCalendarBackfillRepository, GoogleProviderError,
- GoogleProviderErrorKind,
- },
-};
+use calendar_events::domain::ports::{GoogleProviderError, GoogleProviderErrorKind};
use calendar_events::outbound::google::GoogleRequestGate;
+use uuid::Uuid;
use crate::util::redis::RedisClient;
@@ -38,256 +30,3 @@ impl GoogleRequestGate for RedisCalendarRequestGate {
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 57ab116683..134cd5941f 100644
--- a/services/email_service/src/pubsub/context.rs
+++ b/services/email_service/src/pubsub/context.rs
@@ -1,14 +1,9 @@
-use crate::pubsub::calendar_backfill_adapters::{
- PgEmailCalendarBackfillRepository, RedisCalendarRequestGate, SqsEmailCalendarBackfillPublisher,
-};
+use crate::pubsub::calendar_backfill_adapters::RedisCalendarRequestGate;
use crate::util::redis::RedisClient;
use authentication_service_client::AuthServiceClient;
use calendar_events::{
domain::models::GoogleWatchConfig,
- domain::service::{
- EmailCalendarBackfillCoordinator, GoogleCalendarBackfillCoordinator,
- GoogleCalendarBackfillFailureService,
- },
+ domain::service::{GoogleCalendarBackfillCoordinator, GoogleCalendarBackfillFailureService},
outbound::{google::GoogleCalendarClient, pg::PgCalendarRepository},
};
use connection_gateway_client::client::ConnectionGatewayClient;
@@ -44,12 +39,6 @@ pub type GoogleCalendarBackfillService = GoogleCalendarBackfillCoordinator<
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>;
@@ -59,16 +48,14 @@ pub type GoogleCalendarBackfillFailureHandler =
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());
+ pub fn new(db: PgPool, redis_client: RedisClient) -> Self {
+ let repository = PgCalendarRepository::new(db);
Self {
google: Arc::new(GoogleCalendarBackfillCoordinator::new(
repository.clone(),
@@ -82,10 +69,6 @@ impl CalendarBackfillServices {
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)),
}
}
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 f8ada1bbc1..3c085530b3 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
@@ -118,8 +118,6 @@ 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 mut message = map_message_resource_to_service(message_resource, link.id).map_err(|e| {
ProcessingError::NonRetryable(DetailedError {
@@ -328,32 +326,6 @@ pub async fn upsert_message(
publish_email_event(&ctx.macro_event_broker, &event);
}
- if ctx.calendar_sync_enabled {
- 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 93478560b4..e929816522 100644
--- a/services/email_service/src/pubsub/inbox_sync/worker.rs
+++ b/services/email_service/src/pubsub/inbox_sync/worker.rs
@@ -84,8 +84,7 @@ 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 calendar_backfills = CalendarBackfillServices::new(db.clone(), redis_client.clone());
let ctx = PubSubContext {
db,
sqs_worker: worker.clone(),