grain/server/db/mongo/kinds/delete.go

129 lines
4.2 KiB
Go
Raw Permalink Normal View History

2024-07-27 19:47:37 +00:00
package kinds
import (
"context"
"fmt"
"grain/server/handlers/response"
relay "grain/server/types"
2024-07-27 19:47:37 +00:00
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/net/websocket"
)
func HandleDeleteKind(ctx context.Context, evt relay.Event, dbClient *mongo.Client, ws *websocket.Conn) error {
2024-07-27 19:47:37 +00:00
for _, tag := range evt.Tags {
if len(tag) < 2 {
continue
}
if tag[0] == "e" {
eventID := tag[1]
if err := deleteEventByID(ctx, eventID, evt.PubKey, dbClient); err != nil {
response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err))
return fmt.Errorf("error deleting event with ID %s: %v", eventID, err)
}
} else if tag[0] == "a" {
parts := splitTagA(tag[1])
if len(parts) == 3 {
kind := parts[0]
pubKey := parts[1]
dID := parts[2]
2024-08-08 12:05:37 +00:00
// 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
2024-07-27 19:47:37 +00:00
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)
}
}
}
}
// Store the deletion event
if err := storeEvent(ctx, evt, dbClient); err != nil {
response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err))
return fmt.Errorf("error storing deletion event: %v", err)
}
response.SendOK(ws, evt.ID, true, "")
return nil
}
2024-08-08 12:05:37 +00:00
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
}
2024-07-27 19:47:37 +00:00
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 {
return fmt.Errorf("error listing collections: %v", err)
}
for _, collectionName := range collections {
filter := bson.M{"id": eventID, "pubkey": pubKey}
result, err := dbClient.Database("grain").Collection(collectionName).DeleteOne(ctx, filter)
if err != nil {
return fmt.Errorf("error deleting event from collection %s: %v", collectionName, err)
}
if result.DeletedCount > 0 {
fmt.Printf("Deleted event %s from collection %s\n", eventID, collectionName)
return nil
}
}
return nil
}
func splitTagA(tagA string) []string {
return strings.Split(tagA, ":")
}
func deleteEventByKindPubKeyDID(ctx context.Context, kind string, pubKey string, dID string, createdAt int64, dbClient *mongo.Client) error {
filter := bson.M{"kind": kind, "pubkey": pubKey, "tags.d": dID, "createdat": bson.M{"$lte": createdAt}}
collections, err := dbClient.Database("grain").ListCollectionNames(ctx, bson.M{})
if err != nil {
return fmt.Errorf("error listing collections: %v", err)
}
for _, collectionName := range collections {
_, err := dbClient.Database("grain").Collection(collectionName).DeleteMany(ctx, filter)
if err != nil {
return fmt.Errorf("error deleting events from collection %s: %v", collectionName, err)
}
fmt.Printf("Deleted events with kind %s, pubkey %s, and dID %s from collection %s\n", kind, pubKey, dID, collectionName)
}
return nil
}
func storeEvent(ctx context.Context, evt relay.Event, dbClient *mongo.Client) error {
_, err := dbClient.Database("grain").Collection("event-kind5").InsertOne(ctx, evt)
if err != nil {
return fmt.Errorf("error inserting deletion event: %v", err)
}
fmt.Printf("Stored deletion event %s\n", evt.ID)
return nil
}