2019-08-20 19:43:36 +00:00
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-09-09 18:21:45 +00:00
|
|
|
type withObjectFn func (*Object) error
|
2019-12-03 16:23:59 +00:00
|
|
|
type withActivityFn func (*Activity) error
|
|
|
|
type withIntransitiveActivityFn func (*IntransitiveActivity) error
|
|
|
|
type withQuestionFn func (*Question) error
|
2019-12-03 20:53:11 +00:00
|
|
|
type withActorFn func (*Actor) error
|
2019-12-03 16:23:59 +00:00
|
|
|
type withCollectionInterfaceFn func (collection CollectionInterface) error
|
|
|
|
type withCollectionFn func (collection *Collection) error
|
|
|
|
type withCollectionPageFn func (*CollectionPage) error
|
|
|
|
type withOrderedCollectionFn func (*OrderedCollection) error
|
|
|
|
type withOrderedCollectionPageFn func (*OrderedCollectionPage) error
|
|
|
|
type withItemCollectionFn func (collection *ItemCollection) error
|
2019-08-20 19:43:36 +00:00
|
|
|
|
2020-12-28 13:58:56 +00:00
|
|
|
// OnLink
|
|
|
|
func OnLink(it Item, fn withLinkFn) error {
|
|
|
|
ob, err := ToLink(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(ob)
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:02:37 +00:00
|
|
|
// OnObject
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnObject(it Item, fn withObjectFn) error {
|
2020-12-28 13:58:56 +00:00
|
|
|
ob, err := ToObject(it)
|
2019-12-05 15:42:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-20 19:43:36 +00:00
|
|
|
}
|
2019-12-05 15:42:27 +00:00
|
|
|
return fn(ob)
|
2019-08-20 19:43:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 20:02:37 +00:00
|
|
|
// OnActivity
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnActivity(it Item, fn withActivityFn) error {
|
2019-12-05 15:42:27 +00:00
|
|
|
if !(ActivityTypes.Contains(it.GetType()) || IntransitiveActivityTypes.Contains(it.GetType())) {
|
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Activity", it, it.GetType())
|
2019-08-20 19:43:36 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
act, err := ToActivity(it)
|
2019-08-20 19:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(act)
|
|
|
|
}
|
|
|
|
|
2019-08-20 20:02:37 +00:00
|
|
|
// OnIntransitiveActivity
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnIntransitiveActivity(it Item, fn withIntransitiveActivityFn) error {
|
|
|
|
if it.GetType() == QuestionType {
|
2019-12-05 15:42:27 +00:00
|
|
|
fmt.Errorf("for %T[%s] you need to use OnQuestion function", it, it.GetType())
|
2019-08-20 20:02:37 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
act, err := ToIntransitiveActivity(it)
|
2019-08-20 20:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(act)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnQuestion
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnQuestion(it Item, fn withQuestionFn) error {
|
|
|
|
if it.GetType() != QuestionType {
|
2020-01-14 19:57:42 +00:00
|
|
|
fmt.Errorf("for %T[%s] can't be converted to Question", it, it.GetType())
|
2019-08-20 20:02:37 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
act, err := ToQuestion(it)
|
2019-08-20 20:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(act)
|
|
|
|
}
|
|
|
|
|
2019-12-03 20:53:11 +00:00
|
|
|
// OnActor
|
|
|
|
func OnActor(it Item, fn withActorFn) error {
|
2019-12-03 16:23:59 +00:00
|
|
|
if !ActorTypes.Contains(it.GetType()) {
|
2019-12-05 15:42:27 +00:00
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Person", it, it.GetType())
|
2019-08-20 19:43:36 +00:00
|
|
|
}
|
2019-12-05 15:42:27 +00:00
|
|
|
act, err := ToActor(it)
|
2019-08-20 19:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-05 15:42:27 +00:00
|
|
|
return fn(act)
|
2019-08-20 19:43:36 +00:00
|
|
|
}
|
2019-08-21 20:32:53 +00:00
|
|
|
// OnCollection
|
2020-01-14 19:57:42 +00:00
|
|
|
func OnCollection (it Item, fn withCollectionFn) error {
|
|
|
|
if !(ActivityVocabularyTypes{CollectionPageType, CollectionType,}).Contains(it.GetType()) {
|
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Collection", it, it.GetType())
|
|
|
|
}
|
|
|
|
col, err := ToCollection(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnCollectionIntf
|
|
|
|
func OnCollectionIntf(it Item, fn withCollectionInterfaceFn) error {
|
2019-08-29 10:49:01 +00:00
|
|
|
switch it.GetType() {
|
2019-12-03 16:23:59 +00:00
|
|
|
case CollectionOfItems:
|
|
|
|
col, err := ToItemCollection(it)
|
2019-12-01 18:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-29 16:26:30 +00:00
|
|
|
return fn(col)
|
2019-12-03 16:23:59 +00:00
|
|
|
case CollectionType:
|
|
|
|
col, err := ToCollection(it)
|
2019-08-29 10:49:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
2019-12-03 16:23:59 +00:00
|
|
|
case CollectionPageType:
|
|
|
|
return OnCollectionPage(it, func(p *CollectionPage) error {
|
2020-08-30 12:46:49 +00:00
|
|
|
col, err := ToCollectionPage(p)
|
2019-08-29 10:49:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
})
|
2020-03-23 10:27:27 +00:00
|
|
|
case OrderedCollectionType:
|
|
|
|
col, err := ToOrderedCollection(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
case OrderedCollectionPageType:
|
|
|
|
return OnOrderedCollectionPage(it, func(p *OrderedCollectionPage) error {
|
2020-08-30 12:46:49 +00:00
|
|
|
col, err := ToOrderedCollectionPage(p)
|
2020-03-23 10:27:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
})
|
2019-08-29 10:49:01 +00:00
|
|
|
default:
|
2019-12-05 15:42:27 +00:00
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Collection", it, it.GetType())
|
2019-08-21 20:32:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnCollectionPage
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnCollectionPage(it Item, fn withCollectionPageFn) error {
|
|
|
|
if it.GetType() != CollectionPageType {
|
2019-12-05 15:42:27 +00:00
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Collection Page", it, it.GetType())
|
2019-08-21 20:32:53 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
col, err := ToCollectionPage(it)
|
2019-08-21 20:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnOrderedCollection
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnOrderedCollection(it Item, fn withOrderedCollectionFn) error {
|
2020-01-14 19:57:42 +00:00
|
|
|
if !(ActivityVocabularyTypes{OrderedCollectionPageType, OrderedCollectionType,}).Contains(it.GetType()) {
|
|
|
|
return fmt.Errorf("%T[%s] can't be converted to Ordered Collection", it, it.GetType())
|
|
|
|
}
|
2019-12-05 15:42:27 +00:00
|
|
|
col, err := ToOrderedCollection(it)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-21 20:32:53 +00:00
|
|
|
}
|
2019-12-05 15:42:27 +00:00
|
|
|
return fn(col)
|
2019-08-21 20:32:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 07:55:21 +00:00
|
|
|
// OnOrderedCollectionPage executes a function on an ordered collection page type item
|
2019-12-03 16:23:59 +00:00
|
|
|
func OnOrderedCollectionPage(it Item, fn withOrderedCollectionPageFn) error {
|
|
|
|
if it.GetType() != OrderedCollectionPageType {
|
2019-12-05 15:42:27 +00:00
|
|
|
return fmt.Errorf("%T[%s] can't be converted to OrderedCollection Page", it, it.GetType())
|
2019-08-21 20:32:53 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
col, err := ToOrderedCollectionPage(it)
|
2019-08-21 20:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
}
|
2019-12-01 18:49:47 +00:00
|
|
|
|
2019-12-31 07:55:21 +00:00
|
|
|
// OnItemCollection executes a function on a collection type item
|
2019-12-05 15:42:27 +00:00
|
|
|
func OnItemCollection(it Item, fn withItemCollectionFn) error {
|
|
|
|
col, err := ToItemCollection(it)
|
2019-12-01 18:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fn(col)
|
|
|
|
}
|
2019-12-31 07:55:21 +00:00
|
|
|
|
|
|
|
// ItemOrderTimestamp is used for ordering a ItemCollection slice using the slice.Sort function
|
|
|
|
// It orders i1 and i2 based on their Published and Updated timestamps.
|
|
|
|
func ItemOrderTimestamp(i1, i2 Item) bool {
|
|
|
|
o1, e1 := ToObject(i1)
|
|
|
|
o2, e2 := ToObject(i2)
|
|
|
|
if e1 != nil || e2 != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t1 := o1.Published
|
|
|
|
if !o1.Updated.IsZero() {
|
|
|
|
t1 = o1.Updated
|
|
|
|
}
|
|
|
|
t2 := o2.Published
|
|
|
|
if !o2.Updated.IsZero() {
|
|
|
|
t2 = o2.Updated
|
|
|
|
}
|
|
|
|
return t1.Sub(t2) > 0
|
|
|
|
}
|