The new logbot, not committed from the wrong terminal window this time.
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.

66 lines
1.3 KiB

  1. package logs
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "git.aiterp.net/rpdata/logbot3/internal/api"
  7. "git.aiterp.net/rpdata/logbot3/internal/models"
  8. )
  9. // ErrNoneOpen is returned by FindOpen if no logs are open, but the query did succeed.
  10. var ErrNoneOpen = errors.New("No open logs")
  11. // FindOpen lists all changes according to the filter.
  12. func FindOpen(ctx context.Context, channelName string) (models.Log, error) {
  13. data, err := api.Global().Query(ctx, findOpenGQL, map[string]interface{}{"channel": channelName}, nil)
  14. if err != nil {
  15. return models.Log{}, err
  16. }
  17. res := findOpenResult{}
  18. err = json.Unmarshal(data, &res)
  19. if err != nil {
  20. return models.Log{}, err
  21. }
  22. if len(res.Logs) == 0 {
  23. return models.Log{}, ErrNoneOpen
  24. }
  25. // This shouldn't happen, but if there are more than one
  26. // open logs for a channel, select the most recent one.
  27. selected := res.Logs[0]
  28. for _, log := range res.Logs[1:] {
  29. if log.Date.After(selected.Date) {
  30. selected = log
  31. }
  32. }
  33. return selected, nil
  34. }
  35. type findOpenResult struct {
  36. Logs []models.Log `json:"logs"`
  37. }
  38. var findOpenGQL = `
  39. query FindOpen($channel:String!) {
  40. logs(filter:{open:true, channels:[$channel]}) {
  41. id
  42. date
  43. channelName
  44. title
  45. eventName
  46. description
  47. open
  48. posts {
  49. id
  50. time
  51. kind
  52. nick
  53. text
  54. }
  55. }
  56. }
  57. `