-
Notifications
You must be signed in to change notification settings - Fork 10
/
copy.go
234 lines (221 loc) · 6.66 KB
/
copy.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package activitypub
import (
"fmt"
)
func CopyOrderedCollectionPageProperties(to, from *OrderedCollectionPage) (*OrderedCollectionPage, error) {
to.PartOf = replaceIfItem(to.PartOf, from.PartOf)
to.Next = replaceIfItem(to.Next, from.Next)
to.Prev = replaceIfItem(to.Prev, from.Prev)
oldCol, _ := ToOrderedCollection(to)
newCol, _ := ToOrderedCollection(from)
_, err := CopyOrderedCollectionProperties(oldCol, newCol)
if err != nil {
return to, err
}
return to, nil
}
func CopyCollectionPageProperties(to, from *CollectionPage) (*CollectionPage, error) {
to.PartOf = replaceIfItem(to.PartOf, from.PartOf)
to.Next = replaceIfItem(to.Next, from.Next)
to.Prev = replaceIfItem(to.Prev, from.Prev)
toCol, _ := ToCollection(to)
fromCol, _ := ToCollection(from)
_, err := CopyCollectionProperties(toCol, fromCol)
return to, err
}
func CopyOrderedCollectionProperties(to, from *OrderedCollection) (*OrderedCollection, error) {
to.First = replaceIfItem(to.First, from.First)
to.Last = replaceIfItem(to.Last, from.Last)
to.OrderedItems = replaceIfItemCollection(to.OrderedItems, from.OrderedItems)
if to.TotalItems == 0 {
to.TotalItems = from.TotalItems
}
oldOb, _ := ToObject(to)
newOb, _ := ToObject(from)
_, err := CopyObjectProperties(oldOb, newOb)
return to, err
}
func CopyCollectionProperties(to, from *Collection) (*Collection, error) {
to.First = replaceIfItem(to.First, from.First)
to.Last = replaceIfItem(to.Last, from.Last)
to.Items = replaceIfItemCollection(to.Items, from.Items)
if to.TotalItems == 0 {
to.TotalItems = from.TotalItems
}
oldOb, _ := ToObject(to)
newOb, _ := ToObject(from)
_, err := CopyObjectProperties(oldOb, newOb)
return to, err
}
// CopyObjectProperties updates the "old" object properties with the "new's"
// Including ID and Type
func CopyObjectProperties(to, from *Object) (*Object, error) {
to.ID = from.ID
to.Type = from.Type
to.Name = replaceIfNaturalLanguageValues(to.Name, from.Name)
to.Attachment = replaceIfItem(to.Attachment, from.Attachment)
to.AttributedTo = replaceIfItem(to.AttributedTo, from.AttributedTo)
to.Audience = replaceIfItemCollection(to.Audience, from.Audience)
to.Content = replaceIfNaturalLanguageValues(to.Content, from.Content)
to.Context = replaceIfItem(to.Context, from.Context)
if len(from.MediaType) > 0 {
to.MediaType = from.MediaType
}
if !from.EndTime.IsZero() {
to.EndTime = from.EndTime
}
to.Generator = replaceIfItem(to.Generator, from.Generator)
to.Icon = replaceIfItem(to.Icon, from.Icon)
to.Image = replaceIfItem(to.Image, from.Image)
to.InReplyTo = replaceIfItem(to.InReplyTo, from.InReplyTo)
to.Location = replaceIfItem(to.Location, from.Location)
to.Preview = replaceIfItem(to.Preview, from.Preview)
if to.Published.IsZero() && !from.Published.IsZero() {
to.Published = from.Published
}
if to.Updated.IsZero() && !from.Updated.IsZero() {
to.Updated = from.Updated
}
to.Replies = replaceIfItem(to.Replies, from.Replies)
if !from.StartTime.IsZero() {
to.StartTime = from.StartTime
}
to.Summary = replaceIfNaturalLanguageValues(to.Summary, from.Summary)
to.Tag = replaceIfItemCollection(to.Tag, from.Tag)
if from.URL != nil {
to.URL = from.URL
}
to.To = replaceIfItemCollection(to.To, from.To)
to.Bto = replaceIfItemCollection(to.Bto, from.Bto)
to.CC = replaceIfItemCollection(to.CC, from.CC)
to.BCC = replaceIfItemCollection(to.BCC, from.BCC)
if from.Duration == 0 {
to.Duration = from.Duration
}
to.Source = replaceIfSource(to.Source, from.Source)
return to, nil
}
func copyAllItemProperties(to, from Item) (Item, error) {
if CollectionType == to.GetType() {
o, err := ToCollection(to)
if err != nil {
return o, err
}
n, err := ToCollection(from)
if err != nil {
return o, err
}
return CopyCollectionProperties(o, n)
}
if CollectionPageType == to.GetType() {
o, err := ToCollectionPage(to)
if err != nil {
return o, err
}
n, err := ToCollectionPage(from)
if err != nil {
return o, err
}
return CopyCollectionPageProperties(o, n)
}
if OrderedCollectionType == to.GetType() {
o, err := ToOrderedCollection(to)
if err != nil {
return o, err
}
n, err := ToOrderedCollection(from)
if err != nil {
return o, err
}
return CopyOrderedCollectionProperties(o, n)
}
if OrderedCollectionPageType == to.GetType() {
o, err := ToOrderedCollectionPage(to)
if err != nil {
return o, err
}
n, err := ToOrderedCollectionPage(from)
if err != nil {
return o, err
}
return CopyOrderedCollectionPageProperties(o, n)
}
if ActorTypes.Contains(to.GetType()) {
o, err := ToActor(to)
if err != nil {
return o, err
}
n, err := ToActor(from)
if err != nil {
return o, err
}
return UpdatePersonProperties(o, n)
}
if ObjectTypes.Contains(to.GetType()) || to.GetType() == "" {
o, err := ToObject(to)
if err != nil {
return o, err
}
n, err := ToObject(from)
if err != nil {
return o, err
}
return CopyObjectProperties(o, n)
}
return to, fmt.Errorf("could not process objects with type %s", to.GetType())
}
// CopyItemProperties delegates to the correct per type functions for copying
// properties between matching Activity Objects
func CopyItemProperties(to, from Item) (Item, error) {
if to == nil {
return to, fmt.Errorf("nil object to update")
}
if from == nil {
return to, fmt.Errorf("nil object for update")
}
if !to.GetLink().Equals(from.GetLink(), false) {
return to, fmt.Errorf("object IDs don't match")
}
if to.GetType() != "" && to.GetType() != from.GetType() {
return to, fmt.Errorf("invalid object types for update %s(old) and %s(new)", from.GetType(), to.GetType())
}
return copyAllItemProperties(to, from)
}
// UpdatePersonProperties
func UpdatePersonProperties(to, from *Actor) (*Actor, error) {
to.Inbox = replaceIfItem(to.Inbox, from.Inbox)
to.Outbox = replaceIfItem(to.Outbox, from.Outbox)
to.Following = replaceIfItem(to.Following, from.Following)
to.Followers = replaceIfItem(to.Followers, from.Followers)
to.Liked = replaceIfItem(to.Liked, from.Liked)
to.PreferredUsername = replaceIfNaturalLanguageValues(to.PreferredUsername, from.PreferredUsername)
oldOb, _ := ToObject(to)
newOb, _ := ToObject(from)
_, err := CopyObjectProperties(oldOb, newOb)
return to, err
}
func replaceIfItem(old, new Item) Item {
if new == nil {
return old
}
return new
}
func replaceIfItemCollection(old, new ItemCollection) ItemCollection {
if new == nil {
return old
}
return new
}
func replaceIfNaturalLanguageValues(old, new NaturalLanguageValues) NaturalLanguageValues {
if new == nil {
return old
}
return new
}
func replaceIfSource(to, from Source) Source {
if from.MediaType != to.MediaType {
return from
}
to.Content = replaceIfNaturalLanguageValues(to.Content, from.Content)
return to
}