throw out unknown kinds

This commit is contained in:
0ceanSlim 2024-07-25 10:05:33 -04:00
parent d5f2366ff8
commit 8c83d7333b
3 changed files with 24 additions and 20 deletions

View File

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

View File

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

View File

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