diff --git a/relay/handlers/event.go b/relay/handlers/event.go index c690c54..fbbb6bd 100644 --- a/relay/handlers/event.go +++ b/relay/handlers/event.go @@ -6,6 +6,7 @@ import ( "fmt" "grain/relay/db" "grain/relay/handlers/kinds" + "grain/relay/handlers/response" "grain/relay/utils" relay "grain/relay/types" @@ -45,7 +46,7 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) { func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { if !utils.CheckSignature(evt) { - sendOK(ws, evt.ID, false, "invalid: signature verification failed") + response.SendOK(ws, evt.ID, false, "invalid: signature verification failed") return } @@ -77,7 +78,7 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { } if !rateLimiter.AllowEvent(evt.Kind, category) { - sendOK(ws, evt.ID, false, fmt.Sprintf("rate limit exceeded for category: %s", category)) + response.SendOK(ws, evt.ID, false, fmt.Sprintf("rate limit exceeded for category: %s", category)) return } @@ -107,10 +108,10 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { } if err != nil { - sendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err)) + response.SendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err)) return } - sendOK(ws, evt.ID, true, "") + response.SendOK(ws, evt.ID, true, "") } diff --git a/relay/handlers/kinds/kind0.go b/relay/handlers/kinds/kind0.go index 191afad..79c98da 100644 --- a/relay/handlers/kinds/kind0.go +++ b/relay/handlers/kinds/kind0.go @@ -3,6 +3,7 @@ package kinds import ( "context" "fmt" + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/bson" @@ -24,7 +25,7 @@ func HandleKind0(ctx context.Context, evt relay.Event, collection *mongo.Collect if err != mongo.ErrNoDocuments { if existingEvent.CreatedAt >= evt.CreatedAt { // If the existing event is newer or the same, respond with a NOTICE - SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") + response.SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") return nil } } diff --git a/relay/handlers/kinds/kind2.go b/relay/handlers/kinds/kind2.go index 32363ee..db33c86 100644 --- a/relay/handlers/kinds/kind2.go +++ b/relay/handlers/kinds/kind2.go @@ -2,12 +2,13 @@ package kinds import ( "context" + "grain/relay/handlers/response" relay "grain/relay/types" "golang.org/x/net/websocket" ) func HandleKind2Deprecated(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { - SendNotice(ws, evt.PubKey, "kind 2 is deprecated, event not accepted to the relay, please use kind 10002 as defined in NIP-65") + response.SendNotice(ws, evt.PubKey, "kind 2 is deprecated, event not accepted to the relay, please use kind 10002 as defined in NIP-65") return nil } diff --git a/relay/handlers/kinds/replaceable.go b/relay/handlers/kinds/replaceable.go index 90f0612..0214956 100644 --- a/relay/handlers/kinds/replaceable.go +++ b/relay/handlers/kinds/replaceable.go @@ -3,6 +3,7 @@ package kinds import ( "context" "fmt" + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/bson" @@ -21,7 +22,7 @@ func HandleReplaceableKind(ctx context.Context, evt relay.Event, collection *mon 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") + response.SendNotice(ws, evt.PubKey, "relay already has a newer kind 0 event for this pubkey") return nil } } diff --git a/relay/handlers/kinds/replaceableParameters.go b/relay/handlers/kinds/replaceableParameters.go index f947dc3..49be030 100644 --- a/relay/handlers/kinds/replaceableParameters.go +++ b/relay/handlers/kinds/replaceableParameters.go @@ -3,6 +3,7 @@ package kinds import ( "context" "fmt" + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/bson" @@ -28,7 +29,7 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co 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") + response.SendNotice(ws, evt.PubKey, "relay already has a newer event for this pubkey and d tag") return nil } } diff --git a/relay/handlers/kinds/unknown.go b/relay/handlers/kinds/unknown.go index 0b22180..79a2499 100644 --- a/relay/handlers/kinds/unknown.go +++ b/relay/handlers/kinds/unknown.go @@ -2,7 +2,7 @@ package kinds import ( "context" - "encoding/json" + "grain/relay/handlers/response" relay "grain/relay/types" "go.mongodb.org/mongo-driver/mongo" @@ -11,13 +11,8 @@ import ( 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") + response.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 diff --git a/relay/handlers/kinds/notice.go b/relay/handlers/response/notice.go similarity index 93% rename from relay/handlers/kinds/notice.go rename to relay/handlers/response/notice.go index 875c752..b7b153e 100644 --- a/relay/handlers/kinds/notice.go +++ b/relay/handlers/response/notice.go @@ -1,4 +1,4 @@ -package kinds +package response import ( "encoding/json" diff --git a/relay/handlers/ok.go b/relay/handlers/response/ok.go similarity index 74% rename from relay/handlers/ok.go rename to relay/handlers/response/ok.go index c4151a6..b3f5693 100644 --- a/relay/handlers/ok.go +++ b/relay/handlers/response/ok.go @@ -1,4 +1,4 @@ -package handlers +package response import ( "encoding/json" @@ -6,7 +6,7 @@ import ( "golang.org/x/net/websocket" ) -func sendOK(ws *websocket.Conn, eventID string, status bool, message string) { +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))