package resolver import ( "context" "errors" "strings" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/loader" "git.aiterp.net/rpdata/api/model/change" "git.aiterp.net/rpdata/api/model/character" "git.aiterp.net/rpdata/api/model/log" ) // CharacterResolver for the Character graphql type type CharacterResolver struct{ C character.Character } // CharacterArgs is an arg type CharacterArgs struct { ID *string Nick *string } // Character resolver func (r *QueryResolver) Character(ctx context.Context, args *CharacterArgs) (*CharacterResolver, error) { var char character.Character var err error loader := loader.FromContext(ctx) if loader == nil { return nil, errors.New("no loader") } switch { case args.ID != nil: char, err = character.FindID(*args.ID) case args.Nick != nil: char, err = character.FindNick(*args.Nick) default: err = ErrCannotResolve } if err != nil { return nil, err } return &CharacterResolver{C: char}, nil } // CharactersArgs is an arg type CharactersArgs struct { IDs *[]string Nicks *[]string Author *string } // Characters resolves the characters query func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*CharacterResolver, error) { var chars []character.Character var err error loader := loader.FromContext(ctx) if loader == nil { return nil, errors.New("no loader") } switch { case args.IDs != nil: chars, err = character.ListIDs(*args.IDs...) case args.Nicks != nil: chars, err = character.ListNicks(*args.Nicks...) case args.Author != nil: chars, err = character.ListAuthor(*args.Author) default: chars, err = character.List() } if err != nil { return nil, err } resolvers := make([]*CharacterResolver, 0, len(chars)) for i := range chars { if chars[i].ID == "" { continue } resolvers = append(resolvers, &CharacterResolver{C: chars[i]}) } return resolvers, nil } // 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 }) (*CharacterResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.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 := user.ID if input.Author != nil { author = *input.Author if author != user.ID && !user.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", user.ID, character.ID, map[string]interface{}{ "name": character.Name, "nick": character.Nicks[0], "author": character.Author, }) log.ScheduleCharacterUpdate() return &CharacterResolver{C: character}, nil } // CharacterNickInput is args for mutation addCharacterNick/removeCharacterNick type CharacterNickInput struct { ID string Nick string } // AddCharacterNick resolves the addCharacterNick mutation func (r *MutationResolver) AddCharacterNick(ctx context.Context, args struct{ Input *CharacterNickInput }) (*CharacterResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } character, err := character.FindID(input.ID) if err != nil { return nil, err } if character.Author != user.ID && !user.Permitted("character.edit") { return nil, ErrPermissionDenied } err = character.AddNick(input.Nick) if err != nil { return nil, err } go change.Submit("Character", "add.nick", user.ID, character.ID, map[string]interface{}{ "nick": input.Nick, }) log.ScheduleCharacterUpdate() return &CharacterResolver{C: character}, nil } // RemoveCharacterNick resolves the removeCharacterNick mutation func (r *MutationResolver) RemoveCharacterNick(ctx context.Context, args struct{ Input *CharacterNickInput }) (*CharacterResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } character, err := character.FindID(input.ID) if err != nil { return nil, err } if character.Author != user.ID && !user.Permitted("character.edit") { return nil, ErrPermissionDenied } err = character.RemoveNick(input.Nick) if err != nil { return nil, err } go change.Submit("Character", "remove.nick", user.ID, character.ID, map[string]interface{}{ "nick": input.Nick, }) log.ScheduleCharacterUpdate() return &CharacterResolver{C: character}, nil } // CharacterEditInput is args for mutation addCharacterNick/removeCharacterNick type CharacterEditInput struct { ID string Name *string ShortName *string Description *string } // EditCharacter resolves the editCharacter mutation func (r *MutationResolver) EditCharacter(ctx context.Context, args struct{ Input *CharacterEditInput }) (*CharacterResolver, error) { input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } character, err := character.FindID(input.ID) if err != nil { return nil, err } if character.Author != user.ID && !user.Permitted("character.edit") { return nil, ErrPermissionDenied } name := "" if input.Name != nil { name = *input.Name } shortName := "" if input.ShortName != nil { shortName = *input.ShortName } description := "" if input.Description != nil { description = *input.Description } err = character.Edit(name, shortName, description) if err != nil { return nil, err } go change.Submit("Character", "edit", user.ID, character.ID, map[string]interface{}{ "name": character.Name, "shortName": character.ShortName, "description": character.Description, }) return &CharacterResolver{C: character}, nil } // RemoveCharacter resolves the removeCharacter mutation func (r *MutationResolver) RemoveCharacter(ctx context.Context, args struct{ ID string }) (*CharacterResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } character, err := character.FindID(args.ID) if err != nil { return nil, err } if character.Author != user.ID && !user.Permitted("character.remove") { return nil, ErrPermissionDenied } err = character.Remove() if err != nil { return nil, err } go change.Submit("Character", "remove", user.ID, character.ID, map[string]interface{}{ "name": character.Name, "author": character.Author, "nicks": character.Nicks, }) return &CharacterResolver{C: character}, nil } // ID is a property resolver func (r *CharacterResolver) ID() string { return r.C.ID } // Nick is a property resolver func (r *CharacterResolver) Nick() *string { if len(r.C.Nicks) == 0 { return nil } return &r.C.Nicks[0] } // Nicks is a property resolver func (r *CharacterResolver) Nicks() []string { return r.C.Nicks } // Name is a property resolver func (r *CharacterResolver) Name() string { return r.C.Name } // ShortName is a property resolver func (r *CharacterResolver) ShortName() string { return r.C.ShortName } // Author is a property resolver func (r *CharacterResolver) Author() string { return r.C.Author } // Description is a property resolver func (r *CharacterResolver) Description() string { return r.C.Description }