Skip to content

Commit

Permalink
lots of error handling and sanity
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Jan 3, 2024
1 parent 614607c commit 14a63e3
Show file tree
Hide file tree
Showing 53 changed files with 593 additions and 468 deletions.
8 changes: 4 additions & 4 deletions cmd/algia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (cfg *Config) Do(r Relay, f func(context.Context, *nostr.Relay) bool) {
wg.Wait()
}

func (cfg *Config) save(profile string) error {
func (cfg *Config) save(profile string) (e error) {
if cfg.tempRelay {
return nil
}
Expand All @@ -302,7 +302,7 @@ func (cfg *Config) save(profile string) error {
}

// Decode is
func (cfg *Config) Decode(ev *nostr.Event) error {
func (cfg *Config) Decode(ev *nostr.Event) (e error) {
var sk string
var pub string
if _, s, err := nip19.Decode(cfg.PrivateKey); err == nil {
Expand Down Expand Up @@ -442,7 +442,7 @@ func (cfg *Config) Events(filter nostr.Filter) []*nostr.Event {
return evs
}

func doVersion(cCtx *cli.Context) error {
func doVersion(cCtx *cli.Context) (e error) {
fmt.Println(version)
return nil
}
Expand Down Expand Up @@ -658,7 +658,7 @@ func main() {
Action: doVersion,
},
},
Before: func(cCtx *cli.Context) error {
Before: func(cCtx *cli.Context) (e error) {
if cCtx.Args().Get(0) == "version" {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/algia/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/nbd-wtf/nostr-sdk"
)

func doProfile(cCtx *cli.Context) error {
func doProfile(cCtx *cli.Context) (e error) {
user := cCtx.String("u")
j := cCtx.Bool("json")

Expand Down
32 changes: 16 additions & 16 deletions cmd/algia/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/nbd-wtf/nostr-sdk"
)

func doDMList(cCtx *cli.Context) error {
func doDMList(cCtx *cli.Context) (e error) {
j := cCtx.Bool("json")

cfg := cCtx.App.Metadata["config"].(*Config)
Expand Down Expand Up @@ -94,7 +94,7 @@ func doDMList(cCtx *cli.Context) error {
return nil
}

func doDMTimeline(cCtx *cli.Context) error {
func doDMTimeline(cCtx *cli.Context) (e error) {
u := cCtx.String("u")
j := cCtx.Bool("json")
extra := cCtx.Bool("extra")
Expand Down Expand Up @@ -141,7 +141,7 @@ func doDMTimeline(cCtx *cli.Context) error {
return nil
}

func doDMPost(cCtx *cli.Context) error {
func doDMPost(cCtx *cli.Context) (e error) {
u := cCtx.String("u")
stdin := cCtx.Bool("stdin")
if !stdin && cCtx.Args().Len() == 0 {
Expand Down Expand Up @@ -226,7 +226,7 @@ func doDMPost(cCtx *cli.Context) error {
return nil
}

func doPost(cCtx *cli.Context) error {
func doPost(cCtx *cli.Context) (e error) {
stdin := cCtx.Bool("stdin")
if !stdin && cCtx.Args().Len() == 0 {
return cli.ShowSubcommandHelp(cCtx)
Expand Down Expand Up @@ -333,7 +333,7 @@ func doPost(cCtx *cli.Context) error {
return nil
}

func doReply(cCtx *cli.Context) error {
func doReply(cCtx *cli.Context) (e error) {
stdin := cCtx.Bool("stdin")
id := cCtx.String("id")
quote := cCtx.Bool("quote")
Expand Down Expand Up @@ -442,7 +442,7 @@ func doReply(cCtx *cli.Context) error {
return nil
}

func doRepost(cCtx *cli.Context) error {
func doRepost(cCtx *cli.Context) (e error) {
id := cCtx.String("id")

cfg := cCtx.App.Metadata["config"].(*Config)
Expand Down Expand Up @@ -510,7 +510,7 @@ func doRepost(cCtx *cli.Context) error {
return nil
}

func doUnrepost(cCtx *cli.Context) error {
func doUnrepost(cCtx *cli.Context) (e error) {
id := cCtx.String("id")
if evp := sdk.InputToEventPointer(id); evp != nil {
id = evp.ID
Expand Down Expand Up @@ -574,7 +574,7 @@ func doUnrepost(cCtx *cli.Context) error {
return nil
}

func doLike(cCtx *cli.Context) error {
func doLike(cCtx *cli.Context) (e error) {
id := cCtx.String("id")

cfg := cCtx.App.Metadata["config"].(*Config)
Expand Down Expand Up @@ -654,7 +654,7 @@ func doLike(cCtx *cli.Context) error {
return nil
}

func doUnlike(cCtx *cli.Context) error {
func doUnlike(cCtx *cli.Context) (e error) {
id := cCtx.String("id")
if evp := sdk.InputToEventPointer(id); evp != nil {
id = evp.ID
Expand Down Expand Up @@ -718,7 +718,7 @@ func doUnlike(cCtx *cli.Context) error {
return nil
}

func doDelete(cCtx *cli.Context) error {
func doDelete(cCtx *cli.Context) (e error) {
id := cCtx.String("id")

cfg := cCtx.App.Metadata["config"].(*Config)
Expand Down Expand Up @@ -767,7 +767,7 @@ func doDelete(cCtx *cli.Context) error {
return nil
}

func doSearch(cCtx *cli.Context) error {
func doSearch(cCtx *cli.Context) (e error) {
n := cCtx.Int("n")
j := cCtx.Bool("json")
extra := cCtx.Bool("extra")
Expand Down Expand Up @@ -798,7 +798,7 @@ func doSearch(cCtx *cli.Context) error {
return nil
}

func doStream(cCtx *cli.Context) error {
func doStream(cCtx *cli.Context) (e error) {
kinds := cCtx.IntSlice("kind")
authors := cCtx.StringSlice("author")
f := cCtx.Bool("follow")
Expand Down Expand Up @@ -886,7 +886,7 @@ func doStream(cCtx *cli.Context) error {
return nil
}

func doTimeline(cCtx *cli.Context) error {
func doTimeline(cCtx *cli.Context) (e error) {
n := cCtx.Int("n")
j := cCtx.Bool("json")
extra := cCtx.Bool("extra")
Expand Down Expand Up @@ -915,7 +915,7 @@ func doTimeline(cCtx *cli.Context) error {
return nil
}

func postMsg(cCtx *cli.Context, msg string) error {
func postMsg(cCtx *cli.Context, msg string) (e error) {
cfg := cCtx.App.Metadata["config"].(*Config)

var sk string
Expand Down Expand Up @@ -958,10 +958,10 @@ func postMsg(cCtx *cli.Context, msg string) error {
return nil
}

func doPowa(cCtx *cli.Context) error {
func doPowa(cCtx *cli.Context) (e error) {
return postMsg(cCtx, "ぽわ〜")
}

func doPuru(cCtx *cli.Context) error {
func doPuru(cCtx *cli.Context) (e error) {
return postMsg(cCtx, "(((( ˙꒳​˙ ))))プルプルプルプルプルプルプル")
}
4 changes: 2 additions & 2 deletions cmd/algia/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type PayResponse struct {
} `json:"result"`
}

func pay(cfg *Config, invoice string) error {
func pay(cfg *Config, invoice string) (e error) {
uri, err := url.Parse(cfg.NwcURI)
if err != nil {
return err
Expand Down Expand Up @@ -186,7 +186,7 @@ func (cfg *Config) ZapInfo(pub string) (*Lnurlp, error) {
return &lp, nil
}

func doZap(cCtx *cli.Context) error {
func doZap(cCtx *cli.Context) (e error) {
amount := cCtx.Uint64("amount")
comment := cCtx.String("comment")
if cCtx.Args().Len() == 0 {
Expand Down
23 changes: 12 additions & 11 deletions cmd/replicatrd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import (

"github.com/Hubmakerlabs/replicatr/cmd/replicatrd/replicatr"
"github.com/Hubmakerlabs/replicatr/pkg/eventstore/badger"
log2 "mleku.online/git/log"
)

const appName = "replicatr"

func main() {
r := replicatr.NewRelay(appName)

db := badger.BadgerBackend{Path: "/tmp/replicatr-badger"}
if err := db.Init(); err != nil {
r.Log.E.F("unable to start database: '%s'", err)
log2.SetLogLevel(log2.Trace)
rl := replicatr.NewRelay(appName)
db := &badger.BadgerBackend{Path: "/tmp/replicatr-badger"}
if e:= db.Init(); rl.Log.E.Chk(e) {
rl.Log.E.F("unable to start database: '%s'", e)
os.Exit(1)
}
r.StoreEvent = append(r.StoreEvent, db.SaveEvent)
r.QueryEvents = append(r.QueryEvents, db.QueryEvents)
r.CountEvents = append(r.CountEvents, db.CountEvents)
r.DeleteEvent = append(r.DeleteEvent, db.DeleteEvent)
r.Log.I.Ln("running on :3334")
r.Log.E.Chk(http.ListenAndServe(":3334", r))
rl.StoreEvent = append(rl.StoreEvent, db.SaveEvent)
rl.QueryEvents = append(rl.QueryEvents, db.QueryEvents)
rl.CountEvents = append(rl.CountEvents, db.CountEvents)
rl.DeleteEvent = append(rl.DeleteEvent, db.DeleteEvent)
rl.Log.I.Ln("running on :3334")
rl.Log.E.Chk(http.ListenAndServe(":3334", rl))
}
43 changes: 23 additions & 20 deletions cmd/replicatrd/replicatr/adding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,37 @@ import (
)

// AddEvent sends an event through then normal add pipeline, as if it was received from a websocket.
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) (e error) {
if evt == nil {
return errors.New("error: event is nil")
}

for _, reject := range rl.RejectEvent {
if reject, msg := reject(ctx, evt); reject {
for _, rejectors := range rl.RejectEvent {
if reject, msg := rejectors(ctx, evt); reject {
if msg == "" {
return errors.New("blocked: no reason")
e = errors.New("blocked: no reason")
rl.Log.E.Ln(e)
return
} else {
return errors.New(nostr.NormalizeOKMessage(msg, "blocked"))
e = errors.New(nostr.NormalizeOKMessage(msg, "blocked"))
rl.Log.E.Ln(e)
return
}
}
}

if 20000 <= evt.Kind && evt.Kind < 30000 {
// do not store ephemeral events
} else {
if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
// replaceable event, delete before storing
for _, query := range rl.QueryEvents {
ch, err := query(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
if err != nil {
var ch chan *nostr.Event
ch, e = query(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
if rl.Log.E.Chk(e) {
continue
}
if previous := <-ch; previous != nil && isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
rl.Log.D.Chk(del(ctx, previous))
}
}
}
Expand All @@ -46,40 +49,40 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
d := evt.Tags.GetFirst([]string{"d", ""})
if d != nil {
for _, query := range rl.QueryEvents {
ch, err := query(ctx, &nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
if err != nil {
var ch chan *nostr.Event
if ch, e = query(ctx, &nostr.Filter{
Authors: []string{evt.PubKey},
Kinds: []int{evt.Kind},
Tags: nostr.TagMap{"d": []string{d.Value()}},
}); rl.Log.E.Chk(e) {
continue
}
if previous := <-ch; previous != nil && isOlder(previous, evt) {
for _, del := range rl.DeleteEvent {
del(ctx, previous)
rl.Log.E.Chk(del(ctx, previous))
}
}
}
}
}

// store
for _, store := range rl.StoreEvent {
if saveErr := store(ctx, evt); saveErr != nil {
switch saveErr {
case eventstore.ErrDupEvent:
if saveErr := store(ctx, evt); rl.Log.E.Chk(saveErr){
switch {
case errors.Is(saveErr, eventstore.ErrDupEvent):
return nil
default:
return fmt.Errorf(nostr.NormalizeOKMessage(saveErr.Error(), "error"))
}
}
}

for _, ons := range rl.OnEventSaved {
ons(ctx, evt)
}
}

for _, ovw := range rl.OverwriteResponseEvent {
ovw(ctx, evt)
}
notifyListeners(evt)

return nil
}
4 changes: 1 addition & 3 deletions cmd/replicatrd/replicatr/broadcasting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ import (

// BroadcastEvent emits an event to all listeners whose filters' match, skipping all filters and actions
// it also doesn't attempt to store the event or trigger any reactions or callbacks
func (rl *Relay) BroadcastEvent(evt *nostr.Event) {
notifyListeners(evt)
}
func (rl *Relay) BroadcastEvent(evt *nostr.Event) { notifyListeners(evt) }
12 changes: 7 additions & 5 deletions cmd/replicatrd/replicatr/deleting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/nbd-wtf/go-nostr"
)

func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) error {
func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) (e error) {
// event deletion -- nip09
for _, tag := range evt.Tags {
if len(tag) >= 2 && tag[0] == "e" {
// first we fetch the event
for _, query := range rl.QueryEvents {
ch, err := query(ctx, &nostr.Filter{IDs: []string{tag[1]}})
if err != nil {
var ch chan *nostr.Event
if ch, e = query(ctx, &nostr.Filter{IDs: []string{tag[1]}}); rl.Log.E.Chk(e) {
continue
}
target := <-ch
Expand All @@ -34,11 +34,13 @@ func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) erro
if acceptDeletion {
// delete it
for _, del := range rl.DeleteEvent {
del(ctx, target)
rl.Log.E.Chk(del(ctx, target))
}
} else {
// fail and stop here
return fmt.Errorf("blocked: %s", msg)
e = fmt.Errorf("blocked: %s", msg)
rl.Log.E.Chk(e)
return
}

// don't try to query this same event again
Expand Down
Loading

0 comments on commit 14a63e3

Please sign in to comment.