-
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
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
package activitystreams
import (
"encoding"
"encoding/json"
"fmt"
"net/url"
"reflect"
"strings"
"time"
"github.com/buger/jsonparser"
)
var (
apUnmarshalerType = reflect.TypeOf(new(Item)).Elem()
unmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
)
type mockObj map[string]json.RawMessage
func getType(j json.RawMessage) ActivityVocabularyType {
mock := make(mockObj, 0)
json.Unmarshal([]byte(j), &mock)
for key, val := range mock {
if strings.ToLower(key) == "type" {
return ActivityVocabularyType(strings.Trim(string(val), "\""))
}
}
return ""
}
func getAPObjectID(data []byte) ObjectID {
i, err := jsonparser.GetString(data, "id")
if err != nil {
return ObjectID("")
}
return ObjectID(i)
}
func getAPType(data []byte) ActivityVocabularyType {
t, err := jsonparser.GetString(data, "type")
typ := ActivityVocabularyType(t)
if err != nil {
return ActivityVocabularyType("")
}
return typ
}
func getAPMimeType(data []byte) MimeType {
t, err := jsonparser.GetString(data, "mediaType")
if err != nil {
return MimeType("")
}
return MimeType(t)
}
func getAPInt(data []byte, prop string) int64 {
val, err := jsonparser.GetInt(data, prop)
if err != nil {
}
return val
}
func getAPString(data []byte, prop string) string {
val, err := jsonparser.GetString(data, prop)
if err != nil {
}
return val
}
func getAPNaturalLanguageField(data []byte, prop string) NaturalLanguageValue {
n := NaturalLanguageValue{}
val, typ, _, err := jsonparser.Get(data, prop)
if err != nil {
return nil
}
switch typ {
case jsonparser.Object:
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
if dataType == jsonparser.String {
n.Append(LangRef(key), string(value))
}
return err
})
case jsonparser.String:
n.Append(NilLangRef, string(val))
}
return n
}
func getAPTime(data []byte, prop string) time.Time {
t := time.Time{}
str, _ := jsonparser.GetUnsafeString(data, prop)
t.UnmarshalText([]byte(str))
return t
}
func unmarshalToAPObject(data []byte) Item {
if _, err := url.ParseRequestURI(string(data)); err == nil {
// try to see if it's an IRI
return IRI(data)
}
i, err := getAPObjectByType(getAPType(data))
if err != nil {
return nil
}
p := reflect.PtrTo(reflect.TypeOf(i))
if reflect.TypeOf(i).Implements(unmarshalerType) || p.Implements(unmarshalerType) {
err = i.(json.Unmarshaler).UnmarshalJSON(data)
}
if reflect.TypeOf(i).Implements(textUnmarshalerType) || p.Implements(textUnmarshalerType) {
err = i.(encoding.TextUnmarshaler).UnmarshalText(data)
}
if err != nil {
return nil
}
return i
}
func getAPItem(data []byte, prop string) Item {
val, typ, _, err := jsonparser.Get(data, prop)
if err != nil {
return nil
}
switch typ {
case jsonparser.String:
if _, err = url.ParseRequestURI(string(val)); err == nil {
// try to see if it's an IRI
return IRI(val)
}
case jsonparser.Object:
return unmarshalToAPObject(val)
case jsonparser.Number:
fallthrough
case jsonparser.Array:
fallthrough
case jsonparser.Boolean:
fallthrough
case jsonparser.Null:
fallthrough
case jsonparser.Unknown:
fallthrough
default:
return nil
}
return nil
}
func getAPItems(data []byte, prop string) ItemCollection {
val, typ, _, err := jsonparser.Get(data, prop)
if err != nil {
return nil
}
var it ItemCollection
switch typ {
case jsonparser.Array:
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
i := unmarshalToAPObject(value)
if i != nil {
it.Append(i)
}
}, prop)
case jsonparser.Object:
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
i := unmarshalToAPObject(value)
if i != nil {
it.Append(i)
}
return err
}, prop)
case jsonparser.String:
s, _ := jsonparser.GetString(val)
it.Append(IRI(s))
}
return it
}
func getURIField(data []byte, prop string) Item {
val, typ, _, err := jsonparser.Get(data, prop)
if err != nil {
return nil
}
switch typ {
case jsonparser.Object:
return getAPItem(data, prop)
case jsonparser.Array:
var it ItemCollection
jsonparser.ArrayEach(val, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
if _, err := url.Parse(string(value)); err == nil {
it.Append(IRI(value))
return
}
i, err := getAPObjectByType(getAPType(value))
if err != nil {
return
}
err = i.(json.Unmarshaler).UnmarshalJSON(value)
if err != nil {
return
}
it.Append(i)
})
return it
case jsonparser.String:
return IRI(val)
}
return nil
}
func getAPLangRefField(data []byte, prop string) LangRef {
val, err := jsonparser.GetString(data, prop)
if err != nil {
return LangRef("")
}
return LangRef(val)
}
// UnmarshalJSON tries to detect the type of the object in the json data and then outputs a matching
// ActivityStreams object, if possible
func UnmarshalJSON(data []byte) (Item, error) {
return unmarshalToAPObject(data), nil
}
/*
func unmarshal(data []byte, a interface{}) (interface{}, error) {
ta := make(mockObj, 0)
err := jsonld.Unmarshal(data, &ta)
if err != nil {
return nil, err
}
typ := reflect.TypeOf(a)
val := reflect.ValueOf(a)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
for i := 0; i < typ.NumField(); i++ {
cField := typ.Field(i)
cValue := val.Field(i)
cTag := cField.Tag
tag, _ := jsonld.LoadTag(cTag)
var vv reflect.Value
for key, j := range ta {
if j == nil {
continue
}
if key == tag.Name {
if cField.Type.Implements(textUnmarshalerType) {
m, _ := cValue.Interface().(encoding.TextUnmarshaler)
m.UnmarshalText(j)
vv = reflect.ValueOf(m)
}
if cField.Type.Implements(unmarshalerType) {
m, _ := cValue.Interface().(json.Unmarshaler)
m.UnmarshalJSON(j)
vv = reflect.ValueOf(m)
}
if cField.Type.Implements(apUnmarshalerType) {
o := getAPObjectByType(getType(j))
if o != nil {
jsonld.Unmarshal([]byte(j), o)
vv = reflect.ValueOf(o)
}
}
}
if vv.CanAddr() {
cValue.Set(vv)
fmt.Printf("\n\nReflected %q %q => %#v\n\n%#v\n", cField.Name, cField.Type, vv, tag.Name)
}
}
}
return a, nil
}
*/
func getAPObjectByType(typ ActivityVocabularyType) (Item, error) {
var ret Item
var err error
switch typ {
case ObjectType:
ret = ObjectNew(typ)
case LinkType:
ret = &Link{}
o := ret.(*Link)
o.Type = typ
case ActivityType:
ret = &Activity{}
o := ret.(*Activity)
o.Type = typ
case IntransitiveActivityType:
ret = &IntransitiveActivity{}
o := ret.(*IntransitiveActivity)
o.Type = typ
case ActorType:
ret = &Actor{}
o := ret.(*Actor)
o.Type = typ
case CollectionType:
ret = &Collection{}
o := ret.(*Collection)
o.Type = typ
case OrderedCollectionType:
ret = &OrderedCollection{}
o := ret.(*OrderedCollection)
o.Type = typ
case CollectionPageType:
ret = &CollectionPage{}
o := ret.(*CollectionPage)
o.Type = typ
case OrderedCollectionPageType:
ret = &OrderedCollectionPage{}
o := ret.(*OrderedCollectionPage)
o.Type = typ
case ArticleType:
ret = ObjectNew(typ)
case AudioType:
ret = ObjectNew(typ)
case DocumentType:
ret = ObjectNew(typ)
case EventType:
o := Object{}
o.Type = typ
case ImageType:
ret = ObjectNew(typ)
o := ret.(*Object)
o.Type = typ
case NoteType:
ret = ObjectNew(typ)
case PageType:
ret = ObjectNew(typ)
case PlaceType:
ret = ObjectNew(typ)
case ProfileType:
ret = ObjectNew(typ)
case RelationshipType:
ret = ObjectNew(typ)
case TombstoneType:
ret = ObjectNew(typ)
case VideoType:
ret = ObjectNew(typ)
case MentionType:
ret = &Mention{}
o := ret.(*Mention)
o.Type = typ
case ApplicationType:
ret = &Application{}
o := ret.(*Application)
o.Type = typ
case GroupType:
ret = &Group{}
o := ret.(*Group)
o.Type = typ
case OrganizationType:
ret = &Organization{}
o := ret.(*Organization)
o.Type = typ
case PersonType:
ret = &Person{}
o := ret.(*Person)
o.Type = typ
case ServiceType:
ret = &Service{}
o := ret.(*Service)
o.Type = typ
case AcceptType:
ret = &Accept{}
o := ret.(*Accept)
o.Type = typ
case AddType:
ret = &Add{}
o := ret.(*Add)
o.Type = typ
case AnnounceType:
ret = &Announce{}
o := ret.(*Announce)
o.Type = typ
case ArriveType:
ret = &Arrive{}
o := ret.(*Arrive)
o.Type = typ
case BlockType:
ret = &Block{}
o := ret.(*Block)
o.Type = typ
case CreateType:
ret = &Create{}
o := ret.(*Create)
o.Type = typ
case DeleteType:
ret = &Delete{}
o := ret.(*Delete)
o.Type = typ
case DislikeType:
ret = &Dislike{}
o := ret.(*Dislike)
o.Type = typ
case FlagType:
ret = &Flag{}
o := ret.(*Flag)
o.Type = typ
case FollowType:
ret = &Follow{}
o := ret.(*Follow)
o.Type = typ
case IgnoreType:
ret = &Ignore{}
o := ret.(*Ignore)
o.Type = typ
case InviteType:
ret = &Invite{}
o := ret.(*Invite)
o.Type = typ
case JoinType:
ret = &Join{}
o := ret.(*Join)
o.Type = typ
case LeaveType:
ret = &Leave{}
o := ret.(*Leave)
o.Type = typ
case LikeType:
ret = &Like{}
o := ret.(*Like)
o.Type = typ
case ListenType:
ret = &Listen{}
o := ret.(*Listen)
o.Type = typ
case MoveType:
ret = &Move{}
o := ret.(*Move)
o.Type = typ
case OfferType:
ret = &Offer{}
o := ret.(*Offer)
o.Type = typ
case QuestionType:
ret = &Question{}
o := ret.(*Question)
o.Type = typ
case RejectType:
ret = &Reject{}
o := ret.(*Reject)
o.Type = typ
case ReadType:
ret = &Read{}
o := ret.(*Read)
o.Type = typ
case RemoveType:
ret = &Remove{}
o := ret.(*Remove)
o.Type = typ
case TentativeRejectType:
ret = &TentativeReject{}
o := ret.(*TentativeReject)
o.Type = typ
case TentativeAcceptType:
ret = &TentativeAccept{}
o := ret.(*TentativeAccept)
o.Type = typ
case TravelType:
ret = &Travel{}
o := ret.(*Travel)
o.Type = typ
case UndoType:
ret = &Undo{}
o := ret.(*Undo)
o.Type = typ
case UpdateType:
ret = &Update{}
o := ret.(*Update)
o.Type = typ
case ViewType:
ret = &View{}
o := ret.(*View)
o.Type = typ
case "":
// when no type is available use a plain Object
ret = &Object{}
default:
ret = nil
err = fmt.Errorf("unrecognized ActivityPub type %q", typ)
}
return ret, err
}