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.

63 lines
1.3 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. // PostEditArgs is args for the editPost mutation
  11. type PostEditArgs struct {
  12. Input *struct {
  13. ID string
  14. Time *string
  15. Kind *string
  16. Nick *string
  17. Text *string
  18. }
  19. }
  20. // EditPost resolves the editPost mutation
  21. func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*types.PostResolver, error) {
  22. input := args.Input
  23. token := auth.TokenFromContext(ctx)
  24. if !token.Permitted("post.edit") {
  25. return nil, ErrUnauthorized
  26. }
  27. postTime := (*time.Time)(nil)
  28. if args.Input.Time != nil {
  29. t, err := time.Parse(time.RFC3339Nano, *input.Time)
  30. if err != nil {
  31. return nil, err
  32. }
  33. postTime = &t
  34. }
  35. post, err := log.FindPostID(input.ID)
  36. if err != nil {
  37. return nil, err
  38. }
  39. err = post.Edit(postTime, input.Kind, input.Nick, input.Text)
  40. if err != nil {
  41. return nil, err
  42. }
  43. go change.Submit("Post", "edit", token.UserID, post.ID, map[string]interface{}{
  44. "time": postTime,
  45. "kind": input.Kind,
  46. "nick": input.Nick,
  47. "text": input.Text,
  48. })
  49. go log.UpdateCharacters(post.LogID)
  50. return &types.PostResolver{P: post}, nil
  51. }