seperate type for server

This commit is contained in:
Chris kerr 2024-07-22 20:51:44 -04:00
parent 0557a7e93b
commit 5a6740d17e
5 changed files with 27 additions and 22 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"grain/events" "grain/events"
server "grain/server/types"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
@ -12,7 +13,7 @@ import (
) )
// QueryEvents queries events from the MongoDB collection based on filters // 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) collection := client.Database(databaseName).Collection(collectionName)
var results []events.Event var results []events.Event

View File

@ -5,30 +5,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"grain/events" "grain/events"
server "grain/server/types"
"grain/utils" "grain/utils"
"time"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
type Subscription struct { var subscriptions = make(map[string]server.Subscription)
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 client *mongo.Client var client *mongo.Client
func SetClient(mongoClient *mongo.Client) { func SetClient(mongoClient *mongo.Client) {
@ -121,7 +105,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
return return
} }
filters := make([]Filter, len(message)-2) filters := make([]server.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{})
if !ok { if !ok {
@ -129,7 +113,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
return return
} }
var f Filter var f server.Filter
f.IDs = utils.ToStringArray(filterData["ids"]) f.IDs = utils.ToStringArray(filterData["ids"])
f.Authors = utils.ToStringArray(filterData["authors"]) f.Authors = utils.ToStringArray(filterData["authors"])
f.Kinds = utils.ToIntArray(filterData["kinds"]) f.Kinds = utils.ToIntArray(filterData["kinds"])
@ -141,7 +125,7 @@ func handleReq(ws *websocket.Conn, message []interface{}) {
filters[i] = f filters[i] = f
} }
subscriptions[subID] = Subscription{ID: subID, Filters: filters} subscriptions[subID] = server.Subscription{ID: subID, Filters: filters}
fmt.Println("Subscription added:", subID) fmt.Println("Subscription added:", subID)
// Query the database with filters and send back the results // Query the database with filters and send back the results

14
server/types/filter.go Normal file
View File

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

View File

@ -0,0 +1,6 @@
package server
type Subscription struct {
ID string
Filters []Filter
}