2018-10-11 09:26:00 +00:00
|
|
|
package activitystreams
|
2017-09-11 20:45:57 +00:00
|
|
|
|
2017-09-12 15:25:07 +00:00
|
|
|
import (
|
2019-09-23 14:34:14 +00:00
|
|
|
"bytes"
|
2018-06-09 11:06:55 +00:00
|
|
|
"fmt"
|
2019-09-20 13:14:04 +00:00
|
|
|
"github.com/buger/jsonparser"
|
|
|
|
json "github.com/go-ap/jsonld"
|
2018-04-10 17:56:14 +00:00
|
|
|
"sort"
|
2019-10-04 14:27:22 +00:00
|
|
|
"strconv"
|
2018-07-18 14:29:47 +00:00
|
|
|
"strings"
|
2017-09-12 20:37:34 +00:00
|
|
|
"time"
|
2019-12-03 11:47:27 +00:00
|
|
|
"unsafe"
|
2017-09-12 15:25:07 +00:00
|
|
|
)
|
|
|
|
|
2018-03-27 14:16:07 +00:00
|
|
|
// ObjectID designates an unique global identifier.
|
|
|
|
// All Objects in [ActivityStreams] should have unique global identifiers.
|
|
|
|
// ActivityPub extends this requirement; all objects distributed by the ActivityPub protocol MUST
|
|
|
|
// have unique global identifiers, unless they are intentionally transient
|
|
|
|
// (short lived activities that are not intended to be able to be looked up,
|
|
|
|
// such as some kinds of chat messages or game notifications).
|
|
|
|
// These identifiers must fall into one of the following groups:
|
|
|
|
//
|
2019-02-21 16:54:11 +00:00
|
|
|
// 1. Publicly dereferenceable URIs, such as HTTPS URIs, with their authority belonging
|
2018-03-27 14:16:07 +00:00
|
|
|
// to that of their originating server. (Publicly facing content SHOULD use HTTPS URIs).
|
|
|
|
// 2. An ID explicitly specified as the JSON null object, which implies an anonymous object
|
|
|
|
// (a part of its parent context)
|
2018-10-11 18:01:11 +00:00
|
|
|
type ObjectID IRI
|
2017-09-11 20:45:57 +00:00
|
|
|
|
|
|
|
const (
|
2018-03-25 18:54:51 +00:00
|
|
|
// ActivityBaseURI the basic URI for the activity streams namespaces
|
2018-10-18 18:19:18 +00:00
|
|
|
ActivityBaseURI = IRI("https://www.w3.org/ns/activitystreams")
|
|
|
|
ObjectType ActivityVocabularyType = "Object"
|
|
|
|
LinkType ActivityVocabularyType = "Link"
|
|
|
|
ActivityType ActivityVocabularyType = "Activity"
|
|
|
|
IntransitiveActivityType ActivityVocabularyType = "IntransitiveActivity"
|
|
|
|
ActorType ActivityVocabularyType = "Actor"
|
|
|
|
CollectionType ActivityVocabularyType = "Collection"
|
|
|
|
OrderedCollectionType ActivityVocabularyType = "OrderedCollection"
|
|
|
|
CollectionPageType ActivityVocabularyType = "CollectionPage"
|
|
|
|
OrderedCollectionPageType ActivityVocabularyType = "OrderedCollectionPage"
|
2017-09-11 21:10:56 +00:00
|
|
|
|
2018-07-18 15:39:27 +00:00
|
|
|
// Activity Pub Object Types
|
2017-10-02 09:53:09 +00:00
|
|
|
ArticleType ActivityVocabularyType = "Article"
|
|
|
|
AudioType ActivityVocabularyType = "Audio"
|
|
|
|
DocumentType ActivityVocabularyType = "Document"
|
|
|
|
EventType ActivityVocabularyType = "Event"
|
|
|
|
ImageType ActivityVocabularyType = "Image"
|
|
|
|
NoteType ActivityVocabularyType = "Note"
|
|
|
|
PageType ActivityVocabularyType = "Page"
|
|
|
|
PlaceType ActivityVocabularyType = "Place"
|
|
|
|
ProfileType ActivityVocabularyType = "Profile"
|
|
|
|
RelationshipType ActivityVocabularyType = "Relationship"
|
|
|
|
TombstoneType ActivityVocabularyType = "Tombstone"
|
|
|
|
VideoType ActivityVocabularyType = "Video"
|
2017-09-11 20:45:57 +00:00
|
|
|
|
2018-03-27 14:16:07 +00:00
|
|
|
// MentionType is a link type for @mentions
|
2017-10-02 09:53:09 +00:00
|
|
|
MentionType ActivityVocabularyType = "Mention"
|
2017-09-11 20:45:57 +00:00
|
|
|
)
|
|
|
|
|
2018-08-06 11:09:24 +00:00
|
|
|
const (
|
2018-08-31 11:38:15 +00:00
|
|
|
NilLangRef LangRef = "-"
|
2018-08-06 11:09:24 +00:00
|
|
|
)
|
|
|
|
|
2019-05-16 09:08:27 +00:00
|
|
|
var GenericObjectTypes = ActivityVocabularyTypes{
|
2017-09-16 10:31:23 +00:00
|
|
|
ActivityType,
|
|
|
|
IntransitiveActivityType,
|
|
|
|
ObjectType,
|
|
|
|
ActorType,
|
2017-09-16 18:20:33 +00:00
|
|
|
CollectionType,
|
|
|
|
OrderedCollectionType,
|
2017-09-16 10:31:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 09:08:27 +00:00
|
|
|
var GenericLinkTypes = ActivityVocabularyTypes{
|
2017-10-02 09:53:09 +00:00
|
|
|
LinkType,
|
|
|
|
}
|
|
|
|
|
2019-05-16 09:08:27 +00:00
|
|
|
var GenericTypes = append(GenericObjectTypes[:], GenericLinkTypes[:]...)
|
2017-10-02 09:53:09 +00:00
|
|
|
|
2019-05-16 09:08:27 +00:00
|
|
|
var ObjectTypes = ActivityVocabularyTypes{
|
2017-09-11 20:45:57 +00:00
|
|
|
ArticleType,
|
|
|
|
AudioType,
|
|
|
|
DocumentType,
|
|
|
|
EventType,
|
|
|
|
ImageType,
|
|
|
|
NoteType,
|
|
|
|
PageType,
|
|
|
|
PlaceType,
|
|
|
|
ProfileType,
|
|
|
|
RelationshipType,
|
|
|
|
TombstoneType,
|
|
|
|
VideoType,
|
|
|
|
}
|
2017-09-11 21:10:56 +00:00
|
|
|
|
2017-09-16 18:10:41 +00:00
|
|
|
type (
|
2018-03-27 14:16:07 +00:00
|
|
|
// ActivityVocabularyType is the data type for an Activity type object
|
2017-10-02 09:53:09 +00:00
|
|
|
ActivityVocabularyType string
|
2018-07-18 15:39:27 +00:00
|
|
|
// ActivityObject is a subtype of Object that describes some form of action that may happen,
|
2018-10-18 09:48:02 +00:00
|
|
|
// is currently happening, or has already happened
|
2018-07-24 21:11:08 +00:00
|
|
|
ActivityObject interface {
|
2019-02-21 16:54:11 +00:00
|
|
|
// GetID returns the dereferenceable ActivityStreams object id
|
2018-07-25 09:47:03 +00:00
|
|
|
GetID() *ObjectID
|
2019-12-03 11:51:29 +00:00
|
|
|
// GetType returns the ActivityStreams type
|
|
|
|
GetType() ActivityVocabularyType
|
2018-07-18 19:34:08 +00:00
|
|
|
}
|
2019-12-03 11:48:25 +00:00
|
|
|
// LinkOrIRI is an interface that Object and Link structs implement, and at the same time
|
2019-02-21 16:54:11 +00:00
|
|
|
// they are kept disjointed
|
2019-12-03 11:48:25 +00:00
|
|
|
LinkOrIRI interface {
|
2019-02-21 16:54:11 +00:00
|
|
|
// GetLink returns the object id in IRI type
|
|
|
|
GetLink() IRI
|
|
|
|
}
|
|
|
|
// Item describes the requirements of an ActivityStreams object
|
2018-03-25 18:54:51 +00:00
|
|
|
ObjectOrLink interface {
|
2018-07-13 08:15:14 +00:00
|
|
|
ActivityObject
|
2019-12-03 11:48:25 +00:00
|
|
|
LinkOrIRI
|
2019-12-01 18:27:45 +00:00
|
|
|
// IsLink shows if current item represents a Link object or an IRI
|
2018-07-18 19:34:08 +00:00
|
|
|
IsLink() bool
|
2019-12-01 18:27:45 +00:00
|
|
|
// IsObject shows if current item represents an ActivityStreams object
|
2018-07-18 19:34:08 +00:00
|
|
|
IsObject() bool
|
2019-12-01 18:27:45 +00:00
|
|
|
// IsCollection shows if the current item represents an ItemCollection
|
|
|
|
IsCollection()bool
|
2017-10-02 13:59:48 +00:00
|
|
|
}
|
2019-02-21 16:54:11 +00:00
|
|
|
// Mapper interface allows external objects to implement their own mechanism for loading information
|
|
|
|
// from an ActivityStreams vocabulary object
|
|
|
|
Mapper interface {
|
|
|
|
// FromActivityStreams maps an ActivityStreams object to another struct representation
|
|
|
|
FromActivityStreams(Item) error
|
2018-07-13 08:15:14 +00:00
|
|
|
}
|
2019-02-21 16:54:11 +00:00
|
|
|
|
|
|
|
// MimeType is the type for representing MIME types in certain ActivityStreams properties
|
2018-03-25 18:54:51 +00:00
|
|
|
MimeType string
|
2019-02-21 16:54:11 +00:00
|
|
|
// LangRef is the type for a language reference code, should be an ISO639-1 language specifier.
|
2018-03-25 18:54:51 +00:00
|
|
|
LangRef string
|
2018-09-02 13:41:53 +00:00
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// LangRefValue is a type for storing per language values
|
2018-09-02 13:41:53 +00:00
|
|
|
LangRefValue struct {
|
|
|
|
Ref LangRef
|
|
|
|
Value string
|
|
|
|
}
|
2019-02-21 16:54:11 +00:00
|
|
|
// NaturalLanguageValues is a mapping for multiple language values
|
|
|
|
NaturalLanguageValues []LangRefValue
|
2017-09-16 18:10:41 +00:00
|
|
|
)
|
2017-09-12 15:25:07 +00:00
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
func NaturalLanguageValuesNew() NaturalLanguageValues {
|
|
|
|
return make(NaturalLanguageValues, 0)
|
2018-09-02 13:41:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 09:37:25 +00:00
|
|
|
func (n NaturalLanguageValues) String() string {
|
|
|
|
cnt := len(n)
|
|
|
|
if cnt == 1 {
|
|
|
|
return n[0].String()
|
|
|
|
}
|
|
|
|
s := strings.Builder{}
|
|
|
|
s.Write([]byte{'['})
|
|
|
|
for k, v := range n {
|
|
|
|
s.WriteString(v.String())
|
2019-09-09 14:13:59 +00:00
|
|
|
if k != cnt-1 {
|
2019-07-05 09:37:25 +00:00
|
|
|
s.Write([]byte{','})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Write([]byte{']'})
|
|
|
|
return s.String()
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
func (n NaturalLanguageValues) Get(ref LangRef) string {
|
2018-09-02 13:41:53 +00:00
|
|
|
for _, val := range n {
|
|
|
|
if val.Ref == ref {
|
|
|
|
return val.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// Set sets a language, value pair in a NaturalLanguageValues array
|
|
|
|
func (n *NaturalLanguageValues) Set(ref LangRef, v string) error {
|
|
|
|
found := false
|
|
|
|
for k, vv := range *n {
|
|
|
|
if vv.Ref == ref {
|
|
|
|
(*n)[k] = LangRefValue{ref, v}
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
n.Append(ref, v)
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-06 14:07:45 +00:00
|
|
|
|
2018-07-18 15:39:27 +00:00
|
|
|
// IsLink validates if currentActivity Pub Object is a Link
|
2018-06-09 12:05:46 +00:00
|
|
|
func (o Object) IsLink() bool {
|
|
|
|
return false
|
2017-10-02 09:53:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:39:27 +00:00
|
|
|
// IsObject validates if currentActivity Pub Object is an Object
|
2018-06-09 12:05:46 +00:00
|
|
|
func (o Object) IsObject() bool {
|
|
|
|
return true
|
2017-10-02 09:53:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-01 18:27:45 +00:00
|
|
|
// IsCollection returns false for Object objects
|
|
|
|
func (o Object) IsCollection() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// MarshalJSON serializes the NaturalLanguageValues into JSON
|
|
|
|
func (n NaturalLanguageValues) MarshalJSON() ([]byte, error) {
|
2018-09-02 13:41:53 +00:00
|
|
|
if len(n) == 0 {
|
|
|
|
return json.Marshal(nil)
|
|
|
|
}
|
2017-09-16 18:10:41 +00:00
|
|
|
if len(n) == 1 {
|
2019-10-04 07:27:57 +00:00
|
|
|
v := n[0]
|
|
|
|
if v.Ref == NilLangRef {
|
2019-10-04 14:27:22 +00:00
|
|
|
return v.MarshalJSON()
|
2017-09-12 20:37:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
mm := make(map[LangRef]string)
|
|
|
|
for _, val := range n {
|
|
|
|
mm[val.Ref] = val.Value
|
|
|
|
}
|
2017-09-12 20:36:13 +00:00
|
|
|
|
2018-09-02 13:41:53 +00:00
|
|
|
return json.Marshal(mm)
|
2017-09-12 20:36:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// First returns the first element in the array
|
2019-07-05 09:37:25 +00:00
|
|
|
func (n NaturalLanguageValues) First() LangRefValue {
|
2018-07-25 09:47:41 +00:00
|
|
|
for _, v := range n {
|
2019-07-05 09:37:25 +00:00
|
|
|
return v
|
2018-07-25 09:47:41 +00:00
|
|
|
}
|
2019-07-05 09:37:25 +00:00
|
|
|
return LangRefValue{}
|
2018-07-25 09:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// MarshalText serializes the NaturalLanguageValues into Text
|
|
|
|
func (n NaturalLanguageValues) MarshalText() ([]byte, error) {
|
2018-07-24 21:11:08 +00:00
|
|
|
for _, v := range n {
|
|
|
|
return []byte(fmt.Sprintf("%q", v)), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:54:11 +00:00
|
|
|
// Append is syntactic sugar for resizing the NaturalLanguageValues map
|
2018-10-18 09:48:02 +00:00
|
|
|
// and appending an element
|
2019-02-21 16:54:11 +00:00
|
|
|
func (n *NaturalLanguageValues) Append(lang LangRef, value string) error {
|
|
|
|
var t NaturalLanguageValues
|
2018-06-07 14:32:26 +00:00
|
|
|
if len(*n) == 0 {
|
2019-02-21 16:54:11 +00:00
|
|
|
t = make(NaturalLanguageValues, 0)
|
2018-06-07 14:32:26 +00:00
|
|
|
} else {
|
|
|
|
t = *n
|
|
|
|
}
|
2018-09-02 13:41:53 +00:00
|
|
|
t = append(*n, LangRefValue{lang, value})
|
2018-06-07 14:32:26 +00:00
|
|
|
*n = t
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-02 14:45:37 +00:00
|
|
|
// Count returns the length of Items in the item collection
|
|
|
|
func (n *NaturalLanguageValues) Count() uint {
|
|
|
|
return uint(len(*n))
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:37:25 +00:00
|
|
|
// String adds support for Stringer interface. It returns the Value[LangRef] text or just Value if LangRef is NIL
|
2019-07-06 10:15:00 +00:00
|
|
|
func (l LangRefValue) String() string {
|
2019-07-05 09:37:25 +00:00
|
|
|
if l.Ref == NilLangRef {
|
|
|
|
return l.Value
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s[%s]", l.Value, l.Ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the JsonEncoder interface
|
|
|
|
func (l *LangRefValue) UnmarshalJSON(data []byte) error {
|
2019-10-04 14:27:22 +00:00
|
|
|
_, typ, _, err := jsonparser.Get(data)
|
2019-09-23 14:34:14 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Ref = NilLangRef
|
2019-10-04 14:27:22 +00:00
|
|
|
l.Value = string(unescape(data))
|
2019-09-23 14:34:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case jsonparser.Object:
|
|
|
|
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
|
|
|
|
l.Ref = LangRef(key)
|
2019-09-24 05:54:40 +00:00
|
|
|
l.Value = string(unescape(value))
|
2019-09-23 14:34:14 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
case jsonparser.String:
|
|
|
|
l.Ref = NilLangRef
|
2019-09-24 05:54:40 +00:00
|
|
|
l.Value = string(unescape(data))
|
2019-09-23 14:34:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 09:37:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements the TextEncoder interface
|
|
|
|
func (l *LangRefValue) UnmarshalText(data []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-04 14:27:22 +00:00
|
|
|
// MarshalJSON serializes the LangRefValue into JSON
|
|
|
|
func (l LangRefValue) MarshalJSON() ([]byte, error) {
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
if l.Ref != NilLangRef {
|
|
|
|
if l.Value == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
buf.WriteByte('"')
|
|
|
|
buf.WriteString(string(l.Ref))
|
|
|
|
buf.Write([]byte{'"', ':'})
|
|
|
|
}
|
2019-11-18 18:41:39 +00:00
|
|
|
l.Value = string(unescape([]byte(l.Value)))
|
2019-10-04 14:27:22 +00:00
|
|
|
buf.WriteString(strconv.Quote(l.Value))
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText serializes the LangRefValue into JSON
|
|
|
|
func (l LangRefValue) MarshalText() ([]byte, error) {
|
|
|
|
if l.Ref != NilLangRef && l.Value == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
buf.WriteString(l.Value)
|
|
|
|
if l.Ref != NilLangRef {
|
|
|
|
buf.WriteByte('[')
|
|
|
|
buf.WriteString(string(l.Ref))
|
|
|
|
buf.WriteByte(']')
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:37:25 +00:00
|
|
|
// UnmarshalJSON implements the JsonEncoder interface
|
2018-06-07 14:32:26 +00:00
|
|
|
func (l *LangRef) UnmarshalJSON(data []byte) error {
|
2018-11-10 20:57:47 +00:00
|
|
|
return l.UnmarshalText(data)
|
2018-06-07 14:32:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 09:37:25 +00:00
|
|
|
// UnmarshalText implements the TextEncoder interface
|
2018-07-24 21:11:08 +00:00
|
|
|
func (l *LangRef) UnmarshalText(data []byte) error {
|
2018-11-10 20:57:47 +00:00
|
|
|
*l = LangRef("")
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(data) > 2 {
|
|
|
|
if data[0] == '"' && data[len(data)-1] == '"' {
|
|
|
|
*l = LangRef(data[1 : len(data)-1])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*l = LangRef(data)
|
|
|
|
}
|
2018-07-24 21:11:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-24 05:54:40 +00:00
|
|
|
func unescape(b []byte) []byte {
|
|
|
|
// FIMXE(marius): I feel like I'm missing something really obvious about encoding/decoding from Json regarding
|
|
|
|
// escape characters, and that this function is just a hack. Be better future Marius, find the real problem!
|
2019-09-24 11:29:49 +00:00
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 'a'}, []byte{'\a'})
|
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 'f'}, []byte{'\f'})
|
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 'n'}, []byte{'\n'})
|
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 'r'}, []byte{'\r'})
|
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 't'}, []byte{'\t'})
|
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', 'v'}, []byte{'\v'})
|
2019-11-18 18:41:39 +00:00
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', '"'}, []byte{'"'})
|
2019-10-04 07:27:57 +00:00
|
|
|
b = bytes.ReplaceAll(b, []byte{'\\', '\\'}, []byte{'\\'}) // this should cover the case of \\u -> \u
|
2019-09-24 05:54:40 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-06-07 14:32:26 +00:00
|
|
|
// UnmarshalJSON tries to load the NaturalLanguage array from the incoming json value
|
2019-02-21 16:54:11 +00:00
|
|
|
func (n *NaturalLanguageValues) UnmarshalJSON(data []byte) error {
|
2018-08-31 11:40:42 +00:00
|
|
|
val, typ, _, err := jsonparser.Get(data)
|
|
|
|
if err != nil {
|
2018-12-16 12:19:19 +00:00
|
|
|
// try our luck if data contains an unquoted string
|
2019-11-18 18:41:39 +00:00
|
|
|
n.Append(NilLangRef, string(unescape(data)))
|
2018-08-31 11:40:42 +00:00
|
|
|
return nil
|
2018-07-24 21:11:08 +00:00
|
|
|
}
|
2018-08-31 11:40:42 +00:00
|
|
|
switch typ {
|
|
|
|
case jsonparser.Object:
|
2019-11-18 18:41:39 +00:00
|
|
|
jsonparser.ObjectEach(data, func(key []byte, val []byte, dataType jsonparser.ValueType, offset int) error {
|
|
|
|
n.Append(LangRef(key), string(unescape(val)))
|
2018-08-31 11:40:42 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
case jsonparser.String:
|
2019-09-24 05:54:40 +00:00
|
|
|
n.Append(NilLangRef, string(unescape(val)))
|
2019-09-23 14:34:14 +00:00
|
|
|
case jsonparser.Array:
|
|
|
|
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
|
|
|
|
l := LangRefValue{}
|
|
|
|
l.UnmarshalJSON(value)
|
|
|
|
n.Append(l.Ref, l.Value)
|
|
|
|
})
|
2018-06-09 11:06:55 +00:00
|
|
|
}
|
2018-08-31 11:40:42 +00:00
|
|
|
|
2018-06-07 14:32:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:08 +00:00
|
|
|
// UnmarshalText tries to load the NaturalLanguage array from the incoming Text value
|
2019-02-21 16:54:11 +00:00
|
|
|
func (n *NaturalLanguageValues) UnmarshalText(data []byte) error {
|
2018-07-24 21:11:08 +00:00
|
|
|
if data[0] == '"' {
|
|
|
|
// a quoted string - loading it to c.URL
|
|
|
|
if data[len(data)-1] != '"' {
|
2019-11-23 19:36:16 +00:00
|
|
|
return fmt.Errorf("invalid string value when unmarshaling %T value", n)
|
2018-07-24 21:11:08 +00:00
|
|
|
}
|
2018-08-31 11:38:15 +00:00
|
|
|
n.Append(LangRef(NilLangRef), string(data[1:len(data)-1]))
|
2018-07-24 21:11:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-23 14:22:56 +00:00
|
|
|
type object struct {
|
2018-10-24 09:28:55 +00:00
|
|
|
// ID provides the globally unique identifier for anActivity Pub Object or Link.
|
2018-04-08 16:55:25 +00:00
|
|
|
ID ObjectID `jsonld:"id,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
|
2017-10-02 09:53:09 +00:00
|
|
|
Type ActivityVocabularyType `jsonld:"type,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Name a simple, human-readable, plain-text name for the object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
|
2019-02-21 16:54:11 +00:00
|
|
|
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
|
2017-09-12 15:25:07 +00:00
|
|
|
// The intent is to provide a model that is at least semantically similar to attachments in email.
|
2018-10-04 18:33:32 +00:00
|
|
|
Attachment Item `jsonld:"attachment,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
|
2017-09-12 15:25:07 +00:00
|
|
|
// For instance, an object might be attributed to the completion of another activity.
|
2018-10-04 18:33:32 +00:00
|
|
|
AttributedTo Item `jsonld:"attributedTo,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Audience identifies one or more entities that represent the total population of entities
|
2018-10-18 09:48:02 +00:00
|
|
|
// for which the object can considered to be relevant.
|
2019-05-04 22:15:23 +00:00
|
|
|
Audience ItemCollection `jsonld:"audience,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
|
2017-09-12 15:25:07 +00:00
|
|
|
// By default, the value of content is HTML.
|
|
|
|
// The mediaType property can be used in the object to indicate a different content type.
|
|
|
|
// (The content MAY be expressed using multiple language-tagged values.)
|
2019-02-21 16:54:11 +00:00
|
|
|
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Context identifies the context within which the object exists or an activity was performed.
|
2017-09-12 15:25:07 +00:00
|
|
|
// The notion of "context" used is intentionally vague.
|
|
|
|
// The intended function is to serve as a means of grouping objects and activities that share a
|
2018-10-18 09:48:02 +00:00
|
|
|
// common originating context or purpose. An example could be all activities relating to a common project or event.
|
2018-10-04 18:33:32 +00:00
|
|
|
Context Item `jsonld:"context,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
|
2018-07-18 14:19:50 +00:00
|
|
|
// If not specified, the content property is assumed to contain text/html content.
|
|
|
|
MediaType MimeType `jsonld:"mediaType,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// EndTime the date and time describing the actual or expected ending time of the object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// When used with an Activity object, for instance, the endTime property specifies the moment
|
2018-10-18 09:48:02 +00:00
|
|
|
// the activity concluded or is expected to conclude.
|
2017-10-02 09:53:09 +00:00
|
|
|
EndTime time.Time `jsonld:"endTime,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Generator identifies the entity (e.g. an application) that generated the object.
|
2018-10-04 18:33:32 +00:00
|
|
|
Generator Item `jsonld:"generator,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Icon indicates an entity that describes an icon for this object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// The image should have an aspect ratio of one (horizontal) to one (vertical)
|
2018-10-18 09:48:02 +00:00
|
|
|
// and should be suitable for presentation at a small size.
|
2018-10-11 19:47:31 +00:00
|
|
|
Icon Item `jsonld:"icon,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Image indicates an entity that describes an image for this object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
|
2018-10-11 19:47:31 +00:00
|
|
|
Image Item `jsonld:"image,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// InReplyTo indicates one or more entities for which this object is considered a response.
|
2019-12-01 15:56:35 +00:00
|
|
|
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Location indicates one or more physical or logical locations associated with the object.
|
2018-10-04 18:33:32 +00:00
|
|
|
Location Item `jsonld:"location,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Preview identifies an entity that provides a preview of this object.
|
2018-10-04 18:33:32 +00:00
|
|
|
Preview Item `jsonld:"preview,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Published the date and time at which the object was published
|
2017-10-02 09:53:09 +00:00
|
|
|
Published time.Time `jsonld:"published,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Replies identifies a Collection containing objects considered to be responses to this object.
|
2018-10-04 18:33:32 +00:00
|
|
|
Replies Item `jsonld:"replies,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// StartTime the date and time describing the actual or expected starting time of the object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// When used with an Activity object, for instance, the startTime property specifies
|
2018-10-18 09:48:02 +00:00
|
|
|
// the moment the activity began or is scheduled to begin.
|
2017-10-02 09:53:09 +00:00
|
|
|
StartTime time.Time `jsonld:"startTime,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Summary a natural language summarization of the object encoded as HTML.
|
2017-09-12 15:25:07 +00:00
|
|
|
// *Multiple language tagged summaries may be provided.)
|
2019-02-21 16:54:11 +00:00
|
|
|
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
|
2017-09-12 15:25:07 +00:00
|
|
|
// The key difference between attachment and tag is that the former implies association by inclusion,
|
2018-10-18 09:48:02 +00:00
|
|
|
// while the latter implies associated by reference.
|
2018-11-14 11:51:35 +00:00
|
|
|
Tag ItemCollection `jsonld:"tag,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Updated the date and time at which the object was updated
|
2017-10-02 09:53:09 +00:00
|
|
|
Updated time.Time `jsonld:"updated,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// URL identifies one or more links to representations of the object
|
2019-12-03 11:48:25 +00:00
|
|
|
URL LinkOrIRI `jsonld:"url,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
|
2018-10-04 18:33:32 +00:00
|
|
|
To ItemCollection `jsonld:"to,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
|
2019-11-24 08:28:32 +00:00
|
|
|
Bto ItemCollection `jsonld:"bto,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
|
2018-10-04 18:33:32 +00:00
|
|
|
CC ItemCollection `jsonld:"cc,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
|
2019-11-24 08:28:32 +00:00
|
|
|
BCC ItemCollection `jsonld:"bcc,omitempty"`
|
2018-10-24 09:28:55 +00:00
|
|
|
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
|
2018-10-18 09:48:02 +00:00
|
|
|
// the duration property indicates the object's approximate duration.
|
2017-09-12 15:25:07 +00:00
|
|
|
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
|
2018-10-18 09:48:02 +00:00
|
|
|
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
|
2017-10-02 09:53:09 +00:00
|
|
|
Duration time.Duration `jsonld:"duration,omitempty"`
|
2017-09-12 15:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:10:17 +00:00
|
|
|
type (
|
2018-11-23 14:22:56 +00:00
|
|
|
Parent = object
|
|
|
|
// Describes an object of any kind.
|
|
|
|
// The Activity Pub Object type serves as the base type for most of the other kinds of objects defined in the Activity Vocabulary,
|
|
|
|
// including other Core types such as Activity, IntransitiveActivity, Collection and OrderedCollection.
|
|
|
|
Object = object
|
2018-10-24 09:28:55 +00:00
|
|
|
// Article represents any kind of multi-paragraph written work.
|
2019-01-30 19:50:50 +00:00
|
|
|
Article = Object
|
2018-10-24 09:28:55 +00:00
|
|
|
// Audio represents an audio document of any kind.
|
2019-01-30 19:50:50 +00:00
|
|
|
Audio = Document
|
2018-10-24 09:28:55 +00:00
|
|
|
// Document represents a document of any kind.
|
2019-01-30 19:50:50 +00:00
|
|
|
Document = Object
|
2018-10-24 09:28:55 +00:00
|
|
|
// Event represents any kind of event.
|
2019-01-30 19:50:50 +00:00
|
|
|
Event = Object
|
2018-10-11 19:10:17 +00:00
|
|
|
// Image An image document of any kind
|
2019-01-30 19:50:50 +00:00
|
|
|
Image = Document
|
2018-10-24 09:28:55 +00:00
|
|
|
// Note represents a short written work typically less than a single paragraph in length.
|
2019-01-30 19:50:50 +00:00
|
|
|
Note = Object
|
2018-10-24 09:28:55 +00:00
|
|
|
// Page represents a Web Page.
|
2019-01-30 19:50:50 +00:00
|
|
|
Page = Document
|
2018-10-24 09:28:55 +00:00
|
|
|
// Video represents a video document of any kind
|
2019-01-30 19:50:50 +00:00
|
|
|
Video = Document
|
2018-10-11 19:10:17 +00:00
|
|
|
)
|
|
|
|
|
2018-10-24 09:28:55 +00:00
|
|
|
// Profile a Profile is a content object that describes another Object,
|
2018-10-11 19:10:17 +00:00
|
|
|
// typically used to describe Actor Type objects.
|
2018-10-24 09:28:55 +00:00
|
|
|
// The describes property is used to reference the object being described by the profile.
|
2018-10-11 19:10:17 +00:00
|
|
|
type Profile struct {
|
2018-11-23 14:22:56 +00:00
|
|
|
Parent
|
2018-10-11 19:10:17 +00:00
|
|
|
// Describes On a Profile object, the describes property identifies the object described by the Profile.
|
|
|
|
Describes Item `jsonld:"describes,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:28:55 +00:00
|
|
|
// Relationship describes a relationship between two individuals.
|
2018-10-11 19:10:17 +00:00
|
|
|
// The subject and object properties are used to identify the connected individuals.
|
|
|
|
//See 5.2 Representing Relationships Between Entities for additional information.
|
|
|
|
// 5.2: The relationship property specifies the kind of relationship that exists between the two individuals identified
|
|
|
|
// by the subject and object properties. Used together, these three properties form what is commonly known
|
|
|
|
// as a "reified statement" where subject identifies the subject, relationship identifies the predicate,
|
|
|
|
// and object identifies the object.
|
|
|
|
type Relationship struct {
|
2018-11-23 14:22:56 +00:00
|
|
|
Parent
|
2018-10-24 09:28:55 +00:00
|
|
|
// Subject Subject On a Relationship object, the subject property identifies one of the connected individuals.
|
2018-10-11 19:10:17 +00:00
|
|
|
// For instance, for a Relationship object describing "John is related to Sally", subject would refer to John.
|
|
|
|
Subject Item
|
|
|
|
// Object
|
|
|
|
Object Item
|
|
|
|
// Relationship On a Relationship object, the relationship property identifies the kind
|
|
|
|
// of relationship that exists between subject and object.
|
|
|
|
Relationship Item
|
|
|
|
}
|
|
|
|
|
2018-10-24 09:28:55 +00:00
|
|
|
// Tombstone a Tombstone represents a content object that has been deleted.
|
2018-10-11 19:10:17 +00:00
|
|
|
// It can be used in Collections to signify that there used to be an object at this position,
|
|
|
|
// but it has been deleted.
|
|
|
|
type Tombstone struct {
|
2018-11-23 14:22:56 +00:00
|
|
|
Parent
|
2018-10-11 19:10:17 +00:00
|
|
|
// FormerType On a Tombstone object, the formerType property identifies the type of the object that was deleted.
|
2018-12-16 12:19:19 +00:00
|
|
|
FormerType ActivityVocabularyType `jsonld:"formerType,omitempty"`
|
2018-10-11 19:10:17 +00:00
|
|
|
// Deleted On a Tombstone object, the deleted property is a timestamp for when the object was deleted.
|
2018-12-16 12:19:19 +00:00
|
|
|
Deleted time.Time `jsonld:"deleted,omitempty"`
|
2018-10-11 19:10:17 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 15:39:27 +00:00
|
|
|
// ObjectNew initializes a new Object
|
2018-10-11 09:26:00 +00:00
|
|
|
func ObjectNew(typ ActivityVocabularyType) *Object {
|
2019-05-16 09:08:27 +00:00
|
|
|
if !(ObjectTypes.Contains(typ)) {
|
2018-06-09 12:05:46 +00:00
|
|
|
typ = ObjectType
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
2018-10-11 09:26:00 +00:00
|
|
|
o := Object{Type: typ}
|
2019-02-21 16:54:11 +00:00
|
|
|
o.Name = NaturalLanguageValuesNew()
|
|
|
|
o.Content = NaturalLanguageValuesNew()
|
2018-06-09 12:05:46 +00:00
|
|
|
return &o
|
2017-09-11 20:45:57 +00:00
|
|
|
}
|
2018-04-08 16:10:54 +00:00
|
|
|
|
2019-12-03 12:05:11 +00:00
|
|
|
// GetID returns the ObjectID corresponding to the current Object
|
2018-07-25 09:47:03 +00:00
|
|
|
func (o Object) GetID() *ObjectID {
|
|
|
|
return &o.ID
|
2018-04-08 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:05:11 +00:00
|
|
|
// GetLink returns the IRI corresponding to the current Object
|
2018-10-11 18:13:34 +00:00
|
|
|
func (o Object) GetLink() IRI {
|
|
|
|
return IRI(o.ID)
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:05:11 +00:00
|
|
|
// GetType returns the type of the current Object
|
2018-06-09 12:05:46 +00:00
|
|
|
func (o Object) GetType() ActivityVocabularyType {
|
|
|
|
return o.Type
|
2018-04-08 16:10:54 +00:00
|
|
|
}
|
2018-04-10 15:53:05 +00:00
|
|
|
|
2019-08-22 18:48:02 +00:00
|
|
|
// ItemCollectionDeduplication normalizes the received arguments lists into a single unified one
|
|
|
|
func ItemCollectionDeduplication(recCols ...*ItemCollection) (ItemCollection, error) {
|
2019-05-26 20:25:44 +00:00
|
|
|
rec := make(ItemCollection, 0)
|
2018-04-10 15:53:05 +00:00
|
|
|
|
2019-05-26 20:25:44 +00:00
|
|
|
for _, recCol := range recCols {
|
|
|
|
if recCol == nil {
|
2018-04-10 15:53:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-04-10 18:09:06 +00:00
|
|
|
|
2018-04-10 17:56:14 +00:00
|
|
|
toRemove := make([]int, 0)
|
2019-05-26 20:25:44 +00:00
|
|
|
for i, cur := range *recCol {
|
2018-04-10 15:53:05 +00:00
|
|
|
save := true
|
2019-05-26 20:25:44 +00:00
|
|
|
if cur == nil {
|
2018-09-06 14:07:45 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-05-26 20:25:44 +00:00
|
|
|
var testIt Item
|
|
|
|
if cur.IsObject() {
|
|
|
|
testIt = cur
|
|
|
|
} else if cur.IsLink() {
|
|
|
|
testIt = cur.GetLink()
|
2018-09-06 14:07:45 +00:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-26 20:25:44 +00:00
|
|
|
for _, it := range rec {
|
|
|
|
if testIt == it {
|
2018-04-10 17:56:14 +00:00
|
|
|
// mark the element for removal
|
|
|
|
toRemove = append(toRemove, i)
|
2018-04-10 15:53:05 +00:00
|
|
|
save = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if save {
|
2019-05-26 20:25:44 +00:00
|
|
|
rec = append(rec, testIt)
|
2018-04-10 15:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-10 17:56:14 +00:00
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(sort.IntSlice(toRemove)))
|
|
|
|
for _, idx := range toRemove {
|
2019-05-26 20:25:44 +00:00
|
|
|
*recCol = append((*recCol)[:idx], (*recCol)[idx+1:]...)
|
2018-04-10 17:56:14 +00:00
|
|
|
}
|
2018-04-10 15:53:05 +00:00
|
|
|
}
|
2019-05-26 20:25:44 +00:00
|
|
|
return rec, nil
|
2018-04-10 15:53:05 +00:00
|
|
|
}
|
2018-06-15 21:03:55 +00:00
|
|
|
|
2018-07-18 14:29:47 +00:00
|
|
|
// UnmarshalJSON
|
|
|
|
func (i *ObjectID) UnmarshalJSON(data []byte) error {
|
|
|
|
*i = ObjectID(strings.Trim(string(data), "\""))
|
2018-06-15 21:03:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:29:47 +00:00
|
|
|
// UnmarshalJSON
|
2018-11-21 11:25:12 +00:00
|
|
|
func (c *MimeType) UnmarshalJSON(data []byte) error {
|
|
|
|
*c = MimeType(strings.Trim(string(data), "\""))
|
2018-06-15 21:03:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-24 21:11:08 +00:00
|
|
|
// UnmarshalJSON
|
|
|
|
func (o *Object) UnmarshalJSON(data []byte) error {
|
2019-05-18 20:45:54 +00:00
|
|
|
if ItemTyperFunc == nil {
|
|
|
|
ItemTyperFunc = JSONGetItemByType
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
o.ID = JSONGetObjectID(data)
|
|
|
|
o.Type = JSONGetType(data)
|
|
|
|
o.Name = JSONGetNaturalLanguageField(data, "name")
|
|
|
|
o.Content = JSONGetNaturalLanguageField(data, "content")
|
|
|
|
o.Summary = JSONGetNaturalLanguageField(data, "summary")
|
|
|
|
o.Context = JSONGetItem(data, "context")
|
|
|
|
o.URL = JSONGetURIItem(data, "url")
|
|
|
|
o.MediaType = MimeType(JSONGetString(data, "mediaType"))
|
|
|
|
o.Generator = JSONGetItem(data, "generator")
|
|
|
|
o.AttributedTo = JSONGetItem(data, "attributedTo")
|
2019-05-04 22:15:23 +00:00
|
|
|
o.Attachment = JSONGetItem(data, "attachment")
|
|
|
|
o.Location = JSONGetItem(data, "location")
|
2019-01-30 15:19:29 +00:00
|
|
|
o.Published = JSONGetTime(data, "published")
|
|
|
|
o.StartTime = JSONGetTime(data, "startTime")
|
|
|
|
o.EndTime = JSONGetTime(data, "endTime")
|
|
|
|
o.Duration = JSONGetDuration(data, "duration")
|
|
|
|
o.Icon = JSONGetItem(data, "icon")
|
2019-05-04 22:15:23 +00:00
|
|
|
o.Preview = JSONGetItem(data, "preview")
|
2019-01-30 15:19:29 +00:00
|
|
|
o.Image = JSONGetItem(data, "image")
|
|
|
|
o.Updated = JSONGetTime(data, "updated")
|
2019-09-20 13:14:04 +00:00
|
|
|
inReplyTo := JSONGetItems(data, "inReplyTo")
|
|
|
|
if len(inReplyTo) > 0 {
|
|
|
|
o.InReplyTo = inReplyTo
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
to := JSONGetItems(data, "to")
|
2019-09-20 13:14:04 +00:00
|
|
|
if len(to) > 0 {
|
2018-08-31 15:56:18 +00:00
|
|
|
o.To = to
|
|
|
|
}
|
2019-05-04 22:15:23 +00:00
|
|
|
audience := JSONGetItems(data, "audience")
|
2019-09-20 13:14:04 +00:00
|
|
|
if len(audience) > 0 {
|
2019-05-04 22:15:23 +00:00
|
|
|
o.Audience = audience
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
bto := JSONGetItems(data, "bto")
|
2019-09-20 13:14:04 +00:00
|
|
|
if len(bto) > 0 {
|
2018-12-08 13:17:47 +00:00
|
|
|
o.Bto = bto
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
cc := JSONGetItems(data, "cc")
|
2019-09-24 06:51:56 +00:00
|
|
|
if len(cc) > 0 {
|
2018-12-08 13:17:47 +00:00
|
|
|
o.CC = cc
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
bcc := JSONGetItems(data, "bcc")
|
2019-09-24 06:51:56 +00:00
|
|
|
if len(bcc) > 0 {
|
2018-12-08 13:17:47 +00:00
|
|
|
o.BCC = bcc
|
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
replies := JSONGetItem(data, "replies")
|
2018-12-02 15:50:40 +00:00
|
|
|
if replies != nil {
|
|
|
|
o.Replies = replies
|
2018-08-15 09:05:36 +00:00
|
|
|
}
|
2019-01-30 15:19:29 +00:00
|
|
|
tag := JSONGetItems(data, "tag")
|
2019-09-24 06:51:56 +00:00
|
|
|
if len(tag) > 0 {
|
2018-11-14 12:42:04 +00:00
|
|
|
o.Tag = tag
|
|
|
|
}
|
2018-07-24 21:11:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-04 22:52:14 +00:00
|
|
|
|
2019-05-24 18:20:00 +00:00
|
|
|
// ToProfile
|
|
|
|
func ToProfile(it Item) (*Profile, error) {
|
|
|
|
switch i := it.(type) {
|
|
|
|
case *Profile:
|
|
|
|
return i, nil
|
|
|
|
case Profile:
|
|
|
|
return &i, nil
|
2019-09-20 13:14:04 +00:00
|
|
|
case *Object:
|
|
|
|
return &Profile{Parent: *i}, nil
|
|
|
|
case Object:
|
|
|
|
return &Profile{Parent: i}, nil
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
2019-09-20 13:14:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToRelationship
|
|
|
|
func ToRelationship(it Item) (*Relationship, error) {
|
|
|
|
switch i := it.(type) {
|
|
|
|
case *Relationship:
|
|
|
|
return i, nil
|
|
|
|
case Relationship:
|
|
|
|
return &i, nil
|
2019-09-20 13:14:04 +00:00
|
|
|
case *Object:
|
|
|
|
return &Relationship{Parent: *i}, nil
|
|
|
|
case Object:
|
|
|
|
return &Relationship{Parent: i}, nil
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
2019-09-20 13:14:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToPlace
|
|
|
|
func ToPlace(it Item) (*Place, error) {
|
|
|
|
switch i := it.(type) {
|
|
|
|
case *Place:
|
|
|
|
return i, nil
|
|
|
|
case Place:
|
|
|
|
return &i, nil
|
2019-09-20 13:14:04 +00:00
|
|
|
case *Object:
|
2019-12-03 12:05:11 +00:00
|
|
|
// FIXME(marius): **memory_safety** Place has extra properties which will point to invalid memory
|
|
|
|
// we need a safe version for converting from smaller objects to larger ones
|
|
|
|
return (*Place)(unsafe.Pointer(i)), nil
|
2019-09-20 13:14:04 +00:00
|
|
|
case Object:
|
2019-12-03 12:05:11 +00:00
|
|
|
// FIXME(marius): **memory_safety** Place has extra properties which will point to invalid memory
|
|
|
|
// we need a safe version for converting from smaller objects to larger ones
|
|
|
|
return (*Place)(unsafe.Pointer(&i)), nil
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
2019-09-20 13:14:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToTombstone
|
|
|
|
func ToTombstone(it Item) (*Tombstone, error) {
|
|
|
|
switch i := it.(type) {
|
|
|
|
case *Tombstone:
|
|
|
|
return i, nil
|
|
|
|
case Tombstone:
|
|
|
|
return &i, nil
|
2019-09-20 13:14:04 +00:00
|
|
|
case *Object:
|
|
|
|
return &Tombstone{Parent: *i}, nil
|
|
|
|
case Object:
|
|
|
|
return &Tombstone{Parent: i}, nil
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
2019-09-20 13:14:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
2019-05-24 18:20:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 22:52:14 +00:00
|
|
|
// ToObject
|
2019-05-07 21:11:15 +00:00
|
|
|
func ToObject(it Item) (*Object, error) {
|
2019-05-04 22:52:14 +00:00
|
|
|
switch i := it.(type) {
|
|
|
|
case *Object:
|
2019-05-07 21:11:15 +00:00
|
|
|
return i, nil
|
2019-05-04 22:52:14 +00:00
|
|
|
case Object:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case *Place:
|
2019-12-03 12:05:11 +00:00
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case Place:
|
2019-12-03 12:05:11 +00:00
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case *Profile:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case Profile:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case *Relationship:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case Relationship:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case *Tombstone:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-05-05 12:30:26 +00:00
|
|
|
case Tombstone:
|
2019-05-07 21:11:15 +00:00
|
|
|
return &i.Parent, nil
|
2019-12-03 11:47:27 +00:00
|
|
|
case *Activity:
|
|
|
|
return &i.Parent, nil
|
|
|
|
case Activity:
|
|
|
|
return &i.Parent, nil
|
|
|
|
case *IntransitiveActivity:
|
|
|
|
return (*Object)(unsafe.Pointer(&i.Parent)), nil
|
|
|
|
case IntransitiveActivity:
|
|
|
|
return (*Object)(unsafe.Pointer(&i.Parent)), nil
|
|
|
|
case *Question:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case Question:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
2019-05-04 22:52:14 +00:00
|
|
|
}
|
2019-09-20 13:14:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
2019-05-04 22:52:14 +00:00
|
|
|
}
|
2019-05-11 08:31:19 +00:00
|
|
|
|
2019-05-11 09:53:32 +00:00
|
|
|
// FlattenObjectProperties flattens the Object's properties from Object types to IRI
|
2019-06-11 16:08:39 +00:00
|
|
|
func FlattenObjectProperties(o *Object) *Object {
|
2019-05-11 08:31:19 +00:00
|
|
|
if o.Replies != nil && o.Replies.IsObject() {
|
|
|
|
if len(o.Replies.GetLink()) > 0 {
|
|
|
|
o.Replies = o.Replies.GetLink()
|
|
|
|
} else {
|
|
|
|
o.Replies = IRI(fmt.Sprintf("%s/replies", o.GetLink()))
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 18:47:31 +00:00
|
|
|
o.AttributedTo = Flatten(o.AttributedTo)
|
2019-05-11 09:53:32 +00:00
|
|
|
o.To = FlattenItemCollection(o.To)
|
|
|
|
o.Bto = FlattenItemCollection(o.Bto)
|
|
|
|
o.CC = FlattenItemCollection(o.CC)
|
|
|
|
o.BCC = FlattenItemCollection(o.BCC)
|
|
|
|
o.Audience = FlattenItemCollection(o.Audience)
|
2019-05-11 08:31:19 +00:00
|
|
|
|
2019-05-11 09:53:32 +00:00
|
|
|
o.Tag = FlattenItemCollection(o.Tag)
|
2019-05-11 08:31:19 +00:00
|
|
|
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2019-05-11 09:53:32 +00:00
|
|
|
// FlattenProperties flattens the Item's properties from Object types to IRI
|
|
|
|
func FlattenProperties(it Item) Item {
|
2019-05-16 09:08:27 +00:00
|
|
|
if ActivityTypes.Contains(it.GetType()) {
|
2019-05-11 08:31:19 +00:00
|
|
|
act, err := ToActivity(it)
|
|
|
|
if err == nil {
|
2019-06-11 16:08:39 +00:00
|
|
|
return FlattenActivityProperties(act)
|
2019-05-11 08:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 09:08:27 +00:00
|
|
|
if ActorTypes.Contains(it.GetType()) || ObjectTypes.Contains(it.GetType()) {
|
2019-05-11 08:31:19 +00:00
|
|
|
ob, err := ToObject(it)
|
|
|
|
if err == nil {
|
2019-06-11 16:08:39 +00:00
|
|
|
return FlattenObjectProperties(ob)
|
2019-05-11 08:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
2019-05-12 18:13:06 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON
|
|
|
|
func (t *Tombstone) UnmarshalJSON(data []byte) error {
|
|
|
|
t.Parent.UnmarshalJSON(data)
|
|
|
|
t.FormerType = ActivityVocabularyType(JSONGetString(data, "formerType"))
|
|
|
|
t.Deleted = JSONGetTime(data, "deleted")
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-28 19:51:15 +00:00
|
|
|
|
|
|
|
// Recipients performs recipient de-duplication on the Object's To, Bto, CC and BCC properties
|
|
|
|
func (o *object) Recipients() ItemCollection {
|
|
|
|
var aud ItemCollection
|
2019-08-22 18:48:02 +00:00
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience)
|
2019-05-28 19:51:15 +00:00
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:59:34 +00:00
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (o *object) Clean(){
|
|
|
|
o.BCC = nil
|
|
|
|
o.Bto = nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:51:15 +00:00
|
|
|
// Recipients performs recipient de-duplication on the Place object's To, Bto, CC and BCC properties
|
|
|
|
func (p *Place) Recipients() ItemCollection {
|
|
|
|
var aud ItemCollection
|
2019-08-22 18:48:02 +00:00
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
|
2019-05-28 19:51:15 +00:00
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:59:34 +00:00
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (p *Place) Clean(){
|
|
|
|
p.BCC = nil
|
|
|
|
p.Bto = nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:51:15 +00:00
|
|
|
// Recipients performs recipient de-duplication on the Profile object's To, Bto, CC and BCC properties
|
|
|
|
func (p *Profile) Recipients() ItemCollection {
|
|
|
|
var aud ItemCollection
|
2019-08-22 18:48:02 +00:00
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &p.To, &p.Bto, &p.CC, &p.BCC, &p.Audience)
|
2019-05-28 19:51:15 +00:00
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:59:34 +00:00
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (p *Profile) Clean(){
|
|
|
|
p.BCC = nil
|
|
|
|
p.Bto = nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:51:15 +00:00
|
|
|
// Recipients performs recipient de-duplication on the Relationship object's To, Bto, CC and BCC properties
|
|
|
|
func (r *Relationship) Recipients() ItemCollection {
|
|
|
|
var aud ItemCollection
|
2019-08-22 18:48:02 +00:00
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &r.To, &r.Bto, &r.CC, &r.BCC, &r.Audience)
|
2019-05-28 19:51:15 +00:00
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
2019-11-24 09:59:34 +00:00
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (r *Relationship) Clean(){
|
|
|
|
r.BCC = nil
|
|
|
|
r.Bto = nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:51:15 +00:00
|
|
|
// Recipients performs recipient de-duplication on the Tombstone object's To, Bto, CC and BCC properties
|
|
|
|
func (t *Tombstone) Recipients() ItemCollection {
|
|
|
|
var aud ItemCollection
|
2019-08-22 18:48:02 +00:00
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &t.To, &t.Bto, &t.CC, &t.BCC, &t.Audience)
|
2019-05-28 19:51:15 +00:00
|
|
|
return rec
|
|
|
|
}
|
2019-11-24 09:59:34 +00:00
|
|
|
|
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (t *Tombstone) Clean(){
|
|
|
|
t.BCC = nil
|
|
|
|
t.Bto = nil
|
|
|
|
}
|