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