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