|
@ -5,48 +5,51 @@ import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"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/changekeys" |
|
|
"git.aiterp.net/rpdata/api/models/changes" |
|
|
"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/comments" |
|
|
|
|
|
|
|
|
"git.aiterp.net/rpdata/api/internal/auth" |
|
|
|
|
|
"git.aiterp.net/rpdata/api/models/stories" |
|
|
"git.aiterp.net/rpdata/api/models/stories" |
|
|
|
|
|
|
|
|
"git.aiterp.net/rpdata/api/graph2/input" |
|
|
|
|
|
"git.aiterp.net/rpdata/api/models" |
|
|
|
|
|
"git.aiterp.net/rpdata/api/models/chapters" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// Queries
|
|
|
// Queries
|
|
|
|
|
|
|
|
|
func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) { |
|
|
|
|
|
return chapters.FindID(id) |
|
|
|
|
|
|
|
|
func (r *resolver) Chapter(ctx context.Context, id string) (*models.Chapter, error) { |
|
|
|
|
|
chapter, err := chapters.FindID(id) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &chapter, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Mutations
|
|
|
// Mutations
|
|
|
|
|
|
|
|
|
func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (models.Chapter, error) { |
|
|
|
|
|
|
|
|
func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (*models.Chapter, error) { |
|
|
story, err := stories.FindID(input.StoryID) |
|
|
story, err := stories.FindID(input.StoryID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Story not found") |
|
|
|
|
|
|
|
|
return nil, errors.New("Story not found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
token := auth.TokenFromContext(ctx) |
|
|
if !token.Permitted("member", "story.add") { |
|
|
if !token.Permitted("member", "story.add") { |
|
|
return models.Chapter{}, errors.New("Unauthorized") |
|
|
|
|
|
|
|
|
return nil, errors.New("Unauthorized") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
author := token.UserID |
|
|
author := token.UserID |
|
|
if input.Author != nil && *input.Author != author { |
|
|
if input.Author != nil && *input.Author != author { |
|
|
if !token.Permitted("story.add") { |
|
|
if !token.Permitted("story.add") { |
|
|
return models.Chapter{}, errors.New("False pretender") |
|
|
|
|
|
|
|
|
return nil, errors.New("False pretender") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
author = *input.Author |
|
|
author = *input.Author |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if !story.Open && story.Author != author { |
|
|
if !story.Open && story.Author != author { |
|
|
return models.Chapter{}, errors.New("Story is not open") |
|
|
|
|
|
|
|
|
return nil, errors.New("Story is not open") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commentMode := models.ChapterCommentModeDisabled |
|
|
commentMode := models.ChapterCommentModeDisabled |
|
@ -56,38 +59,38 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd |
|
|
|
|
|
|
|
|
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode) |
|
|
chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error()) |
|
|
|
|
|
|
|
|
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) |
|
|
go changes.Submit("Chapter", "add", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) |
|
|
|
|
|
|
|
|
return chapter, nil |
|
|
|
|
|
|
|
|
return &chapter, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) { |
|
|
|
|
|
|
|
|
func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (*models.Chapter, error) { |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Chapter not found") |
|
|
|
|
|
|
|
|
return nil, errors.New("Chapter not found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
token := auth.TokenFromContext(ctx) |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { |
|
|
return models.Chapter{}, errors.New("You are not allowed to move this chapter") |
|
|
|
|
|
|
|
|
return nil, errors.New("You are not allowed to move this chapter") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
target, err := stories.FindID(input.StoryID) |
|
|
target, err := stories.FindID(input.StoryID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Target story not found") |
|
|
|
|
|
|
|
|
return nil, errors.New("Target story not found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") { |
|
|
if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") { |
|
|
return models.Chapter{}, errors.New("You are not permitted to move chapters to this story") |
|
|
|
|
|
|
|
|
return nil, errors.New("You are not permitted to move chapters to this story") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
oldStoryID := chapter.StoryID |
|
|
oldStoryID := chapter.StoryID |
|
|
chapter, err = chapters.Move(chapter, target) |
|
|
chapter, err = chapters.Move(chapter, target) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Failed to move chapter: " + err.Error()) |
|
|
|
|
|
|
|
|
return nil, errors.New("Failed to move chapter: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
go func() { |
|
|
go func() { |
|
@ -105,18 +108,18 @@ func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterM |
|
|
changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) |
|
|
changes.Submit("Chapter", "move-in", token.UserID, story.Listed, changekeys.Listed(story, chapter), story, chapter) |
|
|
}() |
|
|
}() |
|
|
|
|
|
|
|
|
return chapter, nil |
|
|
|
|
|
|
|
|
return &chapter, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) { |
|
|
|
|
|
|
|
|
func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (*models.Chapter, error) { |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Chapter not found") |
|
|
|
|
|
|
|
|
return nil, errors.New("Chapter not found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
token := auth.TokenFromContext(ctx) |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") { |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") { |
|
|
return models.Chapter{}, errors.New("Unauthorized") |
|
|
|
|
|
|
|
|
return nil, errors.New("Unauthorized") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true { |
|
|
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true { |
|
@ -125,7 +128,7 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE |
|
|
|
|
|
|
|
|
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked) |
|
|
chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error()) |
|
|
|
|
|
|
|
|
return nil, errors.New("Failed to edit chapter: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
go func() { |
|
|
go func() { |
|
@ -137,28 +140,28 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE |
|
|
changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) |
|
|
changes.Submit("Chapter", "edit", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) |
|
|
}() |
|
|
}() |
|
|
|
|
|
|
|
|
return chapter, nil |
|
|
|
|
|
|
|
|
return &chapter, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (models.Chapter, error) { |
|
|
|
|
|
|
|
|
func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.ChapterRemoveInput) (*models.Chapter, error) { |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
chapter, err := chapters.FindID(input.ID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Chapter not found") |
|
|
|
|
|
|
|
|
return nil, errors.New("Chapter not found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
token := auth.TokenFromContext(ctx) |
|
|
token := auth.TokenFromContext(ctx) |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") { |
|
|
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") { |
|
|
return models.Chapter{}, errors.New("Unauthorized") |
|
|
|
|
|
|
|
|
return nil, errors.New("Unauthorized") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
chapter, err = chapters.Remove(chapter) |
|
|
chapter, err = chapters.Remove(chapter) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Failed to remove chapter: " + err.Error()) |
|
|
|
|
|
|
|
|
return nil, errors.New("Failed to remove chapter: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = comments.RemoveChapter(chapter) |
|
|
err = comments.RemoveChapter(chapter) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return models.Chapter{}, errors.New("Chapter was removed, but comment removal failed: " + err.Error()) |
|
|
|
|
|
|
|
|
return nil, errors.New("Chapter was removed, but comment removal failed: " + err.Error()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
go func() { |
|
|
go func() { |
|
@ -170,5 +173,5 @@ func (r *mutationResolver) RemoveChapter(ctx context.Context, input input.Chapte |
|
|
changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) |
|
|
changes.Submit("Chapter", "remove", token.UserID, story.Listed, changekeys.Many(story, chapter), chapter) |
|
|
}() |
|
|
}() |
|
|
|
|
|
|
|
|
return chapter, nil |
|
|
|
|
|
|
|
|
return &chapter, nil |
|
|
} |
|
|
} |