first commit
This commit is contained in:
36
internal/nostr/publish.go
Normal file
36
internal/nostr/publish.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
gn "github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
var ErrNoRelayAccepted = errors.New("no relay accepted event")
|
||||
|
||||
// Publish sends an already-signed event to all relays in the pool.
|
||||
// Success if at least one relay accepts.
|
||||
func Publish(ctx context.Context, p *Pool, ev *gn.Event) error {
|
||||
var lastErr error
|
||||
accepted := 0
|
||||
for _, url := range p.URLs() {
|
||||
r, err := p.Connect(ctx, url)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
if err := r.Publish(ctx, *ev); err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
accepted++
|
||||
}
|
||||
if accepted == 0 {
|
||||
if lastErr != nil {
|
||||
return lastErr
|
||||
}
|
||||
return ErrNoRelayAccepted
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user