grain/server/db/mongo/queryMongo.go

113 lines
3.1 KiB
Go
Raw Normal View History

package mongo
2024-09-02 15:56:52 +00:00
import (
"context"
"fmt"
relay "grain/server/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// QueryEvents queries events from the MongoDB collection(s) based on filters
func QueryEvents(filters []relay.Filter, client *mongo.Client, databaseName string) ([]relay.Event, error) {
var results []relay.Event
2024-09-17 12:44:18 +00:00
var combinedFilters []bson.M
2024-09-02 15:56:52 +00:00
2024-09-17 12:44:18 +00:00
// Build MongoDB filters for each relay.Filter
2024-09-02 15:56:52 +00:00
for _, filter := range filters {
filterBson := bson.M{}
if len(filter.IDs) > 0 {
filterBson["id"] = bson.M{"$in": filter.IDs}
}
if len(filter.Authors) > 0 {
filterBson["pubkey"] = bson.M{"$in": filter.Authors}
}
if len(filter.Kinds) > 0 {
filterBson["kind"] = bson.M{"$in": filter.Kinds}
}
if filter.Tags != nil {
for key, values := range filter.Tags {
if len(values) > 0 {
filterBson["tags."+key] = bson.M{"$in": values}
}
}
}
if filter.Since != nil {
filterBson["created_at"] = bson.M{"$gte": *filter.Since}
}
if filter.Until != nil {
if filterBson["created_at"] == nil {
filterBson["created_at"] = bson.M{"$lte": *filter.Until}
} else {
filterBson["created_at"].(bson.M)["$lte"] = *filter.Until
}
}
2024-09-17 12:44:18 +00:00
combinedFilters = append(combinedFilters, filterBson)
}
// Combine all filter conditions using the $or operator
query := bson.M{}
if len(combinedFilters) > 0 {
query["$or"] = combinedFilters
}
2024-09-17 14:44:17 +00:00
// Apply sorting by creation date (descending)
2024-09-17 12:44:18 +00:00
opts := options.Find().SetSort(bson.D{{Key: "created_at", Value: -1}})
2024-09-17 14:44:17 +00:00
// Apply limit if set in any filter
2024-09-17 12:44:18 +00:00
for _, filter := range filters {
2024-09-02 15:56:52 +00:00
if filter.Limit != nil {
opts.SetLimit(int64(*filter.Limit))
}
2024-09-17 12:44:18 +00:00
}
2024-09-17 14:44:17 +00:00
// If no kinds are specified in any filter, query all collections
var collections []string
if len(filters) > 0 && len(filters[0].Kinds) == 0 {
collections, _ = client.Database(databaseName).ListCollectionNames(context.TODO(), bson.D{})
} else {
// Collect all kinds from filters and query those collections
kindsMap := make(map[int]bool)
for _, filter := range filters {
for _, kind := range filter.Kinds {
kindsMap[kind] = true
}
2024-09-17 12:44:18 +00:00
}
2024-09-02 15:56:52 +00:00
2024-09-17 14:44:17 +00:00
// Construct collection names based on kinds
for kind := range kindsMap {
collectionName := fmt.Sprintf("event-kind%d", kind)
collections = append(collections, collectionName)
}
}
2024-09-02 15:56:52 +00:00
2024-09-17 14:44:17 +00:00
// Query each collection
for _, collectionName := range collections {
collection := client.Database(databaseName).Collection(collectionName)
cursor, err := collection.Find(context.TODO(), query, opts)
if err != nil {
return nil, fmt.Errorf("error querying collection %s: %v", collectionName, err)
2024-09-17 12:44:18 +00:00
}
2024-09-17 14:44:17 +00:00
defer cursor.Close(context.TODO())
2024-09-02 15:56:52 +00:00
2024-09-17 14:44:17 +00:00
for cursor.Next(context.TODO()) {
var event relay.Event
if err := cursor.Decode(&event); err != nil {
return nil, fmt.Errorf("error decoding event from collection %s: %v", collectionName, err)
2024-09-02 15:56:52 +00:00
}
2024-09-17 14:44:17 +00:00
results = append(results, event)
}
// Handle cursor errors
if err := cursor.Err(); err != nil {
return nil, fmt.Errorf("cursor error in collection %s: %v", collectionName, err)
2024-09-02 15:56:52 +00:00
}
}
return results, nil
}