Debian Codegraph @

typer.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "path/filepath"
5 "strings"
6
7 "github.com/go-ap/errors"
8 )
9
10 // CollectionPath
11 type CollectionPath string
12
13 // CollectionPaths
14 type CollectionPaths []CollectionPath
15
16 const (
17 Unknown = CollectionPath("")
18 // Outbox
19 //
20 // https://www.w3.org/TR/activitypub/#outbox
21 //
22 // The outbox is discovered through the outbox property of an actor's profile.
23 // The outbox MUST be an OrderedCollection.
24 //
25 // The outbox stream contains activities the user has published, subject to the ability of the requestor
26 // to retrieve the activity (that is, the contents of the outbox are filtered by the permissions
27 // of the person reading it). If a user submits a request without Authorization the server should respond
28 // with all of the Public posts. This could potentially be all relevant objects published by the user,
29 // though the number of available items is left to the discretion of those implementing and deploying the server.
30 //
31 // The outbox accepts HTTP POST requests, with behaviour described in Client to Server Interactions.
32 Outbox = CollectionPath("outbox")
33 // Inbox
34 //
35 // https://www.w3.org/TR/activitypub/#inbox
36 //
37 // The inbox is discovered through the inbox property of an actor's profile. The inbox MUST be an OrderedCollection.
38 //
39 // The inbox stream contains all activities received by the actor. The server SHOULD filter content according
40 // to the requester's permission. In general, the owner of an inbox is likely to be able to access
41 // all of their inbox contents. Depending on access control, some other content may be public,
42 // whereas other content may require authentication for non-owner users, if they can access the inbox at all.
43 //
44 // The server MUST perform de-duplication of activities returned by the inbox. Duplication can occur
45 // if an activity is addressed both to an actor's followers, and a specific actor who also follows
46 // the recipient actor, and the server has failed to de-duplicate the recipients list.
47 // Such deduplication MUST be performed by comparing the id of the activities and dropping any activities already seen.
48 //
49 // The inboxes of actors on federated servers accepts HTTP POST requests, with behaviour described in Delivery.
50 // Non-federated servers SHOULD return a 405 Method Not Allowed upon receipt of a POST request.
51 Inbox = CollectionPath("inbox")
52 // Followers
53 //
54 // https://www.w3.org/TR/activitypub/#followers
55 //
56 // Every actor SHOULD have a followers collection. This is a list of everyone who has sent a Follow activity
57 // for the actor, added as a side effect. This is where one would find a list of all the actors that are following
58 // the actor. The followers collection MUST be either an OrderedCollection or a Collection and MAY be filtered
59 // on privileges of an authenticated user or as appropriate when no authentication is given.
60 //
61 // NOTE: Default for notification targeting
62 // The follow activity generally is a request to see the objects an actor creates.
63 // This makes the Followers collection an appropriate default target for delivery of notifications.
64 Followers = CollectionPath("followers")
65 // Following
66 //
67 // https://www.w3.org/TR/activitypub/#following
68 //
69 // Every actor SHOULD have a following collection. This is a list of everybody that the actor has followed,
70 // added as a side effect. The following collection MUST be either an OrderedCollection or a Collection
71 // and MAY be filtered on privileges of an authenticated user or as appropriate when no authentication is given.
72 Following = CollectionPath("following")
73 // Liked
74 //
75 // https://www.w3.org/TR/activitypub/#liked
76 //
77 // Every actor MAY have a liked collection. This is a list of every object from all of the actor's Like activities,
78 // added as a side effect. The liked collection MUST be either an OrderedCollection or a Collection and
79 // MAY be filtered on privileges of an authenticated user or as appropriate when no authentication is given.
80 Liked = CollectionPath("liked")
81 // Likes
82 //
83 // https://www.w3.org/TR/activitypub/#likes
84 //
85 // Every object MAY have a likes collection. This is a list of all Like activities with this object as the object
86 // property, added as a side effect. The likes collection MUST be either an OrderedCollection or a Collection
87 // and MAY be filtered on privileges of an authenticated user or as appropriate when no authentication is given.
88 //
89 // NOTE
90 // Care should be taken to not confuse the the likes collection with the similarly named but different liked
91 // collection. In sum:
92 //
93 // * liked: Specifically a property of actors. This is a collection of Like activities performed by the actor,
94 // added to the collection as a side effect of delivery to the outbox.
95 // * likes: May be a property of any object. This is a collection of Like activities referencing this object,
96 // added to the collection as a side effect of delivery to the inbox.
97 Likes = CollectionPath("likes")
98 // Shares
99 //
100 // https://www.w3.org/TR/activitypub/#shares
101 //
102 // Every object MAY have a shares collection. This is a list of all Announce activities with this object
103 // as the object property, added as a side effect. The shares collection MUST be either an OrderedCollection
104 // or a Collection and MAY be filtered on privileges of an authenticated user or as appropriate when
105 // no authentication is given.
106 Shares = CollectionPath("shares")
107 Replies = CollectionPath("replies") // activitystreams
108 )
109
110 var (
111 validActivityCollection = CollectionPaths{
112 Outbox,
113 Inbox,
114 Likes,
115 Shares,
116 Replies, // activitystreams
117 }
118 OfObject = CollectionPaths{
119 Likes,
120 Shares,
121 Replies,
122 }
123 OfActor = CollectionPaths{
124 Outbox,
125 Inbox,
126 Liked,
127 Following,
128 Followers,
129 }
130
131 ActivityPubCollections = CollectionPaths{
132 Outbox,
133 Inbox,
134 Liked,
135 Following,
136 Followers,
137 Likes,
138 Shares,
139 Replies,
140 }
141 )
142
143 func (t CollectionPaths) Contains(typ CollectionPath) bool {
144 for _, tt := range t {
145 if strings.EqualFold(string(typ), string(tt)) {
146 return true
147 }
148 }
149 return false
150 }
151
152 // Split splits the IRI in an actor IRI and its CollectionPath
153 // if the CollectionPath is found in the elements in the t CollectionPaths slice
154 func (t CollectionPaths) Split(i IRI) (IRI, CollectionPath) {
155 if u, err := i.URL(); err == nil {
156 maybeActor, maybeCol := filepath.Split(u.Path)
157 if len(maybeActor) == 0 {
158 return i, Unknown
159 }
160 tt := CollectionPath(maybeCol)
161 if !t.Contains(tt) {
162 tt = ""
163 }
164 u.Path = strings.TrimRight(maybeActor, "/")
165 iri := IRI(u.String())
166 return iri, tt
167 }
168 maybeActor, maybeCol := filepath.Split(i.String())
169 if len(maybeActor) == 0 {
170 return i, Unknown
171 }
172 tt := CollectionPath(maybeCol)
173 if !t.Contains(tt) {
174 return i, Unknown
175 }
176 maybeActor = strings.TrimRight(maybeActor, "/")
177 return IRI(maybeActor), tt
178 }
179
180 // IRIf formats an IRI from an existing IRI and the CollectionPath type
181 func IRIf(i IRI, t CollectionPath) IRI {
182 si := i.String()
183 s := strings.Builder{}
184 _, _ = s.WriteString(si)
185 if l := len(si); l == 0 || si[l-1] != '/' {
186 _, _ = s.WriteRune('/')
187 }
188 _, _ = s.WriteString(string(t))
189 return IRI(s.String())
190 }
191
192 // IRI gives us the IRI of the t CollectionPath type corresponding to the i Item,
193 // or generates a new one if not found.
194 func (t CollectionPath) IRI(i Item) IRI {
195 if IsNil(i) {
196 return IRIf("", t)
197 }
198 if IsObject(i) {
199 if it := t.Of(i); !IsNil(it) {
200 return it.GetLink()
201 }
202 }
203 return IRIf(i.GetLink(), t)
204 }
205
206 func (t CollectionPath) ofItemCollection(col ItemCollection) Item {
207 iriCol := make(ItemCollection, len(col))
208 for i, it := range col {
209 iriCol[i] = t.Of(it)
210 }
211 return iriCol
212 }
213
214 func (t CollectionPath) ofObject(ob *Object) Item {
215 var it Item
216 switch t {
217 case Likes:
218 it = ob.Likes
219 case Shares:
220 it = ob.Shares
221 case Replies:
222 it = ob.Replies
223 }
224 return it
225 }
226
227 func (t CollectionPath) ofActor(a *Actor) Item {
228 var it Item
229 switch t {
230 case Inbox:
231 it = a.Inbox
232 case Outbox:
233 it = a.Outbox
234 case Liked:
235 it = a.Liked
236 case Following:
237 it = a.Following
238 case Followers:
239 it = a.Followers
240 }
241 return it
242 }
243
244 func (t CollectionPath) ofIRI(iri IRI) Item {
245 if len(iri) == 0 {
246 return nil
247 }
248 return iri.AddPath(string(t))
249 }
250
251 func (t CollectionPath) ofItem(i Item) Item {
252 if IsNil(i) {
253 return nil
254 }
255 var it Item
256 if IsIRI(i) {
257 it = t.ofIRI(i.GetLink())
258 }
259 if IsItemCollection(i) {
260 _ = OnItemCollection(i, func(col *ItemCollection) error {
261 it = t.ofItemCollection(*col)
262 return nil
263 })
264 }
265 if OfActor.Contains(t) && ActorTypes.Match(i.GetType()) {
266 _ = OnActor(i, func(a *Actor) error {
267 it = t.ofActor(a)
268 return nil
269 })
270 } else {
271 _ = OnObject(i, func(o *Object) error {
272 it = t.ofObject(o)
273 return nil
274 })
275 }
276 return it
277 }
278
279 // Of returns the property of the i [Item] that corresponds to the t [CollectionPath] type.
280 // If it's missing, it returns nil.
281 func (t CollectionPath) Of(i Item) Item {
282 return t.ofItem(i)
283 }
284
285 // OfActor returns the base IRI of received i, if i represents an IRI matching CollectionPath type t
286 func (t CollectionPath) OfActor(i IRI) (IRI, error) {
287 maybeActor, maybeCol := filepath.Split(i.String())
288 if strings.EqualFold(maybeCol, string(t)) {
289 maybeActor = strings.TrimRight(maybeActor, "/")
290 return IRI(maybeActor), nil
291 }
292 return EmptyIRI, errors.Newf("IRI does not represent a valid %s CollectionPath", t)
293 }
294
295 // Split returns the base IRI of received i, if i represents an IRI matching CollectionPath type t
296 func Split(i IRI) (IRI, CollectionPath) {
297 return ActivityPubCollections.Split(i)
298 }
299
300 func getValidActivityCollection(t CollectionPath) CollectionPath {
301 if validActivityCollection.Contains(t) {
302 return t
303 }
304 return Unknown
305 }
306
307 // ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities
308 func ValidActivityCollection(typ CollectionPath) bool {
309 return getValidActivityCollection(typ) != Unknown
310 }
311
312 var validObjectCollection = []CollectionPath{
313 Following,
314 Followers,
315 Liked,
316 }
317
318 func getValidObjectCollection(typ CollectionPath) CollectionPath {
319 for _, t := range validObjectCollection {
320 if strings.EqualFold(string(typ), string(t)) {
321 return t
322 }
323 }
324 return Unknown
325 }
326
327 // ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Objects
328 func ValidObjectCollection(typ CollectionPath) bool {
329 return getValidObjectCollection(typ) != Unknown
330 }
331
332 func getValidCollection(typ CollectionPath) CollectionPath {
333 if typ := getValidActivityCollection(typ); typ != Unknown {
334 return typ
335 }
336 if typ := getValidObjectCollection(typ); typ != Unknown {
337 return typ
338 }
339 return Unknown
340 }
341
342 func ValidCollection(typ CollectionPath) bool {
343 return getValidCollection(typ) != Unknown
344 }
345
346 func ValidCollectionIRI(i IRI) bool {
347 _, t := Split(i)
348 return getValidCollection(t) != Unknown
349 }
350
351 // AddTo adds CollectionPath type IRI on the corresponding property of the i Item
352 func (t CollectionPath) AddTo(i Item) (IRI, bool) {
353 if IsNil(i) || !i.IsObject() {
354 return NilIRI, false
355 }
356 status := false
357 var iri IRI
358 if OfActor.Contains(t) {
359 OnActor(i, func(a *Actor) error {
360 if status = t == Inbox && IsNil(a.Inbox); status {
361 a.Inbox = IRIf(a.GetLink(), t)
362 iri = a.Inbox.GetLink()
363 } else if status = t == Outbox && IsNil(a.Outbox); status {
364 a.Outbox = IRIf(a.GetLink(), t)
365 iri = a.Outbox.GetLink()
366 } else if status = t == Liked && IsNil(a.Liked); status {
367 a.Liked = IRIf(a.GetLink(), t)
368 iri = a.Liked.GetLink()
369 } else if status = t == Following && IsNil(a.Following); status {
370 a.Following = IRIf(a.GetLink(), t)
371 iri = a.Following.GetLink()
372 } else if status = t == Followers && IsNil(a.Followers); status {
373 a.Followers = IRIf(a.GetLink(), t)
374 iri = a.Followers.GetLink()
375 }
376 return nil
377 })
378 } else if OfObject.Contains(t) {
379 OnObject(i, func(o *Object) error {
380 if status = t == Likes && IsNil(o.Likes); status {
381 o.Likes = IRIf(o.GetLink(), t)
382 iri = o.Likes.GetLink()
383 } else if status = t == Shares && IsNil(o.Shares); status {
384 o.Shares = IRIf(o.GetLink(), t)
385 iri = o.Shares.GetLink()
386 } else if status = t == Replies && IsNil(o.Replies); status {
387 o.Replies = IRIf(o.GetLink(), t)
388 iri = o.Replies.GetLink()
389 }
390 return nil
391 })
392 } else {
393 iri = IRIf(i.GetLink(), t)
394 }
395 return iri, status
396 }