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
package relay
import (
"fmt"
"net/url"
"strings"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/events"
)
const (
InfoFrameType = "com.atproto.sync.subscribeRepos#info"
wildcardSuffix = ".*"
wildcardProbe = ".a"
)
type SubscribeFilter struct {
Collections []string
DIDs []string
dids map[string]bool
}
type serverFilterInfo struct {
Type string `json:"$type"`
Name string `json:"name"`
Message string `json:"message"`
WantedCollections []string `json:"wantedCollections"`
WantedDIDs []string `json:"wantedDids"`
}
func ParseSubscribeFilter(query url.Values) (SubscribeFilter, error) {
var f SubscribeFilter
seen := map[string]bool{}
for _, raw := range query[events.WantedCollectionsParam] {
c := strings.TrimSpace(raw)
if c == "" || seen[c] {
continue
}
if err := validCollection(c); err != nil {
return SubscribeFilter{}, err
}
seen[c] = true
f.Collections = append(f.Collections, c)
}
if len(f.Collections) > events.MaxServerFilterCollections {
return SubscribeFilter{}, fmt.Errorf("at most %d %s", events.MaxServerFilterCollections, events.WantedCollectionsParam)
}
for _, raw := range query[events.WantedDIDsParam] {
d := strings.TrimSpace(raw)
if d == "" || f.dids[d] {
continue
}
if _, err := syntax.ParseDID(d); err != nil {
return SubscribeFilter{}, fmt.Errorf("%s %q is not a DID", events.WantedDIDsParam, d)
}
if f.dids == nil {
f.dids = map[string]bool{}
}
f.dids[d] = true
f.DIDs = append(f.DIDs, d)
}
if len(f.DIDs) > events.MaxServerFilterDIDs {
return SubscribeFilter{}, fmt.Errorf("at most %d %s", events.MaxServerFilterDIDs, events.WantedDIDsParam)
}
return f, nil
}
func validCollection(c string) error {
probe := c
if prefix, ok := strings.CutSuffix(c, wildcardSuffix); ok {
probe = prefix + wildcardProbe
}
if _, err := syntax.ParseNSID(probe); err != nil {
return fmt.Errorf("%s %q is not an NSID or an NSID prefix ending in .*", events.WantedCollectionsParam, c)
}
return nil
}
func (f SubscribeFilter) Active() bool {
return len(f.Collections) > 0 || len(f.DIDs) > 0
}
func (f SubscribeFilter) Apply(frame Frame) (Frame, bool) {
if len(f.dids) > 0 && !f.dids[frame.Repo] {
return Frame{}, false
}
if frame.Type != CommitFrameType || len(f.Collections) == 0 {
return frame, true
}
kept := make([]Op, 0, len(frame.Ops))
for _, op := range frame.Ops {
if collection, ok := splitRecordPath(op.Path); ok && events.MatchCollection(f.Collections, collection) {
kept = append(kept, op)
}
}
if len(kept) == 0 {
return Frame{}, false
}
frame.Ops = kept
return frame, true
}
func (f SubscribeFilter) info() serverFilterInfo {
return serverFilterInfo{Type: InfoFrameType, Name: events.ServerFilterInfoName,
Message: fmt.Sprintf("%s=%d %s=%d", events.WantedCollectionsParam, len(f.Collections), events.WantedDIDsParam, len(f.DIDs)),
WantedCollections: append([]string{}, f.Collections...), WantedDIDs: append([]string{}, f.DIDs...)}
}