html: Add search hints.
[software/mumi.git] / mumi / web / view / html.scm
1 ;;; mumi -- Mediocre, uh, mail interface
2 ;;; Copyright © 2016, 2017, 2018, 2019, 2020 Ricardo Wurmus <rekado@elephly.net>
3 ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
4 ;;;
5 ;;; This program is free software: you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Affero General Public License
7 ;;; as published by the Free Software Foundation, either version 3 of
8 ;;; the License, or (at your option) any later version.
9 ;;;
10 ;;; This program is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Affero General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Affero General Public
16 ;;; License along with this program. If not, see
17 ;;; <http://www.gnu.org/licenses/>.
18
19 (define-module (mumi web view html)
20 #:use-module (debbugs bug)
21 #:use-module (email email)
22 #:use-module (mumi config)
23 #:use-module (mumi messages)
24 #:use-module (mumi web view utils)
25 #:use-module (mumi web util)
26 #:use-module (ice-9 match)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-19)
29 #:export (index
30 help
31 unknown
32 error-page
33 issue-page
34 list-of-matching-bugs
35 list-of-recent-issues))
36
37 (define (bug-subject* bug)
38 (or (bug-subject bug) "(no subject)"))
39
40 (define (status-tag bug)
41 "Return a colored tag indicating the BUG status."
42 (let ((status (if (bug-done bug) "Done" "Open")))
43 `(span (@ (class ,(string-append "status-tag "
44 (string-downcase status))))
45 ,status)))
46
47 (define* (layout #:key
48 (head '())
49 (body '())
50 (title "Guix issue tracker"))
51 `((doctype "html")
52 (html
53 (head
54 (title ,title)
55 (meta (@ (http-equiv "Content-Type") (content "text/html; charset=UTF-8")))
56 (meta (@ (http-equiv "Content-Language") (content "en")))
57 (meta (@ (name "author") (content "Ricardo Wurmus")))
58 (meta (@ (name "viewport")
59 (content "width=device-width, initial-scale=1")))
60 (link
61 (@ (rel "stylesheet")
62 (media "screen")
63 (type "text/css")
64 (href "/css/reset.css")))
65 (link
66 (@ (rel "stylesheet")
67 (media "screen")
68 (type "text/css")
69 (href "/css/bootstrap.css")))
70 ,@head
71 (link
72 (@ (rel "stylesheet")
73 (media "screen")
74 (type "text/css")
75 (href "/css/screen.css")))
76 (link
77 (@ (rel "stylesheet")
78 (media "screen")
79 (type "text/css")
80 (href "/css/code.css"))))
81 (body ,@body
82 (footer (@ (class "text-center"))
83 (p "Copyright © 2016—2020 by the GNU Guix community."
84 (br)
85 "Now with even more " (span (@ (class "lambda")) "λ") "! ")
86 (p "This is free software. Download the "
87 (a (@ (href "https://git.elephly.net/software/mumi.git"))
88 "source code here") ".")))
89 (script
90 (@ (type "text/javascript")
91 (src "/js/sort-table.js"))))))
92
93 (define* (search-form #:key (standalone? #f) (text ""))
94 `(form (@ (id "search")
95 ,(if standalone?
96 '(class "")
97 '(class "form-inline"))
98 (action "/search"))
99 (div
100 (@ (class ,(if standalone?
101 "input-group input-group-lg"
102 "input-group")))
103 (input (@ (type "text")
104 (class "form-control")
105 (id "query")
106 (name "query")
107 (value ,text)
108 (placeholder "Input your search query...")))
109 (span (@ (class "input-group-append"))
110 (button
111 (@ (type "submit")
112 (class ,(string-append (if standalone? "btn-lg " "")
113 "btn btn-primary")))
114 ;; This SVG is part of Bootstrap and is available
115 ;; under the Expat license.
116 (svg
117 (@ (class "bi bi-search")
118 (width "1em")
119 (height "1em")
120 (viewBox "0 0 16 16")
121 (fill "currentColor")
122 (xmlns "http://www.w3.org/2000/svg"))
123 (title "Search")
124 (path (@ (fill-rule "evenodd")
125 (d "M10.442 10.442a1 1 0 011.415 0l3.85 3.85a1 1 0 01-1.414 1.415l-3.85-3.85a1 1 0 010-1.415z")
126 (clip-rule "evenodd")) "")
127 (path (@ (fill-rule "evenodd")
128 (d "M6.5 12a5.5 5.5 0 100-11 5.5 5.5 0 000 11zM13 6.5a6.5 6.5 0 11-13 0 6.5 6.5 0 0113 0z")
129 (clip-rule "evenodd")) ""))))
130 (div
131 (@ (id "search-hints"))
132 (p "You can improve the search results by making use of the
133 simple query language. Here is a list of supported query terms:")
134 (table
135 (@ (class "table table-sm table-borderless"))
136 (tbody
137 ,@(map (match-lambda
138 ((term description)
139 `(tr (td (span (@ (class "filter")) ,term))
140 (td ,description))))
141 '(("is:open" "open issues")
142 ("is:done" "closed issues")
143 ("submitter:<who>" "search issue submitter")
144 ("author:<who>" "search by message author")
145 ("date:yesterday..now" "search by issue date")
146 ("mdate:3m..2d" "search by message date")))))))))
147
148 (define* (search-widget #:key (text ""))
149 `(div
150 (@ (class "card mb-3"))
151 (div (@ (class "card-body bg-light"))
152 ,(search-form #:standalone? #t #:text text)
153 (details
154 (summary (strong "Hint"))
155 (p
156 "Refine your search with filters like "
157 (span (@ (class "filter"))
158 "is:open")
159 ", "
160 (span (@ (class "filter"))
161 "author:me")
162 ", "
163 (span (@ (class "filter"))
164 "submitter:me")
165 ", "
166 (span (@ (class "filter"))
167 "date:2d..now")
168 ", "
169 (span (@ (class "filter"))
170 "date:2018-04-01..2018-04-02")
171 ", "
172 (span (@ (class "filter"))
173 "date:1m..today")
174 ", and "
175 (a (@ (href "help#search"))
176 "many more!"))))))
177
178 (define* (header #:key (search-bar? #t))
179 `(nav
180 (@ (class "navbar navbar-expand navbar-light bg-light"))
181 (a (@ (href "/") (class "navbar-brand pt-0 logo navbar-collapse"))
182 (img (@ (src "/img/logo.png") (alt "logo") (height "25"))))
183 ,@(if search-bar?
184 `(,(search-form))
185 '())))
186
187 (define (index)
188 (layout
189 #:body
190 `(,(header #:search-bar? #f)
191 (main
192 (@ (class "container"))
193 (h1 "Guix issue tracker")
194 (p (@ (class "lead"))
195 "This is a web frontend to the Guix patch and bug trackers. Send email to "
196 (a (@ (href ,(string-append "mailto:" (%config 'submission-email-address))))
197 ,(%config 'submission-email-address))
198 " to submit a patch, or email "
199 (a (@ (href ,(string-append "mailto:" (%config 'submission-bug-email-address))))
200 ,(%config 'submission-bug-email-address))
201 " to submit a bug report.")
202 ,(search-widget)
203
204 (div (@ (class "mt-4 h4")) "Recent activity "
205 (small (a (@ (href "recent")) "(More)")))
206 (table
207 (@ (class "table table-borderless table-hover js-sort-table"))
208 (thead
209 (tr (@ (class "heading"))
210 (th (@ (class "js-sort-number")) "ID")
211 (th "Subject")
212 (th "Date submitted")
213 (th "Status")))
214 (tbody
215 ,@(list-of-bugs (recent-bugs 10))))
216
217 (div (@ (class "mt-4 h4")) "Priority bugs")
218 (table
219 (@ (class "table table-borderless table-hover js-sort-table"))
220 (thead
221 (tr (@ (class "heading"))
222 (th (@ (class "js-sort-number")) "ID")
223 (th "Subject")
224 (th "Date submitted")
225 (th "Status")))
226 (tbody
227 ,@(priority-bugs)))))))
228
229 (define (help)
230 (layout
231 #:body
232 `(,(header #:search-bar? #f)
233 (div
234 (@ (class "container"))
235 (h1 "Help")
236 (a (@ (href "search")))
237 (p "You can improve the search results by making use of the
238 simple query language. Here is a list of supported query terms with
239 some examples.")
240 (table
241 (@ (class "table"))
242 (thead
243 (tr
244 (th (@ (class "col-md-3")) "Filter")
245 (th (@ (class "col-md-9")) "Description")))
246 (tbody
247 (tr
248 (td (span (@ (class "filter")) "is:open") ", "
249 (span (@ (class "filter")) "is:pending"))
250 (td "Open issues."))
251 (tr
252 (td (span (@ (class "filter")) "is:closed") ", "
253 (span (@ (class "filter")) "is:done"))
254 (td "Issues marked as done."))
255 (tr
256 (td (span (@ (class "filter")) "submitter:<who>"))
257 (td "Issues submitted by a person named " (strong "who")
258 ", e.g. "
259 (span (@ (class "filter")) "submitter:ludo")
260 " for all issues submitted by ludo. "
261 "The filter matches both the email address and the name."))
262 (tr
263 (td (span (@ (class "filter")) "author:<who>"))
264 (td "Issues where a person named " (strong "who")
265 " has commented, e.g. "
266 (span (@ (class "filter")) "author:rekado")
267 " for all messages where rekado has commented. "
268 "The filter matches both the email address and the name."))
269 (tr
270 (td (span (@ (class "filter")) "date:<start>..<end>"))
271 (td "Issues submitted within the provided range. "
272 (strong "start") " and " (strong "end") " can be one of "
273 (strong "now") ", " (strong "today")
274 ", a date in the formats "
275 (strong "YYYY-MM-DD") " or " (strong "YYYYMMDD")
276 ", or an amount and a unit for a point in the past, such as "
277 (strong "12d..") " for 12 days ago. "
278 "Supported units are: "
279 (strong "h") " (hours), "
280 (strong "d") " (days), "
281 (strong "w") " (weeks), "
282 (strong "m") " (months), and "
283 (strong "y") " (years)."))
284 (tr
285 (td (span (@ (class "filter")) "mdate:<start>..<end>"))
286 (td "This is just like " (span (@ (class "filter")) "date")
287 " except that it also includes comments."))))))))
288
289 (define (unknown id)
290 (layout
291 #:body
292 `(,(header)
293 (div (@ (class "container"))
294 (h1 "Issue not found")
295 (p "There is no submission with id " (strong ,id) ", or we
296 failed to process associated messages.")
297 (p (a (@ (href "/")) "Try another one?"))))))
298
299 (define (error-page message)
300 (layout
301 #:body
302 `(,(header)
303 (div (@ (class "container"))
304 (h1 "Error")
305 (p "An error occurred. Sorry about that!")
306 ,message
307 (p (a (@ (href "/")) "Try something else?"))))))
308
309 (define* (issue-page bug #:optional flash-message)
310 "Render the conversation for the given BUG."
311 (define id (bug-num bug))
312 (define messages (issue-messages id))
313 (define parties (sort (filter (compose (negate bot?) extract-email)
314 (participants (filter identity messages)))
315 (lambda (a b)
316 (string< (extract-email a)
317 (extract-email b)))))
318 (define (show-message message-number message previous-subject)
319 `((div (@ (class "mb-5"))
320 (a (@ (id ,(number->string message-number))))
321 (div
322 (@ (class "avatar")
323 (style ,(string-append "background-color:"
324 (avatar-color (sender-email message)
325 (map extract-email parties)))))
326 ,(string-upcase (string-take (sender-name message) 1)))
327 (div
328 (@ (class "message"))
329 (div
330 (@ (class "card"))
331 (div
332 (@ (class "card-header"))
333 (div
334 (@ (class "from"))
335 (div
336 (@ (class "avatar")
337 (style ,(string-append "background-color:"
338 (avatar-color (sender-email message)
339 (map extract-email parties)))))
340 ,(string-upcase (string-take (sender-name message) 1)))
341 (span (@ (class "address")) ,(sender-name message))
342 " wrote on "
343 (span (@ (class "date"))
344 (a (@ (href ,(string-append "#" (number->string
345 message-number))))
346 ,(date->string (date message)))))
347 ,@(if (string-suffix? previous-subject (subject message))
348 '()
349 `((div (@ (class "subject")) ,(subject message))))
350 (div
351 (@ (class "details"))
352 (div
353 (@ (class "recipients"))
354 (label "Recipients:")
355 ,(map (lambda (address)
356 `(span (@ (class "address")) ,address))
357 (recipients message)))
358 (div
359 (@ (class "message-id"))
360 (label "Message-ID:")
361 ,(message-id message))))
362 (div
363 (@ (class "body card-body"))
364 ,(display-message-body id message-number message)))))
365 ,@(if (closing? message id)
366 '((div
367 (@ (class "row event"))
368 (div
369 (@ (class "col-11 offset-1 text-center"))
370 (div (@ (class "badge badge-primary closed")) "Closed"))))
371 '())))
372 (if (null? messages) #f
373 (layout
374 #:title (bug-subject* bug)
375 #:body
376 `(,(header)
377 (div
378 (@ (class "container"))
379 ,@(match flash-message
380 (('error . text)
381 `((div (@ (class "alert alert-danger")
382 (role "alert"))
383 ,text)))
384 (('info . text)
385 `((div (@ (class "alert alert-info")
386 (role "alert"))
387 ,text)))
388 (_ '()))
389 (div
390 (@ (class "title col-12"))
391 (h1 ,(bug-subject* bug))
392 (span (@ (class "details"))
393 ,(status-tag bug)
394 ,(string-append "Submitted by "
395 ;; We don't use bug-originator here
396 ;; because it includes the email address.
397 ;; We cannot use extract-name on the
398 ;; return value, because it swallows
399 ;; non-ASCII characters.
400 (sender-name (first messages))
401 ".")
402 ,@(if (bug-mergedwith bug)
403 `((ul (@ (class "merged"))
404 "Merged with: "
405 ,(map (lambda (id)
406 `(li (a (@ (href ,(string-append "/issue/" id)))
407 ,id)))
408 ;; XXX: This field can either hold a
409 ;; string of multiple ids, or a single
410 ;; number. Deal with this mess.
411 (match (bug-mergedwith bug)
412 ((? string? str)
413 (string-split str #\space))
414 ((? number? n)
415 (list (number->string n)))))))
416 '())
417 ,@(if (bug-blocks bug)
418 `((ul (@ (class "blocks"))
419 "Blocks: "
420 ,(map (lambda (id)
421 `(li (a (@ (href ,(string-append "/issue/" id)))
422 ,id)))
423 ;; XXX: This field can either hold a
424 ;; string of multiple ids, or a single
425 ;; number. Deal with this mess.
426 (match (bug-blocks bug)
427 ((? string? str)
428 (string-split str #\space))
429 ((? number? n)
430 (list (number->string n)))))))
431 '())
432 ,@(if (bug-blockedby bug)
433 `((ul (@ (class "blockedby"))
434 "Blocked by: "
435 ,(map (lambda (id)
436 `(li (a (@ (href ,(string-append "/issue/" id)))
437 ,id)))
438 ;; XXX: This field can either hold a
439 ;; string of multiple ids, or a single
440 ;; number. Deal with this mess.
441 (match (bug-blockedby bug)
442 ((? string? str)
443 (string-split str #\space))
444 ((? number? n)
445 (list (number->string n)))))))
446 '())))
447 (div (@ (class "row"))
448 (div
449 (@ (class "conversation col-lg-9"))
450 ,(let ((msgs (filter (lambda (msg)
451 ;; Ignore messages
452 ;; without body, and
453 ;; internal messages.
454 (and msg
455 (email-body msg)
456 (not (internal-message? msg))))
457 messages)))
458 (map (lambda (message-number msg previous-subject)
459 (show-message message-number msg previous-subject))
460 (iota (length msgs))
461 msgs
462 (cons (bug-subject* bug)
463 (map subject msgs))))
464 (div
465 (@ (class "comment-box"))
466 (a (@ (id "comment")))
467 (div
468 (@ (class "avatar")
469 (style "background-color:#bc80bd")) "?")
470 (div
471 (@ (class "message"))
472 (div
473 (@ (class "card"))
474 ,@(if (mailer-enabled?)
475 `((form
476 (@ (action ,(format #f "/issue/~a/comment"
477 (number->string id)))
478 (enctype "multipart/form-data")
479 (method "POST"))
480 (input (@ (style "display:none")
481 (name "validation")
482 (placeholder "Please leave this empty.")) "")
483 (input (@ (type "hidden")
484 (name "timestamp")
485 (value ,(timestamp!))) "")
486 (div
487 (@ (class "card-header"))
488 (div (@ (class "from"))
489 (input (@ (class "address form-control")
490 (name "from")
491 (required "required")
492 (placeholder "Your name")) "")))
493 (div
494 (@ (class "body card-body"))
495 (textarea (@ (name "text")
496 (required "required")
497 (class "form-control")
498 (placeholder "Please input your comment..."))
499 ""))
500 (div
501 (@ (class "card-footer"))
502 (button (@ (class "btn btn-primary")
503 (type "submit"))
504 "Send"))))
505 `((div
506 (@ (class "card-header"))
507 (div (@ (class "from"))
508 (span (@ (class "address")) "Your comment")))
509 (div
510 (@ (class "body card-body"))
511 (p "Comments via the web interface are not currently
512 supported. To comment on this conversation "
513 (a (@ (href ,(string-append "mailto:"
514 (number->string id) "@" (%config 'debbugs-domain)
515 "?subject=" (bug-subject* bug))))
516 ,(string-append "send email to "
517 (number->string id) "@" (%config 'debbugs-domain)))))))))))
518
519 (div
520 (@ (class "info col-lg-3"))
521 (dl
522 (@ (class "stat"))
523 ,@(let ((num (length parties)))
524 `((dt ,(if (= num 1)
525 "One participant"
526 (string-append (number->string num)
527 " participants")))
528 (dd
529 (ul ,(map (lambda (name)
530 `(li (span (@ (class "name")))
531 ,name))
532 (map extract-name parties)))))))
533 (dl
534 (@ (class "stat"))
535 (dt "Owner")
536 (dd
537 ,(or (and=> (bug-owner bug) extract-name) "unassigned")))
538 (dl
539 (@ (class "stat"))
540 (dt "Severity")
541 (dd ,(bug-severity bug)))
542 (dl
543 (@ (class "stat"))
544 (dt "Status")
545 (dd ,(status-tag bug))))))))))
546
547 (define (list-of-bugs bugs)
548 "Return table rows for all BUGS."
549 (map (lambda (bug)
550 (let ((id (number->string (bug-num bug))))
551 `(tr (@ (class ,(bug-severity bug)))
552 (td ,(or id "-"))
553 (td
554 ,@(if (member (bug-severity bug) '("serious" "important"))
555 `((svg (@ (xmlns"http://www.w3.org/2000/svg")
556 (xmlns:xlink "http://www.w3.org/1999/xlink")
557 (viewBox "0 0 14 16")
558 (version "1.1")
559 (height "1rem")
560 (width "1rem")
561 (aria-hidden "true"))
562 (title ,(bug-severity bug))
563 (path (@ (fill-rule "evenodd")
564 (d "\
565 M7 2.3c3.14 0 5.7 2.56 5.7 5.7\
566 s-2.56 5.7-5.7 5.7A5.71 5.71 0 011.3 8\
567 c0-3.14 2.56-5.7 5.7-5.7z\
568 M7 1C3.14 1 0 4.14 0 8\
569 s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7z\
570 m1 3H6v5h2V4zm0 6H6v2h2v-2z")))))
571 '())
572 ,(if id
573 `(a (@ (href ,(string-append "/issue/" id)))
574 ,(bug-subject* bug))
575 (bug-subject* bug)))
576 (td ,(date->string (bug-date bug)))
577 (td ,(status-tag bug)))))
578 bugs))
579
580 (define (priority-bugs)
581 (list-of-bugs (sort
582 (append
583 (bugs-by-severity "serious" "open")
584 (bugs-by-severity "important" "open"))
585 (lambda (a b) (< (bug-num a) (bug-num b))))))
586
587 (define* (list-of-recent-issues #:optional (max 100))
588 (layout
589 #:body
590 `(,(header #:search-bar? #f)
591 (div
592 (@ (class "container"))
593 (h1 "Recent issues")
594 (table
595 (@ (class "table table-borderless table-hover js-sort-table"))
596 (thead
597 (tr (@ (class "heading"))
598 (th (@ (class "js-sort-number")) "ID")
599 (th "Subject")
600 (th "Date submitted")
601 (th "Status")))
602 (tbody
603 ,@(list-of-bugs (recent-bugs max))))))))
604
605 (define (list-of-matching-bugs query bugs)
606 (layout
607 #:body
608 `(,(header #:search-bar? #f)
609 (div
610 (@ (class "container"))
611 (h1 "Your search for " (code ,query))
612 ,(search-widget #:text query)
613 ,(if (null? bugs)
614 `(div
615 (@ (class "alert alert-warning")
616 (role "alert"))
617 (h4
618 (@ (class "alert-heading"))
619 "Nothing found!")
620 (p "We could not find any issues matching your query "
621 (code ,query) ". "
622 (a (@ (href "/"))
623 "Try searching for something else?")))
624 `(table
625 (@ (class "table table-borderless table-hover js-sort-table"))
626 (thead
627 (tr (@ (class "heading"))
628 (th (@ (class "js-sort-number")) "ID")
629 (th "Subject")
630 (th "Date submitted")
631 (th "Status")))
632 (tbody
633 ,@(list-of-bugs bugs))))))))