Finished refactor of story/chapter/comment related stuff.
the build was successful

This commit is contained in:
2019-08-25 16:10:10 +02:00
parent 8c4670580c
commit 2eed815993
22 changed files with 737 additions and 502 deletions
+19 -125
View File
@@ -2,54 +2,24 @@ 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
return r.s.Stories.FindChapter(ctx, id)
}
// Mutations
func (r *mutationResolver) AddChapter(ctx context.Context, input graphcore.ChapterAddInput) (*models.Chapter, error) {
story, err := stories.FindID(input.StoryID)
story, err := r.s.Stories.FindStory(ctx, 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")
return nil, err
}
commentMode := models.ChapterCommentModeDisabled
@@ -57,121 +27,45 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input graphcore.Chapt
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
return r.s.Stories.CreateChapter(ctx, *story, input.Title, input.Source, input.Author, time.Now(), input.FictionalDate, commentMode)
}
func (r *mutationResolver) MoveChapter(ctx context.Context, input graphcore.ChapterMoveInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID)
chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
if err != nil {
return nil, errors.New("Chapter not found")
return nil, err
}
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)
from, err := r.s.Stories.FindStory(ctx, chapter.StoryID)
if err != nil {
return nil, errors.New("Target story not found")
return nil, err
}
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)
to, err := r.s.Stories.FindStory(ctx, input.StoryID)
if err != nil {
return nil, errors.New("Failed to move chapter: " + err.Error())
return nil, err
}
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
return r.s.Stories.MoveChapter(ctx, chapter, *from, *to)
}
func (r *mutationResolver) EditChapter(ctx context.Context, input graphcore.ChapterEditInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID)
chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
if err != nil {
return nil, errors.New("Chapter not found")
return nil, err
}
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
return r.s.Stories.EditChapter(ctx, chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked)
}
func (r *mutationResolver) RemoveChapter(ctx context.Context, input graphcore.ChapterRemoveInput) (*models.Chapter, error) {
chapter, err := chapters.FindID(input.ID)
chapter, err := r.s.Stories.FindChapter(ctx, input.ID)
if err != nil {
return nil, errors.New("Chapter not found")
return nil, err
}
token := auth.TokenFromContext(ctx)
if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.remove") {
return nil, errors.New("Unauthorized")
}
chapter, err = chapters.Remove(chapter)
err = r.s.Stories.RemoveChapter(ctx, chapter)
if err != nil {
return nil, errors.New("Failed to remove chapter: " + err.Error())
return nil, err
}
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
return chapter, nil
}
+18 -129
View File
@@ -2,64 +2,24 @@ package resolvers
import (
"context"
"errors"
"log"
"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/characters"
"git.aiterp.net/rpdata/api/models/comments"
"git.aiterp.net/rpdata/api/models/stories"
)
// Queries
func (r *queryResolver) Comment(ctx context.Context, id string) (*models.Comment, error) {
comment, err := comments.Find(id)
if err != nil {
return nil, err
}
return &comment, nil
return r.s.Stories.FindComment(ctx, id)
}
// Mutations
func (r *mutationResolver) AddComment(ctx context.Context, input graphcore.CommentAddInput) (*models.Comment, error) {
chapter, err := chapters.FindID(input.ChapterID)
chapter, err := r.s.Stories.FindChapter(ctx, input.ChapterID)
if err != nil {
return nil, errors.New("Chapter not found")
}
token := auth.TokenFromContext(ctx)
if !token.Permitted("member", "story.edit") {
return nil, errors.New("Unauthorized")
}
if !chapter.CanComment() {
return nil, errors.New("Comments are disabled or locked")
}
var characterPtr *models.Character
if input.CharacterID != nil {
character, err := characters.FindID(*input.CharacterID)
if err != nil {
return nil, errors.New("Character not found")
} else if character.Author != token.UserID {
return nil, errors.New("That is not your character")
}
characterPtr = &character
}
fictionalDate := time.Time{}
if input.FictionalDate != nil {
fictionalDate = *input.FictionalDate
return nil, err
}
subject := ""
@@ -67,109 +27,38 @@ func (r *mutationResolver) AddComment(ctx context.Context, input graphcore.Comme
subject = *input.Subject
}
comment, err := comments.Add(chapter, subject, token.UserID, input.Source, input.CharacterName, characterPtr, time.Now(), fictionalDate)
if err != nil {
return nil, errors.New("Failed to add comment: " + err.Error())
fictionalDate := time.Time{}
if input.FictionalDate != nil {
fictionalDate = *input.FictionalDate
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return &comment, nil
return r.s.Stories.CreateComment(ctx, *chapter, subject, "", input.Source, input.CharacterName, input.CharacterID, time.Now(), fictionalDate)
}
func (r *mutationResolver) EditComment(ctx context.Context, input graphcore.CommentEditInput) (*models.Comment, error) {
comment, err := comments.Find(input.CommentID)
comment, err := r.s.Stories.FindComment(ctx, input.CommentID)
if err != nil {
return nil, errors.New("Comment not found")
return nil, err
}
token := auth.TokenFromContext(ctx)
if !token.PermittedUser(comment.Author, "member", "story.edit") {
return nil, errors.New("You cannot edit this comment")
fictionalDate := input.FictionalDate
if input.ClearFictionalDate != nil && *input.ClearFictionalDate {
fictionalDate = &time.Time{}
}
chapter, err := chapters.FindID(comment.ChapterID)
if err != nil {
return nil, errors.New("Comment's chapter not found")
}
if !chapter.CanComment() {
return nil, errors.New("Comments are disabled or locked")
}
if input.ClearFictionalDate != nil && *input.ClearFictionalDate == true {
input.FictionalDate = &time.Time{}
}
if input.CharacterID != nil && *input.CharacterID != "" {
character, err := characters.FindID(*input.CharacterID)
if err != nil {
return nil, errors.New("Character not found")
} else if character.Author != token.UserID {
return nil, errors.New("That is not your character")
}
}
comment, err = comments.Edit(comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, input.FictionalDate)
if err != nil {
return nil, errors.New("Could not post comment: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "edit", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return &comment, nil
return r.s.Stories.EditComment(ctx, comment, input.Source, input.CharacterName, input.CharacterID, input.Subject, fictionalDate)
}
func (r *mutationResolver) RemoveComment(ctx context.Context, input graphcore.CommentRemoveInput) (*models.Comment, error) {
comment, err := comments.Find(input.CommentID)
comment, err := r.s.Stories.FindComment(ctx, input.CommentID)
if err != nil {
return nil, errors.New("Comment not found")
return nil, err
}
token := auth.TokenFromContext(ctx)
if !token.PermittedUser(comment.Author, "member", "story.edit") {
return nil, errors.New("You cannot remove this comment")
}
chapter, err := chapters.FindID(comment.ChapterID)
err = r.s.Stories.RemoveComment(ctx, comment)
if err != nil {
return nil, errors.New("Comment's chapter not found")
return nil, err
}
if !chapter.CanComment() {
return nil, errors.New("Comments are disabled or locked")
}
err = comments.Remove(comment)
if err != nil {
return nil, errors.New("Failed to remove comment: " + err.Error())
}
go func() {
story, err := stories.FindID(chapter.StoryID)
if err != nil {
log.Println("WARNING: Couldn't log comment change:", err)
return
}
changes.Submit("Comment", "remove", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter, story)
}()
return &comment, nil
return comment, nil
}
+41 -160
View File
@@ -2,69 +2,33 @@ 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/stories"
)
func (r *queryResolver) Story(ctx context.Context, id string) (*models.Story, error) {
story, err := stories.FindID(id)
if err != nil {
return nil, err
}
return &story, nil
return r.s.Stories.FindStory(ctx, id)
}
func (r *queryResolver) 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
}
}
func (r *queryResolver) Stories(ctx context.Context, filter *models.StoryFilter) ([]*models.Story, error) {
if filter == nil {
filter = &models.StoryFilter{}
}
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
return r.s.Stories.ListStories(ctx, *filter)
}
// Mutations
func (r *mutationResolver) AddStory(ctx context.Context, input graphcore.StoryAddInput) (*models.Story, error) {
token := auth.TokenFromContext(ctx)
if token == nil || !token.Permitted("member", "story.add") {
return nil, errors.New("Permission denied")
}
listed := input.Listed != nil && *input.Listed
open := input.Open != nil && *input.Open
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
tags := make([]models.Tag, len(input.Tags))
for i, tag := range input.Tags {
tags[i] = *tag
}
fictionalDate := time.Time{}
@@ -72,129 +36,46 @@ func (r *mutationResolver) AddStory(ctx context.Context, input graphcore.StoryAd
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
return r.s.Stories.CreateStory(ctx, input.Name, input.Author, input.Category, listed, open, tags, time.Now(), fictionalDate)
}
func (r *mutationResolver) AddStoryTag(ctx context.Context, input graphcore.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 graphcore.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 graphcore.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 graphcore.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)
story, err := r.s.Stories.FindStory(ctx, input.ID)
if err != nil {
return nil, err
}
err = chapters.RemoveStory(story)
return r.s.Stories.AddStoryTag(ctx, *story, *input.Tag)
}
func (r *mutationResolver) RemoveStoryTag(ctx context.Context, input graphcore.StoryTagRemoveInput) (*models.Story, error) {
story, err := r.s.Stories.FindStory(ctx, input.ID)
if err != nil {
return nil, errors.New("Failed to remove chapters, but story is removed: " + err.Error())
return nil, err
}
go changes.Submit("Story", "remove", token.UserID, story.Listed, changekeys.Listed(story), story)
return r.s.Stories.RemoveStoryTag(ctx, *story, *input.Tag)
}
return &story, nil
func (r *mutationResolver) EditStory(ctx context.Context, input graphcore.StoryEditInput) (*models.Story, error) {
story, err := r.s.Stories.FindStory(ctx, input.ID)
if err != nil {
return nil, err
}
return r.s.Stories.EditStory(ctx, story, input.Name, input.Category, input.Listed, input.Open, input.FictionalDate)
}
func (r *mutationResolver) RemoveStory(ctx context.Context, input graphcore.StoryRemoveInput) (*models.Story, error) {
story, err := r.s.Stories.FindStory(ctx, input.ID)
if err != nil {
return nil, err
}
err = r.s.Stories.RemoveStory(ctx, story)
if err != nil {
return nil, err
}
return story, err
}