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.

70 lines
1.6 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "strings"
  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/character"
  9. "git.aiterp.net/rpdata/api/model/log"
  10. )
  11. // AddCharacterInput is args for the addCharacter mutation
  12. type AddCharacterInput struct {
  13. Nick string
  14. Name string
  15. ShortName *string
  16. Author *string
  17. Description *string
  18. }
  19. // AddCharacter resolves the addCharacter mutation
  20. func (r *MutationResolver) AddCharacter(ctx context.Context, args struct{ Input *AddCharacterInput }) (*types.CharacterResolver, error) {
  21. input := args.Input
  22. token := auth.TokenFromContext(ctx)
  23. if !token.Permitted("member", "character.add") {
  24. return nil, ErrUnauthorized
  25. }
  26. nick := input.Nick
  27. name := input.Name
  28. shortName := ""
  29. if input.ShortName != nil {
  30. shortName = *input.ShortName
  31. } else {
  32. shortName = strings.SplitN(input.Name, " ", 2)[0]
  33. }
  34. author := token.UserID
  35. if input.Author != nil {
  36. author = *input.Author
  37. if author != token.UserID && !token.Permitted("character.add") {
  38. return nil, ErrPermissionDenied
  39. }
  40. }
  41. description := ""
  42. if input.Description != nil {
  43. description = *input.Description
  44. }
  45. character, err := character.New(nick, name, shortName, author, description)
  46. if err != nil {
  47. return nil, err
  48. }
  49. go change.Submit("Character", "add", token.UserID, character.ID, map[string]interface{}{
  50. "name": character.Name,
  51. "nick": character.Nicks[0],
  52. "author": character.Author,
  53. })
  54. log.ScheduleCharacterUpdate()
  55. return &types.CharacterResolver{C: character}, nil
  56. }