mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-22 00:27:14 +00:00
handling requests and subscriptions properly
This commit is contained in:
parent
44636c95d9
commit
4389be4009
@ -52,8 +52,7 @@ func HandleReq(ws *websocket.Conn, message []interface{}) {
|
|||||||
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
|
||||||
// TO DO why is this taking a certain kind as an argument for collection???
|
queriedEvents, err := QueryEvents(filters, db.GetClient(), "grain")
|
||||||
queriedEvents, err := QueryEvents(filters, db.GetClient(), "grain", "event-kind1")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error querying events:", err)
|
fmt.Println("Error querying events:", err)
|
||||||
return
|
return
|
||||||
@ -78,20 +77,19 @@ func HandleReq(ws *websocket.Conn, message []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// QueryEvents queries events from the MongoDB collection based on filters
|
|
||||||
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName, collectionName string) ([]relay.Event, error) {
|
|
||||||
collection := client.Database(databaseName).Collection(collectionName)
|
|
||||||
|
|
||||||
|
// QueryEvents queries events from the MongoDB collection based on filters
|
||||||
|
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
|
||||||
var results []relay.Event
|
var results []relay.Event
|
||||||
|
|
||||||
for _, filter := range filters {
|
for _, filter := range filters {
|
||||||
filterBson := bson.M{}
|
filterBson := bson.M{}
|
||||||
|
|
||||||
if len(filter.IDs) > 0 {
|
if len(filter.IDs) > 0 {
|
||||||
filterBson["_id"] = bson.M{"$in": filter.IDs}
|
filterBson["id"] = bson.M{"$in": filter.IDs}
|
||||||
}
|
}
|
||||||
if len(filter.Authors) > 0 {
|
if len(filter.Authors) > 0 {
|
||||||
filterBson["author"] = bson.M{"$in": filter.Authors}
|
filterBson["pubkey"] = bson.M{"$in": filter.Authors}
|
||||||
}
|
}
|
||||||
if len(filter.Kinds) > 0 {
|
if len(filter.Kinds) > 0 {
|
||||||
filterBson["kind"] = bson.M{"$in": filter.Kinds}
|
filterBson["kind"] = bson.M{"$in": filter.Kinds}
|
||||||
@ -99,37 +97,45 @@ func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName, col
|
|||||||
if filter.Tags != nil {
|
if filter.Tags != nil {
|
||||||
for key, values := range filter.Tags {
|
for key, values := range filter.Tags {
|
||||||
if len(values) > 0 {
|
if len(values) > 0 {
|
||||||
filterBson[key] = bson.M{"$in": values}
|
filterBson["tags."+key] = bson.M{"$in": values}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if filter.Since != nil {
|
if filter.Since != nil {
|
||||||
filterBson["created_at"] = bson.M{"$gte": *filter.Since}
|
filterBson["createdat"] = bson.M{"$gte": *filter.Since}
|
||||||
}
|
}
|
||||||
if filter.Until != nil {
|
if filter.Until != nil {
|
||||||
filterBson["created_at"] = bson.M{"$lte": *filter.Until}
|
if filterBson["createdat"] == nil {
|
||||||
|
filterBson["createdat"] = bson.M{"$lte": *filter.Until}
|
||||||
|
} else {
|
||||||
|
filterBson["createdat"].(bson.M)["$lte"] = *filter.Until
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts := options.Find()
|
opts := options.Find().SetSort(bson.D{{Key: "createdat", Value: -1}})
|
||||||
if filter.Limit != nil {
|
if filter.Limit != nil {
|
||||||
opts.SetLimit(int64(*filter.Limit))
|
opts.SetLimit(int64(*filter.Limit))
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor, err := collection.Find(context.TODO(), filterBson, opts)
|
for _, kind := range filter.Kinds {
|
||||||
if err != nil {
|
collectionName := fmt.Sprintf("event-kind%d", kind)
|
||||||
return nil, fmt.Errorf("error querying events: %v", err)
|
collection := client.Database(databaseName).Collection(collectionName)
|
||||||
}
|
cursor, err := collection.Find(context.TODO(), filterBson, opts)
|
||||||
defer cursor.Close(context.TODO())
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error querying events: %v", err)
|
||||||
for cursor.Next(context.TODO()) {
|
}
|
||||||
var event relay.Event
|
defer cursor.Close(context.TODO())
|
||||||
if err := cursor.Decode(&event); err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding event: %v", err)
|
for cursor.Next(context.TODO()) {
|
||||||
|
var event relay.Event
|
||||||
|
if err := cursor.Decode(&event); err != nil {
|
||||||
|
return nil, fmt.Errorf("error decoding event: %v", err)
|
||||||
|
}
|
||||||
|
results = append(results, event)
|
||||||
|
}
|
||||||
|
if err := cursor.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("cursor error: %v", err)
|
||||||
}
|
}
|
||||||
results = append(results, event)
|
|
||||||
}
|
|
||||||
if err := cursor.Err(); err != nil {
|
|
||||||
return nil, fmt.Errorf("cursor error: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user