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.
174 lines
5.5 KiB
174 lines
5.5 KiB
package queries
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.aiterp.net/rpdata/api/graph2/input"
|
|
"git.aiterp.net/rpdata/api/internal/auth"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"git.aiterp.net/rpdata/api/models/changekeys"
|
|
"git.aiterp.net/rpdata/api/models/changes"
|
|
"git.aiterp.net/rpdata/api/models/characters"
|
|
"git.aiterp.net/rpdata/api/models/logs"
|
|
)
|
|
|
|
// Queries
|
|
|
|
func (r *resolver) Character(ctx context.Context, id *string, nick *string) (models.Character, error) {
|
|
if id != nil {
|
|
return characters.FindID(*id)
|
|
} else if nick != nil {
|
|
return characters.FindNick(*nick)
|
|
} else {
|
|
return models.Character{}, errors.New("You must specify either an ID or a nick")
|
|
}
|
|
}
|
|
|
|
func (r *resolver) Characters(ctx context.Context, filter *characters.Filter) ([]models.Character, error) {
|
|
return characters.List(filter)
|
|
}
|
|
|
|
// Mutations
|
|
|
|
func (r *mutationResolver) AddCharacter(ctx context.Context, input input.CharacterAddInput) (models.Character, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Permitted("member", "character.add") {
|
|
return models.Character{}, errors.New("You are not permitted to add characters")
|
|
}
|
|
if len(input.Name) < 2 || len(input.Nick) < 2 {
|
|
return models.Character{}, errors.New("You need to provide a name and a nick (min length: 2)")
|
|
}
|
|
|
|
shortName := ""
|
|
if input.ShortName != nil {
|
|
shortName = *input.ShortName
|
|
} else {
|
|
shortName = strings.SplitN(input.Name, " ", 2)[0]
|
|
}
|
|
|
|
description := ""
|
|
if input.Description != nil {
|
|
description = *input.Description
|
|
}
|
|
|
|
author := token.UserID
|
|
if input.Author != nil && *input.Author != author {
|
|
if !token.Permitted("character.add") {
|
|
return models.Character{}, errors.New("You are only permitted to add your own characters")
|
|
}
|
|
|
|
author = *input.Author
|
|
}
|
|
|
|
logs.ScheduleFullUpdate()
|
|
|
|
character, err := characters.Add(input.Nick, input.Name, shortName, author, description)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Adding character failed: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Character", "add", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return character, nil
|
|
}
|
|
|
|
func (r *mutationResolver) AddCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Character not found")
|
|
}
|
|
|
|
if len(input.Nick) < 2 {
|
|
return models.Character{}, errors.New("You need to provide a valid nick (min length: 2)")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") {
|
|
return models.Character{}, errors.New("You are not permitted to edit this character")
|
|
}
|
|
|
|
logs.ScheduleFullUpdate()
|
|
|
|
character, err = characters.AddNick(character, input.Nick)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Failed to add nick: " + err.Error())
|
|
}
|
|
|
|
go logs.ScheduleFullUpdate()
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return character, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveCharacterNick(ctx context.Context, input input.CharacterNickInput) (models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Character not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") {
|
|
return models.Character{}, errors.New("You are not permitted to edit this character")
|
|
}
|
|
|
|
character, err = characters.RemoveNick(character, input.Nick)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Failed to remove nick: " + err.Error())
|
|
}
|
|
|
|
go logs.ScheduleFullUpdate()
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return character, nil
|
|
}
|
|
|
|
func (r *mutationResolver) EditCharacter(ctx context.Context, input input.CharacterEditInput) (models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Character not found")
|
|
}
|
|
|
|
if input.Name != nil && len(*input.Name) < 2 {
|
|
return models.Character{}, errors.New("You need to provide a valid name (min length: 2)")
|
|
}
|
|
if input.ShortName != nil && len(*input.ShortName) < 2 {
|
|
return models.Character{}, errors.New("You need to provide a valid short name (min length: 2)")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") {
|
|
return models.Character{}, errors.New("You are not permitted to edit this character")
|
|
}
|
|
|
|
character, err = characters.Edit(character, input.Name, input.ShortName, input.Description)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Failed to edit character: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Character", "edit", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return character, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveCharacter(ctx context.Context, input input.CharacterRemoveInput) (models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Character not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.remove") {
|
|
return models.Character{}, errors.New("You are not permitted to remove this character")
|
|
}
|
|
|
|
character, err = characters.Remove(character)
|
|
if err != nil {
|
|
return models.Character{}, errors.New("Failed to remove character: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Character", "remove", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return character, nil
|
|
}
|