mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-04-04 01:48:18 +02:00
implement nip-16 on closed and basic relays.
This commit is contained in:
parent
ca1d1ce58f
commit
d2ce4dbf54
@ -15,7 +15,7 @@ import (
|
||||
|
||||
func (b *BasicRelay) QueryEvents(filter *nostr.Filter) (events []nostr.Event, err error) {
|
||||
var conditions []string
|
||||
var params []interface{}
|
||||
var params []any
|
||||
|
||||
if filter == nil {
|
||||
err = errors.New("filter cannot be null")
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fiatjaf/go-nostr"
|
||||
)
|
||||
@ -16,22 +17,18 @@ func (b *BasicRelay) SaveEvent(evt *nostr.Event) error {
|
||||
}
|
||||
|
||||
// react to different kinds of events
|
||||
switch evt.Kind {
|
||||
case nostr.KindSetMetadata:
|
||||
// delete past set_metadata events from this user
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = 0`, evt.PubKey)
|
||||
case nostr.KindRecommendServer:
|
||||
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)
|
||||
} 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 = $2`,
|
||||
evt.PubKey, evt.Content)
|
||||
case nostr.KindContactList:
|
||||
// delete past contact lists from this same pubkey
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = 3`, evt.PubKey)
|
||||
default:
|
||||
// delete all but the 10 most recent ones
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND content = $3`,
|
||||
evt.PubKey, evt.Kind, evt.Content)
|
||||
} else {
|
||||
// delete all but the 100 most recent ones
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND created_at < (
|
||||
SELECT created_at FROM event WHERE pubkey = $1
|
||||
ORDER BY created_at DESC OFFSET 10 LIMIT 1
|
||||
ORDER BY created_at DESC OFFSET 100 LIMIT 1
|
||||
)`,
|
||||
evt.PubKey, evt.Kind)
|
||||
}
|
||||
@ -51,5 +48,11 @@ func (b *BasicRelay) SaveEvent(evt *nostr.Event) error {
|
||||
return fmt.Errorf("failed to save event %s: %w", evt.ID, err)
|
||||
}
|
||||
|
||||
// delete ephemeral events after a minute
|
||||
go func() {
|
||||
time.Sleep(75 * time.Second)
|
||||
b.DB.Exec("DELETE FROM event WHERE id = $1", evt.ID)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
|
||||
func (b *ClosedRelay) QueryEvents(filter *nostr.Filter) (events []nostr.Event, err error) {
|
||||
var conditions []string
|
||||
var params []interface{}
|
||||
var params []any
|
||||
|
||||
if filter == nil {
|
||||
err = errors.New("filter cannot be null")
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fiatjaf/go-nostr"
|
||||
)
|
||||
@ -19,24 +20,15 @@ func (b *ClosedRelay) SaveEvent(evt *nostr.Event) error {
|
||||
|
||||
save:
|
||||
// react to different kinds of events
|
||||
switch evt.Kind {
|
||||
case nostr.KindSetMetadata:
|
||||
// delete past set_metadata events from this user
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = 0`, evt.PubKey)
|
||||
case nostr.KindRecommendServer:
|
||||
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)
|
||||
} 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 = $2`,
|
||||
evt.PubKey, evt.Content)
|
||||
case nostr.KindContactList:
|
||||
// delete past contact lists from this same pubkey
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = 3`, evt.PubKey)
|
||||
default:
|
||||
// delete all but the 10 most recent ones
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND created_at < (
|
||||
SELECT created_at FROM event WHERE pubkey = $1
|
||||
ORDER BY created_at DESC OFFSET 10 LIMIT 1
|
||||
)`,
|
||||
evt.PubKey, evt.Kind)
|
||||
b.DB.Exec(`DELETE FROM event WHERE pubkey = $1 AND kind = $2 AND content = $3`,
|
||||
evt.PubKey, evt.Kind, evt.Content)
|
||||
} else {
|
||||
// do not delete any, this is a closed relay so we trust everybody to not spam
|
||||
}
|
||||
|
||||
// insert
|
||||
@ -54,5 +46,11 @@ save:
|
||||
return fmt.Errorf("failed to save event %s: %w", evt.ID, err)
|
||||
}
|
||||
|
||||
// delete ephemeral events after a minute
|
||||
go func() {
|
||||
time.Sleep(75 * time.Second)
|
||||
b.DB.Exec("DELETE FROM event WHERE id = $1", evt.ID)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
10
go.mod
10
go.mod
@ -1,6 +1,6 @@
|
||||
module github.com/fiatjaf/relayer
|
||||
|
||||
go 1.15
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/fiatjaf/go-nostr v0.7.3
|
||||
@ -12,3 +12,11 @@ require (
|
||||
github.com/rs/cors v1.7.0
|
||||
github.com/rs/zerolog v1.20.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
|
||||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
|
||||
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
|
||||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
|
||||
github.com/valyala/fastjson v1.6.3 // indirect
|
||||
)
|
||||
|
1
go.sum
1
go.sum
@ -2,7 +2,6 @@ github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUq
|
||||
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
github.com/btcsuite/btcd v0.22.1 h1:CnwP9LM/M9xuRrGSCGeMVs9iv09uMqwsVX7EeIpgV2c=
|
||||
github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y=
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
|
||||
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
|
||||
|
Loading…
x
Reference in New Issue
Block a user