From 02729c0915b0ddeb6bdfc8fee415240482edad36 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 23 Nov 2023 15:44:57 +0700 Subject: [PATCH 1/2] chore: update go-nostr to v0.25.5 --- go.mod | 17 ++++++++++++----- go.sum | 26 ++++++++++++++++++++++++++ main.go | 20 +++++++++++++++++--- service.go | 41 ++++++++++++++++++++++------------------- 4 files changed, 77 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index e78b76d2..665369b7 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/gorilla/sessions v1.2.1 github.com/labstack/echo-contrib v0.14.1 github.com/labstack/echo/v4 v4.10.2 - github.com/nbd-wtf/go-nostr v0.13.2 + github.com/nbd-wtf/go-nostr v0.25.5 github.com/nbd-wtf/ln-decodepay v1.11.1 github.com/stretchr/testify v1.8.2 golang.org/x/oauth2 v0.4.0 @@ -49,7 +49,7 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/decred/dcrd/lru v1.1.1 // indirect - github.com/dgraph-io/ristretto v0.1.0 // indirect + github.com/dgraph-io/ristretto v0.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvyukov/go-fuzz v0.0.0-20220726122315-1d375ef9f9f6 // indirect github.com/fergusstrange/embedded-postgres v1.19.0 // indirect @@ -59,6 +59,9 @@ require ( github.com/go-logr/logr v1.2.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-macaroon-bakery/macaroonpb v1.0.0 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.2.0 // indirect github.com/gofrs/uuid v4.2.0+incompatible // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect @@ -117,6 +120,7 @@ require ( github.com/prometheus/client_model v0.3.0 // indirect github.com/prometheus/common v0.40.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect + github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect github.com/rogpeppe/fastuuid v1.2.0 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect @@ -125,6 +129,9 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect github.com/tinylib/msgp v1.1.6 // indirect github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect @@ -182,8 +189,8 @@ require ( github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8 // indirect github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect - github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect + github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect github.com/glebarez/sqlite v1.5.0 github.com/gorilla/websocket v1.5.0 // indirect github.com/jackc/pgx/v5 v5.4.3 @@ -193,6 +200,6 @@ require ( github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/sirupsen/logrus v1.9.0 github.com/valyala/fastjson v1.6.3 // indirect - golang.org/x/exp v0.0.0-20221106115401-f9659909a136 // indirect + golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 // indirect gorm.io/driver/postgres v1.5.2 ) diff --git a/go.sum b/go.sum index b7f055fe..a26ad40e 100644 --- a/go.sum +++ b/go.sum @@ -196,15 +196,21 @@ github.com/davrux/echo-logrus/v4 v4.0.3 h1:V5bM43A+3PNdpiGC2TS8HKAeaUWQph/j8utG7 github.com/davrux/echo-logrus/v4 v4.0.3/go.mod h1:+1y03d0joOKfwnPN4GSFhh/ViG3newZtYZfAPB6yf+g= github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= github.com/decred/dcrd/lru v1.1.1 h1:kWFDaW0OWx6AD6Ki342c+JPmHbiVdE6rK81pT3fuo/Y= github.com/decred/dcrd/lru v1.1.1/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= github.com/denisenkom/go-mssqldb v0.11.0 h1:9rHa233rhdOyrz2GcP9NM+gi2psgJZ4GWDpL/7ND8HI= github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI= github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug= +github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= +github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= @@ -270,6 +276,12 @@ github.com/go-macaroon-bakery/macaroonpb v1.0.0/go.mod h1:UzrGOcbiwTXISFP2XDLDPj github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.2.0 h1:u0p9s3xLYpZCA1z5JgCkMeB34CKCMMQbM+G8Ii7YD0I= +github.com/gobwas/ws v1.2.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= @@ -625,6 +637,8 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nbd-wtf/go-nostr v0.13.2 h1:w/TgXbkWqkZQsPRZffPZpvR/uskOSSUCGYhtW6I3xPI= github.com/nbd-wtf/go-nostr v0.13.2/go.mod h1:qFFTIxh15H5GGN0WsBI/P73DteqsevnhSEW/yk8nEf4= +github.com/nbd-wtf/go-nostr v0.25.5 h1:CqjicJePCLwPjBNo9N98UfYFnUGrxc7Ts8zwIZgzzwg= +github.com/nbd-wtf/go-nostr v0.25.5/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0= github.com/nbd-wtf/ln-decodepay v1.11.1 h1:MPiT4a4qZ2cKY27Aj0dI8sLFrLz5Ycu72Z3EG1HfPjk= github.com/nbd-wtf/ln-decodepay v1.11.1/go.mod h1:xzBXPaCj/7oRRaui+iYSIxy5LYUjoPfAyAGq2WCyNKk= github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= @@ -691,6 +705,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/puzpuzpuz/xsync/v2 v2.5.1 h1:mVGYAvzDSu52+zaGyNjC+24Xw2bQi3kTr4QJ6N9pIIU= +github.com/puzpuzpuz/xsync/v2 v2.5.1/go.mod h1:gD2H2krq/w52MfPLE+Uy64TzJDVY7lP2znR9qmR35kU= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 h1:VstopitMQi3hZP0fzvnsLmzXZdQGc4bEcgu24cp+d4M= github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= @@ -755,6 +771,12 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tinylib/msgp v1.1.6 h1:i+SbKraHhnrf9M5MYmvQhFnbLhAXSDWF8WWsuyRdocw= github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -914,6 +936,8 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= golang.org/x/exp v0.0.0-20221106115401-f9659909a136 h1:Fq7F/w7MAa1KJ5bt2aJ62ihqp9HDcRuyILskkpIAurw= golang.org/x/exp v0.0.0-20221106115401-f9659909a136/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o= +golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -1080,6 +1104,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= diff --git a/main.go b/main.go index 1904741f..2e6a8116 100644 --- a/main.go +++ b/main.go @@ -174,7 +174,9 @@ func main() { //connect to the relay svc.Logger.Infof("Connecting to the relay: %s", cfg.Relay) - relay, err := nostr.RelayConnect(ctx, cfg.Relay) + + + relay, err := nostr.RelayConnect(ctx, cfg.Relay, nostr.WithNoticeHandler(svc.noticeHandler)) if err != nil { svc.Logger.Fatal(err) } @@ -189,11 +191,19 @@ func main() { //TODO: we can start this loop for multiple relays for { svc.Logger.Info("Subscribing to events") - sub := relay.Subscribe(ctx, svc.createFilters()) + sub, err := relay.Subscribe(ctx, svc.createFilters()) + if err != nil { + //err being non-nil means that we have an error on the websocket error channel. In this case we just try to reconnect. + svc.Logger.WithError(err).Error("Got an error from the relay while subscribing. Reconnecting...") + relay, err = nostr.RelayConnect(ctx, cfg.Relay) + if err != nil { + svc.Logger.Fatal(err) + } + } err = svc.StartSubscription(ctx, sub) if err != nil { //err being non-nil means that we have an error on the websocket error channel. In this case we just try to reconnect. - svc.Logger.WithError(err).Error("Got an error from the relay. Reconnecting...") + svc.Logger.WithError(err).Error("Got an error from the relay while listening to subscription. Reconnecting...") relay, err = nostr.RelayConnect(ctx, cfg.Relay) if err != nil { svc.Logger.Fatal(err) @@ -220,3 +230,7 @@ func (svc *Service) createFilters() nostr.Filters { } return []nostr.Filter{filter} } + +func (svc *Service) noticeHandler(notice string) { + svc.Logger.Infof("Received a notice %s", notice) +} \ No newline at end of file diff --git a/service.go b/service.go index 59281282..7eb5e828 100644 --- a/service.go +++ b/service.go @@ -53,16 +53,17 @@ func (svc *Service) GetUser(c echo.Context) (user *User, err error) { func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error { for { + if (sub.Relay.ConnectionError != nil) { + return sub.Relay.ConnectionError + } select { - case notice := <-sub.Relay.Notices: - svc.Logger.Infof("Received a notice %s", notice) - case conErr := <-sub.Relay.ConnectionError: - return conErr case <-ctx.Done(): svc.Logger.Info("Exiting subscription.") return nil case <-sub.EndOfStoredEvents: - svc.Logger.Info("Received EOS") + if !svc.ReceivedEOS { + svc.Logger.Info("Received EOS") + } svc.ReceivedEOS = true case event := <-sub.Events: go func() { @@ -74,7 +75,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti }).Errorf("Failed to process event: %v", err) } if resp != nil { - status := sub.Relay.Publish(ctx, *resp) + status, publishErr := sub.Relay.Publish(ctx, *resp) nostrEvent := NostrEvent{} result := svc.db.Where("nostr_id = ?", event.ID).First(&nostrEvent) if result.Error != nil { @@ -82,14 +83,14 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "eventId": event.ID, "status": status, "replyEventId": resp.ID, + "publishErr": publishErr, }).Error(result.Error) return } nostrEvent.ReplyId = resp.ID - // https://github.com/nbd-wtf/go-nostr/blob/master/relay.go#L321 - if status == nostr.PublishStatusSucceeded { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED - nostrEvent.RepliedAt = time.Now() + + if status == nostr.PublishStatusFailed || publishErr != nil { + nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -97,9 +98,11 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "status": status, "replyEventId": resp.ID, "appId": nostrEvent.AppId, - }).Info("Published reply") - } else if status == nostr.PublishStatusFailed { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED + "publishErr": publishErr, + }).Info("Failed to publish reply") + } else if status == nostr.PublishStatusSucceeded { + nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED + nostrEvent.RepliedAt = time.Now() svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -107,7 +110,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "status": status, "replyEventId": resp.ID, "appId": nostrEvent.AppId, - }).Info("Failed to publish reply") + }).Info("Published reply") } else { nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED svc.db.Save(&nostrEvent) @@ -218,7 +221,7 @@ func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{ } resp := &nostr.Event{ PubKey: svc.cfg.IdentityPubkey, - CreatedAt: time.Now(), + CreatedAt: nostr.Now(), Kind: NIP_47_RESPONSE_KIND, Tags: nostr.Tags{[]string{"p", initialEvent.PubKey}, []string{"e", initialEvent.ID}}, Content: msg, @@ -292,15 +295,15 @@ func (svc *Service) PublishNip47Info(ctx context.Context, relay *nostr.Relay) er ev := &nostr.Event{} ev.Kind = NIP_47_INFO_EVENT_KIND ev.Content = NIP_47_CAPABILITIES - ev.CreatedAt = time.Now() + ev.CreatedAt = nostr.Now() ev.PubKey = svc.cfg.IdentityPubkey err := ev.Sign(svc.cfg.NostrSecretKey) if err != nil { return err } - status := relay.Publish(ctx, *ev) - if status != nostr.PublishStatusSucceeded { - return fmt.Errorf("Nostr publish not successful: %s", status) + status, err := relay.Publish(ctx, *ev) + if err != nil || status != nostr.PublishStatusSucceeded { + return fmt.Errorf("Nostr publish not successful: %s error: %s", status, err) } return nil } From 52c992117865bae9090a7446f82f1b3d1688ca7f Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Sun, 26 Nov 2023 15:45:17 +0700 Subject: [PATCH 2/2] chore: address code review feedback --- main.go | 10 ++-------- service.go | 31 +++++++++++++++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 2e6a8116..151b56b2 100644 --- a/main.go +++ b/main.go @@ -175,7 +175,6 @@ func main() { //connect to the relay svc.Logger.Infof("Connecting to the relay: %s", cfg.Relay) - relay, err := nostr.RelayConnect(ctx, cfg.Relay, nostr.WithNoticeHandler(svc.noticeHandler)) if err != nil { svc.Logger.Fatal(err) @@ -193,12 +192,7 @@ func main() { svc.Logger.Info("Subscribing to events") sub, err := relay.Subscribe(ctx, svc.createFilters()) if err != nil { - //err being non-nil means that we have an error on the websocket error channel. In this case we just try to reconnect. - svc.Logger.WithError(err).Error("Got an error from the relay while subscribing. Reconnecting...") - relay, err = nostr.RelayConnect(ctx, cfg.Relay) - if err != nil { - svc.Logger.Fatal(err) - } + svc.Logger.Fatal(err) } err = svc.StartSubscription(ctx, sub) if err != nil { @@ -233,4 +227,4 @@ func (svc *Service) createFilters() nostr.Filters { func (svc *Service) noticeHandler(notice string) { svc.Logger.Infof("Received a notice %s", notice) -} \ No newline at end of file +} diff --git a/service.go b/service.go index 597a690b..f00a360a 100644 --- a/service.go +++ b/service.go @@ -53,7 +53,7 @@ func (svc *Service) GetUser(c echo.Context) (user *User, err error) { func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error { for { - if (sub.Relay.ConnectionError != nil) { + if sub.Relay.ConnectionError != nil { return sub.Relay.ConnectionError } select { @@ -75,7 +75,16 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti }).Errorf("Failed to process event: %v", err) } if resp != nil { - status, publishErr := sub.Relay.Publish(ctx, *resp) + status, err := sub.Relay.Publish(ctx, *resp) + if err != nil { + svc.Logger.WithFields(logrus.Fields{ + "eventId": event.ID, + "status": status, + "replyEventId": resp.ID, + }).Errorf("Failed to publish reply: %v", err) + return + } + nostrEvent := NostrEvent{} result := svc.db.Where("nostr_id = ?", event.ID).First(&nostrEvent) if result.Error != nil { @@ -83,14 +92,14 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "eventId": event.ID, "status": status, "replyEventId": resp.ID, - "publishErr": publishErr, }).Error(result.Error) return } nostrEvent.ReplyId = resp.ID - - if status == nostr.PublishStatusFailed || publishErr != nil { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED + + if status == nostr.PublishStatusSucceeded { + nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED + nostrEvent.RepliedAt = time.Now() svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -98,11 +107,9 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "status": status, "replyEventId": resp.ID, "appId": nostrEvent.AppId, - "publishErr": publishErr, - }).Info("Failed to publish reply") - } else if status == nostr.PublishStatusSucceeded { - nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_CONFIRMED - nostrEvent.RepliedAt = time.Now() + }).Info("Published reply") + } else if status == nostr.PublishStatusFailed { + nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_FAILED svc.db.Save(&nostrEvent) svc.Logger.WithFields(logrus.Fields{ "nostrEventId": nostrEvent.ID, @@ -110,7 +117,7 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti "status": status, "replyEventId": resp.ID, "appId": nostrEvent.AppId, - }).Info("Published reply") + }).Info("Failed to publish reply") } else { nostrEvent.State = NOSTR_EVENT_STATE_PUBLISH_UNCONFIRMED svc.db.Save(&nostrEvent)