2018-11-21 11:26:20 +00:00
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
2019-12-03 16:23:59 +00:00
|
|
|
"fmt"
|
2018-11-21 11:26:20 +00:00
|
|
|
"github.com/buger/jsonparser"
|
2019-12-03 16:23:59 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
2018-11-21 11:26:20 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 15:28:45 +00:00
|
|
|
const (
|
2019-12-03 16:23:59 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
// Activity Pub Object Types
|
|
|
|
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"
|
|
|
|
|
|
|
|
// MentionType is a link type for @mentions
|
|
|
|
MentionType ActivityVocabularyType = "Mention"
|
|
|
|
)
|
|
|
|
|
|
|
|
var GenericObjectTypes = ActivityVocabularyTypes{
|
|
|
|
ActivityType,
|
|
|
|
IntransitiveActivityType,
|
|
|
|
ObjectType,
|
|
|
|
ActorType,
|
|
|
|
CollectionType,
|
|
|
|
OrderedCollectionType,
|
|
|
|
}
|
|
|
|
|
|
|
|
var GenericLinkTypes = ActivityVocabularyTypes{
|
|
|
|
LinkType,
|
|
|
|
}
|
|
|
|
|
|
|
|
var GenericTypes = append(GenericObjectTypes[:], GenericLinkTypes[:]...)
|
|
|
|
|
|
|
|
var ObjectTypes = ActivityVocabularyTypes{
|
|
|
|
ArticleType,
|
|
|
|
AudioType,
|
|
|
|
DocumentType,
|
|
|
|
EventType,
|
|
|
|
ImageType,
|
|
|
|
NoteType,
|
|
|
|
PageType,
|
|
|
|
PlaceType,
|
|
|
|
ProfileType,
|
|
|
|
RelationshipType,
|
|
|
|
TombstoneType,
|
|
|
|
VideoType,
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// ActivityVocabularyType is the data type for an Activity type object
|
|
|
|
ActivityVocabularyType string
|
|
|
|
// ActivityObject is a subtype of Object that describes some form of action that may happen,
|
|
|
|
// is currently happening, or has already happened
|
|
|
|
ActivityObject interface {
|
|
|
|
// GetID returns the dereferenceable ActivityStreams object id
|
2019-12-05 18:02:15 +00:00
|
|
|
GetID() ID
|
2019-12-03 16:23:59 +00:00
|
|
|
// GetType returns the ActivityStreams type
|
|
|
|
GetType() ActivityVocabularyType
|
|
|
|
}
|
|
|
|
// LinkOrIRI is an interface that Object and Link structs implement, and at the same time
|
|
|
|
// they are kept disjointed
|
|
|
|
LinkOrIRI interface {
|
|
|
|
// GetLink returns the object id in IRI type
|
|
|
|
GetLink() IRI
|
|
|
|
}
|
2019-12-04 15:28:45 +00:00
|
|
|
// ObjectOrLink describes the requirements of an ActivityStreams object
|
2019-12-03 16:23:59 +00:00
|
|
|
ObjectOrLink interface {
|
|
|
|
ActivityObject
|
|
|
|
LinkOrIRI
|
|
|
|
// IsLink shows if current item represents a Link object or an IRI
|
|
|
|
IsLink() bool
|
|
|
|
// IsObject shows if current item represents an ActivityStreams object
|
|
|
|
IsObject() bool
|
|
|
|
// IsCollection shows if the current item represents an ItemCollection
|
|
|
|
IsCollection() bool
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// MimeType is the type for representing MIME types in certain ActivityStreams properties
|
|
|
|
MimeType string
|
|
|
|
)
|
|
|
|
|
2019-12-15 18:05:26 +00:00
|
|
|
func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-19 16:05:12 +00:00
|
|
|
b := make([]byte, 0)
|
2019-12-21 09:44:32 +00:00
|
|
|
writeStringValue(&b, string(a))
|
2019-12-19 16:05:12 +00:00
|
|
|
return b, nil
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 15:28:45 +00:00
|
|
|
// Object describes an ActivityPub object of any kind.
|
|
|
|
// It serves as the base type for most of the other kinds of objects defined in the Activity
|
2019-12-03 16:23:59 +00:00
|
|
|
// Vocabulary, including other Core types such as Activity, IntransitiveActivity, Collection and OrderedCollection.
|
2018-11-21 11:26:20 +00:00
|
|
|
type Object struct {
|
2019-12-03 16:23:59 +00:00
|
|
|
// ID provides the globally unique identifier for anActivity Pub Object or Link.
|
2019-12-05 18:02:15 +00:00
|
|
|
ID ID `jsonld:"id,omitempty"`
|
2019-12-03 16:23:59 +00:00
|
|
|
// Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
|
|
|
|
Type ActivityVocabularyType `jsonld:"type,omitempty"`
|
|
|
|
// Name a simple, human-readable, plain-text name for the object.
|
|
|
|
// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
|
|
|
|
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
|
|
|
|
// Attachment identifies a resource attached or related to an object that potentially requires special handling.
|
|
|
|
// The intent is to provide a model that is at least semantically similar to attachments in email.
|
|
|
|
Attachment Item `jsonld:"attachment,omitempty"`
|
|
|
|
// AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
|
|
|
|
// For instance, an object might be attributed to the completion of another activity.
|
|
|
|
AttributedTo Item `jsonld:"attributedTo,omitempty"`
|
|
|
|
// Audience identifies one or more entities that represent the total population of entities
|
|
|
|
// for which the object can considered to be relevant.
|
|
|
|
Audience ItemCollection `jsonld:"audience,omitempty"`
|
|
|
|
// Content or textual representation of the Activity Pub Object encoded as a JSON string.
|
|
|
|
// 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.)
|
|
|
|
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
|
|
|
|
// Context identifies the context within which the object exists or an activity was performed.
|
|
|
|
// 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
|
|
|
|
// common originating context or purpose. An example could be all activities relating to a common project or event.
|
|
|
|
Context Item `jsonld:"context,omitempty"`
|
|
|
|
// MediaType when used on an Object, identifies the MIME media type of the value of the content property.
|
|
|
|
// If not specified, the content property is assumed to contain text/html content.
|
|
|
|
MediaType MimeType `jsonld:"mediaType,omitempty"`
|
|
|
|
// EndTime the date and time describing the actual or expected ending time of the object.
|
|
|
|
// When used with an Activity object, for instance, the endTime property specifies the moment
|
|
|
|
// the activity concluded or is expected to conclude.
|
|
|
|
EndTime time.Time `jsonld:"endTime,omitempty"`
|
|
|
|
// Generator identifies the entity (e.g. an application) that generated the object.
|
|
|
|
Generator Item `jsonld:"generator,omitempty"`
|
|
|
|
// Icon indicates an entity that describes an icon for this object.
|
|
|
|
// The image should have an aspect ratio of one (horizontal) to one (vertical)
|
|
|
|
// and should be suitable for presentation at a small size.
|
|
|
|
Icon Item `jsonld:"icon,omitempty"`
|
|
|
|
// Image indicates an entity that describes an image for this object.
|
|
|
|
// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
|
|
|
|
Image Item `jsonld:"image,omitempty"`
|
|
|
|
// InReplyTo indicates one or more entities for which this object is considered a response.
|
|
|
|
InReplyTo Item `jsonld:"inReplyTo,omitempty"`
|
|
|
|
// Location indicates one or more physical or logical locations associated with the object.
|
|
|
|
Location Item `jsonld:"location,omitempty"`
|
|
|
|
// Preview identifies an entity that provides a preview of this object.
|
|
|
|
Preview Item `jsonld:"preview,omitempty"`
|
|
|
|
// Published the date and time at which the object was published
|
|
|
|
Published time.Time `jsonld:"published,omitempty"`
|
|
|
|
// Replies identifies a Collection containing objects considered to be responses to this object.
|
|
|
|
Replies Item `jsonld:"replies,omitempty"`
|
|
|
|
// StartTime the date and time describing the actual or expected starting time of the object.
|
|
|
|
// When used with an Activity object, for instance, the startTime property specifies
|
|
|
|
// the moment the activity began or is scheduled to begin.
|
|
|
|
StartTime time.Time `jsonld:"startTime,omitempty"`
|
|
|
|
// Summary a natural language summarization of the object encoded as HTML.
|
|
|
|
// *Multiple language tagged summaries may be provided.)
|
|
|
|
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
|
|
|
|
// Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
|
|
|
|
// The key difference between attachment and tag is that the former implies association by inclusion,
|
|
|
|
// while the latter implies associated by reference.
|
|
|
|
Tag ItemCollection `jsonld:"tag,omitempty"`
|
|
|
|
// Updated the date and time at which the object was updated
|
|
|
|
Updated time.Time `jsonld:"updated,omitempty"`
|
|
|
|
// URL identifies one or more links to representations of the object
|
|
|
|
URL LinkOrIRI `jsonld:"url,omitempty"`
|
|
|
|
// To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
|
|
|
|
To ItemCollection `jsonld:"to,omitempty"`
|
|
|
|
// Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
|
|
|
|
Bto ItemCollection `jsonld:"bto,omitempty"`
|
|
|
|
// CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
|
|
|
|
CC ItemCollection `jsonld:"cc,omitempty"`
|
|
|
|
// BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
|
|
|
|
BCC ItemCollection `jsonld:"bcc,omitempty"`
|
|
|
|
// Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
|
|
|
|
// the duration property indicates the object's approximate duration.
|
|
|
|
// The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
|
|
|
|
// section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
|
|
|
|
Duration time.Duration `jsonld:"duration,omitempty"`
|
2019-09-28 07:30:01 +00:00
|
|
|
// This is a list of all Like activities with this object as the object property, added as a side effect.
|
|
|
|
// The likes collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges
|
|
|
|
// of an authenticated user or as appropriate when no authentication is given.
|
2019-12-03 16:23:59 +00:00
|
|
|
Likes Item `jsonld:"likes,omitempty"`
|
2019-09-28 07:30:01 +00:00
|
|
|
// This is a list of all Announce activities with this object as the object property, added as a side effect.
|
|
|
|
// The shares collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges
|
|
|
|
// of an authenticated user or as appropriate when no authentication is given.
|
2019-12-03 16:23:59 +00:00
|
|
|
Shares Item `jsonld:"shares,omitempty"`
|
2018-11-21 11:26:20 +00:00
|
|
|
// Source property is intended to convey some sort of source from which the content markup was derived,
|
|
|
|
// as a form of provenance, or to support future editing by clients.
|
|
|
|
// In general, clients do the conversion from source to content, not the other way around.
|
|
|
|
Source Source `jsonld:"source,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-12-03 16:23:59 +00:00
|
|
|
// ObjectNew initializes a new Object
|
|
|
|
func ObjectNew(typ ActivityVocabularyType) *Object {
|
|
|
|
if !(ObjectTypes.Contains(typ)) {
|
|
|
|
typ = ObjectType
|
|
|
|
}
|
|
|
|
o := Object{Type: typ}
|
|
|
|
o.Name = NaturalLanguageValuesNew()
|
|
|
|
o.Content = NaturalLanguageValuesNew()
|
|
|
|
return &o
|
|
|
|
}
|
2018-11-21 11:26:20 +00:00
|
|
|
|
2019-12-05 18:02:15 +00:00
|
|
|
// GetID returns the ID corresponding to the current Object
|
|
|
|
func (o Object) GetID() ID {
|
2019-12-03 16:23:59 +00:00
|
|
|
return o.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLink returns the IRI corresponding to the current Object
|
|
|
|
func (o Object) GetLink() IRI {
|
|
|
|
return IRI(o.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of the current Object
|
|
|
|
func (o Object) GetType() ActivityVocabularyType {
|
|
|
|
return o.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLink validates if currentActivity Pub Object is a Link
|
|
|
|
func (o Object) IsLink() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsObject validates if currentActivity Pub Object is an Object
|
|
|
|
func (o Object) IsObject() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCollection returns false for Object objects
|
|
|
|
func (o Object) IsCollection() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON
|
|
|
|
func (o *Object) UnmarshalJSON(data []byte) error {
|
2019-12-21 10:05:38 +00:00
|
|
|
return loadObject(data, o)
|
2019-12-03 16:23:59 +00:00
|
|
|
}
|
2018-11-21 11:26:20 +00:00
|
|
|
|
2019-12-15 18:05:26 +00:00
|
|
|
// MarshalJSON
|
|
|
|
func (o Object) MarshalJSON() ([]byte, error) {
|
2019-12-19 16:05:12 +00:00
|
|
|
b := make([]byte, 0)
|
|
|
|
write(&b, '{')
|
2019-12-18 09:02:23 +00:00
|
|
|
|
2019-12-22 13:08:56 +00:00
|
|
|
if writeObjectValue(&b, o) {
|
2019-12-19 16:05:12 +00:00
|
|
|
write(&b, '}')
|
|
|
|
return b, nil
|
2019-12-18 09:02:23 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:23:59 +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
|
|
|
|
rec, _ := ItemCollectionDeduplication(&aud, &o.To, &o.Bto, &o.CC, &o.BCC, &o.Audience)
|
|
|
|
return rec
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean removes Bto and BCC properties
|
|
|
|
func (o *Object) Clean() {
|
|
|
|
o.BCC = nil
|
|
|
|
o.Bto = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Article represents any kind of multi-paragraph written work.
|
|
|
|
Article = Object
|
|
|
|
// Audio represents an audio document of any kind.
|
|
|
|
Audio = Document
|
|
|
|
// Document represents a document of any kind.
|
|
|
|
Document = Object
|
|
|
|
// Event represents any kind of event.
|
|
|
|
Event = Object
|
|
|
|
// Image An image document of any kind
|
|
|
|
Image = Document
|
|
|
|
// Note represents a short written work typically less than a single paragraph in length.
|
|
|
|
Note = Object
|
|
|
|
// Page represents a Web Page.
|
|
|
|
Page = Document
|
|
|
|
// Video represents a video document of any kind
|
|
|
|
Video = Document
|
|
|
|
)
|
|
|
|
|
2018-11-21 11:26:20 +00:00
|
|
|
// UnmarshalJSON
|
2019-12-03 16:23:59 +00:00
|
|
|
func (c *MimeType) UnmarshalJSON(data []byte) error {
|
|
|
|
*c = MimeType(strings.Trim(string(data), "\""))
|
2018-11-21 11:26:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-09-09 18:21:45 +00:00
|
|
|
|
2019-12-15 18:05:26 +00:00
|
|
|
// MarshalJSON
|
2019-12-19 12:34:04 +00:00
|
|
|
func (m MimeType) MarshalJSON() ([]byte, error) {
|
|
|
|
if len(m) == 0 {
|
2019-12-15 18:05:26 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-19 16:05:12 +00:00
|
|
|
b := make([]byte, 0)
|
2019-12-21 09:44:32 +00:00
|
|
|
writeStringValue(&b, string(m))
|
2019-12-19 16:05:12 +00:00
|
|
|
return b, nil
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:23:59 +00:00
|
|
|
// ToObject returns an Object pointer to the data in the current Item
|
|
|
|
// It relies on the fact that all the types in this package have a data layout compatible with Object.
|
|
|
|
func ToObject(it Item) (*Object, error) {
|
2019-09-09 18:21:45 +00:00
|
|
|
switch i := it.(type) {
|
|
|
|
case *Object:
|
|
|
|
return i, nil
|
|
|
|
case Object:
|
|
|
|
return &i, nil
|
2019-12-03 16:23:59 +00:00
|
|
|
case *Place:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Place:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *Profile:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Profile:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *Relationship:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Relationship:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *Tombstone:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Tombstone:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
2019-12-04 18:34:04 +00:00
|
|
|
case *Actor:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Actor:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
2019-12-03 16:23:59 +00:00
|
|
|
case *Activity:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Activity:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *IntransitiveActivity:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case IntransitiveActivity:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *Question:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Question:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *Collection:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case Collection:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *CollectionPage:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case CollectionPage:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *OrderedCollection:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case OrderedCollection:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
|
|
|
case *OrderedCollectionPage:
|
|
|
|
return (*Object)(unsafe.Pointer(i)), nil
|
|
|
|
case OrderedCollectionPage:
|
|
|
|
return (*Object)(unsafe.Pointer(&i)), nil
|
2019-09-09 18:21:45 +00:00
|
|
|
}
|
2019-12-03 16:23:59 +00:00
|
|
|
return nil, fmt.Errorf("unable to convert %q", it.GetType())
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlattenObjectProperties flattens the Object's properties from Object types to IRI
|
|
|
|
func FlattenObjectProperties(o *Object) *Object {
|
|
|
|
o.Replies = Flatten(o.Replies)
|
|
|
|
o.AttributedTo = Flatten(o.AttributedTo)
|
|
|
|
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)
|
|
|
|
o.Tag = FlattenItemCollection(o.Tag)
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlattenProperties flattens the Item's properties from Object types to IRI
|
|
|
|
func FlattenProperties(it Item) Item {
|
|
|
|
if ActivityTypes.Contains(it.GetType()) {
|
|
|
|
act, err := ToActivity(it)
|
|
|
|
if err == nil {
|
|
|
|
return FlattenActivityProperties(act)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ActorTypes.Contains(it.GetType()) || ObjectTypes.Contains(it.GetType()) {
|
|
|
|
ob, err := ToObject(it)
|
|
|
|
if err == nil {
|
|
|
|
return FlattenObjectProperties(ob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source is intended to convey some sort of source from which the content markup was derived,
|
|
|
|
// as a form of provenance, or to support future editing by clients.
|
|
|
|
type Source struct {
|
|
|
|
// Content
|
|
|
|
Content NaturalLanguageValues `jsonld:"content"`
|
|
|
|
// MediaType
|
|
|
|
MediaType MimeType `jsonld:"mediaType"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAPSource
|
|
|
|
func GetAPSource(data []byte) Source {
|
|
|
|
s := Source{}
|
|
|
|
|
|
|
|
if contBytes, _, _, err := jsonparser.Get(data, "source", "content"); err == nil {
|
|
|
|
s.Content.UnmarshalJSON(contBytes)
|
|
|
|
}
|
|
|
|
if mimeBytes, _, _, err := jsonparser.Get(data, "source", "mediaType"); err == nil {
|
|
|
|
s.MediaType.UnmarshalJSON(mimeBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON
|
|
|
|
func (s *Source) UnmarshalJSON(data []byte) error {
|
|
|
|
*s = GetAPSource(data)
|
|
|
|
return nil
|
2019-09-09 18:21:45 +00:00
|
|
|
}
|
2019-12-15 18:05:26 +00:00
|
|
|
|
|
|
|
// MarshalJSON
|
|
|
|
func (s Source) MarshalJSON() ([]byte, error) {
|
2019-12-19 16:05:12 +00:00
|
|
|
b := make([]byte, 0)
|
2019-12-18 09:02:23 +00:00
|
|
|
empty := true
|
2019-12-19 16:05:12 +00:00
|
|
|
write(&b, '{')
|
2019-12-15 18:05:26 +00:00
|
|
|
if len(s.MediaType) > 0 {
|
2019-12-18 09:02:23 +00:00
|
|
|
if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 {
|
|
|
|
empty = !writeProp(&b, "mediaType", v)
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(s.Content) > 0 {
|
2019-12-18 09:02:23 +00:00
|
|
|
empty = !writeNaturalLanguageProp(&b, "content", s.Content)
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|
2019-12-18 09:02:23 +00:00
|
|
|
if !empty {
|
2019-12-19 16:05:12 +00:00
|
|
|
write(&b, '}')
|
|
|
|
return b, nil
|
2019-12-18 09:02:23 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
2019-12-15 18:05:26 +00:00
|
|
|
}
|