-
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
package handler
import (
"net/http"
"strings"
)
// CollectionType
type CollectionType string
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
}
var col string
pathElements := strings.Split(r.URL.Path[1:], "/") // Skip first /
for i := len(pathElements) - 1; i >= 0; i-- {
col = pathElements[i]
if typ := getValidActivityCollection(col); typ != Unknown {
return typ
}
if typ := getValidObjectCollection(col); typ != Unknown {
return typ
}
}
return CollectionType(strings.ToLower(col))
}
var validActivityCollection = []CollectionType{
Outbox,
Inbox,
Likes,
Shares,
Replies, // activitystreams
}
func getValidActivityCollection(typ string) CollectionType {
for _, t := range validActivityCollection {
if strings.ToLower(typ) == string(t) {
return t
}
}
return Unknown
}
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities
func ValidActivityCollection(typ string) bool {
return getValidActivityCollection(typ) != Unknown
}
var validObjectCollection = []CollectionType{
Following,
Followers,
Liked,
}
func getValidObjectCollection(typ string) CollectionType {
for _, t := range validObjectCollection {
if strings.ToLower(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 string) bool {
return getValidObjectCollection(typ) != Unknown
}
func getValidCollection(typ string) CollectionType {
if typ := getValidActivityCollection(typ); typ != Unknown {
return typ
}
if typ := getValidObjectCollection(typ); typ != Unknown {
return typ
}
return Unknown
}
func ValidCollection(typ string) bool {
return getValidCollection(typ) != Unknown
}