forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_aux.go
143 lines (127 loc) · 3.15 KB
/
event_aux.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
package nostr
import (
"encoding/json"
"fmt"
"time"
"github.com/valyala/fastjson"
)
func (evt *Event) UnmarshalJSON(payload []byte) error {
var fastjsonParser fastjson.Parser
parsed, err := fastjsonParser.ParseBytes(payload)
if err != nil {
return fmt.Errorf("failed to parse event: %w", err)
}
obj, err := parsed.Object()
if err != nil {
return fmt.Errorf("event is not an object")
}
// prepare this to receive any extra property that may serialized along with the event
evt.extra = make(map[string]any)
var visiterr error
obj.Visit(func(k []byte, v *fastjson.Value) {
key := string(k)
switch key {
case "id":
id, err := v.StringBytes()
if err != nil {
visiterr = fmt.Errorf("invalid 'id' field: %w", err)
}
evt.ID = string(id)
case "pubkey":
id, err := v.StringBytes()
if err != nil {
visiterr = fmt.Errorf("invalid 'pubkey' field: %w", err)
}
evt.PubKey = string(id)
case "created_at":
val, err := v.Int64()
if err != nil {
visiterr = fmt.Errorf("invalid 'created_at' field: %w", err)
}
evt.CreatedAt = time.Unix(val, 0)
case "kind":
kind, err := v.Int64()
if err != nil {
visiterr = fmt.Errorf("invalid 'kind' field: %w", err)
}
evt.Kind = int(kind)
case "tags":
evt.Tags, err = fastjsonArrayToTags(v)
if err != nil {
visiterr = fmt.Errorf("invalid '%s' field: %w", key, err)
}
case "content":
id, err := v.StringBytes()
if err != nil {
visiterr = fmt.Errorf("invalid 'content' field: %w", err)
}
evt.Content = string(id)
case "sig":
id, err := v.StringBytes()
if err != nil {
visiterr = fmt.Errorf("invalid 'sig' field: %w", err)
}
evt.Sig = string(id)
default:
var anyValue any
json.Unmarshal(v.MarshalTo(nil), anyValue)
evt.extra[key] = anyValue
}
})
if visiterr != nil {
return visiterr
}
return nil
}
func (evt Event) MarshalJSON() ([]byte, error) {
var arena fastjson.Arena
o := arena.NewObject()
o.Set("id", arena.NewString(evt.ID))
o.Set("pubkey", arena.NewString(evt.PubKey))
o.Set("created_at", arena.NewNumberInt(int(evt.CreatedAt.Unix())))
o.Set("kind", arena.NewNumberInt(evt.Kind))
o.Set("tags", tagsToFastjsonArray(&arena, evt.Tags))
o.Set("content", arena.NewString(evt.Content))
o.Set("sig", arena.NewString(evt.Sig))
for k, v := range evt.extra {
b, _ := json.Marshal(v)
if val, err := fastjson.ParseBytes(b); err == nil {
o.Set(k, val)
}
}
return o.MarshalTo(nil), nil
}
func fastjsonArrayToTags(v *fastjson.Value) (Tags, error) {
arr, err := v.Array()
if err != nil {
return nil, err
}
sll := make(Tags, len(arr))
for i, v := range arr {
subarr, err := v.Array()
if err != nil {
return nil, err
}
sl := make(Tag, len(subarr))
for j, subv := range subarr {
sb, err := subv.StringBytes()
if err != nil {
return nil, err
}
sl[j] = string(sb)
}
sll[i] = sl
}
return sll, nil
}
func tagsToFastjsonArray(arena *fastjson.Arena, tags Tags) *fastjson.Value {
jtags := arena.NewArray()
for i, v := range tags {
arr := arena.NewArray()
for j, subv := range v {
arr.SetArrayItem(j, arena.NewString(subv))
}
jtags.SetArrayItem(i, arr)
}
return jtags
}