This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyper.go
289 lines (260 loc) · 7.2 KB
/
typer.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
281
282
283
284
285
286
287
288
289
package handlers
import (
"fmt"
"net/http"
"path"
"strings"
pub "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
)
// CollectionType
type CollectionType string
// CollectionTypes
type CollectionTypes []CollectionType
const (
Unknown = CollectionType("")
Outbox = CollectionType("outbox")
Inbox = CollectionType("inbox")
Shares = CollectionType("shares")
Replies = CollectionType("replies") // activitystreams
Following = CollectionType("following")
Followers = CollectionType("followers")
Liked = CollectionType("liked")
Likes = CollectionType("likes")
)
// Typer is the static package variable that determines a collection type for a particular request
// It can be overloaded from outside packages.
// @TODO(marius): This should be moved as a property on an instantiable package object, instead of keeping it here
var Typer CollectionTyper = pathTyper{}
// CollectionTyper allows external packages to tell us which collection the current HTTP request addresses
type CollectionTyper interface {
Type(r *http.Request) CollectionType
}
type pathTyper struct{}
func (d pathTyper) Type(r *http.Request) CollectionType {
if r.URL == nil || len(r.URL.Path) == 0 {
return Unknown
}
col := Unknown
pathElements := strings.Split(r.URL.Path[1:], "/") // Skip first /
for i := len(pathElements) - 1; i >= 0; i-- {
col = CollectionType(pathElements[i])
if typ := getValidActivityCollection(col); typ != Unknown {
return typ
}
if typ := getValidObjectCollection(col); typ != Unknown {
return typ
}
}
return col
}
var (
validActivityCollection = CollectionTypes{
Outbox,
Inbox,
Likes,
Shares,
Replies, // activitystreams
}
OnObject = CollectionTypes{
Likes,
Shares,
Replies,
}
OnActor = CollectionTypes{
Outbox,
Inbox,
Liked,
Following,
Followers,
}
ActivityPubCollections = CollectionTypes{
Outbox,
Inbox,
Liked,
Following,
Followers,
Likes,
Shares,
Replies,
}
)
func (t CollectionTypes) Contains(typ CollectionType) bool {
for _, tt := range t {
if strings.ToLower(string(typ)) == string(tt) {
return true
}
}
return false
}
// Split splits the IRI in an actor IRI and its collection
// if the collection is found in the elements in the t CollectionTypes slice
func (t CollectionTypes) Split(i pub.IRI) (pub.IRI, CollectionType) {
maybeActor, maybeCol := path.Split(i.String())
tt := CollectionType(maybeCol)
if !t.Contains(tt) {
tt = ""
maybeActor = i.String()
}
iri := pub.IRI(strings.TrimRight(maybeActor, "/"))
return iri, tt
}
// IRIf formats an IRI from an existing IRI and the collection type
func IRIf(i pub.IRI, t CollectionType) pub.IRI {
onePastLast := len(i)
if onePastLast > 1 && i[onePastLast-1] == '/' {
i = i[:onePastLast-1]
}
return pub.IRI(fmt.Sprintf("%s/%s", i, t))
}
// IRI gives us the IRI of the t collection type corresponding to the i Item,
// or generates a new one if not found.
func (t CollectionType) IRI(i pub.Item) pub.IRI {
if pub.IsNil(i) {
return IRIf("", t)
}
if pub.IsObject(i) {
if it := t.Of(i); !pub.IsNil(it) {
return it.GetLink()
}
}
return IRIf(i.GetLink(), t)
}
// Of gives us the property of the i Item that corresponds to the t collection type.
func (t CollectionType) Of(i pub.Item) pub.Item {
if pub.IsNil(i) || !i.IsObject() {
return nil
}
var it pub.Item
if OnActor.Contains(t) && pub.ActorTypes.Contains(i.GetType()) {
pub.OnActor(i, func(a *pub.Actor) error {
switch t {
case Inbox:
it = a.Inbox
case Outbox:
it = a.Outbox
case Liked:
it = a.Liked
case Following:
it = a.Following
case Followers:
it = a.Followers
}
return nil
})
}
pub.OnObject(i, func(o *pub.Object) error {
switch t {
case Likes:
it = o.Likes
case Shares:
it = o.Shares
case Replies:
it = o.Replies
}
return nil
})
return it
}
// OfActor returns the base IRI of received i, if i represents an IRI matching collection type t
func (t CollectionType) OfActor(i pub.IRI) (pub.IRI, error) {
maybeActor, maybeCol := path.Split(i.String())
if strings.ToLower(maybeCol) == strings.ToLower(string(t)) {
maybeActor = strings.TrimRight(maybeActor, "/")
return pub.IRI(maybeActor), nil
}
return pub.EmptyIRI, errors.Newf("IRI does not represent a valid %s collection", t)
}
// Split returns the base IRI of received i, if i represents an IRI matching collection type t
func Split(i pub.IRI) (pub.IRI, CollectionType) {
return ActivityPubCollections.Split(i)
}
func getValidActivityCollection(t CollectionType) CollectionType {
if validActivityCollection.Contains(t) {
return t
}
return Unknown
}
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities
func ValidActivityCollection(typ CollectionType) bool {
return getValidActivityCollection(typ) != Unknown
}
var validObjectCollection = []CollectionType{
Following,
Followers,
Liked,
}
func getValidObjectCollection(typ CollectionType) CollectionType {
for _, t := range validObjectCollection {
if strings.ToLower(string(typ)) == string(t) {
return t
}
}
return Unknown
}
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Objects
func ValidObjectCollection(typ CollectionType) bool {
return getValidObjectCollection(typ) != Unknown
}
func getValidCollection(typ CollectionType) CollectionType {
if typ := getValidActivityCollection(typ); typ != Unknown {
return typ
}
if typ := getValidObjectCollection(typ); typ != Unknown {
return typ
}
return Unknown
}
func ValidCollection(typ CollectionType) bool {
return getValidCollection(typ) != Unknown
}
func ValidCollectionIRI(i pub.IRI) bool {
_, t := Split(i)
return getValidCollection(t) != Unknown
}
// AddTo adds collection type IRI on the corresponding property of the i Item
func (t CollectionType) AddTo(i pub.Item) (pub.IRI, bool) {
if pub.IsNil(i) || !i.IsObject() {
return pub.NilIRI, false
}
status := false
var iri pub.IRI
if OnActor.Contains(t) {
pub.OnActor(i, func(a *pub.Actor) error {
if status = t == Inbox && pub.IsNil(a.Inbox); status {
a.Inbox = IRIf(a.GetLink(), t)
iri = a.Inbox.GetLink()
} else if status = t == Outbox && pub.IsNil(a.Outbox); status {
a.Outbox = IRIf(a.GetLink(), t)
iri = a.Outbox.GetLink()
} else if status = t == Liked && pub.IsNil(a.Liked); status {
a.Liked = IRIf(a.GetLink(), t)
iri = a.Liked.GetLink()
} else if status = t == Following && pub.IsNil(a.Following); status {
a.Following = IRIf(a.GetLink(), t)
iri = a.Following.GetLink()
} else if status = t == Followers && pub.IsNil(a.Followers); status {
a.Followers = IRIf(a.GetLink(), t)
iri = a.Followers.GetLink()
}
return nil
})
} else if OnObject.Contains(t) {
pub.OnObject(i, func(o *pub.Object) error {
if status = t == Likes && pub.IsNil(o.Likes); status {
o.Likes = IRIf(o.GetLink(), t)
iri = o.Likes.GetLink()
} else if status = t == Shares && pub.IsNil(o.Shares); status {
o.Shares = IRIf(o.GetLink(), t)
iri = o.Shares.GetLink()
} else if status = t == Replies && pub.IsNil(o.Replies); status {
o.Replies = IRIf(o.GetLink(), t)
iri = o.Replies.GetLink()
}
return nil
})
} else {
iri = IRIf(i.GetLink(), t)
}
return iri, status
}