| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/gob" |
| 6 | "fmt" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/valyala/fastjson" |
| 10 | ) |
| 11 | |
| 12 | // Profile a Profile is a content object that describes another Object, |
| 13 | // typically used to describe CanReceiveActivities Type objects. |
| 14 | // The describes property is used to reference the object being described by the profile. |
| 15 | type Profile struct { |
| 16 | // ID provides the globally unique identifier for anActivity Pub Object or Link. |
| 17 | ID ID `jsonld:"id,omitempty"` |
| 18 | // Type identifies the Activity Pub Object or Link type. Multiple values may be specified. |
| 19 | Type Typer `jsonld:"type,omitempty"` |
| 20 | // Name a simple, human-readable, plain-text name for the object. |
| 21 | // HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values. |
| 22 | Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 23 | // Attachment identifies a resource attached or related to an object that potentially requires special handling. |
| 24 | // The intent is to provide a model that is at least semantically similar to attachments in email. |
| 25 | Attachment Item `jsonld:"attachment,omitempty"` |
| 26 | // AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors. |
| 27 | // For instance, an object might be attributed to the completion of another activity. |
| 28 | AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 29 | // Audience identifies one or more entities that represent the total population of entities |
| 30 | // for which the object can considered to be relevant. |
| 31 | Audience ItemCollection `jsonld:"audience,omitempty"` |
| 32 | // Content or textual representation of the Activity Pub Object encoded as a JSON string. |
| 33 | // By default, the value of content is HTML. |
| 34 | // The mediaType property can be used in the object to indicate a different content type. |
| 35 | // (The content MAY be expressed using multiple language-tagged values.) |
| 36 | Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 37 | // Context identifies the context within which the object exists or an activity was performed. |
| 38 | // The notion of "context" used is intentionally vague. |
| 39 | // The intended function is to serve as a means of grouping objects and activities that share a |
| 40 | // common originating context or purpose. An example could be all activities relating to a common project or event. |
| 41 | Context Item `jsonld:"context,omitempty"` |
| 42 | // MediaType when used on an Object, identifies the MIME media type of the value of the content property. |
| 43 | // If not specified, the content property is assumed to contain text/html content. |
| 44 | MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 45 | // EndTime the date and time describing the actual or expected ending time of the object. |
| 46 | // When used with an Activity object, for instance, the endTime property specifies the moment |
| 47 | // the activity concluded or is expected to conclude. |
| 48 | EndTime time.Time `jsonld:"endTime,omitempty"` |
| 49 | // Generator identifies the entity (e.g. an application) that generated the object. |
| 50 | Generator Item `jsonld:"generator,omitempty"` |
| 51 | // Icon indicates an entity that describes an icon for this object. |
| 52 | // The image should have an aspect ratio of one (horizontal) to one (vertical) |
| 53 | // and should be suitable for presentation at a small size. |
| 54 | Icon Item `jsonld:"icon,omitempty"` |
| 55 | // Image indicates an entity that describes an image for this object. |
| 56 | // Unlike the icon property, there are no aspect ratio or display size limitations assumed. |
| 57 | Image Item `jsonld:"image,omitempty"` |
| 58 | // InReplyTo indicates one or more entities for which this object is considered a response. |
| 59 | InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 60 | // Location indicates one or more physical or logical locations associated with the object. |
| 61 | Location Item `jsonld:"location,omitempty"` |
| 62 | // Preview identifies an entity that provides a preview of this object. |
| 63 | Preview Item `jsonld:"preview,omitempty"` |
| 64 | // Published the date and time at which the object was published |
| 65 | Published time.Time `jsonld:"published,omitempty"` |
| 66 | // Replies identifies a Collection containing objects considered to be responses to this object. |
| 67 | Replies Item `jsonld:"replies,omitempty"` |
| 68 | // StartTime the date and time describing the actual or expected starting time of the object. |
| 69 | // When used with an Activity object, for instance, the startTime property specifies |
| 70 | // the moment the activity began or is scheduled to begin. |
| 71 | StartTime time.Time `jsonld:"startTime,omitempty"` |
| 72 | // Summary a natural language summarization of the object encoded as HTML. |
| 73 | // *Multiple language tagged summaries may be provided.) |
| 74 | Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 75 | // Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object. |
| 76 | // The key difference between attachment and tag is that the former implies association by inclusion, |
| 77 | // while the latter implies associated by reference. |
| 78 | Tag ItemCollection `jsonld:"tag,omitempty"` |
| 79 | // Updated the date and time at which the object was updated |
| 80 | Updated time.Time `jsonld:"updated,omitempty"` |
| 81 | // URL identifies one or more links to representations of the object |
| 82 | URL Item `jsonld:"url,omitempty"` |
| 83 | // To identifies an entity considered to be part of the public primary audience of an Activity Pub Object |
| 84 | To ItemCollection `jsonld:"to,omitempty"` |
| 85 | // Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object. |
| 86 | Bto ItemCollection `jsonld:"bto,omitempty"` |
| 87 | // CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object. |
| 88 | CC ItemCollection `jsonld:"cc,omitempty"` |
| 89 | // BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object. |
| 90 | BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 91 | // Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc, |
| 92 | // the duration property indicates the object's approximate duration. |
| 93 | // The value must be expressed as an xsd:duration as defined by [ xmlschema11-2], |
| 94 | // section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S"). |
| 95 | Duration time.Duration `jsonld:"duration,omitempty"` |
| 96 | // This is a list of all Like activities with this object as the object property, added as a side effect. |
| 97 | // The likes collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 98 | // of an authenticated user or as appropriate when no authentication is given. |
| 99 | Likes Item `jsonld:"likes,omitempty"` |
| 100 | // This is a list of all Announce activities with this object as the object property, added as a side effect. |
| 101 | // The shares collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 102 | // of an authenticated user or as appropriate when no authentication is given. |
| 103 | Shares Item `jsonld:"shares,omitempty"` |
| 104 | // Source property is intended to convey some sort of source from which the content markup was derived, |
| 105 | // as a form of provenance, or to support future editing by clients. |
| 106 | // In general, clients do the conversion from source to content, not the other way around. |
| 107 | Source Source `jsonld:"source,omitempty"` |
| 108 | // Describes On a Profile object, the describes property identifies the object described by the Profile. |
| 109 | Describes Item `jsonld:"describes,omitempty"` |
| 110 | } |
| 111 | |
| 112 | // IsLink returns false for Profile objects |
| 113 | func (p Profile) IsLink() bool { |
| 114 | return false |
| 115 | } |
| 116 | |
| 117 | // IsObject returns true for Profile objects |
| 118 | func (p Profile) IsObject() bool { |
| 119 | return true |
| 120 | } |
| 121 | |
| 122 | // IsCollection returns false for Profile objects |
| 123 | func (p Profile) IsCollection() bool { |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | // GetLink returns the IRI corresponding to the current Profile object |
| 128 | func (p Profile) GetLink() IRI { |
| 129 | return IRI(p.ID) |
| 130 | } |
| 131 | |
| 132 | // GetType returns the type of the current Profile |
| 133 | func (p Profile) GetType() Typer { |
| 134 | return p.Type |
| 135 | } |
| 136 | |
| 137 | // GetID returns the ID corresponding to the current Profile |
| 138 | func (p Profile) GetID() ID { |
| 139 | return p.ID |
| 140 | } |
| 141 | |
| 142 | // Match returns whether the receiver matches the ActivityVocabularyType arguments. |
| 143 | func (p Profile) Match(tt ...ActivityVocabularyType) bool { |
| 144 | return ActivityVocabularyTypes(tt).Match(p.Type) |
| 145 | } |
| 146 | |
| 147 | // UnmarshalJSON decodes an incoming JSON document into the receiver object. |
| 148 | func (p *Profile) UnmarshalJSON(data []byte) error { |
| 149 | par := fastjson.Parser{} |
| 150 | val, err := par.ParseBytes(data) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | return JSONLoadProfile(val, p) |
| 155 | } |
| 156 | |
| 157 | // MarshalJSON encodes the receiver object to a JSON document. |
| 158 | func (p Profile) MarshalJSON() ([]byte, error) { |
| 159 | b := make([]byte, 0) |
| 160 | notEmpty := false |
| 161 | JSONWrite(&b, '{') |
| 162 | |
| 163 | OnObject(p, func(o *Object) error { |
| 164 | return nil |
| 165 | }) |
| 166 | |
| 167 | if p.Describes != nil { |
| 168 | notEmpty = JSONWriteItemProp(&b, "describes", p.Describes) || notEmpty |
| 169 | } |
| 170 | |
| 171 | if notEmpty { |
| 172 | JSONWrite(&b, '}') |
| 173 | return b, nil |
| 174 | } |
| 175 | return nil, nil |
| 176 | } |
| 177 | |
| 178 | // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. |
| 179 | func (p *Profile) UnmarshalBinary(data []byte) error { |
| 180 | return p.GobDecode(data) |
| 181 | } |
| 182 | |
| 183 | // MarshalBinary implements the encoding.BinaryMarshaler interface. |
| 184 | func (p Profile) MarshalBinary() ([]byte, error) { |
| 185 | return p.GobEncode() |
| 186 | } |
| 187 | |
| 188 | // GobEncode |
| 189 | func (p Profile) GobEncode() ([]byte, error) { |
| 190 | mm := make(map[string][]byte) |
| 191 | hasData, err := mapProfileProperties(mm, p) |
| 192 | if err != nil { |
| 193 | return nil, err |
| 194 | } |
| 195 | if !hasData { |
| 196 | return []byte{}, nil |
| 197 | } |
| 198 | bb := bytes.Buffer{} |
| 199 | g := gob.NewEncoder(&bb) |
| 200 | if err := g.Encode(mm); err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | return bb.Bytes(), nil |
| 204 | } |
| 205 | |
| 206 | // GobDecode |
| 207 | func (p *Profile) GobDecode(data []byte) error { |
| 208 | if len(data) == 0 { |
| 209 | return nil |
| 210 | } |
| 211 | mm, err := gobDecodeObjectAsMap(data) |
| 212 | if err != nil { |
| 213 | return err |
| 214 | } |
| 215 | return unmapProfileProperties(mm, p) |
| 216 | } |
| 217 | |
| 218 | // Recipients performs recipient de-duplication on the Profile object's To, Bto, CC and BCC properties |
| 219 | func (p *Profile) Recipients() ItemCollection { |
| 220 | aud := p.Audience |
| 221 | return ItemCollectionDeduplication(&p.To, &p.CC, &p.Bto, &p.BCC, &aud) |
| 222 | } |
| 223 | |
| 224 | // Clean removes Bto and BCC properties |
| 225 | func (p *Profile) Clean() { |
| 226 | _ = OnObject(p, func(o *Object) error { |
| 227 | o.Clean() |
| 228 | return nil |
| 229 | }) |
| 230 | } |
| 231 | |
| 232 | func (p Profile) Format(s fmt.State, verb rune) { |
| 233 | switch verb { |
| 234 | case 's', 'v': |
| 235 | _, _ = fmt.Fprintf(s, "%T[%s] { }", p, p.GetType()) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // ToProfile tries to convert the "it" Item to a Profile object |
| 240 | func ToProfile(it LinkOrIRI) (*Profile, error) { |
| 241 | switch i := it.(type) { |
| 242 | case *Profile: |
| 243 | return i, nil |
| 244 | case Profile: |
| 245 | return &i, nil |
| 246 | default: |
| 247 | return reflectItemToType[Profile](it) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | type withProfileFn func(*Profile) error |
| 252 | |
| 253 | // OnProfile calls function fn on it Item if it can be asserted to type *Profile |
| 254 | // |
| 255 | // This function should be called if trying to access the Profile specific properties |
| 256 | // like "describes". |
| 257 | // For the other properties OnObject should be used instead. |
| 258 | func OnProfile(it LinkOrIRI, fn func(*Profile) error) error { |
| 259 | if it == nil { |
| 260 | return nil |
| 261 | } |
| 262 | if IsItemCollection(it) { |
| 263 | return callOnItemCollection(it, OnProfile, fn) |
| 264 | } |
| 265 | ob, err := ToProfile(it) |
| 266 | if err != nil { |
| 267 | return err |
| 268 | } |
| 269 | return fn(ob) |
| 270 | } |