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.
201 lines
5.3 KiB
201 lines
5.3 KiB
package queries
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/models/changekeys"
|
|
"git.aiterp.net/rpdata/api/models/changes"
|
|
|
|
"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/chapters"
|
|
"git.aiterp.net/rpdata/api/models/stories"
|
|
)
|
|
|
|
func (r *resolver) Story(ctx context.Context, id string) (*models.Story, error) {
|
|
story, err := stories.FindID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &story, nil
|
|
}
|
|
|
|
func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]*models.Story, error) {
|
|
if filter != nil {
|
|
if filter.Unlisted != nil && *filter.Unlisted == true {
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() {
|
|
return nil, errors.New("You are not permitted to view unlisted stories")
|
|
}
|
|
|
|
if !token.Permitted("story.unlisted") {
|
|
filter.Author = &token.UserID
|
|
}
|
|
}
|
|
}
|
|
|
|
stories, err := stories.List(filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
stories2 := make([]*models.Story, len(stories))
|
|
for i := range stories {
|
|
stories2[i] = &stories[i]
|
|
}
|
|
|
|
return stories2, nil
|
|
}
|
|
|
|
// Mutations
|
|
|
|
func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (*models.Story, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
if token == nil || !token.Permitted("member", "story.add") {
|
|
return nil, errors.New("Permission denied")
|
|
}
|
|
|
|
author := token.UserID
|
|
if input.Author != nil && *input.Author != author {
|
|
if !token.Permitted("story.add") {
|
|
return nil, errors.New("You are not permitted to add a story in another author's name")
|
|
}
|
|
|
|
author = *input.Author
|
|
}
|
|
|
|
fictionalDate := time.Time{}
|
|
if input.FictionalDate != nil {
|
|
fictionalDate = *input.FictionalDate
|
|
}
|
|
|
|
listed := input.Listed != nil && *input.Listed
|
|
open := input.Open != nil && *input.Open
|
|
|
|
tags := make([]models.Tag, len(input.Tags))
|
|
for i := range input.Tags {
|
|
tags[i] = *input.Tags[i]
|
|
}
|
|
|
|
story, err := stories.Add(input.Name, author, input.Category, listed, open, tags, time.Now(), fictionalDate)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to add story: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story)
|
|
|
|
return &story, nil
|
|
}
|
|
|
|
func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (*models.Story, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
|
|
story, err := stories.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Story not found")
|
|
}
|
|
|
|
if story.Open {
|
|
if !token.Permitted("member") {
|
|
return nil, errors.New("You are not permitted to edit this story")
|
|
}
|
|
} else {
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") {
|
|
return nil, errors.New("You are not permitted to edit this story")
|
|
}
|
|
}
|
|
|
|
story, err = stories.AddTag(story, *input.Tag)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to add story: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Story", "tag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
|
|
|
|
return &story, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (*models.Story, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
|
|
story, err := stories.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Story not found")
|
|
}
|
|
|
|
if story.Open {
|
|
if !token.Permitted("member") {
|
|
return nil, errors.New("You are not permitted to edit this story")
|
|
}
|
|
} else {
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") {
|
|
return nil, errors.New("You are not permitted to edit this story")
|
|
}
|
|
}
|
|
|
|
story, err = stories.RemoveTag(story, *input.Tag)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to add story: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Story", "untag", token.UserID, story.Listed, changekeys.Listed(story), story, input.Tag)
|
|
|
|
return &story, nil
|
|
}
|
|
|
|
func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (*models.Story, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
|
|
story, err := stories.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Story not found")
|
|
}
|
|
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") {
|
|
return nil, errors.New("You are not permitted to remove this story")
|
|
}
|
|
|
|
if input.ClearFictionalDate != nil && *input.ClearFictionalDate {
|
|
input.FictionalDate = &time.Time{}
|
|
}
|
|
|
|
story, err = stories.Edit(story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to add story: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story)
|
|
|
|
return &story, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (*models.Story, error) {
|
|
token := auth.TokenFromContext(ctx)
|
|
|
|
story, err := stories.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Story not found")
|
|
}
|
|
|
|
if !token.PermittedUser(story.Author, "member", "story.remove") {
|
|
return nil, errors.New("You are not permitted to remove this story")
|
|
}
|
|
|
|
story, err = stories.Remove(story)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = chapters.RemoveStory(story)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to remove chapters, but story is removed: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Story", "remove", token.UserID, story.Listed, changekeys.Listed(story), story)
|
|
|
|
return &story, nil
|
|
}
|