From 1cfe7c8e948e2dd4b4bdac43747076db50fe1b02 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 8 Aug 2024 08:05:37 -0400 Subject: [PATCH] replace kind 5 with same a tags --- server/handlers/kinds/kind5.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server/handlers/kinds/kind5.go b/server/handlers/kinds/kind5.go index f314b2d..7217c18 100644 --- a/server/handlers/kinds/kind5.go +++ b/server/handlers/kinds/kind5.go @@ -29,6 +29,14 @@ func HandleKind5(ctx context.Context, evt relay.Event, dbClient *mongo.Client, w kind := parts[0] pubKey := parts[1] dID := parts[2] + + // Delete previous kind 5 events with the same "a" tag if they exist + if err := deletePreviousKind5Events(ctx, kind, pubKey, dID, dbClient); err != nil { + response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err)) + return fmt.Errorf("error deleting previous kind 5 events: %v", err) + } + + // Delete target events by kind, pubKey, and dID if err := deleteEventByKindPubKeyDID(ctx, kind, pubKey, dID, evt.CreatedAt, dbClient); err != nil { response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err)) return fmt.Errorf("error deleting events with kind %s, pubkey %s, and dID %s: %v", kind, pubKey, dID, err) @@ -47,6 +55,26 @@ func HandleKind5(ctx context.Context, evt relay.Event, dbClient *mongo.Client, w return nil } +func deletePreviousKind5Events(ctx context.Context, kind string, pubKey string, dID string, dbClient *mongo.Client) error { + collection := dbClient.Database("grain").Collection("event-kind5") + filter := bson.M{ + "tags": bson.M{ + "$elemMatch": bson.M{ + "0": "a", + "1": fmt.Sprintf("%s:%s:%s", kind, pubKey, dID), + }, + }, + } + + _, err := collection.DeleteMany(ctx, filter) + if err != nil { + return fmt.Errorf("error deleting previous kind 5 events from collection event-kind5: %v", err) + } + + fmt.Printf("Deleted previous kind 5 events for kind %s, pubkey %s, and dID %s\n", kind, pubKey, dID) + return nil +} + func deleteEventByID(ctx context.Context, eventID string, pubKey string, dbClient *mongo.Client) error { collections, err := dbClient.Database("grain").ListCollectionNames(ctx, bson.M{}) if err != nil {