/
predicates.go
115 lines (99 loc) · 3.36 KB
/
predicates.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
package firehose
import (
"context"
"strings"
comatproto "github.com/bluesky-social/indigo/api/atproto"
"github.com/bluesky-social/indigo/api/bsky"
"github.com/uabluerail/bsky-tools/didset"
cbg "github.com/whyrusleeping/cbor-gen"
)
func AllOf(predicates ...Predicate) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
for _, p := range predicates {
if !p(ctx, commit, op, record) {
return false
}
}
return true
}
}
func AnyOf(predicates ...Predicate) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
for _, p := range predicates {
if p(ctx, commit, op, record) {
return true
}
}
return false
}
}
func Not(predicate Predicate) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
return !predicate(ctx, commit, op, record)
}
}
func MentionsDID(did string) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
rec, ok := record.(*bsky.FeedPost)
if !ok {
return false
}
for _, facet := range rec.Facets {
for _, feature := range facet.Features {
if mention := feature.RichtextFacet_Mention; mention != nil {
if mention.Did == did {
return true
}
}
}
}
return false
}
}
func CreateOrUpdateOp() Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
return op.Action == "create" || op.Action == "update"
}
}
func DeleteOp() Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
return op.Action == "delete"
}
}
func From(did string) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
return commit.Repo == did
}
}
func IsInCollection(collection string) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
return strings.HasPrefix(op.Path, collection+"/")
}
}
func IsBlock() Predicate {
return IsInCollection("app.bsky.graph.block")
}
func IsPost() Predicate {
return IsInCollection("app.bsky.feed.post")
}
func IsFollow() Predicate {
return IsInCollection("app.bsky.graph.follow")
}
func SenderInSet(set didset.QueryableDIDSet) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
r, err := set.Contains(ctx, commit.Repo)
if err != nil {
return false
}
return r
}
}
func SenderNotInSet(set didset.QueryableDIDSet) Predicate {
return func(ctx context.Context, commit *comatproto.SyncSubscribeRepos_Commit, op *comatproto.SyncSubscribeRepos_RepoOp, record cbg.CBORMarshaler) bool {
r, err := set.Contains(ctx, commit.Repo)
if err != nil {
return false
}
return !r
}
}