mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 00:27:14 +00:00
handling all kinds based on number with replaceable, regular and replaceable parameterized in addition to explicity handling kinds
This commit is contained in:
parent
c228833c91
commit
990e47eb2b
@ -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
19
relay/kinds/regular.go
Normal 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
|
||||
}
|
40
relay/kinds/replaceable.go
Normal file
40
relay/kinds/replaceable.go
Normal 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
|
||||
}
|
47
relay/kinds/replaceableParameters.go
Normal file
47
relay/kinds/replaceableParameters.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user