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.

60 lines
1.2 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "time"
  5. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/model/change"
  8. "git.aiterp.net/rpdata/api/model/log"
  9. )
  10. // PostAddArgs is args for addPost mutation
  11. type PostAddArgs struct {
  12. Input *struct {
  13. LogID string
  14. Time string
  15. Kind string
  16. Nick string
  17. Text string
  18. }
  19. }
  20. // AddPost resolves the addPost mutation
  21. func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*types.PostResolver, error) {
  22. input := args.Input
  23. token := auth.TokenFromContext(ctx)
  24. if !token.Permitted("post.add") {
  25. return nil, ErrUnauthorized
  26. }
  27. postTime, err := time.Parse(time.RFC3339Nano, input.Time)
  28. if err != nil {
  29. return nil, err
  30. }
  31. log, err := log.FindID(input.LogID)
  32. if err != nil {
  33. return nil, err
  34. }
  35. post, err := log.NewPost(postTime, input.Kind, input.Nick, input.Text)
  36. if err != nil {
  37. return nil, err
  38. }
  39. go change.Submit("Post", "add", token.UserID, post.ID, map[string]interface{}{
  40. "logId": post.LogID,
  41. "time": post.Time,
  42. "kind": post.Kind,
  43. "nick": post.Nick,
  44. "text": post.Text,
  45. "position": post.Position,
  46. })
  47. go log.UpdateCharacters()
  48. return &types.PostResolver{P: post}, nil
  49. }