Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add publish handler #62

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {

e.POST("/nip47/info", svc.InfoHandler)
e.POST("/nip47", svc.NIP47Handler)
e.POST("/publish", svc.PublishHandler)
e.POST("/subscriptions", svc.SubscriptionHandler)
e.DELETE("/subscriptions/:id", svc.StopSubscriptionHandler)
e.Use(echologrus.Middleware())
Expand Down
5 changes: 5 additions & 0 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ type NIP47Request struct {
SignedEvent *nostr.Event `json:"event"`
}

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

type SubscriptionRequest struct {
RelayUrl string `json:"relayUrl"`
WebhookUrl string `json:"webhookUrl"`
Expand Down
62 changes: 62 additions & 0 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -203,6 +204,67 @@ func (svc *Service) InfoHandler(c echo.Context) error {
}
}

func (svc *Service) PublishHandler(c echo.Context) error {
var requestData PublishRequest
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: fmt.Sprintf("error decoding publish request: %s", err.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 {
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
wg.Add(1)
go func(relayUrl string) {
defer wg.Done()
ctx, cancel := context.WithTimeout(svc.Ctx, 30*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)
}

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

if len(errors) > 0 {
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "errors occurred while publishing to relays: " + strings.Join(errors, "; "),
})
}

return c.JSON(http.StatusOK, "published")
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
}

func (svc *Service) NIP47Handler(c echo.Context) error {
var requestData NIP47Request
if err := c.Bind(&requestData); err != nil {
Expand Down
Loading