Debian Codegraph @

ordered_collection.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 // OrderedCollection is a subtype of Collection in which members of the logical
14 // collection are assumed to always be strictly ordered.
15 type OrderedCollection struct {
16 // ID provides the globally unique identifier for an Activity Pub Object or Link.
17 ID ID `jsonld:"id,omitempty"`
18 // Type identifies the Activity Pub Object or Link type. Multiple values may be specified.
19 Type Typer `jsonld:"type,omitempty"`
20 // Name a simple, human-readable, plain-text name for the object.
21 // HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged values.
22 Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"`
23 // Attachment identifies a resource attached or related to an object that potentially requires special handling.
24 // The intent is to provide a model that is at least semantically similar to attachments in email.
25 Attachment Item `jsonld:"attachment,omitempty"`
26 // AttributedTo identifies one or more entities to which this object is attributed. The attributed entities might not be Actors.
27 // For instance, an object might be attributed to the completion of another activity.
28 AttributedTo Item `jsonld:"attributedTo,omitempty"`
29 // Audience identifies one or more entities that represent the total population of entities
30 // for which the object can considered to be relevant.
31 Audience ItemCollection `jsonld:"audience,omitempty"`
32 // Content or textual representation of the Activity Pub Object encoded as a JSON string.
33 // By default, the value of content is HTML.
34 // The mediaType property can be used in the object to indicate a different content type.
35 // (The content MAY be expressed using multiple language-tagged values.)
36 Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"`
37 // Context identifies the context within which the object exists or an activity was performed.
38 // The notion of "context" used is intentionally vague.
39 // The intended function is to serve as a means of grouping objects and activities that share a
40 // common originating context or purpose. An example could be all activities relating to a common project or event.
41 Context Item `jsonld:"context,omitempty"`
42 // MediaType when used on an Object, identifies the MIME media type of the value of the content property.
43 // If not specified, the content property is assumed to contain text/html content.
44 MediaType MimeType `jsonld:"mediaType,omitempty"`
45 // EndTime the date and time describing the actual or expected ending time of the object.
46 // When used with an Activity object, for instance, the endTime property specifies the moment
47 // the activity concluded or is expected to conclude.
48 EndTime time.Time `jsonld:"endTime,omitempty"`
49 // Generator identifies the entity (e.g. an application) that generated the object.
50 Generator Item `jsonld:"generator,omitempty"`
51 // Icon indicates an entity that describes an icon for this object.
52 // The image should have an aspect ratio of one (horizontal) to one (vertical)
53 // and should be suitable for presentation at a small size.
54 Icon Item `jsonld:"icon,omitempty"`
55 // Image indicates an entity that describes an image for this object.
56 // Unlike the icon property, there are no aspect ratio or display size limitations assumed.
57 Image Item `jsonld:"image,omitempty"`
58 // InReplyTo indicates one or more entities for which this object is considered a response.
59 InReplyTo Item `jsonld:"inReplyTo,omitempty"`
60 // Location indicates one or more physical or logical locations associated with the object.
61 Location Item `jsonld:"location,omitempty"`
62 // Preview identifies an entity that provides a preview of this object.
63 Preview Item `jsonld:"preview,omitempty"`
64 // Published the date and time at which the object was published
65 Published time.Time `jsonld:"published,omitempty"`
66 // Replies identifies a Collection containing objects considered to be responses to this object.
67 Replies Item `jsonld:"replies,omitempty"`
68 // StartTime the date and time describing the actual or expected starting time of the object.
69 // When used with an Activity object, for instance, the startTime property specifies
70 // the moment the activity began or is scheduled to begin.
71 StartTime time.Time `jsonld:"startTime,omitempty"`
72 // Summary a natural language summarization of the object encoded as HTML.
73 // *Multiple language tagged summaries may be provided.)
74 Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"`
75 // Tag one or more "tags" that have been associated with an objects. A tag can be any kind of Activity Pub Object.
76 // The key difference between attachment and tag is that the former implies association by inclusion,
77 // while the latter implies associated by reference.
78 Tag ItemCollection `jsonld:"tag,omitempty"`
79 // Updated the date and time at which the object was updated
80 Updated time.Time `jsonld:"updated,omitempty"`
81 // URL identifies one or more links to representations of the object
82 URL Item `jsonld:"url,omitempty"`
83 // To identifies an entity considered to be part of the public primary audience of an Activity Pub Object
84 To ItemCollection `jsonld:"to,omitempty"`
85 // Bto identifies anActivity Pub Object that is part of the private primary audience of this Activity Pub Object.
86 Bto ItemCollection `jsonld:"bto,omitempty"`
87 // CC identifies anActivity Pub Object that is part of the public secondary audience of this Activity Pub Object.
88 CC ItemCollection `jsonld:"cc,omitempty"`
89 // BCC identifies one or more Objects that are part of the private secondary audience of this Activity Pub Object.
90 BCC ItemCollection `jsonld:"bcc,omitempty"`
91 // Duration when the object describes a time-bound resource, such as an audio or video, a meeting, etc,
92 // the duration property indicates the object's approximate duration.
93 // The value must be expressed as an xsd:duration as defined by [ xmlschema11-2],
94 // section 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
95 Duration time.Duration `jsonld:"duration,omitempty"`
96 // This is a list of all Like activities with this object as the object property, added as a side effect.
97 // The likes collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges
98 // of an authenticated user or as appropriate when no authentication is given.
99 Likes Item `jsonld:"likes,omitempty"`
100 // This is a list of all Announce activities with this object as the object property, added as a side effect.
101 // The shares collection MUST be either an OrderedCollection or a Collection and MAY be filtered on privileges
102 // of an authenticated user or as appropriate when no authentication is given.
103 Shares Item `jsonld:"shares,omitempty"`
104 // Source property is intended to convey some sort of source from which the content markup was derived,
105 // as a form of provenance, or to support future editing by clients.
106 // In general, clients do the conversion from source to content, not the other way around.
107 Source Source `jsonld:"source,omitempty"`
108 // In a paged Collection, indicates the page that contains the most recently updated member items.
109 Current ObjectOrLink `jsonld:"current,omitempty"`
110 // In a paged Collection, indicates the furthest preceding page of items in the collection.
111 First ObjectOrLink `jsonld:"first,omitempty"`
112 // In a paged Collection, indicates the furthest proceeding page of the collection.
113 Last ObjectOrLink `jsonld:"last,omitempty"`
114 // A non-negative integer specifying the total number of objects contained by the logical view of the collection.
115 // This number might not reflect the actual number of items serialized within the Collection object instance.
116 TotalItems uint `jsonld:"totalItems"`
117 // Identifies the items contained in a collection. The items might be ordered or unordered.
118 OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"`
119 }
120
121 type (
122 // InboxStream contains all activities received by the actor.
123 // The server SHOULD filter content according to the requester's permission.
124 // In general, the owner of an inbox is likely to be able to access all of their inbox contents.
125 // Depending on access control, some other content may be public, whereas other content may
126 // require authentication for non-owner users, if they can access the inbox at all.
127 InboxStream = OrderedCollection
128
129 // LikedCollection is a list of every object from all of the actor's Like activities,
130 // added as a side effect. The liked collection MUST be either an OrderedCollection or
131 // a Collection and MAY be filtered on privileges of an authenticated user or as
132 // appropriate when no authentication is given.
133 LikedCollection = OrderedCollection
134
135 // LikesCollection is a list of all Like activities with this object as the object property,
136 // added as a side effect. The likes collection MUST be either an OrderedCollection or a Collection
137 // and MAY be filtered on privileges of an authenticated user or as appropriate when
138 // no authentication is given.
139 LikesCollection = OrderedCollection
140
141 // OutboxStream contains activities the user has published,
142 // subject to the ability of the requestor to retrieve the activity (that is,
143 // the contents of the outbox are filtered by the permissions of the person reading it).
144 OutboxStream = OrderedCollection
145
146 // SharesCollection is a list of all Announce activities with this object as the object property,
147 // added as a side effect. The shares collection MUST be either an OrderedCollection or a Collection
148 // and MAY be filtered on privileges of an authenticated user or as appropriate when no authentication
149 // is given.
150 SharesCollection = OrderedCollection
151 )
152
153 // GetType returns the OrderedCollection's type
154 func (o OrderedCollection) GetType() Typer {
155 return o.Type
156 }
157
158 // IsLink returns false for an OrderedCollection object
159 func (o OrderedCollection) IsLink() bool {
160 return false
161 }
162
163 // GetID returns the ID corresponding to the OrderedCollection
164 func (o OrderedCollection) GetID() ID {
165 return o.ID
166 }
167
168 // GetLink returns the IRI corresponding to the OrderedCollection object
169 func (o OrderedCollection) GetLink() IRI {
170 return IRI(o.ID)
171 }
172
173 // Match returns whether the receiver matches the ActivityVocabularyType arguments.
174 func (o OrderedCollection) Match(tt ...ActivityVocabularyType) bool {
175 return ActivityVocabularyTypes(tt).Match(o.Type)
176 }
177
178 // IsObject returns true for am OrderedCollection object
179 func (o OrderedCollection) IsObject() bool {
180 return true
181 }
182
183 // Collection returns the underlying Collection type
184 func (o OrderedCollection) Collection() ItemCollection {
185 return o.OrderedItems
186 }
187
188 // IsCollection returns true for OrderedCollection objects.
189 func (o OrderedCollection) IsCollection() bool {
190 return true
191 }
192
193 // Contains verifies if OrderedCollection array contains the received item r.
194 func (o OrderedCollection) Contains(r Item) bool {
195 if len(o.OrderedItems) == 0 {
196 return false
197 }
198 for _, it := range o.OrderedItems {
199 if ItemsEqual(it, r) {
200 return true
201 }
202 }
203 return false
204 }
205
206 // Count returns the maximum between the length of Items in collection and its TotalItems property
207 func (o *OrderedCollection) Count() uint {
208 if o == nil {
209 return 0
210 }
211 return uint(len(o.OrderedItems))
212 }
213
214 // Append adds an element to an the receiver collection object.
215 func (o *OrderedCollection) Append(it ...Item) error {
216 for _, ob := range it {
217 if !o.OrderedItems.Contains(ob) {
218 o.OrderedItems = append(o.OrderedItems, ob)
219 o.TotalItems += 1
220 }
221 }
222 return nil
223 }
224
225 // Remove removes items from an OrderedCollection
226 func (o *OrderedCollection) Remove(it ...Item) {
227 for _, ob := range it {
228 if o.OrderedItems.Contains(ob) {
229 o.OrderedItems.Remove(ob)
230 o.TotalItems -= 1
231 }
232 }
233 }
234
235 // UnmarshalJSON decodes an incoming JSON document into the receiver object.
236 func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
237 p := fastjson.Parser{}
238 val, err := p.ParseBytes(data)
239 if err != nil {
240 return err
241 }
242 return JSONLoadOrderedCollection(val, o)
243 }
244
245 // MarshalJSON encodes the receiver object to a JSON document.
246 func (o OrderedCollection) MarshalJSON() ([]byte, error) {
247 b := make([]byte, 0)
248 notEmpty := false
249 JSONWrite(&b, '{')
250
251 OnObject(o, func(o *Object) error {
252 notEmpty = JSONWriteObjectValue(&b, *o)
253 return nil
254 })
255 if o.Current != nil {
256 notEmpty = JSONWriteItemProp(&b, "current", o.Current) || notEmpty
257 }
258 if o.First != nil {
259 notEmpty = JSONWriteItemProp(&b, "first", o.First) || notEmpty
260 }
261 if o.Last != nil {
262 notEmpty = JSONWriteItemProp(&b, "last", o.Last) || notEmpty
263 }
264 notEmpty = JSONWriteIntProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty
265 if o.OrderedItems != nil {
266 notEmpty = JSONWriteItemCollectionProp(&b, "orderedItems", o.OrderedItems, false) || notEmpty
267 }
268 if notEmpty {
269 JSONWrite(&b, '}')
270 return b, nil
271 }
272 return nil, nil
273 }
274
275 // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
276 func (o *OrderedCollection) UnmarshalBinary(data []byte) error {
277 return o.GobDecode(data)
278 }
279
280 // MarshalBinary implements the encoding.BinaryMarshaler interface.
281 func (o OrderedCollection) MarshalBinary() ([]byte, error) {
282 return o.GobEncode()
283 }
284
285 // GobEncode
286 func (o OrderedCollection) GobEncode() ([]byte, error) {
287 mm := make(map[string][]byte)
288 hasData, err := mapOrderedCollectionProperties(mm, o)
289 if err != nil {
290 return nil, err
291 }
292 if !hasData {
293 return []byte{}, nil
294 }
295 bb := bytes.Buffer{}
296 g := gob.NewEncoder(&bb)
297 if err := g.Encode(mm); err != nil {
298 return nil, err
299 }
300 return bb.Bytes(), nil
301 }
302
303 // GobDecode
304 func (o *OrderedCollection) GobDecode(data []byte) error {
305 if len(data) == 0 {
306 return nil
307 }
308 mm, err := gobDecodeObjectAsMap(data)
309 if err != nil {
310 return err
311 }
312 return unmapOrderedCollectionProperties(mm, o)
313 }
314
315 // OrderedCollectionPageNew initializes a new OrderedCollectionPage
316 func OrderedCollectionPageNew(parent CollectionInterface) *OrderedCollectionPage {
317 p := OrderedCollectionPage{
318 PartOf: parent.GetLink(),
319 }
320 if pc, ok := parent.(*OrderedCollection); ok {
321 copyOrderedCollectionToPage(pc, &p)
322 }
323 p.Type = OrderedCollectionPageType
324 return &p
325 }
326
327 // ToOrderedCollection
328 func ToOrderedCollection(it LinkOrIRI) (*OrderedCollection, error) {
329 switch i := it.(type) {
330 case *OrderedCollection:
331 return i, nil
332 case OrderedCollection:
333 return &i, nil
334 case *OrderedCollectionPage:
335 return (*OrderedCollection)(unsafe.Pointer(i)), nil
336 case OrderedCollectionPage:
337 return (*OrderedCollection)(unsafe.Pointer(&i)), nil
338 // NOTE(marius): let's try again to convert Collection -> OrderedCollection, as they have the same
339 // shape in memory.
340 case *Collection:
341 return (*OrderedCollection)(unsafe.Pointer(i)), nil
342 case Collection:
343 return (*OrderedCollection)(unsafe.Pointer(&i)), nil
344 case *CollectionPage:
345 return (*OrderedCollection)(unsafe.Pointer(i)), nil
346 case CollectionPage:
347 return (*OrderedCollection)(unsafe.Pointer(&i)), nil
348 default:
349 return reflectItemToType[OrderedCollection](it)
350 }
351 }
352
353 func copyOrderedCollectionToPage(c *OrderedCollection, p *OrderedCollectionPage) error {
354 p.Type = OrderedCollectionPageType
355 p.Name = c.Name
356 p.Content = c.Content
357 p.Summary = c.Summary
358 p.Context = c.Context
359 p.URL = c.URL
360 p.MediaType = c.MediaType
361 p.Generator = c.Generator
362 p.AttributedTo = c.AttributedTo
363 p.Attachment = c.Attachment
364 p.Location = c.Location
365 p.Published = c.Published
366 p.StartTime = c.StartTime
367 p.EndTime = c.EndTime
368 p.Duration = c.Duration
369 p.Icon = c.Icon
370 p.Preview = c.Preview
371 p.Image = c.Image
372 p.Updated = c.Updated
373 p.InReplyTo = c.InReplyTo
374 p.To = c.To
375 p.Audience = c.Audience
376 p.Bto = c.Bto
377 p.CC = c.CC
378 p.BCC = c.BCC
379 p.Replies = c.Replies
380 p.Tag = c.Tag
381 p.TotalItems = c.TotalItems
382 p.OrderedItems = c.OrderedItems
383 p.Current = c.Current
384 p.First = c.First
385 p.PartOf = c.GetLink()
386 return nil
387 }
388
389 // ItemsMatch
390 func (o OrderedCollection) ItemsMatch(col ...Item) bool {
391 for _, it := range col {
392 if match := o.OrderedItems.Contains(it); !match {
393 return false
394 }
395 }
396 return true
397 }
398
399 // Equals verifies if our receiver OrderedCollection is equals with the "with" Item
400 func (o *OrderedCollection) Equals(with Item) bool {
401 if IsNil(with) {
402 return o == nil
403 }
404 if !with.IsCollection() {
405 return false
406 }
407 withCollection, err := ToOrderedCollection(with)
408 if err != nil {
409 return false
410 }
411 return o.equal(*withCollection)
412 }
413
414 // equal verifies if our receiver OrderedCollection is equals with the "with" OrderedCollection
415 func (o OrderedCollection) equal(with OrderedCollection) bool {
416 result := true
417 _ = OnObject(with, func(wo *Object) error {
418 if !wo.Equals(o) {
419 result = false
420 return nil
421 }
422 return nil
423 })
424 if with.TotalItems > 0 {
425 if with.TotalItems != o.TotalItems {
426 result = false
427 }
428 }
429 if with.Current != nil {
430 if !ItemsEqual(o.Current, with.Current) {
431 result = false
432 }
433 }
434 if with.First != nil {
435 if !ItemsEqual(o.First, with.First) {
436 result = false
437 }
438 }
439 if with.Last != nil {
440 if !ItemsEqual(o.Last, with.Last) {
441 result = false
442 }
443 }
444 if with.OrderedItems != nil {
445 if !ItemsEqual(o.OrderedItems, with.OrderedItems) {
446 result = false
447 }
448 }
449 return result
450 }
451
452 func (o OrderedCollection) Format(s fmt.State, verb rune) {
453 switch verb {
454 case 's', 'v':
455 _, _ = fmt.Fprintf(s, "%T[%s] { totalItems: %d }", o, o.GetType(), o.TotalItems)
456 }
457 }
458 func (o *OrderedCollection) Recipients() ItemCollection {
459 aud := o.Audience
460 return ItemCollectionDeduplication(&o.To, &o.CC, &o.Bto, &o.BCC, &aud)
461 }
462
463 func (o *OrderedCollection) Clean() {
464 _ = OnObject(o, func(o *Object) error {
465 o.Clean()
466 return nil
467 })
468 }
469
470 // OnOrderedCollection calls function fn on it Item if it can be asserted
471 // to type *OrderedCollection
472 //
473 // This function should be called if trying to access the Collection specific
474 // properties like "totalItems", "orderedItems", etc. For the other properties
475 // OnObject should be used instead.
476 func OnOrderedCollection(it Item, fn WithOrderedCollectionFn) error {
477 if it == nil {
478 return nil
479 }
480 col, err := ToOrderedCollection(it)
481 if err != nil {
482 return err
483 }
484 return fn(col)
485 }