Extending usage of OnXXX functions to cover ItemCollections

Updated activitysatreams package
This commit is contained in:
Marius Orcsik 2019-12-01 19:49:47 +01:00
parent 88e9eefd40
commit 0ad2174ed0
No known key found for this signature in database
GPG key ID: 77618B618F79EB72
2 changed files with 37 additions and 3 deletions

2
go.mod
View file

@ -4,6 +4,6 @@ go 1.13
require (
github.com/buger/jsonparser v0.0.0-20181023193515-52c6e1462ebd
github.com/go-ap/activitystreams v0.0.0-20191201155635-d82e449e14e3
github.com/go-ap/activitystreams v0.0.0-20191201184731-495abe28e08d
github.com/go-ap/jsonld v0.0.0-20191123195936-1e43eac08b0c
)

View file

@ -11,10 +11,12 @@ type withActivityFn func (*activitystreams.Activity) error
type withIntransitiveActivityFn func (*activitystreams.IntransitiveActivity) error
type withQuestionFn func (*activitystreams.Question) error
type withPersonFn func (*Person) error
type withCollectionFn func (*activitystreams.Collection) error
type withCollectionInterfaceFn func (collection activitystreams.CollectionInterface) error
type withCollectionFn func (collection *activitystreams.Collection) error
type withCollectionPageFn func (*activitystreams.CollectionPage) error
type withOrderedCollectionFn func (*activitystreams.OrderedCollection) error
type withOrderedCollectionPageFn func (*activitystreams.OrderedCollectionPage) error
type withItemCollectionFn func (collection *activitystreams.ItemCollection) error
// OnObject
func OnObject(it activitystreams.Item, fn withObjectFn) error {
@ -34,6 +36,16 @@ func OnObject(it activitystreams.Item, fn withObjectFn) error {
}
return fn(ob)
})
} else if it.IsCollection() {
return OnCollection(it, func(col activitystreams.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 {
@ -95,8 +107,18 @@ func OnPerson(it activitystreams.Item, fn withPersonFn) error {
}
// OnCollection
func OnCollection(it activitystreams.Item, fn withCollectionFn) error {
func OnCollection(it activitystreams.Item, fn withCollectionInterfaceFn) error {
switch it.GetType() {
case activitystreams.CollectionOfItems:
col, err := activitystreams.ToItemCollection(it)
if err != nil {
return err
}
c := activitystreams.Collection{
TotalItems: uint(len(*col)),
Items: *col,
}
return fn(&c)
case activitystreams.CollectionType:
col, err := activitystreams.ToCollection(it)
if err != nil {
@ -161,3 +183,15 @@ func OnOrderedCollectionPage(it activitystreams.Item, fn withOrderedCollectionPa
}
return fn(col)
}
// OnOrderedCollectionPage
func OnItemCOllection(it activitystreams.Item, fn withOrderedCollectionPageFn) error {
if it.GetType() != activitystreams.OrderedCollectionPageType {
return errors.New(fmt.Sprintf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType()))
}
col, err := activitystreams.ToOrderedCollectionPage(it)
if err != nil {
return err
}
return fn(col)
}