Some cleanups on helper OnXXX functions
This commit is contained in:
parent
bd2b51124f
commit
df62354866
90
helpers.go
90
helpers.go
|
@ -1,7 +1,6 @@
|
||||||
package activitypub
|
package activitypub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,45 +18,17 @@ type withItemCollectionFn func (collection *ItemCollection) error
|
||||||
|
|
||||||
// OnObject
|
// OnObject
|
||||||
func OnObject(it Item, fn withObjectFn) error {
|
func OnObject(it Item, fn withObjectFn) error {
|
||||||
if ActivityTypes.Contains(it.GetType()) {
|
ob, err := ToObject(it)
|
||||||
return OnActivity(it, func(a *Activity) error {
|
if err != nil {
|
||||||
ob, err := ToObject(a)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fn(ob)
|
|
||||||
})
|
|
||||||
} else if ActorTypes.Contains(it.GetType()) {
|
|
||||||
return OnActor(it, func(p *Actor) error {
|
|
||||||
ob, err := ToObject(p)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fn(ob)
|
|
||||||
})
|
|
||||||
} else if it.IsCollection() {
|
|
||||||
return OnCollection(it, func(col CollectionInterface) error {
|
|
||||||
for _, it := range col.Collection() {
|
|
||||||
err := OnObject(it, fn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
ob, err := ToObject(it)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fn(ob)
|
|
||||||
}
|
}
|
||||||
|
return fn(ob)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnActivity
|
// OnActivity
|
||||||
func OnActivity(it Item, fn withActivityFn) error {
|
func OnActivity(it Item, fn withActivityFn) error {
|
||||||
if !ActivityTypes.Contains(it.GetType()) {
|
if !(ActivityTypes.Contains(it.GetType()) || IntransitiveActivityTypes.Contains(it.GetType())) {
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Activity", it, it.GetType()))
|
return fmt.Errorf("%T[%s] can't be converted to Activity", it, it.GetType())
|
||||||
}
|
}
|
||||||
act, err := ToActivity(it)
|
act, err := ToActivity(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -68,11 +39,8 @@ func OnActivity(it Item, fn withActivityFn) error {
|
||||||
|
|
||||||
// OnIntransitiveActivity
|
// OnIntransitiveActivity
|
||||||
func OnIntransitiveActivity(it Item, fn withIntransitiveActivityFn) error {
|
func OnIntransitiveActivity(it Item, fn withIntransitiveActivityFn) error {
|
||||||
if !IntransitiveActivityTypes.Contains(it.GetType()) {
|
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Activity", it, it.GetType()))
|
|
||||||
}
|
|
||||||
if it.GetType() == QuestionType {
|
if it.GetType() == QuestionType {
|
||||||
errors.New(fmt.Sprintf("For %T[%s] you need to use OnQuestion function", it, it.GetType()))
|
fmt.Errorf("for %T[%s] you need to use OnQuestion function", it, it.GetType())
|
||||||
}
|
}
|
||||||
act, err := ToIntransitiveActivity(it)
|
act, err := ToIntransitiveActivity(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -84,7 +52,7 @@ func OnIntransitiveActivity(it Item, fn withIntransitiveActivityFn) error {
|
||||||
// OnQuestion
|
// OnQuestion
|
||||||
func OnQuestion(it Item, fn withQuestionFn) error {
|
func OnQuestion(it Item, fn withQuestionFn) error {
|
||||||
if it.GetType() != QuestionType {
|
if it.GetType() != QuestionType {
|
||||||
errors.New(fmt.Sprintf("For %T[%s] can't be converted to Question", it, it.GetType()))
|
fmt.Errorf("For %T[%s] can't be converted to Question", it, it.GetType())
|
||||||
}
|
}
|
||||||
act, err := ToQuestion(it)
|
act, err := ToQuestion(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -96,13 +64,13 @@ func OnQuestion(it Item, fn withQuestionFn) error {
|
||||||
// OnActor
|
// OnActor
|
||||||
func OnActor(it Item, fn withActorFn) error {
|
func OnActor(it Item, fn withActorFn) error {
|
||||||
if !ActorTypes.Contains(it.GetType()) {
|
if !ActorTypes.Contains(it.GetType()) {
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Person", it, it.GetType()))
|
return fmt.Errorf("%T[%s] can't be converted to Person", it, it.GetType())
|
||||||
}
|
}
|
||||||
pers, err := ToActor(it)
|
act, err := ToActor(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return fn(pers)
|
return fn(act)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnCollection
|
// OnCollection
|
||||||
|
@ -133,14 +101,14 @@ func OnCollection(it Item, fn withCollectionInterfaceFn) error {
|
||||||
return fn(col)
|
return fn(col)
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Collection", it, it.GetType()))
|
return fmt.Errorf("%T[%s] can't be converted to Collection", it, it.GetType())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnCollectionPage
|
// OnCollectionPage
|
||||||
func OnCollectionPage(it Item, fn withCollectionPageFn) error {
|
func OnCollectionPage(it Item, fn withCollectionPageFn) error {
|
||||||
if it.GetType() != CollectionPageType {
|
if it.GetType() != CollectionPageType {
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to Collection Page", it, it.GetType()))
|
return fmt.Errorf("%T[%s] can't be converted to Collection Page", it, it.GetType())
|
||||||
}
|
}
|
||||||
col, err := ToCollectionPage(it)
|
col, err := ToCollectionPage(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -151,30 +119,17 @@ func OnCollectionPage(it Item, fn withCollectionPageFn) error {
|
||||||
|
|
||||||
// OnOrderedCollection
|
// OnOrderedCollection
|
||||||
func OnOrderedCollection(it Item, fn withOrderedCollectionFn) error {
|
func OnOrderedCollection(it Item, fn withOrderedCollectionFn) error {
|
||||||
switch it.GetType() {
|
col, err := ToOrderedCollection(it)
|
||||||
case OrderedCollectionType:
|
if err != nil {
|
||||||
col, err := ToOrderedCollection(it)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fn(col)
|
|
||||||
case OrderedCollectionPageType:
|
|
||||||
return OnOrderedCollectionPage(it, func(p *OrderedCollectionPage) error {
|
|
||||||
col, err := ToOrderedCollection(p)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return fn(col)
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection", it, it.GetType()))
|
|
||||||
}
|
}
|
||||||
|
return fn(col)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnOrderedCollectionPage
|
// OnOrderedCollectionPage
|
||||||
func OnOrderedCollectionPage(it Item, fn withOrderedCollectionPageFn) error {
|
func OnOrderedCollectionPage(it Item, fn withOrderedCollectionPageFn) error {
|
||||||
if it.GetType() != OrderedCollectionPageType {
|
if it.GetType() != OrderedCollectionPageType {
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType()))
|
return fmt.Errorf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType())
|
||||||
}
|
}
|
||||||
col, err := ToOrderedCollectionPage(it)
|
col, err := ToOrderedCollectionPage(it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -183,12 +138,9 @@ func OnOrderedCollectionPage(it Item, fn withOrderedCollectionPageFn) error {
|
||||||
return fn(col)
|
return fn(col)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnOrderedCollectionPage
|
// OnItemCollection
|
||||||
func OnItemCOllection(it Item, fn withOrderedCollectionPageFn) error {
|
func OnItemCollection(it Item, fn withItemCollectionFn) error {
|
||||||
if it.GetType() != OrderedCollectionPageType {
|
col, err := ToItemCollection(it)
|
||||||
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType()))
|
|
||||||
}
|
|
||||||
col, err := ToOrderedCollectionPage(it)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue