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): cancel scheduled sends when trashing a thread (#2029)

Evan Hutnik1d80187d8fd0
diff --git a/rust/cloud-storage/email/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json b/rust/cloud-storage/email/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json
new file mode 100644
index 0000000000..15557826d9
--- /dev/null
+++ b/rust/cloud-storage/email/.sqlx/query-3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n DELETE FROM email_scheduled_messages\n WHERE message_id = ANY($1) AND link_id = $2 AND sent = false\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "UuidArray",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "3b570b222c479d824a07d83d5af9bb51833195bf60052e04a1c754df22f57220"
+}
diff --git a/rust/cloud-storage/email/src/domain/models/label.rs b/rust/cloud-storage/email/src/domain/models/label.rs
index c38d6caeb7..1809482aaf 100644
--- a/rust/cloud-storage/email/src/domain/models/label.rs
+++ b/rust/cloud-storage/email/src/domain/models/label.rs
@@ -80,4 +80,6 @@ pub mod system_labels {
pub const UNREAD: &str = "UNREAD";
/// The STARRED label.
pub const STARRED: &str = "STARRED";
+ /// The TRASH label.
+ pub const TRASH: &str = "TRASH";
}
diff --git a/rust/cloud-storage/email/src/domain/ports.rs b/rust/cloud-storage/email/src/domain/ports.rs
index 7b660ad3c8..aebdb3d79f 100644
--- a/rust/cloud-storage/email/src/domain/ports.rs
+++ b/rust/cloud-storage/email/src/domain/ports.rs
@@ -201,6 +201,13 @@ pub trait EmailRepo: Send + Sync + 'static {
&self,
link_id: Uuid,
) -> impl Future<Output = Result<Vec<LinkLabel>, Self::Err>> + Send;
+
+ /// Delete unsent scheduled messages for a batch of draft message IDs.
+ fn delete_scheduled_messages_batch(
+ &self,
+ message_ids: &[Uuid],
+ link_id: Uuid,
+ ) -> impl Future<Output = Result<(), Self::Err>> + Send;
}
pub trait EmailService: Send + Sync + 'static {
diff --git a/rust/cloud-storage/email/src/domain/service/thread_labels.rs b/rust/cloud-storage/email/src/domain/service/thread_labels.rs
index 758fc87c20..007bfbd467 100644
--- a/rust/cloud-storage/email/src/domain/service/thread_labels.rs
+++ b/rust/cloud-storage/email/src/domain/service/thread_labels.rs
@@ -143,6 +143,25 @@ where
let err = anyhow::Error::from(e);
tracing::error!(error=?err, "failed to update message starred status");
}
+
+ // When trashing a thread, cancel any pending scheduled sends for drafts
+ if add && provider_label_id == system_labels::TRASH {
+ let draft_message_ids: Vec<_> = messages
+ .iter()
+ .filter(|m| m.is_draft && successful_ids.contains(&m.db_id))
+ .map(|m| m.db_id)
+ .collect();
+
+ if !draft_message_ids.is_empty()
+ && let Err(e) = self
+ .email_repo
+ .delete_scheduled_messages_batch(&draft_message_ids, link.id)
+ .await
+ {
+ let err = anyhow::Error::from(e);
+ tracing::error!(error=?err, "failed to cancel scheduled sends for trashed drafts");
+ }
+ }
}
successful_ids.sort();
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/label.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/label.rs
index a61a3e5eb3..9d2ee8feed 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/label.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/label.rs
@@ -333,3 +333,27 @@ pub(crate) async fn update_message_starred_status_batch(
Ok(())
}
+
+#[tracing::instrument(skip(pool), err)]
+pub(crate) async fn delete_scheduled_messages_batch(
+ pool: &PgPool,
+ message_ids: &[Uuid],
+ link_id: Uuid,
+) -> Result<(), sqlx::Error> {
+ if message_ids.is_empty() {
+ return Ok(());
+ }
+
+ sqlx::query!(
+ r#"
+ DELETE FROM email_scheduled_messages
+ WHERE message_id = ANY($1) AND link_id = $2 AND sent = false
+ "#,
+ message_ids,
+ link_id
+ )
+ .execute(pool)
+ .await?;
+
+ Ok(())
+}
diff --git a/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs b/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs
index 80b2da5361..3764e844db 100644
--- a/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs
+++ b/rust/cloud-storage/email/src/outbound/email_pg_repo/mod.rs
@@ -229,4 +229,12 @@ impl EmailRepo for EmailPgRepo {
async fn list_labels_by_link_id(&self, link_id: Uuid) -> Result<Vec<LinkLabel>, Self::Err> {
label::list_labels_by_link_id(&self.pool, link_id).await
}
+
+ async fn delete_scheduled_messages_batch(
+ &self,
+ message_ids: &[Uuid],
+ link_id: Uuid,
+ ) -> Result<(), Self::Err> {
+ label::delete_scheduled_messages_batch(&self.pool, message_ids, link_id).await
+ }
}