forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

task.rs648 lines, 25.4 KiBHistoryRawDownload
1//! The task modal's markup: the `<dialog>` over the board, its sections, and the plain-text renderer for
2//! descriptions and comments.
3//!
4//! Text from users is never markup. [`segments`] splits a text into lines, text runs and links; only
5//! addresses that start with `http://` or `https://` become links, with `rel="noopener noreferrer
6//! nofollow"`. Everything else is escaped by `view!`, so `<img onerror=...>` and `javascript:` addresses
7//! reach the page as plain characters.
8//!
9//! Forms carry the CSRF token and the modal's own URL as `next`. A viewer gets the same sections with no
10//! form, so the read-only view and the write view differ only in controls.
11
12use topcoat::Result;
13use topcoat::view::{View, component, view};
14
15use crate::i18n::task::{K, t};
16use crate::pages::board::BoardPage;
17use crate::pages::task::{TaskModal, task_link};
18use crate::views::alert;
19use crate::views::board::flag;
20use crate::views::files::task_files_section;
21
22/// One piece of a plain-text body: a run of text, a link, or a line break.
23pub struct Segment {
24 /// The text of the run, or the address of the link. Empty for a line break.
25 pub text: String,
26 /// Whether this piece is an address that becomes a link.
27 pub link: bool,
28 /// Whether this piece is a line break.
29 pub brk: bool,
30}
31
32/// The text run `text` as a segment.
33fn run(text: &str) -> Segment {
34 Segment {
35 text: text.to_owned(),
36 link: false,
37 brk: false,
38 }
39}
40
41/// The first `http://` or `https://` address in `text` that is longer than its scheme, as its byte range.
42/// Trailing sentence punctuation and brackets are not part of the address; whitespace and `<>"'` end it.
43fn next_link(text: &str) -> Option<(usize, usize)> {
44 let lower = text.to_ascii_lowercase();
45 let mut from = 0;
46 loop {
47 let (offset, scheme) = ["http://", "https://"]
48 .iter()
49 .filter_map(|scheme| lower[from..].find(scheme).map(|at| (at, *scheme)))
50 .min_by_key(|(at, _)| *at)?;
51 let start = from + offset;
52 let tail = &text[start..];
53 let length = tail
54 .find(|c: char| c.is_whitespace() || matches!(c, '<' | '>' | '"' | '\'' | '`'))
55 .unwrap_or(tail.len());
56 let mut end = start + length;
57 while end > start
58 && text[..end]
59 .chars()
60 .last()
61 .is_some_and(|c| matches!(c, '.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '}'))
62 {
63 end -= 1;
64 }
65 if end - start > scheme.len() {
66 return Some((start, end));
67 }
68 from = start + scheme.len();
69 }
70}
71
72/// Splits a plain-text body into segments: each line, its runs of text and its links, with a line break
73/// between lines. A carriage return before a line feed is dropped.
74pub fn segments(text: &str) -> Vec<Segment> {
75 let mut out = Vec::new();
76 for (index, line) in text.split('\n').enumerate() {
77 if index > 0 {
78 out.push(Segment {
79 text: String::new(),
80 link: false,
81 brk: true,
82 });
83 }
84 let mut rest = line.strip_suffix('\r').unwrap_or(line);
85 while let Some((start, end)) = next_link(rest) {
86 if start > 0 {
87 out.push(run(&rest[..start]));
88 }
89 out.push(Segment {
90 text: rest[start..end].to_owned(),
91 link: true,
92 brk: false,
93 });
94 rest = &rest[end..];
95 }
96 if !rest.is_empty() {
97 out.push(run(rest));
98 }
99 }
100 out
101}
102
103/// A plain-text body as markup: escaped text, line breaks, and links for http and https addresses only.
104#[component]
105pub async fn rich_text(text: &str) -> Result<impl View> {
106 let pieces = segments(text);
107 Ok(view! {
108 <div class="rich-text">
109 for piece in pieces.iter() {
110 if piece.brk {
111 <br>
112 }
113 if piece.link {
114 <a href=(piece.text.as_str()) rel="noopener noreferrer nofollow" target="_blank">(piece.text.as_str())</a>
115 }
116 if !piece.link && !piece.brk {
117 (piece.text.as_str())
118 }
119 }
120 </div>
121 })
122}
123
124/// The modal: the `<dialog>` over the board. Escape and the close link return to the board with its
125/// filters (`board.js` reads `data-close-href`). Without JavaScript the dialog is shown in place.
126#[component]
127pub async fn task_modal(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
128 let lang = page.lang;
129 let task = &modal.detail.task;
130 let key = task.key.as_str();
131 let close = modal.close.as_str();
132 Ok(view! {
133 <dialog class="task-dialog" open="" aria-labelledby="task-heading" data-close-href=(close)>
134 <header class="task-head">
135 <p class="task-key">(key)</p>
136 <a class="button ghost" href=(close)>(t(lang, K::Close))</a>
137 </header>
138 if let Some(message) = modal.error {
139 alert(message: message)
140 }
141 <h2 id="task-heading" class="task-title">(task.title.as_str())</h2>
142 task_details(page: page, modal: modal)
143 task_files(page: page, modal: modal)
144 task_people(page: page, modal: modal)
145 task_structure(page: page, modal: modal)
146 task_links(page: page, modal: modal)
147 task_comments(page: page, modal: modal)
148 task_activity(page: page, modal: modal)
149 task_delete(page: page, modal: modal)
150 </dialog>
151 })
152}
153
154/// The fields as read, and for a member the edit form folded under them. The form opens by itself after a
155/// refusal, so the typed values stay in view.
156#[component]
157pub async fn task_details(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
158 let lang = page.lang;
159 let task = &modal.detail.task;
160 let deadline = task.deadline.map(|day| day.to_string()).unwrap_or_default();
161 let deadline = if deadline.is_empty() {
162 "-".to_owned()
163 } else {
164 deadline
165 };
166 let time = if modal.edit.time.is_empty() {
167 "-".to_owned()
168 } else {
169 modal.edit.time.clone()
170 };
171 let project = modal.detail.tag.name.as_str();
172 let id = task.id.as_str();
173 let edit_action = format!("/tasks/{id}/edit");
174 let csrf = page.viewer.csrf.as_str();
175 let next = modal.base.as_str();
176 let edit = &modal.edit;
177 Ok(view! {
178 <section class="task-section" aria-label=(t(lang, K::Description))>
179 <dl class="task-facts">
180 <dt>(t(lang, K::Project))</dt>
181 <dd>(project)</dd>
182 <dt>(t(lang, K::Deadline))</dt>
183 <dd>(deadline.as_str())</dd>
184 <dt>(t(lang, K::Meeting))</dt>
185 <dd>(time.as_str())</dd>
186 </dl>
187 rich_text(text: task.description.as_str())
188 if modal.can_write {
189 <details class="task-edit" open=(flag(modal.error.is_some()))>
190 <summary>(t(lang, K::EditComment))</summary>
191 <form class="task-form" method="post" action=(edit_action.as_str())>
192 <input type="hidden" name="csrf" value=(csrf)>
193 <input type="hidden" name="next" value=(next)>
194 <div class="new-task-grid">
195 <div class="field field-wide">
196 <label for="tm-title">(t(lang, K::Title))</label>
197 <input id="tm-title" name="title" maxlength="200" required="" value=(edit.title.as_str())>
198 </div>
199 <div class="field">
200 <label for="tm-tag">(t(lang, K::Project))</label>
201 <select id="tm-tag" name="tag">
202 for tag in page.tags.iter() {
203 <option value=(tag.id.as_str()) selected=(flag(edit.tag == tag.id))>(tag.name.as_str())</option>
204 }
205 </select>
206 </div>
207 <div class="field">
208 <label for="tm-deadline">(t(lang, K::Deadline))</label>
209 <input id="tm-deadline" type="date" name="deadline" value=(edit.deadline.as_str())>
210 </div>
211 <div class="field">
212 <label for="tm-time">(t(lang, K::Meeting))</label>
213 <input id="tm-time" type="time" name="time" value=(edit.time.as_str())>
214 <p class="field-hint">(t(lang, K::MeetingHint))</p>
215 </div>
216 <div class="field field-wide">
217 <label for="tm-description">(t(lang, K::Description))</label>
218 <textarea id="tm-description" name="description" maxlength="20000">(edit.description.as_str())</textarea>
219 <p class="field-hint">(t(lang, K::DescriptionHint))</p>
220 </div>
221 </div>
222 <button class="button primary" type="submit">(t(lang, K::Save))</button>
223 </form>
224 </details>
225 }
226 </section>
227 })
228}
229
230/// The attachments of the task: the viewer when a file is open, the list and the upload form.
231#[component]
232pub async fn task_files(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
233 let id = modal.detail.task.id.as_str();
234 let csrf = page.viewer.csrf.as_str();
235 let next = modal.base.as_str();
236 Ok(view! {
237 task_files_section(
238 lang: page.lang,
239 csrf: csrf,
240 next: next,
241 task_id: id,
242 can_write: modal.can_write,
243 files: modal.files.as_slice(),
244 open: modal.viewer.as_ref(),
245 )
246 })
247}
248
249/// The assignees, with a remove control each, the picker for a new one, and the watch switch.
250#[component]
251pub async fn task_people(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
252 let lang = page.lang;
253 let id = modal.detail.task.id.as_str();
254 let csrf = page.viewer.csrf.as_str();
255 let next = modal.base.as_str();
256 let assign_action = format!("/tasks/{id}/assign");
257 let unassign_action = format!("/tasks/{id}/unassign");
258 let watch_action = format!("/tasks/{id}/watch");
259 let watch_on = if modal.watching { "0" } else { "1" };
260 let watch_label = if modal.watching {
261 t(lang, K::Unwatch)
262 } else {
263 t(lang, K::Watch)
264 };
265 let watch_text = if modal.watching {
266 t(lang, K::Watching)
267 } else {
268 t(lang, K::NotWatching)
269 };
270 let assignees: Vec<(String, String)> = modal
271 .detail
272 .assignees
273 .iter()
274 .map(|a| (a.sub.clone(), a.name.clone()))
275 .collect();
276 let picks: Vec<(String, String)> = modal
277 .assignable
278 .iter()
279 .map(|member| (member.sub.clone(), member.name.clone()))
280 .collect();
281 Ok(view! {
282 <section class="task-section" aria-labelledby="task-people">
283 <h3 id="task-people">(t(lang, K::Assignees))</h3>
284 if assignees.is_empty() {
285 <p class="muted">(t(lang, K::NobodyAssigned))</p>
286 }
287 <ul class="task-list">
288 for (sub, name) in assignees.iter() {
289 <li class="task-row">
290 <span>(name.as_str())</span>
291 if modal.can_write {
292 <form method="post" action=(unassign_action.as_str())>
293 <input type="hidden" name="csrf" value=(csrf)>
294 <input type="hidden" name="next" value=(next)>
295 <input type="hidden" name="sub" value=(sub.as_str())>
296 <button class="button ghost" type="submit">(t(lang, K::Remove))</button>
297 </form>
298 }
299 </li>
300 }
301 </ul>
302 if modal.can_write && !picks.is_empty() {
303 <form class="task-inline" method="post" action=(assign_action.as_str())>
304 <input type="hidden" name="csrf" value=(csrf)>
305 <input type="hidden" name="next" value=(next)>
306 <div class="field">
307 <label for="assign-sub">(t(lang, K::AssignPick))</label>
308 <select id="assign-sub" name="sub">
309 for (sub, name) in picks.iter() {
310 <option value=(sub.as_str())>(name.as_str())</option>
311 }
312 </select>
313 </div>
314 <button class="button" type="submit">(t(lang, K::Assign))</button>
315 </form>
316 }
317 <p class="muted">(watch_text)</p>
318 if modal.can_write {
319 <form class="task-inline" method="post" action=(watch_action.as_str())>
320 <input type="hidden" name="csrf" value=(csrf)>
321 <input type="hidden" name="next" value=(next)>
322 <input type="hidden" name="on" value=(watch_on)>
323 <button class="button ghost" type="submit">(watch_label)</button>
324 </form>
325 }
326 </section>
327 })
328}
329
330/// The subtasks, the form that adds one, and the parent picker.
331#[component]
332pub async fn task_structure(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
333 let lang = page.lang;
334 let task = &modal.detail.task;
335 let id = task.id.as_str();
336 let csrf = page.viewer.csrf.as_str();
337 let next = modal.base.as_str();
338 let subtask_action = format!("/tasks/{id}/subtasks");
339 let parent_action = format!("/tasks/{id}/parent");
340 let subtasks: Vec<(String, String)> = modal
341 .detail
342 .subtasks
343 .iter()
344 .map(|sub| {
345 (
346 task_link(&modal.close, &sub.key),
347 format!("{} {}", sub.key, sub.title),
348 )
349 })
350 .collect();
351 let parent_id = task.parent_id.clone().unwrap_or_default();
352 let parents: Vec<(String, String, bool)> = modal
353 .options
354 .iter()
355 .filter(|other| other.parent_id.is_none())
356 .map(|other| {
357 (
358 other.id.clone(),
359 format!("{} {}", other.key, other.title),
360 other.id == parent_id,
361 )
362 })
363 .collect();
364 let title_max = iz_core::limits::TITLE_MAX.to_string();
365 Ok(view! {
366 <section class="task-section" aria-labelledby="task-subtasks">
367 <h3 id="task-subtasks">(t(lang, K::Subtasks))</h3>
368 if subtasks.is_empty() {
369 <p class="muted">(t(lang, K::NoSubtasks))</p>
370 }
371 <ul class="task-list">
372 for (link, label) in subtasks.iter() {
373 <li class="task-row"><a href=(link.as_str())>(label.as_str())</a></li>
374 }
375 </ul>
376 if modal.can_write && task.parent_id.is_none() {
377 <form class="task-inline" method="post" action=(subtask_action.as_str())>
378 <input type="hidden" name="csrf" value=(csrf)>
379 <input type="hidden" name="next" value=(next)>
380 <div class="field field-wide">
381 <label for="sub-title">(t(lang, K::SubtaskTitle))</label>
382 <input id="sub-title" name="title" maxlength=(title_max.as_str()) required="">
383 </div>
384 <button class="button" type="submit">(t(lang, K::AddSubtask))</button>
385 </form>
386 }
387 if modal.can_write {
388 <form class="task-inline" method="post" action=(parent_action.as_str())>
389 <input type="hidden" name="csrf" value=(csrf)>
390 <input type="hidden" name="next" value=(next)>
391 <div class="field">
392 <label for="parent-pick">(t(lang, K::Parent))</label>
393 <select id="parent-pick" name="parent">
394 <option value="" selected=(flag(task.parent_id.is_none()))>(t(lang, K::NoParent))</option>
395 for (parent, label, chosen) in parents.iter() {
396 <option value=(parent.as_str()) selected=(flag(*chosen))>(label.as_str())</option>
397 }
398 </select>
399 </div>
400 <button class="button" type="submit">(t(lang, K::SetParent))</button>
401 </form>
402 }
403 </section>
404 })
405}
406
407/// The dependencies in both directions, each with a remove control, and the form that adds one.
408#[component]
409pub async fn task_links(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
410 let lang = page.lang;
411 let id = modal.detail.task.id.as_str();
412 let csrf = page.viewer.csrf.as_str();
413 let next = modal.base.as_str();
414 let remove_action = format!("/tasks/{id}/links/remove");
415 let add_action = format!("/tasks/{id}/links");
416 let waits: Vec<(String, String, String)> = modal
417 .detail
418 .blocked_by
419 .iter()
420 .map(|other| {
421 (
422 other.id.clone(),
423 task_link(&modal.close, &other.key),
424 format!("{} {}", other.key, other.title),
425 )
426 })
427 .collect();
428 let blocks: Vec<(String, String, String)> = modal
429 .detail
430 .blocks
431 .iter()
432 .map(|other| {
433 (
434 other.id.clone(),
435 task_link(&modal.close, &other.key),
436 format!("{} {}", other.key, other.title),
437 )
438 })
439 .collect();
440 let options: Vec<(String, String)> = modal
441 .options
442 .iter()
443 .map(|other| (other.id.clone(), format!("{} {}", other.key, other.title)))
444 .collect();
445 let nothing = waits.is_empty() && blocks.is_empty();
446 Ok(view! {
447 <section class="task-section" aria-labelledby="task-deps">
448 <h3 id="task-deps">(t(lang, K::Dependencies))</h3>
449 if nothing {
450 <p class="muted">(t(lang, K::NoLinks))</p>
451 }
452 if !waits.is_empty() {
453 <h4>(t(lang, K::BlockedBy))</h4>
454 <ul class="task-list">
455 for (other_id, link, label) in waits.iter() {
456 <li class="task-row">
457 <a href=(link.as_str())>(label.as_str())</a>
458 if modal.can_write {
459 <form method="post" action=(remove_action.as_str())>
460 <input type="hidden" name="csrf" value=(csrf)>
461 <input type="hidden" name="next" value=(next)>
462 <input type="hidden" name="other" value=(other_id.as_str())>
463 <input type="hidden" name="direction" value="blocked_by">
464 <button class="button ghost" type="submit">(t(lang, K::RemoveLink))</button>
465 </form>
466 }
467 </li>
468 }
469 </ul>
470 }
471 if !blocks.is_empty() {
472 <h4>(t(lang, K::Blocks))</h4>
473 <ul class="task-list">
474 for (other_id, link, label) in blocks.iter() {
475 <li class="task-row">
476 <a href=(link.as_str())>(label.as_str())</a>
477 if modal.can_write {
478 <form method="post" action=(remove_action.as_str())>
479 <input type="hidden" name="csrf" value=(csrf)>
480 <input type="hidden" name="next" value=(next)>
481 <input type="hidden" name="other" value=(other_id.as_str())>
482 <input type="hidden" name="direction" value="blocks">
483 <button class="button ghost" type="submit">(t(lang, K::RemoveLink))</button>
484 </form>
485 }
486 </li>
487 }
488 </ul>
489 }
490 if modal.can_write && !options.is_empty() {
491 <form class="task-inline" method="post" action=(add_action.as_str())>
492 <input type="hidden" name="csrf" value=(csrf)>
493 <input type="hidden" name="next" value=(next)>
494 <div class="field">
495 <label for="link-other">(t(lang, K::LinkWith))</label>
496 <select id="link-other" name="other">
497 for (other_id, label) in options.iter() {
498 <option value=(other_id.as_str())>(label.as_str())</option>
499 }
500 </select>
501 </div>
502 <div class="field">
503 <label for="link-direction">(t(lang, K::LinkDirection))</label>
504 <select id="link-direction" name="direction">
505 <option value="blocked_by">(t(lang, K::DirBlockedBy))</option>
506 <option value="blocks">(t(lang, K::DirBlocks))</option>
507 </select>
508 </div>
509 <button class="button" type="submit">(t(lang, K::AddLink))</button>
510 </form>
511 }
512 </section>
513 })
514}
515
516/// The comments, oldest first, with edit and delete for the ones the viewer may change, and the composer.
517#[component]
518pub async fn task_comments(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
519 let lang = page.lang;
520 let id = modal.detail.task.id.as_str();
521 let csrf = page.viewer.csrf.as_str();
522 let next = modal.base.as_str();
523 let comment_action = format!("/tasks/{id}/comments");
524 let actions: Vec<(String, String)> = modal
525 .comments
526 .iter()
527 .map(|c| {
528 (
529 format!("/comments/{}/edit", c.id),
530 format!("/comments/{}/delete", c.id),
531 )
532 })
533 .collect();
534 let limit = iz_core::limits::COMMENT_MAX.to_string();
535 Ok(view! {
536 <section class="task-section" aria-labelledby="task-comments">
537 <h3 id="task-comments">(t(lang, K::Comments))</h3>
538 if modal.comments.is_empty() {
539 <p class="muted">(t(lang, K::NoComments))</p>
540 }
541 <ol class="comment-list">
542 for (c, (edit_action, delete_action)) in modal.comments.iter().zip(actions.iter()) {
543 <li class="comment">
544 <p class="comment-meta">
545 <strong>(c.author.as_str())</strong>
546 " "
547 <time>(c.when.as_str())</time>
548 if c.edited {
549 " "
550 <span>(t(lang, K::Edited))</span>
551 }
552 </p>
553 rich_text(text: c.body.as_str())
554 if c.can_edit {
555 <details class="comment-edit">
556 <summary>(t(lang, K::EditComment))</summary>
557 <form class="task-form" method="post" action=(edit_action.as_str())>
558 <input type="hidden" name="csrf" value=(csrf)>
559 <input type="hidden" name="next" value=(next)>
560 <textarea name="body" maxlength=(limit.as_str()) required="">(c.body.as_str())</textarea>
561 <button class="button" type="submit">(t(lang, K::SaveComment))</button>
562 </form>
563 </details>
564 }
565 if c.can_delete {
566 <form method="post" action=(delete_action.as_str())>
567 <input type="hidden" name="csrf" value=(csrf)>
568 <input type="hidden" name="next" value=(next)>
569 <button class="button ghost danger" type="submit">(t(lang, K::DeleteComment))</button>
570 </form>
571 }
572 </li>
573 }
574 </ol>
575 if modal.can_write {
576 <form class="task-form" method="post" action=(comment_action.as_str())>
577 <input type="hidden" name="csrf" value=(csrf)>
578 <input type="hidden" name="next" value=(next)>
579 <div class="field field-wide">
580 <label for="comment-body">(t(lang, K::CommentBody))</label>
581 <textarea id="comment-body" name="body" maxlength=(limit.as_str()) required="">(modal.comment_draft.as_str())</textarea>
582 </div>
583 <button class="button primary" type="submit">(t(lang, K::PostComment))</button>
584 </form>
585 }
586 </section>
587 })
588}
589
590/// The activity feed, newest first, with the link to the next older page.
591#[component]
592pub async fn task_activity(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
593 let lang = page.lang;
594 Ok(view! {
595 <section class="task-section" aria-labelledby="task-activity">
596 <h3 id="task-activity">(t(lang, K::Activity))</h3>
597 if modal.activity.is_empty() {
598 <p class="muted">(t(lang, K::NoActivity))</p>
599 }
600 <ul class="activity-list">
601 for row in modal.activity.iter() {
602 <li>
603 <time>(row.when.as_str())</time>
604 " "
605 <span>(row.actor.as_str())</span>
606 " "
607 (row.sentence)
608 </li>
609 }
610 </ul>
611 if let Some(older) = modal.older.as_ref() {
612 <p><a class="button ghost" href=(older.as_str())>(t(lang, K::Older))</a></p>
613 }
614 </section>
615 })
616}
617
618/// Deleting the task. The first step is a link to `?confirm=delete`; the confirmation form posts the
619/// delete and returns to the board, without the task.
620#[component]
621pub async fn task_delete(page: &BoardPage, modal: &TaskModal) -> Result<impl View> {
622 let lang = page.lang;
623 let id = modal.detail.task.id.as_str();
624 let csrf = page.viewer.csrf.as_str();
625 let delete_action = format!("/tasks/{id}/delete");
626 let confirm_url = format!("{}&confirm=delete", modal.base);
627 let close = modal.close.as_str();
628 Ok(view! {
629 if modal.can_write {
630 <section class="task-section">
631 if modal.confirm_delete {
632 <div class="card task-confirm">
633 <h3>(t(lang, K::DeleteQuestion))</h3>
634 <p>(t(lang, K::DeleteConsequence))</p>
635 <form class="task-inline" method="post" action=(delete_action.as_str())>
636 <input type="hidden" name="csrf" value=(csrf)>
637 <input type="hidden" name="next" value=(close)>
638 <button class="button danger" type="submit">(t(lang, K::DeleteYes))</button>
639 <a class="button ghost" href=(modal.base.as_str())>(t(lang, K::Close))</a>
640 </form>
641 </div>
642 } else {
643 <a class="button ghost danger" href=(confirm_url.as_str())>(t(lang, K::DeleteTask))</a>
644 }
645 </section>
646 }
647 })
648}