Skip to content

Commit

Permalink
nip17: PublishMessage()
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Sep 19, 2024
1 parent 8327310 commit 15aa7ac
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions nip17/nip17.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nip17

import (
"context"
"fmt"
"strings"

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/keyer"
Expand Down Expand Up @@ -30,6 +32,62 @@ func GetDMRelays(ctx context.Context, pubkey string, pool *nostr.SimplePool, rel
return res
}

func PublishMessage(
ctx context.Context,
content string,
tags nostr.Tags,
pool *nostr.SimplePool,
ourRelays []string,
theirRelays []string,
kr keyer.Keyer,
recipientPubKey string,
modify func(*nostr.Event),
) error {
toUs, toThem, err := PrepareMessage(ctx, content, tags, kr, recipientPubKey, modify)
if err != nil {
return fmt.Errorf("failed to prepare message: %w", err)
}

sendErr := fmt.Errorf("failed to send event to ourselves in any of %v", ourRelays)
publishOrAuth := func(ctx context.Context, url string, event nostr.Event) {
r, err := pool.EnsureRelay(url)
if err != nil {
return
}

err = r.Publish(ctx, event)
if strings.HasPrefix(err.Error(), "auth-required:") {
authErr := r.Auth(ctx, func(ae *nostr.Event) error { return kr.SignEvent(ctx, ae) })
if authErr == nil {
err = r.Publish(ctx, event)
}
}

if err != nil {
return
}

sendErr = nil
}

// send to ourselves
for _, url := range ourRelays {
publishOrAuth(ctx, url, toUs)
}

if sendErr != nil {
return sendErr
}

// send to them
sendErr = fmt.Errorf("failed to send event to them in any of %v", theirRelays)
for _, url := range theirRelays {
publishOrAuth(ctx, url, toThem)
}

return sendErr
}

func PrepareMessage(
ctx context.Context,
content string,
Expand Down

0 comments on commit 15aa7ac

Please sign in to comment.