| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/gob" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "time" |
| 9 | "unsafe" |
| 10 | |
| 11 | "github.com/valyala/fastjson" |
| 12 | ) |
| 13 | |
| 14 | type IntransitiveActivities interface { |
| 15 | IntransitiveActivity | Question |
| 16 | } |
| 17 | |
| 18 | // IntransitiveActivity Instances of IntransitiveActivity are a subtype of Activity representing intransitive actions. |
| 19 | // The object property is therefore inappropriate for these activities. |
| 20 | type IntransitiveActivity struct { |
| 21 | // ID provides the globally unique identifier for anActivity Pub Object or Link. |
| 22 | ID ID `jsonld:"id,omitempty"` |
| 23 | // Type identifies the Activity Pub Object or Link type. Multiple values may be specified. |
| 24 | Type Typer `jsonld:"type,omitempty"` |
| 25 | // Name a simple, human-readable, plain-text name for the object. |
| 26 | // HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values. |
| 27 | Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 28 | // Attachment identifies a resource attached or related to an object that potentially requires special handling. |
| 29 | // The intent is to provide a model that is at least semantically similar to attachments in email. |
| 30 | Attachment Item `jsonld:"attachment,omitempty"` |
| 31 | // AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors. |
| 32 | // For instance, an object might be attributed to the completion of another activity. |
| 33 | AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 34 | // Audience identifies one or more entities that represent the total population of entities |
| 35 | // for which the object can considered to be relevant. |
| 36 | Audience ItemCollection `jsonld:"audience,omitempty"` |
| 37 | // Content or textual representation of the Activity Pub Object encoded as a JSON string. |
| 38 | // By default, the value of content is HTML. |
| 39 | // The mediaType property can be used in the object to indicate a different content type. |
| 40 | // (The content MAY be expressed using multiple language-tagged values.) |
| 41 | Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 42 | // Context identifies the context within which the object exists or an activity was performed. |
| 43 | // The notion of "context" used is intentionally vague. |
| 44 | // The intended function is to serve as a means of grouping objects and activities that share a |
| 45 | // common originating context or purpose. An example could be all activities relating to a common project or event. |
| 46 | Context Item `jsonld:"context,omitempty"` |
| 47 | // MediaType when used on an Object, identifies the MIME media type of the value of the content property. |
| 48 | // If not specified, the content property is assumed to contain text/html content. |
| 49 | MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 50 | // EndTime the date and time describing the actual or expected ending time of the object. |
| 51 | // When used with an Activity object, for instance, the endTime property specifies the moment |
| 52 | // the activity concluded or is expected to conclude. |
| 53 | EndTime time.Time `jsonld:"endTime,omitempty"` |
| 54 | // Generator identifies the entity (e.g. an application) that generated the object. |
| 55 | Generator Item `jsonld:"generator,omitempty"` |
| 56 | // Icon indicates an entity that describes an icon for this object. |
| 57 | // The image should have an aspect ratio of one (horizontal) to one (vertical) |
| 58 | // and should be suitable for presentation at a small size. |
| 59 | Icon Item `jsonld:"icon,omitempty"` |
| 60 | // Image indicates an entity that describes an image for this object. |
| 61 | // Unlike the icon property, there are no aspect ratio or display size limitations assumed. |
| 62 | Image Item `jsonld:"image,omitempty"` |
| 63 | // InReplyTo indicates one or more entities for which this object is considered a response. |
| 64 | InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 65 | // Location indicates one or more physical or logical locations associated with the object. |
| 66 | Location Item `jsonld:"location,omitempty"` |
| 67 | // Preview identifies an entity that provides a preview of this object. |
| 68 | Preview Item `jsonld:"preview,omitempty"` |
| 69 | // Published the date and time at which the object was published |
| 70 | Published time.Time `jsonld:"published,omitempty"` |
| 71 | // Replies identifies a Collection containing objects considered to be responses to this object. |
| 72 | Replies Item `jsonld:"replies,omitempty"` |
| 73 | // StartTime the date and time describing the actual or expected starting time of the object. |
| 74 | // When used with an Activity object, for instance, the startTime property specifies |
| 75 | // the moment the activity began or is scheduled to begin. |
| 76 | StartTime time.Time `jsonld:"startTime,omitempty"` |
| 77 | // Summary a natural language summarization of the object encoded as HTML. |
| 78 | // *Multiple language tagged summaries may be provided.) |
| 79 | Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 80 | // Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object. |
| 81 | // The key difference between attachment and tag is that the former implies association by inclusion, |
| 82 | // while the latter implies associated by reference. |
| 83 | Tag ItemCollection `jsonld:"tag,omitempty"` |
| 84 | // Updated the date and time at which the object was updated |
| 85 | Updated time.Time `jsonld:"updated,omitempty"` |
| 86 | // URL identifies one or more links to representations of the object |
| 87 | URL Item `jsonld:"url,omitempty"` |
| 88 | // To identifies an entity considered to be part of the public primary audience of an Activity Pub Object |
| 89 | To ItemCollection `jsonld:"to,omitempty"` |
| 90 | // Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object. |
| 91 | Bto ItemCollection `jsonld:"bto,omitempty"` |
| 92 | // CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object. |
| 93 | CC ItemCollection `jsonld:"cc,omitempty"` |
| 94 | // BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object. |
| 95 | BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 96 | // Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc, |
| 97 | // the duration property indicates the object's approximate duration. |
| 98 | // The value must be expressed as an xsd:duration as defined by [ xmlschema11-2], |
| 99 | // section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S"). |
| 100 | Duration time.Duration `jsonld:"duration,omitempty"` |
| 101 | // This is a list of all Like activities with this object as the object property, added as a side effect. |
| 102 | // The likes collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 103 | // of an authenticated user or as appropriate when no authentication is given. |
| 104 | Likes Item `jsonld:"likes,omitempty"` |
| 105 | // This is a list of all Announce activities with this object as the object property, added as a side effect. |
| 106 | // The shares collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges |
| 107 | // of an authenticated user or as appropriate when no authentication is given. |
| 108 | Shares Item `jsonld:"shares,omitempty"` |
| 109 | // Source property is intended to convey some sort of source from which the content markup was derived, |
| 110 | // as a form of provenance, or to support future editing by clients. |
| 111 | // In general, clients do the conversion from source to content, not the other way around. |
| 112 | Source Source `jsonld:"source,omitempty"` |
| 113 | // CanReceiveActivities describes one or more entities that either performed or are expected to perform the activity. |
| 114 | // Any single activity can have multiple actors. The actor may be specified using an indirect Link. |
| 115 | Actor CanReceiveActivities `jsonld:"actor,omitempty"` |
| 116 | // Target describes the indirect object, or target, of the activity. |
| 117 | // The precise meaning of the target is largely dependent on the type of action being described |
| 118 | // but will often be the object of the English preposition "to". |
| 119 | // For instance, in the activity "John added a movie to his wishlist", |
| 120 | // the target of the activity is John's wishlist. An activity can have more than one target. |
| 121 | Target Item `jsonld:"target,omitempty"` |
| 122 | // Result describes the result of the activity. For instance, if a particular action results in the creation |
| 123 | // of a new resource, the result property can be used to describe that new resource. |
| 124 | Result Item `jsonld:"result,omitempty"` |
| 125 | // Origin describes an indirect object of the activity from which the activity is directed. |
| 126 | // The precise meaning of the origin is the object of the English preposition "from". |
| 127 | // For instance, in the activity "John moved an item to List B from List A", the origin of the activity is "List A". |
| 128 | Origin Item `jsonld:"origin,omitempty"` |
| 129 | // Instrument identifies one or more objects used (or to be used) in the completion of an Activity. |
| 130 | Instrument Item `jsonld:"instrument,omitempty"` |
| 131 | } |
| 132 | |
| 133 | type ( |
| 134 | // Arrive is an IntransitiveActivity that indicates that the actor has arrived at the location. |
| 135 | // The origin can be used to identify the context from which the actor originated. |
| 136 | // The target typically has no defined meaning. |
| 137 | Arrive = IntransitiveActivity |
| 138 | |
| 139 | // Travel indicates that the actor is traveling to target from origin. |
| 140 | // Travel is an IntransitiveObject whose actor specifies the direct object. |
| 141 | // If the target or origin are not specified, either can be determined by context. |
| 142 | Travel = IntransitiveActivity |
| 143 | ) |
| 144 | |
| 145 | // Recipients performs recipient de-duplication on the IntransitiveActivity's To, Bto, CC and BCC properties |
| 146 | func (i *IntransitiveActivity) Recipients() ItemCollection { |
| 147 | aud := i.Audience |
| 148 | return ItemCollectionDeduplication(&i.To, &i.CC, &i.Bto, &i.BCC, &ItemCollection{i.Actor}, &aud) |
| 149 | } |
| 150 | |
| 151 | // Clean removes Bto and BCC properties |
| 152 | func (i *IntransitiveActivity) Clean() { |
| 153 | _ = OnObject(i, func(o *Object) error { |
| 154 | o.Clean() |
| 155 | return nil |
| 156 | }) |
| 157 | } |
| 158 | |
| 159 | // GetType returns the ActivityVocabulary type of the current Intransitive Activity |
| 160 | func (i IntransitiveActivity) GetType() Typer { |
| 161 | return i.Type |
| 162 | } |
| 163 | |
| 164 | // IsLink returns false for Activity objects |
| 165 | func (i IntransitiveActivity) IsLink() bool { |
| 166 | return false |
| 167 | } |
| 168 | |
| 169 | // GetID returns the ID corresponding to the IntransitiveActivity object |
| 170 | func (i IntransitiveActivity) GetID() ID { |
| 171 | return i.ID |
| 172 | } |
| 173 | |
| 174 | // GetLink returns the IRI corresponding to the IntransitiveActivity object |
| 175 | func (i IntransitiveActivity) GetLink() IRI { |
| 176 | return IRI(i.ID) |
| 177 | } |
| 178 | |
| 179 | // IsObject returns true for IntransitiveActivity objects |
| 180 | func (i IntransitiveActivity) IsObject() bool { |
| 181 | return true |
| 182 | } |
| 183 | |
| 184 | // IsCollection returns false for IntransitiveActivity objects |
| 185 | func (i IntransitiveActivity) IsCollection() bool { |
| 186 | return false |
| 187 | } |
| 188 | |
| 189 | // Match returns whether the receiver matches the ActivityVocabularyType arguments. |
| 190 | func (i IntransitiveActivity) Match(tt ...ActivityVocabularyType) bool { |
| 191 | return ActivityVocabularyTypes(tt).Match(i.Type) |
| 192 | } |
| 193 | |
| 194 | // UnmarshalJSON decodes an incoming JSON document into the receiver object. |
| 195 | func (i *IntransitiveActivity) UnmarshalJSON(data []byte) error { |
| 196 | p := fastjson.Parser{} |
| 197 | val, err := p.ParseBytes(data) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | return JSONLoadIntransitiveActivity(val, i) |
| 202 | } |
| 203 | |
| 204 | // MarshalJSON encodes the receiver object to a JSON document. |
| 205 | func (i IntransitiveActivity) MarshalJSON() ([]byte, error) { |
| 206 | b := make([]byte, 0) |
| 207 | JSONWrite(&b, '{') |
| 208 | |
| 209 | if !JSONWriteIntransitiveActivityValue(&b, i) { |
| 210 | return nil, nil |
| 211 | } |
| 212 | JSONWrite(&b, '}') |
| 213 | return b, nil |
| 214 | } |
| 215 | |
| 216 | // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. |
| 217 | func (i *IntransitiveActivity) UnmarshalBinary(data []byte) error { |
| 218 | return i.GobDecode(data) |
| 219 | } |
| 220 | |
| 221 | // MarshalBinary implements the encoding.BinaryMarshaler interface. |
| 222 | func (i IntransitiveActivity) MarshalBinary() ([]byte, error) { |
| 223 | return i.GobEncode() |
| 224 | } |
| 225 | |
| 226 | func (i IntransitiveActivity) GobEncode() ([]byte, error) { |
| 227 | mm := make(map[string][]byte) |
| 228 | hasData, err := mapIntransitiveActivityProperties(mm, &i) |
| 229 | if err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | if !hasData { |
| 233 | return []byte{}, nil |
| 234 | } |
| 235 | bb := bytes.Buffer{} |
| 236 | g := gob.NewEncoder(&bb) |
| 237 | if err := g.Encode(mm); err != nil { |
| 238 | return nil, err |
| 239 | } |
| 240 | return bb.Bytes(), nil |
| 241 | } |
| 242 | |
| 243 | func (i *IntransitiveActivity) GobDecode(data []byte) error { |
| 244 | if len(data) == 0 { |
| 245 | return nil |
| 246 | } |
| 247 | mm, err := gobDecodeObjectAsMap(data) |
| 248 | if err != nil { |
| 249 | return err |
| 250 | } |
| 251 | return unmapIntransitiveActivityProperties(mm, i) |
| 252 | } |
| 253 | |
| 254 | // IntransitiveActivityNew initializes a intransitive activity |
| 255 | func IntransitiveActivityNew(id ID, typ ActivityVocabularyType) *IntransitiveActivity { |
| 256 | if !IntransitiveActivityTypes.Match(typ) { |
| 257 | typ = IntransitiveActivityType |
| 258 | } |
| 259 | i := IntransitiveActivity{ID: id, Type: typ} |
| 260 | i.Name = NaturalLanguageValuesNew() |
| 261 | i.Content = NaturalLanguageValuesNew() |
| 262 | |
| 263 | return &i |
| 264 | } |
| 265 | |
| 266 | // ToIntransitiveActivity tries to convert it Item to an IntransitiveActivity object |
| 267 | func ToIntransitiveActivity(it LinkOrIRI) (*IntransitiveActivity, error) { |
| 268 | switch i := it.(type) { |
| 269 | case *IntransitiveActivity: |
| 270 | return i, nil |
| 271 | case IntransitiveActivity: |
| 272 | return &i, nil |
| 273 | case *Question: |
| 274 | return (*IntransitiveActivity)(unsafe.Pointer(i)), nil |
| 275 | case Question: |
| 276 | return (*IntransitiveActivity)(unsafe.Pointer(&i)), nil |
| 277 | case *Activity: |
| 278 | return (*IntransitiveActivity)(unsafe.Pointer(i)), nil |
| 279 | case Activity: |
| 280 | return (*IntransitiveActivity)(unsafe.Pointer(&i)), nil |
| 281 | default: |
| 282 | return reflectItemToType[IntransitiveActivity](it) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // ArriveNew initializes an Arrive activity |
| 287 | func ArriveNew(id ID) *Arrive { |
| 288 | a := IntransitiveActivityNew(id, ArriveType) |
| 289 | o := Arrive(*a) |
| 290 | return &o |
| 291 | } |
| 292 | |
| 293 | // TravelNew initializes a Travel activity |
| 294 | func TravelNew(id ID) *Travel { |
| 295 | a := IntransitiveActivityNew(id, TravelType) |
| 296 | o := Travel(*a) |
| 297 | return &o |
| 298 | } |
| 299 | |
| 300 | // Equal verifies if our receiver IntransitiveActivity is equals with the "with" Item |
| 301 | func (i *IntransitiveActivity) Equals(with Item) bool { |
| 302 | if IsNil(with) { |
| 303 | return i == nil |
| 304 | } |
| 305 | withActivity, err := ToIntransitiveActivity(with) |
| 306 | if err != nil { |
| 307 | return false |
| 308 | } |
| 309 | return i.equal(*withActivity) |
| 310 | } |
| 311 | |
| 312 | // equal verifies if our receiver IntransitiveActivity is equals with the "with" IntransitiveActivity |
| 313 | func (i IntransitiveActivity) equal(with IntransitiveActivity) bool { |
| 314 | result := true |
| 315 | |
| 316 | _ = OnObject(i, func(oa *Object) error { |
| 317 | result = oa.Equals(with) |
| 318 | return nil |
| 319 | }) |
| 320 | if with.Actor != nil { |
| 321 | if !ItemsEqual(i.Actor, with.Actor) { |
| 322 | result = false |
| 323 | } |
| 324 | } |
| 325 | if with.Target != nil { |
| 326 | if !ItemsEqual(i.Target, with.Target) { |
| 327 | result = false |
| 328 | } |
| 329 | } |
| 330 | if with.Result != nil { |
| 331 | if !ItemsEqual(i.Result, with.Result) { |
| 332 | result = false |
| 333 | } |
| 334 | } |
| 335 | if with.Origin != nil { |
| 336 | if !ItemsEqual(i.Origin, with.Origin) { |
| 337 | result = false |
| 338 | } |
| 339 | } |
| 340 | if with.Instrument != nil { |
| 341 | if !ItemsEqual(i.Instrument, with.Instrument) { |
| 342 | result = false |
| 343 | } |
| 344 | } |
| 345 | return result |
| 346 | } |
| 347 | |
| 348 | func (i IntransitiveActivity) Format(s fmt.State, verb rune) { |
| 349 | switch verb { |
| 350 | case 's': |
| 351 | if HasTypes(i) && i.ID != "" { |
| 352 | _, _ = fmt.Fprintf(s, "%T[%s]( %s )", i, i.GetType(), i.ID) |
| 353 | } else if i.ID != "" { |
| 354 | _, _ = fmt.Fprintf(s, "%T( %s )", i, i.ID) |
| 355 | } else { |
| 356 | _, _ = fmt.Fprintf(s, "%T[%p]", i, &i) |
| 357 | } |
| 358 | case 'v': |
| 359 | _, _ = fmt.Fprintf(s, "%T[%s] {", i, i.GetType()) |
| 360 | _ = fmtIntransitiveActivityProps(s)(&i) |
| 361 | _, _ = io.WriteString(s, " }") |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | func fmtIntransitiveActivityProps(w io.Writer) func(*IntransitiveActivity) error { |
| 366 | return func(ia *IntransitiveActivity) error { |
| 367 | if !IsNil(ia.Actor) { |
| 368 | _, _ = fmt.Fprintf(w, " actor: %s", ia.Actor) |
| 369 | } |
| 370 | if !IsNil(ia.Target) { |
| 371 | _, _ = fmt.Fprintf(w, " target: %s", ia.Target) |
| 372 | } |
| 373 | if !IsNil(ia.Result) { |
| 374 | _, _ = fmt.Fprintf(w, " result: %s", ia.Result) |
| 375 | } |
| 376 | if !IsNil(ia.Origin) { |
| 377 | _, _ = fmt.Fprintf(w, " origin: %s", ia.Origin) |
| 378 | } |
| 379 | if !IsNil(ia.Instrument) { |
| 380 | _, _ = fmt.Fprintf(w, " instrument: %s", ia.Instrument) |
| 381 | } |
| 382 | return OnObject(ia, fmtObjectProps(w)) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // OnIntransitiveActivity calls function fn on it Item if it can be asserted |
| 387 | // to type *IntransitiveActivity |
| 388 | // |
| 389 | // This function should be called if trying to access the IntransitiveActivity |
| 390 | // specific properties like "actor", for the other properties OnObject |
| 391 | // should be used instead. |
| 392 | func OnIntransitiveActivity(it LinkOrIRI, fn func(*IntransitiveActivity) error) error { |
| 393 | if it == nil { |
| 394 | return nil |
| 395 | } |
| 396 | if IsItemCollection(it) { |
| 397 | return callOnItemCollection(it, OnIntransitiveActivity, fn) |
| 398 | } |
| 399 | act, err := ToIntransitiveActivity(it) |
| 400 | if err != nil { |
| 401 | return err |
| 402 | } |
| 403 | return fn(act) |
| 404 | } |