Debian Codegraph @

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