diff --git a/crates/iz-core/src/store/turso_store.rs b/crates/iz-core/src/store/turso_store.rs index b4fdd53..3b69509 100644 --- a/crates/iz-core/src/store/turso_store.rs +++ b/crates/iz-core/src/store/turso_store.rs @@ -137,6 +137,7 @@ impl TursoStore { }; store.migrate(path).await?; store.retire_actor_only_decisions().await?; + store.retire_viewer_assignments().await?; store.encrypt_plaintext_passwords().await?; store.resniff_generic_attachments().await?; store.sweep_orphan_files().await?; @@ -207,6 +208,50 @@ impl TursoStore { Ok(()) } + /// Assignment rows a Viewer still carries — written before a demotion + /// swept them, or by a hand at the table — are a chip the mail audience + /// refuses: the card says four people, two get the letter. Like the two + /// repairs around it, this runs once per boot, idempotently: a database + /// the pass has healed has no row left to match, and one that never + /// carried any reads only the empty set. Reminders are re-derived to + /// match, so a pending warning names nobody the sweep just removed. + async fn retire_viewer_assignments(&self) -> Result<()> { + let mut conn = self.tx_conn().await?; + let tx = conn + .transaction_with_behavior(TransactionBehavior::Immediate) + .await + .map_err(backend)?; + let mut rows = tx + .query( + "SELECT DISTINCT a.task_id FROM task_assignee a \ + JOIN user u ON u.id = a.user_id WHERE u.role = ?1", + params![Role::Viewer.as_str()], + ) + .await + .map_err(backend)?; + let mut tasks = Vec::new(); + while let Some(row) = rows.next().await.map_err(backend)? { + tasks.push(text(&row, 0)?); + } + drop(rows); + if tasks.is_empty() { + return Ok(()); + } + tx.execute( + "DELETE FROM task_assignee WHERE user_id IN \ + (SELECT id FROM user WHERE role = ?1)", + params![Role::Viewer.as_str()], + ) + .await + .map_err(backend)?; + let now = OffsetDateTime::now_utc(); + for task_id in &tasks { + self.sync_task_reminders(&tx, task_id, now).await?; + } + tx.commit().await.map_err(backend)?; + Ok(()) + } + /// Re-decides the stored type of attachments whose `mime_type` is one of /// [`sniff::GENERIC_MIME_TYPES`] — rows written before the sniffer could /// name OOXML files, which a file chip then renders as a plain download @@ -682,6 +727,49 @@ impl TursoStore { Ok(abandoned > 0 || minted > 0) } + + /// Every task `user_id` stops being assigned to here, with whether the + /// reminder re-derivation that followed touched the queue — the pairs a + /// demotion owes its readers. Deletes the rows inside the caller's + /// transaction and re-derives each task's reminders to match, the same + /// write `unassign_task` makes: a person leaving every card at once is + /// many unassignments, not a new shape. + async fn retire_assignments( + &self, + tx: &Transaction<'_>, + user_id: &str, + now: OffsetDateTime, + ) -> Result> { + let mut rows = tx + .query( + "SELECT task_id FROM task_assignee WHERE user_id = ?1", + params![user_id], + ) + .await + .map_err(backend)?; + let mut tasks = Vec::new(); + while let Some(row) = rows.next().await.map_err(backend)? { + tasks.push(text(&row, 0)?); + } + drop(rows); + if tasks.is_empty() { + return Ok(Vec::new()); + } + // Before the resync: it reads the assignee list, and the person being + // retired must not be minted a reminder on their way out. + tx.execute( + "DELETE FROM task_assignee WHERE user_id = ?1", + params![user_id], + ) + .await + .map_err(backend)?; + let mut touched = Vec::with_capacity(tasks.len()); + for task_id in tasks { + let queue = self.sync_task_reminders(tx, &task_id, now).await?; + touched.push((task_id, queue)); + } + Ok(touched) + } } const RULE_COLUMNS: &str = "id, board_id, trigger_kind, trigger_column, subject, audience, \ @@ -1871,10 +1959,16 @@ impl Store for TursoStore { Ok(()) } } - async fn set_role(&self, user_id: &str, role: Role) -> Result<()> { - let conn = self.conn.lock().await; - let n = conn + // IMMEDIATE: a demotion's role write and the assignment sweep it owes + // are one write set — a card that renders between the two would show + // a chip the mail audience already refuses. + let mut conn = self.tx_conn().await?; + let tx = conn + .transaction_with_behavior(TransactionBehavior::Immediate) + .await + .map_err(backend)?; + let n = tx .execute( "UPDATE user SET role = ?1 WHERE id = ?2", params![role.as_str(), user_id], @@ -1882,12 +1976,36 @@ impl Store for TursoStore { .await .map_err(backend)?; if n == 0 { - Err(StoreError::NotFound) - } else { - drop(conn); - self.announce([Topic::Members]); - Ok(()) + return Err(StoreError::NotFound); + } + let mut topics = vec![Topic::Members]; + // A Viewer cannot be assigned, so a demotion to one is also every + // unassignment: a row left behind draws a chip the mail audience + // silently skips — the card saying four people while two get the + // letter. + if role == Role::Viewer { + match self + .retire_assignments(&tx, user_id, OffsetDateTime::now_utc()) + .await + { + Ok(touched) => { + for (task_id, queue) in touched { + topics.push(Topic::Task(task_id)); + if queue { + topics.push(Topic::Queue); + } + } + } + Err(e) => { + let _ = tx.rollback().await; + return Err(e); + } + } } + tx.commit().await.map_err(backend)?; + drop(conn); + self.announce(topics); + Ok(()) } async fn set_user_disabled(&self, user_id: &str, disabled: bool) -> Result<()> { diff --git a/crates/iz-core/tests/store.rs b/crates/iz-core/tests/store.rs index 523b5b2..8f6b931 100644 --- a/crates/iz-core/tests/store.rs +++ b/crates/iz-core/tests/store.rs @@ -8257,6 +8257,76 @@ async fn losing_the_admin_flag_drops_an_admin_to_member_and_nothing_else() { .unwrap(); assert_eq!(still.role, Role::Viewer); } + +/// A demotion to Viewer is every unassignment at once: the role cannot be +/// assigned, and a row left behind is a chip on the card that the mail +/// audience silently skips — the card says four people, two get the letter. +#[tokio::test] +async fn demoting_a_member_to_viewer_takes_their_assignments_with_them() { + let (dir, store, workspace, admin) = shared().await; + let kept = member(&store, &workspace, "emre@iz.sh", "Emre").await; + let demoted = member(&store, &workspace, "grace@iz.sh", "Grace").await; + let first = add_task(&store, &workspace, "Backlog", "First", None, &admin).await; + let second = add_task(&store, &workspace, "Backlog", "Second", None, &admin).await; + for task in [&first, &second] { + store.assign_task(task, &kept).await.unwrap(); + store.assign_task(task, &demoted).await.unwrap(); + } + + store.set_role(&demoted, Role::Viewer).await.unwrap(); + + for task in [&first, &second] { + let chips = store.assignees_for_task(task).await.unwrap(); + assert_eq!( + chips.iter().map(|p| p.display_name.as_str()).collect::>(), + ["Emre"], + "the demoted person is still a chip on {task}" + ); + let mailed = store.recipients_for_task(task).await.unwrap(); + assert_eq!( + mailed.iter().map(|p| p.email.as_str()).collect::>(), + ["emre@iz.sh"], + "task {task} reads two audiences" + ); + } + let _ = std::fs::remove_dir_all(&dir); +} + +/// A database from before demotions swept assignments — or a hand at the +/// table — can carry a Viewer's rows. The boot heal takes them, so the two +/// reads agree from the first render after the deploy. +#[tokio::test] +async fn a_boot_leaves_no_viewer_still_assigned() { + let dir = std::env::temp_dir().join(format!("iz-test-{}", Ulid::new())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("iz.db").to_string_lossy().into_owned(); + let store = TursoStore::open(&path, &dir.join("storage")).await.unwrap(); + let (workspace, admin) = claim(&store).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(); + drop(store); + + // The broken state itself: a role write that swept nothing, the shape a + // database written before the sweep carries. + let db = turso::Builder::new_local(&path).build().await.unwrap(); + let conn = db.connect().unwrap(); + conn.execute("PRAGMA foreign_keys = ON", ()).await.unwrap(); + conn.execute( + "UPDATE user SET role = 'viewer' WHERE id = ?1", + turso::params![mate], + ) + .await + .unwrap(); + + let reopened = TursoStore::open(&path, &dir.join("storage")).await.unwrap(); + assert!( + reopened.assignees_for_task(&task).await.unwrap().is_empty(), + "the boot kept a chip the mail audience refuses" + ); + drop(reopened); + let _ = std::fs::remove_dir_all(&dir); +} #[tokio::test] async fn a_provision_announces_only_when_the_row_changes() { let scratch = Scratch::open().await;