iz-core: 'when a task is deleted' rules actually fire
Browse files at this commitdb99ba58413c206b4623acd30301ce195ebb0250Parent: f4152e14 changed files +187 -9
- crates/iz-core/src/detail.rs +7 -0
- crates/iz-core/src/mail.rs +36 -9
- crates/iz-core/src/store/turso_store.rs +42 -0
- crates/iz-core/tests/store.rs +102 -0
crates/iz-core/src/detail.rsmodified+7 -0
| @@ -521,6 +521,13 @@ mod reads { | |||
| 521 | /// The task and the board it belongs to, or `None` if it is gone. | 521 | /// The task and the board it belongs to, or `None` if it is gone. |
| 522 | async fn task(&self, task_id: &str) -> Result<Option<TaskFacts>>; | 522 | async fn task(&self, task_id: &str) -> Result<Option<TaskFacts>>; |
| 523 | 523 | ||
| 524 | + /// A deleted task's facts, read through the soft delete. Every other | ||
| 525 | + /// read here stops at `deleted_at` — the board and the detail page | ||
| 526 | + /// have no use for a row that is gone — but the mail a delete itself | ||
| 527 | + /// owes still names the card: its key, its title, the board whose | ||
| 528 | + /// rules fire on it. `None` for a task that was never there. | ||
| 529 | + async fn deleted_task(&self, task_id: &str) -> Result<Option<TaskFacts>>; | ||
| 530 | + | ||
| 524 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>>; | 531 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>>; |
| 525 | 532 | ||
| 526 | async fn assignees_for_task(&self, task_id: &str) -> Result<Vec<Person>>; | 533 | async fn assignees_for_task(&self, task_id: &str) -> Result<Vec<Person>>; |
crates/iz-core/src/mail.rsmodified+36 -9
| @@ -425,11 +425,23 @@ impl Engine { | |||
| 425 | /// Called after the write has committed, never inside it. | 425 | /// Called after the write has committed, never inside it. |
| 426 | pub async fn on_activity(&self, event: &ActivityEvent) -> crate::store::Result<Report> { | 426 | pub async fn on_activity(&self, event: &ActivityEvent) -> crate::store::Result<Report> { |
| 427 | let mut report = Report::default(); | 427 | let mut report = Report::default(); |
| 428 | - let Some(facts) = self.store.task(&event.task_id).await? else { | 428 | + // A delete is its own activity, written in the same transaction as |
| 429 | - // A task deleted between the write and this run has no facts left | 429 | + // the soft delete, so by the time this runs `store.task` has no row |
| 430 | - // to read a board from `store.task`, but its rules still get a | 430 | + // to give. The facts a delete's mail needs are read through the |
| 431 | - // `task_gone` row each — `board_of_task` reads through the soft | 431 | + // delete instead, and the rules fire on the card as it stood the |
| 432 | - // delete to find them. | 432 | + // moment it left — the same carry-the-snapshot shape the freeing |
| 433 | + // uses. Any other kind whose task has no row left is news about | ||
| 434 | + // something already gone: no facts to speak about, so its rules | ||
| 435 | + // still get a `task_gone` row each — `board_of_task` reads through | ||
| 436 | + // the soft delete to find them. | ||
| 437 | + let facts = match self.store.task(&event.task_id).await? { | ||
| 438 | + Some(facts) => Some(facts), | ||
| 439 | + None if event.kind == ActivityKind::Deleted => { | ||
| 440 | + self.store.deleted_task(&event.task_id).await? | ||
| 441 | + } | ||
| 442 | + None => None, | ||
| 443 | + }; | ||
| 444 | + let Some(facts) = facts else { | ||
| 433 | let Some(board_id) = self.store.board_of_task(&event.task_id).await? else { | 445 | let Some(board_id) = self.store.board_of_task(&event.task_id).await? else { |
| 434 | return Ok(report); | 446 | return Ok(report); |
| 435 | }; | 447 | }; |
| @@ -835,8 +847,16 @@ impl Engine { | |||
| 835 | let Some(task_id) = send.task_id.as_deref() else { | 847 | let Some(task_id) = send.task_id.as_deref() else { |
| 836 | return Ok(None); | 848 | return Ok(None); |
| 837 | }; | 849 | }; |
| 838 | - let Some(facts) = self.store.task(task_id).await? else { | 850 | + // The task a mail speaks about may have been deleted by the very |
| 839 | - return Ok(None); | 851 | + // event the mail is about — a Deleted rule's whole point — so the |
| 852 | + // facts are read through the soft delete, exactly as the rules | ||
| 853 | + // engine read them when it owed the mail. | ||
| 854 | + let facts = match self.store.task(task_id).await? { | ||
| 855 | + Some(facts) => facts, | ||
| 856 | + None => match self.store.deleted_task(task_id).await? { | ||
| 857 | + Some(facts) => facts, | ||
| 858 | + None => return Ok(None), | ||
| 859 | + }, | ||
| 840 | }; | 860 | }; |
| 841 | let actor = self | 861 | let actor = self |
| 842 | .store | 862 | .store |
| @@ -987,8 +1007,15 @@ impl Engine { | |||
| 987 | let Some(task_id) = send.task_id.as_deref() else { | 1007 | let Some(task_id) = send.task_id.as_deref() else { |
| 988 | return Ok(None); | 1008 | return Ok(None); |
| 989 | }; | 1009 | }; |
| 990 | - let Some(facts) = self.store.task(task_id).await? else { | 1010 | + // A batch whose card was deleted inside the window — maybe by the |
| 991 | - return Ok(None); | 1011 | + // newest event itself — still goes out: where the task stands now is |
| 1012 | + // its final row, read through the soft delete. | ||
| 1013 | + let facts = match self.store.task(task_id).await? { | ||
| 1014 | + Some(facts) => facts, | ||
| 1015 | + None => match self.store.deleted_task(task_id).await? { | ||
| 1016 | + Some(facts) => facts, | ||
| 1017 | + None => return Ok(None), | ||
| 1018 | + }, | ||
| 992 | }; | 1019 | }; |
| 993 | let columns = self.store.columns_for_board(&facts.board_id).await?; | 1020 | let columns = self.store.columns_for_board(&facts.board_id).await?; |
| 994 | let column = columns | 1021 | let column = columns |
crates/iz-core/src/store/turso_store.rsmodified+42 -0
| @@ -5098,6 +5098,48 @@ impl DetailReads for TursoStore { | |||
| 5098 | })) | 5098 | })) |
| 5099 | } | 5099 | } |
| 5100 | 5100 | ||
| 5101 | + /// The same query with no `deleted_at` guard: the one read in the store | ||
| 5102 | + /// that answers for a soft-deleted row, because the mail a delete itself | ||
| 5103 | + /// owes is about the card as it stood the moment it left. The row keeps | ||
| 5104 | + /// every fact the mail names. | ||
| 5105 | + async fn deleted_task(&self, task_id: &str) -> Result<Option<TaskFacts>> { | ||
| 5106 | + let row = self | ||
| 5107 | + .one_row( | ||
| 5108 | + "SELECT t.id, t.task_key, t.title, t.column_id, t.deadline, t.clock_at, \ | ||
| 5109 | + t.position, t.done_at, t.description, t.board_id, b.workspace_id, \ | ||
| 5110 | + t.parent_id, t.tag_id, g.name \ | ||
| 5111 | + FROM task t LEFT JOIN tag g ON g.id = t.tag_id \ | ||
| 5112 | + JOIN board b ON b.id = t.board_id \ | ||
| 5113 | + WHERE t.id = ?1", | ||
| 5114 | + params![task_id], | ||
| 5115 | + ) | ||
| 5116 | + .await?; | ||
| 5117 | + let Some(row) = row else { return Ok(None) }; | ||
| 5118 | + Ok(Some(TaskFacts { | ||
| 5119 | + row: TaskRow { | ||
| 5120 | + id: text(&row, 0)?, | ||
| 5121 | + task_key: text(&row, 1)?, | ||
| 5122 | + title: text(&row, 2)?, | ||
| 5123 | + column_id: text(&row, 3)?, | ||
| 5124 | + deadline: opt_day(&row, 4)?, | ||
| 5125 | + clock_at: opt_stamp(&row, 5)?, | ||
| 5126 | + position: row.get::<f64>(6).map_err(backend)?, | ||
| 5127 | + done_at: opt_stamp(&row, 7)?, | ||
| 5128 | + parent_id: opt_text(&row, 11)?, | ||
| 5129 | + tag: match opt_text(&row, 12)? { | ||
| 5130 | + Some(id) => Some(TagChip { | ||
| 5131 | + id, | ||
| 5132 | + name: text(&row, 13)?, | ||
| 5133 | + }), | ||
| 5134 | + None => None, | ||
| 5135 | + }, | ||
| 5136 | + }, | ||
| 5137 | + description: text(&row, 8)?, | ||
| 5138 | + board_id: text(&row, 9)?, | ||
| 5139 | + workspace_id: text(&row, 10)?, | ||
| 5140 | + })) | ||
| 5141 | + } | ||
| 5142 | + | ||
| 5101 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>> { | 5143 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<Column>> { |
| 5102 | BoardReads::columns(self, board_id).await | 5144 | BoardReads::columns(self, board_id).await |
| 5103 | } | 5145 | } |
crates/iz-core/tests/store.rsmodified+102 -0
| @@ -1848,6 +1848,11 @@ impl DetailReads for CountingDetail<'_> { | |||
| 1848 | self.inner.task(task_id).await | 1848 | self.inner.task(task_id).await |
| 1849 | } | 1849 | } |
| 1850 | 1850 | ||
| 1851 | + async fn deleted_task(&self, task_id: &str) -> Result<Option<TaskFacts>, StoreError> { | ||
| 1852 | + self.tick(); | ||
| 1853 | + self.inner.deleted_task(task_id).await | ||
| 1854 | + } | ||
| 1855 | + | ||
| 1851 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<iz_core::Column>, StoreError> { | 1856 | async fn columns_for_board(&self, board_id: &str) -> Result<Vec<iz_core::Column>, StoreError> { |
| 1852 | self.tick(); | 1857 | self.tick(); |
| 1853 | self.inner.columns_for_board(board_id).await | 1858 | self.inner.columns_for_board(board_id).await |
| @@ -4873,6 +4878,103 @@ async fn assigning_one_person_mails_that_person_not_every_assignee() { | |||
| 4873 | let _ = std::fs::remove_dir_all(&dir); | 4878 | let _ = std::fs::remove_dir_all(&dir); |
| 4874 | } | 4879 | } |
| 4875 | 4880 | ||
| 4881 | +/// A "when a task is deleted" rule has to fire on the delete itself. The | ||
| 4882 | +/// activity and the soft delete ride the same write, so by the time the | ||
| 4883 | +/// engine reads the event there is no live row to read facts from — the | ||
| 4884 | +/// facts come through the delete, and the mail names the card that left. | ||
| 4885 | +#[tokio::test] | ||
| 4886 | +async fn a_delete_fires_the_rule_that_watched_for_it() { | ||
| 4887 | + let (dir, store, workspace, admin) = shared().await; | ||
| 4888 | + let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await; | ||
| 4889 | + let task = add_task(&store, &workspace, "Backlog", "Mistaken card", None, &admin).await; | ||
| 4890 | + store.assign_task(&task, &mate).await.unwrap(); | ||
| 4891 | + let board = store.board(&workspace).await.unwrap().unwrap(); | ||
| 4892 | + store | ||
| 4893 | + .create_mail_rule( | ||
| 4894 | + &board.id, | ||
| 4895 | + &Trigger::Deleted, | ||
| 4896 | + "A card was deleted", | ||
| 4897 | + Audience::Assignees, | ||
| 4898 | + OffsetDateTime::now_utc(), | ||
| 4899 | + false, | ||
| 4900 | + ) | ||
| 4901 | + .await | ||
| 4902 | + .unwrap(); | ||
| 4903 | + | ||
| 4904 | + let mailer = Remembering::taking_everything(); | ||
| 4905 | + let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh"); | ||
| 4906 | + let deletion = store | ||
| 4907 | + .delete_task(&task, &admin, OffsetDateTime::now_utc()) | ||
| 4908 | + .await | ||
| 4909 | + .unwrap(); | ||
| 4910 | + let event = activity_event(&store, &deletion.activity_id).await; | ||
| 4911 | + | ||
| 4912 | + let report = engine.on_activity(&event).await.unwrap(); | ||
| 4913 | + assert_eq!(report.sent, 1, "the delete owes its own mail"); | ||
| 4914 | + let sent = mailer.sent(); | ||
| 4915 | + assert_eq!(sent.len(), 1); | ||
| 4916 | + assert_eq!(sent[0].to, "emre@iz.sh"); | ||
| 4917 | + assert_eq!(sent[0].subject, "A card was deleted"); | ||
| 4918 | + assert!( | ||
| 4919 | + sent[0].body.contains("deleted it."), | ||
| 4920 | + "the compose line names what happened: {}", | ||
| 4921 | + sent[0].body | ||
| 4922 | + ); | ||
| 4923 | + assert!( | ||
| 4924 | + sent[0].body.contains("Mistaken card"), | ||
| 4925 | + "the mail names the card it outlives: {}", | ||
| 4926 | + sent[0].body | ||
| 4927 | + ); | ||
| 4928 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 4929 | +} | ||
| 4930 | + | ||
| 4931 | +/// The gate is the delete itself, not the missing row: an older activity a | ||
| 4932 | +/// delete raced — written first, read after — is news about a card that is | ||
| 4933 | +/// gone, and stands down the way it always did. | ||
| 4934 | +#[tokio::test] | ||
| 4935 | +async fn an_activity_a_delete_raced_still_stands_down() { | ||
| 4936 | + let (dir, store, workspace, admin) = shared().await; | ||
| 4937 | + let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await; | ||
| 4938 | + let task = add_task(&store, &workspace, "Backlog", "Ship it", None, &admin).await; | ||
| 4939 | + store.assign_task(&task, &mate).await.unwrap(); | ||
| 4940 | + let board = store.board(&workspace).await.unwrap().unwrap(); | ||
| 4941 | + store | ||
| 4942 | + .create_mail_rule( | ||
| 4943 | + &board.id, | ||
| 4944 | + &Trigger::Commented, | ||
| 4945 | + "New comment", | ||
| 4946 | + Audience::Board, | ||
| 4947 | + OffsetDateTime::now_utc(), | ||
| 4948 | + false, | ||
| 4949 | + ) | ||
| 4950 | + .await | ||
| 4951 | + .unwrap(); | ||
| 4952 | + | ||
| 4953 | + let commented = store | ||
| 4954 | + .record_activity( | ||
| 4955 | + &task, | ||
| 4956 | + Some(&mate), | ||
| 4957 | + None, | ||
| 4958 | + &ActivityKind::Commented, | ||
| 4959 | + "", | ||
| 4960 | + OffsetDateTime::now_utc(), | ||
| 4961 | + ) | ||
| 4962 | + .await | ||
| 4963 | + .unwrap(); | ||
| 4964 | + store | ||
| 4965 | + .delete_task(&task, &admin, OffsetDateTime::now_utc()) | ||
| 4966 | + .await | ||
| 4967 | + .unwrap(); | ||
| 4968 | + | ||
| 4969 | + let mailer = Remembering::taking_everything(); | ||
| 4970 | + let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh"); | ||
| 4971 | + let event = activity_event(&store, &commented).await; | ||
| 4972 | + let report = engine.on_activity(&event).await.unwrap(); | ||
| 4973 | + assert_eq!(report.sent, 0); | ||
| 4974 | + assert!(mailer.sent().is_empty()); | ||
| 4975 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 4976 | +} | ||
| 4977 | + | ||
| 4876 | #[tokio::test] | 4978 | #[tokio::test] |
| 4877 | async fn a_time_change_fires_a_deadline_set_rule() { | 4979 | async fn a_time_change_fires_a_deadline_set_rule() { |
| 4878 | let (dir, store, workspace, admin) = shared().await; | 4980 | let (dir, store, workspace, admin) = shared().await; |