2019-01-30 21:01:56 +00:00
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
2019-12-03 16:23:59 +00:00
|
|
|
"encoding"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2021-08-15 11:41:01 +00:00
|
|
|
"strings"
|
2019-12-03 16:23:59 +00:00
|
|
|
"time"
|
|
|
|
|
2021-08-14 16:34:02 +00:00
|
|
|
"github.com/valyala/fastjson"
|
2019-01-30 21:01:56 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 16:23:59 +00:00
|
|
|
var (
|
|
|
|
apUnmarshalerType = reflect.TypeOf(new(Item)).Elem()
|
|
|
|
unmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
|
|
|
|
textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
|
|
|
|
)
|
|
|
|
|
|
|
|
// ItemTyperFunc will return an instance of a struct that implements activitystreams.Item
|
2020-11-06 18:40:46 +00:00
|
|
|
// The default for this package is GetItemByType but can be overwritten
|
2021-01-31 12:30:40 +00:00
|
|
|
var ItemTyperFunc TyperFn = GetItemByType
|
2019-12-03 16:23:59 +00:00
|
|
|
|
|
|
|
// TyperFn is the type of the function which returns an activitystreams.Item struct instance
|
|
|
|
// for a specific ActivityVocabularyType
|
|
|
|
type TyperFn func(ActivityVocabularyType) (Item, error)
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetID(val *fastjson.Value) ID {
|
2021-08-15 17:11:18 +00:00
|
|
|
i := val.Get("id").GetStringBytes()
|
2019-12-05 18:02:15 +00:00
|
|
|
return ID(i)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetType(val *fastjson.Value) ActivityVocabularyType {
|
|
|
|
t := val.Get("type").GetStringBytes()
|
2021-01-09 16:11:57 +00:00
|
|
|
return ActivityVocabularyType(t)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetMimeType(val *fastjson.Value, prop string) MimeType {
|
2021-08-15 17:11:18 +00:00
|
|
|
t := val.GetStringBytes(prop)
|
2019-12-03 16:23:59 +00:00
|
|
|
return MimeType(t)
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetInt(val *fastjson.Value, prop string) int64 {
|
|
|
|
i := val.Get(prop).GetInt64()
|
|
|
|
return i
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetFloat(val *fastjson.Value, prop string) float64 {
|
|
|
|
f := val.Get(prop).GetFloat64()
|
|
|
|
return f
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetString(val *fastjson.Value, prop string) string {
|
|
|
|
s := val.Get(prop).GetStringBytes()
|
|
|
|
return string(s)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetBytes(val *fastjson.Value, prop string) []byte {
|
|
|
|
s := val.Get(prop).GetStringBytes()
|
|
|
|
return s
|
2019-12-03 19:59:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetBoolean(val *fastjson.Value, prop string) bool {
|
|
|
|
t, _ := val.Get(prop).Bool()
|
|
|
|
return t
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetNaturalLanguageField(val *fastjson.Value, prop string) NaturalLanguageValues {
|
2019-12-03 16:23:59 +00:00
|
|
|
n := NaturalLanguageValues{}
|
2021-08-15 11:41:01 +00:00
|
|
|
if val == nil {
|
|
|
|
return n
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
v := val.Get(prop)
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch v.Type() {
|
|
|
|
case fastjson.TypeObject:
|
|
|
|
ob, _ := v.Object()
|
|
|
|
ob.Visit(func(key []byte, v *fastjson.Value) {
|
|
|
|
l := LangRefValue{}
|
|
|
|
l.Ref = LangRef(key)
|
|
|
|
if err := l.Value.UnmarshalJSON(v.GetStringBytes()); err == nil {
|
|
|
|
if l.Ref != NilLangRef || len(l.Value) > 0 {
|
|
|
|
n = append(n, l)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeString:
|
2019-12-03 16:23:59 +00:00
|
|
|
l := LangRefValue{}
|
2021-08-14 16:34:02 +00:00
|
|
|
if err := l.UnmarshalJSON(v.GetStringBytes()); err == nil {
|
2019-12-03 16:23:59 +00:00
|
|
|
n = append(n, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetTime(val *fastjson.Value, prop string) time.Time {
|
2019-12-03 16:23:59 +00:00
|
|
|
t := time.Time{}
|
2021-08-15 11:41:01 +00:00
|
|
|
if val == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
if str := val.Get(prop).GetStringBytes(); len(str) > 0 {
|
2021-08-14 16:34:02 +00:00
|
|
|
t.UnmarshalText(str)
|
2020-12-30 12:30:34 +00:00
|
|
|
return t.UTC()
|
|
|
|
}
|
|
|
|
return t
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetDuration(val *fastjson.Value, prop string) time.Duration {
|
|
|
|
if str := val.Get(prop).GetStringBytes(); len(str) > 0 {
|
2020-12-30 12:30:34 +00:00
|
|
|
// TODO(marius): this needs to be replaced to be compatible with xsd:duration
|
2021-08-15 11:41:01 +00:00
|
|
|
d, _ := time.ParseDuration(string(str))
|
2020-12-30 12:30:34 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
return 0
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetPublicKey(val *fastjson.Value, prop string) PublicKey {
|
2019-12-03 19:59:57 +00:00
|
|
|
key := PublicKey{}
|
2021-08-16 14:28:43 +00:00
|
|
|
if val == nil {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
val = val.Get(prop)
|
|
|
|
if val == nil {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
loadPublicKey(val, &key)
|
2019-12-03 19:59:57 +00:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetStreams(val *fastjson.Value, prop string) []ItemCollection {
|
2020-12-30 12:30:34 +00:00
|
|
|
// TODO(marius)
|
2019-12-03 19:59:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 20:22:48 +00:00
|
|
|
func itemsFn(val *fastjson.Value) (Item, error) {
|
|
|
|
if val.Type() == fastjson.TypeArray {
|
|
|
|
it := val.GetArray()
|
|
|
|
if len(it) == 1 {
|
|
|
|
return itemFn(it[0])
|
|
|
|
}
|
|
|
|
items := make(ItemCollection, 0)
|
|
|
|
for _, v := range it {
|
|
|
|
if it, _ := itemFn(v); it != nil {
|
|
|
|
items.Append(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
return itemFn(val)
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func itemFn(val *fastjson.Value) (Item, error) {
|
|
|
|
typ := JSONGetType(val)
|
2021-08-22 16:59:32 +00:00
|
|
|
if typ == "" && val.Type() == fastjson.TypeString {
|
2021-01-09 16:11:57 +00:00
|
|
|
// try to see if it's an IRI
|
2021-08-15 11:41:01 +00:00
|
|
|
if i, ok := asIRI(val); ok {
|
2019-12-03 16:23:59 +00:00
|
|
|
return i, nil
|
|
|
|
}
|
2021-01-09 16:11:57 +00:00
|
|
|
}
|
|
|
|
i, err := ItemTyperFunc(typ)
|
2021-02-02 13:18:16 +00:00
|
|
|
if err != nil || IsNil(i) {
|
2019-12-03 16:23:59 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
switch typ {
|
2021-08-22 16:59:32 +00:00
|
|
|
case "":
|
|
|
|
// NOTE(marius): this handles Tags, that don't have types
|
|
|
|
fallthrough
|
2021-08-15 17:11:18 +00:00
|
|
|
case ObjectType, ArticleType, AudioType, DocumentType, EventType, ImageType, NoteType, PageType, VideoType:
|
2021-08-15 11:41:01 +00:00
|
|
|
err = OnObject(i, func(ob *Object) error {
|
|
|
|
return loadObject(val, ob)
|
|
|
|
})
|
|
|
|
case LinkType, MentionType:
|
|
|
|
err = OnLink(i, func(l *Link) error {
|
|
|
|
return loadLink(val, l)
|
|
|
|
})
|
|
|
|
case ActivityType, AcceptType, AddType, AnnounceType, BlockType, CreateType, DeleteType, DislikeType,
|
|
|
|
FlagType, FollowType, IgnoreType, InviteType, JoinType, LeaveType, LikeType, ListenType, MoveType, OfferType,
|
|
|
|
RejectType, ReadType, RemoveType, TentativeRejectType, TentativeAcceptType, UndoType, UpdateType, ViewType:
|
|
|
|
err = OnActivity(i, func(act *Activity) error {
|
|
|
|
return loadActivity(val, act)
|
|
|
|
})
|
|
|
|
case IntransitiveActivityType, ArriveType, TravelType:
|
|
|
|
err = OnIntransitiveActivity(i, func(act *IntransitiveActivity) error {
|
|
|
|
return loadIntransitiveActivity(val, act)
|
|
|
|
})
|
2021-08-15 17:11:18 +00:00
|
|
|
case ActorType, ApplicationType, GroupType, OrganizationType, PersonType, ServiceType:
|
2021-08-15 11:41:01 +00:00
|
|
|
err = OnActor(i, func(a *Actor) error {
|
|
|
|
return loadActor(val, a)
|
|
|
|
})
|
|
|
|
case CollectionType:
|
|
|
|
err = OnCollection(i, func(c *Collection) error {
|
|
|
|
return loadCollection(val, c)
|
|
|
|
})
|
|
|
|
case OrderedCollectionType:
|
|
|
|
err = OnOrderedCollection(i, func(c *OrderedCollection) error {
|
|
|
|
return loadOrderedCollection(val, c)
|
|
|
|
})
|
|
|
|
case CollectionPageType:
|
|
|
|
err = OnCollectionPage(i, func(p *CollectionPage) error {
|
|
|
|
return loadCollectionPage(val, p)
|
|
|
|
})
|
|
|
|
case OrderedCollectionPageType:
|
|
|
|
err = OnOrderedCollectionPage(i, func(p *OrderedCollectionPage) error {
|
|
|
|
return loadOrderedCollectionPage(val, p)
|
|
|
|
})
|
|
|
|
case PlaceType:
|
|
|
|
err = OnPlace(i, func(p *Place) error {
|
|
|
|
return loadPlace(val, p)
|
|
|
|
})
|
|
|
|
case ProfileType:
|
|
|
|
err = OnProfile(i, func(p *Profile) error {
|
|
|
|
return loadProfile(val, p)
|
|
|
|
})
|
|
|
|
case RelationshipType:
|
|
|
|
err = OnRelationship(i, func(r *Relationship) error {
|
|
|
|
return loadRelationship(val, r)
|
|
|
|
})
|
|
|
|
case TombstoneType:
|
|
|
|
err = OnTombstone(i, func(t *Tombstone) error {
|
|
|
|
return loadTombstone(val, t)
|
|
|
|
})
|
|
|
|
case QuestionType:
|
|
|
|
err = OnQuestion(i, func(q *Question) error {
|
|
|
|
return loadQuestion(val, q)
|
|
|
|
})
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
|
2021-01-09 17:37:38 +00:00
|
|
|
if !NotEmpty(i) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONUnmarshalToItem(val *fastjson.Value) Item {
|
2019-12-03 16:23:59 +00:00
|
|
|
if ItemTyperFunc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
var (
|
|
|
|
i Item
|
|
|
|
err error
|
|
|
|
)
|
2021-08-14 16:34:02 +00:00
|
|
|
switch val.Type() {
|
|
|
|
case fastjson.TypeArray:
|
2021-08-20 20:22:48 +00:00
|
|
|
i, err = itemsFn(val)
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeObject:
|
2021-08-15 11:41:01 +00:00
|
|
|
i, err = itemFn(val)
|
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 {
|
2019-12-03 16:23:59 +00:00
|
|
|
// try to see if it's an IRI
|
|
|
|
i = iri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func asIRI(val *fastjson.Value) (IRI, bool) {
|
|
|
|
if val == nil {
|
2020-04-12 08:57:04 +00:00
|
|
|
return NilIRI, true
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
s := strings.Trim(val.String(), `"`)
|
|
|
|
u, err := url.ParseRequestURI(s)
|
2019-12-03 16:23:59 +00:00
|
|
|
if err == nil && len(u.Scheme) > 0 && len(u.Host) > 0 {
|
|
|
|
// try to see if it's an IRI
|
2021-08-15 11:41:01 +00:00
|
|
|
return IRI(s), true
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
2020-04-12 08:57:04 +00:00
|
|
|
return EmptyIRI, false
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetItem(val *fastjson.Value, prop string) Item {
|
2021-08-20 20:22:48 +00:00
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
if val = val.Get(prop); val == nil {
|
2021-08-14 16:34:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch val.Type() {
|
|
|
|
case fastjson.TypeString:
|
2021-08-15 11:41:01 +00:00
|
|
|
if i, ok := asIRI(val); ok {
|
2019-12-03 16:23:59 +00:00
|
|
|
// try to see if it's an IRI
|
|
|
|
return i
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeArray:
|
2021-08-20 20:22:48 +00:00
|
|
|
it, _ := itemsFn(val)
|
|
|
|
return it
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeObject:
|
2021-08-20 20:22:48 +00:00
|
|
|
it, _ := itemFn(val)
|
|
|
|
return it
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeNumber:
|
2019-12-03 16:23:59 +00:00
|
|
|
fallthrough
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeNull:
|
2019-12-03 16:23:59 +00:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetURIItem(val *fastjson.Value, prop string) Item {
|
2021-08-15 17:11:18 +00:00
|
|
|
if val == nil {
|
2021-08-14 16:34:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-15 17:11:18 +00:00
|
|
|
if val = val.Get(prop); val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch val.Type() {
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeObject:
|
2021-08-15 17:11:18 +00:00
|
|
|
if it, _ := itemFn(val); it != nil {
|
|
|
|
return it
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeArray:
|
2021-08-20 20:22:48 +00:00
|
|
|
if it, _ := itemsFn(val); it != nil {
|
|
|
|
return it
|
2021-08-14 16:34:02 +00:00
|
|
|
}
|
|
|
|
case fastjson.TypeString:
|
2021-08-15 17:11:18 +00:00
|
|
|
return IRI(val.GetStringBytes())
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetItems(val *fastjson.Value, prop string) ItemCollection {
|
|
|
|
if val == nil {
|
2019-12-03 16:23:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
if val = val.Get(prop); val == nil {
|
2021-08-14 17:04:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:23:59 +00:00
|
|
|
it := make(ItemCollection, 0)
|
2021-08-14 18:12:13 +00:00
|
|
|
switch val.Type() {
|
2021-08-14 16:34:02 +00:00
|
|
|
case fastjson.TypeArray:
|
2021-08-14 18:12:13 +00:00
|
|
|
for _, v := range val.GetArray() {
|
2021-08-15 17:11:18 +00:00
|
|
|
if i, _ := itemFn(v); i != nil {
|
2019-12-03 16:23:59 +00:00
|
|
|
it.Append(i)
|
|
|
|
}
|
2021-08-14 16:34:02 +00:00
|
|
|
}
|
|
|
|
case fastjson.TypeObject:
|
2021-08-15 11:41:01 +00:00
|
|
|
if i := JSONGetItem(val, prop); i != nil {
|
2021-08-14 16:34:02 +00:00
|
|
|
it.Append(i)
|
|
|
|
}
|
|
|
|
case fastjson.TypeString:
|
2021-08-14 18:12:13 +00:00
|
|
|
if iri := val.GetStringBytes(); len(iri) > 0 {
|
2021-08-14 16:34:02 +00:00
|
|
|
it.Append(IRI(iri))
|
2020-12-30 13:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(it) == 0 {
|
|
|
|
return nil
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetLangRefField(val *fastjson.Value, prop string) LangRef {
|
|
|
|
s := val.Get(prop).GetStringBytes()
|
|
|
|
return LangRef(s)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetIRI(val *fastjson.Value, prop string) IRI {
|
|
|
|
s := val.Get(prop).GetStringBytes()
|
|
|
|
return IRI(s)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON tries to detect the type of the object in the json data and then outputs a matching
|
|
|
|
// ActivityStreams object, if possible
|
|
|
|
func UnmarshalJSON(data []byte) (Item, error) {
|
|
|
|
if ItemTyperFunc == nil {
|
2020-11-06 18:40:46 +00:00
|
|
|
ItemTyperFunc = GetItemByType
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
p := fastjson.Parser{}
|
|
|
|
val, err := p.ParseBytes(data)
|
2021-08-28 20:10:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return JSONUnmarshalToItem(val), nil
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 18:40:46 +00:00
|
|
|
func GetItemByType(typ ActivityVocabularyType) (Item, error) {
|
2019-12-03 16:23:59 +00:00
|
|
|
switch typ {
|
2021-08-15 17:11:18 +00:00
|
|
|
case ObjectType, ArticleType, AudioType, DocumentType, EventType, ImageType, NoteType, PageType, VideoType:
|
2019-12-03 16:23:59 +00:00
|
|
|
return ObjectNew(typ), nil
|
2021-08-15 17:11:18 +00:00
|
|
|
case LinkType, MentionType:
|
2019-12-03 16:23:59 +00:00
|
|
|
return &Link{Type: typ}, nil
|
2021-08-15 17:11:18 +00:00
|
|
|
case ActivityType, AcceptType, AddType, AnnounceType, BlockType, CreateType, DeleteType, DislikeType,
|
|
|
|
FlagType, FollowType, IgnoreType, InviteType, JoinType, LeaveType, LikeType, ListenType, MoveType, OfferType,
|
|
|
|
RejectType, ReadType, RemoveType, TentativeRejectType, TentativeAcceptType, UndoType, UpdateType, ViewType:
|
2019-12-03 16:23:59 +00:00
|
|
|
return &Activity{Type: typ}, nil
|
2021-08-15 17:11:18 +00:00
|
|
|
case IntransitiveActivityType, ArriveType, TravelType:
|
2019-12-03 16:23:59 +00:00
|
|
|
return &IntransitiveActivity{Type: typ}, nil
|
2021-08-15 17:11:18 +00:00
|
|
|
case ActorType, ApplicationType, GroupType, OrganizationType, PersonType, ServiceType:
|
2019-12-21 14:14:43 +00:00
|
|
|
return &Actor{Type: typ}, nil
|
2019-12-03 16:23:59 +00:00
|
|
|
case CollectionType:
|
|
|
|
return &Collection{Type: typ}, nil
|
|
|
|
case OrderedCollectionType:
|
|
|
|
return &OrderedCollection{Type: typ}, nil
|
|
|
|
case CollectionPageType:
|
|
|
|
return &CollectionPage{Type: typ}, nil
|
|
|
|
case OrderedCollectionPageType:
|
|
|
|
return &OrderedCollectionPage{Type: typ}, nil
|
|
|
|
case PlaceType:
|
|
|
|
return &Place{Type: typ}, nil
|
|
|
|
case ProfileType:
|
|
|
|
return &Profile{Type: typ}, nil
|
|
|
|
case RelationshipType:
|
|
|
|
return &Relationship{Type: typ}, nil
|
|
|
|
case TombstoneType:
|
|
|
|
return &Tombstone{Type: typ}, nil
|
|
|
|
case QuestionType:
|
|
|
|
return &Question{Type: typ}, nil
|
|
|
|
case "":
|
|
|
|
// when no type is available use a plain Object
|
2021-01-09 16:11:57 +00:00
|
|
|
return &Object{}, nil
|
2019-01-30 21:01:56 +00:00
|
|
|
}
|
2021-01-09 16:11:57 +00:00
|
|
|
return nil, fmt.Errorf("empty ActivityStreams type")
|
2019-01-30 21:01:56 +00:00
|
|
|
}
|
2019-08-01 11:40:43 +00:00
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func JSONGetActorEndpoints(val *fastjson.Value, prop string) *Endpoints {
|
|
|
|
str := val.Get(prop).GetStringBytes()
|
2019-08-01 11:40:43 +00:00
|
|
|
|
|
|
|
var e *Endpoints
|
|
|
|
if len(str) > 0 {
|
|
|
|
e = &Endpoints{}
|
2021-08-14 16:34:02 +00:00
|
|
|
e.UnmarshalJSON(str)
|
2019-08-01 11:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
2019-12-21 10:05:38 +00:00
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadObject(val *fastjson.Value, o *Object) error {
|
2019-12-21 10:05:38 +00:00
|
|
|
if ItemTyperFunc == nil {
|
2020-11-06 18:40:46 +00:00
|
|
|
ItemTyperFunc = GetItemByType
|
2019-12-21 10:05:38 +00:00
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
o.ID = JSONGetID(val)
|
|
|
|
o.Type = JSONGetType(val)
|
|
|
|
o.Name = JSONGetNaturalLanguageField(val, "name")
|
|
|
|
o.Content = JSONGetNaturalLanguageField(val, "content")
|
|
|
|
o.Summary = JSONGetNaturalLanguageField(val, "summary")
|
|
|
|
o.Context = JSONGetItem(val, "context")
|
|
|
|
o.URL = JSONGetURIItem(val, "url")
|
|
|
|
o.MediaType = JSONGetMimeType(val, "mediaType")
|
|
|
|
o.Generator = JSONGetItem(val, "generator")
|
|
|
|
o.AttributedTo = JSONGetItem(val, "attributedTo")
|
|
|
|
o.Attachment = JSONGetItem(val, "attachment")
|
|
|
|
o.Location = JSONGetItem(val, "location")
|
|
|
|
o.Published = JSONGetTime(val, "published")
|
|
|
|
o.StartTime = JSONGetTime(val, "startTime")
|
|
|
|
o.EndTime = JSONGetTime(val, "endTime")
|
|
|
|
o.Duration = JSONGetDuration(val, "duration")
|
|
|
|
o.Icon = JSONGetItem(val, "icon")
|
|
|
|
o.Preview = JSONGetItem(val, "preview")
|
|
|
|
o.Image = JSONGetItem(val, "image")
|
|
|
|
o.Updated = JSONGetTime(val, "updated")
|
|
|
|
o.InReplyTo = JSONGetItem(val, "inReplyTo")
|
|
|
|
o.To = JSONGetItems(val, "to")
|
|
|
|
o.Audience = JSONGetItems(val, "audience")
|
|
|
|
o.Bto = JSONGetItems(val, "bto")
|
|
|
|
o.CC = JSONGetItems(val, "cc")
|
|
|
|
o.BCC = JSONGetItems(val, "bcc")
|
|
|
|
o.Replies = JSONGetItem(val, "replies")
|
|
|
|
o.Tag = JSONGetItems(val, "tag")
|
|
|
|
o.Likes = JSONGetItem(val, "likes")
|
|
|
|
o.Shares = JSONGetItem(val, "shares")
|
|
|
|
o.Source = GetAPSource(val)
|
2019-12-21 10:05:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadIntransitiveActivity(val *fastjson.Value, i *IntransitiveActivity) error {
|
2019-12-21 10:05:38 +00:00
|
|
|
OnObject(i, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 10:05:38 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
i.Actor = JSONGetItem(val, "actor")
|
|
|
|
i.Target = JSONGetItem(val, "target")
|
|
|
|
i.Result = JSONGetItem(val, "result")
|
|
|
|
i.Origin = JSONGetItem(val, "origin")
|
|
|
|
i.Instrument = JSONGetItem(val, "instrument")
|
2019-12-21 10:05:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadActivity(val *fastjson.Value, a *Activity) error {
|
2019-12-21 10:05:38 +00:00
|
|
|
OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadIntransitiveActivity(val, i)
|
2019-12-21 10:05:38 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
a.Object = JSONGetItem(val, "object")
|
2019-12-21 10:05:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-21 14:14:43 +00:00
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadQuestion(val *fastjson.Value, q *Question) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadIntransitiveActivity(val, i)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
q.OneOf = JSONGetItem(val, "oneOf")
|
|
|
|
q.AnyOf = JSONGetItem(val, "anyOf")
|
|
|
|
q.Closed = JSONGetBoolean(val, "closed")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadActor(val *fastjson.Value, a *Actor) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(a, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
a.PreferredUsername = JSONGetNaturalLanguageField(val, "preferredUsername")
|
|
|
|
a.Followers = JSONGetItem(val, "followers")
|
|
|
|
a.Following = JSONGetItem(val, "following")
|
|
|
|
a.Inbox = JSONGetItem(val, "inbox")
|
|
|
|
a.Outbox = JSONGetItem(val, "outbox")
|
|
|
|
a.Liked = JSONGetItem(val, "liked")
|
|
|
|
a.Endpoints = JSONGetActorEndpoints(val, "endpoints")
|
|
|
|
a.Streams = JSONGetStreams(val, "streams")
|
|
|
|
a.PublicKey = JSONGetPublicKey(val, "publicKey")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadCollection(val *fastjson.Value, c *Collection) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(c, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
c.Current = JSONGetItem(val, "current")
|
|
|
|
c.First = JSONGetItem(val, "first")
|
|
|
|
c.Last = JSONGetItem(val, "last")
|
|
|
|
c.TotalItems = uint(JSONGetInt(val, "totalItems"))
|
|
|
|
c.Items = JSONGetItems(val, "items")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadCollectionPage(val *fastjson.Value, c *CollectionPage) error {
|
2020-01-14 19:57:42 +00:00
|
|
|
OnCollection(c, func(c *Collection) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadCollection(val, c)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
c.Next = JSONGetItem(val, "next")
|
|
|
|
c.Prev = JSONGetItem(val, "prev")
|
|
|
|
c.PartOf = JSONGetItem(val, "partOf")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadOrderedCollection(val *fastjson.Value, c *OrderedCollection) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(c, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
c.Current = JSONGetItem(val, "current")
|
|
|
|
c.First = JSONGetItem(val, "first")
|
|
|
|
c.Last = JSONGetItem(val, "last")
|
|
|
|
c.TotalItems = uint(JSONGetInt(val, "totalItems"))
|
|
|
|
c.OrderedItems = JSONGetItems(val, "orderedItems")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadOrderedCollectionPage(val *fastjson.Value, c *OrderedCollectionPage) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnOrderedCollection(c, func(c *OrderedCollection) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadOrderedCollection(val, c)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
c.Next = JSONGetItem(val, "next")
|
|
|
|
c.Prev = JSONGetItem(val, "prev")
|
|
|
|
c.PartOf = JSONGetItem(val, "partOf")
|
|
|
|
c.StartIndex = uint(JSONGetInt(val, "startIndex"))
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadPlace(val *fastjson.Value, p *Place) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(p, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
p.Accuracy = JSONGetFloat(val, "accuracy")
|
|
|
|
p.Altitude = JSONGetFloat(val, "altitude")
|
|
|
|
p.Latitude = JSONGetFloat(val, "latitude")
|
|
|
|
p.Longitude = JSONGetFloat(val, "longitude")
|
|
|
|
p.Radius = JSONGetInt(val, "radius")
|
|
|
|
p.Units = JSONGetString(val, "units")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadProfile(val *fastjson.Value, p *Profile) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(p, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
p.Describes = JSONGetItem(val, "describes")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadRelationship(val *fastjson.Value, r *Relationship) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(r, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
r.Subject = JSONGetItem(val, "subject")
|
|
|
|
r.Object = JSONGetItem(val, "object")
|
|
|
|
r.Relationship = JSONGetItem(val, "relationship")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadTombstone(val *fastjson.Value, t *Tombstone) error {
|
2019-12-21 14:14:43 +00:00
|
|
|
OnObject(t, func(o *Object) error {
|
2021-08-15 11:41:01 +00:00
|
|
|
return loadObject(val, o)
|
2019-12-21 14:14:43 +00:00
|
|
|
})
|
2021-08-15 11:41:01 +00:00
|
|
|
t.FormerType = ActivityVocabularyType(JSONGetString(val, "formerType"))
|
|
|
|
t.Deleted = JSONGetTime(val, "deleted")
|
2019-12-21 14:14:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-21 14:25:22 +00:00
|
|
|
|
2021-08-15 11:41:01 +00:00
|
|
|
func loadLink(val *fastjson.Value, l *Link) error {
|
2019-12-21 14:25:22 +00:00
|
|
|
if ItemTyperFunc == nil {
|
2020-11-06 18:40:46 +00:00
|
|
|
ItemTyperFunc = GetItemByType
|
2019-12-21 14:25:22 +00:00
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
l.ID = JSONGetID(val)
|
|
|
|
l.Type = JSONGetType(val)
|
|
|
|
l.MediaType = JSONGetMimeType(val, "mediaType")
|
|
|
|
l.Preview = JSONGetItem(val, "preview")
|
|
|
|
h := JSONGetInt(val, "height")
|
2019-12-22 13:08:56 +00:00
|
|
|
if h != 0 {
|
|
|
|
l.Height = uint(h)
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
w := JSONGetInt(val, "width")
|
2019-12-22 13:08:56 +00:00
|
|
|
if w != 0 {
|
|
|
|
l.Width = uint(w)
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
l.Name = JSONGetNaturalLanguageField(val, "name")
|
|
|
|
hrefLang := JSONGetLangRefField(val, "hrefLang")
|
2019-12-22 13:08:56 +00:00
|
|
|
if len(hrefLang) > 0 {
|
|
|
|
l.HrefLang = hrefLang
|
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
href := JSONGetURIItem(val, "href")
|
2019-12-22 13:08:56 +00:00
|
|
|
if href != nil {
|
|
|
|
ll := href.GetLink()
|
|
|
|
if len(ll) > 0 {
|
|
|
|
l.Href = ll
|
|
|
|
}
|
2019-12-21 14:25:22 +00:00
|
|
|
}
|
2021-08-15 11:41:01 +00:00
|
|
|
rel := JSONGetURIItem(val, "rel")
|
2019-12-22 13:08:56 +00:00
|
|
|
if rel != nil {
|
|
|
|
rr := rel.GetLink()
|
|
|
|
if len(rr) > 0 {
|
|
|
|
l.Rel = rr
|
|
|
|
}
|
2019-12-21 14:25:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-16 14:28:43 +00:00
|
|
|
|
|
|
|
func loadPublicKey(val *fastjson.Value, p *PublicKey) error {
|
|
|
|
if id := val.GetStringBytes("id"); len(id) > 0 {
|
|
|
|
p.ID = ID(id)
|
|
|
|
}
|
|
|
|
if o := val.GetStringBytes("owner"); len(o) > 0 {
|
|
|
|
p.Owner = IRI(o)
|
|
|
|
}
|
|
|
|
if pub := val.GetStringBytes("publicKeyPem"); len(pub) > 0 {
|
|
|
|
p.PublicKeyPem = string(pub)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|