| 1 |
package activitypub |
| 2 |
|
| 3 |
|
| 4 |
func FlattenActivityProperties(act *Activity) *Activity { |
| 5 |
if act == nil { |
| 6 |
return nil |
| 7 |
} |
| 8 |
_ = OnIntransitiveActivity(act, func(in *IntransitiveActivity) error { |
| 9 |
FlattenIntransitiveActivityProperties(in) |
| 10 |
return nil |
| 11 |
}) |
| 12 |
act.Object = FlattenToIRI(act.Object) |
| 13 |
return act |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
func FlattenIntransitiveActivityProperties(act *IntransitiveActivity) *IntransitiveActivity { |
| 18 |
if act == nil { |
| 19 |
return nil |
| 20 |
} |
| 21 |
act.Actor = FlattenToIRI(act.Actor) |
| 22 |
act.Target = FlattenToIRI(act.Target) |
| 23 |
act.Result = FlattenToIRI(act.Result) |
| 24 |
act.Origin = FlattenToIRI(act.Origin) |
| 25 |
act.Result = FlattenToIRI(act.Result) |
| 26 |
act.Instrument = FlattenToIRI(act.Instrument) |
| 27 |
_ = OnObject(act, func(o *Object) error { |
| 28 |
FlattenObjectProperties(o) |
| 29 |
return nil |
| 30 |
}) |
| 31 |
return act |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func FlattenItemCollection(col ItemCollection) ItemCollection { |
| 36 |
if col == nil { |
| 37 |
return col |
| 38 |
} |
| 39 |
for k, it := range ItemCollectionDeduplication(&col) { |
| 40 |
if iri := it.GetLink(); iri != "" { |
| 41 |
col[k] = iri |
| 42 |
} |
| 43 |
} |
| 44 |
return col |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func FlattenCollection(col *Collection) *Collection { |
| 49 |
if col == nil { |
| 50 |
return col |
| 51 |
} |
| 52 |
col.Items = FlattenItemCollection(col.Items) |
| 53 |
|
| 54 |
return col |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func FlattenOrderedCollection(col *OrderedCollection) *OrderedCollection { |
| 59 |
if col == nil { |
| 60 |
return col |
| 61 |
} |
| 62 |
col.OrderedItems = FlattenItemCollection(col.OrderedItems) |
| 63 |
|
| 64 |
return col |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func FlattenActorProperties(a *Actor) *Actor { |
| 69 |
if a == nil { |
| 70 |
return nil |
| 71 |
} |
| 72 |
OnObject(a, func(o *Object) error { |
| 73 |
FlattenObjectProperties(o) |
| 74 |
return nil |
| 75 |
}) |
| 76 |
return a |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func FlattenObjectProperties(o *Object) *Object { |
| 81 |
if o == nil { |
| 82 |
return nil |
| 83 |
} |
| 84 |
o.Replies = Flatten(o.Replies) |
| 85 |
o.Shares = Flatten(o.Shares) |
| 86 |
o.Likes = Flatten(o.Likes) |
| 87 |
o.AttributedTo = Flatten(o.AttributedTo) |
| 88 |
o.To = FlattenItemCollection(o.To) |
| 89 |
o.Bto = FlattenItemCollection(o.Bto) |
| 90 |
o.CC = FlattenItemCollection(o.CC) |
| 91 |
o.BCC = FlattenItemCollection(o.BCC) |
| 92 |
o.Audience = FlattenItemCollection(o.Audience) |
| 93 |
|
| 94 |
return o |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func FlattenProperties(it Item) Item { |
| 99 |
if IsNil(it) { |
| 100 |
return nil |
| 101 |
} |
| 102 |
typ := it.GetType() |
| 103 |
if IntransitiveActivityTypes.Match(typ) { |
| 104 |
_ = OnIntransitiveActivity(it, func(a *IntransitiveActivity) error { |
| 105 |
FlattenIntransitiveActivityProperties(a) |
| 106 |
return nil |
| 107 |
}) |
| 108 |
} else if ActivityTypes.Match(typ) { |
| 109 |
_ = OnActivity(it, func(a *Activity) error { |
| 110 |
FlattenActivityProperties(a) |
| 111 |
return nil |
| 112 |
}) |
| 113 |
} |
| 114 |
if ActorTypes.Match(typ) { |
| 115 |
_ = OnActor(it, func(a *Actor) error { |
| 116 |
FlattenActorProperties(a) |
| 117 |
return nil |
| 118 |
}) |
| 119 |
} |
| 120 |
if ObjectTypes.Match(typ) { |
| 121 |
_ = OnObject(it, func(o *Object) error { |
| 122 |
FlattenObjectProperties(o) |
| 123 |
return nil |
| 124 |
}) |
| 125 |
} |
| 126 |
return it |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
func Flatten(it Item) Item { |
| 131 |
if IsNil(it) { |
| 132 |
return nil |
| 133 |
} |
| 134 |
if it.IsCollection() { |
| 135 |
_ = OnCollectionIntf(it, func(c CollectionInterface) error { |
| 136 |
it = FlattenItemCollection(c.Collection()).Normalize() |
| 137 |
return nil |
| 138 |
}) |
| 139 |
return it |
| 140 |
} |
| 141 |
return it.GetLink() |
| 142 |
} |