forked from go-ap/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.go
280 lines (254 loc) · 8.06 KB
/
pub.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package client
import (
"context"
"fmt"
"net/url"
"github.com/go-ap/errors"
vocab "github.com/mix/activitypub"
)
type FilterFn func() url.Values
type PubGetter interface {
Inbox(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Outbox(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Following(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Followers(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Likes(ctx context.Context, object vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Liked(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Replies(ctx context.Context, object vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error)
Collection(ctx context.Context, i vocab.IRI, filters ...FilterFn) (vocab.CollectionInterface, error)
Actor(ctx context.Context, iri vocab.IRI) (*vocab.Actor, error)
Activity(ctx context.Context, iri vocab.IRI) (*vocab.Activity, error)
Object(ctx context.Context, iri vocab.IRI) (*vocab.Object, error)
}
type PubSubmitter interface {
ToOutbox(ctx context.Context, a vocab.Item) (vocab.IRI, vocab.Item, error)
ToInbox(ctx context.Context, a vocab.Item) (vocab.IRI, vocab.Item, error)
}
type PubClient interface {
PubGetter
PubSubmitter
}
// Inbox
func (c C) Inbox(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateActor(actor); err != nil {
return nil, err
}
return c.collection(ctx, inbox(actor, filters...))
}
// Outbox
func (c C) Outbox(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateActor(actor); err != nil {
return nil, err
}
return c.collection(ctx, outbox(actor, filters...))
}
// Following
func (c C) Following(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateActor(actor); err != nil {
return nil, err
}
return c.collection(ctx, following(actor, filters...))
}
// Followers
func (c C) Followers(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateActor(actor); err != nil {
return nil, err
}
return c.collection(ctx, followers(actor, filters...))
}
// Likes
func (c C) Likes(ctx context.Context, object vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateObject(object); err != nil {
return nil, err
}
return c.collection(ctx, likes(object, filters...))
}
// Liked
func (c C) Liked(ctx context.Context, actor vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateActor(actor); err != nil {
return nil, err
}
return c.collection(ctx, liked(actor, filters...))
}
// Replies
func (c C) Replies(ctx context.Context, object vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateObject(object); err != nil {
return nil, err
}
return c.collection(ctx, replies(object, filters...))
}
// Shares
func (c C) Shares(ctx context.Context, object vocab.Item, filters ...FilterFn) (vocab.CollectionInterface, error) {
if err := validateObject(object); err != nil {
return nil, err
}
return c.collection(ctx, shares(object, filters...))
}
// Collection
func (c C) Collection(ctx context.Context, i vocab.IRI, filters ...FilterFn) (vocab.CollectionInterface, error) {
return c.collection(ctx, iri(i, filters...))
}
// Actor
func (c C) Actor(ctx context.Context, iri vocab.IRI) (*vocab.Actor, error) {
it, err := c.object(ctx, iri)
if err != nil {
return nil, errors.Annotatef(err, "Unable to load Actor: %s", iri)
}
var person *vocab.Actor
vocab.OnActor(it, func(p *vocab.Actor) error {
person = p
return nil
})
return person, nil
}
// Activity
func (c C) Activity(ctx context.Context, iri vocab.IRI) (*vocab.Activity, error) {
it, err := c.object(ctx, iri)
if err != nil {
return nil, errors.Annotatef(err, "Unable to load Activity: %s", iri)
}
var activity *vocab.Activity
vocab.OnActivity(it, func(a *vocab.Activity) error {
activity = a
return nil
})
return activity, nil
}
// Object
func (c C) Object(ctx context.Context, iri vocab.IRI) (*vocab.Object, error) {
it, err := c.object(ctx, iri)
if err != nil {
return nil, errors.Annotatef(err, "Unable to load Object: %s", iri)
}
var object *vocab.Object
vocab.OnObject(it, func(o *vocab.Object) error {
object = o
return nil
})
return object, nil
}
func validateIRIForRequest(i vocab.IRI) error {
u, err := i.URL()
if err != nil {
return err
}
if u.Host == "" {
return errors.Newf("Host is empty")
}
return nil
}
func (c C) ToOutbox(ctx context.Context, a vocab.Item) (vocab.IRI, vocab.Item, error) {
var iri vocab.IRI
vocab.OnActivity(a, func(a *vocab.Activity) error {
iri = outbox(a.Actor)
return nil
})
if err := validateIRIForRequest(iri); err != nil {
return "", nil, errors.Annotatef(err, "Invalid Outbox IRI")
}
return c.CtxToCollection(ctx, iri, a)
}
func (c C) ToInbox(ctx context.Context, a vocab.Item) (vocab.IRI, vocab.Item, error) {
var iri vocab.IRI
vocab.OnActivity(a, func(a *vocab.Activity) error {
iri = inbox(a.Actor)
return nil
})
if err := validateIRIForRequest(iri); err != nil {
return "", nil, errors.Annotatef(err, "Invalid Inbox IRI")
}
return c.CtxToCollection(ctx, iri, a)
}
func (c C) collection(ctx context.Context, i vocab.IRI) (vocab.CollectionInterface, error) {
it, err := c.CtxLoadIRI(ctx, i)
if err != nil {
return nil, errors.Annotatef(err, "Unable to load IRI: %s", i)
}
if vocab.IsNil(it) {
return nil, errors.Newf("Unable to load IRI, nil item: %s", i)
}
var col vocab.CollectionInterface
typ := it.GetType()
if !vocab.CollectionTypes.Contains(it.GetType()) {
return nil, errors.Errorf("Response item type is not a valid collection: %s", typ)
}
var ok bool
switch typ {
case vocab.CollectionType:
col, ok = it.(*vocab.Collection)
case vocab.CollectionPageType:
col, ok = it.(*vocab.CollectionPage)
case vocab.OrderedCollectionType:
col, ok = it.(*vocab.OrderedCollection)
case vocab.OrderedCollectionPageType:
col, ok = it.(*vocab.OrderedCollectionPage)
}
if !ok {
return nil, errors.Errorf("Unable to convert item type %s to any of the collection types", typ)
}
return col, nil
}
func (c C) object(ctx context.Context, i vocab.IRI) (vocab.Item, error) {
return c.CtxLoadIRI(ctx, i)
}
func rawFilterQuery(f ...FilterFn) string {
if len(f) == 0 {
return ""
}
q := make(url.Values)
for _, ff := range f {
qq := ff()
for k, v := range qq {
q[k] = append(q[k], v...)
}
}
if len(q) == 0 {
return ""
}
return "?" + q.Encode()
}
func iri(i vocab.IRI, f ...FilterFn) vocab.IRI {
return vocab.IRI(fmt.Sprintf("%s%s", i, rawFilterQuery(f...)))
}
func inbox(a vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Inbox.IRI(a), f...)
}
func outbox(a vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Outbox.IRI(a), f...)
}
func following(a vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Following.IRI(a), f...)
}
func followers(a vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Followers.IRI(a), f...)
}
func liked(a vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Liked.IRI(a), f...)
}
func likes(o vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Likes.IRI(o), f...)
}
func shares(o vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Shares.IRI(o), f...)
}
func replies(o vocab.Item, f ...FilterFn) vocab.IRI {
return iri(vocab.Replies.IRI(o), f...)
}
func validateActor(a vocab.Item) error {
if vocab.IsNil(a) {
return errors.Errorf("Actor is nil")
}
if a.IsObject() && !vocab.ActorTypes.Contains(a.GetType()) {
return errors.Errorf("Invalid Actor type %s", a.GetType())
}
return nil
}
func validateObject(o vocab.Item) error {
if vocab.IsNil(o) {
return errors.Errorf("object is nil")
}
if o.IsObject() && !vocab.ObjectTypes.Contains(o.GetType()) {
return errors.Errorf("invalid Object type %q", o.GetType())
}
return nil
}