|
|
@ -15,7 +15,7 @@ import ( |
|
|
|
"git.aiterp.net/rpdata/api/models/stories" |
|
|
|
) |
|
|
|
|
|
|
|
func (r *resolver) Story(ctx context.Context, id string) (models.Story, error) { |
|
|
|
func (r *resolver) Story(ctx context.Context, id string) (*models.Story, error) { |
|
|
|
return stories.FindID(id) |
|
|
|
} |
|
|
|
|
|
|
@ -38,16 +38,16 @@ func (r *resolver) Stories(ctx context.Context, filter *stories.Filter) ([]model |
|
|
|
|
|
|
|
// Mutations
|
|
|
|
|
|
|
|
func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInput) (models.Story, error) { |
|
|
|
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 models.Story{}, errors.New("Permission denied") |
|
|
|
return nil, errors.New("Permission denied") |
|
|
|
} |
|
|
|
|
|
|
|
author := token.UserID |
|
|
|
if input.Author != nil && *input.Author != author { |
|
|
|
if !token.Permitted("story.add") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to add a story in another author's name") |
|
|
|
return nil, errors.New("You are not permitted to add a story in another author's name") |
|
|
|
} |
|
|
|
|
|
|
|
author = *input.Author |
|
|
@ -63,7 +63,7 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp |
|
|
|
|
|
|
|
story, err := stories.Add(input.Name, author, input.Category, listed, open, input.Tags, time.Now(), fictionalDate) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, errors.New("Failed to add story: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to add story: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go changes.Submit("Story", "add", token.UserID, story.Listed, changekeys.Listed(story), story) |
|
|
@ -71,27 +71,27 @@ func (r *mutationResolver) AddStory(ctx context.Context, input input.StoryAddInp |
|
|
|
return story, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTagAddInput) (models.Story, error) { |
|
|
|
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 models.Story{}, errors.New("Story not found") |
|
|
|
return nil, errors.New("Story not found") |
|
|
|
} |
|
|
|
|
|
|
|
if story.Open { |
|
|
|
if !token.Permitted("member") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to edit this story") |
|
|
|
return nil, errors.New("You are not permitted to edit this story") |
|
|
|
} |
|
|
|
} else { |
|
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to edit this story") |
|
|
|
return nil, errors.New("You are not permitted to edit this story") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
story, err = stories.AddTag(story, input.Tag) |
|
|
|
story, err = stories.AddTag(*story, input.Tag) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, errors.New("Failed to add story: " + err.Error()) |
|
|
|
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) |
|
|
@ -99,27 +99,27 @@ func (r *mutationResolver) AddStoryTag(ctx context.Context, input input.StoryTag |
|
|
|
return story, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.StoryTagRemoveInput) (models.Story, error) { |
|
|
|
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 models.Story{}, errors.New("Story not found") |
|
|
|
return nil, errors.New("Story not found") |
|
|
|
} |
|
|
|
|
|
|
|
if story.Open { |
|
|
|
if !token.Permitted("member") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to edit this story") |
|
|
|
return nil, errors.New("You are not permitted to edit this story") |
|
|
|
} |
|
|
|
} else { |
|
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to edit this story") |
|
|
|
return nil, errors.New("You are not permitted to edit this story") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
story, err = stories.RemoveTag(story, input.Tag) |
|
|
|
story, err = stories.RemoveTag(*story, input.Tag) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, errors.New("Failed to add story: " + err.Error()) |
|
|
|
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) |
|
|
@ -127,25 +127,25 @@ func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input input.Story |
|
|
|
return story, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditInput) (models.Story, error) { |
|
|
|
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 models.Story{}, errors.New("Story not found") |
|
|
|
return nil, errors.New("Story not found") |
|
|
|
} |
|
|
|
|
|
|
|
if !token.PermittedUser(story.Author, "member", "story.edit") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to remove this story") |
|
|
|
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) |
|
|
|
story, err = stories.Edit(*story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, errors.New("Failed to add story: " + err.Error()) |
|
|
|
return nil, errors.New("Failed to add story: " + err.Error()) |
|
|
|
} |
|
|
|
|
|
|
|
go changes.Submit("Story", "edit", token.UserID, story.Listed, changekeys.Listed(story), story) |
|
|
@ -153,26 +153,26 @@ func (r *mutationResolver) EditStory(ctx context.Context, input input.StoryEditI |
|
|
|
return story, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveStory(ctx context.Context, input input.StoryRemoveInput) (models.Story, error) { |
|
|
|
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 models.Story{}, errors.New("Story not found") |
|
|
|
return nil, errors.New("Story not found") |
|
|
|
} |
|
|
|
|
|
|
|
if !token.PermittedUser(story.Author, "member", "story.remove") { |
|
|
|
return models.Story{}, errors.New("You are not permitted to remove this story") |
|
|
|
return nil, errors.New("You are not permitted to remove this story") |
|
|
|
} |
|
|
|
|
|
|
|
story, err = stories.Remove(story) |
|
|
|
story, err = stories.Remove(*story) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
err = chapters.RemoveStory(story) |
|
|
|
err = chapters.RemoveStory(*story) |
|
|
|
if err != nil { |
|
|
|
return models.Story{}, errors.New("Failed to remove chapters, but story is removed: " + err.Error()) |
|
|
|
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) |
|
|
|