2024-07-24 20:37:55 +00:00
|
|
|
package kinds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-31 18:12:33 +00:00
|
|
|
"grain/server/handlers/response"
|
|
|
|
relay "grain/server/types"
|
2024-07-24 20:37:55 +00:00
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
2024-09-18 23:51:53 +00:00
|
|
|
// HandleParameterizedReplaceableKind handles parameterized replaceable events based on NIP-01 rules
|
2024-07-24 20:37:55 +00:00
|
|
|
func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error {
|
2024-09-18 23:54:35 +00:00
|
|
|
// Step 1: Extract the dTag from the event's tags
|
2024-07-24 20:37:55 +00:00
|
|
|
var dTag string
|
|
|
|
for _, tag := range evt.Tags {
|
|
|
|
if len(tag) > 0 && tag[0] == "d" {
|
|
|
|
dTag = tag[1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-09-18 23:51:53 +00:00
|
|
|
|
2024-09-18 23:54:35 +00:00
|
|
|
// Step 2: Create a filter to find the existing event based on pubkey, kind, and dTag
|
2024-07-24 20:37:55 +00:00
|
|
|
filter := bson.M{"pubkey": evt.PubKey, "kind": evt.Kind, "tags.d": dTag}
|
2024-09-18 23:51:53 +00:00
|
|
|
|
|
|
|
// Step 3: Find the existing event from the database
|
2024-07-24 20:37:55 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-18 23:54:35 +00:00
|
|
|
// Step 4: If an existing event is found, check if it should be replaced based on created_at
|
2024-07-24 20:37:55 +00:00
|
|
|
if err != mongo.ErrNoDocuments {
|
|
|
|
if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) {
|
2024-09-18 23:51:53 +00:00
|
|
|
response.SendOK(ws, evt.ID, false, "blocked: relay already has a newer event for this pubkey and dTag")
|
2024-07-24 20:37:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-09-18 23:51:53 +00:00
|
|
|
|
2024-09-18 23:54:35 +00:00
|
|
|
// Step 5: If the new event is valid, delete the older event before inserting the new one
|
|
|
|
_, err := collection.DeleteOne(ctx, filter)
|
2024-09-18 23:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting the older event: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Deleted older event with ID: %s\n", existingEvent.ID)
|
2024-07-24 20:37:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 00:00:14 +00:00
|
|
|
// Step 6: Insert the new event (replaceable)
|
|
|
|
_, err = collection.InsertOne(ctx, evt)
|
2024-07-24 20:37:55 +00:00
|
|
|
if err != nil {
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendOK(ws, evt.ID, false, "error: could not connect to the database")
|
2024-09-19 00:00:14 +00:00
|
|
|
return fmt.Errorf("error inserting event kind %d into MongoDB: %v", evt.Kind, err)
|
2024-07-24 20:37:55 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 00:00:14 +00:00
|
|
|
fmt.Printf("Inserted event kind %d into MongoDB: %s\n", evt.Kind, evt.ID)
|
2024-07-27 14:06:34 +00:00
|
|
|
response.SendOK(ws, evt.ID, true, "")
|
2024-07-24 20:37:55 +00:00
|
|
|
return nil
|
2024-07-25 03:04:26 +00:00
|
|
|
}
|