| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
type CollectionPage struct { |
| 17 |
|
| 18 |
ID ID `jsonld:"id,omitempty"` |
| 19 |
|
| 20 |
Type Typer `jsonld:"type,omitempty"` |
| 21 |
|
| 22 |
|
| 23 |
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 24 |
|
| 25 |
|
| 26 |
Attachment Item `jsonld:"attachment,omitempty"` |
| 27 |
|
| 28 |
|
| 29 |
AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 30 |
|
| 31 |
|
| 32 |
Audience ItemCollection `jsonld:"audience,omitempty"` |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
Context Item `jsonld:"context,omitempty"` |
| 43 |
|
| 44 |
|
| 45 |
MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
EndTime time.Time `jsonld:"endTime,omitempty"` |
| 50 |
|
| 51 |
Generator Item `jsonld:"generator,omitempty"` |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
Icon Item `jsonld:"icon,omitempty"` |
| 56 |
|
| 57 |
|
| 58 |
Image Item `jsonld:"image,omitempty"` |
| 59 |
|
| 60 |
InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 61 |
|
| 62 |
Location Item `jsonld:"location,omitempty"` |
| 63 |
|
| 64 |
Preview Item `jsonld:"preview,omitempty"` |
| 65 |
|
| 66 |
Published time.Time `jsonld:"published,omitempty"` |
| 67 |
|
| 68 |
Replies Item `jsonld:"replies,omitempty"` |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
StartTime time.Time `jsonld:"startTime,omitempty"` |
| 73 |
|
| 74 |
|
| 75 |
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
Tag ItemCollection `jsonld:"tag,omitempty"` |
| 80 |
|
| 81 |
Updated time.Time `jsonld:"updated,omitempty"` |
| 82 |
|
| 83 |
URL Item `jsonld:"url,omitempty"` |
| 84 |
|
| 85 |
To ItemCollection `jsonld:"to,omitempty"` |
| 86 |
|
| 87 |
Bto ItemCollection `jsonld:"bto,omitempty"` |
| 88 |
|
| 89 |
CC ItemCollection `jsonld:"cc,omitempty"` |
| 90 |
|
| 91 |
BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
Duration time.Duration `jsonld:"duration,omitempty"` |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Likes Item `jsonld:"likes,omitempty"` |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
Shares Item `jsonld:"shares,omitempty"` |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
Source Source `jsonld:"source,omitempty"` |
| 109 |
|
| 110 |
Current ObjectOrLink `jsonld:"current,omitempty"` |
| 111 |
|
| 112 |
First ObjectOrLink `jsonld:"first,omitempty"` |
| 113 |
|
| 114 |
Last ObjectOrLink `jsonld:"last,omitempty"` |
| 115 |
|
| 116 |
|
| 117 |
TotalItems uint `jsonld:"totalItems"` |
| 118 |
|
| 119 |
Items ItemCollection `jsonld:"items,omitempty"` |
| 120 |
|
| 121 |
PartOf Item `jsonld:"partOf,omitempty"` |
| 122 |
|
| 123 |
Next Item `jsonld:"next,omitempty"` |
| 124 |
|
| 125 |
Prev Item `jsonld:"prev,omitempty"` |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (c CollectionPage) GetID() ID { |
| 130 |
return c.ID |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (c CollectionPage) GetType() Typer { |
| 135 |
return c.Type |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (c CollectionPage) IsLink() bool { |
| 140 |
return false |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (c CollectionPage) IsObject() bool { |
| 145 |
return true |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
func (c CollectionPage) IsCollection() bool { |
| 150 |
return true |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (c CollectionPage) GetLink() IRI { |
| 155 |
return IRI(c.ID) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (c CollectionPage) Match(tt ...ActivityVocabularyType) bool { |
| 160 |
return ActivityVocabularyTypes(tt).Match(c.Type) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (c CollectionPage) Collection() ItemCollection { |
| 165 |
return c.Items |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func (c *CollectionPage) Count() uint { |
| 170 |
if c == nil { |
| 171 |
return 0 |
| 172 |
} |
| 173 |
return uint(len(c.Items)) |
| 174 |
} |
| 175 |
|
| 176 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 260 |
func (c *CollectionPage) UnmarshalBinary(data []byte) error { |
| 261 |
return c.GobDecode(data) |
| 262 |
} |
| 263 |
|
| 264 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 353 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 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 |
} |