-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.go
705 lines (645 loc) · 19.2 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
package main
import (
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"math"
"net/http"
"strconv"
"time"
"unicode/utf8"
"dmitri.shuralyov.com/html/belt"
statepkg "dmitri.shuralyov.com/state"
"github.com/dustin/go-humanize"
"github.com/shurcooL/component"
"github.com/shurcooL/events"
"github.com/shurcooL/events/event"
"github.com/shurcooL/go/timeutil"
homecomponent "github.com/shurcooL/home/component"
"github.com/shurcooL/home/httputil"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/htmlg"
"github.com/shurcooL/octicon"
"github.com/shurcooL/users"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var indexHTML = template.Must(template.New("").Parse(`<!DOCTYPE html>
<html lang="en">
<head>
{{.AnalyticsHTML}} <title>Dmitri Shuralyov</title>
<link href="/icon.svg" rel="icon" type="image/svg+xml">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/index/style.css" rel="stylesheet" type="text/css">
<link href="https://github.com/{{.GitHubRelMe}}" rel="me">
</head>
<body>
<div style="max-width: 800px; margin: 0 auto 100px auto;">`))
func initIndex(events events.Service, notification notification.Service, users users.Service) http.Handler {
h := &indexHandler{
AuthzEndpoint: indieauthMeFlag.Me != nil,
events: events,
notification: notification,
users: users,
}
return cookieAuth{httputil.ErrorHandler(users, h.ServeHTTP)}
}
type indexHandler struct {
AuthzEndpoint bool // Whether to advertise the IndieAuth authorization endpoint.
events events.Service
notification notification.Service
users users.Service
}
func (h *indexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) error {
if err := httputil.AllowMethods(req, http.MethodGet, http.MethodHead); err != nil {
return err
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if h.AuthzEndpoint {
// Advertise the IndieAuth authorization endpoint.
w.Header().Set("Link", `</api/indieauth/authorization>; rel="authorization_endpoint"`)
}
if req.Method == http.MethodHead {
return nil
}
data := struct {
AnalyticsHTML template.HTML
GitHubRelMe string // GitHub username to advertise in a rel='me' link.
}{analyticsHTML, *githubRelMeFlag}
err := indexHTML.Execute(w, data)
if err != nil {
return err
}
authenticatedUser, err := h.users.GetAuthenticated(req.Context())
if err != nil {
return err
}
var nc uint64
if authenticatedUser.ID != 0 {
nc, err = h.notification.CountNotifications(req.Context())
if err != nil {
return err
}
}
returnURL := req.RequestURI
// Render the header.
header := homecomponent.Header{
CurrentUser: authenticatedUser,
NotificationCount: nc,
ReturnURL: returnURL,
}
err = htmlg.RenderComponents(w, header)
if err != nil {
return err
}
err = html.Render(w, htmlg.H3(htmlg.Text("Activity")))
if err != nil {
return err
}
events, eventsError := h.events.List(req.Context())
var error string
if eventsError != nil {
error = "There was a problem getting latest activity."
if authenticatedUser.SiteAdmin {
error += "\n\n" + eventsError.Error()
}
}
activity := activity{
Events: events,
Error: error,
ShowWIP: req.URL.Query().Get("events") == "all" || authenticatedUser.UserSpec == dmitshur,
}
activity.ShowRaw, _ = strconv.ParseBool(req.URL.Query().Get("raw"))
err = htmlg.RenderComponents(w, activity)
if err != nil {
return err
}
_, err = io.WriteString(w, `</div>`)
if err != nil {
return err
}
_, err = io.WriteString(w, `</body></html>`)
return err
}
type activity struct {
Events []event.Event
Error string
ShowWIP bool // Controls whether all events are displayed, including WIP ones.
ShowRaw bool // Controls whether full raw payload are available as titles.
}
func (a activity) Render() []*html.Node {
var nodes []*html.Node
if a.Error != "" {
nodes = append(nodes,
&html.Node{
Type: html.ElementNode, Data: atom.P.String(),
Attr: []html.Attribute{{Key: atom.Style.String(), Val: "white-space: pre;"}},
FirstChild: htmlg.Text(a.Error),
},
)
}
if len(a.Events) == 0 {
nodes = append(nodes,
htmlg.Text("No recent activity."),
)
return []*html.Node{htmlg.DivClass("activity", nodes...)}
}
var (
now = time.Now()
headings = []struct {
Text string
End time.Time
}{
{Text: "Today", End: timeutil.StartOfDay(now).Add(24 * time.Hour)},
{Text: "Yesterday", End: timeutil.StartOfDay(now)},
{Text: "This Week", End: timeutil.StartOfDay(now).Add(-24 * time.Hour)},
{Text: "Last Week", End: timeutil.StartOfWeek(now)},
{Text: "Earlier", End: timeutil.StartOfWeek(now).Add(-7 * 24 * time.Hour)},
}
)
for _, e := range a.Events {
// Heading.
if len(headings) > 0 && headings[0].End.After(e.Time) {
for len(headings) >= 2 && headings[1].End.After(e.Time) {
headings = headings[1:]
}
nodes = append(nodes,
htmlg.DivClass("events-heading", htmlg.Text(headings[0].Text)),
)
headings = headings[1:]
}
// Event.
basicEvent := basicEvent{
Time: e.Time,
Actor: e.Actor.Login,
}
if a.ShowRaw {
// For debugging, include full raw payload as a title.
raw, err := json.MarshalIndent(e.Payload, "", "\t")
if err != nil {
panic(err)
}
basicEvent.Raw = string(raw)
}
var displayEvent htmlg.Component
switch p := e.Payload.(type) {
case event.Issue:
ae := activityEvent{
basicEvent: &basicEvent,
Icon: octicon.IssueOpened,
Action: component.Join(p.Action, " an issue ", issueFromAction(p, e.Container)),
}
if p.Action == "reopened" {
ae.Icon = octicon.IssueReopened
} else if p.Action == "opened" && p.IssueBody != "" {
ae.Details = imageText{
ImageURL: e.Actor.AvatarURL,
Text: shortBody(p.IssueBody),
}
}
displayEvent = ae
case event.Change:
ae := activityEvent{
basicEvent: &basicEvent,
Icon: octicon.GitPullRequest,
Action: component.Join(p.Action, " a change ", changeFromAction(p, e.Container)),
}
if p.Action == "opened" && p.ChangeBody != "" {
ae.Details = imageText{
ImageURL: e.Actor.AvatarURL,
Text: shortBody(p.ChangeBody),
}
}
displayEvent = ae
case event.IssueComment:
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.CommentDiscussion,
Action: component.Join("commented on ", issueFromComment(p, e.Container)),
Details: imageText{
ImageURL: e.Actor.AvatarURL,
Text: shortBody(p.CommentBody),
},
}
case event.ChangeComment:
var verb string
switch p.CommentReview {
case 0:
verb = "commented"
default:
verb = fmt.Sprintf("reviewed %+d", p.CommentReview)
}
var details htmlg.Component
if p.CommentBody != "" {
details = imageText{
ImageURL: e.Actor.AvatarURL,
Text: shortBody(p.CommentBody),
}
}
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.CommentDiscussion,
Action: component.Join(verb, " on ", changeFromComment(p, e.Container)),
Details: details,
}
case event.CommitComment:
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.CommentDiscussion,
Action: component.Join("commented on ", commitName(p, e.Container)),
Details: imageText{
ImageURL: e.Actor.AvatarURL,
Text: shortBody(p.CommentBody),
},
}
case event.Push:
basicEvent.Container = e.Container
ae := activityEvent{
basicEvent: &basicEvent,
Icon: octicon.GitCommit,
Action: component.Join("pushed to ", belt.Reference{Name: p.Branch}),
}
switch len(p.Commits) {
default:
ae.Details = commits{
Commits: p.Commits,
}
case 0:
before := belt.CommitID{SHA: p.Before, HTMLURL: p.BeforeHTMLURL}
head := belt.CommitID{SHA: p.Head, HTMLURL: p.HeadHTMLURL}
ae.Details = component.Join(before, " → ", head)
}
displayEvent = ae
case event.Star:
basicEvent.Container = e.Container
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.Star,
Action: component.Text("starred"),
}
case event.Create:
basicEvent.Container = e.Container
ae := activityEvent{
basicEvent: &basicEvent,
}
switch p.Type {
case "repository":
ae.Icon = octicon.Repo
ae.Action = component.Text("created repository")
if p.Description != "" {
ae.Details = plainText{Text: p.Description}
}
case "package":
ae.Icon = octicon.Package
ae.Action = component.Text("created package")
if p.Description != "" {
ae.Details = plainText{Text: p.Description}
}
case "branch":
ae.Icon = octicon.GitBranch
ae.Action = component.Text("created branch")
ae.Details = belt.Reference{Name: p.Name}
case "tag":
ae.Icon = octicon.Tag
ae.Action = component.Text("created tag")
ae.Details = belt.Reference{Name: p.Name}
//default:
//basicEvent.WIP = true
//e.Action = component.Text(fmt.Sprintf("created %v", *p.RefType))
//e.Details = code{
// Text: p.Name,
//}
}
displayEvent = ae
case event.Fork:
basicEvent.Container = e.Container
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.RepoForked,
Action: component.Text("forked"),
Details: iconLink{
Text: p.Container,
URL: "https://" + p.Container,
Black: true,
Icon: octicon.Repo,
IconColor: &RGB{R: 35, G: 35, B: 35}, // Black (not pure).
},
}
case event.Delete:
basicEvent.Container = e.Container
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.Trashcan,
Action: component.Text(fmt.Sprintf("deleted %v", p.Type)),
Details: belt.Reference{
Name: p.Name,
Strikethrough: true,
},
}
case event.Wiki:
basicEvent.Container = e.Container
displayEvent = activityEvent{
basicEvent: &basicEvent,
Icon: octicon.Book,
Action: component.Text("edited the wiki"),
Details: pages{
ActorAvatarURL: e.Actor.AvatarURL,
Pages: p.Pages,
},
}
default:
log.Printf("activity.Render: unexpected event type: %T\n", p)
continue
}
if basicEvent.WIP && !a.ShowWIP {
continue
}
nodes = append(nodes, displayEvent.Render()...)
}
return []*html.Node{htmlg.DivClass("activity", nodes...)}
}
func issueFromAction(p event.Issue, importPath string) htmlg.Component {
var is statepkg.Issue
switch p.Action {
case "opened", "reopened":
is = statepkg.IssueOpen
case "closed":
is = statepkg.IssueClosed
default:
log.Printf("issueFromAction: unsupported event.Issue action %q\n", p.Action)
is = statepkg.IssueOpen
}
return belt.Issue{
State: is,
Title: importPath + ": " + p.IssueTitle,
HTMLURL: p.IssueHTMLURL,
}
}
func issueFromComment(p event.IssueComment, importPath string) htmlg.Component {
return belt.Issue{
State: p.IssueState,
Title: importPath + ": " + p.IssueTitle,
HTMLURL: p.CommentHTMLURL,
}
}
func changeFromAction(p event.Change, importPath string) htmlg.Component {
var cs statepkg.Change
switch p.Action {
case "opened", "reopened":
cs = statepkg.ChangeOpen
case "closed":
cs = statepkg.ChangeClosed
case "merged":
cs = statepkg.ChangeMerged
default:
log.Printf("changeFromAction: unsupported event.Change action %q\n", p.Action)
cs = statepkg.ChangeOpen
}
return belt.Change{
State: cs,
Title: importPath + ": " + p.ChangeTitle,
HTMLURL: p.ChangeHTMLURL,
}
}
func changeFromComment(p event.ChangeComment, importPath string) htmlg.Component {
return belt.Change{
State: p.ChangeState,
Title: importPath + ": " + p.ChangeTitle,
HTMLURL: p.CommentHTMLURL,
}
}
func commitName(p event.CommitComment, importPath string) htmlg.Component {
c := p.Commit
if c.Message == "" {
a := htmlg.A(importPath, "https://"+importPath)
return component.Join("a commit in ", htmlg.NodeComponent(*a))
}
return belt.Commit{
SHA: c.SHA,
Message: importPath + ": " + c.Message,
AuthorAvatarURL: c.AuthorAvatarURL,
HTMLURL: c.HTMLURL,
}
}
type basicEvent struct {
Time time.Time
Actor string
Container string // URL of container without schema. E.g., "github.com/user/repo". Optional.
WIP bool // Whether this event's presentation is a work in progress.
Raw string // Raw event for debugging to display as title. Empty string excludes it.
}
// activityEvent is an event within the activity stream.
// Action must be not nil.
type activityEvent struct {
*basicEvent
Icon func() *html.Node
Action htmlg.Component // Not nil.
Details htmlg.Component
}
func (e activityEvent) Render() []*html.Node {
divClass := "event"
if e.WIP {
divClass += " wip"
}
if e.Icon == nil {
e.Icon = func() *html.Node { return &html.Node{Type: html.TextNode} }
}
action := &html.Node{Type: html.ElementNode, Data: atom.Span.String()}
if e.Raw != "" {
action.Attr = append(action.Attr, html.Attribute{Key: atom.Title.String(), Val: e.Raw})
}
htmlg.AppendChildren(action, e.Action.Render()...)
middle := []*html.Node{
htmlg.Text(e.Actor),
htmlg.Text(" "),
action,
}
if e.Container != "" {
middle = append(middle, htmlg.Text(" in "))
middle = append(middle, htmlg.A(e.Container, "https://"+e.Container))
}
div := htmlg.DivClass(divClass, htmlg.DivClass("overview",
htmlg.SpanClass("icon", e.Icon()),
htmlg.SpanClass("middle", middle...),
&html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{
{Key: atom.Class.String(), Val: "time"},
{Key: atom.Title.String(), Val: humanize.Time(e.Time) + " – " + e.Time.Local().Format(timeFormat)}, // TODO: Use local time of page viewer, not server.
},
FirstChild: htmlg.Text(compactTime(e.Time)),
},
))
if e.Details != nil {
div.AppendChild(htmlg.DivClass("details", e.Details.Render()...))
}
return []*html.Node{div}
}
const timeFormat = "Jan 2, 2006, 3:04 PM MST"
// compactTime formats time t into a relative string.
//
// For example, "5s" for 5 seconds ago, "47m" for 47 minutes ago,
// "3w" for 3 weeks ago, etc.
func compactTime(t time.Time) string {
return humanize.CustomRelTime(t, time.Now(), "", "", compactMagnitudes)
}
var compactMagnitudes = []humanize.RelTimeMagnitude{
{D: time.Minute, Format: "%ds", DivBy: time.Second},
{D: time.Hour, Format: "%dm", DivBy: time.Minute},
{D: humanize.Day, Format: "%dh", DivBy: time.Hour},
{D: humanize.Week, Format: "%dd", DivBy: humanize.Day},
{D: humanize.Month, Format: "%dw", DivBy: humanize.Week},
{D: humanize.Year, Format: "%dmo", DivBy: humanize.Month},
{D: math.MaxInt64, Format: "%dy", DivBy: humanize.Year},
}
// TODO: Dedup.
//
// RGB represents a 24-bit color without alpha channel.
type RGB struct {
R, G, B uint8
}
// HexString returns a hexadecimal color string. For example, "#ff0000" for red.
func (c RGB) HexString() string {
return fmt.Sprintf("#%02x%02x%02x", c.R, c.G, c.B)
}
// iconLink consists of an icon and a text link.
// Icon must be not nil.
type iconLink struct {
Text string
Tooltip string
URL string
Black bool // Black link.
Icon func() *html.Node // Not nil.
IconColor *RGB // Optional icon color override.
}
func (d iconLink) Render() []*html.Node {
a := &html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{{Key: atom.Href.String(), Val: d.URL}},
}
if d.Tooltip != "" {
a.Attr = append(a.Attr, html.Attribute{Key: atom.Title.String(), Val: d.Tooltip})
}
if d.Black {
a.Attr = append(a.Attr, html.Attribute{Key: atom.Class.String(), Val: "black"})
}
iconSpanStyle := "margin-right: 4px;"
if d.IconColor != nil {
iconSpanStyle += fmt.Sprintf(" color: %s;", d.IconColor.HexString())
}
a.AppendChild(&html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{{Key: atom.Style.String(), Val: iconSpanStyle}},
FirstChild: d.Icon(),
})
a.AppendChild(htmlg.Text(d.Text))
return []*html.Node{a}
}
type plainText struct {
Text string
}
func (d plainText) Render() []*html.Node {
text := &html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{{Key: atom.Style.String(), Val: "font-size: 13px; color: #666;"}},
FirstChild: htmlg.Text(d.Text),
}
return []*html.Node{text}
}
type imageText struct {
ImageURL string
Text string
}
func (d imageText) Render() []*html.Node {
image := &html.Node{
Type: html.ElementNode, Data: atom.Img.String(),
Attr: []html.Attribute{
{Key: atom.Src.String(), Val: d.ImageURL},
{Key: atom.Style.String(), Val: "width: 28px; height: 28px; margin-right: 6px; flex-shrink: 0;"},
},
}
text := &html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{{Key: atom.Style.String(), Val: "font-size: 13px; color: #666; flex-grow: 1;"}},
FirstChild: htmlg.Text(d.Text),
}
div := htmlg.Div(image, text)
div.Attr = append(div.Attr, html.Attribute{Key: atom.Style.String(), Val: "display: flex;"})
return []*html.Node{div}
}
func shortBody(s string) string {
if len(s) <= 200 {
return s
}
var i int
for i = 1; i < utf8.UTFMax && !utf8.RuneStart(s[200-i]); i++ {
}
return s[:200-i] + "…"
}
type commits struct {
Commits []event.Commit // Ordered from earliest to most recent (head).
}
func (d commits) Render() []*html.Node {
var nodes []*html.Node
// Display latest commits on top.
for i := len(d.Commits) - 1; i >= 0; i-- {
c := d.Commits[i]
commit := belt.Commit{
SHA: c.SHA,
Message: c.Message,
AuthorAvatarURL: c.AuthorAvatarURL,
HTMLURL: c.HTMLURL,
}
div := htmlg.Div(commit.Render()...)
div.Attr = append(div.Attr, html.Attribute{Key: atom.Style.String(), Val: "margin-top: 4px;"})
nodes = append(nodes, div)
}
return nodes
}
type pages struct {
ActorAvatarURL string // Actor that acted on the pages.
Pages []event.Page // Wiki pages that are affected.
}
func (d pages) Render() []*html.Node {
var nodes []*html.Node
for _, p := range d.Pages {
avatar := &html.Node{
Type: html.ElementNode, Data: atom.Img.String(),
Attr: []html.Attribute{
{Key: atom.Src.String(), Val: d.ActorAvatarURL},
{Key: atom.Style.String(), Val: "width: 16px; height: 16px; vertical-align: top; margin-right: 6px;"},
},
}
commitID := belt.CommitID{SHA: p.SHA, HTMLURL: p.CompareHTMLURL}
message := &html.Node{
Type: html.ElementNode, Data: atom.Span.String(),
Attr: []html.Attribute{{Key: atom.Style.String(), Val: "margin-left: 4px;"}},
}
switch p.Action {
case "created":
message.AppendChild(htmlg.Text("Create"))
case "edited":
message.AppendChild(htmlg.Text("Edit"))
}
message.AppendChild(htmlg.Text(" page "))
message.AppendChild(&html.Node{
Type: html.ElementNode, Data: atom.A.String(),
Attr: []html.Attribute{
{Key: atom.Href.String(), Val: p.HTMLURL},
},
FirstChild: htmlg.Text(p.Title),
})
message.AppendChild(htmlg.Text("."))
var ns []*html.Node
ns = append(ns, avatar)
ns = append(ns, commitID.Render()...)
ns = append(ns, message)
div := htmlg.Div(ns...)
div.Attr = append(div.Attr, html.Attribute{Key: atom.Style.String(), Val: "margin-top: 4px;"})
nodes = append(nodes, div)
}
return nodes
}