| 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 |
type OrderedCollection struct { |
| 16 |
|
| 17 |
ID ID `jsonld:"id,omitempty"` |
| 18 |
|
| 19 |
Type Typer `jsonld:"type,omitempty"` |
| 20 |
|
| 21 |
|
| 22 |
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 23 |
|
| 24 |
|
| 25 |
Attachment Item `jsonld:"attachment,omitempty"` |
| 26 |
|
| 27 |
|
| 28 |
AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 29 |
|
| 30 |
|
| 31 |
Audience ItemCollection `jsonld:"audience,omitempty"` |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
Context Item `jsonld:"context,omitempty"` |
| 42 |
|
| 43 |
|
| 44 |
MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
EndTime time.Time `jsonld:"endTime,omitempty"` |
| 49 |
|
| 50 |
Generator Item `jsonld:"generator,omitempty"` |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
Icon Item `jsonld:"icon,omitempty"` |
| 55 |
|
| 56 |
|
| 57 |
Image Item `jsonld:"image,omitempty"` |
| 58 |
|
| 59 |
InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 60 |
|
| 61 |
Location Item `jsonld:"location,omitempty"` |
| 62 |
|
| 63 |
Preview Item `jsonld:"preview,omitempty"` |
| 64 |
|
| 65 |
Published time.Time `jsonld:"published,omitempty"` |
| 66 |
|
| 67 |
Replies Item `jsonld:"replies,omitempty"` |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
StartTime time.Time `jsonld:"startTime,omitempty"` |
| 72 |
|
| 73 |
|
| 74 |
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Tag ItemCollection `jsonld:"tag,omitempty"` |
| 79 |
|
| 80 |
Updated time.Time `jsonld:"updated,omitempty"` |
| 81 |
|
| 82 |
URL Item `jsonld:"url,omitempty"` |
| 83 |
|
| 84 |
To ItemCollection `jsonld:"to,omitempty"` |
| 85 |
|
| 86 |
Bto ItemCollection `jsonld:"bto,omitempty"` |
| 87 |
|
| 88 |
CC ItemCollection `jsonld:"cc,omitempty"` |
| 89 |
|
| 90 |
BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
Duration time.Duration `jsonld:"duration,omitempty"` |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Likes Item `jsonld:"likes,omitempty"` |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
Shares Item `jsonld:"shares,omitempty"` |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
Source Source `jsonld:"source,omitempty"` |
| 108 |
|
| 109 |
Current ObjectOrLink `jsonld:"current,omitempty"` |
| 110 |
|
| 111 |
First ObjectOrLink `jsonld:"first,omitempty"` |
| 112 |
|
| 113 |
Last ObjectOrLink `jsonld:"last,omitempty"` |
| 114 |
|
| 115 |
|
| 116 |
TotalItems uint `jsonld:"totalItems"` |
| 117 |
|
| 118 |
OrderedItems ItemCollection `jsonld:"orderedItems,omitempty"` |
| 119 |
} |
| 120 |
|
| 121 |
type ( |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
InboxStream = OrderedCollection |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
LikedCollection = OrderedCollection |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
LikesCollection = OrderedCollection |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
OutboxStream = OrderedCollection |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
SharesCollection = OrderedCollection |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
func (o OrderedCollection) GetType() Typer { |
| 155 |
return o.Type |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (o OrderedCollection) IsLink() bool { |
| 160 |
return false |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
func (o OrderedCollection) GetID() ID { |
| 165 |
return o.ID |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
func (o OrderedCollection) GetLink() IRI { |
| 170 |
return IRI(o.ID) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
func (o OrderedCollection) Match(tt ...ActivityVocabularyType) bool { |
| 175 |
return ActivityVocabularyTypes(tt).Match(o.Type) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func (o OrderedCollection) IsObject() bool { |
| 180 |
return true |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
func (o OrderedCollection) Collection() ItemCollection { |
| 185 |
return o.OrderedItems |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
func (o OrderedCollection) IsCollection() bool { |
| 190 |
return true |
| 191 |
} |
| 192 |
|
| 193 |
|
| 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 |
|
| 207 |
func (o *OrderedCollection) Count() uint { |
| 208 |
if o == nil { |
| 209 |
return 0 |
| 210 |
} |
| 211 |
return uint(len(o.OrderedItems)) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 276 |
func (o *OrderedCollection) UnmarshalBinary(data []byte) error { |
| 277 |
return o.GobDecode(data) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
func (o OrderedCollection) MarshalBinary() ([]byte, error) { |
| 282 |
return o.GobEncode() |
| 283 |
} |
| 284 |
|
| 285 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 339 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 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 |
} |