2019-12-03 16:23:59 +00:00
|
|
|
package activitypub
|
2017-09-11 20:45:57 +00:00
|
|
|
|
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 (
|
|
|
|
EmptyIRI IRI = ""
|
|
|
|
NilIRI IRI = "-"
|
|
|
|
|
|
|
|
EmptyID = EmptyIRI
|
|
|
|
NilID = NilIRI
|
|
|
|
)
|
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()) {
|
|
|
|
OnActivity(it, func(i*Activity) error {
|
|
|
|
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 {
|
|
|
|
_, ok := it.(IRI)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsObject returns if the current Item interface holds an IRI
|
|
|
|
func IsObject(it Item) bool {
|
|
|
|
ok := it.IsObject()
|
|
|
|
if it.IsLink() {
|
|
|
|
_, ok = it.(IRI)
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
// This is the default if the argument can't be casted to Object, as is the case for an ItemCollection
|
|
|
|
isNil := false
|
|
|
|
if IsItemCollection(it) {
|
|
|
|
OnItemCollection(it, func(c *ItemCollection) error {
|
|
|
|
isNil = c == nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
OnObject(it, func(o *Object) error {
|
|
|
|
isNil = o == nil
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return isNil
|
2021-03-27 13:06:24 +00:00
|
|
|
}
|