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.

67 lines
1.4 KiB

  1. package logs
  2. import (
  3. "context"
  4. "encoding/json"
  5. "git.aiterp.net/rpdata/logbot3/internal/api"
  6. "git.aiterp.net/rpdata/logbot3/internal/models"
  7. )
  8. // SetOpen changes the open state of a log.
  9. func SetOpen(ctx context.Context, log models.Log, open bool) (models.Log, error) {
  10. return edit(ctx, editInput{ID: log.ID, Open: &open})
  11. }
  12. // SetEventName changes the event name of a log.
  13. func SetEventName(ctx context.Context, log models.Log, event string) (models.Log, error) {
  14. return edit(ctx, editInput{ID: log.ID, Event: &event})
  15. }
  16. func edit(ctx context.Context, input editInput) (models.Log, error) {
  17. data, err := api.Global().Query(ctx, editGQL, map[string]interface{}{"input": input}, []string{"log.edit"})
  18. if err != nil {
  19. return models.Log{}, err
  20. }
  21. res := editResult{}
  22. err = json.Unmarshal(data, &res)
  23. if err != nil {
  24. return models.Log{}, err
  25. }
  26. return res.Log, nil
  27. }
  28. type editResult struct {
  29. Log models.Log `json:"editLog"`
  30. }
  31. type editInput struct {
  32. ID string `json:"id"`
  33. Title *string `json:"title"`
  34. Event *string `json:"event"`
  35. Description *string `json:"description"`
  36. Open *bool `json:"open"`
  37. }
  38. const editGQL = `
  39. mutation EditLog($input:LogEditInput!) {
  40. editLog(input:$input) {
  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. `