mirror of
https://github.com/edufeed-org/amb-relay.git
synced 2025-12-10 00:34:33 +00:00
Replacable Events working
This commit is contained in:
parent
ad554fbba8
commit
49d0c7ff55
3 changed files with 696 additions and 42 deletions
67
main.go
67
main.go
|
|
@ -3,31 +3,33 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"log"
|
||||
|
||||
"github.com/fiatjaf/eventstore/badger"
|
||||
"github.com/fiatjaf/khatru"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
var typesense string = "http://localhost:8108"
|
||||
const (
|
||||
apiKey string = "xyz"
|
||||
typesenseHost string = "http://localhost:8108"
|
||||
collectionName string = "amb-test"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, Go!")
|
||||
|
||||
err:= CheckOrCreateCollection("amb-test")
|
||||
if err != nil {
|
||||
err := CheckOrCreateCollection("amb-test")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to check/create collection: %v", err)
|
||||
}
|
||||
|
||||
relay := khatru.NewRelay()
|
||||
relay.Info.Name = "my relay"
|
||||
// relay.Info.PubKey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||
relay.Info.Description = "this is my custom relay"
|
||||
// relay.Info.Icon = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images"
|
||||
db := badger.BadgerBackend{Path: "/tmp/khatru-badgern-tmp"}
|
||||
if err := db.Init(); err != nil {
|
||||
relay.Info.Name = "my typesense relay"
|
||||
// tsRelay.Info.PubKey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||
relay.Info.Description = "this is the typesense custom relay"
|
||||
// tsRelay.Info.Icon = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images"
|
||||
dbts := badger.BadgerBackend{Path: "/tmp/khatru-badgern-tmp-2"}
|
||||
if err := dbts.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
|
@ -35,28 +37,47 @@ func main() {
|
|||
khatru.RequestAuth(ctx)
|
||||
})
|
||||
|
||||
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent, handleEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents, handleQuery)
|
||||
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
|
||||
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||
relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent)
|
||||
relay.StoreEvent = append(relay.StoreEvent)
|
||||
relay.QueryEvents = append(relay.QueryEvents, handleQuery)
|
||||
relay.CountEvents = append(relay.CountEvents, dbts.CountEvents)
|
||||
relay.DeleteEvent = append(relay.DeleteEvent, dbts.DeleteEvent)
|
||||
relay.ReplaceEvent = append(relay.ReplaceEvent, dbts.ReplaceEvent, handleEvent) // use badger for counting events -> TODO switch to typesense?
|
||||
relay.Negentropy = true
|
||||
|
||||
relay.RejectEvent = append(relay.RejectEvent,
|
||||
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||
if event.Kind != 30142 {
|
||||
return true, "we don't allow these kinds here. It's a 30142 only place."
|
||||
}
|
||||
return false, ""
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println("running on :3334")
|
||||
http.ListenAndServe(":3334", relay)
|
||||
}
|
||||
|
||||
func handleEvent(ctx context.Context, event *nostr.Event) error {
|
||||
fmt.Println("got one", event)
|
||||
// TODO index md-event
|
||||
IndexNostrEvent(collectionName, event)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleQuery(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
|
||||
ch := make(chan *nostr.Event)
|
||||
// TODO do stuff with search nips and look for an edufeed or amb something in the tags
|
||||
fmt.Println("a query!", filter)
|
||||
ch := make(chan *nostr.Event)
|
||||
|
||||
return ch, nil
|
||||
nostrs, err := SearchResources(collectionName, filter.Search)
|
||||
if err != nil {
|
||||
log.Printf("Search failed: %v", err)
|
||||
return ch, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
for _, evt := range nostrs {
|
||||
ch <- &evt
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue