forge
Explore
Log in

Don't you dare go Hollow

iz-web: set project, assignees, and a dependency on create

Browse files at this commit

d3483eb104cca591a5ee6ce6a8540d956e756390Ahmet Kaan GümüşParent: 7d488cd
The new-task modal now carries the Task-tab settings that used to appear
only after the card existed. One form, extras validated before insert.
crates/iz-web/src/board.rsmodified+163 -44
@@ -74,31 +74,42 @@ async fn move_card_shared(
7474 }
7575 }
7676
77-/// Adds a card to a column. A Viewer is refused here, in the handler.
78-async fn create_task_shared(
79- cx: &Cx,
80- title: &str,
81- column_id: &str,
82- description: &str,
83- deadline_raw: &str,
84- clock_hour: Option<&str>,
85- clock_minute: Option<&str>,
86-) -> Result<Option<Refusal>> {
77+/// Adds a card to a column, wearing whatever the new-task modal posted with
78+/// it: the project, the assignees, one dependency. A Viewer is refused here,
79+/// in the handler; every extra is checked before the insert, so a bad one
80+/// refuses without leaving a half-written card. The pairs speak for absent
81+/// and blank the way `save_task`'s do.
82+async fn create_task_shared(cx: &Cx, pairs: &[(String, String)]) -> Result<Option<Refusal>> {
83+ use iz_core::detail::ActivityKind;
84+
85+ let field = |name: &str| {
86+ pairs
87+ .iter()
88+ .find(|(key, _)| key == name)
89+ .map(|(_, value)| value.as_str())
90+ };
91+ let fields = |name: &str| -> Vec<&str> {
92+ pairs
93+ .iter()
94+ .filter(|(key, _)| *key == name)
95+ .map(|(_, value)| value.as_str())
96+ .collect()
97+ };
8798 let user = match require_writer(cx).await {
8899 Ok(user) => user,
89100 Err(refusal) => return Ok(Some(refusal)),
90101 };
91- let title = title.trim();
102+ let title = field("title").unwrap_or_default().trim();
92103 if title.is_empty() {
93104 return Ok(Some(Refusal::EmptyTitle));
94105 }
95106 let zone = iz_core::detail::parse_zone(&user.timezone);
96- let time = match crate::detail::combine_clock(clock_hour, clock_minute) {
107+ let time = match crate::detail::combine_clock(field("clock_hour"), field("clock_minute")) {
97108 Ok(time) => time,
98109 Err(refusal) => return Ok(Some(refusal)),
99110 };
100111 let (deadline, clock_at) = match crate::detail::moment_field(
101- Some(deadline_raw),
112+ field("deadline"),
102113 (None, None),
103114 time.as_deref(),
104115 zone,
@@ -110,24 +121,118 @@ async fn create_task_shared(
110121 let Some(board) = store.board(&user.workspace_id).await? else {
111122 return Ok(Some(Refusal::Unavailable));
112123 };
124+ let column_id = field("column_id").unwrap_or_default();
113125 let columns = store.columns(&board.id).await?;
114126 if !columns.iter().any(|column| column.id == column_id) {
115127 return Ok(Some(Refusal::Forbidden));
116128 }
129+
130+ // The project: a posted tag has to be this board's own. Absent or blank
131+ // leaves the default on — and the select never posts blank, so blank is
132+ // a hand-crafted form, not a state the modal can reach.
133+ let tag_id = field("tag_id").filter(|tag_id| !tag_id.is_empty());
134+ if let Some(tag_id) = tag_id {
135+ let tags = store.tags(&board.id).await?;
136+ if !tags.iter().any(|tag| tag.id == tag_id) {
137+ return Ok(Some(Refusal::NotFound));
138+ }
139+ }
140+
141+ // The assignees: every id has to be a person of this workspace a task
142+ // can sit on, the same checks the assign call makes. A repeated id is
143+ // one assignment.
144+ let mut assignees: Vec<User> = Vec::new();
145+ for person_id in fields("assignee_id") {
146+ if person_id.is_empty() || assignees.iter().any(|person| person.id == person_id) {
147+ continue;
148+ }
149+ let Some(person) = store.user(person_id).await? else {
150+ return Ok(Some(Refusal::NotFound));
151+ };
152+ if person.workspace_id != user.workspace_id {
153+ return Ok(Some(Refusal::NotFound));
154+ }
155+ if !person.role.can_be_assigned() {
156+ return Ok(Some(Refusal::Forbidden));
157+ }
158+ assignees.push(person);
159+ }
160+
161+ // One dependency, if the picker named one. The card does not exist yet,
162+ // so it cannot close a circle — but the store still gets the last word
163+ // after the insert.
164+ let direction = match field("direction") {
165+ Some("blocks") => crate::detail::Direction::Blocks,
166+ // Missing, blank, or anything else: the modal's first radio arrives
167+ // pre-checked, so a form that says nothing meant that one.
168+ _ => crate::detail::Direction::BlockedBy,
169+ };
170+ let linked = match field("other_id").filter(|other_id| !other_id.is_empty()) {
171+ Some(other_id) => match crate::detail::task_of(store.as_ref(), &user, other_id).await {
172+ Ok(facts) => Some((other_id, facts)),
173+ Err(refusal) => return Ok(Some(refusal)),
174+ },
175+ None => None,
176+ };
177+
117178 let created = store
118179 .create_task(NewTask {
119180 board_id: &board.id,
120181 column_id,
121182 parent_id: None,
122183 title,
123- description: description.trim(),
184+ description: field("description").unwrap_or_default().trim(),
124185 deadline,
125186 clock_at,
126187 created_by: &user.id,
127188 })
128189 .await?;
129- mail(cx).after_activity(store, created.activity_id);
190+ mail(cx).after_activity(store.clone(), created.activity_id);
130191 mail(cx).after(created.transition);
192+
193+ let task_id = &created.row.id;
194+
195+ // The card was born wearing this tag, so no `tagged` line on its trail —
196+ // that one is for a later relabel.
197+ if let Some(tag_id) = tag_id {
198+ if created.row.tag.as_ref().map(|tag| tag.id.as_str()) != Some(tag_id) {
199+ store.set_task_tag(task_id, tag_id).await?;
200+ }
201+ }
202+ for person in &assignees {
203+ store.assign_task(task_id, &person.id).await?;
204+ let activity_id = store
205+ .record_activity(
206+ task_id,
207+ Some(&user.id),
208+ Some(&person.id),
209+ &ActivityKind::Assigned,
210+ &person.display_name,
211+ OffsetDateTime::now_utc(),
212+ )
213+ .await?;
214+ mail(cx).after_activity(store.clone(), activity_id);
215+ }
216+ if let Some((other_id, other)) = &linked {
217+ let (blocked, blocking) = crate::detail::resolve_direction(task_id, other_id, direction);
218+ let now = OffsetDateTime::now_utc();
219+ match store.add_dependency(&blocked, &blocking, now).await {
220+ Ok(()) => {}
221+ Err(iz_core::store::StoreError::Cycle) => return Ok(Some(Refusal::Cycle)),
222+ Err(error) => return Err(error.into()),
223+ }
224+ let activity_id = store
225+ .record_activity(
226+ task_id,
227+ Some(&user.id),
228+ None,
229+ &ActivityKind::Linked,
230+ &other.row.task_key,
231+ now,
232+ )
233+ .await?;
234+ mail(cx).after_activity(store, activity_id);
235+ }
131236 Ok(None)
132237 }
133238
@@ -156,35 +261,12 @@ fn redirect_refused(cx: &Cx, call: &str, refusal: Refusal) -> Redirect {
156261 Ok((StatusCode::SEE_OTHER, [(header::LOCATION, location)]))
157262 }
158263
159-#[derive(serde::Deserialize)]
160-struct CreateTaskForm {
161- title: String,
162- column_id: String,
163- #[serde(default)]
164- description: String,
165- #[serde(default)]
166- deadline: String,
167- #[serde(default)]
168- clock_hour: Option<String>,
169- #[serde(default)]
170- clock_minute: Option<String>,
171-}
172-
173-/// A browser without script's way onto the board: a real form post, same
174-/// fields the procedure below trades over the wire.
264+/// A browser without script's way onto the board: a real form post, the same
265+/// fields the procedure above trades over the wire — including the new-task
266+/// modal's extras, which arrive as repeated pairs.
175267 #[route(POST "/api/create_task")]
176-async fn create_task(cx: &Cx, Form(input): Form<CreateTaskForm>) -> Redirect {
177- match create_task_shared(
178- cx,
179- &input.title,
180- &input.column_id,
181- &input.description,
182- &input.deadline,
183- input.clock_hour.as_deref(),
184- input.clock_minute.as_deref(),
185- )
186- .await?
187- {
268+async fn create_task(cx: &Cx, Form(pairs): Form<Vec<(String, String)>>) -> Redirect {
269+ match create_task_shared(cx, &pairs).await? {
188270 Some(refusal) => redirect_refused(cx, "create_task", refusal),
189271 // Created; the referring `/?new=1` would reopen a blank new-task
190272 // modal, so land on the board itself instead.
@@ -759,6 +841,33 @@ pub async fn board_page<'a>(cx: &'a Cx, user: &'a User) -> Result<impl View + 'a
759841 };
760842
761843 let me = crate::detail::Me::from(user);
844+ // The new-task modal's extra choices are only read when it is opening:
845+ // the people a task can sit on, and every task a birth link could name,
846+ // sorted by id, not `task_key` — the key's tail is a random ULID suffix
847+ // now, so only the id still orders these by creation time (the same
848+ // reason `load_snapshot` sorts there).
849+ let (new_task_people, new_task_linkable) = if open_new {
850+ let people: Vec<User> = members
851+ .iter()
852+ .filter(|member| member.role.can_be_assigned())
853+ .cloned()
854+ .collect();
855+ let store = store(cx).clone();
856+ let mut linkable: Vec<crate::detail::LinkTarget> = store
857+ .tasks_for_board(&view_data.board.id)
858+ .await?
859+ .into_iter()
860+ .map(|task| crate::detail::LinkTarget {
861+ id: task.id,
862+ task_key: task.task_key,
863+ title: task.title,
864+ })
865+ .collect();
866+ linkable.sort_by(|a, b| a.id.cmp(&b.id));
867+ (people, linkable)
868+ } else {
869+ (Vec::new(), Vec::new())
870+ };
762871 Ok(view! {
763872 cx =>
764873 <header class="topbar">
@@ -853,7 +962,17 @@ pub async fn board_page<'a>(cx: &'a Cx, user: &'a User) -> Result<impl View + 'a
853962 }
854963 }
855964 if open_new {
856- (topcoat::view::Child::new(crate::detail::new_task_modal(cx, &all_columns, lang).await?))
965+ (topcoat::view::Child::new(
966+ crate::detail::new_task_modal(
967+ cx,
968+ &all_columns,
969+ &tags,
970+ &new_task_people,
971+ &new_task_linkable,
972+ lang,
973+ )
974+ .await?,
975+ ))
857976 }
858977 (topcoat::view::Child::new(crate::dropdown::dropdown_script(cx).await?))
859978 (topcoat::view::Child::new(crate::layout::escape_script(cx).await?))
crates/iz-web/src/detail.rsmodified+72 -2
@@ -140,7 +140,7 @@ pub enum Direction {
140140 /// Which task is `blocked` and which is `blocking`, from the task the modal
141141 /// is open on, the other task it names, and which way the form said the link
142142 /// runs.
143-fn resolve_direction(task_id: &str, other_id: &str, direction: Direction) -> (String, String) {
143+pub(crate) fn resolve_direction(task_id: &str, other_id: &str, direction: Direction) -> (String, String) {
144144 match direction {
145145 Direction::BlockedBy => (task_id.to_string(), other_id.to_string()),
146146 Direction::Blocks => (other_id.to_string(), task_id.to_string()),
@@ -173,7 +173,7 @@ mod resolve_direction_tests {
173173 /// The task, if this person's workspace is the one holding it. A task in
174174 /// another workspace is not found rather than forbidden: the answer says
175175 /// nothing about whether the id is real.
176-async fn task_of(
176+pub(crate) async fn task_of(
177177 store: &dyn Store,
178178 user: &User,
179179 task_id: &str,
@@ -2879,9 +2879,14 @@ async fn slides_view<'a>(
28792879 pub async fn new_task_modal<'a>(
28802880 cx: &'a Cx,
28812881 columns: &[(String, String)],
2882+ tags: &'a [iz_core::store::Tag],
2883+ people: &'a [iz_core::store::User],
2884+ linkable: &'a [LinkTarget],
28822885 lang: Lang,
28832886 ) -> Result<impl View + 'a> {
28842887 let columns = columns.to_vec();
2888+ let put_on_aria = t(lang, Key::PutSomeoneOnThisTask);
2889+ let link_aria = t(lang, Key::LinkAnotherTask);
28852890 Ok(view! {
28862891 cx =>
28872892 <div class="modal-scrim">
@@ -2937,6 +2942,71 @@ pub async fn new_task_modal<'a>(
29372942 </div>
29382943 </div>
29392944 </div>
2945+ <label class="field">
2946+ <span class="field-label">(t(lang, Key::Project))</span>
2947+ <span class="field-box">
2948+ <select class="status-select" name="tag_id" data-search="">
2949+ for tag in tags {
2950+ <option value=(tag.id.clone()) selected=(tag.is_default)>(tag.name.clone())</option>
2951+ }
2952+ </select>
2953+ (topcoat::view::Child::new(glyph::chevron(cx).await?))
2954+ </span>
2955+ </label>
2956+ <div class="field">
2957+ <span class="field-label">(t(lang, Key::Assignees))</span>
2958+ if !people.is_empty() {
2959+ <div class="edit edit-pop assignee-pop">
2960+ <input class="edit-toggle" type="checkbox" id="new-task-assign" aria-label=(put_on_aria)>
2961+ <label class="assignee-add edit-view edit-hit" for="new-task-assign">(topcoat::view::Child::new(glyph::plus(cx).await?))</label>
2962+ <div class="edit-form pop-panel">
2963+ <div class="pop-list pop-list-scroll">
2964+ for person in people {
2965+ <label class="pick-row">
2966+ <input type="checkbox" name="assignee_id" value=(person.id.clone())>
2967+ (topcoat::view::Child::new(crate::layout::avatar(cx, &person.id, &person.display_name, person.photo_version, "avatar-sm").await?))
2968+ <span class="pop-row-name">(person.display_name.clone())</span>
2969+ </label>
2970+ }
2971+ </div>
2972+ </div>
2973+ </div>
2974+ }
2975+ </div>
2976+ if !linkable.is_empty() {
2977+ <div class="field">
2978+ <span class="field-label">(t(lang, Key::Dependencies))</span>
2979+ <div class="edit edit-pop link-pop">
2980+ <input class="edit-toggle" type="checkbox" id="new-task-link" aria-label=(link_aria)>
2981+ <label class="dep-chip edit-view edit-hit" for="new-task-link">
2982+ (topcoat::view::Child::new(glyph::plus(cx).await?))
2983+ <span class="dep-chip-text">(t(lang, Key::LinkATask))</span>
2984+ </label>
2985+ <div class="edit-form pop-panel pop-panel-wide">
2986+ <div class="pop-list pop-list-scroll">
2987+ for target in linkable {
2988+ <label class="pick-row">
2989+ <input type="radio" name="other_id" value=(target.id.clone())>
2990+ <span class="dep-key">(target.task_key.clone())</span>
2991+ <span class="pick-title">(target.title.clone())</span>
2992+ </label>
2993+ }
2994+ </div>
2995+ <fieldset class="pick-direction">
2996+ <legend class="detail-label">(t(lang, Key::Direction))</legend>
2997+ <label class="pick-row">
2998+ <input type="radio" name="direction" value="blocked_by" checked="">
2999+ <span class="pick-title">(t(lang, Key::BlocksThisTask))</span>
3000+ </label>
3001+ <label class="pick-row">
3002+ <input type="radio" name="direction" value="blocks">
3003+ <span class="pick-title">(t(lang, Key::WaitsOnThisTask))</span>
3004+ </label>
3005+ </fieldset>
3006+ </div>
3007+ </div>
3008+ </div>
3009+ }
29403010 <label class="field">
29413011 <span class="field-label">(t(lang, Key::Description))</span>
29423012 <textarea class="detail-textarea" name="description" rows="4"></textarea>
crates/iz-web/tests/http.rsmodified+157 -0
@@ -5700,6 +5700,163 @@ async fn the_new_task_modal_opens_from_the_board_and_creates_into_the_chosen_col
57005700 .expect("the created task did not land in the chosen column");
57015701 assert_eq!(card.title, "Ship the new-task modal");
57025702 }
5703+
5704+/// The new-task modal carries the Task-tab settings at birth: the project
5705+/// select, the assignee checkboxes and — once something exists to link —
5706+/// the dependency radios. The create form is still one form: no picker may
5707+/// smuggle in a post of its own, and no control may create the card before
5708+/// the footer's submit.
5709+#[tokio::test]
5710+async fn the_new_task_modal_carries_the_task_tab_fields() {
5711+ let app = App::open().await;
5712+ let admin = admin(&app).await;
5713+ let column = first_column(&app).await;
5714+ a_tag(&app, &admin, "Atlas").await;
5715+ a_task(&app, &admin, &column, "Something to link").await;
5716+
5717+ let raw = app.get("/?new=1", Some(&admin)).await;
5718+ let html = String::from_utf8_lossy(&raw.bytes).into_owned();
5719+ assert!(
5720+ html.contains("modal-new-task"),
5721+ "the new-task modal did not render: {html}"
5722+ );
5723+ // Anchor at the modal div itself — the page's scripts quote `modal-scrim`
5724+ // too — and stop at the first script after it: what sits between is the
5725+ // modal's own markup.
5726+ let start = html
5727+ .find("modal modal-new-task")
5728+ .expect("no modal scrim");
5729+ let modal = &html[start..];
5730+ let modal = &modal[..modal.find("<script").unwrap_or(modal.len())];
5731+ assert!(modal.contains("name=\"tag_id\""), "no project field: {modal}");
5732+ assert!(
5733+ modal.contains("name=\"assignee_id\""),
5734+ "no assignee field: {modal}"
5735+ );
5736+ assert!(
5737+ modal.contains("name=\"other_id\""),
5738+ "no dependency field: {modal}"
5739+ );
5740+ assert!(
5741+ !modal.contains("action=\"/api/assign\"")
5742+ && !modal.contains("action=\"/api/link_tasks\"")
5743+ && !modal.contains("action=\"/api/set_task_tag\""),
5744+ "the modal nests a form of its own: {modal}"
5745+ );
5746+ assert!(
5747+ !modal.contains("data-autosubmit"),
5748+ "a create-modal control posts the card by itself: {modal}"
5749+ );
5750+}
5751+
5752+/// Project, assignees and one dependency can all be set on the create post
5753+/// itself: the card is born wearing the tag, carrying the person and blocking
5754+/// the older task.
5755+#[tokio::test]
5756+async fn creating_a_task_can_set_project_assignees_and_a_dependency() {
5757+ let app = App::open().await;
5758+ let admin = admin(&app).await;
5759+ invited(&app, &admin, "derya@iz.sh", "Derya", Role::Member).await;
5760+ let column = first_column(&app).await;
5761+ let other = a_task(&app, &admin, &column, "The older card").await;
5762+ let atlas = a_tag(&app, &admin, "Atlas").await;
5763+ let assignee = user_id(&app, "derya@iz.sh").await;
5764+
5765+ let answer = app
5766+ .post(
5767+ "/api/create_task",
5768+ Some(&admin),
5769+ &[
5770+ ("title", "Born dressed"),
5771+ ("column_id", &column),
5772+ ("tag_id", &atlas.id),
5773+ ("assignee_id", &assignee),
5774+ ("other_id", &other),
5775+ ("direction", "blocks"),
5776+ ],
5777+ )
5778+ .await;
5779+ assert_eq!(answer.body, "", "the create was refused: {}", answer.body);
5780+
5781+ let workspace_id = app.workspace_id().await;
5782+ let board = iz_core::board::load(app.store.as_ref(), &workspace_id)
5783+ .await
5784+ .unwrap()
5785+ .unwrap();
5786+ let older_key = board
5787+ .columns
5788+ .iter()
5789+ .flat_map(|c| &c.cards)
5790+ .find(|card| card.id == other)
5791+ .map(|card| card.task_key.clone())
5792+ .expect("the older card is not on the board");
5793+ let card = board
5794+ .columns
5795+ .iter()
5796+ .flat_map(|c| &c.cards)
5797+ .find(|card| card.title == "Born dressed")
5798+ .expect("the created task is not on the board");
5799+ assert_eq!(
5800+ card.tag.as_ref().map(|tag| tag.name.as_str()),
5801+ Some("Atlas"),
5802+ "the card did not wear the chosen tag: {:?}",
5803+ card.tag
5804+ );
5805+ assert!(
5806+ card.assignees.iter().any(|person| person.id == assignee),
5807+ "the assignee did not land: {:?}",
5808+ card.assignees
5809+ );
5810+ assert!(
5811+ card.blocks.iter().any(|key| *key == older_key),
5812+ "the new card does not block the older one: {:?}",
5813+ card.blocks
5814+ );
5815+}
5816+
5817+/// A tag from nowhere refuses the whole create: nothing is written, the same
5818+/// shape as a create into another workspace's column.
5819+#[tokio::test]
5820+async fn a_create_with_a_foreign_tag_is_refused_and_writes_nothing() {
5821+ let app = App::open().await;
5822+ let admin = admin(&app).await;
5823+ let column = first_column(&app).await;
5824+
5825+ let answer = app
5826+ .post(
5827+ "/api/create_task",
5828+ Some(&admin),
5829+ &[
5830+ ("title", "Wearing a tag from nowhere"),
5831+ ("column_id", &column),
5832+ ("tag_id", "00000000-0000-0000-0000-000000000000"),
5833+ ],
5834+ )
5835+ .await;
5836+ assert_eq!(answer.body, "");
5837+ assert!(
5838+ answer
5839+ .location
5840+ .as_deref()
5841+ .unwrap_or_default()
5842+ .contains("refusal=not-found&on=create_task"),
5843+ "{:?}",
5844+ answer.location
5845+ );
5846+ let workspace_id = app.workspace_id().await;
5847+ let board = iz_core::board::load(app.store.as_ref(), &workspace_id)
5848+ .await
5849+ .unwrap()
5850+ .unwrap();
5851+ assert!(
5852+ board
5853+ .columns
5854+ .iter()
5855+ .flat_map(|c| &c.cards)
5856+ .all(|card| card.title != "Wearing a tag from nowhere"),
5857+ "the refused task was written anyway"
5858+ );
5859+}
57035860 /// A 1×1 transparent PNG: 67 real bytes, header to checksum, so it sniffs as
57045861 /// `image/png` and survives a byte-for-byte round trip.
57055862 const PNG: [u8; 67] = [
style/main.scssmodified+8 -1
@@ -1186,7 +1186,14 @@ img.avatar {
11861186 color: var(--danger);
11871187 }
11881188
1189-.modal-new-task { width: 480px; }
1189+.modal-new-task {
1190+ width: 480px;
1191+ // The Task-tab fields can push this past a short viewport; it pans inside
1192+ // itself, but it never shows a scrollbar.
1193+ scrollbar-width: none;
1194+
1195+ &::-webkit-scrollbar { display: none; }
1196+}
11901197
11911198 // The task-detail modal shares `.modal` with the new-task modal and the
11921199 // viewer, which stay centered. The base only fixes the frame — a header on