mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-06-08 21:59:18 +02:00
handle event responses as per NIP-20
This commit is contained in:
5
storage/errors.go
Normal file
5
storage/errors.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package storage
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrDupEvent = errors.New("duplicate: event already exists")
|
||||
@@ -2,9 +2,8 @@ package postgresql
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/fiatjaf/relayer/storage"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
@@ -21,17 +20,22 @@ func (b *PostgresBackend) SaveEvent(evt *nostr.Event) error {
|
||||
|
||||
// insert
|
||||
tagsj, _ := json.Marshal(evt.Tags)
|
||||
_, err := b.DB.Exec(`
|
||||
res, err := b.DB.Exec(`
|
||||
INSERT INTO event (id, pubkey, created_at, kind, tags, content, sig)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
`, evt.ID, evt.PubKey, evt.CreatedAt.Unix(), evt.Kind, tagsj, evt.Content, evt.Sig)
|
||||
if err != nil {
|
||||
if strings.Index(err.Error(), "UNIQUE") != -1 {
|
||||
// already exists
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to save event %s: %w", evt.ID, err)
|
||||
nr, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if nr == 0 {
|
||||
return storage.ErrDupEvent
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user