This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/typer.go

292 lines
7.3 KiB
Go
Raw Normal View History

package handlers
import (
"fmt"
"net/http"
"path"
"strings"
2022-03-18 13:02:09 +00:00
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,
}
)
2020-04-13 10:02:50 +00:00
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)
2021-01-24 16:13:42 +00:00
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 {
2022-05-23 21:05:36 +00:00
if pub.IsNil(i) {
return IRIf("", t)
}
2022-05-23 21:36:56 +00:00
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() {
2022-05-23 18:47:23 +00:00
return nil
}
var it pub.Item
pub.OnActor(i, func(a *pub.Actor) error {
if t == Inbox && a.Inbox != nil {
it = a.Inbox
}
if t == Outbox && a.Outbox != nil {
it = a.Outbox
}
if t == Liked && a.Liked != nil {
it = a.Liked
}
if t == Following && a.Following != nil {
it = a.Following
}
if t == Followers && a.Followers != nil {
it = a.Followers
}
return nil
})
pub.OnObject(i, func(o *pub.Object) error {
if t == Likes && o.Likes != nil {
it = o.Likes
}
if t == Shares && o.Shares != nil {
it = o.Shares
}
if t == Replies && o.Replies != nil {
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
}
2020-06-15 09:36:15 +00:00
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) {
2021-02-02 13:21:45 +00:00
if pub.IsNil(i) || !i.IsObject() {
return pub.NilIRI, false
}
2020-05-19 18:10:40 +00:00
status := false
var iri pub.IRI
if OnActor.Contains(t) {
pub.OnActor(i, func(a *pub.Actor) error {
2021-02-02 13:21:45 +00:00
if status = t == Inbox && pub.IsNil(a.Inbox); status {
a.Inbox = IRIf(a.GetLink(), t)
iri = a.Inbox.GetLink()
2021-02-02 13:21:45 +00:00
} else if status = t == Outbox && pub.IsNil(a.Outbox); status {
a.Outbox = IRIf(a.GetLink(), t)
iri = a.Outbox.GetLink()
2021-02-02 13:21:45 +00:00
} else if status = t == Liked && pub.IsNil(a.Liked); status {
a.Liked = IRIf(a.GetLink(), t)
iri = a.Liked.GetLink()
2021-02-02 13:21:45 +00:00
} else if status = t == Following && pub.IsNil(a.Following); status {
a.Following = IRIf(a.GetLink(), t)
iri = a.Following.GetLink()
2021-02-02 13:21:45 +00:00
} 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 {
2021-02-02 13:21:45 +00:00
if status = t == Likes && pub.IsNil(o.Likes); status {
o.Likes = IRIf(o.GetLink(), t)
iri = o.Likes.GetLink()
2021-02-02 13:21:45 +00:00
} else if status = t == Shares && pub.IsNil(o.Shares); status {
o.Shares = IRIf(o.GetLink(), t)
iri = o.Shares.GetLink()
2021-02-02 13:21:45 +00:00
} else if status = t == Replies && pub.IsNil(o.Replies); status {
o.Replies = IRIf(o.GetLink(), t)
iri = o.Replies.GetLink()
}
return nil
})
2020-06-10 19:49:30 +00:00
} else {
iri = IRIf(i.GetLink(), t)
}
return iri, status
}