Added logged filter to characters query: character appears in logs.
the build was successful

This commit is contained in:
2018-07-18 00:13:19 +02:00
parent 388d5378e7
commit 0ec923d686
3 changed files with 32 additions and 2 deletions
+27 -1
View File
@@ -14,6 +14,8 @@ import (
var collection *mgo.Collection
var logsCollection *mgo.Collection
// Character is a common data model representing an RP character or NPC.
type Character struct {
ID string `json:"id" bson:"_id"`
@@ -155,9 +157,31 @@ func ListIDs(ids ...string) ([]Character, error) {
}
// ListFilter lists all logs matching the filters.
func ListFilter(ids []string, nicks []string, names []string, author *string, search *string) ([]Character, error) {
func ListFilter(ids []string, nicks []string, names []string, author *string, search *string, logged *bool) ([]Character, error) {
query := bson.M{}
if logged != nil {
loggedIDs := make([]string, 0, 64)
err := logsCollection.Find(bson.M{"characterIds": bson.M{"$ne": nil}}).Distinct("characterIds", &loggedIDs)
if err != nil {
return nil, err
}
if len(ids) > 0 {
newIds := make([]string, 0, len(ids))
for _, id := range ids {
for _, loggedID := range loggedIDs {
if id == loggedID {
newIds = append(newIds, id)
break
}
}
}
ids = newIds
} else {
ids = loggedIDs
}
}
if len(ids) > 0 {
query["_id"] = bson.M{"$in": ids}
}
@@ -256,5 +280,7 @@ func init() {
if err != nil {
log.Fatalln("init common.characters:", err)
}
logsCollection = db.C("logbot3.logs")
})
}