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/iri.go

78 lines
1.3 KiB
Go
Raw Normal View History

package activitystreams
2017-09-12 15:22:10 +00:00
import (
2019-06-02 12:59:04 +00:00
"net/url"
"strings"
)
const PublicNS = IRI("https://www.w3.org/ns/activitystreams#Public")
2017-09-16 17:53:11 +00:00
type (
// IRI is a Internationalized Resource Identifiers (IRIs) RFC3987
2018-10-11 18:13:34 +00:00
IRI string
IRIs []IRI
2017-09-16 17:53:11 +00:00
)
// String returns the String value of the IRI object
func (i IRI) String() string {
return string(i)
}
2018-10-11 18:13:34 +00:00
// GetLink
func (i IRI) GetLink() IRI {
return i
}
2019-06-02 12:59:04 +00:00
// URL
func (i IRI) URL() (*url.URL, error) {
return url.Parse(i.String())
}
// UnmarshalJSON
func (i *IRI) UnmarshalJSON(s []byte) error {
*i = IRI(strings.Trim(string(s), "\""))
return nil
}
// IsObject
func (i IRI) GetID() *ObjectID {
2019-02-19 12:02:38 +00:00
o := ObjectID(i)
return &o
}
// GetType
func (i IRI) GetType() ActivityVocabularyType {
return LinkType
}
// IsLink
func (i IRI) IsLink() bool {
return true
}
// IsObject
func (i IRI) IsObject() bool {
return false
}
// FlattenToIRI checks if Item can be flatten to an IRI and returns it if so
func FlattenToIRI(it Item) Item {
if it!= nil && it.IsObject() && len(it.GetLink()) > 0 {
return it.GetLink()
}
return it
}
// Contains verifies if IRIs array contains the received one
func(i IRIs) Contains(r IRI) bool {
if len(i) == 0 {
return true
}
for _, iri := range i {
if strings.ToLower(r.String()) == strings.ToLower(iri.String()) {
return true
}
}
return false
}