From 8c83d7333b0159aa3964bb43f1b19370f730161e Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 25 Jul 2024 10:05:33 -0400 Subject: [PATCH] throw out unknown kinds --- relay/handlers/event.go | 2 +- relay/handlers/kinds/unhandled.go | 19 ------------------- relay/handlers/kinds/unknown.go | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) delete mode 100644 relay/handlers/kinds/unhandled.go create mode 100644 relay/handlers/kinds/unknown.go diff --git a/relay/handlers/event.go b/relay/handlers/event.go index bbbc034..c690c54 100644 --- a/relay/handlers/event.go +++ b/relay/handlers/event.go @@ -103,7 +103,7 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { case evt.Kind >= 30000 && evt.Kind < 40000: err = kinds.HandleParameterizedReplaceableKind(ctx, evt, collection, ws) default: - err = kinds.HandleUnknownKind(ctx, evt, collection) + err = kinds.HandleUnknownKind(ctx, evt, collection, ws) } if err != nil { diff --git a/relay/handlers/kinds/unhandled.go b/relay/handlers/kinds/unhandled.go deleted file mode 100644 index 0b1dd0e..0000000 --- a/relay/handlers/kinds/unhandled.go +++ /dev/null @@ -1,19 +0,0 @@ -package kinds - -import ( - "context" - "fmt" - relay "grain/relay/types" - - "go.mongodb.org/mongo-driver/mongo" -) - -func HandleUnknownKind(ctx context.Context, evt relay.Event, collection *mongo.Collection) error { - _, err := collection.InsertOne(ctx, evt) - if err != nil { - return fmt.Errorf("error inserting unknown event into MongoDB: %v", err) - } - - fmt.Println("Inserted unknown event into MongoDB:", evt.ID) - return nil -} diff --git a/relay/handlers/kinds/unknown.go b/relay/handlers/kinds/unknown.go new file mode 100644 index 0000000..0b22180 --- /dev/null +++ b/relay/handlers/kinds/unknown.go @@ -0,0 +1,23 @@ +package kinds + +import ( + "context" + "encoding/json" + relay "grain/relay/types" + + "go.mongodb.org/mongo-driver/mongo" + "golang.org/x/net/websocket" +) + +func HandleUnknownKind(ctx context.Context, evt relay.Event, collection *mongo.Collection, ws *websocket.Conn) error { + // Respond with an OK message indicating the event is not accepted + sendOK(ws, evt.ID, false, "kind is unknown and not accepted") + + // Return nil as there's no error in the process, just that the event is not accepted + return nil +} +func sendOK(ws *websocket.Conn, eventID string, status bool, message string) { + response := []interface{}{"OK", eventID, status, message} + responseBytes, _ := json.Marshal(response) + websocket.Message.Send(ws, string(responseBytes)) +} \ No newline at end of file