Removed the collection and path typer functionality

This will be moved to the processing package
This commit is contained in:
Marius Orcsik 2022-05-30 14:48:13 +02:00
parent 051d30fa3f
commit 495ce2ea66
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
3 changed files with 44 additions and 81 deletions

1
go.mod
View file

@ -6,6 +6,5 @@ require (
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20200411073322-f0bcc40f0bf2 git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20200411073322-f0bcc40f0bf2
github.com/go-ap/errors v0.0.0-20220529131844-4c7dbeabb369 github.com/go-ap/errors v0.0.0-20220529131844-4c7dbeabb369
github.com/go-ap/jsonld v0.0.0-20200327122108-fafac2de2660 github.com/go-ap/jsonld v0.0.0-20200327122108-fafac2de2660
github.com/go-ap/storage v0.0.0-20220529132413-43d0dcf851c6
github.com/valyala/fastjson v1.6.3 github.com/valyala/fastjson v1.6.3
) )

110
typer.go
View file

@ -2,66 +2,30 @@ package activitypub
import ( import (
"fmt" "fmt"
"net/http"
"path" "path"
"strings" "strings"
"github.com/go-ap/errors" "github.com/go-ap/errors"
) )
// collectionPath // CollectionPath
type collectionPath string type CollectionPath string
// CollectionPaths // CollectionPaths
type CollectionPaths []collectionPath type CollectionPaths []CollectionPath
const ( const (
Unknown = collectionPath("") Unknown = CollectionPath("")
Outbox = collectionPath("outbox") Outbox = CollectionPath("outbox")
Inbox = collectionPath("inbox") Inbox = CollectionPath("inbox")
Shares = collectionPath("shares") Shares = CollectionPath("shares")
Replies = collectionPath("replies") // activitystreams Replies = CollectionPath("replies") // activitystreams
Following = collectionPath("following") Following = CollectionPath("following")
Followers = collectionPath("followers") Followers = CollectionPath("followers")
Liked = collectionPath("liked") Liked = CollectionPath("liked")
Likes = collectionPath("likes") Likes = CollectionPath("likes")
) )
func CollectionPath(s string) collectionPath {
return collectionPath(s)
}
// Typer is the static package variable that determines a collectionPath 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 collectionPath the current HTTP request addresses
type CollectionTyper interface {
Type(r *http.Request) collectionPath
}
type pathTyper struct{}
func (d pathTyper) Type(r *http.Request) collectionPath {
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 = collectionPath(pathElements[i])
if typ := getValidActivityCollection(col); typ != Unknown {
return typ
}
if typ := getValidObjectCollection(col); typ != Unknown {
return typ
}
}
return col
}
var ( var (
validActivityCollection = CollectionPaths{ validActivityCollection = CollectionPaths{
Outbox, Outbox,
@ -95,7 +59,7 @@ var (
} }
) )
func (t CollectionPaths) Contains(typ collectionPath) bool { func (t CollectionPaths) Contains(typ CollectionPath) bool {
for _, tt := range t { for _, tt := range t {
if strings.ToLower(string(typ)) == string(tt) { if strings.ToLower(string(typ)) == string(tt) {
return true return true
@ -104,11 +68,11 @@ func (t CollectionPaths) Contains(typ collectionPath) bool {
return false return false
} }
// Split splits the IRI in an actor IRI and its collectionPath // Split splits the IRI in an actor IRI and its CollectionPath
// if the collectionPath is found in the elements in the t CollectionPaths slice // if the CollectionPath is found in the elements in the t CollectionPaths slice
func (t CollectionPaths) Split(i IRI) (IRI, collectionPath) { func (t CollectionPaths) Split(i IRI) (IRI, CollectionPath) {
maybeActor, maybeCol := path.Split(i.String()) maybeActor, maybeCol := path.Split(i.String())
tt := collectionPath(maybeCol) tt := CollectionPath(maybeCol)
if !t.Contains(tt) { if !t.Contains(tt) {
tt = "" tt = ""
maybeActor = i.String() maybeActor = i.String()
@ -117,8 +81,8 @@ func (t CollectionPaths) Split(i IRI) (IRI, collectionPath) {
return iri, tt return iri, tt
} }
// IRIf formats an IRI from an existing IRI and the collectionPath type // IRIf formats an IRI from an existing IRI and the CollectionPath type
func IRIf(i IRI, t collectionPath) IRI { func IRIf(i IRI, t CollectionPath) IRI {
onePastLast := len(i) onePastLast := len(i)
if onePastLast > 1 && i[onePastLast-1] == '/' { if onePastLast > 1 && i[onePastLast-1] == '/' {
i = i[:onePastLast-1] i = i[:onePastLast-1]
@ -126,9 +90,9 @@ func IRIf(i IRI, t collectionPath) IRI {
return IRI(fmt.Sprintf("%s/%s", i, t)) return IRI(fmt.Sprintf("%s/%s", i, t))
} }
// IRI gives us the IRI of the t collectionPath type corresponding to the i Item, // IRI gives us the IRI of the t CollectionPath type corresponding to the i Item,
// or generates a new one if not found. // or generates a new one if not found.
func (t collectionPath) IRI(i Item) IRI { func (t CollectionPath) IRI(i Item) IRI {
if IsNil(i) { if IsNil(i) {
return IRIf("", t) return IRIf("", t)
} }
@ -140,8 +104,8 @@ func (t collectionPath) IRI(i Item) IRI {
return IRIf(i.GetLink(), t) return IRIf(i.GetLink(), t)
} }
// Of gives us the property of the i Item that corresponds to the t collectionPath type. // Of gives us the property of the i Item that corresponds to the t CollectionPath type.
func (t collectionPath) Of(i Item) Item { func (t CollectionPath) Of(i Item) Item {
if IsNil(i) || !i.IsObject() { if IsNil(i) || !i.IsObject() {
return nil return nil
} }
@ -178,22 +142,22 @@ func (t collectionPath) Of(i Item) Item {
return it return it
} }
// OfActor returns the base IRI of received i, if i represents an IRI matching collectionPath type t // OfActor returns the base IRI of received i, if i represents an IRI matching CollectionPath type t
func (t collectionPath) OfActor(i IRI) (IRI, error) { func (t CollectionPath) OfActor(i IRI) (IRI, error) {
maybeActor, maybeCol := path.Split(i.String()) maybeActor, maybeCol := path.Split(i.String())
if strings.ToLower(maybeCol) == strings.ToLower(string(t)) { if strings.ToLower(maybeCol) == strings.ToLower(string(t)) {
maybeActor = strings.TrimRight(maybeActor, "/") maybeActor = strings.TrimRight(maybeActor, "/")
return IRI(maybeActor), nil return IRI(maybeActor), nil
} }
return EmptyIRI, errors.Newf("IRI does not represent a valid %s collectionPath", t) return EmptyIRI, errors.Newf("IRI does not represent a valid %s CollectionPath", t)
} }
// Split returns the base IRI of received i, if i represents an IRI matching collectionPath type t // Split returns the base IRI of received i, if i represents an IRI matching CollectionPath type t
func Split(i IRI) (IRI, collectionPath) { func Split(i IRI) (IRI, CollectionPath) {
return ActivityPubCollections.Split(i) return ActivityPubCollections.Split(i)
} }
func getValidActivityCollection(t collectionPath) collectionPath { func getValidActivityCollection(t CollectionPath) CollectionPath {
if validActivityCollection.Contains(t) { if validActivityCollection.Contains(t) {
return t return t
} }
@ -201,17 +165,17 @@ func getValidActivityCollection(t collectionPath) collectionPath {
} }
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities // ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Activities
func ValidActivityCollection(typ collectionPath) bool { func ValidActivityCollection(typ CollectionPath) bool {
return getValidActivityCollection(typ) != Unknown return getValidActivityCollection(typ) != Unknown
} }
var validObjectCollection = []collectionPath{ var validObjectCollection = []CollectionPath{
Following, Following,
Followers, Followers,
Liked, Liked,
} }
func getValidObjectCollection(typ collectionPath) collectionPath { func getValidObjectCollection(typ CollectionPath) CollectionPath {
for _, t := range validObjectCollection { for _, t := range validObjectCollection {
if strings.ToLower(string(typ)) == string(t) { if strings.ToLower(string(typ)) == string(t) {
return t return t
@ -221,11 +185,11 @@ func getValidObjectCollection(typ collectionPath) collectionPath {
} }
// ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Objects // ValidActivityCollection shows if the current ActivityPub end-point type is a valid one for handling Objects
func ValidObjectCollection(typ collectionPath) bool { func ValidObjectCollection(typ CollectionPath) bool {
return getValidObjectCollection(typ) != Unknown return getValidObjectCollection(typ) != Unknown
} }
func getValidCollection(typ collectionPath) collectionPath { func getValidCollection(typ CollectionPath) CollectionPath {
if typ := getValidActivityCollection(typ); typ != Unknown { if typ := getValidActivityCollection(typ); typ != Unknown {
return typ return typ
} }
@ -235,7 +199,7 @@ func getValidCollection(typ collectionPath) collectionPath {
return Unknown return Unknown
} }
func ValidCollection(typ collectionPath) bool { func ValidCollection(typ CollectionPath) bool {
return getValidCollection(typ) != Unknown return getValidCollection(typ) != Unknown
} }
@ -244,8 +208,8 @@ func ValidCollectionIRI(i IRI) bool {
return getValidCollection(t) != Unknown return getValidCollection(t) != Unknown
} }
// AddTo adds collectionPath type IRI on the corresponding property of the i Item // AddTo adds CollectionPath type IRI on the corresponding property of the i Item
func (t collectionPath) AddTo(i Item) (IRI, bool) { func (t CollectionPath) AddTo(i Item) (IRI, bool) {
if IsNil(i) || !i.IsObject() { if IsNil(i) || !i.IsObject() {
return NilIRI, false return NilIRI, false
} }

View file

@ -31,7 +31,7 @@ func TestSplit(t *testing.T) {
func TestCollectionTypes_Of(t *testing.T) { func TestCollectionTypes_Of(t *testing.T) {
type args struct { type args struct {
o Item o Item
t collectionPath t CollectionPath
} }
tests := []struct { tests := []struct {
name string name string
@ -47,7 +47,7 @@ func TestCollectionTypes_Of(t *testing.T) {
want: nil, want: nil,
}, },
{ {
name: "nil from invalid collectionPath type", name: "nil from invalid CollectionPath type",
args: args{ args: args{
o: Object{ o: Object{
Likes: IRI("test"), Likes: IRI("test"),
@ -57,7 +57,7 @@ func TestCollectionTypes_Of(t *testing.T) {
want: nil, want: nil,
}, },
{ {
name: "nil from nil collectionPath type", name: "nil from nil CollectionPath type",
args: args{ args: args{
o: Object{ o: Object{
Likes: nil, Likes: nil,
@ -90,7 +90,7 @@ func TestCollectionTypes_Of(t *testing.T) {
func TestCollectionType_IRI(t *testing.T) { func TestCollectionType_IRI(t *testing.T) {
type args struct { type args struct {
o Item o Item
t collectionPath t CollectionPath
} }
tests := []struct { tests := []struct {
name string name string
@ -106,7 +106,7 @@ func TestCollectionType_IRI(t *testing.T) {
want: IRI("/likes"), want: IRI("/likes"),
}, },
{ {
name: "emptyIRI from invalid collectionPath type", name: "emptyIRI from invalid CollectionPath type",
args: args{ args: args{
o: Object{ o: Object{
Likes: IRI("test"), Likes: IRI("test"),
@ -156,7 +156,7 @@ func TestCollectionTypes_Contains(t *testing.T) {
func TestIRIf(t *testing.T) { func TestIRIf(t *testing.T) {
type args struct { type args struct {
i IRI i IRI
t collectionPath t CollectionPath
} }
tests := []struct { tests := []struct {
name string name string
@ -213,7 +213,7 @@ func TestCollectionType_AddTo(t *testing.T) {
var o *Object var o *Object
tests := []struct { tests := []struct {
name string name string
t collectionPath t CollectionPath
args args args args
want IRI want IRI
want1 bool want1 bool