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
package events
import (
"context"
"errors"
"slices"
"strings"
"time"
)
const (
CapabilitySubscribe = "events.subscribe"
CapabilityDeliver = "events.deliver"
HandlerKind = "EventHandler"
RecordWatchKind = "AtprotoRecordWatch"
ScopeAll = "*"
)
var ErrFutureCursor = errors.New("events: the cursor is past the source's newest event")
type Op struct {
Action string
Path string
CID string
Prev string
}
type Event struct {
Source string
Seq int64
Repo string
Commit string
Rev string
Time time.Time
Ops []Op
ServerFilter *ServerFilter
}
type ServerFilterSupport string
const (
ServerFilterSupported ServerFilterSupport = "true"
ServerFilterUnsupported ServerFilterSupport = "false"
ServerFilterUnknown ServerFilterSupport = "unknown"
MaxServerFilterCollections = 100
MaxServerFilterDIDs = 10000
WantedCollectionsParam = "wantedCollections"
WantedDIDsParam = "wantedDids"
ServerFilterInfoName = "ServerFilter"
collectionWildcard = ".*"
logDIDs = 10
)
func MatchCollection(patterns []string, collection string) bool {
for _, p := range patterns {
p = strings.TrimSpace(p)
if prefix, ok := strings.CutSuffix(p, collectionWildcard); ok {
if strings.HasPrefix(collection, prefix+".") {
return true
}
continue
}
if p == collection {
return true
}
}
return false
}
type ServerFilter struct {
Collections []string
DIDs []string
Supported ServerFilterSupport
}
func (f *ServerFilter) Gapless() bool {
return f != nil && f.Supported == ServerFilterSupported
}
type Filter struct {
Collections []string
DIDs []string
Cursor *int64
}
type Stream interface {
Next(ctx context.Context) (Event, error)
Close() error
}
type SavedCursor struct {
Cursor int64
SavedAt time.Time
}
type CursorStore interface {
Load(workspace, name, uid string) (SavedCursor, bool)
Save(workspace, name, uid string, cursor int64) error
}
type Source interface {
Subscribe(ctx context.Context, filter Filter) (Stream, error)
}
type Sink interface {
Deliver(ctx context.Context, event Event) error
}
func (f Filter) Scope() []string {
var out []string
for _, v := range append(append([]string(nil), f.Collections...), f.DIDs...) {
if v = strings.TrimSpace(v); v != "" && !slices.Contains(out, v) {
out = append(out, v)
}
}
if len(out) == 0 {
return []string{ScopeAll}
}
return out
}
func (f Filter) ServerParams() (collections, dids []string) {
collections, dids = trimmedUnique(f.Collections), trimmedUnique(f.DIDs)
if len(collections) > MaxServerFilterCollections {
collections = nil
}
if len(dids) > MaxServerFilterDIDs {
dids = nil
}
return collections, dids
}
func LogDIDs(dids []string) []string {
if len(dids) > logDIDs {
return dids[:logDIDs]
}
return dids
}
func trimmedUnique(in []string) []string {
var out []string
seen := map[string]bool{}
for _, v := range in {
if v = strings.TrimSpace(v); v != "" && !seen[v] {
seen[v] = true
out = append(out, v)
}
}
return out
}
func (f Filter) Narrow(e Event) Event {
if len(f.DIDs) > 0 && !slices.ContainsFunc(f.DIDs, func(d string) bool { return strings.TrimSpace(d) == e.Repo }) {
e.Ops = nil
return e
}
if len(f.Collections) == 0 {
return e
}
kept := make([]Op, 0, len(e.Ops))
for _, op := range e.Ops {
collection, _, _ := strings.Cut(op.Path, "/")
if MatchCollection(f.Collections, collection) {
kept = append(kept, op)
}
}
e.Ops = kept
return e
}