From 990e47eb2bfd3e1a2f3120c6008c285a506715c0 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Wed, 24 Jul 2024 16:37:55 -0400 Subject: [PATCH] handling all kinds based on number with replaceable, regular and replaceable parameterized in addition to explicity handling kinds --- relay/handlers/event.go | 15 +++++++-- relay/kinds/regular.go | 19 +++++++++++ relay/kinds/replaceable.go | 40 +++++++++++++++++++++++ relay/kinds/replaceableParameters.go | 47 ++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 relay/kinds/regular.go create mode 100644 relay/kinds/replaceable.go create mode 100644 relay/kinds/replaceableParameters.go diff --git a/relay/handlers/event.go b/relay/handlers/event.go index 95c7fab..77d7eb3 100644 --- a/relay/handlers/event.go +++ b/relay/handlers/event.go @@ -52,11 +52,20 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { collection := db.GetCollection(evt.Kind) var err error - switch evt.Kind { - case 0: + switch { + case evt.Kind == 0: err = kinds.HandleKind0(ctx, evt, collection, ws) - case 1: + case evt.Kind == 1: err = kinds.HandleKind1(ctx, evt, collection) + case evt.Kind == 3: + err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) + case evt.Kind >= 10000 && evt.Kind < 20000: + err = kinds.HandleReplaceableKind(ctx, evt, collection, ws) + case evt.Kind >= 20000 && evt.Kind < 30000: + // Ephemeral events are not stored + fmt.Println("Ephemeral event received and ignored:", evt.ID) + case evt.Kind >= 30000 && evt.Kind < 40000: + err = kinds.HandleParameterizedReplaceableKind(ctx, evt, collection, ws) default: err = kinds.HandleUnknownKind(ctx, evt, collection) } diff --git a/relay/kinds/regular.go b/relay/kinds/regular.go new file mode 100644 index 0000000..4035315 --- /dev/null +++ b/relay/kinds/regular.go @@ -0,0 +1,19 @@ +package kinds + +import ( + "context" + "fmt" + relay "grain/relay/types" + + "go.mongodb.org/mongo-driver/mongo" +) + +func HandleRegularKind(ctx context.Context, evt relay.Event, collection *mongo.Collection) error { + _, err := collection.InsertOne(ctx, evt) + if err != nil { + return fmt.Errorf("error inserting event kind %d into MongoDB: %v", evt.Kind, err) + } + + fmt.Printf("Inserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + return nil +} \ No newline at end of file diff --git a/relay/kinds/replaceable.go b/relay/kinds/replaceable.go new file mode 100644 index 0000000..ec9ab55 --- /dev/null +++ b/relay/kinds/replaceable.go @@ -0,0 +1,40 @@ +package kinds + +import ( + "context" + "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 HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { + filter := bson.M{"pubkey": evt.PubKey, "kind": evt.Kind} + 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 err != mongo.ErrNoDocuments { + if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { + sendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") + return nil + } + } + + opts := options.Update().SetUpsert(true) + update := bson.M{ + "$set": evt, + } + _, err = collection.UpdateOne(ctx, filter, update, opts) + if err != nil { + return fmt.Errorf("error updating/inserting event kind %d into MongoDB: %v", evt.Kind, err) + } + + fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + return nil +} \ No newline at end of file diff --git a/relay/kinds/replaceableParameters.go b/relay/kinds/replaceableParameters.go new file mode 100644 index 0000000..980e1ac --- /dev/null +++ b/relay/kinds/replaceableParameters.go @@ -0,0 +1,47 @@ +package kinds + +import ( + "context" + "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 HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { + var dTag string + for _, tag := range evt.Tags { + if len(tag) > 0 && tag[0] == "d" { + dTag = tag[1] + break + } + } + filter := bson.M{"pubkey": evt.PubKey, "kind": evt.Kind, "tags.d": dTag} + 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 err != mongo.ErrNoDocuments { + if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { + sendNotice(ws, evt.PubKey, "relay already has a newer event for this pubkey and d tag") + return nil + } + } + + opts := options.Update().SetUpsert(true) + update := bson.M{ + "$set": evt, + } + _, err = collection.UpdateOne(ctx, filter, update, opts) + if err != nil { + return fmt.Errorf("error updating/inserting event kind %d into MongoDB: %v", evt.Kind, err) + } + + fmt.Printf("Upserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID) + return nil +} \ No newline at end of file