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.

58 lines
1.0 KiB

  1. package posts
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "git.aiterp.net/rpdata/logbot3/internal/api"
  7. "git.aiterp.net/rpdata/logbot3/internal/models"
  8. )
  9. // Add adds a log file.
  10. func Add(ctx context.Context, log models.Log, time time.Time, kind, nick, text string) (models.Post, error) {
  11. input := addInput{
  12. LogID: log.ID,
  13. Time: time,
  14. Kind: kind,
  15. Nick: nick,
  16. Text: text,
  17. }
  18. data, err := api.Global().Query(ctx, addGQL, map[string]interface{}{"input": input}, []string{"post.add"})
  19. if err != nil {
  20. return models.Post{}, err
  21. }
  22. res := addResult{}
  23. err = json.Unmarshal(data, &res)
  24. if err != nil {
  25. return models.Post{}, err
  26. }
  27. return res.Post, nil
  28. }
  29. type addResult struct {
  30. Post models.Post `json:"addPost"`
  31. }
  32. type addInput struct {
  33. LogID string `json:"logId"`
  34. Time time.Time `json:"time"`
  35. Kind string `json:"kind"`
  36. Nick string `json:"nick"`
  37. Text string `json:"text"`
  38. }
  39. const addGQL = `
  40. mutation AddPost($input:PostAddInput!) {
  41. addPost(input:$input) {
  42. id
  43. time
  44. kind
  45. nick
  46. text
  47. }
  48. }
  49. `