mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-06-12 10:00:53 +02:00
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/fiatjaf/go-nostr"
|
|
"github.com/fiatjaf/relayer"
|
|
"github.com/fiatjaf/relayer/storage/postgresql"
|
|
"github.com/kelseyhightower/envconfig"
|
|
)
|
|
|
|
type Relay struct {
|
|
PostgresDatabase string `envconfig:"POSTGRESQL_DATABASE"`
|
|
}
|
|
|
|
func (r *Relay) Name() string {
|
|
return "BasicRelay"
|
|
}
|
|
|
|
func (r *Relay) Storage() relayer.Storage {
|
|
return &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase}
|
|
}
|
|
|
|
func (r *Relay) OnInitialized() {}
|
|
|
|
func (r *Relay) Init() error {
|
|
err := envconfig.Process("", r)
|
|
if err != nil {
|
|
return fmt.Errorf("couldn't process envconfig: %w", err)
|
|
}
|
|
|
|
// every hour, delete all very old events
|
|
go func() {
|
|
db := r.Storage().(*postgresql.PostgresBackend)
|
|
|
|
for {
|
|
time.Sleep(60 * time.Minute)
|
|
db.DB.Exec(`DELETE FROM event WHERE created_at < $1`, time.Now().AddDate(0, -3, 0)) // 3 months
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Relay) AcceptEvent(evt *nostr.Event) bool {
|
|
// block events that are too large
|
|
jsonb, _ := json.Marshal(evt)
|
|
if len(jsonb) > 10000 {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (r *Relay) BeforeSave(evt *nostr.Event) {
|
|
// do nothing
|
|
}
|
|
|
|
func (r *Relay) AfterSave(evt *nostr.Event) {
|
|
// delete all but the 100 most recent ones for each key
|
|
r.Storage().(*postgresql.PostgresBackend).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 100 LIMIT 1
|
|
)`, evt.PubKey, evt.Kind)
|
|
}
|
|
|
|
func main() {
|
|
relayer.Start(&Relay{})
|
|
}
|