refactored ok and notice response

This commit is contained in:
0ceanSlim 2024-07-25 10:19:42 -04:00
parent 8c83d7333b
commit 7365ef5c11
8 changed files with 18 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"grain/relay/db" "grain/relay/db"
"grain/relay/handlers/kinds" "grain/relay/handlers/kinds"
"grain/relay/handlers/response"
"grain/relay/utils" "grain/relay/utils"
relay "grain/relay/types" 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) { func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
if !utils.CheckSignature(evt) { if !utils.CheckSignature(evt) {
sendOK(ws, evt.ID, false, "invalid: signature verification failed") response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
return return
} }
@ -77,7 +78,7 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
} }
if !rateLimiter.AllowEvent(evt.Kind, category) { 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 return
} }
@ -107,10 +108,10 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
} }
if err != nil { 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 return
} }
sendOK(ws, evt.ID, true, "") response.SendOK(ws, evt.ID, true, "")
} }

View File

@ -3,6 +3,7 @@ package kinds
import ( import (
"context" "context"
"fmt" "fmt"
"grain/relay/handlers/response"
relay "grain/relay/types" relay "grain/relay/types"
"go.mongodb.org/mongo-driver/bson" "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 err != mongo.ErrNoDocuments {
if existingEvent.CreatedAt >= evt.CreatedAt { if existingEvent.CreatedAt >= evt.CreatedAt {
// If the existing event is newer or the same, respond with a NOTICE // 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 return nil
} }
} }

View File

@ -2,12 +2,13 @@ package kinds
import ( import (
"context" "context"
"grain/relay/handlers/response"
relay "grain/relay/types" relay "grain/relay/types"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
func HandleKind2Deprecated(ctx context.Context, evt relay.Event, ws *websocket.Conn) error { 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 return nil
} }

View File

@ -3,6 +3,7 @@ package kinds
import ( import (
"context" "context"
"fmt" "fmt"
"grain/relay/handlers/response"
relay "grain/relay/types" relay "grain/relay/types"
"go.mongodb.org/mongo-driver/bson" "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 err != mongo.ErrNoDocuments {
if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { 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 return nil
} }
} }

View File

@ -3,6 +3,7 @@ package kinds
import ( import (
"context" "context"
"fmt" "fmt"
"grain/relay/handlers/response"
relay "grain/relay/types" relay "grain/relay/types"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -28,7 +29,7 @@ func HandleParameterizedReplaceableKind(ctx context.Context, evt relay.Event, co
if err != mongo.ErrNoDocuments { if err != mongo.ErrNoDocuments {
if existingEvent.CreatedAt > evt.CreatedAt || (existingEvent.CreatedAt == evt.CreatedAt && existingEvent.ID < evt.ID) { 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 return nil
} }
} }

View File

@ -2,7 +2,7 @@ package kinds
import ( import (
"context" "context"
"encoding/json" "grain/relay/handlers/response"
relay "grain/relay/types" relay "grain/relay/types"
"go.mongodb.org/mongo-driver/mongo" "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 { 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 // 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 as there's no error in the process, just that the event is not accepted
return nil 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))
}

View File

@ -1,4 +1,4 @@
package kinds package response
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package handlers package response
import ( import (
"encoding/json" "encoding/json"
@ -6,7 +6,7 @@ import (
"golang.org/x/net/websocket" "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} response := []interface{}{"OK", eventID, status, message}
responseBytes, _ := json.Marshal(response) responseBytes, _ := json.Marshal(response)
websocket.Message.Send(ws, string(responseBytes)) websocket.Message.Send(ws, string(responseBytes))