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