mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 08:37:13 +00:00
seperate type for server
This commit is contained in:
parent
0557a7e93b
commit
5a6740d17e
@ -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
|
||||||
|
@ -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
14
server/types/filter.go
Normal 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"`
|
||||||
|
}
|
6
server/types/subscription.go
Normal file
6
server/types/subscription.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
type Subscription struct {
|
||||||
|
ID string
|
||||||
|
Filters []Filter
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user