-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiri.go
64 lines (52 loc) · 1.47 KB
/
iri.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
package filters
import (
"net/url"
"strings"
vocab "github.com/go-ap/activitypub"
"golang.org/x/text/unicode/norm"
)
type iriEquals vocab.IRI
func (i iriEquals) Match(it vocab.Item) bool {
if vocab.IsNil(it) {
return len(i) == 0
}
return it.GetLink().Equals(vocab.IRI(i), true)
}
// SameIRI checks an activitypub.Object's IRI
func SameIRI(iri vocab.IRI) Check {
return iriEquals(iri)
}
type iriLike string
func (frag iriLike) Match(it vocab.Item) bool {
if vocab.IsNil(it) {
return false
}
nfc := norm.NFC.String
fragStr, _ := url.QueryUnescape(string(frag))
return strings.Contains(nfc(it.GetLink().String()), nfc(fragStr))
}
// IRILike
func IRILike(frag string) Check {
return iriLike(frag)
}
// NilIRI checks if the activitypub.Item's IRI that matches any of the two magic values
// that denote an empty value: activitypub.NilID = "-", or activitypub.EmptyID = ""
var NilIRI = iriNil{}
type iriNil struct{}
func (n iriNil) Match(it vocab.Item) bool {
if vocab.IsNil(it) {
return true
}
if vocab.IsItemCollection(it) {
result := false
_ = vocab.OnItemCollection(it, func(col *vocab.ItemCollection) error {
result = len(*col) == 0
return nil
})
return result
}
return Any(SameIRI(vocab.NilIRI), SameIRI(vocab.EmptyIRI)).Match(it.GetLink())
}
// NotNilIRI checks if the activitypub.Object's URL property matches any of the two magic values
// that denote an empty value: activitypub.NilID = "-", or activitypub.EmptyID = ""
var NotNilIRI = Not(iriNil{})