graph2: Added Log type and queries, moved loader out of old graphql pkg.
the build failed

This commit is contained in:
2018-09-15 13:14:05 +02:00
parent 0444911e73
commit 2b05d90f4a
10 changed files with 215 additions and 66 deletions
+1 -22
View File
@@ -7,26 +7,5 @@ type Filter struct {
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
Limit int
}
+52 -40
View File
@@ -31,7 +31,7 @@ type Log struct {
ID string `bson:"_id"`
ShortID string `bson:"shortId"`
Date time.Time `bson:"date"`
Channel string `bson:"channel"`
ChannelName string `bson:"channel"`
Title string `bson:"title,omitempty"`
Event string `bson:"event,omitempty"`
Description string `bson:"description,omitempty"`
@@ -55,7 +55,7 @@ func New(date time.Time, channelName, title, event, description string, open boo
ID: MakeLogID(date, channelName),
ShortID: "L" + strconv.Itoa(nextID),
Date: date,
Channel: channelName,
ChannelName: channelName,
Title: title,
Event: event,
Description: description,
@@ -84,43 +84,44 @@ func FindID(id string) (Log, error) {
// List lists all logs
func List(filter *Filter) ([]Log, error) {
query := bson.M{}
// Run a text search
if filter.Search != nil {
searchResults := make([]string, 0, 32)
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 filter.Channels != nil {
query["channel"] = bson.M{"$in": *filter.Channels}
}
if filter.Events != nil {
query["event"] = bson.M{"$in": *filter.Events}
}
// Find logs including all of the specified character IDs.
if filter.Characters != nil {
query["characterIds"] = bson.M{"$all": *filter.Characters}
}
// Limit to only open logs
if filter.Open != nil {
query["open"] = *filter.Open
}
limit := 0
if filter.Limit != nil {
limit = int(*filter.Limit)
if filter != nil {
// Run a text search
if filter.Search != nil {
searchResults := make([]string, 0, 32)
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 filter.Channels != nil {
query["channel"] = bson.M{"$in": *filter.Channels}
}
if filter.Events != nil {
query["event"] = bson.M{"$in": *filter.Events}
}
// Find logs including all of the specified character IDs.
if filter.Characters != nil {
query["characterIds"] = bson.M{"$all": *filter.Characters}
}
// Limit to only open logs
if filter.Open != nil {
query["open"] = *filter.Open
}
// Set the limit from the filter
limit = filter.Limit
}
return listLog(query, limit)
@@ -168,9 +169,20 @@ func (log *Log) Edit(title *string, event *string, description *string, open *bo
return nil
}
// Characters get all the characters for the character IDs stored in the
// log file.
func (log *Log) Characters() ([]character.Character, error) {
return character.ListIDs(log.CharacterIDs...)
}
// Channel gets the channel.
func (log *Log) Channel() (channel.Channel, error) {
return channel.FindName(log.ChannelName)
}
// Posts gets all the posts under the log. If no kinds are specified, it
// will get all posts
func (log *Log) Posts(kinds ...string) ([]Post, error) {
func (log *Log) Posts(kinds []string) ([]Post, error) {
postMutex.RLock()
defer postMutex.RUnlock()
@@ -238,7 +250,7 @@ func (log *Log) UpdateCharacters() error {
characterUpdateMutex.Lock()
defer characterUpdateMutex.Unlock()
posts, err := log.Posts()
posts, err := log.Posts([]string{"action", "text", "chars"})
if err != nil {
return err
}