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.

41 lines
962 B

  1. package mutations
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/internal/auth"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/story"
  8. )
  9. // StoryRemoveArgs is args for the removeStory mutation
  10. type StoryRemoveArgs struct {
  11. ID string
  12. }
  13. // RemoveStory resolves the removeStory mutation
  14. func (r *MutationResolver) RemoveStory(ctx context.Context, args *StoryRemoveArgs) (*types.StoryResolver, error) {
  15. token := auth.TokenFromContext(ctx)
  16. if !token.Permitted("member", "story.edit") {
  17. return nil, ErrUnauthorized
  18. }
  19. story, err := story.FindID(args.ID)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if story.Author != token.UserID && !token.Permitted("story.remove") {
  24. return nil, ErrPermissionDenied
  25. }
  26. err = story.Remove()
  27. if err != nil {
  28. return nil, err
  29. }
  30. go change.Submit("Story", "remove", token.UserID, story.ID, nil)
  31. return &types.StoryResolver{S: story}, nil
  32. }