2019-12-03 16:23:59 +00:00
|
|
|
package activitypub
|
2017-09-12 15:22:10 +00:00
|
|
|
|
2018-07-18 14:29:47 +00:00
|
|
|
import (
|
2019-06-02 12:59:04 +00:00
|
|
|
"net/url"
|
2019-11-20 10:42:33 +00:00
|
|
|
"path"
|
2018-07-18 14:29:47 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-12-04 15:34:38 +00:00
|
|
|
const (
|
|
|
|
// ActivityBaseURI the URI for the activity streams namespace
|
|
|
|
ActivityBaseURI = IRI("https://www.w3.org/ns/activitystreams")
|
|
|
|
// SecurityContextURI the URI for the secruity namespace (for an Actor's PublicKey)
|
|
|
|
SecurityContextURI = IRI("https://w3id.org/security/v1")
|
|
|
|
// PublicNs is the reference to the Public entity in the Acitivystreams namespace
|
|
|
|
PublicNS = IRI(ActivityBaseURI + "#Public")
|
|
|
|
)
|
|
|
|
|
|
|
|
var JsonLDContext = []IRI{
|
|
|
|
ActivityBaseURI,
|
|
|
|
SecurityContextURI,
|
|
|
|
}
|
2019-08-22 19:16:40 +00:00
|
|
|
|
2017-09-16 17:53:11 +00:00
|
|
|
type (
|
2018-03-27 14:16:07 +00:00
|
|
|
// IRI is a Internationalized Resource Identifiers (IRIs) RFC3987
|
2019-11-18 15:39:28 +00:00
|
|
|
IRI string
|
2019-08-17 13:17:10 +00:00
|
|
|
IRIs []IRI
|
2017-09-16 17:53:11 +00:00
|
|
|
)
|
2018-07-13 08:17:03 +00:00
|
|
|
|
2018-07-16 13:58:36 +00:00
|
|
|
// String returns the String value of the IRI object
|
2018-07-13 08:17:03 +00:00
|
|
|
func (i IRI) String() string {
|
|
|
|
return string(i)
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:13:34 +00:00
|
|
|
// GetLink
|
|
|
|
func (i IRI) GetLink() IRI {
|
|
|
|
return i
|
2018-07-18 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 12:59:04 +00:00
|
|
|
// URL
|
|
|
|
func (i IRI) URL() (*url.URL, error) {
|
|
|
|
return url.Parse(i.String())
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:29:47 +00:00
|
|
|
// UnmarshalJSON
|
|
|
|
func (i *IRI) UnmarshalJSON(s []byte) error {
|
|
|
|
*i = IRI(strings.Trim(string(s), "\""))
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-24 21:11:08 +00:00
|
|
|
|
2019-11-18 15:39:28 +00:00
|
|
|
// GetID
|
2019-12-03 15:26:43 +00:00
|
|
|
func (i IRI) GetID() ObjectID {
|
|
|
|
return ObjectID(i)
|
2018-07-25 09:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetType
|
|
|
|
func (i IRI) GetType() ActivityVocabularyType {
|
|
|
|
return LinkType
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLink
|
|
|
|
func (i IRI) IsLink() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsObject
|
|
|
|
func (i IRI) IsObject() bool {
|
|
|
|
return false
|
|
|
|
}
|
2019-05-11 08:31:19 +00:00
|
|
|
|
2019-12-01 18:27:45 +00:00
|
|
|
// IsCollection returns false for IRI objects
|
|
|
|
func (i IRI) IsCollection() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-11 09:53:32 +00:00
|
|
|
// FlattenToIRI checks if Item can be flatten to an IRI and returns it if so
|
|
|
|
func FlattenToIRI(it Item) Item {
|
2019-11-18 15:39:28 +00:00
|
|
|
if it != nil && it.IsObject() && len(it.GetLink()) > 0 {
|
2019-05-11 08:31:19 +00:00
|
|
|
return it.GetLink()
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
2019-08-17 13:17:10 +00:00
|
|
|
|
|
|
|
// Contains verifies if IRIs array contains the received one
|
2019-11-18 15:39:28 +00:00
|
|
|
func (i IRIs) Contains(r IRI) bool {
|
2019-08-17 13:17:10 +00:00
|
|
|
if len(i) == 0 {
|
2019-08-23 19:49:42 +00:00
|
|
|
return false
|
2019-08-17 13:17:10 +00:00
|
|
|
}
|
|
|
|
for _, iri := range i {
|
2019-11-18 15:41:31 +00:00
|
|
|
if r.Equals(iri, false) {
|
2019-08-17 13:17:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-11-18 15:39:28 +00:00
|
|
|
|
2019-12-03 18:59:20 +00:00
|
|
|
func validURL(u *url.URL) bool {
|
|
|
|
return len(u.Scheme) > 0 && len(u.Host) > 0
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:39:28 +00:00
|
|
|
// Equals verifies if our receiver IRI is equals with the "with" IRI
|
|
|
|
// It ignores the protocol
|
|
|
|
// It tries to use the URL representation if possible and fallback to string comparison if unable to convert
|
|
|
|
// In URL representation checks hostname in a case insensitive way and the path and
|
|
|
|
func (i IRI) Equals(with IRI, checkScheme bool) bool {
|
|
|
|
u, e := i.URL()
|
|
|
|
uw, ew := with.URL()
|
2019-12-03 18:59:20 +00:00
|
|
|
if e != nil || ew != nil || !validURL(u) || !validURL(uw) {
|
2019-11-18 15:39:28 +00:00
|
|
|
return strings.ToLower(i.String()) == strings.ToLower(with.String())
|
|
|
|
}
|
|
|
|
if checkScheme {
|
|
|
|
if strings.ToLower(u.Scheme) != strings.ToLower(uw.Scheme) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.ToLower(u.Host) != strings.ToLower(uw.Host) {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-20 10:42:33 +00:00
|
|
|
if path.Clean(u.Path) != path.Clean(uw.Path) {
|
2019-11-18 15:39:28 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
uq := u.Query()
|
|
|
|
uwq := uw.Query()
|
|
|
|
if len(uq) != len(uwq) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k, uqv := range uq {
|
|
|
|
uwqv, ok := uwq[k]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(uqv) != len(uwqv) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, uqvv := range uqv {
|
|
|
|
eq := false
|
|
|
|
for _, uwqvv := range uwqv {
|
|
|
|
if uwqvv == uqvv {
|
|
|
|
eq = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !eq {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-11-20 10:42:33 +00:00
|
|
|
|
|
|
|
func (i IRI) Contains(what IRI, checkScheme bool) bool {
|
|
|
|
u, e := i.URL()
|
|
|
|
uw, ew := what.URL()
|
|
|
|
if e != nil || ew != nil {
|
|
|
|
return strings.Contains(i.String(), what.String())
|
|
|
|
}
|
|
|
|
if checkScheme {
|
|
|
|
if u.Scheme != uw.Scheme {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u.Host != uw.Host {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
p := u.Path
|
|
|
|
if p != "" {
|
|
|
|
p = path.Clean(p)
|
|
|
|
}
|
|
|
|
pw := uw.Path
|
|
|
|
if pw != "" {
|
|
|
|
pw = path.Clean(pw)
|
|
|
|
}
|
|
|
|
return strings.Contains(p, pw)
|
|
|
|
}
|