mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-30 01:29:20 +02:00
support CLOSED messages.
This commit is contained in:
parent
b6ec7327ef
commit
fa20f84ec7
33
envelopes.go
33
envelopes.go
@ -37,6 +37,8 @@ func ParseMessage(message []byte) Envelope {
|
||||
case bytes.Contains(label, []byte("CLOSE")):
|
||||
x := CloseEnvelope("")
|
||||
v = &x
|
||||
case bytes.Contains(label, []byte("CLOSED")):
|
||||
v = &ClosedEnvelope{}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@ -255,12 +257,41 @@ func (v *CloseEnvelope) UnmarshalJSON(data []byte) error {
|
||||
|
||||
func (v CloseEnvelope) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
w.RawString(`["CLOSE",`)
|
||||
w.RawString(`["CLOSED",`)
|
||||
w.Raw(json.Marshal(string(v)))
|
||||
w.RawString(`]`)
|
||||
return w.BuildBytes()
|
||||
}
|
||||
|
||||
type ClosedEnvelope struct {
|
||||
SubscriptionID string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (_ ClosedEnvelope) Label() string { return "CLOSED" }
|
||||
|
||||
func (v *ClosedEnvelope) UnmarshalJSON(data []byte) error {
|
||||
r := gjson.ParseBytes(data)
|
||||
arr := r.Array()
|
||||
switch len(arr) {
|
||||
case 3:
|
||||
*v = ClosedEnvelope{arr[1].Str, arr[2].Str}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("failed to decode CLOSED envelope")
|
||||
}
|
||||
}
|
||||
|
||||
func (v ClosedEnvelope) MarshalJSON() ([]byte, error) {
|
||||
w := jwriter.Writer{}
|
||||
w.RawString(`["CLOSED",`)
|
||||
w.Raw(json.Marshal(string(v.SubscriptionID)))
|
||||
w.RawString(`,`)
|
||||
w.Raw(json.Marshal(v.Reason))
|
||||
w.RawString(`]`)
|
||||
return w.BuildBytes()
|
||||
}
|
||||
|
||||
type OKEnvelope struct {
|
||||
EventID string
|
||||
OK bool
|
||||
|
@ -88,6 +88,19 @@ func TestOKEnvelopeEncodingAndDecoding(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestClosedEnvelopeEncodingAndDecoding(t *testing.T) {
|
||||
src := `["CLOSED","_","error: something went wrong"]`
|
||||
var env ClosedEnvelope
|
||||
json.Unmarshal([]byte(src), &env)
|
||||
if env.SubscriptionID != "_" {
|
||||
t.Error("failed to decode CLOSED")
|
||||
}
|
||||
|
||||
if res, _ := json.Marshal(env); string(res) != src {
|
||||
t.Errorf("failed to encode CLOSED: expected '%s', got '%s'", src, string(res))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) {
|
||||
authEnvelopes := []string{
|
||||
`["AUTH","kjsabdlasb aslkd kasndkad \"as.kdnbskadb"]`,
|
||||
|
Loading…
x
Reference in New Issue
Block a user