From 56ecf48b575ffdd64ead868597b0702a0ba5e993 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Tue, 15 Jan 2019 21:34:57 +0100 Subject: [PATCH] graph2, models: Added addComment mutation. --- cmd/rpdata-server/main.go | 2 ++ graph2/queries/comment.go | 60 +++++++++++++++++++++++++++++++++ graph2/schema/root.gql | 4 +++ graph2/schema/types/Comment.gql | 23 +++++++++++++ models/comments/add.go | 35 +++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 models/comments/add.go diff --git a/cmd/rpdata-server/main.go b/cmd/rpdata-server/main.go index b909717..9f18f62 100644 --- a/cmd/rpdata-server/main.go +++ b/cmd/rpdata-server/main.go @@ -64,6 +64,8 @@ func logListedChanges() { log.Printf("\tTag: (%s,%s)", object.Kind, object.Name) case models.Chapter: log.Printf("\tChapter: %s", object.ID) + case models.Comment: + log.Printf("\tComment: %s", object.ID) } } } diff --git a/graph2/queries/comment.go b/graph2/queries/comment.go index 309d9b7..5d6806d 100644 --- a/graph2/queries/comment.go +++ b/graph2/queries/comment.go @@ -2,7 +2,18 @@ package queries import ( "context" + "errors" + "time" + "git.aiterp.net/rpdata/api/models/characters" + + "git.aiterp.net/rpdata/api/models/changekeys" + + "git.aiterp.net/rpdata/api/models/changes" + + "git.aiterp.net/rpdata/api/graph2/input" + "git.aiterp.net/rpdata/api/internal/auth" + "git.aiterp.net/rpdata/api/models/chapters" "git.aiterp.net/rpdata/api/models/comments" "git.aiterp.net/rpdata/api/models" @@ -13,3 +24,52 @@ import ( func (r *resolver) Comment(ctx context.Context, id string) (models.Comment, error) { return comments.Find(id) } + +// Mutations + +func (r *mutationResolver) AddComment(ctx context.Context, input input.CommentAddInput) (models.Comment, error) { + chapter, err := chapters.FindID(input.ChapterID) + if err != nil { + return models.Comment{}, errors.New("Chapter not found") + } + + token := auth.TokenFromContext(ctx) + if !token.Permitted("member", "story.edit") { + return models.Comment{}, errors.New("Unauthorized") + } + + if !chapter.CanComment() { + return models.Comment{}, errors.New("You cannot comment on this chapter") + } + + var characterPtr *models.Character + if input.CharacterID != nil { + character, err := characters.FindID(*input.CharacterID) + if err != nil { + return models.Comment{}, errors.New("Character not found") + } else if character.Author != token.UserID { + return models.Comment{}, errors.New("That is not your character") + } + + characterPtr = &character + } + + fictionalDate := time.Time{} + if input.FictionalDate != nil { + fictionalDate = *input.FictionalDate + } + + subject := "" + if input.Subject != nil { + subject = *input.Subject + } + + comment, err := comments.Add(chapter, subject, token.UserID, input.Source, input.CharacterName, characterPtr, time.Now(), fictionalDate) + if err != nil { + return models.Comment{}, errors.New("Failed to add comment: " + err.Error()) + } + + go changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment) + + return comment, nil +} diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index 892bd08..d92873b 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -101,6 +101,10 @@ type Mutation { removeChapter(input: ChapterRemoveInput!): Chapter! + "Add a comment to a chapter." + addComment(input: CommentAddInput!): Comment! + + # Add a new log addLog(input: LogAddInput!): Log! diff --git a/graph2/schema/types/Comment.gql b/graph2/schema/types/Comment.gql index dfcf97c..44e7380 100644 --- a/graph2/schema/types/Comment.gql +++ b/graph2/schema/types/Comment.gql @@ -28,4 +28,27 @@ type Comment { "The markdown source of the comment." source: String! +} + +""" +Input for the addComment mutation +""" +input CommentAddInput { + "What chapter to comment on." + chapterId: String! + + "The markdown source of the comment." + source: String! + + "The name of the character associated with this comment." + characterName: String! + + "Optioanlly, a character ID." + characterId: String + + "Optional in-universe date of the comment." + fictionalDate: Date + + "Optional subject to add to the comment, only shown in some modes." + subject: String } \ No newline at end of file diff --git a/models/comments/add.go b/models/comments/add.go new file mode 100644 index 0000000..2958f24 --- /dev/null +++ b/models/comments/add.go @@ -0,0 +1,35 @@ +package comments + +import ( + "time" + + "git.aiterp.net/rpdata/api/models" +) + +// Add adds a comment. +func Add(chapter models.Chapter, subject, author, source, characterName string, character *models.Character, createdDate time.Time, fictionalDate time.Time) (models.Comment, error) { + characterID := "" + if character != nil { + characterID = character.ID + } + + comment := models.Comment{ + ID: makeCommentID(), + ChapterID: chapter.ID, + Subject: subject, + Author: author, + CharacterName: characterName, + CharacterID: characterID, + FictionalDate: fictionalDate, + CreatedDate: createdDate, + EditedDate: createdDate, + Source: source, + } + + err := collection.Insert(comment) + if err != nil { + return models.Comment{}, err + } + + return comment, nil +}