mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-06-04 09:41:28 +02:00
migrate all built-in storage backends.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
@@ -8,30 +9,30 @@ import (
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
func (b *SQLite3Backend) SaveEvent(evt *nostr.Event) error {
|
||||
func (b *SQLite3Backend) 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
|
||||
d := evt.Tags.GetFirst([]string{"d"})
|
||||
if d != nil {
|
||||
tagsLike := fmt.Sprintf(`%%"d","%s"%%`, d.Value())
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND tags LIKE $3`, evt.PubKey, evt.Kind, tagsLike)
|
||||
b.DB.ExecContext(ctx, `DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND tags LIKE $3`, evt.PubKey, evt.Kind, tagsLike)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
`, 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 *SQLite3Backend) SaveEvent(evt *nostr.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *SQLite3Backend) BeforeSave(evt *nostr.Event) {
|
||||
func (b *SQLite3Backend) BeforeSave(ctx context.Context, evt *nostr.Event) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user