diff --git a/server/query.go b/server/query.go index c5dfa02..d68f9d5 100644 --- a/server/query.go +++ b/server/query.go @@ -5,6 +5,7 @@ import ( "fmt" "grain/events" + server "grain/server/types" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -12,7 +13,7 @@ import ( ) // QueryEvents queries events from the MongoDB collection based on filters -func QueryEvents(filters []Filter, client *mongo.Client, databaseName, collectionName string) ([]events.Event, error) { +func QueryEvents(filters []server.Filter, client *mongo.Client, databaseName, collectionName string) ([]events.Event, error) { collection := client.Database(databaseName).Collection(collectionName) var results []events.Event diff --git a/server/server.go b/server/server.go index f5b193f..3c41aaf 100644 --- a/server/server.go +++ b/server/server.go @@ -5,30 +5,14 @@ import ( "encoding/json" "fmt" "grain/events" + server "grain/server/types" "grain/utils" - "time" "go.mongodb.org/mongo-driver/mongo" "golang.org/x/net/websocket" ) -type Subscription struct { - ID string - Filters []Filter -} - -// Filter represents the criteria used to query events -type Filter struct { - IDs []string `json:"ids"` - Authors []string `json:"authors"` - Kinds []int `json:"kinds"` - Tags map[string][]string `json:"tags"` - Since *time.Time `json:"since"` - Until *time.Time `json:"until"` - Limit *int `json:"limit"` -} - -var subscriptions = make(map[string]Subscription) +var subscriptions = make(map[string]server.Subscription) var client *mongo.Client func SetClient(mongoClient *mongo.Client) { @@ -121,7 +105,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) { return } - filters := make([]Filter, len(message)-2) + filters := make([]server.Filter, len(message)-2) for i, filter := range message[2:] { filterData, ok := filter.(map[string]interface{}) if !ok { @@ -129,7 +113,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) { return } - var f Filter + var f server.Filter f.IDs = utils.ToStringArray(filterData["ids"]) f.Authors = utils.ToStringArray(filterData["authors"]) f.Kinds = utils.ToIntArray(filterData["kinds"]) @@ -141,7 +125,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) { filters[i] = f } - subscriptions[subID] = Subscription{ID: subID, Filters: filters} + subscriptions[subID] = server.Subscription{ID: subID, Filters: filters} fmt.Println("Subscription added:", subID) // Query the database with filters and send back the results diff --git a/server/types/filter.go b/server/types/filter.go new file mode 100644 index 0000000..24bc2bd --- /dev/null +++ b/server/types/filter.go @@ -0,0 +1,14 @@ +package server + +import "time" + +// Filter represents the criteria used to query events +type Filter struct { + IDs []string `json:"ids"` + Authors []string `json:"authors"` + Kinds []int `json:"kinds"` + Tags map[string][]string `json:"tags"` + Since *time.Time `json:"since"` + Until *time.Time `json:"until"` + Limit *int `json:"limit"` +} diff --git a/server/types/subscription.go b/server/types/subscription.go new file mode 100644 index 0000000..e8e6cbb --- /dev/null +++ b/server/types/subscription.go @@ -0,0 +1,6 @@ +package server + +type Subscription struct { + ID string + Filters []Filter +} diff --git a/utils/decode.go b/utils/typeConversion.go similarity index 100% rename from utils/decode.go rename to utils/typeConversion.go