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