/
asvocab_base_types.go
252 lines (224 loc) · 11.1 KB
/
asvocab_base_types.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package astreams
import (
"sort"
"time"
)
// ObjectOrLink is a wrapper type that represents any valid ActivityStreams 2.0 object or link
// Single objects are still presented as slices, but with a single element
type ObjectOrLink []ObjectLinker
// ObjectOrLinkOrString is a type that can either represent simple string URL(s) or an Object/Link slice
type ObjectOrLinkOrString struct {
URL []string
Target ObjectOrLink
}
// StringWithOrderedCollection can store a string URL pointing to an OrderedCollection, which can itself be stored in the struct
type StringWithOrderedCollection struct {
URL string
OrdCollection OrderedCollection
}
// StringWithCollection can store a string URL pointing to a Collection, which can itself be stored in the struct
type StringWithCollection struct {
URL string
Collection Collection
}
// Icons is a type representing the possible ActivityPub icon values
type Icons struct {
URL string
Icons []Icon
}
type EndpointsOrString struct {
URL string
Endpoints Endpoint
}
// https://www.w3.org/TR/activitystreams-vocabulary
// Object represents the base ActivityStreams Object and all of its properties
// Most of the other types extend Object
type Object struct {
ASContext *ObjectOrLinkOrString `json:"@context,omitempty"`
ASLanguage string `json:"@language,omitempty"`
Schema string `json:"schema,omitempty"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
AtType string `json:"@type,omitempty"`
Summary string `json:"summary,omitempty"`
SummaryMap map[string]string `json:"summaryMap,omitempty"`
AtID string `json:"@id,omitempty"`
Name string `json:"name,omitempty"`
NameMap map[string]string `json:"nameMap,omitempty"`
AlsoKnownAs *ObjectOrLinkOrString `json:"alsoKnownAs,omitempty"`
MovedTo *ObjectOrLinkOrString `json:"movedTo,omitempty"`
Attachment *ObjectOrLinkOrString `json:"attachment,omitempty"`
AttributedTo *ObjectOrLinkOrString `json:"attributedTo,omitempty"`
Audience *ObjectOrLinkOrString `json:"audience,omitempty"`
Content string `json:"content,omitempty"` // needs to be parsed safely ie by https://golang.org/pkg/html/template
ContentMap map[string]string `json:"contentMap,omitempty"`
Source *ObjectOrLinkOrString `json:"source,omitempty"`
Context *ObjectOrLinkOrString `json:"context,omitempty"`
StartTime *time.Time `json:"startTime,omitempty"`
EndTime *time.Time `json:"endTime,omitempty"`
Generator *ObjectOrLinkOrString `json:"generator,omitempty"`
Featured *ObjectOrLinkOrString `json:"featured,omitempty"`
Likes *ObjectOrLinkOrString `json:"likes,omitempty"`
Shares *ObjectOrLinkOrString `json:"shares,omitempty"`
Icon *ObjectOrLinkOrString `json:"icon,omitempty"`
Image *ObjectOrLinkOrString `json:"image,omitempty"`
InReplyTo *ObjectOrLinkOrString `json:"inReplyTo,omitempty"`
Location *ObjectOrLinkOrString `json:"location,omitempty"`
Preview *ObjectOrLinkOrString `json:"preview,omitempty"`
Published *time.Time `json:"published,omitempty"`
Replies *OrderedCollection `json:"replies,omitempty"`
Tag *ObjectOrLinkOrString `json:"tag,omitempty"`
Updated *time.Time `json:"updated,omitempty"`
URL *ObjectOrLinkOrString `json:"url,omitempty"`
To *ObjectOrLinkOrString `json:"to,omitempty"`
Bto *ObjectOrLinkOrString `json:"bto,omitempty"`
Cc *ObjectOrLinkOrString `json:"cc,omitempty"`
Bcc *ObjectOrLinkOrString `json:"bcc,omitempty"`
MediaType string `json:"mediaType,omitempty"`
Duration string `json:"duration,omitempty"` // xsd:duration
}
// Link represents the base ActivityStreams Link and all of its properties
// Other Link types extend it
type Link struct {
ASContext *ObjectOrLinkOrString `json:"@context,omitempty"`
ASLanguage string `json:"@language,omitempty"`
Type string `json:"type"`
Href string `json:"href,omitempty"`
Hreflang string `json:"hreflang,omitempty"`
Rel []string `json:"rel,omitempty"`
MediaType string `json:"mediaType,omitempty"`
Height int `json:"height,omitempty"`
Width int `json:"width,omitempty"`
Name string `json:"name,omitempty"`
NameMap map[string]string `json:"nameMap,omitempty"`
Value string `json:"value,omitempty"`
Preview *ObjectOrLinkOrString `json:"preview,omitempty"`
Published *time.Time `json:"published,omitempty"`
}
type Location struct {
Object
Longitude float32 `json:"longitude,omitempty"`
Latitude float32 `json:"latitude,omitempty"`
Altitude int `json:"altitude,omitempty"`
Units string `json:"units,omitempty"`
}
type Endpoint struct {
ProxyUrl string `json:"proxyUrl,omitempty"`
OauthAuthorizationEndpoint string `json:"oauthAuthorizationEndpoint,omitempty"`
OauthTokenEndpoint string `json:"oauthTokenEndpoint,omitempty"`
ProvideClientKey string `json:"provideClientKey,omitempty"`
SignClientKey string `json:"signClientKey,omitempty"`
SharedInbox *StringWithOrderedCollection `json:"sharedInbox,omitempty"`
}
type Icon struct {
Object
Width int `json:"width"`
Height int `json:"height"`
}
type Actor struct {
Object
PublicKey *PublicKey `json:"publicKey,omitempty"`
Inbox *StringWithOrderedCollection `json:"inbox,omitempty"`
Outbox *StringWithOrderedCollection `json:"outbox,omitempty"`
Followers *StringWithOrderedCollection `json:"followers,omitempty"`
Following *StringWithOrderedCollection `json:"following,omitempty"`
Liked *StringWithOrderedCollection `json:"liked,omitempty"`
Streams *ObjectOrLinkOrString `json:"streams,omitempty"`
PreferredUsername string `json:"preferredUsername,omitempty"`
ManuallyApprovesFollowers bool `json:"manuallyApprovesFollowers,omitempty"`
EndpointsOrURI *EndpointsOrString `json:"endpoints,omitempty"`
}
type Activity struct {
Object
Actor *ObjectOrLinkOrString `json:"actor,omitempty"`
ActivityObject *ObjectOrLinkOrString `json:"object,omitempty"` // the 'object' property of Activity
Target *ObjectOrLinkOrString `json:"target,omitempty"`
Result *ObjectOrLinkOrString `json:"result,omitempty"`
Origin *ObjectOrLinkOrString `json:"origin,omitempty"`
Instrument *ObjectOrLinkOrString `json:"instrument,omitempty"`
}
type IntransitiveActivity struct {
Object
Actor *ObjectOrLinkOrString `json:"actor,omitempty"`
Target *ObjectOrLinkOrString `json:"target,omitempty"`
Result *ObjectOrLinkOrString `json:"result,omitempty"`
Origin *ObjectOrLinkOrString `json:"origin,omitempty"`
Instrument *ObjectOrLinkOrString `json:"instrument,omitempty"`
}
// CollectionPage is provided for spec compliance, prefer OrderedCollectionPage
type CollectionPage struct {
Collection
PartOf *ObjectOrLinkOrString `json:"partOf,omitempty"`
Next *ObjectOrLinkOrString `json:"next,omitempty"`
Prev *ObjectOrLinkOrString `json:"prev,omitempty"`
}
// Collection is provided for spec compliance, prefer OrderedCollection
type Collection struct {
Object
TotalItems int `json:"totalItems,omitempty"`
Current *ObjectOrLinkOrString `json:"current,omitempty"`
First *ObjectOrLinkOrString `json:"first,omitempty"`
Last *ObjectOrLinkOrString `json:"last,omitempty"`
Items *ObjectOrLinkOrString `json:"items,omitempty"`
}
// OrderedCollectionPage implements https://golang.org/pkg/sort/#Interface
type OrderedCollectionPage struct {
OrderedCollection
PartOf *ObjectOrLinkOrString `json:"partOf,omitempty"`
Next *ObjectOrLinkOrString `json:"next,omitempty"`
Prev *ObjectOrLinkOrString `json:"prev,omitempty"`
StartIndex uint `json:"startIndex,omitempty"`
}
// OrderedCollection implements https://golang.org/pkg/sort/#Interface
type OrderedCollection struct {
Object
TotalItems int `json:"totalItems"`
Current *ObjectOrLinkOrString `json:"current,omitempty"`
First *ObjectOrLinkOrString `json:"first,omitempty"`
Last *ObjectOrLinkOrString `json:"last,omitempty"`
OrderedItems *ObjectOrLinkOrString `json:"orderedItems"`
}
func (oc *OrderedCollection) Len() int {
if len(oc.OrderedItems.URL) > 0 {
return len(oc.OrderedItems.URL)
}
return len(oc.OrderedItems.Target)
}
func (oc *OrderedCollection) Less(i, j int) bool {
if len(oc.OrderedItems.Target) > 0 {
if oc.OrderedItems.Target[i].IsObject() && oc.OrderedItems.Target[j].IsObject() {
return oc.OrderedItems.Target[i].GetObject().Published.Before(*oc.OrderedItems.Target[j].GetObject().Published)
} else if oc.OrderedItems.Target[i].IsObject() && oc.OrderedItems.Target[j].IsLink() {
return oc.OrderedItems.Target[i].GetObject().Published.Before(*oc.OrderedItems.Target[j].GetLink().Published)
} else if oc.OrderedItems.Target[i].IsLink() && oc.OrderedItems.Target[j].IsLink() {
return oc.OrderedItems.Target[i].GetLink().Published.Before(*oc.OrderedItems.Target[j].GetLink().Published)
} else if oc.OrderedItems.Target[i].IsLink() && oc.OrderedItems.Target[j].IsObject() {
return oc.OrderedItems.Target[i].GetLink().Published.Before(*oc.OrderedItems.Target[j].GetObject().Published)
}
}
return false
}
func (oc *OrderedCollection) Swap(i, j int) {
if len(oc.OrderedItems.URL) > 0 {
oc.OrderedItems.URL[i], oc.OrderedItems.URL[j] = oc.OrderedItems.URL[j], oc.OrderedItems.URL[i]
} else {
oc.OrderedItems.Target[i], oc.OrderedItems.Target[j] = oc.OrderedItems.Target[j], oc.OrderedItems.Target[i]
}
}
// SortByUpdated sorts OrderedCollection objects by Updated rather than Published date
func (oc *OrderedCollection) SortByUpdated() {
sort.Slice(oc.OrderedItems, func(i, j int) bool {
if len(oc.OrderedItems.Target) > 0 {
if oc.OrderedItems.Target[i].IsObject() && oc.OrderedItems.Target[j].IsObject() {
return oc.OrderedItems.Target[i].GetObject().Updated.Before(*oc.OrderedItems.Target[j].GetObject().Updated)
} else if oc.OrderedItems.Target[i].IsObject() && oc.OrderedItems.Target[j].IsLink() {
return oc.OrderedItems.Target[i].GetObject().Updated.Before(*oc.OrderedItems.Target[j].GetLink().Published)
} else if oc.OrderedItems.Target[i].IsLink() && oc.OrderedItems.Target[j].IsLink() {
return oc.OrderedItems.Target[i].GetLink().Published.Before(*oc.OrderedItems.Target[j].GetLink().Published)
} else if oc.OrderedItems.Target[i].IsLink() && oc.OrderedItems.Target[j].IsObject() {
return oc.OrderedItems.Target[i].GetLink().Published.Before(*oc.OrderedItems.Target[j].GetObject().Updated)
}
}
return false
})
}