From 44f01d0b401841d8ceec298b795797a481f75bbe Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Wed, 18 Sep 2024 20:04:57 -0400 Subject: [PATCH] change in parameterized replaceable handling --- server/db/mongo/kinds/replaceableParameters.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/db/mongo/kinds/replaceableParameters.go b/server/db/mongo/kinds/replaceableParameters.go index f1ccd57..af1b881 100644 --- a/server/db/mongo/kinds/replaceableParameters.go +++ b/server/db/mongo/kinds/replaceableParameters.go @@ -23,23 +23,23 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co } // Step 2: Create a filter to find the existing event based on pubkey, kind, and dTag - filter := bson.M{"pubkey": evt.PubKey, "kind": evt.Kind, "tags.d": dTag} + filter := bson.M{"pubkey": evt.PubKey, "kind": evt.Kind, "tags": bson.M{"$elemMatch": bson.M{"0": "d", "1": dTag}}} - // Step 3: Find the existing event from the database + // Step 3: Check if an existing event is found 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) } - // Step 4: If an existing event is found, check if it should be replaced based on created_at + // Step 4: If an existing event is found, compare created_at and id to decide if it should be replaced if err != mongo.ErrNoDocuments { if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { response.SendOK(ws, evt.ID, false, "blocked: relay already has a newer event for this pubkey and dTag") return nil } - // Step 5: If the new event is valid, delete the older event before inserting the new one + // Step 5: Delete the older event before inserting the new one _, err := collection.DeleteOne(ctx, filter) if err != nil { return fmt.Errorf("error deleting the older event: %v", err) @@ -47,10 +47,10 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co fmt.Printf("Deleted older event with ID: %s\n", existingEvent.ID) } - // Step 6: Insert the new event (replaceable) + // Step 6: Insert the new event (without upsert since we already deleted the old one) _, err = collection.InsertOne(ctx, evt) if err != nil { - response.SendOK(ws, evt.ID, false, "error: could not connect to the database") + response.SendOK(ws, evt.ID, false, "error: could not insert the new event into the database") return fmt.Errorf("error inserting event kind %d into MongoDB: %v", evt.Kind, err) }