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.
194 lines
5.6 KiB
194 lines
5.6 KiB
package resolvers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.aiterp.net/rpdata/api/graph2/graphcore"
|
|
"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 *queryResolver) Character(ctx context.Context, id *string, nick *string) (*models.Character, error) {
|
|
if id != nil {
|
|
character, err := characters.FindID(*id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &character, nil
|
|
} else if nick != nil {
|
|
character, err := characters.FindNick(*nick)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &character, nil
|
|
} else {
|
|
return nil, errors.New("You must specify either an ID or a nick")
|
|
}
|
|
}
|
|
|
|
func (r *queryResolver) Characters(ctx context.Context, filter *characters.Filter) ([]*models.Character, error) {
|
|
characters, err := characters.List(filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
characters2 := make([]*models.Character, len(characters))
|
|
for i := range characters {
|
|
characters2[i] = &characters[i]
|
|
}
|
|
|
|
return characters2, nil
|
|
}
|
|
|
|
// Mutations
|
|
|
|
func (r *mutationResolver) AddCharacter(ctx context.Context, input graphcore.CharacterAddInput) (*models.Character, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Permitted("member", "character.add") {
|
|
return nil, errors.New("You are not permitted to add characters")
|
|
}
|
|
if len(input.Name) < 2 || len(input.Nick) < 2 {
|
|
return nil, 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 nil, 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 nil, 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 graphcore.CharacterNickInput) (*models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
}
|
|
|
|
if len(input.Nick) < 2 {
|
|
return nil, 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 nil, errors.New("You are not permitted to edit this character")
|
|
}
|
|
|
|
logs.ScheduleFullUpdate()
|
|
|
|
character, err = characters.AddNick(character, input.Nick)
|
|
if err != nil {
|
|
return nil, 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 graphcore.CharacterNickInput) (*models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.edit") {
|
|
return nil, errors.New("You are not permitted to edit this character")
|
|
}
|
|
|
|
character, err = characters.RemoveNick(character, input.Nick)
|
|
if err != nil {
|
|
return nil, 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 graphcore.CharacterEditInput) (*models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
}
|
|
|
|
if input.Name != nil && len(*input.Name) < 2 {
|
|
return nil, errors.New("You need to provide a valid name (min length: 2)")
|
|
}
|
|
if input.ShortName != nil && len(*input.ShortName) < 2 {
|
|
return nil, 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 nil, 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 nil, 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 graphcore.CharacterRemoveInput) (*models.Character, error) {
|
|
character, err := characters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Character not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.PermittedUser(character.Author, "member", "character.remove") {
|
|
return nil, errors.New("You are not permitted to remove this character")
|
|
}
|
|
|
|
character, err = characters.Remove(character)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to remove character: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Character", "remove", token.UserID, true, changekeys.Listed(character), character)
|
|
|
|
return &character, nil
|
|
}
|