From 22592439dace66b7cce68b65d99a3e1e00efbc7f Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Wed, 16 Jan 2019 22:03:28 +0100 Subject: [PATCH] graph2, models: Added removeComment mutation. --- graph2/queries/comment.go | 28 ++++++++++++++++++++++++++++ graph2/schema/root.gql | 3 +++ graph2/schema/types/Comment.gql | 8 ++++++++ 3 files changed, 39 insertions(+) diff --git a/graph2/queries/comment.go b/graph2/queries/comment.go index f6492e1..5f314c7 100644 --- a/graph2/queries/comment.go +++ b/graph2/queries/comment.go @@ -116,3 +116,31 @@ func (r *mutationResolver) EditComment(ctx context.Context, input input.CommentE return comment, nil } + +func (r *mutationResolver) RemoveComment(ctx context.Context, input input.CommentRemoveInput) (models.Comment, error) { + comment, err := comments.Find(input.CommentID) + if err != nil { + return models.Comment{}, errors.New("Comment not found") + } + + token := auth.TokenFromContext(ctx) + if !token.PermittedUser(comment.Author, "member", "story.edit") { + return models.Comment{}, errors.New("You cannot remove this comment") + } + + chapter, err := chapters.FindID(comment.ChapterID) + if err != nil { + return models.Comment{}, errors.New("Comment's chapter not found") + } + + if !chapter.CanComment() { + return models.Comment{}, errors.New("Comments are disabled or locked") + } + + err = comments.Remove(comment) + if err != nil { + return models.Comment{}, errors.New("Failed to remove comment: " + err.Error()) + } + + return comment, nil +} diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index a522cf5..611edff 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -107,6 +107,9 @@ type Mutation { "Edit a comment in a chapter." editComment(input: CommentEditInput!): Comment! + "Remove a comemnt in a chapter." + removeComment(input: CommentRemoveInput!): Comment! + # Add a new log addLog(input: LogAddInput!): Log! diff --git a/graph2/schema/types/Comment.gql b/graph2/schema/types/Comment.gql index cf58819..2200ba0 100644 --- a/graph2/schema/types/Comment.gql +++ b/graph2/schema/types/Comment.gql @@ -77,4 +77,12 @@ input CommentEditInput { "Change the optional subject for the comment, only shown in some modes." subject: String +} + +""" +Input for the removeComment mutation +""" +input CommentRemoveInput { + "What comment to edit." + commentId: String! } \ No newline at end of file