This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package log
|
||||
|
||||
// Filter for the List() function
|
||||
type Filter struct {
|
||||
Search *string
|
||||
Characters *[]string
|
||||
Channels *[]string
|
||||
Events *[]string
|
||||
Open *bool
|
||||
Limit *int32
|
||||
}
|
||||
|
||||
// NewFilter makes a new filter
|
||||
func NewFilter() *Filter {
|
||||
return &Filter{}
|
||||
}
|
||||
|
||||
// WithLimit adds a max amount of results to be returned.
|
||||
func (filter *Filter) WithLimit(limit int) *Filter {
|
||||
limitPtr := int32(limit)
|
||||
filter.Limit = &limitPtr
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// WithOpen filters on whether a log is open.
|
||||
func (filter *Filter) WithOpen(limit int) *Filter {
|
||||
limitPtr := int32(limit)
|
||||
filter.Limit = &limitPtr
|
||||
|
||||
return filter
|
||||
}
|
||||
+18
-22
@@ -82,49 +82,45 @@ func FindID(id string) (Log, error) {
|
||||
}
|
||||
|
||||
// List lists all logs
|
||||
func List(limit int) ([]Log, error) {
|
||||
return listLog(bson.M{}, limit)
|
||||
}
|
||||
|
||||
// ListSearch lists the logs matching the parameters. Empty/zero values means the parameter is ingored when
|
||||
// building the query. This is the old aitelogs2 way, but with the addition of a text search.
|
||||
//
|
||||
// If a text search is specified, it will make two trips to the database.
|
||||
func ListSearch(textSearch string, channels []string, characterIds []string, events []string, open bool, limit int) ([]Log, error) {
|
||||
postMutex.RLock()
|
||||
defer postMutex.RUnlock()
|
||||
|
||||
func List(filter *Filter) ([]Log, error) {
|
||||
query := bson.M{}
|
||||
|
||||
// Run a text search
|
||||
if textSearch != "" {
|
||||
if filter.Search != nil {
|
||||
searchResults := make([]string, 0, 32)
|
||||
|
||||
err := postCollection.Find(bson.M{"$text": bson.M{"$search": textSearch}}).Distinct("logId", &searchResults)
|
||||
postMutex.RLock()
|
||||
err := postCollection.Find(bson.M{"$text": bson.M{"$search": *filter.Search}}).Distinct("logId", &searchResults)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
postMutex.RUnlock()
|
||||
|
||||
// Posts always use shortId to refer to the log
|
||||
query["shortId"] = bson.M{"$in": searchResults}
|
||||
}
|
||||
|
||||
// Find logs including any of the specified events and channels
|
||||
if len(channels) > 0 {
|
||||
query["channel"] = bson.M{"$in": channels}
|
||||
if filter.Channels != nil {
|
||||
query["channel"] = bson.M{"$in": *filter.Channels}
|
||||
}
|
||||
if len(events) > 0 {
|
||||
query["events"] = bson.M{"$in": channels}
|
||||
if filter.Events != nil {
|
||||
query["event"] = bson.M{"$in": *filter.Events}
|
||||
}
|
||||
|
||||
// Find logs including all of the specified character IDs.
|
||||
if len(characterIds) > 0 {
|
||||
query["characterIds"] = bson.M{"$all": characterIds}
|
||||
if filter.Characters != nil {
|
||||
query["characterIds"] = bson.M{"$all": *filter.Characters}
|
||||
}
|
||||
|
||||
// Limit to only open logs
|
||||
if open {
|
||||
query["open"] = true
|
||||
if filter.Open != nil {
|
||||
query["open"] = *filter.Open
|
||||
}
|
||||
|
||||
limit := 0
|
||||
if filter.Limit != nil {
|
||||
limit = int(*filter.Limit)
|
||||
}
|
||||
|
||||
return listLog(query, limit)
|
||||
|
||||
Reference in New Issue
Block a user