only upserting kind0 event if created at is newer

This commit is contained in:
0ceanSlim 2024-07-24 10:04:45 -04:00
parent 4389be4009
commit 8ad674401b
3 changed files with 30 additions and 8 deletions

View File

@ -1,6 +1,6 @@
# GRAIN 🌾
**Go Relay Archetecture for Implementing Nostr**
## Go Relay Archetecture for Implementing Nostr
GRAIN is an open-source Nostr relay implementation written in Go. This project aims to provide a robust and efficient Nostr relay that currently supports the NIP-01 of the nostr protocol.
@ -29,8 +29,7 @@ cp config.example.yml config.yml
### TODO
- Handle more kinds
- Fix Request Query to handle and serve unhandled events by kind
- Explicitely Handle more kinds
- create whitelist/blacklist functionality
for:
- valid nip05 domain

View File

@ -54,7 +54,7 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
var err error
switch evt.Kind {
case 0:
err = kinds.HandleKind0(ctx, evt, collection)
err = kinds.HandleKind0(ctx, evt, collection, ws)
case 1:
err = kinds.HandleKind1(ctx, evt, collection)
default:

View File

@ -2,18 +2,35 @@ package kinds
import (
"context"
"encoding/json"
"fmt"
relay "grain/relay/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/net/websocket"
)
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection) error {
// Replace the existing event if it has the same pubkey
func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
// Find the existing event with the same pubkey
filter := bson.M{"pubkey": evt.PubKey}
var existingEvent relay.Event
err := collection.FindOne(ctx, filter).Decode(&existingEvent)
if err != nil && err != mongo.ErrNoDocuments {
return fmt.Errorf("Error finding existing event: %v", err)
}
// If an existing event is found, compare the created_at times
if err != mongo.ErrNoDocuments {
if existingEvent.CreatedAt >= evt.CreatedAt {
// If the existing event is newer or the same, respond with a NOTICE
sendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey")
return nil
}
}
// Replace the existing event if it has the same pubkey
update := bson.M{
"$set": bson.M{
"id": evt.ID,
@ -26,7 +43,7 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect
}
opts := options.Update().SetUpsert(true) // Insert if not found
_, err := collection.UpdateOne(ctx, filter, update, opts)
_, err = collection.UpdateOne(ctx, filter, update, opts)
if err != nil {
return fmt.Errorf("Error updating/inserting event kind 0 into MongoDB: %v", err)
}
@ -34,3 +51,9 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect
fmt.Println("Upserted event kind 0 into MongoDB:", evt.ID)
return nil
}
func sendNotice(ws *websocket.Conn, pubKey, message string) {
notice := []interface{}{"NOTICE", pubKey, message}
noticeBytes, _ := json.Marshal(notice)
websocket.Message.Send(ws, string(noticeBytes))
}