A writer may delete, softly, after being told what goes
Browse files at this commit9b4b86ead56a74c370ed5dcc734bc151ee976e5dParent: b0f8661- feat
- a writer may delete, softly, after being told what goes
8 changed files +350 -121
- crates/dizey-core/migrations/0001_init.sql +1 -3
- crates/dizey-core/src/detail.rs +13 -0
- crates/dizey-core/src/store/mod.rs +5 -29
- crates/dizey-core/src/store/turso_store.rs +81 -15
- crates/dizey-core/tests/store.rs +61 -10
- crates/dizey-web/src/detail.rs +108 -34
- crates/dizey-web/tests/http.rs +39 -30
- style/main.scss +42 -0
crates/dizey-core/migrations/0001_init.sqlmodified+1 -3
| @@ -20,9 +20,7 @@ CREATE TABLE workspace ( | ||
| 20 | 20 | attachment_limit_bytes INTEGER NOT NULL DEFAULT 26214400, |
| 21 | 21 | photo_limit_bytes INTEGER NOT NULL DEFAULT 2097152, |
| 22 | 22 | -- JSON array of allowed extensions. An empty array means every type. |
| 23 | - allowed_file_types TEXT NOT NULL DEFAULT '[]', | |
| 24 | - -- 'anyone' | 'admin' | |
| 25 | - who_can_delete_tasks TEXT NOT NULL DEFAULT 'anyone' | |
| 23 | + allowed_file_types TEXT NOT NULL DEFAULT '[]' | |
| 26 | 24 | ); |
| 27 | 25 | |
| 28 | 26 | CREATE TABLE user ( |
crates/dizey-core/src/detail.rsmodified+13 -0
| @@ -60,6 +60,19 @@ impl DependencyEdge { | ||
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | +/// What deleting a task would take with it. The confirmation step says this | |
| 64 | +/// out loud before the button fires. | |
| 65 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 66 | +pub struct DeletionCost { | |
| 67 | + pub task_key: String, | |
| 68 | + pub title: String, | |
| 69 | + pub comment_count: u32, | |
| 70 | + /// Links in either direction that stop applying. | |
| 71 | + pub link_count: u32, | |
| 72 | + /// Keys of the tasks that would have nothing in front of them afterwards. | |
| 73 | + pub frees: Vec<String>, | |
| 74 | +} | |
| 75 | + | |
| 63 | 76 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 64 | 77 | pub struct Comment { |
| 65 | 78 | pub id: String, |
crates/dizey-core/src/store/mod.rsmodified+5 -29
| @@ -10,7 +10,7 @@ use time::OffsetDateTime; | ||
| 10 | 10 | |
| 11 | 11 | use crate::Role; |
| 12 | 12 | use crate::board::{BoardReads, TaskRow}; |
| 13 | -use crate::detail::{ActivityKind, DetailReads}; | |
| 13 | +use crate::detail::{ActivityKind, DeletionCost, DetailReads}; | |
| 14 | 14 | |
| 15 | 15 | pub mod turso_store; |
| 16 | 16 | |
| @@ -51,33 +51,6 @@ pub struct Workspace { | ||
| 51 | 51 | pub attachment_limit_bytes: u64, |
| 52 | 52 | pub photo_limit_bytes: u64, |
| 53 | 53 | pub allowed_file_types: Vec<String>, |
| 54 | - pub who_can_delete_tasks: DeletePolicy, | |
| 55 | -} | |
| 56 | - | |
| 57 | -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] | |
| 58 | -#[serde(rename_all = "snake_case")] | |
| 59 | -pub enum DeletePolicy { | |
| 60 | - /// Anyone who can write tasks may delete one. | |
| 61 | - Anyone, | |
| 62 | - /// Only the admin. | |
| 63 | - Admin, | |
| 64 | -} | |
| 65 | - | |
| 66 | -impl DeletePolicy { | |
| 67 | - pub fn as_str(self) -> &'static str { | |
| 68 | - match self { | |
| 69 | - DeletePolicy::Anyone => "anyone", | |
| 70 | - DeletePolicy::Admin => "admin", | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - pub fn parse(raw: &str) -> Result<Self> { | |
| 75 | - match raw { | |
| 76 | - "anyone" => Ok(DeletePolicy::Anyone), | |
| 77 | - "admin" => Ok(DeletePolicy::Admin), | |
| 78 | - other => Err(StoreError::Corrupt(format!("delete policy {other:?}"))), | |
| 79 | - } | |
| 80 | - } | |
| 81 | 54 | } |
| 82 | 55 | |
| 83 | 56 | /// An account. `password_hash` is `None` for an invited member who has not |
| @@ -202,7 +175,6 @@ pub trait Store: BoardReads + DetailReads + 'static { | ||
| 202 | 175 | attachment_limit_bytes: u64, |
| 203 | 176 | photo_limit_bytes: u64, |
| 204 | 177 | allowed_file_types: &[String], |
| 205 | - who_can_delete_tasks: DeletePolicy, | |
| 206 | 178 | ) -> Result<()>; |
| 207 | 179 | |
| 208 | 180 | // -- users ------------------------------------------------------------- |
| @@ -371,6 +343,10 @@ pub trait Store: BoardReads + DetailReads + 'static { | ||
| 371 | 343 | |
| 372 | 344 | /// Appends one line to a task's activity trail. `actor_id` is `None` when |
| 373 | 345 | /// the system did it rather than a person. |
| 346 | + /// What a delete would take with it, for the confirmation step. Reads | |
| 347 | + /// only; nothing here writes. | |
| 348 | + async fn deletion_cost(&self, task_id: &str) -> Result<Option<DeletionCost>>; | |
| 349 | + | |
| 374 | 350 | async fn record_activity( |
| 375 | 351 | &self, |
| 376 | 352 | task_id: &str, |
crates/dizey-core/src/store/turso_store.rsmodified+81 -15
| @@ -12,12 +12,12 @@ use turso::transaction::TransactionBehavior; | ||
| 12 | 12 | use turso::{Builder, Connection, Row, Value, params}; |
| 13 | 13 | use uuid::Uuid; |
| 14 | 14 | |
| 15 | -use super::{ | |
| 16 | - DeletePolicy, NewTask, NewUser, Result, Session, SigninLink, Store, StoreError, User, Workspace, | |
| 17 | -}; | |
| 15 | +use super::{NewTask, NewUser, Result, Session, SigninLink, Store, StoreError, User, Workspace}; | |
| 18 | 16 | use crate::Role; |
| 19 | 17 | use crate::board::{BoardMeta, BoardReads, Column, Person, TaskRow}; |
| 20 | -use crate::detail::{ActivityEntry, ActivityKind, Comment, DependencyEdge, DetailReads, TaskFacts}; | |
| 18 | +use crate::detail::{ | |
| 19 | + ActivityEntry, ActivityKind, Comment, DeletionCost, DependencyEdge, DetailReads, TaskFacts, | |
| 20 | +}; | |
| 21 | 21 | use time::Date; |
| 22 | 22 | use time::format_description::BorrowedFormatItem; |
| 23 | 23 | use time::macros::format_description; |
| @@ -216,13 +216,12 @@ fn workspace_from(row: &Row) -> Result<Workspace> { | ||
| 216 | 216 | attachment_limit_bytes: row.get::<i64>(8).map_err(backend)?.max(0) as u64, |
| 217 | 217 | allowed_file_types, |
| 218 | 218 | photo_limit_bytes: row.get::<i64>(10).map_err(backend)?.max(0) as u64, |
| 219 | - who_can_delete_tasks: DeletePolicy::parse(&text(row, 11)?)?, | |
| 220 | 219 | }) |
| 221 | 220 | } |
| 222 | 221 | |
| 223 | 222 | const WORKSPACE_COLUMNS: &str = "id, name, created_at, smtp_host, smtp_port, smtp_username, \ |
| 224 | 223 | smtp_from_name, smtp_from_address, attachment_limit_bytes, allowed_file_types, \ |
| 225 | - photo_limit_bytes, who_can_delete_tasks"; | |
| 224 | + photo_limit_bytes"; | |
| 226 | 225 | |
| 227 | 226 | fn user_from(row: &Row) -> Result<User> { |
| 228 | 227 | Ok(User { |
| @@ -439,19 +438,17 @@ impl Store for TursoStore { | ||
| 439 | 438 | attachment_limit_bytes: u64, |
| 440 | 439 | photo_limit_bytes: u64, |
| 441 | 440 | allowed_file_types: &[String], |
| 442 | - who_can_delete_tasks: DeletePolicy, | |
| 443 | 441 | ) -> Result<()> { |
| 444 | 442 | let types = serde_json::to_string(allowed_file_types) |
| 445 | 443 | .map_err(|e| StoreError::Corrupt(format!("allowed_file_types: {e}")))?; |
| 446 | 444 | self.conn |
| 447 | 445 | .execute( |
| 448 | 446 | "UPDATE workspace SET attachment_limit_bytes = ?1, photo_limit_bytes = ?2, \ |
| 449 | - allowed_file_types = ?3, who_can_delete_tasks = ?4 WHERE id = ?5", | |
| 447 | + allowed_file_types = ?3 WHERE id = ?4", | |
| 450 | 448 | params![ |
| 451 | 449 | attachment_limit_bytes as i64, |
| 452 | 450 | photo_limit_bytes as i64, |
| 453 | 451 | types, |
| 454 | - who_can_delete_tasks.as_str(), | |
| 455 | 452 | workspace_id |
| 456 | 453 | ], |
| 457 | 454 | ) |
| @@ -1208,12 +1205,9 @@ impl Store for TursoStore { | ||
| 1208 | 1205 | } |
| 1209 | 1206 | drop(rows); |
| 1210 | 1207 | |
| 1211 | - tx.execute( | |
| 1212 | - "DELETE FROM task_dependency \ | |
| 1213 | - WHERE blocked_task_id = ?1 OR blocking_task_id = ?1", | |
| 1214 | - params![task_id], | |
| 1215 | - ) | |
| 1216 | - .await?; | |
| 1208 | + // The edges and the comments stay in the table: the delete is a | |
| 1209 | + // soft one, and every read filters on the task's deleted_at, so a | |
| 1210 | + // deleted task's links stop applying without being destroyed. | |
| 1217 | 1211 | tx.execute( |
| 1218 | 1212 | "UPDATE task SET deleted_at = ?2, updated_at = ?2 WHERE id = ?1", |
| 1219 | 1213 | params![task_id, stamp.clone()], |
| @@ -1286,6 +1280,78 @@ impl Store for TursoStore { | ||
| 1286 | 1280 | } |
| 1287 | 1281 | } |
| 1288 | 1282 | |
| 1283 | + async fn deletion_cost(&self, task_id: &str) -> Result<Option<DeletionCost>> { | |
| 1284 | + let mut rows = self | |
| 1285 | + .conn | |
| 1286 | + .query( | |
| 1287 | + "SELECT task_key, title FROM task WHERE id = ?1 AND deleted_at IS NULL", | |
| 1288 | + params![task_id], | |
| 1289 | + ) | |
| 1290 | + .await | |
| 1291 | + .map_err(backend)?; | |
| 1292 | + let Some(row) = rows.next().await.map_err(backend)? else { | |
| 1293 | + return Ok(None); | |
| 1294 | + }; | |
| 1295 | + let task_key = text(&row, 0)?; | |
| 1296 | + let title = text(&row, 1)?; | |
| 1297 | + drop(rows); | |
| 1298 | + | |
| 1299 | + // Both counts in one sweep: the confirmation says what goes, and it | |
| 1300 | + // should not cost three round trips to say it. | |
| 1301 | + let mut rows = self | |
| 1302 | + .conn | |
| 1303 | + .query( | |
| 1304 | + "SELECT (SELECT COUNT(*) FROM comment WHERE task_id = ?1), \ | |
| 1305 | + (SELECT COUNT(*) FROM task_dependency d JOIN task t \ | |
| 1306 | + ON t.id = CASE WHEN d.blocked_task_id = ?1 \ | |
| 1307 | + THEN d.blocking_task_id ELSE d.blocked_task_id END \ | |
| 1308 | + WHERE (d.blocked_task_id = ?1 OR d.blocking_task_id = ?1) \ | |
| 1309 | + AND d.cleared_at IS NULL AND t.deleted_at IS NULL)", | |
| 1310 | + params![task_id], | |
| 1311 | + ) | |
| 1312 | + .await | |
| 1313 | + .map_err(backend)?; | |
| 1314 | + let (comment_count, link_count) = match rows.next().await.map_err(backend)? { | |
| 1315 | + Some(row) => ( | |
| 1316 | + row.get::<i64>(0).map_err(backend)?.max(0) as u32, | |
| 1317 | + row.get::<i64>(1).map_err(backend)?.max(0) as u32, | |
| 1318 | + ), | |
| 1319 | + None => (0, 0), | |
| 1320 | + }; | |
| 1321 | + drop(rows); | |
| 1322 | + | |
| 1323 | + // Who would be left with nothing in front of them. The same reading the | |
| 1324 | + // delete itself uses: an uncleared edge to a live task is what counts. | |
| 1325 | + let mut rows = self | |
| 1326 | + .conn | |
| 1327 | + .query( | |
| 1328 | + "SELECT t.task_key FROM task_dependency d \ | |
| 1329 | + JOIN task t ON t.id = d.blocked_task_id \ | |
| 1330 | + WHERE d.blocking_task_id = ?1 AND d.cleared_at IS NULL \ | |
| 1331 | + AND t.deleted_at IS NULL \ | |
| 1332 | + AND NOT EXISTS ( \ | |
| 1333 | + SELECT 1 FROM task_dependency o \ | |
| 1334 | + JOIN task b ON b.id = o.blocking_task_id \ | |
| 1335 | + WHERE o.blocked_task_id = t.id AND o.blocking_task_id <> ?1 \ | |
| 1336 | + AND o.cleared_at IS NULL AND b.deleted_at IS NULL)", | |
| 1337 | + params![task_id], | |
| 1338 | + ) | |
| 1339 | + .await | |
| 1340 | + .map_err(backend)?; | |
| 1341 | + let mut frees = Vec::new(); | |
| 1342 | + while let Some(row) = rows.next().await.map_err(backend)? { | |
| 1343 | + frees.push(text(&row, 0)?); | |
| 1344 | + } | |
| 1345 | + | |
| 1346 | + Ok(Some(DeletionCost { | |
| 1347 | + task_key, | |
| 1348 | + title, | |
| 1349 | + comment_count, | |
| 1350 | + link_count, | |
| 1351 | + frees, | |
| 1352 | + })) | |
| 1353 | + } | |
| 1354 | + | |
| 1289 | 1355 | async fn record_activity( |
| 1290 | 1356 | &self, |
| 1291 | 1357 | task_id: &str, |
crates/dizey-core/tests/store.rsmodified+61 -10
| @@ -8,7 +8,7 @@ use std::path::PathBuf; | ||
| 8 | 8 | |
| 9 | 9 | use dizey_core::Role; |
| 10 | 10 | use dizey_core::auth::{Token, hash_password}; |
| 11 | -use dizey_core::store::{DeletePolicy, NewUser, Store, StoreError, TursoStore, User}; | |
| 11 | +use dizey_core::store::{NewUser, Store, StoreError, TursoStore, User}; | |
| 12 | 12 | use time::{Duration, OffsetDateTime}; |
| 13 | 13 | use uuid::Uuid; |
| 14 | 14 | |
| @@ -213,7 +213,6 @@ async fn workspace_defaults_match_the_settings_screen() { | ||
| 213 | 213 | ws.allowed_file_types.is_empty(), |
| 214 | 214 | "every type until narrowed" |
| 215 | 215 | ); |
| 216 | - assert_eq!(ws.who_can_delete_tasks, DeletePolicy::Anyone); | |
| 217 | 216 | assert!(ws.smtp_host.is_none()); |
| 218 | 217 | } |
| 219 | 218 | |
| @@ -258,20 +257,13 @@ async fn limits_round_trip_including_the_file_type_list() { | ||
| 258 | 257 | let types = vec!["png".to_string(), "pdf".to_string()]; |
| 259 | 258 | scratch |
| 260 | 259 | .store |
| 261 | - .set_limits( | |
| 262 | - &ws_id, | |
| 263 | - 10 * 1024 * 1024, | |
| 264 | - 512 * 1024, | |
| 265 | - &types, | |
| 266 | - DeletePolicy::Admin, | |
| 267 | - ) | |
| 260 | + .set_limits(&ws_id, 10 * 1024 * 1024, 512 * 1024, &types) | |
| 268 | 261 | .await |
| 269 | 262 | .unwrap(); |
| 270 | 263 | let ws = scratch.store.workspace().await.unwrap().unwrap(); |
| 271 | 264 | assert_eq!(ws.attachment_limit_bytes, 10 * 1024 * 1024); |
| 272 | 265 | assert_eq!(ws.photo_limit_bytes, 512 * 1024); |
| 273 | 266 | assert_eq!(ws.allowed_file_types, types); |
| 274 | - assert_eq!(ws.who_can_delete_tasks, DeletePolicy::Admin); | |
| 275 | 267 | } |
| 276 | 268 | |
| 277 | 269 | #[tokio::test] |
| @@ -2118,3 +2110,62 @@ async fn a_task_detail_costs_seven_queries_whatever_it_carries() { | ||
| 2118 | 2110 | "the round trips a detail costs must not follow what it carries" |
| 2119 | 2111 | ); |
| 2120 | 2112 | } |
| 2113 | + | |
| 2114 | +#[tokio::test] | |
| 2115 | +async fn a_delete_says_what_it_would_take_with_it() { | |
| 2116 | + let (scratch, workspace, admin) = workspace_with_admin().await; | |
| 2117 | + let store = &scratch.store; | |
| 2118 | + let now = OffsetDateTime::now_utc(); | |
| 2119 | + | |
| 2120 | + let doomed = add_task(store, &workspace, "Backlog", "doomed", None, &admin).await; | |
| 2121 | + let other = add_task(store, &workspace, "Backlog", "other", None, &admin).await; | |
| 2122 | + let freed = add_task(store, &workspace, "Backlog", "freed", None, &admin).await; | |
| 2123 | + let still_stuck = add_task(store, &workspace, "Backlog", "stuck", None, &admin).await; | |
| 2124 | + store.add_dependency(&freed, &doomed, now).await.unwrap(); | |
| 2125 | + store | |
| 2126 | + .add_dependency(&still_stuck, &doomed, now) | |
| 2127 | + .await | |
| 2128 | + .unwrap(); | |
| 2129 | + store | |
| 2130 | + .add_dependency(&still_stuck, &other, now) | |
| 2131 | + .await | |
| 2132 | + .unwrap(); | |
| 2133 | + store | |
| 2134 | + .add_comment(&doomed, &admin, "one", now) | |
| 2135 | + .await | |
| 2136 | + .unwrap(); | |
| 2137 | + store | |
| 2138 | + .add_comment(&doomed, &admin, "two", now) | |
| 2139 | + .await | |
| 2140 | + .unwrap(); | |
| 2141 | + | |
| 2142 | + let cost = store.deletion_cost(&doomed).await.unwrap().unwrap(); | |
| 2143 | + assert_eq!(cost.title, "doomed"); | |
| 2144 | + assert_eq!(cost.comment_count, 2); | |
| 2145 | + assert_eq!(cost.link_count, 2, "both tasks waiting on this one"); | |
| 2146 | + | |
| 2147 | + // The same reading the delete itself takes: only the task with nothing | |
| 2148 | + // else in front of it is named. | |
| 2149 | + let freed_key = key_of(store, &workspace, "freed").await; | |
| 2150 | + assert_eq!(cost.frees, vec![freed_key]); | |
| 2151 | + | |
| 2152 | + // And it is a preview: nothing was written. | |
| 2153 | + assert_eq!( | |
| 2154 | + store.delete_task(&doomed, &admin, now).await.unwrap(), | |
| 2155 | + vec![freed.clone()] | |
| 2156 | + ); | |
| 2157 | + assert!(store.deletion_cost(&doomed).await.unwrap().is_none()); | |
| 2158 | +} | |
| 2159 | + | |
| 2160 | +/// The key the board shows for a task with this title. | |
| 2161 | +async fn key_of(store: &TursoStore, workspace: &str, title: &str) -> String { | |
| 2162 | + let board = board_of(store, workspace).await; | |
| 2163 | + board | |
| 2164 | + .columns | |
| 2165 | + .iter() | |
| 2166 | + .flat_map(|column| column.cards.iter()) | |
| 2167 | + .find(|card| card.title == title) | |
| 2168 | + .expect("no such card") | |
| 2169 | + .task_key | |
| 2170 | + .clone() | |
| 2171 | +} |
crates/dizey-web/src/detail.rsmodified+108 -34
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | //! authorization question, not a validation one. |
| 8 | 8 | |
| 9 | 9 | use dizey_core::board::Person; |
| 10 | -use dizey_core::detail::{Comment, DependencyEdge, TaskDetail}; | |
| 10 | +use dizey_core::detail::{Comment, DeletionCost, DependencyEdge, TaskDetail}; | |
| 11 | 11 | use leptos::prelude::*; |
| 12 | 12 | use serde::{Deserialize, Serialize}; |
| 13 | 13 | use time::Date; |
| @@ -34,7 +34,7 @@ pub struct DetailSnapshot { | ||
| 34 | 34 | pub linkable: Vec<LinkTarget>, |
| 35 | 35 | pub may_write: bool, |
| 36 | 36 | pub may_comment: bool, |
| 37 | - /// The workspace's delete policy, already applied to this person. | |
| 37 | + /// Whether this person may delete the task. A writer may. | |
| 38 | 38 | pub may_delete: bool, |
| 39 | 39 | } |
| 40 | 40 | |
| @@ -83,7 +83,6 @@ mod guard { | ||
| 83 | 83 | pub async fn fetch_task(task_id: String) -> Result<Result<DetailSnapshot, Refusal>, ServerFnError> { |
| 84 | 84 | use crate::server::{accounts, require_user}; |
| 85 | 85 | use dizey_core::detail::load; |
| 86 | - use dizey_core::store::DeletePolicy; | |
| 87 | 86 | use time::OffsetDateTime; |
| 88 | 87 | |
| 89 | 88 | let user = match require_user().await { |
| @@ -123,25 +122,16 @@ pub async fn fetch_task(task_id: String) -> Result<Result<DetailSnapshot, Refusa | ||
| 123 | 122 | linkable.sort_by(|a, b| a.task_key.cmp(&b.task_key)); |
| 124 | 123 | } |
| 125 | 124 | |
| 125 | + // A writer may delete. The delete is soft and the confirmation says what | |
| 126 | + // goes with it, which is what makes that safe — not a role. | |
| 126 | 127 | let may_write = user.role.can_write_tasks(); |
| 127 | - let policy = store | |
| 128 | - .workspace() | |
| 129 | - .await | |
| 130 | - .map_err(fail)? | |
| 131 | - .map(|workspace| workspace.who_can_delete_tasks) | |
| 132 | - .unwrap_or(DeletePolicy::Admin); | |
| 133 | - let may_delete = may_write | |
| 134 | - && match policy { | |
| 135 | - DeletePolicy::Anyone => true, | |
| 136 | - DeletePolicy::Admin => user.role.can_administer(), | |
| 137 | - }; | |
| 138 | 128 | |
| 139 | 129 | Ok(Ok(DetailSnapshot { |
| 140 | 130 | detail, |
| 141 | 131 | linkable, |
| 142 | 132 | may_write, |
| 143 | 133 | may_comment: user.role.can_comment(), |
| 144 | - may_delete, | |
| 134 | + may_delete: may_write, | |
| 145 | 135 | me: Me { |
| 146 | 136 | id: user.id, |
| 147 | 137 | display_name: user.display_name, |
| @@ -398,34 +388,45 @@ pub async fn post_comment(task_id: String, body: String) -> Result<Option<Refusa | ||
| 398 | 388 | Ok(None) |
| 399 | 389 | } |
| 400 | 390 | |
| 401 | -/// Deletes a task, subject to the workspace's policy. Whatever was waiting only | |
| 402 | -/// on it becomes unblocked, and the store records that. | |
| 391 | +/// What a delete would take with it, for the confirmation step. Reads only. | |
| 392 | +#[server] | |
| 393 | +pub async fn what_delete_costs( | |
| 394 | + task_id: String, | |
| 395 | +) -> Result<Result<DeletionCost, Refusal>, ServerFnError> { | |
| 396 | + use crate::server::accounts; | |
| 397 | + | |
| 398 | + if let Err(refusal) = guard::writer_and_task(&task_id).await { | |
| 399 | + return Ok(Err(refusal)); | |
| 400 | + } | |
| 401 | + match accounts() | |
| 402 | + .store() | |
| 403 | + .deletion_cost(&task_id) | |
| 404 | + .await | |
| 405 | + .map_err(|e| ServerFnError::new(e.to_string()))? | |
| 406 | + { | |
| 407 | + Some(cost) => Ok(Ok(cost)), | |
| 408 | + None => Ok(Err(Refusal::NotFound)), | |
| 409 | + } | |
| 410 | +} | |
| 411 | + | |
| 412 | +/// Deletes a task. A writer may: the delete is soft — the row keeps a | |
| 413 | +/// `deleted_at`, its comments and its edges stay in the table — so a mistake is | |
| 414 | +/// recoverable by hand. Whatever was waiting only on it becomes unblocked, and | |
| 415 | +/// the store records that. | |
| 403 | 416 | #[server] |
| 404 | 417 | pub async fn delete_task(task_id: String) -> Result<Option<Refusal>, ServerFnError> { |
| 405 | 418 | use crate::server::accounts; |
| 406 | - use dizey_core::store::DeletePolicy; | |
| 407 | 419 | use time::OffsetDateTime; |
| 408 | 420 | |
| 409 | 421 | let (user, _) = match guard::writer_and_task(&task_id).await { |
| 410 | 422 | Ok(pair) => pair, |
| 411 | 423 | Err(refusal) => return Ok(Some(refusal)), |
| 412 | 424 | }; |
| 413 | - let store = accounts().store().clone(); | |
| 414 | - let fail = |e: dizey_core::store::StoreError| ServerFnError::new(e.to_string()); | |
| 415 | - let policy = store | |
| 416 | - .workspace() | |
| 417 | - .await | |
| 418 | - .map_err(fail)? | |
| 419 | - .map(|workspace| workspace.who_can_delete_tasks) | |
| 420 | - .unwrap_or(DeletePolicy::Admin); | |
| 421 | - if matches!(policy, DeletePolicy::Admin) && !user.role.can_administer() { | |
| 422 | - return Ok(Some(Refusal::Forbidden)); | |
| 423 | - } | |
| 424 | - | |
| 425 | - store | |
| 425 | + accounts() | |
| 426 | + .store() | |
| 426 | 427 | .delete_task(&task_id, &user.id, OffsetDateTime::now_utc()) |
| 427 | 428 | .await |
| 428 | - .map_err(fail)?; | |
| 429 | + .map_err(|e| ServerFnError::new(e.to_string()))?; | |
| 429 | 430 | Ok(None) |
| 430 | 431 | } |
| 431 | 432 | |
| @@ -536,6 +537,10 @@ fn DetailScreen( | ||
| 536 | 537 | let comment = ServerAction::<PostComment>::new(); |
| 537 | 538 | let link = ServerAction::<LinkTasks>::new(); |
| 538 | 539 | let remove = ServerAction::<DeleteTask>::new(); |
| 540 | + // Deleting is two steps: ask what it would cost, then say it out loud and | |
| 541 | + // let the person decide. The artboard's red button had no confirmation and | |
| 542 | + // this action reaches other people's tasks. | |
| 543 | + let ask = ServerAction::<WhatDeleteCosts>::new(); | |
| 539 | 544 | // Every action that lands re-reads the task and the board behind it. |
| 540 | 545 | for value in [save.value(), comment.value(), link.value()] { |
| 541 | 546 | Effect::new(move |_| { |
| @@ -793,16 +798,85 @@ fn DetailScreen( | ||
| 793 | 798 | |
| 794 | 799 | {problem} |
| 795 | 800 | |
| 801 | + {move || { | |
| 802 | + ask.value() | |
| 803 | + .get() | |
| 804 | + .and_then(|answer| answer.ok().and_then(|inner| inner.ok())) | |
| 805 | + .map(|cost| { | |
| 806 | + let DeletionCost { task_key, title, comment_count, link_count, frees } = cost; | |
| 807 | + let freed = frees.join(", "); | |
| 808 | + view! { | |
| 809 | + <div class="confirm"> | |
| 810 | + <div class="confirm-title"> | |
| 811 | + {format!("Delete {task_key} — {title}?")} | |
| 812 | + </div> | |
| 813 | + <ul class="confirm-list"> | |
| 814 | + {(comment_count > 0) | |
| 815 | + .then(|| { | |
| 816 | + view! { | |
| 817 | + <li> | |
| 818 | + {if comment_count == 1 { | |
| 819 | + "1 comment goes with it".to_string() | |
| 820 | + } else { | |
| 821 | + format!("{comment_count} comments go with it") | |
| 822 | + }} | |
| 823 | + </li> | |
| 824 | + } | |
| 825 | + })} | |
| 826 | + {(link_count > 0) | |
| 827 | + .then(|| { | |
| 828 | + view! { | |
| 829 | + <li> | |
| 830 | + {if link_count == 1 { | |
| 831 | + "1 dependency stops applying".to_string() | |
| 832 | + } else { | |
| 833 | + format!("{link_count} dependencies stop applying") | |
| 834 | + }} | |
| 835 | + </li> | |
| 836 | + } | |
| 837 | + })} | |
| 838 | + {(!freed.is_empty()) | |
| 839 | + .then(|| { | |
| 840 | + view! { <li>{format!("{freed} stops being blocked")}</li> } | |
| 841 | + })} | |
| 842 | + </ul> | |
| 843 | + <div class="confirm-note"> | |
| 844 | + "The task keeps its record in the database, but nothing in Dizey brings it back." | |
| 845 | + </div> | |
| 846 | + <div class="confirm-row"> | |
| 847 | + <button | |
| 848 | + class="detail-cancel" | |
| 849 | + type="button" | |
| 850 | + on:click=move |_| ask.value().set(None) | |
| 851 | + > | |
| 852 | + "Keep it" | |
| 853 | + </button> | |
| 854 | + <ActionForm action=remove attr:class="detail-delete-form"> | |
| 855 | + <input type="hidden" name="task_id" value=move || id.get_value()/> | |
| 856 | + <button | |
| 857 | + class="detail-delete detail-delete-sure" | |
| 858 | + type="submit" | |
| 859 | + disabled=move || remove.pending().get() | |
| 860 | + > | |
| 861 | + {format!("Delete {task_key}")} | |
| 862 | + </button> | |
| 863 | + </ActionForm> | |
| 864 | + </div> | |
| 865 | + </div> | |
| 866 | + } | |
| 867 | + }) | |
| 868 | + }} | |
| 869 | + | |
| 796 | 870 | <footer class="detail-foot"> |
| 797 | 871 | {may_delete |
| 798 | 872 | .then(|| { |
| 799 | 873 | view! { |
| 800 | - <ActionForm action=remove attr:class="detail-delete-form"> | |
| 874 | + <ActionForm action=ask attr:class="detail-delete-form"> | |
| 801 | 875 | <input type="hidden" name="task_id" value=move || id.get_value()/> |
| 802 | 876 | <button |
| 803 | 877 | class="detail-delete" |
| 804 | 878 | type="submit" |
| 805 | - disabled=move || remove.pending().get() | |
| 879 | + disabled=move || ask.pending().get() | |
| 806 | 880 | > |
| 807 | 881 | "Delete task" |
| 808 | 882 | </button> |
crates/dizey-web/tests/http.rsmodified+39 -30
| @@ -28,23 +28,18 @@ use uuid::Uuid; | ||
| 28 | 28 | struct App { |
| 29 | 29 | dir: PathBuf, |
| 30 | 30 | router: Router, |
| 31 | - /// The same store the router talks to, so a test can set a workspace | |
| 32 | - /// setting that has no screen yet. | |
| 33 | - store: Arc<TursoStore>, | |
| 34 | 31 | } |
| 35 | 32 | |
| 36 | 33 | impl App { |
| 37 | 34 | async fn open() -> Self { |
| 38 | 35 | let dir = std::env::temp_dir().join(format!("dizey-http-{}", Uuid::new_v4())); |
| 39 | 36 | std::fs::create_dir_all(&dir).unwrap(); |
| 40 | - let store = Arc::new( | |
| 41 | - TursoStore::open(dir.join("dizey.db").to_str().unwrap()) | |
| 42 | - .await | |
| 43 | - .unwrap(), | |
| 44 | - ); | |
| 37 | + let store = TursoStore::open(dir.join("dizey.db").to_str().unwrap()) | |
| 38 | + .await | |
| 39 | + .unwrap(); | |
| 45 | 40 | let options = LeptosOptions::builder().output_name("dizey").build(); |
| 46 | - let router = dizey_web::server::router(Accounts::new(store.clone()), options); | |
| 47 | - Self { dir, router, store } | |
| 41 | + let router = dizey_web::server::router(Accounts::new(Arc::new(store)), options); | |
| 42 | + Self { dir, router } | |
| 48 | 43 | } |
| 49 | 44 | |
| 50 | 45 | /// Posts a form to a server function, as the browser does, and returns the |
| @@ -472,28 +467,33 @@ async fn a_viewer_who_posts_a_delete_anyway_is_refused() { | ||
| 472 | 467 | } |
| 473 | 468 | |
| 474 | 469 | #[tokio::test] |
| 475 | -async fn a_member_cannot_delete_when_the_workspace_says_admins_only() { | |
| 476 | - use dizey_core::store::{DeletePolicy, Store}; | |
| 477 | - | |
| 470 | +async fn a_member_may_delete_and_the_delete_is_soft() { | |
| 478 | 471 | let app = App::open().await; |
| 479 | 472 | let admin = admin(&app).await; |
| 480 | 473 | let member = invited(&app, &admin, "rae@dizey.sh", "Rae Okonkwo", Role::Member).await; |
| 481 | 474 | let column = first_column(&app, &admin).await; |
| 482 | - let task = a_task(&app, &admin, &column, "Not yours to remove").await; | |
| 483 | - | |
| 484 | - // The workspace ships as 'anyone'; the settings screen that flips this is a | |
| 485 | - // later slice, so the test sets it the way that screen will. | |
| 486 | - let workspace = app.store.workspace().await.unwrap().unwrap(); | |
| 487 | - app.store | |
| 488 | - .set_limits( | |
| 489 | - &workspace.id, | |
| 490 | - workspace.attachment_limit_bytes, | |
| 491 | - workspace.photo_limit_bytes, | |
| 492 | - &workspace.allowed_file_types, | |
| 493 | - DeletePolicy::Admin, | |
| 475 | + let task = a_task(&app, &admin, &column, "Mistyped in a hurry").await; | |
| 476 | + | |
| 477 | + // What it would cost is a read: it says so and writes nothing. | |
| 478 | + let answer = app | |
| 479 | + .post( | |
| 480 | + path::<dizey_web::detail::WhatDeleteCosts>(), | |
| 481 | + Some(&member), | |
| 482 | + &[("task_id", &task)], | |
| 494 | 483 | ) |
| 495 | - .await | |
| 496 | - .unwrap(); | |
| 484 | + .await; | |
| 485 | + assert!( | |
| 486 | + answer.body.contains("Mistyped in a hurry"), | |
| 487 | + "{}", | |
| 488 | + answer.body | |
| 489 | + ); | |
| 490 | + let answer = app | |
| 491 | + .post(path::<dizey_web::board::CurrentBoard>(), Some(&admin), &[]) | |
| 492 | + .await; | |
| 493 | + assert!( | |
| 494 | + answer.body.contains("Mistyped in a hurry"), | |
| 495 | + "asking cost deleted it" | |
| 496 | + ); | |
| 497 | 497 | |
| 498 | 498 | let answer = app |
| 499 | 499 | .post( |
| @@ -502,14 +502,23 @@ async fn a_member_cannot_delete_when_the_workspace_says_admins_only() { | ||
| 502 | 502 | &[("task_id", &task)], |
| 503 | 503 | ) |
| 504 | 504 | .await; |
| 505 | - assert_eq!(answer.body, "\"Forbidden\""); | |
| 505 | + assert_eq!(answer.body, "null", "a member was refused: {}", answer.body); | |
| 506 | 506 | |
| 507 | + // Gone from the board, and gone from the detail: soft is not visible. | |
| 507 | 508 | let answer = app |
| 508 | 509 | .post(path::<dizey_web::board::CurrentBoard>(), Some(&admin), &[]) |
| 509 | 510 | .await; |
| 510 | 511 | assert!( |
| 511 | - answer.body.contains("Not yours to remove"), | |
| 512 | - "the refused delete happened anyway: {}", | |
| 512 | + !answer.body.contains("Mistyped in a hurry"), | |
| 513 | + "{}", | |
| 513 | 514 | answer.body |
| 514 | 515 | ); |
| 516 | + let answer = app | |
| 517 | + .post( | |
| 518 | + path::<dizey_web::detail::FetchTask>(), | |
| 519 | + Some(&admin), | |
| 520 | + &[("task_id", &task)], | |
| 521 | + ) | |
| 522 | + .await; | |
| 523 | + assert_eq!(answer.body, "{\"Err\":\"NotFound\"}"); | |
| 515 | 524 | } |
style/main.scssmodified+42 -0
| @@ -1006,3 +1006,45 @@ a { | ||
| 1006 | 1006 | padding: 6px 12px; |
| 1007 | 1007 | cursor: pointer; |
| 1008 | 1008 | } |
| 1009 | + | |
| 1010 | +.confirm { | |
| 1011 | + margin-top: 18px; | |
| 1012 | + padding: 14px 16px; | |
| 1013 | + background: #fdf6f2; | |
| 1014 | + border: 1px solid #e8c9b8; | |
| 1015 | + border-radius: var(--radius); | |
| 1016 | +} | |
| 1017 | + | |
| 1018 | +.confirm-title { | |
| 1019 | + font-size: 14px; | |
| 1020 | + font-weight: 600; | |
| 1021 | + color: #a04a24; | |
| 1022 | +} | |
| 1023 | + | |
| 1024 | +.confirm-list { | |
| 1025 | + margin: 8px 0 0; | |
| 1026 | + padding-left: 18px; | |
| 1027 | + font-size: 13px; | |
| 1028 | + line-height: 1.6; | |
| 1029 | + color: #3d3a34; | |
| 1030 | +} | |
| 1031 | + | |
| 1032 | +.confirm-note { | |
| 1033 | + margin-top: 8px; | |
| 1034 | + font-size: 12px; | |
| 1035 | + color: var(--muted); | |
| 1036 | +} | |
| 1037 | + | |
| 1038 | +.confirm-row { | |
| 1039 | + display: flex; | |
| 1040 | + align-items: center; | |
| 1041 | + justify-content: flex-end; | |
| 1042 | + gap: 8px; | |
| 1043 | + margin-top: 12px; | |
| 1044 | +} | |
| 1045 | + | |
| 1046 | +.detail-delete-sure { | |
| 1047 | + color: #fffefb; | |
| 1048 | + background: #a04a24; | |
| 1049 | + border-color: #a04a24; | |
| 1050 | +} |