-
Notifications
You must be signed in to change notification settings - Fork 2
/
blog.go
283 lines (259 loc) · 8.86 KB
/
blog.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
package main
import (
"context"
"fmt"
"html/template"
"io"
"net/http"
"os"
"strconv"
"github.com/shurcooL/home/component"
"github.com/shurcooL/home/httputil"
"github.com/shurcooL/home/internal/exp/service/notification"
"github.com/shurcooL/home/internal/exp/service/notification/v2tov1"
blogpkg "github.com/shurcooL/home/internal/page/blog"
"github.com/shurcooL/htmlg"
"github.com/shurcooL/httperror"
"github.com/shurcooL/issues"
"github.com/shurcooL/issuesapp"
"github.com/shurcooL/users"
)
var blogHTML = template.Must(template.New("").Parse(`<html>
<head>
{{.AnalyticsHTML}} <title>Dmitri Shuralyov - Blog</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="/blog/assets/gfm/gfm.css" rel="stylesheet" type="text/css">
<style type="text/css">
.markdown-body { font-family: Go; }
tt, code, pre { font-family: "Go Mono"; }
</style>
<link href="/assets/blog/style.css" rel="stylesheet" type="text/css">
<script async src="/assets/blog/blog.js"></script>
</head>
<body>`))
// initBlog registers a blog handler with blog URI as blog content source.
func initBlog(mux *http.ServeMux, issuesService issues.Service, blog issues.RepoSpec, notification notification.Service, users users.Service) error {
dmitshurBlogService := dmitshurBlogService{
Service: issuesService,
users: users,
}
opt := issuesapp.Options{
Notifications: v2tov1.Service{V2: notification},
HeadPre: analyticsHTML + `<title>Dmitri Shuralyov - Blog</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">
<style type="text/css">
body {
margin: 20px;
font-family: Go;
font-size: 14px;
line-height: initial;
color: rgb(35, 35, 35);
}
.btn {
font-family: inherit;
font-size: 11px;
line-height: 11px;
height: 18px;
border-radius: 4px;
border: solid #d2d2d2 1px;
background-color: #fff;
box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
}
.post .markdown-body {
font-size: 16px;
line-height: 1.6;
}
.post .black-link a, .black-link a:focus, .black-link a:hover {
color: rgb(35, 35, 35);
}
.post ul.post-meta {
padding-left: 0;
list-style: none;
margin-top: 10px;
margin-bottom: 20px;
font-family: inherit;
font-size: 14px;
line-height: 18px;
color: #999;
}
.post li.post-meta {
display: inline-block;
margin-right: 30px;
}
.post div.reactable-container {
display: inline-block;
vertical-align: top;
margin-left: 0;
}
.post .reaction-bar-appear:hover div.new-reaction {
display: inline-block;
}
/* Make new-reaction button visible if there are no other reactions. */
.post div.reactable-container a:first-child div.new-reaction {
display: inline-block;
}
</style>`,
HeadPost: `<style type="text/css">
.markdown-body { font-family: Go; }
tt, code, pre { font-family: "Go Mono"; }
</style>`,
BodyPre: `{{/* Override create issue button to only show up for dmitshur as New Blog Post button. */}}
{{define "create-issue"}}
{{if and (eq .CurrentUser.ID 1924134) (eq .CurrentUser.Domain "github.com")}}
<div style="text-align: right;"><button class="btn btn-success btn-small" onclick="window.location = '{{.BaseURI}}/new';">New Blog Post</button></div>
{{end}}
{{end}}
{{define "issue"}}
{{if .ForceIssuesApp}}
<h1>{{.Issue.Title}} <span class="gray">#{{.Issue.ID}}</span></h1>
<div id="issue-state-badge" style="margin-bottom: 20px;">{{render (issueStateBadge .Issue)}}</div>
{{else}}
<h2 id="comments">Comments</h2>
{{end}}
{{range .Items}}
{{template "issue-item" .}}
{{end}}
<div id="new-item-marker"></div>
{{if (and (eq .CurrentUser.ID 0) (not .Items))}}
{{/* HACK: Negative offset to make "Sign in via URL to comment." appear aligned. */}}
<div style="margin-left: -58px;">{{template "new-comment" .}}</div>
{{else}}
{{template "new-comment" .}}
{{end}}
{{end}}
<div style="max-width: 800px; margin: 0 auto 100px auto;">`,
SignIn: signInViaURL,
}
opt.BodyTop = func(req *http.Request) ([]htmlg.Component, error) {
authenticatedUser, err := users.GetAuthenticated(req.Context())
if err != nil {
return nil, err
}
var nc uint64
if authenticatedUser.ID != 0 {
nc, err = notification.CountNotifications(req.Context())
if err != nil {
return nil, err
}
}
returnURL := req.RequestURI
header := component.Header{
CurrentUser: authenticatedUser,
NotificationCount: nc,
ReturnURL: returnURL,
}
// Check if we're on an idividual blog post /{id:[0-9]+} page.
// This is a copy of issueapp's router logic.
_, forceIssuesApp := req.Context().Value(forceIssuesAppContextKey).(struct{})
if issueID, err := strconv.ParseUint(req.URL.Path[1:], 10, 64); err == nil && !forceIssuesApp {
issue, err := issuesService.Get(req.Context(), blog, issueID)
if err != nil {
return nil, err
}
comments, err := issuesService.ListComments(req.Context(), blog, issueID, &issues.ListOptions{Length: 1})
if err != nil {
return nil, err
}
if len(comments) == 0 {
return nil, fmt.Errorf("blog post %d has no body", issueID)
}
issue.Comment = comments[0]
post := blogpkg.Post{CurrentUser: authenticatedUser, Issue: issue}
return []htmlg.Component{header, post}, nil
}
// If this is not an issue page, that's okay, only include the header.
return []htmlg.Component{header}, nil
}
issuesApp := issuesapp.New(dmitshurBlogService, users, opt)
blogHandler := cookieAuth{httputil.ErrorHandler(users, func(w http.ResponseWriter, req *http.Request) error {
prefixLen := len("/blog")
if prefix := req.URL.Path[:prefixLen]; req.URL.Path == prefix+"/" {
baseURL := prefix
if req.URL.RawQuery != "" {
baseURL += "?" + req.URL.RawQuery
}
return httperror.Redirect{URL: baseURL}
}
req = copyRequestAndURL(req)
req.URL.Path = req.URL.Path[prefixLen:]
if req.URL.Path == "" {
req.URL.Path = "/"
}
forceIssuesApp, _ := strconv.ParseBool(req.URL.Query().Get("issuesapp"))
switch {
case req.URL.Path == "/" && !forceIssuesApp:
if req.Method != "GET" {
return httperror.Method{Allowed: []string{"GET"}}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := blogHTML.Execute(w, data)
if err != nil {
return err
}
authenticatedUser, err := users.GetAuthenticated(req.Context())
if err != nil {
return err // THINK: Should it be a fatal error or not? What about on frontend vs backend?
}
returnURL := req.RequestURI
err = blogpkg.RenderBodyInnerHTML(req.Context(), w, issuesService, blog, notification, authenticatedUser, returnURL)
if err != nil {
return err
}
_, err = io.WriteString(w, `</body></html>`)
return err
default:
req = req.WithContext(context.WithValue(req.Context(), issuesapp.RepoSpecContextKey, blog))
req = req.WithContext(context.WithValue(req.Context(), issuesapp.BaseURIContextKey, "/blog"))
if forceIssuesApp {
req = req.WithContext(context.WithValue(req.Context(), forceIssuesAppContextKey, struct{}{}))
}
issuesApp.ServeHTTP(w, req)
return nil
}
})}
mux.Handle("/blog", blogHandler)
mux.Handle("/blog/", blogHandler)
return nil
}
// dmitshurBlogService skips first comment (the issue body), because we're
// taking on responsibility to render it ourselves (unless forceIssuesApp
// is set). It also limits an issues.Service's Create method to allow only
// dmitshur to create new blog posts.
type dmitshurBlogService struct {
issues.Service
users users.Service
}
func (s dmitshurBlogService) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) {
cs, listCommentsError := s.Service.ListComments(ctx, repo, id, opt)
_, forceIssuesApp := ctx.Value(forceIssuesAppContextKey).(struct{})
if len(cs) >= 1 && !forceIssuesApp {
// Skip first comment (the issue body), we're taking on responsibility
// to render it ourselves.
cs = cs[1:]
}
return cs, listCommentsError
}
func (s dmitshurBlogService) Create(ctx context.Context, repo issues.RepoSpec, issue issues.Issue) (issues.Issue, error) {
currentUser, err := s.users.GetAuthenticatedSpec(ctx)
if err != nil {
return issues.Issue{}, err
}
if currentUser != dmitshur {
return issues.Issue{}, os.ErrPermission
}
return s.Service.Create(ctx, repo, issue)
}
func (s dmitshurBlogService) ThreadType(repo issues.RepoSpec) string {
return s.Service.(interface {
ThreadType(issues.RepoSpec) string
}).ThreadType(repo)
}
// forceIssuesAppContextKey is a context key. It can be used to check whether
// issuesapp is being forced upon the blog. The associated value will be of type struct{}.
// Eventually, a better solution should be found, and this removed.
var forceIssuesAppContextKey = &contextKey{"ForceIssuesApp"}