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.

48 lines
1.2 KiB

  1. package mutations
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/graphql/resolver/types"
  5. "git.aiterp.net/rpdata/api/internal/session"
  6. "git.aiterp.net/rpdata/api/model/change"
  7. "git.aiterp.net/rpdata/api/model/character"
  8. "git.aiterp.net/rpdata/api/model/log"
  9. )
  10. // AddCharacterNickInput is args for the addCharacterNick mutation
  11. type AddCharacterNickInput struct {
  12. ID string
  13. Nick string
  14. }
  15. // AddCharacterNick resolves the addCharacterNick mutation
  16. func (r *MutationResolver) AddCharacterNick(ctx context.Context, args struct{ Input *AddCharacterNickInput }) (*types.CharacterResolver, error) {
  17. input := args.Input
  18. user := session.FromContext(ctx).User()
  19. if user == nil || !user.Permitted("member") {
  20. return nil, ErrUnauthorized
  21. }
  22. character, err := character.FindID(input.ID)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if character.Author != user.ID && !user.Permitted("character.edit") {
  27. return nil, ErrPermissionDenied
  28. }
  29. err = character.AddNick(input.Nick)
  30. if err != nil {
  31. return nil, err
  32. }
  33. go change.Submit("Character", "add.nick", user.ID, character.ID, map[string]interface{}{
  34. "nick": input.Nick,
  35. })
  36. log.ScheduleCharacterUpdate()
  37. return &types.CharacterResolver{C: character}, nil
  38. }