| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/gob" |
| 6 | "fmt" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/valyala/fastjson" |
| 10 | ) |
| 11 | |
| 12 | // Relationship describes a relationship between two individuals. |
| 13 | // The subject and object properties are used to identify the connected individuals. |
| 14 | // See 5.2 Representing Relationships Between Entities for additional information. |
| 15 | // |
| 16 | // 5.2: The relationship property specifies the kind of relationship that exists between the two individuals identified |
| 17 | // by the subject and object properties. Used together, these three properties form what is commonly known |
| 18 | // as a "reified statement" where subject identifies the subject, relationship identifies the predicate, |
| 19 | // and object identifies the object. |
| 20 | type Relationship 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 | // Subject property identifies one of the connected individuals. |
| 114 | // For instance, for a Relationship object describing "John is related to Sally", subject would refer to John. |
| 115 | Subject Item `jsonld:"subject,omitempty"` |
| 116 | // Object property identifies one of the connected individuals. |
| 117 | // For instance, for a Relationship object describing "John is related to Sally", object would refer to Sally. |
| 118 | Object Item `jsonld:"object,omitempty"` |
| 119 | // Relationship property identifies the kind of relationship that exists between subject and object. |
| 120 | Relationship Item `jsonld:"relationship,omitempty"` |
| 121 | } |
| 122 | |
| 123 | // IsLink returns false for Relationship objects |
| 124 | func (r Relationship) IsLink() bool { |
| 125 | return false |
| 126 | } |
| 127 | |
| 128 | // IsObject returns true for Relationship objects |
| 129 | func (r Relationship) IsObject() bool { |
| 130 | return true |
| 131 | } |
| 132 | |
| 133 | // IsCollection returns false for Relationship objects |
| 134 | func (r Relationship) IsCollection() bool { |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | // GetLink returns the IRI corresponding to the current Relationship object |
| 139 | func (r Relationship) GetLink() IRI { |
| 140 | return IRI(r.ID) |
| 141 | } |
| 142 | |
| 143 | // GetType returns the type of the current Relationship |
| 144 | func (r Relationship) GetType() Typer { |
| 145 | return r.Type |
| 146 | } |
| 147 | |
| 148 | // GetID returns the ID corresponding to the current Relationship |
| 149 | func (r Relationship) GetID() ID { |
| 150 | return r.ID |
| 151 | } |
| 152 | |
| 153 | // Match returns whether the receiver matches the ActivityVocabularyType arguments. |
| 154 | func (r Relationship) Match(tt ...ActivityVocabularyType) bool { |
| 155 | return ActivityVocabularyTypes(tt).Match(r.Type) |
| 156 | } |
| 157 | |
| 158 | // UnmarshalJSON decodes an incoming JSON document into the receiver object. |
| 159 | func (r *Relationship) UnmarshalJSON(data []byte) error { |
| 160 | par := fastjson.Parser{} |
| 161 | val, err := par.ParseBytes(data) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | return JSONLoadRelationship(val, r) |
| 166 | } |
| 167 | |
| 168 | // MarshalJSON encodes the receiver object to a JSON document. |
| 169 | func (r Relationship) MarshalJSON() ([]byte, error) { |
| 170 | b := make([]byte, 0) |
| 171 | notEmpty := false |
| 172 | JSONWrite(&b, '{') |
| 173 | |
| 174 | OnObject(r, func(o *Object) error { |
| 175 | notEmpty = JSONWriteObjectValue(&b, *o) |
| 176 | return nil |
| 177 | }) |
| 178 | |
| 179 | if r.Subject != nil { |
| 180 | notEmpty = JSONWriteItemProp(&b, "subject", r.Subject) || notEmpty |
| 181 | } |
| 182 | if r.Object != nil { |
| 183 | notEmpty = JSONWriteItemProp(&b, "object", r.Object) || notEmpty |
| 184 | } |
| 185 | if r.Relationship != nil { |
| 186 | notEmpty = JSONWriteItemProp(&b, "relationship", r.Relationship) || notEmpty |
| 187 | } |
| 188 | |
| 189 | if notEmpty { |
| 190 | JSONWrite(&b, '}') |
| 191 | return b, nil |
| 192 | } |
| 193 | return nil, nil |
| 194 | } |
| 195 | |
| 196 | // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. |
| 197 | func (r *Relationship) UnmarshalBinary(data []byte) error { |
| 198 | return r.GobDecode(data) |
| 199 | } |
| 200 | |
| 201 | // MarshalBinary implements the encoding.BinaryMarshaler interface. |
| 202 | func (r Relationship) MarshalBinary() ([]byte, error) { |
| 203 | return r.GobEncode() |
| 204 | } |
| 205 | |
| 206 | // GobEncode |
| 207 | func (r Relationship) GobEncode() ([]byte, error) { |
| 208 | mm := make(map[string][]byte) |
| 209 | hasData, err := mapRelationshipProperties(mm, r) |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | if !hasData { |
| 214 | return []byte{}, nil |
| 215 | } |
| 216 | bb := bytes.Buffer{} |
| 217 | g := gob.NewEncoder(&bb) |
| 218 | if err := g.Encode(mm); err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | return bb.Bytes(), nil |
| 222 | } |
| 223 | |
| 224 | // GobDecode |
| 225 | func (r *Relationship) GobDecode(data []byte) error { |
| 226 | if len(data) == 0 { |
| 227 | return nil |
| 228 | } |
| 229 | mm, err := gobDecodeObjectAsMap(data) |
| 230 | if err != nil { |
| 231 | return err |
| 232 | } |
| 233 | return unmapRelationshipProperties(mm, r) |
| 234 | } |
| 235 | |
| 236 | // Recipients performs recipient de-duplication on the Relationship object's To, Bto, CC and BCC properties |
| 237 | func (r *Relationship) Recipients() ItemCollection { |
| 238 | aud := r.Audience |
| 239 | return ItemCollectionDeduplication(&r.To, &r.CC, &r.Bto, &r.BCC, &aud) |
| 240 | } |
| 241 | |
| 242 | // Clean removes Bto and BCC properties |
| 243 | func (r *Relationship) Clean() { |
| 244 | _ = OnObject(r, func(o *Object) error { |
| 245 | o.Clean() |
| 246 | return nil |
| 247 | }) |
| 248 | } |
| 249 | |
| 250 | func (r Relationship) Format(s fmt.State, verb rune) { |
| 251 | switch verb { |
| 252 | case 's', 'v': |
| 253 | _, _ = fmt.Fprintf(s, "%T[%s] { }", r, r.Type) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // ToRelationship tries to convert the "it" Item to a Relationship object. |
| 258 | func ToRelationship(it LinkOrIRI) (*Relationship, error) { |
| 259 | switch i := it.(type) { |
| 260 | case *Relationship: |
| 261 | return i, nil |
| 262 | case Relationship: |
| 263 | return &i, nil |
| 264 | default: |
| 265 | return reflectItemToType[Relationship](it) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | type withRelationshipFn func(*Relationship) error |
| 270 | |
| 271 | // OnRelationship calls function fn on it Item if it can be asserted to type *Relationship |
| 272 | // |
| 273 | // This function should be called if trying to access the Relationship specific properties |
| 274 | // like "subject", "object", or "relationship". |
| 275 | // For the other properties OnObject should be used instead. |
| 276 | func OnRelationship(it LinkOrIRI, fn func(*Relationship) error) error { |
| 277 | if it == nil { |
| 278 | return nil |
| 279 | } |
| 280 | if IsItemCollection(it) { |
| 281 | return callOnItemCollection(it, OnRelationship, fn) |
| 282 | } |
| 283 | ob, err := ToRelationship(it) |
| 284 | if err != nil { |
| 285 | return err |
| 286 | } |
| 287 | return fn(ob) |
| 288 | } |