Skip to content

armgit / macro

Public Imported from GitHub
0

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

fix(email): stop syncing Gmail links whose grant was revoked (#7898)

teoe34be24de64b
diff --git a/services/authentication_service/src/api/internal/google_access_token.rs b/services/authentication_service/src/api/internal/google_access_token.rs
index 1d400f743c..2cc6fc25f4 100644
--- a/services/authentication_service/src/api/internal/google_access_token.rs
+++ b/services/authentication_service/src/api/internal/google_access_token.rs
@@ -116,10 +116,17 @@ async fn get_access_token(
.refresh_google_token(link.token.as_str())
.await
.map_err(|e| {
- tracing::error!(error=?e, "error fetching access token for userid {}", fusionauth_user_id);
let status_code = match &e {
- FusionAuthClientError::InvalidGrant => StatusCode::FORBIDDEN,
- _ => StatusCode::INTERNAL_SERVER_ERROR,
+ // A revoked or expired grant is an expected outcome the caller
+ // handles by asking the user to reauthorize.
+ FusionAuthClientError::InvalidGrant => {
+ tracing::warn!(error=?e, "refresh token rejected for userid {}", fusionauth_user_id);
+ StatusCode::FORBIDDEN
+ }
+ _ => {
+ tracing::error!(error=?e, "error fetching access token for userid {}", fusionauth_user_id);
+ StatusCode::INTERNAL_SERVER_ERROR
+ }
};
let message = format!("unable to fetch {} access token", identity_provider_name);
(status_code, Json(ErrorResponse { message: message.into() })).into_response()
diff --git a/services/email_service/src/pubsub/backfill/process.rs b/services/email_service/src/pubsub/backfill/process.rs
index 8e051f9fc3..afe3e41f0d 100644
--- a/services/email_service/src/pubsub/backfill/process.rs
+++ b/services/email_service/src/pubsub/backfill/process.rs
@@ -131,11 +131,15 @@ async fn inner_process_message(
seed_sent_contact::seed_sent_contact(ctx, scope, &link).await
}
BackfillOperation::PopulateCrmContact(scope) => {
- let link = fetch_link(ctx, scope.link_id).await?;
+ let Some(link) = fetch_link_if_present(ctx, scope.link_id).await? else {
+ return Ok(());
+ };
populate_crm_contact::populate_crm_contact(ctx, &link, &scope.payload).await
}
BackfillOperation::DepopulateCrmContact(scope) => {
- let link = fetch_link(ctx, scope.link_id).await?;
+ let Some(link) = fetch_link_if_present(ctx, scope.link_id).await? else {
+ return Ok(());
+ };
depopulate_crm_contact::depopulate_crm_contact(ctx, &link, &scope.payload).await
}
BackfillOperation::PopulateCrmForUser(payload) => {
@@ -219,6 +223,28 @@ async fn fetch_link(ctx: &PubSubContext, link_id: Uuid) -> Result<link::Link, Pr
}
}
+/// Looks up the link a link-scoped CRM message was fanned out for.
+///
+/// Returns `None` once the link is deleted: link teardown already removed the
+/// link's CRM rows, so messages still queued for it have nothing left to do.
+async fn fetch_link_if_present(
+ ctx: &PubSubContext,
+ link_id: Uuid,
+) -> Result<Option<link::Link>, ProcessingError> {
+ let link = email_db_client::links::get::fetch_link_by_id(&ctx.db, link_id)
+ .await
+ .map_err(|e| {
+ ProcessingError::Retryable(DetailedError {
+ reason: FailureReason::DatabaseQueryFailed,
+ source: e,
+ })
+ })?;
+ if link.is_none() {
+ tracing::debug!(%link_id, "Link was deleted; dropping link-scoped CRM message");
+ }
+ Ok(link)
+}
+
/// Extracts backfill message from the SQS message body
#[tracing::instrument(skip(message))]
fn extract_backfill_message(
diff --git a/services/email_service/src/pubsub/inbox_sync/process.rs b/services/email_service/src/pubsub/inbox_sync/process.rs
index 6161e6c6f6..fb9448b26b 100644
--- a/services/email_service/src/pubsub/inbox_sync/process.rs
+++ b/services/email_service/src/pubsub/inbox_sync/process.rs
@@ -6,10 +6,14 @@ use crate::pubsub::inbox_sync::operations::update_labels::update_labels;
use crate::pubsub::inbox_sync::operations::upsert_message::upsert_message;
use anyhow::{Context, Result, anyhow};
use models_email::gmail::inbox_sync::{InboxSyncOperation, InboxSyncPubsubMessage};
+use models_email::service::link::Link;
use models_email::service::pubsub::{DetailedError, FailureReason, ProcessingError};
use sqs_worker::cleanup_message;
use std::result;
+#[cfg(test)]
+mod test;
+
/// Processes a message from the gmail inbox sync queue.
pub async fn process_message(
ctx: PubSubContext,
@@ -72,8 +76,13 @@ async fn inner_process_message(
})
})?;
- // if sync is disabled we shouldn't update the user's inbox
- if !link.is_sync_active {
+ if !should_process(&link, &data.operation) {
+ tracing::debug!(
+ link_id = %link.id,
+ is_sync_active = link.is_sync_active,
+ needs_reauth = link.needs_reauth,
+ "Skipping inbox sync operation for link"
+ );
return Ok(());
}
@@ -107,6 +116,22 @@ async fn inner_process_message(
Ok(())
}
+/// Whether inbox sync should process `operation` for `link`.
+///
+/// Gmail notifications for a link that needs reauth are skipped: listing their
+/// changes needs the dead grant, and the untouched sync cursor lets the first
+/// notification after the grant recovers catch up. Operations fanned out by an
+/// earlier notification still run because the cursor has already moved past
+/// them.
+fn should_process(link: &Link, operation: &InboxSyncOperation) -> bool {
+ match operation {
+ InboxSyncOperation::GmailMessage(_) => link.is_sync_active && !link.needs_reauth,
+ InboxSyncOperation::UpsertMessage(_)
+ | InboxSyncOperation::DeleteMessage(_)
+ | InboxSyncOperation::UpdateLabels(_) => link.is_sync_active,
+ }
+}
+
/// Extracts backfill message from the SQS message body
#[tracing::instrument(skip(message))]
fn extract_inbox_sync_message(
diff --git a/services/email_service/src/pubsub/inbox_sync/process/test.rs b/services/email_service/src/pubsub/inbox_sync/process/test.rs
new file mode 100644
index 0000000000..9bb6365533
--- /dev/null
+++ b/services/email_service/src/pubsub/inbox_sync/process/test.rs
@@ -0,0 +1,71 @@
+use super::*;
+use macro_user_id::email::EmailStr;
+use macro_user_id::user_id::MacroUserIdStr;
+use models_email::gmail::inbox_sync::{
+ DeleteMessagePayload, GmailMessagePayload, UpdateLabelsPayload, UpsertMessagePayload,
+};
+use models_email::service::link::UserProvider;
+use uuid::Uuid;
+
+fn link(is_sync_active: bool, needs_reauth: bool) -> Link {
+ Link {
+ id: Uuid::from_u128(1),
+ macro_id: MacroUserIdStr::try_from("macro|[email protected]".to_string()).unwrap(),
+ fusionauth_user_id: "fusionauth-user".to_string(),
+ email_address: EmailStr::try_from("[email protected]".to_string()).unwrap(),
+ provider: UserProvider::Gmail,
+ is_sync_active,
+ is_primary: true,
+ needs_reauth,
+ last_sync_error_at: None,
+ created_at: Default::default(),
+ updated_at: Default::default(),
+ }
+}
+
+fn gmail_notification() -> InboxSyncOperation {
+ InboxSyncOperation::GmailMessage(GmailMessagePayload { history_id: 42 })
+}
+
+fn fanned_out_operations() -> Vec<InboxSyncOperation> {
+ let provider_message_id = "provider-message".to_string();
+ vec![
+ InboxSyncOperation::UpsertMessage(UpsertMessagePayload {
+ provider_message_id: provider_message_id.clone(),
+ }),
+ InboxSyncOperation::DeleteMessage(DeleteMessagePayload {
+ provider_message_id: provider_message_id.clone(),
+ }),
+ InboxSyncOperation::UpdateLabels(UpdateLabelsPayload {
+ provider_message_id,
+ }),
+ ]
+}
+
+#[test]
+fn processes_gmail_notification_for_healthy_link() {
+ assert!(should_process(&link(true, false), &gmail_notification()));
+}
+
+#[test]
+fn skips_gmail_notification_for_link_that_needs_reauth() {
+ assert!(!should_process(&link(true, true), &gmail_notification()));
+}
+
+#[test]
+fn processes_fanned_out_operations_for_link_that_needs_reauth() {
+ for operation in fanned_out_operations() {
+ assert!(should_process(&link(true, true), &operation), "{operation}");
+ }
+}
+
+#[test]
+fn skips_every_operation_when_sync_is_disabled() {
+ for needs_reauth in [false, true] {
+ let link = link(false, needs_reauth);
+ assert!(!should_process(&link, &gmail_notification()));
+ for operation in fanned_out_operations() {
+ assert!(!should_process(&link, &operation), "{operation}");
+ }
+ }
+}