-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
67 lines (64 loc) · 1.88 KB
/
example_test.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
package filters
import (
"fmt"
vocab "github.com/go-ap/activitypub"
)
func ExampleFilter() {
collection := vocab.ItemCollection{
// doesn't match due to Actor ID
vocab.Create{
Type: "Create",
Actor: vocab.IRI("https://example.com/bob"),
Object: vocab.IRI("https//example.com/test"),
},
// doesn't match due to nil Object
vocab.Create{
Type: "Create",
Actor: vocab.IRI("https://example.com/jdoe"),
},
// match
vocab.Create{
Type: "Create",
Actor: vocab.IRI("https://example.com/jdoe"),
Object: vocab.IRI("https//example.com/test"),
},
// match
vocab.Create{
Type: "Create",
Actor: vocab.Person{
ID: "https://example.com/jdoe1",
Name: vocab.DefaultNaturalLanguageValue("JohnDoe"),
},
Object: vocab.IRI("https//example.com/test"),
},
// doesn't match due to the activity Type
vocab.Follow{Type: "Follow"},
// doesn't match due to Arrive being an intransitive activity
vocab.Arrive{Type: "Arrive"},
// doesn't match due to Question being an intransitive activity
vocab.Question{Type: "Question"},
}
// This filters all activities that are not:
// Create activities,
// published by an Actor with the ID https://example.com/authors/jdoe, or with the name "JohnDoe"
// and, which have an object with a non nil ID.
filterFn := All(
HasType("Create"),
Actor(
Any(
SameID("https://example.com/jdoe"),
NameIs("JohnDoe"),
),
),
Object(Not(NilID)),
)
result := make(vocab.ItemCollection, 0)
for _, it := range collection {
if filterFn.Match(it) {
result = append(result, it)
}
}
output, _ := vocab.MarshalJSON(result)
fmt.Printf("Result[%d]: %s", len(result), output)
// Output: Result[2]: [{"type":"Create","actor":"https://example.com/jdoe","object":"https//example.com/test"},{"type":"Create","actor":{"id":"https://example.com/jdoe1","name":"JohnDoe"},"object":"https//example.com/test"}]
}