mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-26 17:52:20 +01:00
kind-0 metadata helper type.
This commit is contained in:
parent
0c39530d57
commit
ea15550039
31
metadata.go
Normal file
31
metadata.go
Normal file
@ -0,0 +1,31 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ProfileMetadata struct {
|
||||
Name string `json:"name"`
|
||||
About string `json:"about"`
|
||||
Picture string `json:"picture"`
|
||||
NIP05 string `json:"nip05"`
|
||||
}
|
||||
|
||||
func ParseMetadata(event Event) (*ProfileMetadata, error) {
|
||||
if event.Kind != 0 {
|
||||
return nil, fmt.Errorf("event %s is kind %d, not 0", event.ID, event.Kind)
|
||||
}
|
||||
|
||||
var meta ProfileMetadata
|
||||
err := json.Unmarshal([]byte(event.Content), &meta)
|
||||
if err != nil {
|
||||
cont := event.Content
|
||||
if len(cont) > 100 {
|
||||
cont = cont[0:99]
|
||||
}
|
||||
return nil, fmt.Errorf("failed to parse metadata (%s) from event %s: %w", cont, event.ID, err)
|
||||
}
|
||||
|
||||
return &meta, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user