Debian Codegraph @

ordered_collection_page.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "bytes"
5 "encoding/gob"
6 "fmt"
7 "time"
8 "unsafe"
9
10 "github.com/valyala/fastjson"
11 )
12
13 // OrderedCollectionPage type extends from both CollectionPage and OrderedCollection.
14 // In addition to the properties inherited from each of those, the OrderedCollectionPage
15 // may contain an additional startIndex property whose value indicates the relative index position
16 // of the first item contained by the page within the OrderedCollection to which the page belongs.
17 type OrderedCollectionPage 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 // In a paged Collection, indicates the page that contains the most recently updated member items.
111 Current ObjectOrLink `jsonld:"current,omitempty"`
112 // In a paged Collection, indicates the furthest preceding page of items in the collection.
113 First ObjectOrLink `jsonld:"first,omitempty"`
114 // In a paged Collection, indicates the furthest proceeding page of the collection.
115 Last ObjectOrLink `jsonld:"last,omitempty"`
116 // A non-negative integer specifying the total number of objects contained by the logical view of the collection.
117 // This number might not reflect the actual number of items serialized within the Collection object instance.
118 TotalItems uint `jsonld:"totalItems"`
119 // Identifies the items contained in a collection. The items might be ordered or unordered.
120 OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"`
121 // Identifies the Collection to which a CollectionPage objects items belong.
122 PartOf Item `jsonld:"partOf,omitempty"`
123 // In a paged Collection, indicates the next page of items.
124 Next Item `jsonld:"next,omitempty"`
125 // In a paged Collection, identifies the previous page of items.
126 Prev Item `jsonld:"prev,omitempty"`
127 // A non-negative integer value identifying the relative position within the logical view of a strictly ordered collection.
128 StartIndex uint `jsonld:"startIndex,omitempty"`
129 }
130
131 // GetID returns the ID corresponding to the OrderedCollectionPage object
132 func (o OrderedCollectionPage) GetID() ID {
133 return o.ID
134 }
135
136 // GetType returns the OrderedCollectionPage's type
137 func (o OrderedCollectionPage) GetType() Typer {
138 return o.Type
139 }
140
141 // IsLink returns false for a OrderedCollectionPage object
142 func (o OrderedCollectionPage) IsLink() bool {
143 return false
144 }
145
146 // IsObject returns true for a OrderedCollectionPage object
147 func (o OrderedCollectionPage) IsObject() bool {
148 return true
149 }
150
151 // IsCollection returns true for OrderedCollectionPage objects
152 func (o OrderedCollectionPage) IsCollection() bool {
153 return true
154 }
155
156 // GetLink returns the IRI corresponding to the OrderedCollectionPage object
157 func (o OrderedCollectionPage) GetLink() IRI {
158 return IRI(o.ID)
159 }
160
161 // Collection returns the underlying Collection type
162 func (o OrderedCollectionPage) Collection() ItemCollection {
163 return o.OrderedItems
164 }
165
166 // Match returns whether the receiver matches the ActivityVocabularyType arguments.
167 func (o OrderedCollectionPage) Match(tt ...ActivityVocabularyType) bool {
168 return ActivityVocabularyTypes(tt).Match(o.Type)
169 }
170
171 // Count returns the maximum between the length of Items in the collection page and its TotalItems property
172 func (o *OrderedCollectionPage) Count() uint {
173 if o == nil {
174 return 0
175 }
176 return uint(len(o.OrderedItems))
177 }
178
179 // Append adds an element to an OrderedCollectionPage
180 func (o *OrderedCollectionPage) Append(it ...Item) error {
181 for _, ob := range it {
182 if !o.OrderedItems.Contains(ob) {
183 o.OrderedItems = append(o.OrderedItems, ob)
184 o.TotalItems += 1
185 }
186 }
187 return nil
188 }
189
190 // Remove removes items from an OrderedCollectionPage
191 func (o *OrderedCollectionPage) Remove(it ...Item) {
192 for _, ob := range it {
193 if o.OrderedItems.Contains(ob) {
194 o.OrderedItems.Remove(ob)
195 o.TotalItems -= 1
196 }
197 }
198 }
199
200 // Contains verifies if OrderedCollectionPage array contains the received one
201 func (o OrderedCollectionPage) Contains(r Item) bool {
202 if len(o.OrderedItems) == 0 {
203 return false
204 }
205 for _, it := range o.OrderedItems {
206 if ItemsEqual(it, r) {
207 return true
208 }
209 }
210 return false
211 }
212
213 // UnmarshalJSON decodes an incoming JSON document into the receiver object.
214 func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error {
215 p := fastjson.Parser{}
216 val, err := p.ParseBytes(data)
217 if err != nil {
218 return err
219 }
220 return JSONLoadOrderedCollectionPage(val, o)
221 }
222
223 // MarshalJSON encodes the receiver object to a JSON document.
224 func (o OrderedCollectionPage) MarshalJSON() ([]byte, error) {
225 b := make([]byte, 0)
226 notEmpty := false
227 JSONWrite(&b, '{')
228
229 OnObject(o, func(o *Object) error {
230 notEmpty = JSONWriteObjectValue(&b, *o)
231 return nil
232 })
233 if o.PartOf != nil {
234 notEmpty = JSONWriteItemProp(&b, "partOf", o.PartOf) || notEmpty
235 }
236 if o.Current != nil {
237 notEmpty = JSONWriteItemProp(&b, "current", o.Current) || notEmpty
238 }
239 if o.First != nil {
240 notEmpty = JSONWriteItemProp(&b, "first", o.First) || notEmpty
241 }
242 if o.Last != nil {
243 notEmpty = JSONWriteItemProp(&b, "last", o.Last) || notEmpty
244 }
245 if o.Next != nil {
246 notEmpty = JSONWriteItemProp(&b, "next", o.Next) || notEmpty
247 }
248 if o.Prev != nil {
249 notEmpty = JSONWriteItemProp(&b, "prev", o.Prev) || notEmpty
250 }
251 notEmpty = JSONWriteIntProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
252 if o.OrderedItems != nil {
253 notEmpty = JSONWriteItemCollectionProp(&b, "orderedItems", o.OrderedItems, false) || notEmpty
254 }
255 if notEmpty {
256 JSONWrite(&b, '}')
257 return b, nil
258 }
259 return nil, nil
260 }
261
262 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
263 func (o *OrderedCollectionPage) UnmarshalBinary(data []byte) error {
264 return o.GobDecode(data)
265 }
266
267 // MarshalBinary implements the encoding.BinaryMarshaler interface.
268 func (o OrderedCollectionPage) MarshalBinary() ([]byte, error) {
269 return o.GobEncode()
270 }
271
272 // GobEncode
273 func (o OrderedCollectionPage) GobEncode() ([]byte, error) {
274 mm := make(map[string][]byte)
275 hasData, err := mapOrderedCollectionPageProperties(mm, o)
276 if err != nil {
277 return nil, err
278 }
279 if !hasData {
280 return []byte{}, nil
281 }
282 bb := bytes.Buffer{}
283 g := gob.NewEncoder(&bb)
284 if err := g.Encode(mm); err != nil {
285 return nil, err
286 }
287 return bb.Bytes(), nil
288 }
289
290 // GobDecode
291 func (o *OrderedCollectionPage) GobDecode(data []byte) error {
292 if len(data) == 0 {
293 return nil
294 }
295 mm, err := gobDecodeObjectAsMap(data)
296 if err != nil {
297 return err
298 }
299 return unmapOrderedCollectionPageProperties(mm, o)
300 }
301
302 // ToOrderedCollectionPage
303 func ToOrderedCollectionPage(it LinkOrIRI) (*OrderedCollectionPage, error) {
304 switch i := it.(type) {
305 case *OrderedCollectionPage:
306 return i, nil
307 case OrderedCollectionPage:
308 return &i, nil
309 // NOTE(marius): let's try again to convert CollectionPage -> OrderedCollectionPage, as they have the same
310 // shape in memory.
311 case *CollectionPage:
312 return (*OrderedCollectionPage)(unsafe.Pointer(i)), nil
313 case CollectionPage:
314 return (*OrderedCollectionPage)(unsafe.Pointer(&i)), nil
315 default:
316 return reflectItemToType[OrderedCollectionPage](it)
317 }
318 }
319
320 // ItemsMatch
321 func (o OrderedCollectionPage) ItemsMatch(col ...Item) bool {
322 for _, it := range col {
323 if match := o.OrderedItems.Contains(it); !match {
324 return false
325 }
326 }
327 return true
328 }
329
330 // Equals verifies if our receiver OrderedCollectionPage is equals with the "with" Item
331 func (o *OrderedCollectionPage) Equals(with Item) bool {
332 if IsNil(with) {
333 return o == nil
334 }
335 if !with.IsCollection() {
336 return false
337 }
338 withPage, err := ToOrderedCollectionPage(with)
339 if err != nil {
340 return false
341 }
342 return o.equal(*withPage)
343 }
344
345 // equal verifies if our receiver OrderedCollectionPage is equals with the "with" OrderedCollectionPage
346 func (o OrderedCollectionPage) equal(with OrderedCollectionPage) bool {
347 result := true
348
349 _ = OnOrderedCollection(with, func(wo *OrderedCollection) error {
350 if !wo.Equals(o) {
351 result = false
352 }
353 return nil
354 })
355 if with.PartOf != nil {
356 if !ItemsEqual(o.PartOf, with.PartOf) {
357 result = false
358 }
359 }
360 if with.Current != nil {
361 if !ItemsEqual(o.Current, with.Current) {
362 result = false
363 }
364 }
365 if with.First != nil {
366 if !ItemsEqual(o.First, with.First) {
367 result = false
368 }
369 }
370 if with.Last != nil {
371 if !ItemsEqual(o.Last, with.Last) {
372 result = false
373 }
374 }
375 if with.Next != nil {
376 if !ItemsEqual(o.Next, with.Next) {
377 result = false
378 }
379 }
380 if with.Prev != nil {
381 if !ItemsEqual(o.Prev, with.Prev) {
382 result = false
383 }
384 }
385 return result
386 }
387
388 func (o OrderedCollectionPage) Format(s fmt.State, verb rune) {
389 switch verb {
390 case 's', 'v':
391 _, _ = fmt.Fprintf(s, "%T[%s] { totalItems: %d }", o, o.GetType(), o.TotalItems)
392 }
393 }
394
395 func (o *OrderedCollectionPage) Recipients() ItemCollection {
396 aud := o.Audience
397 return ItemCollectionDeduplication(&o.To, &o.CC, &o.Bto, &o.BCC, &aud)
398 }
399
400 func (o *OrderedCollectionPage) Clean() {
401 _ = OnObject(o, func(o *Object) error {
402 o.Clean()
403 return nil
404 })
405 }
406
407 // OnOrderedCollectionPage calls function fn on it Item if it can be asserted
408 // to type *OrderedCollectionPage
409 //
410 // This function should be called if trying to access the OrderedCollectionPage specific
411 // properties like "partOf", "next", "perv". For the other properties
412 // OnObject or OnOrderedCollection should be used instead.
413 func OnOrderedCollectionPage(it Item, fn WithOrderedCollectionPageFn) error {
414 if it == nil {
415 return nil
416 }
417 col, err := ToOrderedCollectionPage(it)
418 if err != nil {
419 return err
420 }
421 return fn(col)
422 }