mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-06-02 08:43:01 +02:00
migrate all built-in storage backends.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package postgresql
|
||||
|
||||
func (b PostgresBackend) DeleteEvent(id string, pubkey string) error {
|
||||
_, err := b.DB.Exec("DELETE FROM event WHERE id = $1 AND pubkey = $2", id, pubkey)
|
||||
import "context"
|
||||
|
||||
func (b PostgresBackend) DeleteEvent(ctx context.Context, id string, pubkey string) error {
|
||||
_, err := b.DB.ExecContext(ctx, "DELETE FROM event WHERE id = $1 AND pubkey = $2", id, pubkey)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"github.com/fiatjaf/relayer/v2"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/jmoiron/sqlx/reflectx"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var _ relayer.Storage = (*PostgresBackend)(nil)
|
||||
|
||||
func (b *PostgresBackend) Init() error {
|
||||
db, err := sqlx.Connect("postgres", b.DatabaseURL)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
func (b PostgresBackend) QueryEvents(filter *nostr.Filter) (events []nostr.Event, err error) {
|
||||
func (b PostgresBackend) QueryEvents(ctx context.Context, filter *nostr.Filter) (ch chan *nostr.Event, err error) {
|
||||
ch = make(chan *nostr.Event)
|
||||
|
||||
var conditions []string
|
||||
var params []any
|
||||
|
||||
@@ -116,11 +118,11 @@ func (b PostgresBackend) QueryEvents(filter *nostr.Filter) (events []nostr.Event
|
||||
|
||||
if filter.Since != nil {
|
||||
conditions = append(conditions, "created_at > ?")
|
||||
params = append(params, filter.Since.Unix())
|
||||
params = append(params, filter.Since)
|
||||
}
|
||||
if filter.Until != nil {
|
||||
conditions = append(conditions, "created_at < ?")
|
||||
params = append(params, filter.Until.Unix())
|
||||
params = append(params, filter.Until)
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -145,19 +147,21 @@ func (b PostgresBackend) QueryEvents(filter *nostr.Filter) (events []nostr.Event
|
||||
return nil, fmt.Errorf("failed to fetch events using query %q: %w", query, err)
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var evt nostr.Event
|
||||
var timestamp int64
|
||||
err := rows.Scan(&evt.ID, &evt.PubKey, ×tamp,
|
||||
&evt.Kind, &evt.Tags, &evt.Content, &evt.Sig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan row: %w", err)
|
||||
go func() {
|
||||
defer rows.Close()
|
||||
defer close(ch)
|
||||
for rows.Next() {
|
||||
var evt nostr.Event
|
||||
var timestamp int64
|
||||
err := rows.Scan(&evt.ID, &evt.PubKey, ×tamp,
|
||||
&evt.Kind, &evt.Tags, &evt.Content, &evt.Sig)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
evt.CreatedAt = nostr.Timestamp(timestamp)
|
||||
ch <- &evt
|
||||
}
|
||||
evt.CreatedAt = time.Unix(timestamp, 0)
|
||||
events = append(events, evt)
|
||||
}
|
||||
}()
|
||||
|
||||
return events, nil
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/fiatjaf/relayer/storage"
|
||||
"github.com/fiatjaf/relayer/v2/storage"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
func (b *PostgresBackend) SaveEvent(evt *nostr.Event) error {
|
||||
func (b *PostgresBackend) SaveEvent(ctx context.Context, evt *nostr.Event) error {
|
||||
// react to different kinds of events
|
||||
if evt.Kind == nostr.KindSetMetadata || evt.Kind == nostr.KindContactList || (10000 <= evt.Kind && evt.Kind < 20000) {
|
||||
// delete past events from this user
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2`, evt.PubKey, evt.Kind)
|
||||
b.DB.ExecContext(ctx, `DELETE FROM event WHERE pubkey = $1 AND kind = $2`, evt.PubKey, evt.Kind)
|
||||
} else if evt.Kind == nostr.KindRecommendServer {
|
||||
// delete past recommend_server events equal to this one
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND content = $3`,
|
||||
b.DB.ExecContext(ctx, `DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND content = $3`,
|
||||
evt.PubKey, evt.Kind, evt.Content)
|
||||
} else if evt.Kind >= 30000 && evt.Kind < 40000 {
|
||||
// NIP-33
|
||||
@@ -27,11 +28,11 @@ func (b *PostgresBackend) SaveEvent(evt *nostr.Event) error {
|
||||
|
||||
// insert
|
||||
tagsj, _ := json.Marshal(evt.Tags)
|
||||
res, err := b.DB.Exec(`
|
||||
res, err := b.DB.ExecContext(ctx, `
|
||||
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)
|
||||
`, evt.ID, evt.PubKey, evt.CreatedAt, evt.Kind, tagsj, evt.Content, evt.Sig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -48,7 +49,7 @@ func (b *PostgresBackend) SaveEvent(evt *nostr.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *PostgresBackend) BeforeSave(evt *nostr.Event) {
|
||||
func (b *PostgresBackend) BeforeSave(ctx context.Context, evt *nostr.Event) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user