-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebmention.go
379 lines (352 loc) · 10.1 KB
/
webmention.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
units "github.com/docker/go-units"
"github.com/gorilla/mux"
"github.com/spf13/viper"
"github.com/jcgregorio/go-lib/admin"
"github.com/jcgregorio/logger"
"github.com/jcgregorio/webmention-run/mention"
)
// Config keys as found in config.json.
const (
DATASTORE_NAMESPACE = "DATASTORE_NAMESPACE"
CLIENT_ID = "CLIENT_ID"
REGION = "REGION"
PROJECT = "PROJECT"
ADMINS = "ADMINS"
HOST = "HOST"
AUTHOR = "AUTHOR"
TARGETS = "TARGETS"
)
// flags
var (
local = flag.Bool("local", false, "Running locally if true. As opposed to in production.")
resourcesDir = flag.String("resources_dir", "", "The directory to find templates, JS, and CSS files. If blank the current directory will be used.")
)
var (
m *mention.Mentions
log = logger.New()
ad *admin.Admin
triageTemplate *template.Template
mentionsTemplate *template.Template
)
func initialize() {
flag.Parse()
viper.SetConfigType("json")
if *resourcesDir == "" {
_, filename, _, _ := runtime.Caller(0)
*resourcesDir = filepath.Join(filepath.Dir(filename))
}
f, err := os.Open(filepath.Join(*resourcesDir, "config.json"))
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := viper.ReadConfig(f); err != nil {
log.Fatal(err)
}
log.Infof("%q\n", *resourcesDir)
ad = admin.New(viper.GetString(CLIENT_ID), viper.GetStringSlice(ADMINS))
triageTemplate = template.Must(template.New("triage").Funcs(template.FuncMap{
"trunc": func(s string) string {
if len(s) > 80 {
return s[:80] + "..."
}
return s
},
"humanTime": func(t time.Time) string {
if t.IsZero() {
return ""
}
return " • " + units.HumanDuration(time.Now().Sub(t)) + " ago"
},
}).Parse(fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=egde,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="%s">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<style type="text/css" media="screen">
#webmentions {
display: grid;
padding: 1em;
grid-template-columns: 5em 10em 1fr;
grid-column-gap: 10px;
grid-row-gap: 6px;
}
</style>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<script>
function onSignIn(googleUser) {
document.cookie = "id_token=" + googleUser.getAuthResponse().id_token;
if (!{{.IsAdmin}}) {
window.location.reload();
}
};
</script>
<div id=webmentions>
{{range .Mentions }}
<select name="text" data-key="{{ .Key }}">
<option value="good" {{if eq .State "good" }}selected{{ end }} >Good</option>
<option value="spam" {{if eq .State "spam" }}selected{{ end }} >Spam</option>
<option value="untriaged" {{if eq .State "untriaged" }}selected{{ end }} >Untriaged</option>
</select>
<span>{{ .TS | humanTime }}</span>
<div>
<div>Source: <a href="{{ .Source }}">{{ .Source | trunc }}</a></div>
<div>Target: <a href="{{ .Target }}">{{ .Target | trunc }}</a></div>
</div>
{{end}}
</div>
<div><a href="?offset={{.Offset}}">Next</a></div>
<script type="text/javascript" charset="utf-8">
// TODO - listen on div.webmentions for click/input and then write
// triage action back to server.
document.getElementById('webmentions').addEventListener('change', e => {
console.log(e);
if (e.target.dataset.key != "") {
fetch("/UpdateMention", {
credentials: 'same-origin',
method: 'POST',
body: JSON.stringify({
key: e.target.dataset.key,
value: e.target.value,
}),
headers: new Headers({
'Content-Type': 'application/json'
})
}).catch(e => console.error('Error:', e));
}
});
</script>
</body>
</html>`, viper.GetString(CLIENT_ID))))
mentionsTemplate = template.Must(template.New("mentions").Funcs(template.FuncMap{
"humanTime": func(t time.Time) string {
if t.IsZero() {
return ""
}
return " • " + units.HumanDuration(time.Now().Sub(t)) + " ago"
},
"rfc3999": func(t time.Time) string {
if t.IsZero() {
return ""
}
return t.Format(time.RFC3339)
},
"trunc": func(s string) string {
if len(s) > 200 {
return s[:200] + "..."
}
return s
},
}).Parse(`
<section id=webmention>
<h3>WebMentions</h3>
{{ $host := .Host }}
{{ range .Mentions }}
<span class="wm-author">
{{ if .AuthorURL }}
{{ if .Thumbnail }}
<a href="{{ .AuthorURL}}" rel=nofollow class="wm-thumbnail">
<img src="{{ $host }}/Thumbnail/{{ .Thumbnail }}"/>
</a>
{{ end }}
<a href="{{ .AuthorURL}}" rel=nofollow>
{{ .Author }}
</a>
{{ else }}
{{ .Author }}
{{ end }}
</span>
<time datetime="{{ .TS | rfc3999 }}">{{ .TS | humanTime }}</time>
{{ if .URL }}
<a class="wm-content" href="{{ .URL }}" rel=nofollow>
{{ else }}
<a class="wm-content" href="{{ .Source }}" rel=nofollow>
{{ end }}
{{ if .Title }}
{{ .Title | trunc }}
{{ else }}
{{ .Source | trunc }}
{{ end }}
</a>
{{ end }}
</section>
`))
m, err = mention.NewMentions(context.Background(), viper.GetString(PROJECT), viper.GetString(DATASTORE_NAMESPACE), log)
if err != nil {
log.Fatal(err)
} else {
log.Info("Initialized.")
}
}
type triageContext struct {
IsAdmin bool
Mentions []*mention.MentionWithKey
Offset int64
}
// triageHandler displays the triage page for Webmentions.
func triageHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
context := &triageContext{}
isAdmin := ad.IsAdmin(r, log)
if isAdmin {
limitText := r.FormValue("limit")
if limitText == "" {
limitText = "20"
}
offsetText := r.FormValue("offset")
if offsetText == "" {
offsetText = "0"
}
limit, err := strconv.ParseInt(limitText, 10, 32)
if err != nil {
log.Infof("Failed to parse limit: %s", err)
return
}
offset, err := strconv.ParseInt(offsetText, 10, 32)
if err != nil {
log.Infof("Failed to parse offset: %s", err)
return
}
context = &triageContext{
IsAdmin: isAdmin,
Mentions: m.GetTriage(r.Context(), int(limit), int(offset)),
Offset: offset + limit,
}
}
if err := triageTemplate.Execute(w, context); err != nil {
log.Errorf("Failed to render triage template: %s", err)
}
}
type updateMention struct {
Key string `json:"key"`
Value string `json:"value"`
}
// updateMentionHandler updates the triage state of a webmention.
// Called from the Triage page.
func updateMentionHandler(w http.ResponseWriter, r *http.Request) {
isAdmin := ad.IsAdmin(r, log)
if !isAdmin {
http.Error(w, "Unauthorized", 401)
}
var u updateMention
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
log.Infof("Failed to decode update: %s", err)
http.Error(w, "Bad JSON", 400)
}
if err := m.UpdateState(r.Context(), u.Key, u.Value); err != nil {
log.Infof("Failed to write update: %s", err)
http.Error(w, "Failed to write", 400)
}
}
// MentionsContext is the data for expanding the Mentions template.
type MentionsContext struct {
Host string
Mentions []*mention.Mention
}
// mentionsHandler returns HTML describing all the good Webmentions for the given URL.
func mentionsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Referrer-Policy", "unsafe-url")
if r.Method == "OPTIONS" {
return
}
ref := r.Referer()
if len(ref) == 0 {
return
}
if strings.HasSuffix(ref, "/") {
ref = ref[:len(ref)-1]
}
mentions := m.GetGood(r.Context(), ref)
if len(mentions) == 0 {
return
}
context := MentionsContext{
Host: viper.GetString(HOST),
Mentions: mentions,
}
if err := mentionsTemplate.Execute(w, context); err != nil {
log.Errorf("Failed to expand template: %s", err)
}
}
// incomingWebMentionHandler handles incoming Webmentions.
func incomingWebMentionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
mention := mention.New(r.FormValue("source"), r.FormValue("target"))
if err := mention.FastValidate(viper.GetStringSlice(TARGETS)); err != nil {
log.Infof("Invalid request: %s", err)
http.Error(w, fmt.Sprintf("Invalid request."), 400)
return
}
if err := m.Put(r.Context(), mention); err != nil {
log.Infof("Failed to enqueue mention: %s", err)
http.Error(w, fmt.Sprintf("Failed to enqueue mention."), 400)
return
}
w.WriteHeader(http.StatusAccepted)
}
func thumbnailHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
vars := mux.Vars(r)
b, err := m.GetThumbnail(r.Context(), vars["id"])
if err != nil {
http.Error(w, "Image not found", 404)
log.Warningf("Failed to get image: %s", err)
return
}
if _, err = w.Write(b); err != nil {
log.Errorf("Failed to write image: %s", err)
return
}
}
// verifyQueuedMentions verifies untriaged webmentions.
//
// Should be called on a timer.
func verifyQueuedMentions(w http.ResponseWriter, r *http.Request) {
client := &http.Client{
Timeout: time.Second * 30,
}
m.VerifyQueuedMentions(client)
}
func main() {
initialize()
r := mux.NewRouter()
r.HandleFunc("/Mentions", mentionsHandler).Methods("GET", "OPTIONS")
r.HandleFunc("/IncomingWebMention", incomingWebMentionHandler).Methods("POST")
r.HandleFunc("/UpdateMention", updateMentionHandler).Methods("POST")
r.HandleFunc("/Thumbnail/{id:[a-z0-9]+}", thumbnailHandler).Methods("GET")
r.HandleFunc("/VerifyQueuedMentions", verifyQueuedMentions).Methods("POST")
r.HandleFunc("/", triageHandler).Methods("GET")
http.Handle("/", r)
port := os.Getenv("PORT")
if port == "" {
port = "1313"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}