-
Notifications
You must be signed in to change notification settings - Fork 13
/
filters.go
461 lines (414 loc) · 13.3 KB
/
filters.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
package brutalinks
import (
"context"
"fmt"
"net/http"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
"github.com/go-chi/chi/v5"
"github.com/mariusor/qstring"
"gitlab.com/golang-commonmark/puny"
)
var (
nilFilter = EqualsString("-")
nilFilters = CompStrs{nilFilter}
notNilFilter = DifferentThanString("-")
notNilFilters = CompStrs{notNilFilter}
derefIRIFilters = &Filters{IRI: notNilFilters}
)
type CompStr = qstring.ComparativeString
type CompStrs []CompStr
func (cs CompStrs) Contains(f CompStr) bool {
for _, c := range cs {
if c.Str == f.Str {
return true
}
}
return false
}
type Filters struct {
Name CompStrs `qstring:"name,omitempty"`
Cont CompStrs `qstring:"content,omitempty"`
MedTypes CompStrs `qstring:"mediaType,omitempty"`
URL CompStrs `qstring:"url,omitempty"`
IRI CompStrs `qstring:"iri,omitempty"`
Generator CompStrs `qstring:"generator,omitempty"`
Type CompStrs `qstring:"type,omitempty"`
AttrTo CompStrs `qstring:"attributedTo,omitempty"`
InReplTo CompStrs `qstring:"inReplyTo,omitempty"`
OP CompStrs `qstring:"context,omitempty"`
Recipients CompStrs `qstring:"recipients,omitempty"`
Next vocab.IRI `qstring:"after,omitempty"`
Prev vocab.IRI `qstring:"before,omitempty"`
MaxItems int `qstring:"maxItems,omitempty"`
Object *Filters `qstring:"object,omitempty"`
Tag *Filters `qstring:"tag,omitempty"`
Actor *Filters `qstring:"actor,omitempty"`
}
// FiltersFromRequest loads the filters we use for generating storage queries from the HTTP request
func FiltersFromRequest(r *http.Request) *Filters {
f := new(Filters)
if err := qstring.Unmarshal(r.URL.Query(), f); err != nil {
return nil
}
if f.MaxItems <= 0 {
f.MaxItems = MaxContentItems
}
return f
}
var (
CreateActivitiesFilter = ActivityTypesFilter(vocab.CreateType)
AppreciationActivitiesFilter = ActivityTypesFilter(vocab.LikeType, vocab.DislikeType)
)
func AllFilters(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := defaultFilters(r)
f.Object = derefIRIFilters
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func DefaultFilters(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := topLevelFilters(r)
m := ContextListingModel(r.Context())
m.Title = "Newest items"
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// ContextLoads loads the searches we use for generating storage queries from the HTTP request
func ContextLoads(ctx context.Context) RemoteLoads {
if f, ok := ctx.Value(LoadsCtxtKey).(RemoteLoads); ok {
return f
}
return nil
}
// ContextActivityFilters loads the filters we use for generating storage queries from the HTTP request
func ContextActivityFilters(ctx context.Context) []*Filters {
if f, ok := ctx.Value(FilterCtxtKey).([]*Filters); ok {
return f
}
return nil
}
func SelfFiltersMw(id vocab.IRI) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := topLevelFilters(r)
f.Actor.IRI = CompStrs{LikeString(id.String())}
m := ContextListingModel(r.Context())
m.Title = "Local instance items"
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
var CreateFollowActivitiesFilter = CompStrs{
CompStr{Str: string(vocab.CreateType)},
CompStr{Str: string(vocab.FollowType)},
}
func FollowFilterMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := new(Filters)
f.IRI = CompStrs{LikeString(chi.URLParam(r, "hash"))}
f.Object = derefIRIFilters
f.Actor = derefIRIFilters
f.Type = ActivityTypesFilter(vocab.FollowType)
f.MaxItems = 1
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func FollowedFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := new(Filters)
f.Object = derefIRIFilters
f.Actor = derefIRIFilters
f.Type = CreateFollowActivitiesFilter
if m := ContextListingModel(r.Context()); m != nil {
m.Title = "Followed items"
m.ShowText = true
}
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func DifferentThanString(s string) CompStr {
return CompStr{Operator: "!", Str: s}
}
func defaultFilters(r *http.Request) *Filters {
f := FiltersFromRequest(r)
f.Type = CreateActivitiesFilter
f.Object = new(Filters)
f.Object.Type = ActivityTypesFilter(ValidContentTypes...)
f.Actor = derefIRIFilters
return f
}
func topLevelFilters(r *http.Request) *Filters {
f := defaultFilters(r)
f.Object.Name = notNilFilters
f.Object.InReplTo = nilFilters
return f
}
func FederatedFiltersMw(id vocab.IRI) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := topLevelFilters(r)
f.IRI = CompStrs{DifferentThanString(id.String())}
m := ContextListingModel(r.Context())
m.Title = "Federated items"
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func LikeString(s string) CompStr {
return CompStr{Operator: "~", Str: s}
}
func DomainFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
domain := chi.URLParam(r, "domain")
f := FiltersFromRequest(r)
f.Type = CreateActivitiesFilter
f.Object = &Filters{}
m := ContextListingModel(r.Context())
if len(domain) > 0 {
domainFilter := fmt.Sprintf("https://%s", puny.ToASCII(domain))
f.Object.URL = CompStrs{LikeString(domainFilter)}
f.Object.Type = CompStrs{EqualsString(string(vocab.PageType))}
m.Title = htmlf("Items pointing to %s", domain)
} else {
f.Object.MedTypes = CompStrs{
EqualsString(MimeTypeMarkdown),
EqualsString(MimeTypeText),
EqualsString(MimeTypeHTML),
}
f.Object.Type = ActivityTypesFilter(ValidContentTypes...)
m.Title = htmlf("Discussion items")
}
f.Object.OP = nilFilters
f.Actor = derefIRIFilters
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func tagsFilter(tag string) *Filters {
f := new(Filters)
f.Name = CompStrs{EqualsString("#" + tag)}
return f
}
func TagFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tag := chi.URLParam(r, "tag")
if len(tag) == 0 {
ctxtErr(next, w, r, errors.NotFoundf("tag not found"))
return
}
fc := new(Filters)
fc.MaxItems = MaxContentItems
fc.Type = CreateActivitiesFilter
fc.Object = new(Filters)
fc.Object.Tag = tagsFilter(tag)
fa := new(Filters)
fa.MaxItems = MaxContentItems
fa.Type = ModerationActivitiesFilter
fa.Tag = tagsFilter(tag)
fa.Object = derefIRIFilters
allFilters := []*Filters{fc, fa}
m := ContextListingModel(r.Context())
m.ShowText = true
m.Title = htmlf("Items tagged as #%s", tag)
ctx := context.WithValue(r.Context(), FilterCtxtKey, allFilters)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (h handler) ItemFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := FiltersFromRequest(r)
f.Type = CreateActivitiesFilter
hash := chi.URLParam(r, "hash")
f.MaxItems = 1
m := ContextContentModel(r.Context())
m.Hash = HashFromString(hash)
if !m.Hash.IsValid() {
h.v.HandleErrors(w, r, errors.NotFoundf("%q item", hash))
return
}
f.Object = &Filters{IRI: CompStrs{LikeString(hash)}}
f.Actor = derefIRIFilters
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func MessageFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authors := ContextAuthors(r.Context())
if len(authors) == 0 {
next.ServeHTTP(w, r)
return
}
f := FiltersFromRequest(r)
if len(f.IRI) > 0 {
for _, author := range authors {
f.AttrTo = append(f.AttrTo, EqualsString(author.AP().GetID().String()))
}
f.Type = append(CreateActivitiesFilter, AppreciationActivitiesFilter...)
f.Actor = derefIRIFilters
}
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
type moderationFilter struct {
Mod []string `qstring:"m"`
Type []string `qstring:"t"`
}
var (
modSubmissionsObjectFilter = &Filters{
Type: ActivityTypesFilter(ValidContentTypes...),
InReplTo: nilFilters,
}
modCommentsObjectFilter = &Filters{
Type: ActivityTypesFilter(ValidContentTypes...),
InReplTo: notNilFilters,
}
modAccountsObjectFilter = &Filters{
Type: ActivityTypesFilter(ValidActorTypes...),
}
)
func ModerationFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := FiltersFromRequest(r)
f.Type = ModerationActivitiesFilter
f.IRI = CompStrs{LikeString(chi.URLParam(r, "hash"))}
f.MaxItems = 1
f.Object = derefIRIFilters
f.Actor = derefIRIFilters
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func ModerationListingFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := FiltersFromRequest(r)
f.Type = ModerationActivitiesFilter
f.Object = derefIRIFilters
f.Actor = derefIRIFilters
f.MaxItems = MaxContentItems
mf := new(moderationFilter)
qstring.Unmarshal(r.URL.Query(), mf)
allFilters := make([]*Filters, 0)
showSubmissions := stringInSlice(mf.Type)("s")
showComments := stringInSlice(mf.Type)("c")
showUsers := stringInSlice(mf.Type)("a")
if len(mf.Type) > 0 && !(showSubmissions == showComments && showSubmissions == showUsers) {
if showSubmissions {
fs := *f
fs.Object = modSubmissionsObjectFilter
allFilters = append(allFilters, &fs)
}
if showComments {
fc := *f
fc.Object = modCommentsObjectFilter
allFilters = append(allFilters, &fc)
}
if showUsers {
fu := *f
fu.Object = modAccountsObjectFilter
allFilters = append(allFilters, &fu)
}
} else {
allFilters = append(allFilters, f)
}
if m := ContextListingModel(r.Context()); m != nil {
m.Title = "Moderation log"
}
ctx := context.WithValue(r.Context(), FilterCtxtKey, allFilters)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (h handler) ModerationListing(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := ContextCursor(r.Context())
if c == nil {
next.ServeHTTP(w, r)
return
}
s := ContextRepository(r.Context())
if s == nil {
next.ServeHTTP(w, r)
return
}
followups, _ := s.loadModerationFollowups(r.Context(), c.items)
if withFollowups := aggregateModeration(c.items, followups); len(withFollowups) > 0 {
c.items = withFollowups
}
defer next.ServeHTTP(w, r)
if Instance.Conf.AutoAcceptFollows {
fol, err := vocab.ToActor(s.app.AP())
if err != nil || fol.PublicKey.ID == "" {
return
}
for _, ren := range c.items {
maybeFollow, ok := ren.(*FollowRequest)
if !ok || maybeFollow == nil {
continue
}
follow := maybeFollow.AP()
if follow == nil {
continue
}
if !accountsEqual(*s.app, *maybeFollow.Object) {
continue
}
followerIRI := maybeFollow.SubmittedBy.Metadata.URL
if follow.GetType() != vocab.FollowType || AccountIsFollowed(s.app, maybeFollow.SubmittedBy) {
continue
}
s.WithAccount(s.app)
if err = s.SendFollowResponse(r.Context(), *maybeFollow, true, nil); err != nil {
h.v.addFlashMessage(Error, w, r, fmt.Sprintf("Unable to accept the follow request from %s", followerIRI))
return
}
s.app.Followers = append(s.app.Followers, *maybeFollow.SubmittedBy)
h.v.addFlashMessage(Success, w, r, fmt.Sprintf("Successfully accepted the follow request from %s", followerIRI))
}
}
})
}
func LoadInvitedMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hash := chi.URLParam(r, "hash")
if len(hash) == 0 {
next.ServeHTTP(w, r)
return
}
s := ContextRepository(r.Context())
if s == nil {
next.ServeHTTP(w, r)
return
}
a, err := s.LoadAccount(r.Context(), actors.IRI(s.fedbox.Service()).AddPath(hash))
if err != nil {
ctxtErr(next, w, r, err)
return
}
if m := ContextRegisterModel(r.Context()); a.IsValid() && m != nil {
m.Account = *a
}
next.ServeHTTP(w, r)
})
}
func ActorsFiltersMw(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f := FiltersFromRequest(r)
f.Type = CreateActivitiesFilter
f.Object = &Filters{Type: ActivityTypesFilter(ValidActorTypes...)}
f.Actor = derefIRIFilters
m := ContextListingModel(r.Context())
m.Title = "Account listing"
ctx := context.WithValue(r.Context(), FilterCtxtKey, []*Filters{f})
next.ServeHTTP(w, r.WithContext(ctx))
})
}