GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.4 KiB

  1. package logs
  2. import (
  3. "git.aiterp.net/rpdata/api/models"
  4. "github.com/globalsign/mgo/bson"
  5. )
  6. // Filter for the List() function
  7. type Filter struct {
  8. Search *string
  9. Characters *[]string
  10. Channels *[]string
  11. Events *[]string
  12. Open *bool
  13. Limit int
  14. }
  15. // List lists all logs
  16. func List(filter *Filter) ([]models.Log, error) {
  17. query := bson.M{}
  18. limit := 0
  19. if filter != nil {
  20. // Run a text search
  21. if filter.Search != nil {
  22. searchResults, err := search(*filter.Search)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // Posts always use shortId to refer to the log
  27. query["shortId"] = bson.M{"$in": searchResults}
  28. }
  29. // Find logs including any of the specified events and channels
  30. if filter.Channels != nil {
  31. query["channel"] = bson.M{"$in": *filter.Channels}
  32. }
  33. if filter.Events != nil {
  34. query["event"] = bson.M{"$in": *filter.Events}
  35. }
  36. // Find logs including all of the specified character IDs.
  37. if filter.Characters != nil {
  38. query["characterIds"] = bson.M{"$all": *filter.Characters}
  39. }
  40. // Limit to only open logs
  41. if filter.Open != nil {
  42. query["open"] = *filter.Open
  43. }
  44. // Set the limit from the filter
  45. limit = filter.Limit
  46. }
  47. return list(query, limit)
  48. }
  49. func search(text string) ([]string, error) {
  50. query := bson.M{
  51. "$text": bson.M{"$search": text},
  52. "logId": bson.M{"$ne": nil},
  53. }
  54. ids := make([]string, 0, 64)
  55. err := postCollection.Find(query).Distinct("logId", &ids)
  56. return ids, err
  57. }