-
Notifications
You must be signed in to change notification settings - Fork 12
/
mention.go
410 lines (307 loc) · 11.3 KB
/
mention.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
package service
import (
"io"
"net/url"
"strings"
"github.com/EmissarySocial/emissary/model"
"github.com/PuerkitoBio/goquery"
"github.com/benpate/data"
"github.com/benpate/data/option"
"github.com/benpate/derp"
"github.com/benpate/exp"
"github.com/benpate/remote"
"github.com/benpate/rosetta/convert"
"github.com/benpate/rosetta/schema"
"github.com/benpate/rosetta/slice"
"github.com/tomnomnom/linkheader"
"go.mongodb.org/mongo-driver/bson/primitive"
"willnorris.com/go/microformats"
)
/*********************
* Mentions are a W3C standard for connecting conversations across the web.
*
* https://indieweb.org/Webmention
* https://www.w3.org/TR/mention/
*
* Golang RegExp syntax:
* - https://pkg.go.dev/regexp/syntax
* - https://github.com/google/re2/wiki/Syntax
*
*********************/
// Mention defines a service that can send and receive mention data
type Mention struct {
collection data.Collection
}
// NewMention returns a fully initialized Mention service
func NewMention(collection data.Collection) Mention {
service := Mention{}
service.Refresh(collection)
return service
}
/******************************************
* Lifecycle Methods
******************************************/
// Refresh updates any stateful data that is cached inside this service.
func (service *Mention) Refresh(collection data.Collection) {
service.collection = collection
}
// Close stops any background processes controlled by this service
func (service *Mention) Close() {
// Nothin to do here.
}
/******************************************
* Common Data Methods
******************************************/
// Query returns a slice containing all of the Mentions that match the provided criteria
func (service *Mention) Query(criteria exp.Expression, options ...option.Option) ([]model.Mention, error) {
result := make([]model.Mention, 0)
err := service.collection.Query(&result, notDeleted(criteria), options...)
return result, err
}
// List returns an iterator containing all of the Mentions that match the provided criteria
func (service *Mention) List(criteria exp.Expression, options ...option.Option) (data.Iterator, error) {
return service.collection.List(notDeleted(criteria), options...)
}
// Load retrieves an Mention from the database
func (service *Mention) Load(criteria exp.Expression, mention *model.Mention) error {
if err := service.collection.Load(notDeleted(criteria), mention); err != nil {
return derp.Wrap(err, "service.Mention.Load", "Error loading Mention", criteria)
}
return nil
}
// Save adds/updates an Mention in the database
func (service *Mention) Save(mention *model.Mention, note string) error {
// Clean the value before saving
if err := service.Schema().Clean(mention); err != nil {
return derp.Wrap(err, "service.Mention.Save", "Error cleaning Mention", mention)
}
// Save the value to the database
if err := service.collection.Save(mention, note); err != nil {
return derp.Wrap(err, "service.Mention.Save", "Error saving Mention", mention, note)
}
return nil
}
// Delete removes an Mention from the database (virtual delete)
func (service *Mention) Delete(mention *model.Mention, note string) error {
criteria := exp.Equal("_id", mention.StreamID)
// Delete this Mention
if err := service.collection.HardDelete(criteria); err != nil {
return derp.Wrap(err, "service.Mention.Delete", "Error deleting Mention", criteria)
}
return nil
}
/******************************************
* Model Service Methods
******************************************/
// ObjectType returns the type of object that this service manages
func (service *Mention) ObjectType() string {
return "Mention"
}
// New returns a fully initialized model.Group as a data.Object.
func (service *Mention) ObjectNew() data.Object {
result := model.NewMention()
return &result
}
func (service *Mention) ObjectID(object data.Object) primitive.ObjectID {
if mention, ok := object.(*model.Mention); ok {
return mention.MentionID
}
return primitive.NilObjectID
}
func (service *Mention) ObjectQuery(result any, criteria exp.Expression, options ...option.Option) error {
return service.collection.Query(result, notDeleted(criteria), options...)
}
func (service *Mention) ObjectList(criteria exp.Expression, options ...option.Option) (data.Iterator, error) {
return service.List(criteria, options...)
}
func (service *Mention) ObjectLoad(criteria exp.Expression) (data.Object, error) {
result := model.NewMention()
err := service.Load(criteria, &result)
return &result, err
}
func (service *Mention) ObjectSave(object data.Object, comment string) error {
if mention, ok := object.(*model.Mention); ok {
return service.Save(mention, comment)
}
return derp.NewInternalError("service.Mention.ObjectSave", "Invalid Object Type", object)
}
func (service *Mention) ObjectDelete(object data.Object, comment string) error {
if mention, ok := object.(*model.Mention); ok {
return service.Delete(mention, comment)
}
return derp.NewInternalError("service.Mention.ObjectDelete", "Invalid Object Type", object)
}
func (service *Mention) ObjectUserCan(object data.Object, authorization model.Authorization, action string) error {
return derp.NewUnauthorizedError("service.Mention", "Not Authorized")
}
func (service *Mention) Schema() schema.Schema {
return schema.New(model.MentionSchema())
}
/******************************************
* Custom Queries
******************************************/
func (service *Mention) QueryByObjectID(objectID primitive.ObjectID, options ...option.Option) ([]model.Mention, error) {
return service.Query(exp.Equal("objectId", objectID), options...)
}
/******************************************
* Web-Mention Helpers
******************************************/
func (service *Mention) FindLinks(body string) []string {
result := make([]string, 0)
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
return result
}
links := doc.Find("a[href]").Map(getHrefFromNode)
links = slice.Filter(links, isExternalHref)
return links
}
// Send will send a mention to the target's endpoint
func (service *Mention) Send(source string, target string) error {
const location = "service.Mention.Sent"
// Try to look up the target's endpoint
endpoint, err := service.DiscoverEndpoint(target)
if err != nil {
return derp.Wrap(err, location, "Error discovering endpoint", source, target)
}
// Try to send the mention data to the endpoint
txn := remote.Post(endpoint).
Form("source", source).
Form("target", target)
if err := txn.Send(); err != nil {
return derp.Wrap(err, location, "Error sending mention", source, target)
}
// Silence means success
return nil
}
// DiscoverEndpoint tries to find the Mention endpoint for the provided URL
func (service *Mention) DiscoverEndpoint(url string) (string, error) {
const location = "service.Mention.Discover"
var body string
txn := remote.Get(url).Response(&body, nil)
if err := txn.Send(); err != nil {
return "", derp.Wrap(err, location, "Error retrieving remote document", url)
}
// Look for Mention link in the response headers
if value := txn.ResponseObject.Header.Get("Link"); value != "" {
links := linkheader.Parse(value)
for _, link := range links {
if strings.ToLower(link.Rel) == "mention" {
return link.URL, nil
}
}
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
return "", derp.Wrap(err, location, "Error parsing remote document", url)
}
linkTag := doc.Find("link[rel=webmention]").First()
result := linkTag.AttrOr("href", "")
if result == "" {
return "", derp.NewBadRequestError(location, "No Mention endpoint found", url)
}
return result, nil
}
// Verify confirms that the source document includes a link to the target document
func (service *Mention) Verify(source string, target string, buffer io.Writer) error {
const location = "service.Mention.Verify"
var content string
// Try to load the source document
txn := remote.Get(source).Response(&content, nil)
if err := txn.Send(); err != nil {
return derp.Wrap(err, location, "Error retreiving source", source)
}
// Try to parse the source document as HTML
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
if err != nil {
return derp.Wrap(err, location, "Error parsing source", source)
}
// Find all anchor tags with an href attribute
hrefs := doc.Find("a[href]").Map(getHrefFromNode)
for _, href := range hrefs {
if href != target {
continue
}
// If buffer exists, write the source document to the buffer, then return without error.
if buffer != nil {
buffer.Write([]byte(content))
}
return nil
}
// Fall through means the link was not found. Return an error.
return derp.NewNotFoundError(location, "Target link not found", source, target)
}
func (service *Mention) ParseMicroformats(source io.Reader, originURL string) model.Mention {
mention := model.NewMention()
mention.OriginURL = originURL
parsedURL, err := url.Parse(originURL)
if err != nil {
return mention
}
// Try to parse microformats in the source document...
if mf := microformats.Parse(source, parsedURL); mf != nil {
populateMention(mf, &mention)
}
// No errors
return mention
}
func populateMention(mf *microformats.Data, mention *model.Mention) {
for _, item := range mf.Items {
for _, itemType := range item.Type {
switch itemType {
// Parse author information [https://microformats.org/wiki/h-card]
case "h-card":
if mention.AuthorName == "" {
mention.AuthorName = convert.String(item.Properties["name"])
}
if mention.AuthorName == "" {
mention.AuthorPhotoURL = convert.String(item.Properties["given-name"])
}
if mention.AuthorName == "" {
mention.AuthorPhotoURL = convert.String(item.Properties["nickname"])
}
if mention.AuthorWebsiteURL == "" {
mention.AuthorWebsiteURL = convert.String(item.Properties["url"])
}
if mention.AuthorEmail == "" {
mention.AuthorEmail = convert.String(item.Properties["email"])
}
if mention.AuthorPhotoURL == "" {
mention.AuthorPhotoURL = convert.String(item.Properties["photo"])
}
if mention.AuthorPhotoURL == "" {
mention.AuthorPhotoURL = convert.String(item.Properties["logo"])
}
if mention.AuthorStatus == "" {
mention.AuthorStatus = convert.String(item.Properties["note"])
}
continue
// Parse entry data
case "h-entry": // [https://microformats.org/wiki/h-entry]
if mention.EntryName == "" {
mention.EntryName = convert.String(item.Properties["name"])
}
if mention.EntrySummary == "" {
mention.EntrySummary = convert.String(item.Properties["summary"])
}
if mention.EntryPhotoURL == "" {
mention.EntryPhotoURL = convert.String(item.Properties["photo"])
}
}
}
}
// Last, scan global values for data that may not have been found in the h-entry
if mention.AuthorWebsiteURL == "" {
if me, ok := mf.Rels["me"]; ok {
mention.AuthorWebsiteURL = convert.String(me)
}
}
}
// getHrefFromNode returns the [href] value for a given goquery selection
func getHrefFromNode(index int, node *goquery.Selection) string {
return node.AttrOr("href", "")
}
// isExternalHref returns TRUE if this URL points to an external domain
func isExternalHref(href string) bool {
return strings.HasPrefix("http://", href) || strings.HasPrefix("https://", href)
}