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