| 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 |
const CollectionOfIRIs ActivityVocabularyType = "IRICollection" |
| 14 |
const CollectionOfItems ActivityVocabularyType = "ItemCollection" |
| 15 |
|
| 16 |
var CollectionTypes = ActivityVocabularyTypes{ |
| 17 |
CollectionOfItems, |
| 18 |
CollectionType, |
| 19 |
OrderedCollectionType, |
| 20 |
CollectionPageType, |
| 21 |
OrderedCollectionPageType, |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
type Collections interface { |
| 43 |
Collection | CollectionPage | OrderedCollection | OrderedCollectionPage | ItemCollection | IRIs |
| 44 |
} |
| 45 |
|
| 46 |
type CollectionInterface interface { |
| 47 |
ObjectOrLink |
| 48 |
Collection() ItemCollection |
| 49 |
Append(ob ...Item) error |
| 50 |
Remove(ob ...Item) |
| 51 |
Count() uint |
| 52 |
Contains(Item) bool |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
type Collection struct { |
| 57 |
|
| 58 |
ID ID `jsonld:"id,omitempty"` |
| 59 |
|
| 60 |
Type Typer `jsonld:"type,omitempty"` |
| 61 |
|
| 62 |
|
| 63 |
Name NaturalLanguageValues `jsonld:"name,omitempty,collapsible"` |
| 64 |
|
| 65 |
|
| 66 |
Attachment Item `jsonld:"attachment,omitempty"` |
| 67 |
|
| 68 |
|
| 69 |
AttributedTo Item `jsonld:"attributedTo,omitempty"` |
| 70 |
|
| 71 |
|
| 72 |
Audience ItemCollection `jsonld:"audience,omitempty"` |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
Content NaturalLanguageValues `jsonld:"content,omitempty,collapsible"` |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
Context Item `jsonld:"context,omitempty"` |
| 83 |
|
| 84 |
|
| 85 |
MediaType MimeType `jsonld:"mediaType,omitempty"` |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
EndTime time.Time `jsonld:"endTime,omitempty"` |
| 90 |
|
| 91 |
Generator Item `jsonld:"generator,omitempty"` |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
Icon Item `jsonld:"icon,omitempty"` |
| 96 |
|
| 97 |
|
| 98 |
Image Item `jsonld:"image,omitempty"` |
| 99 |
|
| 100 |
InReplyTo Item `jsonld:"inReplyTo,omitempty"` |
| 101 |
|
| 102 |
Location Item `jsonld:"location,omitempty"` |
| 103 |
|
| 104 |
Preview Item `jsonld:"preview,omitempty"` |
| 105 |
|
| 106 |
Published time.Time `jsonld:"published,omitempty"` |
| 107 |
|
| 108 |
Replies Item `jsonld:"replies,omitempty"` |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
StartTime time.Time `jsonld:"startTime,omitempty"` |
| 113 |
|
| 114 |
|
| 115 |
Summary NaturalLanguageValues `jsonld:"summary,omitempty,collapsible"` |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
Tag ItemCollection `jsonld:"tag,omitempty"` |
| 120 |
|
| 121 |
Updated time.Time `jsonld:"updated,omitempty"` |
| 122 |
|
| 123 |
URL Item `jsonld:"url,omitempty"` |
| 124 |
|
| 125 |
To ItemCollection `jsonld:"to,omitempty"` |
| 126 |
|
| 127 |
Bto ItemCollection `jsonld:"bto,omitempty"` |
| 128 |
|
| 129 |
CC ItemCollection `jsonld:"cc,omitempty"` |
| 130 |
|
| 131 |
BCC ItemCollection `jsonld:"bcc,omitempty"` |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
Duration time.Duration `jsonld:"duration,omitempty"` |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
Likes Item `jsonld:"likes,omitempty"` |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
Shares Item `jsonld:"shares,omitempty"` |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
Source Source `jsonld:"source,omitempty"` |
| 149 |
|
| 150 |
Current ObjectOrLink `jsonld:"current,omitempty"` |
| 151 |
|
| 152 |
First ObjectOrLink `jsonld:"first,omitempty"` |
| 153 |
|
| 154 |
Last ObjectOrLink `jsonld:"last,omitempty"` |
| 155 |
|
| 156 |
|
| 157 |
TotalItems uint `jsonld:"totalItems"` |
| 158 |
|
| 159 |
Items ItemCollection `jsonld:"items,omitempty"` |
| 160 |
} |
| 161 |
|
| 162 |
type ( |
| 163 |
|
| 164 |
FollowersCollection = Collection |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
FollowingCollection = Collection |
| 170 |
) |
| 171 |
|
| 172 |
|
| 173 |
func CollectionNew(id ID) *Collection { |
| 174 |
c := Collection{ID: id, Type: CollectionType} |
| 175 |
c.Name = NaturalLanguageValuesNew() |
| 176 |
c.Content = NaturalLanguageValuesNew() |
| 177 |
c.Summary = NaturalLanguageValuesNew() |
| 178 |
return &c |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
func OrderedCollectionNew(id ID) *OrderedCollection { |
| 183 |
o := OrderedCollection{ID: id, Type: OrderedCollectionType} |
| 184 |
o.Name = NaturalLanguageValuesNew() |
| 185 |
o.Content = NaturalLanguageValuesNew() |
| 186 |
|
| 187 |
return &o |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
func (c Collection) GetID() ID { |
| 192 |
return c.ID |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func (c Collection) GetType() Typer { |
| 197 |
return c.Type |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
func (c Collection) IsLink() bool { |
| 202 |
return false |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
func (c Collection) IsObject() bool { |
| 207 |
return true |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
func (c Collection) IsCollection() bool { |
| 212 |
return true |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
func (c Collection) GetLink() IRI { |
| 217 |
return IRI(c.ID) |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
func (c Collection) Match(tt ...ActivityVocabularyType) bool { |
| 222 |
return ActivityVocabularyTypes(tt).Match(c.Type) |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
func (c Collection) Collection() ItemCollection { |
| 227 |
return c.Items |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
func (c *Collection) Append(it ...Item) error { |
| 232 |
for _, ob := range it { |
| 233 |
if !c.Items.Contains(ob) { |
| 234 |
c.Items = append(c.Items, ob) |
| 235 |
c.TotalItems += 1 |
| 236 |
} |
| 237 |
} |
| 238 |
return nil |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
func (c *Collection) Remove(it ...Item) { |
| 243 |
for _, ob := range it { |
| 244 |
if c.Items.Contains(ob) { |
| 245 |
c.Items.Remove(ob) |
| 246 |
c.TotalItems -= 1 |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
func (c *Collection) Count() uint { |
| 253 |
if c == nil { |
| 254 |
return 0 |
| 255 |
} |
| 256 |
return uint(len(c.Items)) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
func (c Collection) Contains(r Item) bool { |
| 261 |
if len(c.Items) == 0 { |
| 262 |
return false |
| 263 |
} |
| 264 |
for _, it := range c.Items { |
| 265 |
if ItemsEqual(it, r) { |
| 266 |
return true |
| 267 |
} |
| 268 |
} |
| 269 |
return false |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
func (c *Collection) UnmarshalJSON(data []byte) error { |
| 274 |
par := fastjson.Parser{} |
| 275 |
val, err := par.ParseBytes(data) |
| 276 |
if err != nil { |
| 277 |
return err |
| 278 |
} |
| 279 |
return JSONLoadCollection(val, c) |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
func (c Collection) MarshalJSON() ([]byte, error) { |
| 284 |
b := make([]byte, 0) |
| 285 |
notEmpty := false |
| 286 |
JSONWrite(&b, '{') |
| 287 |
|
| 288 |
OnObject(c, func(o *Object) error { |
| 289 |
notEmpty = JSONWriteObjectValue(&b, *o) |
| 290 |
return nil |
| 291 |
}) |
| 292 |
if c.Current != nil { |
| 293 |
notEmpty = JSONWriteItemProp(&b, "current", c.Current) || notEmpty |
| 294 |
} |
| 295 |
if c.First != nil { |
| 296 |
notEmpty = JSONWriteItemProp(&b, "first", c.First) || notEmpty |
| 297 |
} |
| 298 |
if c.Last != nil { |
| 299 |
notEmpty = JSONWriteItemProp(&b, "last", c.Last) || notEmpty |
| 300 |
} |
| 301 |
notEmpty = JSONWriteIntProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty |
| 302 |
if c.Items != nil { |
| 303 |
notEmpty = JSONWriteItemCollectionProp(&b, "items", c.Items, false) || notEmpty |
| 304 |
} |
| 305 |
if notEmpty { |
| 306 |
JSONWrite(&b, '}') |
| 307 |
return b, nil |
| 308 |
} |
| 309 |
return nil, nil |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
func (c *Collection) UnmarshalBinary(data []byte) error { |
| 314 |
return c.GobDecode(data) |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
func (c Collection) MarshalBinary() ([]byte, error) { |
| 319 |
return c.GobEncode() |
| 320 |
} |
| 321 |
|
| 322 |
func (c Collection) GobEncode() ([]byte, error) { |
| 323 |
mm := make(map[string][]byte) |
| 324 |
hasData, err := mapCollectionProperties(mm, c) |
| 325 |
if err != nil { |
| 326 |
return nil, err |
| 327 |
} |
| 328 |
if !hasData { |
| 329 |
return []byte{}, nil |
| 330 |
} |
| 331 |
bb := bytes.Buffer{} |
| 332 |
g := gob.NewEncoder(&bb) |
| 333 |
if err := g.Encode(mm); err != nil { |
| 334 |
return nil, err |
| 335 |
} |
| 336 |
return bb.Bytes(), nil |
| 337 |
} |
| 338 |
|
| 339 |
func (c *Collection) GobDecode(data []byte) error { |
| 340 |
if len(data) == 0 { |
| 341 |
return nil |
| 342 |
} |
| 343 |
mm, err := gobDecodeObjectAsMap(data) |
| 344 |
if err != nil { |
| 345 |
return err |
| 346 |
} |
| 347 |
return unmapCollectionProperties(mm, c) |
| 348 |
} |
| 349 |
|
| 350 |
func (c Collection) Format(s fmt.State, verb rune) { |
| 351 |
switch verb { |
| 352 |
case 's', 'v': |
| 353 |
_, _ = fmt.Fprintf(s, "%T[%s] { totalItems: %d }", c, c.Type, c.TotalItems) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
func ToCollection(it LinkOrIRI) (*Collection, error) { |
| 359 |
switch i := it.(type) { |
| 360 |
case *Collection: |
| 361 |
return i, nil |
| 362 |
case Collection: |
| 363 |
return &i, nil |
| 364 |
case *CollectionPage: |
| 365 |
return (*Collection)(unsafe.Pointer(i)), nil |
| 366 |
case CollectionPage: |
| 367 |
return (*Collection)(unsafe.Pointer(&i)), nil |
| 368 |
|
| 369 |
|
| 370 |
case *OrderedCollection: |
| 371 |
return (*Collection)(unsafe.Pointer(i)), nil |
| 372 |
case OrderedCollection: |
| 373 |
return (*Collection)(unsafe.Pointer(&i)), nil |
| 374 |
case *OrderedCollectionPage: |
| 375 |
return (*Collection)(unsafe.Pointer(i)), nil |
| 376 |
case OrderedCollectionPage: |
| 377 |
return (*Collection)(unsafe.Pointer(&i)), nil |
| 378 |
default: |
| 379 |
return reflectItemToType[Collection](it) |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
func (c Collection) ItemsMatch(col ...Item) bool { |
| 385 |
for _, it := range col { |
| 386 |
if match := c.Items.Contains(it); !match { |
| 387 |
return false |
| 388 |
} |
| 389 |
} |
| 390 |
return true |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
func (c *Collection) Equals(with Item) bool { |
| 395 |
if IsNil(with) { |
| 396 |
return c == nil |
| 397 |
} |
| 398 |
if !with.IsCollection() { |
| 399 |
return false |
| 400 |
} |
| 401 |
withCollection, err := ToCollection(with) |
| 402 |
if err != nil { |
| 403 |
return false |
| 404 |
} |
| 405 |
return c.equal(*withCollection) |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
func (c Collection) equal(with Collection) bool { |
| 410 |
result := true |
| 411 |
_ = OnObject(with, func(wo *Object) error { |
| 412 |
if !wo.Equals(c) { |
| 413 |
result = false |
| 414 |
return nil |
| 415 |
} |
| 416 |
return nil |
| 417 |
}) |
| 418 |
if with.TotalItems > 0 { |
| 419 |
if with.TotalItems != c.TotalItems { |
| 420 |
result = false |
| 421 |
} |
| 422 |
} |
| 423 |
if with.Current != nil { |
| 424 |
if !ItemsEqual(c.Current, with.Current) { |
| 425 |
result = false |
| 426 |
} |
| 427 |
} |
| 428 |
if with.First != nil { |
| 429 |
if !ItemsEqual(c.First, with.First) { |
| 430 |
result = false |
| 431 |
} |
| 432 |
} |
| 433 |
if with.Last != nil { |
| 434 |
if !ItemsEqual(c.Last, with.Last) { |
| 435 |
result = false |
| 436 |
} |
| 437 |
} |
| 438 |
if with.Items != nil { |
| 439 |
if !ItemsEqual(c.Items, with.Items) { |
| 440 |
result = false |
| 441 |
} |
| 442 |
} |
| 443 |
return result |
| 444 |
} |
| 445 |
|
| 446 |
func (c *Collection) Recipients() ItemCollection { |
| 447 |
aud := c.Audience |
| 448 |
return ItemCollectionDeduplication(&c.To, &c.CC, &c.Bto, &c.BCC, &aud) |
| 449 |
} |
| 450 |
|
| 451 |
func (c *Collection) Clean() { |
| 452 |
_ = OnObject(c, func(o *Object) error { |
| 453 |
o.Clean() |
| 454 |
return nil |
| 455 |
}) |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
func OnCollection(it Item, fn WithCollectionFn) error { |
| 464 |
if it == nil { |
| 465 |
return nil |
| 466 |
} |
| 467 |
col, err := ToCollection(it) |
| 468 |
if err != nil { |
| 469 |
return err |
| 470 |
} |
| 471 |
return fn(col) |
| 472 |
} |