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/internal/auth" "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 func (r *resolver) Chapter(ctx context.Context, id string) (models.Chapter, error) { return chapters.FindID(id) } // Mutations func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAddInput) (models.Chapter, error) { story, err := stories.FindID(input.StoryID) if err != nil { return models.Chapter{}, errors.New("Story not found") } token := auth.TokenFromContext(ctx) if !token.Permitted("member", "story.add") { return models.Chapter{}, errors.New("Unauthorized") } author := token.UserID if input.Author != nil && *input.Author != author { if !token.Permitted("story.add") { return models.Chapter{}, errors.New("False pretender") } author = *input.Author } if !story.Open && story.Author != author { return models.Chapter{}, errors.New("Story is not open") } chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate) if err != nil { return models.Chapter{}, 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 input.ChapterMoveInput) (models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { return models.Chapter{}, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { return models.Chapter{}, errors.New("You are not allowed to move this chapter") } target, err := stories.FindID(input.StoryID) if err != nil { return models.Chapter{}, errors.New("Target story not found") } 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") } oldStoryID := chapter.StoryID chapter, err = chapters.Move(chapter, target) if err != nil { return models.Chapter{}, 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 input.ChapterEditInput) (models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { return models.Chapter{}, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.edit") { return models.Chapter{}, 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) if err != nil { return models.Chapter{}, 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 input.ChapterRemoveInput) (models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { return models.Chapter{}, errors.New("Chapter not found") } token := auth.TokenFromContext(ctx) if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") { return models.Chapter{}, errors.New("Unauthorized") } chapter, err = chapters.Remove(chapter) if err != nil { return models.Chapter{}, errors.New("Failed to remove chapter: " + 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 }