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.

44 lines
1.1 KiB

  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/character"
  8. )
  9. // RemoveCharacterArgs is args for the removeCharacter mutation
  10. type RemoveCharacterArgs struct {
  11. ID string
  12. }
  13. // RemoveCharacter resolves the removeCharacter mutation
  14. func (r *MutationResolver) RemoveCharacter(ctx context.Context, args RemoveCharacterArgs) (*types.CharacterResolver, error) {
  15. token := auth.TokenFromContext(ctx)
  16. if !token.Permitted("member") {
  17. return nil, ErrUnauthorized
  18. }
  19. character, err := character.FindID(args.ID)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if character.Author != token.UserID && !token.Permitted("character.remove") {
  24. return nil, ErrPermissionDenied
  25. }
  26. err = character.Remove()
  27. if err != nil {
  28. return nil, err
  29. }
  30. go change.Submit("Character", "remove", token.UserID, character.ID, map[string]interface{}{
  31. "name": character.Name,
  32. "author": character.Author,
  33. "nicks": character.Nicks,
  34. })
  35. return &types.CharacterResolver{C: character}, nil
  36. }