package mutations import ( "context" "strings" "git.aiterp.net/rpdata/api/graphql/resolver/types" "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/character" "git.aiterp.net/rpdata/api/model/log" ) // AddCharacterInput is args for the addCharacter mutation type AddCharacterInput struct { Nick string Name string ShortName *string Author *string Description *string } // AddCharacter resolves the addCharacter mutation func (r *MutationResolver) AddCharacter(ctx context.Context, args struct{ Input *AddCharacterInput }) (*types.CharacterResolver, error) { input := args.Input token := auth.TokenFromContext(ctx) if !token.Permitted("member", "character.add") { return nil, ErrUnauthorized } nick := input.Nick name := input.Name shortName := "" if input.ShortName != nil { shortName = *input.ShortName } else { shortName = strings.SplitN(input.Name, " ", 2)[0] } author := token.UserID if input.Author != nil { author = *input.Author if author != token.UserID && !token.Permitted("character.add") { return nil, ErrPermissionDenied } } description := "" if input.Description != nil { description = *input.Description } character, err := character.New(nick, name, shortName, author, description) if err != nil { return nil, err } go change.Submit("Character", "add", token.UserID, character.ID, map[string]interface{}{ "name": character.Name, "nick": character.Nicks[0], "author": character.Author, }) log.ScheduleCharacterUpdate() return &types.CharacterResolver{C: character}, nil }