Replace was not working as expected, should no be fixed. Mess has to be cleaned up

This commit is contained in:
@s.roertgen 2025-03-28 15:01:25 +01:00
parent 5d0be447f0
commit 47b5e2fb56
3 changed files with 63 additions and 31 deletions

30
main.go
View file

@ -37,9 +37,8 @@ func main() {
khatru.RequestAuth(ctx) khatru.RequestAuth(ctx)
}) })
relay.StoreEvent = append(relay.StoreEvent)
relay.QueryEvents = append(relay.QueryEvents, handleQuery) relay.QueryEvents = append(relay.QueryEvents, handleQuery)
relay.CountEvents = append(relay.CountEvents, dbts.CountEvents) // use badger for counting events -> TODO switch to typesense? relay.CountEvents = append(relay.CountEvents, handleCount)
relay.DeleteEvent = append(relay.DeleteEvent, handleDelete) relay.DeleteEvent = append(relay.DeleteEvent, handleDelete)
relay.ReplaceEvent = append(relay.ReplaceEvent, handleReplaceEvent) relay.ReplaceEvent = append(relay.ReplaceEvent, handleReplaceEvent)
relay.Negentropy = true relay.Negentropy = true
@ -57,16 +56,6 @@ func main() {
http.ListenAndServe(":3334", relay) http.ListenAndServe(":3334", relay)
} }
func handleDelete(ctx context.Context, event *nostr.Event) error {
DeleteNostrEvent(collectionName, event)
return nil
}
func handleReplaceEvent(ctx context.Context, event *nostr.Event) error {
IndexNostrEvent(collectionName, event)
return nil
}
func handleQuery(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) { func handleQuery(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
ch := make(chan *nostr.Event) ch := make(chan *nostr.Event)
@ -84,3 +73,20 @@ func handleQuery(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, e
}() }()
return ch, nil return ch, nil
} }
func handleCount(ctx context.Context, filter nostr.Filter) (int64, error) {
CountEvents(collectionName, filter)
return 0, nil
}
func handleDelete(ctx context.Context, event *nostr.Event) error {
fmt.Println("delete event", event)
DeleteNostrEvent(collectionName, event)
return nil
}
func handleReplaceEvent(ctx context.Context, event *nostr.Event) error {
IndexNostrEvent(collectionName, event)
return nil
}

View file

@ -156,8 +156,9 @@ type NostrMetadata struct {
// AMBMetadata represents the full metadata structure // AMBMetadata represents the full metadata structure
type AMBMetadata struct { type AMBMetadata struct {
// Document ID, same a d-tag // Event ID
ID string `json:"id"` ID string `json:"id"`
// Document ID
D string `json:"d"` D string `json:"d"`
Type string `json:"type"` Type string `json:"type"`
Name string `json:"name"` Name string `json:"name"`

View file

@ -177,9 +177,19 @@ func createCollection(name string) error {
return nil return nil
} }
// TODO Count events
func CountEvents(collectionName string, filter nostr.Filter) (int64, error) {
fmt.Println("filter", filter)
// search by author
// search by d-tag
return 0, nil
}
// Delete a nostr event from the index // Delete a nostr event from the index
func DeleteNostrEvent(collectionName string, event *nostr.Event) error { func DeleteNostrEvent(collectionName string, event *nostr.Event) error {
fmt.Println("handle delete") fmt.Println("deleting event")
d := event.Tags.GetD() d := event.Tags.GetD()
url := fmt.Sprintf( url := fmt.Sprintf(
@ -223,12 +233,19 @@ func IndexNostrEvent(collectionName string, event *nostr.Event) error {
return indexDocument(collectionName, ambData, alreadyIndexed) return indexDocument(collectionName, ambData, alreadyIndexed)
} }
func eventAlreadyIndexed(collectionName string, doc *AMBMetadata) (bool, error) { func eventAlreadyIndexed(collectionName string, doc *AMBMetadata) (*nostr.Event, error) {
url := fmt.Sprintf("%s/collections/%s/documents/%s", typesenseHost, collectionName, url.QueryEscape(doc.ID)) // TODO das muss eine Query nach d-tag und hexkey werden
// als Ergebnis kommt dann die eventID zurück
// wenn es eine Event ID gibt, wird die zuerst bei indexDocument gelöscht und dann die neue angelegt
// function für query event by d-tag and hex key
url := fmt.Sprintf(
"%s/collections/%s/documents/search?filter_by=d:=%s&&eventPubKey:=%s&q=&query_by=d,eventPubKey",
typesenseHost, collectionName, doc.D, doc.EventPubKey)
fmt.Println("url", url)
req, err := http.NewRequest(http.MethodGet, url, nil) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil { if err != nil {
return false, err return nil, err
} }
req.Header.Set("X-TYPESENSE-API-KEY", apiKey) req.Header.Set("X-TYPESENSE-API-KEY", apiKey)
@ -238,26 +255,35 @@ func eventAlreadyIndexed(collectionName string, doc *AMBMetadata) (bool, error)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return false, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return false, fmt.Errorf("failed to index document, status: %d, body: %s", resp.StatusCode, string(body)) return nil, fmt.Errorf("failed to index document, status: %d, body: %s", resp.StatusCode, string(body))
} }
return true, err events, err := parseSearchResponse(body)
if err != nil {
return nil, fmt.Errorf("got parsing search response: %v", err)
}
fmt.Println("response", events)
// Check if we found any events
if len(events) == 0 {
return nil, nil
}
return &events[0], nil
} }
// Index a document in Typesense // Index a document in Typesense
func indexDocument(collectionName string, doc *AMBMetadata, update bool) error { func indexDocument(collectionName string, doc *AMBMetadata, alreadyIndexedEvent *nostr.Event) error {
if update { if alreadyIndexedEvent != nil {
fmt.Println("updating", doc) // delete it
} else { fmt.Println("updating")
fmt.Println("indexing", doc) DeleteNostrEvent(collectionName, alreadyIndexedEvent)
} }
url := fmt.Sprintf("%s/collections/%s/documents", typesenseHost, collectionName) url := fmt.Sprintf("%s/collections/%s/documents", typesenseHost, collectionName)
@ -266,11 +292,7 @@ func indexDocument(collectionName string, doc *AMBMetadata, update bool) error {
if err != nil { if err != nil {
return err return err
} }
method := http.MethodPost method := http.MethodPost
if update {
method = http.MethodPatch
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData)) req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil { if err != nil {
@ -427,9 +449,12 @@ func SearchResources(collectionName, searchStr string) ([]nostr.Event, error) {
return nil, fmt.Errorf("search failed with status code %d: %s", resp.StatusCode, string(body)) return nil, fmt.Errorf("search failed with status code %d: %s", resp.StatusCode, string(body))
} }
// Parse the search response return parseSearchResponse(body)
}
func parseSearchResponse(responseBody []byte) ([]nostr.Event, error) {
var searchResponse SearchResponse var searchResponse SearchResponse
if err := json.Unmarshal(body, &searchResponse); err != nil { if err := json.Unmarshal(responseBody, &searchResponse); err != nil {
return nil, fmt.Errorf("error parsing search response: %v", err) return nil, fmt.Errorf("error parsing search response: %v", err)
} }