2019-05-31 13:47:52 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-03-26 10:53:47 +00:00
|
|
|
"fmt"
|
|
|
|
pub "github.com/go-ap/activitypub"
|
2020-06-13 13:45:28 +00:00
|
|
|
"github.com/go-ap/errors"
|
2019-05-31 13:47:52 +00:00
|
|
|
"net/http"
|
2020-06-13 13:45:28 +00:00
|
|
|
"path"
|
2019-05-31 13:47:52 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CollectionType
|
|
|
|
type CollectionType string
|
|
|
|
|
2020-03-26 10:53:47 +00:00
|
|
|
// CollectionTypes
|
|
|
|
type CollectionTypes []CollectionType
|
|
|
|
|
2019-05-31 13:47:52 +00:00
|
|
|
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.
|
2020-06-13 13:45:28 +00:00
|
|
|
// @TODO(marius): This should be moved as a property on an instantiable package object, instead of keeping it here
|
2019-05-31 13:47:52 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-15 09:35:37 +00:00
|
|
|
col := Unknown
|
2019-05-31 13:47:52 +00:00
|
|
|
pathElements := strings.Split(r.URL.Path[1:], "/") // Skip first /
|
|
|
|
for i := len(pathElements) - 1; i >= 0; i-- {
|
2020-06-15 09:35:37 +00:00
|
|
|
col = CollectionType(pathElements[i])
|
2019-05-31 13:47:52 +00:00
|
|
|
if typ := getValidActivityCollection(col); typ != Unknown {
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
if typ := getValidObjectCollection(col); typ != Unknown {
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:35:37 +00:00
|
|
|
return col
|
2019-05-31 13:47:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:45:28 +00:00
|
|
|
var (
|
|
|
|
validActivityCollection = CollectionTypes{
|
|
|
|
Outbox,
|
|
|
|
Inbox,
|
|
|
|
Likes,
|
|
|
|
Shares,
|
|
|
|
Replies, // activitystreams
|
|
|
|
}
|
|
|
|
OnObject = CollectionTypes{
|
|
|
|
Likes,
|
|
|
|
Shares,
|
|
|
|
Replies,
|
|
|
|
}
|
|
|
|
OnActor = CollectionTypes{
|
|
|
|
Outbox,
|
|
|
|
Inbox,
|
|
|
|
Liked,
|
|
|
|
Following,
|
|
|
|
Followers,
|
|
|
|
}
|
2020-03-26 10:53:47 +00:00
|
|
|
|
2020-06-13 13:45:28 +00:00
|
|
|
ActivityPubCollections = CollectionTypes{
|
|
|
|
Outbox,
|
|
|
|
Inbox,
|
|
|
|
Liked,
|
|
|
|
Following,
|
|
|
|
Followers,
|
|
|
|
Likes,
|
|
|
|
Shares,
|
|
|
|
Replies,
|
|
|
|
}
|
|
|
|
)
|
2020-04-13 10:02:50 +00:00
|
|
|
|
2020-03-26 10:53:47 +00:00
|
|
|
func (t CollectionTypes) Contains(typ CollectionType) bool {
|
|
|
|
for _, tt := range t {
|
|
|
|
if strings.ToLower(string(typ)) == string(tt) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:49:05 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:12:16 +00:00
|
|
|
// IRIf formats an IRI from an existing IRI and the collection type
|
|
|
|
func IRIf(i pub.IRI, t CollectionType) pub.IRI {
|
2021-01-24 14:45:46 +00:00
|
|
|
onePastLast := len(i)
|
|
|
|
if i[onePastLast-1] == '/' {
|
|
|
|
i = i[:onePastLast-1]
|
|
|
|
}
|
2020-05-19 16:12:16 +00:00
|
|
|
return pub.IRI(fmt.Sprintf("%s/%s", i, t))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IRI gives us the property of the i Item that corresponds to the t collection type
|
|
|
|
// or generates a new one if not found.
|
2020-03-26 10:53:47 +00:00
|
|
|
func (t CollectionType) IRI(i pub.Item) pub.IRI {
|
|
|
|
var iri pub.IRI
|
2020-04-05 11:18:57 +00:00
|
|
|
if i == nil {
|
|
|
|
return pub.EmptyIRI
|
|
|
|
}
|
2020-03-26 10:53:47 +00:00
|
|
|
if i.IsObject() {
|
2020-05-19 16:12:16 +00:00
|
|
|
if OnActor.Contains(t) {
|
2020-03-26 10:53:47 +00:00
|
|
|
pub.OnActor(i, func(a *pub.Actor) error {
|
|
|
|
if t == Inbox && a.Inbox != nil {
|
|
|
|
iri = a.Inbox.GetLink()
|
|
|
|
}
|
|
|
|
if t == Outbox && a.Outbox != nil {
|
|
|
|
iri = a.Outbox.GetLink()
|
|
|
|
}
|
|
|
|
if t == Liked && a.Liked != nil {
|
|
|
|
iri = a.Liked.GetLink()
|
|
|
|
}
|
|
|
|
if t == Following && a.Following != nil {
|
|
|
|
iri = a.Following.GetLink()
|
|
|
|
}
|
|
|
|
if t == Followers && a.Followers != nil {
|
|
|
|
iri = a.Followers.GetLink()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-05-19 16:12:16 +00:00
|
|
|
if OnObject.Contains(t) {
|
2020-03-26 10:53:47 +00:00
|
|
|
pub.OnObject(i, func(o *pub.Object) error {
|
|
|
|
if t == Likes && o.Likes != nil {
|
|
|
|
iri = o.Likes.GetLink()
|
|
|
|
}
|
|
|
|
if t == Shares && o.Shares != nil {
|
|
|
|
iri = o.Shares.GetLink()
|
|
|
|
}
|
|
|
|
if t == Replies && o.Replies != nil {
|
|
|
|
iri = o.Replies.GetLink()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-05-31 13:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 10:02:50 +00:00
|
|
|
|
2020-05-19 16:12:16 +00:00
|
|
|
if len(iri) > 0 {
|
|
|
|
return iri
|
2020-03-26 10:53:47 +00:00
|
|
|
}
|
2020-05-19 16:12:16 +00:00
|
|
|
return IRIf(i.GetLink(), t)
|
2020-03-26 10:53:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:45:28 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:49:05 +00:00
|
|
|
// Split returns the base IRI of received i, if i represents an IRI matching collection type t
|
2020-06-15 09:35:37 +00:00
|
|
|
func Split(i pub.IRI) (pub.IRI, CollectionType) {
|
2020-07-14 16:49:05 +00:00
|
|
|
return ActivityPubCollections.Split(i)
|
2020-06-15 09:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getValidActivityCollection(t CollectionType) CollectionType {
|
2020-03-26 10:53:47 +00:00
|
|
|
if validActivityCollection.Contains(t) {
|
|
|
|
return t
|
|
|
|
}
|
2019-05-31 13:47:52 +00:00
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities
|
2020-06-15 09:35:37 +00:00
|
|
|
func ValidActivityCollection(typ CollectionType) bool {
|
2019-05-31 13:47:52 +00:00
|
|
|
return getValidActivityCollection(typ) != Unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
var validObjectCollection = []CollectionType{
|
|
|
|
Following,
|
|
|
|
Followers,
|
|
|
|
Liked,
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:35:37 +00:00
|
|
|
func getValidObjectCollection(typ CollectionType) CollectionType {
|
2019-05-31 13:47:52 +00:00
|
|
|
for _, t := range validObjectCollection {
|
2020-06-15 09:35:37 +00:00
|
|
|
if strings.ToLower(string(typ)) == string(t) {
|
2019-05-31 13:47:52 +00:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Objects
|
2020-06-15 09:35:37 +00:00
|
|
|
func ValidObjectCollection(typ CollectionType) bool {
|
2019-05-31 13:47:52 +00:00
|
|
|
return getValidObjectCollection(typ) != Unknown
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:35:37 +00:00
|
|
|
func getValidCollection(typ CollectionType) CollectionType {
|
2019-05-31 13:47:52 +00:00
|
|
|
if typ := getValidActivityCollection(typ); typ != Unknown {
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
if typ := getValidObjectCollection(typ); typ != Unknown {
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
return Unknown
|
|
|
|
}
|
|
|
|
|
2020-06-15 09:35:37 +00:00
|
|
|
func ValidCollection(typ CollectionType) bool {
|
2019-05-31 13:47:52 +00:00
|
|
|
return getValidCollection(typ) != Unknown
|
|
|
|
}
|
2020-05-19 16:13:06 +00:00
|
|
|
|
2020-06-15 09:36:15 +00:00
|
|
|
func ValidCollectionIRI(i pub.IRI) bool {
|
|
|
|
_, t := Split(i)
|
|
|
|
return getValidCollection(t) != Unknown
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:13:06 +00:00
|
|
|
// AddTo adds collection type IRI on the corresponding property of the i Item
|
2020-05-23 17:47:48 +00:00
|
|
|
func (t CollectionType) AddTo(i pub.Item) (pub.IRI, bool) {
|
2020-05-19 18:10:40 +00:00
|
|
|
if i == nil || !i.IsObject() {
|
2020-05-23 17:47:48 +00:00
|
|
|
return pub.NilIRI, false
|
2020-05-19 16:13:06 +00:00
|
|
|
}
|
2020-05-19 18:10:40 +00:00
|
|
|
status := false
|
2020-05-23 17:47:48 +00:00
|
|
|
var iri pub.IRI
|
2020-05-19 16:13:06 +00:00
|
|
|
if OnActor.Contains(t) {
|
|
|
|
pub.OnActor(i, func(a *pub.Actor) error {
|
|
|
|
if status = t == Inbox && a.Inbox == nil; status {
|
|
|
|
a.Inbox = IRIf(a.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = a.Inbox.GetLink()
|
2020-06-13 13:45:28 +00:00
|
|
|
} else if status = t == Outbox && a.Outbox == nil; status {
|
2020-05-19 16:13:06 +00:00
|
|
|
a.Outbox = IRIf(a.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = a.Outbox.GetLink()
|
2020-06-13 13:45:28 +00:00
|
|
|
} else if status = t == Liked && a.Liked == nil; status {
|
2020-05-19 16:13:06 +00:00
|
|
|
a.Liked = IRIf(a.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = a.Liked.GetLink()
|
2020-06-13 13:45:28 +00:00
|
|
|
} else if status = t == Following && a.Following == nil; status {
|
2020-05-19 16:13:06 +00:00
|
|
|
a.Following = IRIf(a.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = a.Following.GetLink()
|
2020-05-19 16:13:06 +00:00
|
|
|
} else if status = t == Followers && a.Followers == nil; status {
|
|
|
|
a.Followers = IRIf(a.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = a.Followers.GetLink()
|
2020-05-19 16:13:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
} else if OnObject.Contains(t) {
|
|
|
|
pub.OnObject(i, func(o *pub.Object) error {
|
|
|
|
if status = t == Likes && o.Likes == nil; status {
|
|
|
|
o.Likes = IRIf(o.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = o.Likes.GetLink()
|
2020-05-19 16:13:06 +00:00
|
|
|
} else if status = t == Shares && o.Shares == nil; status {
|
|
|
|
o.Shares = IRIf(o.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = o.Shares.GetLink()
|
2020-05-19 16:13:06 +00:00
|
|
|
} else if status = t == Replies && o.Replies == nil; status {
|
|
|
|
o.Replies = IRIf(o.GetLink(), t)
|
2020-05-23 17:47:48 +00:00
|
|
|
iri = o.Replies.GetLink()
|
2020-05-19 16:13:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-10 19:49:30 +00:00
|
|
|
} else {
|
|
|
|
iri = IRIf(i.GetLink(), t)
|
2020-05-19 16:13:06 +00:00
|
|
|
}
|
2020-05-23 17:47:48 +00:00
|
|
|
return iri, status
|
2020-05-19 16:13:06 +00:00
|
|
|
}
|