2019-12-03 16:23:59 +00:00
|
|
|
package activitypub
|
2017-09-11 20:45:57 +00:00
|
|
|
|
2022-05-25 11:41:52 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-03-25 18:54:51 +00:00
|
|
|
// Item struct
|
2020-03-31 11:45:24 +00:00
|
|
|
type Item = ObjectOrLink
|
2018-07-25 09:54:12 +00:00
|
|
|
|
2020-03-31 11:45:24 +00:00
|
|
|
const (
|
2021-11-12 19:05:08 +00:00
|
|
|
// EmptyIRI represents a zero length IRI
|
2020-03-31 11:45:24 +00:00
|
|
|
EmptyIRI IRI = ""
|
2021-11-12 19:05:08 +00:00
|
|
|
// NilIRI represents by convention an IRI which is nil
|
|
|
|
// Its use is mostly to check if a property of an ActivityPub Item is nil
|
|
|
|
NilIRI IRI = "-"
|
2020-03-31 11:45:24 +00:00
|
|
|
|
2021-11-12 19:05:08 +00:00
|
|
|
// EmptyID represents a zero length ID
|
2020-03-31 11:45:24 +00:00
|
|
|
EmptyID = EmptyIRI
|
2021-11-12 19:05:08 +00:00
|
|
|
// NilID represents by convention an ID which is nil, see details of NilIRI
|
|
|
|
NilID = NilIRI
|
2020-03-31 11:45:24 +00:00
|
|
|
)
|
2019-12-03 15:26:43 +00:00
|
|
|
|
2020-05-07 14:44:17 +00:00
|
|
|
// ItemsEqual checks if it and with Items are equal
|
|
|
|
func ItemsEqual(it, with Item) bool {
|
2021-02-02 13:18:16 +00:00
|
|
|
if IsNil(it) || IsNil(with) {
|
2020-05-07 14:44:17 +00:00
|
|
|
return with == it
|
|
|
|
}
|
2021-03-27 13:06:24 +00:00
|
|
|
result := false
|
2020-05-07 14:44:17 +00:00
|
|
|
if it.IsCollection() {
|
|
|
|
if it.GetType() == CollectionOfItems {
|
|
|
|
OnItemCollection(it, func(c *ItemCollection) error {
|
|
|
|
result = c.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if it.GetType() == CollectionType {
|
|
|
|
OnCollection(it, func(c *Collection) error {
|
|
|
|
result = c.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if it.GetType() == OrderedCollectionType {
|
|
|
|
OnOrderedCollection(it, func(c *OrderedCollection) error {
|
|
|
|
result = c.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if it.GetType() == CollectionPageType {
|
|
|
|
OnCollectionPage(it, func(c *CollectionPage) error {
|
|
|
|
result = c.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if it.GetType() == OrderedCollectionPageType {
|
|
|
|
OnOrderedCollectionPage(it, func(c *OrderedCollectionPage) error {
|
|
|
|
result = c.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if it.IsObject() {
|
|
|
|
if ActivityTypes.Contains(with.GetType()) {
|
2021-11-12 17:26:50 +00:00
|
|
|
OnActivity(it, func(i *Activity) error {
|
2020-05-07 14:44:17 +00:00
|
|
|
result = i.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-24 11:47:37 +00:00
|
|
|
} else if ActorTypes.Contains(with.GetType()) {
|
2020-05-07 14:44:17 +00:00
|
|
|
OnActor(it, func(i *Actor) error {
|
|
|
|
result = i.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-24 11:47:37 +00:00
|
|
|
} else {
|
|
|
|
OnObject(it, func(i *Object) error {
|
|
|
|
result = i.Equals(with)
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-07 14:44:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if with.IsLink() {
|
|
|
|
result = with.GetLink().Equals(it.GetLink(), false)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-12-31 09:37:05 +00:00
|
|
|
|
2021-01-31 12:30:40 +00:00
|
|
|
// IsItemCollection returns if the current Item interface holds a Collection
|
|
|
|
func IsItemCollection(it Item) bool {
|
|
|
|
_, ok := it.(ItemCollection)
|
2021-02-02 13:18:16 +00:00
|
|
|
_, okP := it.(*ItemCollection)
|
|
|
|
return ok || okP
|
2021-01-31 12:30:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 09:37:05 +00:00
|
|
|
// IsIRI returns if the current Item interface holds an IRI
|
|
|
|
func IsIRI(it Item) bool {
|
2022-01-14 15:21:52 +00:00
|
|
|
_, okV := it.(IRI)
|
|
|
|
_, okP := it.(*IRI)
|
|
|
|
return okV || okP
|
2020-12-31 09:37:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 11:41:52 +00:00
|
|
|
// IsLink returns if the current Item interface holds a Link
|
|
|
|
func IsLink(it Item) bool {
|
|
|
|
_, okV := it.(Link)
|
|
|
|
_, okP := it.(*Link)
|
|
|
|
return okV || okP
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsObject returns if the current Item interface holds an Object
|
2020-12-31 09:37:05 +00:00
|
|
|
func IsObject(it Item) bool {
|
2022-01-23 15:53:15 +00:00
|
|
|
switch it.(type) {
|
|
|
|
case Actor, *Actor,
|
|
|
|
Object, *Object, Profile, *Profile, Place, *Place, Relationship, *Relationship, Tombstone, *Tombstone,
|
|
|
|
Activity, *Activity, IntransitiveActivity, *IntransitiveActivity, Question, *Question,
|
|
|
|
Collection, *Collection, CollectionPage, *CollectionPage,
|
|
|
|
OrderedCollection, *OrderedCollection, OrderedCollectionPage, *OrderedCollectionPage:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2020-12-31 09:37:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 13:18:16 +00:00
|
|
|
|
|
|
|
// IsNil checks if the object matching an ObjectOrLink interface is nil
|
|
|
|
func IsNil(it Item) bool {
|
|
|
|
if it == nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-05-25 11:41:52 +00:00
|
|
|
// This is the default if the argument can't be cast to Object, as is the case for an ItemCollection
|
2021-02-02 13:18:16 +00:00
|
|
|
isNil := false
|
|
|
|
if IsItemCollection(it) {
|
|
|
|
OnItemCollection(it, func(c *ItemCollection) error {
|
|
|
|
isNil = c == nil
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-25 11:41:52 +00:00
|
|
|
} else if IsObject(it) {
|
2021-02-02 13:18:16 +00:00
|
|
|
OnObject(it, func(o *Object) error {
|
|
|
|
isNil = o == nil
|
|
|
|
return nil
|
|
|
|
})
|
2022-05-25 11:41:52 +00:00
|
|
|
} else if IsLink(it) {
|
|
|
|
OnLink(it, func(l *Link) error {
|
|
|
|
isNil = l == nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
} else if IsIRI(it) {
|
|
|
|
isNil = len(it.GetLink()) == 0
|
|
|
|
} else {
|
|
|
|
// NOTE(marius): we're not dealing with a type that we know about, so we use slow reflection
|
|
|
|
// as we still care about the result
|
2022-05-25 12:46:36 +00:00
|
|
|
v := reflect.ValueOf(it)
|
|
|
|
isNil = v.Kind() != reflect.Pointer || v.IsNil()
|
2021-02-02 13:18:16 +00:00
|
|
|
}
|
|
|
|
return isNil
|
2021-03-27 13:06:24 +00:00
|
|
|
}
|
2022-05-25 11:55:18 +00:00
|
|
|
|
|
|
|
func ErrorInvalidType[T Objects | Links](received Item) error {
|
|
|
|
return fmt.Errorf("unable to convert %T to %T", received, new(T))
|
|
|
|
}
|