This repository has been archived on 2022-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
activitypub/item.go
Marius Orcsik 15ac5c20c7
Updated OnX functions to apply the function on an item collection if that's what's being passed to it
This still has the downside that the collection might contain different types of objects

but I can't think of a better way to do it at the moment
2021-01-31 13:30:40 +01:00

96 lines
2 KiB
Go

package activitypub
// Item struct
type Item = ObjectOrLink
const (
EmptyIRI IRI = ""
NilIRI IRI = "-"
EmptyID = EmptyIRI
NilID = NilIRI
)
// ItemsEqual checks if it and with Items are equal
func ItemsEqual(it, with Item) bool {
if it == nil || with == nil{
return with == it
}
result := true
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
})
} else if ActorTypes.Contains(with.GetType()) {
OnActor(it, func(i *Actor) error {
result = i.Equals(with)
return nil
})
} else {
OnObject(it, func(i *Object) error {
result = i.Equals(with)
return nil
})
}
}
if with.IsLink() {
result = with.GetLink().Equals(it.GetLink(), false)
}
return result
}
// IsItemCollection returns if the current Item interface holds a Collection
func IsItemCollection(it Item) bool {
_, ok := it.(ItemCollection)
return ok
}
// 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
}