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 (
|
2021-12-30 15:09:33 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2020-04-13 10:01:07 +00:00
|
|
|
"fmt"
|
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"
|
2021-08-14 16:34:02 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fastjson"
|
2018-07-18 14:29:47 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 15:34:38 +00:00
|
|
|
const (
|
2021-06-06 15:04:34 +00:00
|
|
|
// ActivityBaseURI the URI for the ActivityStreams namespace
|
2019-12-04 15:34:38 +00:00
|
|
|
ActivityBaseURI = IRI("https://www.w3.org/ns/activitystreams")
|
2021-06-06 15:04:34 +00:00
|
|
|
// SecurityContextURI the URI for the security namespace (for an Actor's PublicKey)
|
2019-12-04 15:34:38 +00:00
|
|
|
SecurityContextURI = IRI("https://w3id.org/security/v1")
|
2021-06-06 15:04:34 +00:00
|
|
|
// PublicNS is the reference to the Public entity in the ActivityStreams namespace
|
2020-06-30 11:35:04 +00:00
|
|
|
PublicNS = ActivityBaseURI + "#Public"
|
2019-12-04 15:34:38 +00:00
|
|
|
)
|
|
|
|
|
2021-11-12 19:05:08 +00:00
|
|
|
// JsonLDContext is a slice of IRIs that form the default context for the objects in the
|
|
|
|
// GoActivitypub vocabulary.
|
|
|
|
// It does not represent just the default ActivityStreams public namespace, but it also
|
|
|
|
// has the W3 Permanent Identifier Community Group's Security namespace, which appears
|
|
|
|
// in the Actor type objects, which contain public key related data.
|
2019-12-04 15:34:38 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:10:31 +00:00
|
|
|
// UnmarshalJSON decodes an incoming JSON document into the receiver object.
|
2018-07-18 14:29:47 +00:00
|
|
|
func (i *IRI) UnmarshalJSON(s []byte) error {
|
|
|
|
*i = IRI(strings.Trim(string(s), "\""))
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-24 21:11:08 +00:00
|
|
|
|
2021-11-12 18:10:31 +00:00
|
|
|
// MarshalJSON encodes the receiver object to a JSON document.
|
2019-12-19 16:05:12 +00:00
|
|
|
func (i IRI) MarshalJSON() ([]byte, error) {
|
2020-03-26 18:24:44 +00:00
|
|
|
if i == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-19 16:05:12 +00:00
|
|
|
b := make([]byte, 0)
|
|
|
|
write(&b, '"')
|
|
|
|
writeS(&b, i.String())
|
|
|
|
write(&b, '"')
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:36:32 +00:00
|
|
|
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
|
|
|
func (i *IRI) UnmarshalBinary(data []byte) error {
|
2021-12-30 15:09:33 +00:00
|
|
|
return i.GobDecode(data)
|
2021-03-14 18:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
|
|
|
func (i IRI) MarshalBinary() ([]byte, error) {
|
2021-12-30 15:09:33 +00:00
|
|
|
return i.GobEncode()
|
2021-03-14 18:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GobEncode
|
|
|
|
func (i IRI) GobEncode() ([]byte, error) {
|
2021-12-30 15:09:33 +00:00
|
|
|
if len(i) == 0 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
gg := gob.NewEncoder(&b)
|
2022-01-02 15:17:05 +00:00
|
|
|
if err := gobEncodeStringLikeType(gg, []byte(i)); err != nil {
|
2021-12-30 15:09:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
2021-03-14 18:36:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 14:39:08 +00:00
|
|
|
// GobEncode
|
|
|
|
func (i IRIs) GobEncode() ([]byte, error) {
|
|
|
|
if len(i) == 0 {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
gg := gob.NewEncoder(&b)
|
|
|
|
bb := make([][]byte, 0)
|
|
|
|
for _, iri := range i {
|
|
|
|
bb = append(bb, []byte(iri))
|
|
|
|
}
|
|
|
|
if err := gg.Encode(bb); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-03-14 18:36:32 +00:00
|
|
|
// GobDecode
|
2021-12-30 15:09:33 +00:00
|
|
|
func (i *IRI) GobDecode(data []byte) error {
|
|
|
|
if len(data) == 0 {
|
|
|
|
// NOTE(marius): this behaviour diverges from vanilla gob package
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-30 18:49:25 +00:00
|
|
|
var bb []byte
|
2021-12-30 15:09:33 +00:00
|
|
|
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&bb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*i = IRI(bb)
|
|
|
|
return nil
|
2021-03-14 18:36:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 14:39:08 +00:00
|
|
|
func (i *IRIs) GobDecode(data []byte) error {
|
|
|
|
if len(data) == 0 {
|
|
|
|
// NOTE(marius): this behaviour diverges from vanilla gob package
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bb := make([][]byte, 0)
|
|
|
|
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&bb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, b := range bb {
|
|
|
|
*i = append(*i, IRI(b))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:01:07 +00:00
|
|
|
// AddPath concatenates el elements as a path to i
|
|
|
|
func (i IRI) AddPath(el ...string) IRI {
|
|
|
|
return IRI(fmt.Sprintf("%s/%s", i, path.Join(el...)))
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:39:28 +00:00
|
|
|
// GetID
|
2019-12-05 18:02:15 +00:00
|
|
|
func (i IRI) GetID() ID {
|
2020-04-13 10:01:07 +00:00
|
|
|
return i
|
2018-07-25 09:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetType
|
|
|
|
func (i IRI) GetType() ActivityVocabularyType {
|
2022-01-12 14:34:59 +00:00
|
|
|
return IRIType
|
2018-07-25 09:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-06-06 15:04:34 +00:00
|
|
|
if !IsNil(it) && 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
|
|
|
|
2019-12-19 16:05:12 +00:00
|
|
|
func (i IRIs) MarshalJSON() ([]byte, error) {
|
|
|
|
b := make([]byte, 0)
|
|
|
|
if len(i) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
writeCommaIfNotEmpty := func(notEmpty bool) {
|
|
|
|
if notEmpty {
|
2019-12-19 20:03:31 +00:00
|
|
|
writeS(&b, ",")
|
2019-12-19 16:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
write(&b, '[')
|
2019-12-19 20:03:31 +00:00
|
|
|
for k, iri := range i {
|
|
|
|
writeCommaIfNotEmpty(k > 0)
|
2019-12-19 16:05:12 +00:00
|
|
|
write(&b, '"')
|
|
|
|
writeS(&b, iri.String())
|
|
|
|
write(&b, '"')
|
|
|
|
}
|
|
|
|
write(&b, ']')
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-04-11 18:36:16 +00:00
|
|
|
func (i *IRIs) UnmarshalJSON(data []byte) error {
|
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
p := fastjson.Parser{}
|
2021-08-15 11:41:01 +00:00
|
|
|
val, err := p.ParseBytes(data)
|
2020-04-11 18:36:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
switch val.Type() {
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeString:
|
2021-08-15 11:41:01 +00:00
|
|
|
if iri, ok := asIRI(val); ok && len(iri) > 0 {
|
2020-04-11 18:36:16 +00:00
|
|
|
*i = append(*i, iri)
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeArray:
|
2021-08-15 11:41:01 +00:00
|
|
|
for _, v := range val.GetArray() {
|
|
|
|
if iri, ok := asIRI(v); ok && len(iri) > 0 {
|
2020-04-11 18:36:16 +00:00
|
|
|
*i = append(*i, iri)
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
}
|
2020-04-11 18:36:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-10 19:54:35 +00:00
|
|
|
func hostSplit(h string) (string, string) {
|
|
|
|
pieces := strings.Split(h, ":")
|
|
|
|
if len(pieces) == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
if len(pieces) == 1 {
|
|
|
|
return pieces[0], ""
|
|
|
|
}
|
|
|
|
return pieces[0], pieces[1]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 19:54:35 +00:00
|
|
|
uHost, _ := hostSplit(u.Host)
|
|
|
|
uwHost, _ := hostSplit(uw.Host)
|
|
|
|
if uHost != uwHost {
|
2019-11-20 10:42:33 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-04-11 18:36:16 +00:00
|
|
|
|
2020-12-28 16:53:47 +00:00
|
|
|
func (i IRI) ItemsMatch(col ...Item) bool {
|
|
|
|
for _, it := range col {
|
|
|
|
if match := it.GetLink().Contains(i, false); !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2020-04-11 18:36:16 +00:00
|
|
|
}
|