diff --git a/crates/iz-core/src/detail.rs b/crates/iz-core/src/detail.rs index 8f6feb9..c6570cb 100644 --- a/crates/iz-core/src/detail.rs +++ b/crates/iz-core/src/detail.rs @@ -521,6 +521,13 @@ mod reads { /// The task and the board it belongs to, or `None` if it is gone. async fn task(&self, task_id: &str) -> Result>; + /// A deleted task's facts, read through the soft delete. Every other + /// read here stops at `deleted_at` — the board and the detail page + /// have no use for a row that is gone — but the mail a delete itself + /// owes still names the card: its key, its title, the board whose + /// rules fire on it. `None` for a task that was never there. + async fn deleted_task(&self, task_id: &str) -> Result>; + async fn columns_for_board(&self, board_id: &str) -> Result>; async fn assignees_for_task(&self, task_id: &str) -> Result>; diff --git a/crates/iz-core/src/mail.rs b/crates/iz-core/src/mail.rs index 2de4745..c328c2e 100644 --- a/crates/iz-core/src/mail.rs +++ b/crates/iz-core/src/mail.rs @@ -425,11 +425,23 @@ impl Engine { /// Called after the write has committed, never inside it. pub async fn on_activity(&self, event: &ActivityEvent) -> crate::store::Result { let mut report = Report::default(); - let Some(facts) = self.store.task(&event.task_id).await? else { - // A task deleted between the write and this run has no facts left - // to read a board from `store.task`, but its rules still get a - // `task_gone` row each — `board_of_task` reads through the soft - // delete to find them. + // A delete is its own activity, written in the same transaction as + // the soft delete, so by the time this runs `store.task` has no row + // to give. The facts a delete's mail needs are read through the + // delete instead, and the rules fire on the card as it stood the + // moment it left — the same carry-the-snapshot shape the freeing + // uses. Any other kind whose task has no row left is news about + // something already gone: no facts to speak about, so its rules + // still get a `task_gone` row each — `board_of_task` reads through + // the soft delete to find them. + let facts = match self.store.task(&event.task_id).await? { + Some(facts) => Some(facts), + None if event.kind == ActivityKind::Deleted => { + self.store.deleted_task(&event.task_id).await? + } + None => None, + }; + let Some(facts) = facts else { let Some(board_id) = self.store.board_of_task(&event.task_id).await? else { return Ok(report); }; @@ -835,8 +847,16 @@ impl Engine { let Some(task_id) = send.task_id.as_deref() else { return Ok(None); }; - let Some(facts) = self.store.task(task_id).await? else { - return Ok(None); + // The task a mail speaks about may have been deleted by the very + // event the mail is about — a Deleted rule's whole point — so the + // facts are read through the soft delete, exactly as the rules + // engine read them when it owed the mail. + let facts = match self.store.task(task_id).await? { + Some(facts) => facts, + None => match self.store.deleted_task(task_id).await? { + Some(facts) => facts, + None => return Ok(None), + }, }; let actor = self .store @@ -987,8 +1007,15 @@ impl Engine { let Some(task_id) = send.task_id.as_deref() else { return Ok(None); }; - let Some(facts) = self.store.task(task_id).await? else { - return Ok(None); + // A batch whose card was deleted inside the window — maybe by the + // newest event itself — still goes out: where the task stands now is + // its final row, read through the soft delete. + let facts = match self.store.task(task_id).await? { + Some(facts) => facts, + None => match self.store.deleted_task(task_id).await? { + Some(facts) => facts, + None => return Ok(None), + }, }; let columns = self.store.columns_for_board(&facts.board_id).await?; let column = columns diff --git a/crates/iz-core/src/store/turso_store.rs b/crates/iz-core/src/store/turso_store.rs index 45bbc4a..92e96a2 100644 --- a/crates/iz-core/src/store/turso_store.rs +++ b/crates/iz-core/src/store/turso_store.rs @@ -5098,6 +5098,48 @@ impl DetailReads for TursoStore { })) } + /// The same query with no `deleted_at` guard: the one read in the store + /// that answers for a soft-deleted row, because the mail a delete itself + /// owes is about the card as it stood the moment it left. The row keeps + /// every fact the mail names. + async fn deleted_task(&self, task_id: &str) -> Result> { + let row = self + .one_row( + "SELECT t.id, t.task_key, t.title, t.column_id, t.deadline, t.clock_at, \ + t.position, t.done_at, t.description, t.board_id, b.workspace_id, \ + t.parent_id, t.tag_id, g.name \ + FROM task t LEFT JOIN tag g ON g.id = t.tag_id \ + JOIN board b ON b.id = t.board_id \ + WHERE t.id = ?1", + params![task_id], + ) + .await?; + let Some(row) = row else { return Ok(None) }; + Ok(Some(TaskFacts { + row: TaskRow { + id: text(&row, 0)?, + task_key: text(&row, 1)?, + title: text(&row, 2)?, + column_id: text(&row, 3)?, + deadline: opt_day(&row, 4)?, + clock_at: opt_stamp(&row, 5)?, + position: row.get::(6).map_err(backend)?, + done_at: opt_stamp(&row, 7)?, + parent_id: opt_text(&row, 11)?, + tag: match opt_text(&row, 12)? { + Some(id) => Some(TagChip { + id, + name: text(&row, 13)?, + }), + None => None, + }, + }, + description: text(&row, 8)?, + board_id: text(&row, 9)?, + workspace_id: text(&row, 10)?, + })) + } + async fn columns_for_board(&self, board_id: &str) -> Result> { BoardReads::columns(self, board_id).await } diff --git a/crates/iz-core/tests/store.rs b/crates/iz-core/tests/store.rs index a81291c..946d6c6 100644 --- a/crates/iz-core/tests/store.rs +++ b/crates/iz-core/tests/store.rs @@ -1848,6 +1848,11 @@ impl DetailReads for CountingDetail<'_> { self.inner.task(task_id).await } + async fn deleted_task(&self, task_id: &str) -> Result, StoreError> { + self.tick(); + self.inner.deleted_task(task_id).await + } + async fn columns_for_board(&self, board_id: &str) -> Result, StoreError> { self.tick(); self.inner.columns_for_board(board_id).await @@ -4873,6 +4878,103 @@ async fn assigning_one_person_mails_that_person_not_every_assignee() { let _ = std::fs::remove_dir_all(&dir); } +/// A "when a task is deleted" rule has to fire on the delete itself. The +/// activity and the soft delete ride the same write, so by the time the +/// engine reads the event there is no live row to read facts from — the +/// facts come through the delete, and the mail names the card that left. +#[tokio::test] +async fn a_delete_fires_the_rule_that_watched_for_it() { + let (dir, store, workspace, admin) = shared().await; + let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await; + let task = add_task(&store, &workspace, "Backlog", "Mistaken card", None, &admin).await; + store.assign_task(&task, &mate).await.unwrap(); + let board = store.board(&workspace).await.unwrap().unwrap(); + store + .create_mail_rule( + &board.id, + &Trigger::Deleted, + "A card was deleted", + Audience::Assignees, + OffsetDateTime::now_utc(), + false, + ) + .await + .unwrap(); + + let mailer = Remembering::taking_everything(); + let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh"); + let deletion = store + .delete_task(&task, &admin, OffsetDateTime::now_utc()) + .await + .unwrap(); + let event = activity_event(&store, &deletion.activity_id).await; + + let report = engine.on_activity(&event).await.unwrap(); + assert_eq!(report.sent, 1, "the delete owes its own mail"); + let sent = mailer.sent(); + assert_eq!(sent.len(), 1); + assert_eq!(sent[0].to, "emre@iz.sh"); + assert_eq!(sent[0].subject, "A card was deleted"); + assert!( + sent[0].body.contains("deleted it."), + "the compose line names what happened: {}", + sent[0].body + ); + assert!( + sent[0].body.contains("Mistaken card"), + "the mail names the card it outlives: {}", + sent[0].body + ); + let _ = std::fs::remove_dir_all(&dir); +} + +/// The gate is the delete itself, not the missing row: an older activity a +/// delete raced — written first, read after — is news about a card that is +/// gone, and stands down the way it always did. +#[tokio::test] +async fn an_activity_a_delete_raced_still_stands_down() { + let (dir, store, workspace, admin) = shared().await; + let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await; + let task = add_task(&store, &workspace, "Backlog", "Ship it", None, &admin).await; + store.assign_task(&task, &mate).await.unwrap(); + let board = store.board(&workspace).await.unwrap().unwrap(); + store + .create_mail_rule( + &board.id, + &Trigger::Commented, + "New comment", + Audience::Board, + OffsetDateTime::now_utc(), + false, + ) + .await + .unwrap(); + + let commented = store + .record_activity( + &task, + Some(&mate), + None, + &ActivityKind::Commented, + "", + OffsetDateTime::now_utc(), + ) + .await + .unwrap(); + store + .delete_task(&task, &admin, OffsetDateTime::now_utc()) + .await + .unwrap(); + + let mailer = Remembering::taking_everything(); + let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh"); + let event = activity_event(&store, &commented).await; + let report = engine.on_activity(&event).await.unwrap(); + assert_eq!(report.sent, 0); + assert!(mailer.sent().is_empty()); + let _ = std::fs::remove_dir_all(&dir); +} + #[tokio::test] async fn a_time_change_fires_a_deadline_set_rule() { let (dir, store, workspace, admin) = shared().await;