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.
177 lines
4.9 KiB
177 lines
4.9 KiB
package resolvers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"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/chapters"
|
|
"git.aiterp.net/rpdata/api/models/comments"
|
|
"git.aiterp.net/rpdata/api/models/stories"
|
|
)
|
|
|
|
// Queries
|
|
|
|
func (r *queryResolver) Chapter(ctx context.Context, id string) (*models.Chapter, error) {
|
|
chapter, err := chapters.FindID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &chapter, nil
|
|
}
|
|
|
|
// Mutations
|
|
|
|
func (r *mutationResolver) AddChapter(ctx context.Context, input graphcore.ChapterAddInput) (*models.Chapter, error) {
|
|
story, err := stories.FindID(input.StoryID)
|
|
if err != nil {
|
|
return nil, errors.New("Story not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Permitted("member", "story.add") {
|
|
return nil, errors.New("Unauthorized")
|
|
}
|
|
|
|
author := token.UserID
|
|
if input.Author != nil && *input.Author != author {
|
|
if !token.Permitted("story.add") {
|
|
return nil, errors.New("False pretender")
|
|
}
|
|
|
|
author = *input.Author
|
|
}
|
|
|
|
if !story.Open && story.Author != author {
|
|
return nil, errors.New("Story is not open")
|
|
}
|
|
|
|
commentMode := models.ChapterCommentModeDisabled
|
|
if input.CommentMode != nil {
|
|
commentMode = *input.CommentMode
|
|
}
|
|
|
|
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to create chapter: " + err.Error())
|
|
}
|
|
|
|
go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
|
|
|
|
return &chapter, nil
|
|
}
|
|
|
|
func (r *mutationResolver) MoveChapter(ctx context.Context, input graphcore.ChapterMoveInput) (*models.Chapter, error) {
|
|
chapter, err := chapters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Chapter not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") {
|
|
return nil, errors.New("You are not allowed to move this chapter")
|
|
}
|
|
|
|
target, err := stories.FindID(input.StoryID)
|
|
if err != nil {
|
|
return nil, errors.New("Target story not found")
|
|
}
|
|
|
|
if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") {
|
|
return nil, errors.New("You are not permitted to move chapters to this story")
|
|
}
|
|
|
|
oldStoryID := chapter.StoryID
|
|
chapter, err = chapters.Move(chapter, target)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to move chapter: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
story.ID = chapter.StoryID
|
|
}
|
|
|
|
oldStory, err := stories.FindID(oldStoryID)
|
|
if err != nil {
|
|
oldStory.ID = oldStoryID
|
|
}
|
|
|
|
changes.Submit("Chapter", "move-out", chapter.Author, oldStory.Listed, changekeys.Many(oldStory, chapter), chapter)
|
|
changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter)
|
|
}()
|
|
|
|
return &chapter, nil
|
|
}
|
|
|
|
func (r *mutationResolver) EditChapter(ctx context.Context, input graphcore.ChapterEditInput) (*models.Chapter, error) {
|
|
chapter, err := chapters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Chapter not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") {
|
|
return nil, errors.New("Unauthorized")
|
|
}
|
|
|
|
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
|
|
input.FictionalDate = &time.Time{}
|
|
}
|
|
|
|
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to edit chapter: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
story.ID = chapter.StoryID
|
|
}
|
|
|
|
changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
|
|
}()
|
|
|
|
return &chapter, nil
|
|
}
|
|
|
|
func (r *mutationResolver) RemoveChapter(ctx context.Context, input graphcore.ChapterRemoveInput) (*models.Chapter, error) {
|
|
chapter, err := chapters.FindID(input.ID)
|
|
if err != nil {
|
|
return nil, errors.New("Chapter not found")
|
|
}
|
|
|
|
token := auth.TokenFromContext(ctx)
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") {
|
|
return nil, errors.New("Unauthorized")
|
|
}
|
|
|
|
chapter, err = chapters.Remove(chapter)
|
|
if err != nil {
|
|
return nil, errors.New("Failed to remove chapter: " + err.Error())
|
|
}
|
|
|
|
err = comments.RemoveChapter(chapter)
|
|
if err != nil {
|
|
return nil, errors.New("Chapter was removed, but comment removal failed: " + err.Error())
|
|
}
|
|
|
|
go func() {
|
|
story, err := stories.FindID(chapter.StoryID)
|
|
if err != nil {
|
|
story.ID = chapter.StoryID
|
|
}
|
|
|
|
changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter)
|
|
}()
|
|
|
|
return &chapter, nil
|
|
}
|