Skip to content

Commit

Permalink
chore: only allow one relay instead of multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed May 28, 2024
1 parent 2f4ffa1 commit a392749
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ type NIP47Request struct {
}

type PublishRequest struct {
Relays []string `json:"relays"`
RelayUrl string `json:"relayUrl"`
SignedEvent *nostr.Event `json:"event"`
}

Expand Down
63 changes: 27 additions & 36 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -212,56 +211,48 @@ func (svc *Service) PublishHandler(c echo.Context) error {
})
}

if len(requestData.Relays) == 0 {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "relay(s) not specified",
})
}

if (requestData.SignedEvent == nil) {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "signed event is empty",
})
}

var wg sync.WaitGroup
errorCh := make(chan error, len(requestData.Relays))

for _, relayUrl := range requestData.Relays {
wg.Add(1)
go func(relayUrl string) {
defer wg.Done()
ctx, cancel := context.WithTimeout(svc.Ctx, 30*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(c.Request().Context(), 90*time.Second)
defer cancel()

relay, isCustomRelay, err := svc.getRelayConnection(ctx, relayUrl)
if err != nil {
errorCh <- fmt.Errorf("error connecting to relay %s: %v", relayUrl, err)
return
}
if isCustomRelay {
defer relay.Close()
}
err = relay.Publish(ctx, *requestData.SignedEvent)
if err != nil {
errorCh <- fmt.Errorf("error publishing to relay %s: %v", relayUrl, err)
}
}(relayUrl)
relay, isCustomRelay, err := svc.getRelayConnection(ctx, requestData.RelayUrl)
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "error connecting to relay: " + err.Error(),
})
}

wg.Wait()
close(errorCh)
var errors []string
for err := range errorCh {
errors = append(errors, err.Error())
if isCustomRelay {
defer relay.Close()
}

if len(errors) > 0 {
svc.Logger.WithFields(logrus.Fields{
"eventId": requestData.SignedEvent.ID,
"relayUrl": requestData.RelayUrl,
}).Info("Publishing event...")

err = relay.Publish(ctx, *requestData.SignedEvent)
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"eventId": requestData.SignedEvent.ID,
"relayUrl": requestData.RelayUrl,
}).Error("Failed to publish event")

return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "errors occurred while publishing to relays: " + strings.Join(errors, "; "),
Message: "error publishing the event: " + err.Error(),
})
}

svc.Logger.WithFields(logrus.Fields{
"eventId": requestData.SignedEvent.ID,
"relayUrl": requestData.RelayUrl,
}).Info("Published event")

return c.JSON(http.StatusOK, "published")
}

Expand Down

0 comments on commit a392749

Please sign in to comment.