-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.go
More file actions
431 lines (370 loc) · 10.9 KB
/
object.go
File metadata and controls
431 lines (370 loc) · 10.9 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package pana
import (
"encoding/json"
"iter"
ld "sourcery.dny.nu/longdistance"
"sourcery.dny.nu/pana/vocab/ostatus"
as "sourcery.dny.nu/pana/vocab/w3/activitystreams"
)
// Object is the ActivityStreams Object type.
type Object ld.Node
// NewObject initialises a new Object.
func NewObject() *Object {
return &Object{
Properties: make(ld.Properties),
Type: []string{as.TypeObject},
}
}
// Build finalises the Object.
//
// This returns [Any] since that's what [Activity.SetObject] expects.
func (o *Object) Build() Any {
return Any(*o)
}
// GetID returns the object ID from [ld.Node.ID].
//
// This is equivalent to the 'id' property in compacted form JSON or the '@id'
// property in expanded form.
//
// If an object doesn't have an ID but does have other properties it is an
// anonymous object (a blank node). This represents something that exists but is
// not named or referencable.
func (o *Object) GetID() string {
return o.ID
}
// SetID sets the object ID in [ld.Node.ID].
func (o *Object) SetID(id string) *Object {
o.ID = id
return o
}
// GetType returns the first type from [ld.Node.Type].
//
// This is equivalent to the 'type' property in compacted form JSON or the
// '@type' property in expanded form.
//
// It will return the empty string if the object has no type.
//
// An object can have multiple types, but this is so uncommon on the fediverse
// that this method only returns the first type. You can check the size of
// [ld.Node.Type] if you'd like.
func (o *Object) GetType() string {
if o.Type == nil {
return ""
}
return o.Type[0]
}
// SetType sets the type in [ld.Node.Type].
func (o *Object) SetType(t string) *Object {
o.Type = []string{t}
return o
}
// GetCc returns the audience IDs from [as.Cc].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-cc.
func (o *Object) GetCc() iter.Seq[string] {
return func(yield func(string) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Cc) {
if !yield(n.ID) {
return
}
}
}
}
// AddCc appends audience IDs to [as.Cc].
func (o *Object) AddCc(ids ...string) *Object {
(*ld.Node)(o).AddNodes(as.Cc, toReference(ids...)...)
return o
}
// GetContent returns the localised values in [as.Content].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-content.
func (o *Object) GetContent() iter.Seq[*Localised] {
return func(yield func(*Localised) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Content) {
if !yield((*Localised)(&n)) {
return
}
}
}
}
// AddContent adds values to [as.Content].
func (o *Object) AddContent(ls ...Localised) *Object {
(*ld.Node)(o).AddNodes(as.Content, toLDNodes(ls...)...)
return o
}
// GetContext returns the ID in [as.Context].
//
// This is not the JSON-LD [ld.KeywordContext].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context.
func (o *Object) GetContext() string {
if nodes := (*ld.Node)(o).GetNodes(as.Context); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetContext sets the ID in [as.Context].
func (o *Object) SetContext(id string) *Object {
(*ld.Node)(o).SetNodes(as.Context, ld.Node{ID: id})
return o
}
// GetTo returns the audience IDs from [as.To].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to.
func (o *Object) GetTo() iter.Seq[string] {
return func(yield func(string) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.To) {
if !yield(n.ID) {
return
}
}
}
}
// AddTo appends audience IDs to [as.To].
func (o *Object) AddTo(ids ...string) *Object {
(*ld.Node)(o).AddNodes(as.To, toReference(ids...)...)
return o
}
// GetPublished retrieves the value from [as.Published].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-published.
func (o *Object) GetPublished() json.RawMessage {
if nodes := (*ld.Node)(o).GetNodes(as.Published); len(nodes) == 1 {
return nodes[0].Value
}
return nil
}
// SetPublished sets the value in [as.Published].
func (o *Object) SetPublished(value json.RawMessage) *Object {
(*ld.Node)(o).SetNodes(as.Published, ld.Node{Value: value})
return o
}
// GetAttributedTo returns the actor IDs from [as.AttributedTo].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attributedto.
func (o *Object) GetAttributedTo() iter.Seq[string] {
return func(yield func(string) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.AttributedTo) {
if !yield(n.ID) {
return
}
}
}
}
// AddAttributedTo appends actor IDs to [as.AttributedTo].
func (o *Object) AddAttributedTo(ids ...string) *Object {
(*ld.Node)(o).AddNodes(as.To, toReference(ids...)...)
return o
}
// GetReplies returns the [Collection] stored in [as.Replies].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies.
func (o *Object) GetReplies() *Collection {
if nodes := (*ld.Node)(o).GetNodes(as.Replies); len(nodes) == 1 {
return (*Collection)(&nodes[0])
}
return nil
}
// SetReplies sets the [Collection] in [as.Replies].
func (o *Object) SetReplies(c Collection) *Object {
(*ld.Node)(o).SetNodes(as.Replies, ld.Node(c))
return o
}
// GetURL returns the URL in [as.URL].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-url.
func (o *Object) GetURL() string {
if nodes := (*ld.Node)(o).GetNodes(as.URL); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetURL sets the URL in [as.URL].
func (o *Object) SetURL(url string) *Object {
(*ld.Node)(o).SetNodes(as.URL, toReference(url)...)
return o
}
// GetConversation returns the URI in [ostatus.Conversation].
//
// This is a [tag URI].
//
// [tag URI]: https://datatracker.ietf.org/doc/html/rfc4151
func (o *Object) GetConversation() string {
if nodes := (*ld.Node)(o).GetNodes(ostatus.Conversation); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetConversation sets the URI in [ostatus.Conversation].
func (o *Object) SetConversation(uri string) *Object {
(*ld.Node)(o).SetNodes(ostatus.Conversation, ld.Node{ID: uri})
return o
}
// GetAtomUri returns the URI in [ostatus.AtomURI].
//
// [tag URI]: https://datatracker.ietf.org/doc/html/rfc4151
func (o *Object) GetAtomURI() string {
if nodes := (*ld.Node)(o).GetNodes(ostatus.AtomURI); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetAtomUri sets the URI in [ostatus.AtomURI].
func (o *Object) SetAtomURI(uri string) *Object {
(*ld.Node)(o).SetNodes(ostatus.AtomURI, ld.Node{ID: uri})
return o
}
// GetSummary returns the localised values in [as.Summary].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary.
func (o *Object) GetSummary() iter.Seq[*Localised] {
return func(yield func(*Localised) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Summary) {
if !yield((*Localised)(&n)) {
return
}
}
}
}
// AddSummary adds values to [as.Summary].
func (o *Object) AddSummary(ls ...Localised) *Object {
(*ld.Node)(o).AddNodes(as.Summary, toLDNodes(ls...)...)
return o
}
// GetTag returns the values in [as.Tag].
//
// This returns Any because it can be an Emoji, a Hashtag, combinations of the
// two or more.
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag.
func (o *Object) GetTag() iter.Seq[*Any] {
return func(yield func(*Any) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Tag) {
if !yield((*Any)(&n)) {
return
}
}
}
}
// AddTag appends a tag to [as.Tag].
func (o *Object) AddTag(tag ...Any) *Object {
(*ld.Node)(o).AddNodes(as.Tag, toLDNodes(tag...)...)
return o
}
// GetInReplyTo returns the URL in [as.InReplyTo].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto.
func (o *Object) GetInReplyTo() string {
if nodes := (*ld.Node)(o).GetNodes(as.InReplyTo); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetInReplyTo sets the URL in [as.InReplyTo].
func (o *Object) SetInReplyTo(url string) *Object {
(*ld.Node)(o).SetNodes(as.InReplyTo, ld.Node{ID: url})
return o
}
// GetAttachment returns the attachments in [as.Attachment].
//
// This returns [Any] because is can be a Document, PropertyValue, a combination
// of both and more.
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment.
func (o *Object) GetAttachment() iter.Seq[*Any] {
return func(yield func(*Any) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Attachment) {
if !yield((*Any)(&n)) {
return
}
}
}
}
// AddAttachment appends attachments to [as.Attachment].
func (o *Object) AddAttachment(atch ...Any) *Object {
(*ld.Node)(o).AddNodes(as.Attachment, toLDNodes(atch...)...)
return o
}
// GetSensitive returns the value from [as.Sensitive].
//
// See https://swicg.github.io/miscellany/#sensitive.
func (o *Object) GetSensitive() json.RawMessage {
if nodes := (*ld.Node)(o).GetNodes(as.Sensitive); len(nodes) == 1 {
return nodes[0].Value
}
return nil
}
// SetSensitive sets the value in [as.Sensitive].
func (o *Object) SetSensitive(v json.RawMessage) *Object {
(*ld.Node)(o).SetNodes(as.Sensitive, ld.Node{Value: v})
return o
}
// GetInReplyToAtomURI returns the URI in [ostatus.InReplyToAtomUri].
func (o *Object) GetInReplyToAtomURI() string {
if nodes := (*ld.Node)(o).GetNodes(ostatus.InReplyToAtomURI); len(nodes) == 1 {
return nodes[0].ID
}
return ""
}
// SetInReplyToAtomURI sets the URI in [ostatus.InReplyToAtomUri].
func (o *Object) SetInReplyToAtomURI(uri string) *Object {
(*ld.Node)(o).SetNodes(ostatus.InReplyToAtomURI, ld.Node{ID: uri})
return o
}
// GetUpdated returns the value in [as.Updated].
//
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-endtime.
func (o *Object) GetUpdated() json.RawMessage {
if nodes := (*ld.Node)(o).GetNodes(as.Updated); len(nodes) == 1 {
return nodes[0].Value
}
return nil
}
// SetUpdated sets the value in [as.Updated].
func (o *Object) SetUpdated(v json.RawMessage) *Object {
(*ld.Node)(o).SetNodes(as.Updated, ld.Node{Value: v})
return o
}
// GetName returns the localised values in [as.Name].
func (o *Object) GetName() iter.Seq[*Localised] {
return func(yield func(*Localised) bool) {
for _, n := range (*ld.Node)(o).GetNodes(as.Name) {
if !yield((*Localised)(&n)) {
return
}
}
}
}
// AddName appends localised values to [as.Name].
func (o *Object) AddName(ls ...Localised) *Object {
(*ld.Node)(o).AddNodes(as.Name, toLDNodes(ls...)...)
return o
}
// GetLikes returns the [Collection] stored in [as.Likes].
//
// See https://www.w3.org/TR/activitypub/#likes.
func (o *Object) GetLikes() *Collection {
if nodes := (*ld.Node)(o).GetNodes(as.Likes); len(nodes) == 1 {
return (*Collection)(&nodes[0])
}
return nil
}
// SetLikes sets the [Collection] in [as.Likes].
func (o *Object) SetLikes(c Collection) *Object {
(*ld.Node)(o).SetNodes(as.Likes, ld.Node(c))
return o
}
// GetShares returns the [Collection] stored in [as.Shares].
//
// See https://www.w3.org/TR/activitypub/#shares.
func (o *Object) GetShares() *Collection {
if nodes := (*ld.Node)(o).GetNodes(as.Shares); len(nodes) == 1 {
return (*Collection)(&nodes[0])
}
return nil
}
// SetShares sets the [Collection] in [as.Shares].
func (o *Object) SetShares(c Collection) *Object {
(*ld.Node)(o).SetNodes(as.Shares, ld.Node(c))
return o
}