forked from coracle/nostrlib
7289da9c72
this commit also remove all the sonic envelope parsing and reintroduces filters in REQ as a slice instead of as a singleton. why? well, the sonic stuff wasn't really that fast, it was a little bit but only got fast enough once I introduced unsafe conversions between []byte and string and did weird unsafe reuse of []byte in order to save the values of tags, which would definitely cause issues in the future if the caller wasn't aware of it (and even if they were, like myself). and the filters stuff is because we abandoned the idea of changing NIP-01 to only accept one filter per REQ.
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"fiatjaf.com/nostr"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
// this is the default command when no subcommands are given, we will just try everything
|
|
var queryOrSave = &cli.Command{
|
|
Hidden: true,
|
|
Name: "query-or-save",
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
|
line := getStdin()
|
|
|
|
ee := &nostr.EventEnvelope{}
|
|
re := &nostr.ReqEnvelope{}
|
|
e := &nostr.Event{}
|
|
f := &nostr.Filter{}
|
|
if json.Unmarshal([]byte(line), ee) == nil && ee.Event.ID != nostr.ZeroID {
|
|
return doSave(ctx, line, ee.Event)
|
|
}
|
|
if json.Unmarshal([]byte(line), e) == nil && e.ID != nostr.ZeroID {
|
|
return doSave(ctx, line, *e)
|
|
}
|
|
if json.Unmarshal([]byte(line), re) == nil {
|
|
return doQuery(ctx, &re.Filters[0])
|
|
}
|
|
if json.Unmarshal([]byte(line), f) == nil && len(f.String()) > 2 {
|
|
return doQuery(ctx, f)
|
|
}
|
|
|
|
return fmt.Errorf("couldn't parse input '%s'", line)
|
|
},
|
|
}
|
|
|
|
func doSave(ctx context.Context, line string, evt nostr.Event) error {
|
|
if err := db.SaveEvent(evt); err != nil {
|
|
return fmt.Errorf("failed to save event '%s': %s", line, err)
|
|
}
|
|
fmt.Fprintf(os.Stderr, "saved %s", evt.ID)
|
|
return nil
|
|
}
|
|
|
|
func doQuery(ctx context.Context, f *nostr.Filter) error {
|
|
for evt := range db.QueryEvents(*f, 1_000_000) {
|
|
fmt.Println(evt)
|
|
}
|
|
return nil
|
|
}
|