handling all kinds based on number with replaceable, regular and replaceable parameterized in addition to explicity handling kinds

This commit is contained in:
0ceanSlim 2024-07-24 16:37:55 -04:00
parent c228833c91
commit 990e47eb2b
4 changed files with 118 additions and 3 deletions

View File

@ -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)
}

19
relay/kinds/regular.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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
}