mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 00:27:14 +00:00
imporve requests to the relay
This commit is contained in:
parent
7009533c8d
commit
bfa3e6b08e
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ config.yml
|
|||||||
relay_metadata.json
|
relay_metadata.json
|
||||||
grain.exe
|
grain.exe
|
||||||
/build
|
/build
|
||||||
|
/logs
|
@ -13,11 +13,12 @@ import (
|
|||||||
// QueryEvents queries events from the MongoDB collection(s) based on filters
|
// QueryEvents queries events from the MongoDB collection(s) based on filters
|
||||||
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
|
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
|
||||||
var results []relay.Event
|
var results []relay.Event
|
||||||
|
var combinedFilters []bson.M
|
||||||
|
|
||||||
|
// Build MongoDB filters for each relay.Filter
|
||||||
for _, filter := range filters {
|
for _, filter := range filters {
|
||||||
filterBson := bson.M{}
|
filterBson := bson.M{}
|
||||||
|
|
||||||
// Construct the BSON query based on the filters
|
|
||||||
if len(filter.IDs) > 0 {
|
if len(filter.IDs) > 0 {
|
||||||
filterBson["id"] = bson.M{"$in": filter.IDs}
|
filterBson["id"] = bson.M{"$in": filter.IDs}
|
||||||
}
|
}
|
||||||
@ -45,23 +46,34 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
combinedFilters = append(combinedFilters, filterBson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine all filter conditions using the $or operator
|
||||||
|
query := bson.M{}
|
||||||
|
if len(combinedFilters) > 0 {
|
||||||
|
query["$or"] = combinedFilters
|
||||||
|
}
|
||||||
|
|
||||||
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
|
||||||
|
|
||||||
|
// Apply limit if set for initial query
|
||||||
|
for _, filter := range filters {
|
||||||
if filter.Limit != nil {
|
if filter.Limit != nil {
|
||||||
opts.SetLimit(int64(*filter.Limit))
|
opts.SetLimit(int64(*filter.Limit))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If no specific kinds are specified, query all collections in the database
|
// If no specific kinds are specified, query all collections
|
||||||
if len(filter.Kinds) == 0 {
|
if len(filters[0].Kinds) == 0 {
|
||||||
collections, err := client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
|
collections, err := client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error listing collections: %v", err)
|
return nil, fmt.Errorf("error listing collections: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, collectionName := range collections {
|
for _, collectionName := range collections {
|
||||||
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson)
|
|
||||||
|
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
collection := client.Database(databaseName).Collection(collectionName)
|
||||||
cursor, err := collection.Find(context.TODO(), filterBson, opts)
|
cursor, err := collection.Find(context.TODO(), query, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
||||||
}
|
}
|
||||||
@ -80,12 +92,10 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Query specific collections based on kinds
|
// Query specific collections based on kinds
|
||||||
for _, kind := range filter.Kinds {
|
for _, kind := range filters[0].Kinds {
|
||||||
collectionName := fmt.Sprintf("event-kind%d", kind)
|
collectionName := fmt.Sprintf("event-kind%d", kind)
|
||||||
fmt.Printf("Querying collection: %s with query: %v\n", collectionName, filterBson)
|
|
||||||
|
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
collection := client.Database(databaseName).Collection(collectionName)
|
||||||
cursor, err := collection.Find(context.TODO(), filterBson, opts)
|
cursor, err := collection.Find(context.TODO(), query, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
|
||||||
}
|
}
|
||||||
@ -103,7 +113,6 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,7 @@ func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// processRequest handles the actual processing of each request
|
// processRequest handles the actual processing of each request
|
||||||
func processRequest(ws *websocket.Conn, message []interface{}) {
|
func processRequest(ws *websocket.Conn, message []interface{}) {
|
||||||
if len(message) < 3 {
|
if len(message) < 3 {
|
||||||
@ -58,18 +59,17 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subID, ok := message[1].(string)
|
subID, ok := message[1].(string)
|
||||||
if !ok {
|
if !ok || len(subID) == 0 || len(subID) > 64 {
|
||||||
fmt.Println("Invalid subscription ID format")
|
fmt.Println("Invalid subscription ID format or length")
|
||||||
response.SendClosed(ws, "", "invalid: invalid subscription ID format")
|
response.SendClosed(ws, "", "invalid: subscription ID must be between 1 and 64 characters long")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
// Check the current number of subscriptions for the client
|
// Remove oldest subscription if needed
|
||||||
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
|
if len(subscriptions) >= config.GetConfig().Server.MaxSubscriptionsPerClient {
|
||||||
// Find and remove the oldest subscription (FIFO)
|
|
||||||
var oldestSubID string
|
var oldestSubID string
|
||||||
for id := range subscriptions {
|
for id := range subscriptions {
|
||||||
oldestSubID = id
|
oldestSubID = id
|
||||||
@ -79,7 +79,7 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
|
|||||||
fmt.Println("Dropped oldest subscription:", oldestSubID)
|
fmt.Println("Dropped oldest subscription:", oldestSubID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare filters based on the incoming message
|
// Parse and validate filters
|
||||||
filters := make([]relay.Filter, len(message)-2)
|
filters := make([]relay.Filter, len(message)-2)
|
||||||
for i, filter := range message[2:] {
|
for i, filter := range message[2:] {
|
||||||
filterData, ok := filter.(map[string]interface{})
|
filterData, ok := filter.(map[string]interface{})
|
||||||
@ -101,7 +101,14 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
|
|||||||
filters[i] = f
|
filters[i] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new subscription or update the existing one
|
// Validate filters
|
||||||
|
if !utils.ValidateFilters(filters) {
|
||||||
|
fmt.Println("Invalid filters: hex values not valid")
|
||||||
|
response.SendClosed(ws, subID, "invalid: filters contain invalid hex values")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add subscription
|
||||||
subscriptions[subID] = relay.Subscription{Filters: filters}
|
subscriptions[subID] = relay.Subscription{Filters: filters}
|
||||||
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
|
fmt.Printf("Subscription updated: %s with %d filters\n", subID, len(filters))
|
||||||
|
|
||||||
@ -113,13 +120,11 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the number of events retrieved
|
|
||||||
fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID)
|
fmt.Printf("Retrieved %d events for subscription %s\n", len(queriedEvents), subID)
|
||||||
if len(queriedEvents) == 0 {
|
if len(queriedEvents) == 0 {
|
||||||
fmt.Printf("No matching events found for subscription %s\n", subID)
|
fmt.Printf("No matching events found for subscription %s\n", subID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send each event back to the client
|
|
||||||
for _, evt := range queriedEvents {
|
for _, evt := range queriedEvents {
|
||||||
msg := []interface{}{"EVENT", subID, evt}
|
msg := []interface{}{"EVENT", subID, evt}
|
||||||
msgBytes, _ := json.Marshal(msg)
|
msgBytes, _ := json.Marshal(msg)
|
||||||
@ -131,7 +136,7 @@ func processRequest(ws *websocket.Conn, message []interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indicate end of stored events
|
// Send EOSE message
|
||||||
eoseMsg := []interface{}{"EOSE", subID}
|
eoseMsg := []interface{}{"EOSE", subID}
|
||||||
eoseBytes, _ := json.Marshal(eoseMsg)
|
eoseBytes, _ := json.Marshal(eoseMsg)
|
||||||
err = websocket.Message.Send(ws, string(eoseBytes))
|
err = websocket.Message.Send(ws, string(eoseBytes))
|
||||||
|
38
server/utils/validateFilter.go
Normal file
38
server/utils/validateFilter.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
relay "grain/server/types"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// isValidHex validates if the given string is a 64-character lowercase hex string
|
||||||
|
func isValidHex(str string) bool {
|
||||||
|
return len(str) == 64 && regexp.MustCompile(`^[a-f0-9]{64}$`).MatchString(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateFilters ensures the IDs, Authors, and Tags follow the correct hex format
|
||||||
|
func ValidateFilters(filters []relay.Filter) bool {
|
||||||
|
for _, f := range filters {
|
||||||
|
// Validate IDs
|
||||||
|
for _, id := range f.IDs {
|
||||||
|
if !isValidHex(id) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Validate Authors
|
||||||
|
for _, author := range f.Authors {
|
||||||
|
if !isValidHex(author) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Validate Tags
|
||||||
|
for _, tags := range f.Tags {
|
||||||
|
for _, tag := range tags {
|
||||||
|
if !isValidHex(tag) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user