-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
369 lines (314 loc) · 8.26 KB
/
object.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
package filters
import (
"net/url"
"strings"
vocab "github.com/go-ap/activitypub"
"golang.org/x/text/unicode/norm"
)
// NilID checks if the activitypub.Object's ID property matches any of the two magic values
// that denote an empty value: activitypub.NilID = "-", or activitypub.EmptyID = ""
var NilID = idNil{}
// NotNilID checks if the activitypub.Object's ID property is not nil
var NotNilID = Not(NilID)
type idNil iriNil
func (n idNil) Apply(it vocab.Item) bool {
return vocab.IsNil(it) || Any(SameIRI(vocab.NilIRI), SameIRI(vocab.EmptyIRI)).Apply(it.GetID())
}
// SameID checks an activitypub.Object's ID property against the received iri.
func SameID(i vocab.IRI) Check {
return idEquals(i)
}
type idEquals iriEquals
func (i idEquals) Apply(item vocab.Item) bool {
if vocab.IsNil(item) {
return len(i) == 0
}
return item.GetID().Equals(vocab.IRI(i), true)
}
// IDLike
func IDLike(frag string) Check {
return idLike(frag)
}
type idLike iriLike
func (l idLike) Apply(item vocab.Item) bool {
if vocab.IsNil(item) {
return false
}
nfc := norm.NFC.String
fragStr, _ := url.QueryUnescape(string(l))
return strings.Contains(nfc(item.GetID().String()), nfc(fragStr))
}
// HasType checks an activitypub.Object's Type property against a series of values.
// If any of the ty values matches, the function returns true.
func HasType(ty ...vocab.ActivityVocabularyType) Check {
return withTypes(ty)
}
type withTypes vocab.ActivityVocabularyTypes
func (types withTypes) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(types) == 0
}
return vocab.ActivityVocabularyTypes(types).Contains(it.GetType())
}
func accumURLs(item vocab.Item) vocab.IRIs {
urls := make(vocab.IRIs, 0)
if vocab.LinkTypes.Contains(item.GetType()) {
_ = vocab.OnLink(item, func(lnk *vocab.Link) error {
urls = append(urls, lnk.Href)
return nil
})
} else {
_ = vocab.OnObject(item, func(ob *vocab.Object) error {
if vocab.IsNil(ob.URL) {
return nil
}
if vocab.IsIRI(ob.URL) {
urls = append(urls, ob.URL.GetLink())
} else if vocab.IsIRIs(ob.URL) {
_ = vocab.OnIRIs(ob.URL, func(replTos *vocab.IRIs) error {
for _, r := range *replTos {
urls = append(urls, r.GetLink())
}
return nil
})
} else if vocab.IsItemCollection(ob.URL) {
_ = vocab.OnItemCollection(ob.URL, func(uc *vocab.ItemCollection) error {
for _, u := range *uc {
urls = append(urls, u.GetLink())
}
return nil
})
} else {
_ = vocab.OnObject(ob.URL, func(url *vocab.Object) error {
urls = append(urls, url.GetLink())
return nil
})
}
return nil
})
}
return urls
}
// SameURL checks an activitypub.Object's IRI
func SameURL(iri vocab.IRI) Check {
return urlEquals(iri)
}
type urlEquals iriEquals
func (i urlEquals) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(i) == 0
}
return accumURLs(it).Contains(vocab.IRI(i))
}
type urlLike iriLike
func (frag urlLike) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(frag) == 0
}
nfc := norm.NFC.String
fragStr, _ := url.QueryUnescape(string(frag))
for _, u := range accumURLs(it) {
if strings.Contains(nfc(u.String()), nfc(fragStr)) {
return true
}
}
return false
}
func URLLike(frag string) Check {
return urlLike(frag)
}
func accumContexts(item vocab.Item) vocab.IRIs {
iris := make(vocab.IRIs, 0)
_ = vocab.OnObject(item, func(ob *vocab.Object) error {
if vocab.IsNil(ob.Context) {
return nil
}
if vocab.IsIRI(ob.Context) {
iris = append(iris, ob.Context.GetLink())
} else if vocab.IsIRIs(ob.Context) {
_ = vocab.OnIRIs(ob.Context, func(col *vocab.IRIs) error {
for _, r := range *col {
iris = append(iris, r.GetLink())
}
return nil
})
} else if vocab.IsItemCollection(ob.Context) {
_ = vocab.OnItemCollection(ob.Context, func(col *vocab.ItemCollection) error {
for _, c := range *col {
iris = append(iris, c.GetLink())
}
return nil
})
} else {
_ = vocab.OnObject(ob.Context, func(c *vocab.Object) error {
iris = append(iris, c.GetLink())
return nil
})
}
return nil
})
return iris
}
func SameContext(iri vocab.IRI) Check {
return contextEquals(iri)
}
type contextEquals iriEquals
func (c contextEquals) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(c) == 0
}
return accumContexts(it).Contains(vocab.IRI(c))
}
func ContextLike(frag string) Check {
return contextLike(frag)
}
type contextLike iriLike
func (c contextLike) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(c) == 0
}
return accumContexts(it).Contains(vocab.IRI(c))
}
var NilContext = contextNil{}
type contextNil iriNil
func (c contextNil) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return true
}
return len(accumContexts(it)) == 0
}
func accumAttributedTos(item vocab.Item) vocab.IRIs {
iris := make(vocab.IRIs, 0)
_ = vocab.OnObject(item, func(ob *vocab.Object) error {
if vocab.IsNil(ob.AttributedTo) {
return nil
}
if vocab.IsIRI(ob.AttributedTo) {
iris = append(iris, ob.AttributedTo.GetLink())
} else if vocab.IsIRIs(ob.AttributedTo) {
_ = vocab.OnIRIs(ob.AttributedTo, func(col *vocab.IRIs) error {
for _, r := range *col {
iris = append(iris, r.GetLink())
}
return nil
})
} else if vocab.IsItemCollection(ob.AttributedTo) {
_ = vocab.OnItemCollection(ob.AttributedTo, func(attrTos *vocab.ItemCollection) error {
for _, a := range *attrTos {
iris = append(iris, a.GetLink())
}
return nil
})
} else {
_ = vocab.OnObject(ob.AttributedTo, func(attrTo *vocab.Object) error {
iris = append(iris, attrTo.GetLink())
return nil
})
}
return nil
})
return iris
}
func SameAttributedTo(iri vocab.IRI) Check {
return attributedToEquals(iri)
}
type attributedToEquals iriEquals
func (a attributedToEquals) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(a) == 0
}
return accumAttributedTos(it).Contains(vocab.IRI(a))
}
func AttributedToLike(frag string) Check {
return attributedToLike(frag)
}
type attributedToLike iriLike
func (a attributedToLike) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(a) == 0
}
nfc := norm.NFC.String
fragStr, _ := url.QueryUnescape(string(a))
for _, u := range accumAttributedTos(it) {
if strings.Contains(nfc(u.String()), nfc(fragStr)) {
return true
}
}
return false
}
var NilAttributedTo = attributedToNil{}
type attributedToNil iriNil
func (a attributedToNil) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return true
}
return len(accumAttributedTos(it)) == 0
}
func accumInReplyTos(item vocab.Item) vocab.IRIs {
iris := make(vocab.IRIs, 0)
_ = vocab.OnObject(item, func(ob *vocab.Object) error {
if vocab.IsNil(ob.InReplyTo) {
return nil
}
if vocab.IsIRI(ob.InReplyTo) {
iris = append(iris, ob.InReplyTo.GetLink())
} else if vocab.IsIRIs(ob.InReplyTo) {
_ = vocab.OnIRIs(ob.InReplyTo, func(replTos *vocab.IRIs) error {
for _, r := range *replTos {
iris = append(iris, r.GetLink())
}
return nil
})
} else if vocab.IsItemCollection(ob.InReplyTo) {
_ = vocab.OnItemCollection(ob.InReplyTo, func(replTos *vocab.ItemCollection) error {
for _, r := range *replTos {
iris = append(iris, r.GetLink())
}
return nil
})
} else {
_ = vocab.OnObject(ob.InReplyTo, func(inReplyTo *vocab.Object) error {
iris = append(iris, inReplyTo.GetLink())
return nil
})
}
return nil
})
return iris
}
var NilInReplyTo = inReplyToNil{}
type inReplyToNil iriNil
func (c inReplyToNil) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return true
}
return len(accumInReplyTos(it)) == 0
}
func InReplyToLike(frag string) Check {
return inReplyToLike(frag)
}
type inReplyToLike iriLike
func (a inReplyToLike) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(a) == 0
}
nfc := norm.NFC.String
fragStr, _ := url.QueryUnescape(string(a))
for _, u := range accumInReplyTos(it) {
if strings.Contains(nfc(u.String()), nfc(fragStr)) {
return true
}
}
return false
}
// SameInReplyTo checks an activitypub.Object's InReplyTo
func SameInReplyTo(iri vocab.IRI) Check {
return inReplyToEquals(iri)
}
type inReplyToEquals iriEquals
func (i inReplyToEquals) Apply(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(i) == 0
}
return accumInReplyTos(it).Contains(vocab.IRI(i))
}