From 0e2a68a2d4f64cd372621d97ff8608cf7d77bcd1 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Wed, 16 Jan 2019 21:45:00 +0100 Subject: [PATCH] graph2, models: Added editComment mutation. --- graph2/queries/comment.go | 45 ++++++++++++++++++++++++++++++++- graph2/schema/root.gql | 3 +++ graph2/schema/types/Comment.gql | 26 +++++++++++++++++++ models/comments/edit.go | 44 ++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 models/comments/edit.go diff --git a/graph2/queries/comment.go b/graph2/queries/comment.go index 5d6806d..f6492e1 100644 --- a/graph2/queries/comment.go +++ b/graph2/queries/comment.go @@ -69,7 +69,50 @@ func (r *mutationResolver) AddComment(ctx context.Context, input input.CommentAd 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) + go changes.Submit("Comment", "add", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter) + + return comment, nil +} + +func (r *mutationResolver) EditComment(ctx context.Context, input input.CommentEditInput) (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 edit 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") + } + + 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 models.Comment{}, errors.New("Character not found") + } else if character.Author != token.UserID { + return models.Comment{}, 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 models.Comment{}, errors.New("Could not post comment: " + err.Error()) + } + + go changes.Submit("Comment", "edit", token.UserID, true, changekeys.Many(comment, chapter, models.Story{ID: chapter.StoryID}), comment, chapter) return comment, nil } diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index d92873b..a522cf5 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -104,6 +104,9 @@ type Mutation { "Add a comment to a chapter." addComment(input: CommentAddInput!): Comment! + "Edit a comment in a chapter." + editComment(input: CommentEditInput!): Comment! + # Add a new log addLog(input: LogAddInput!): Log! diff --git a/graph2/schema/types/Comment.gql b/graph2/schema/types/Comment.gql index 44e7380..cf58819 100644 --- a/graph2/schema/types/Comment.gql +++ b/graph2/schema/types/Comment.gql @@ -51,4 +51,30 @@ input CommentAddInput { "Optional subject to add to the comment, only shown in some modes." subject: String +} + +""" +Input for the editComment mutation +""" +input CommentEditInput { + "What comment to edit." + commentId: String! + + "Change the markdown source." + source: String + + "Change the name of the character associated with this comment." + characterName: String + + "Change the character assocaited with this comment." + characterId: String + + "Change the optional in-universe date of the comment." + fictionalDate: Date + + "Set to clear the fictional date." + clearFictionalDate: Boolean + + "Change the optional subject for the comment, only shown in some modes." + subject: String } \ No newline at end of file diff --git a/models/comments/edit.go b/models/comments/edit.go new file mode 100644 index 0000000..ee20338 --- /dev/null +++ b/models/comments/edit.go @@ -0,0 +1,44 @@ +package comments + +import ( + "time" + + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo/bson" +) + +// Edit edits a comment. +func Edit(comment models.Comment, source, characterName, characterID, subject *string, fictionalDate *time.Time) (models.Comment, error) { + changes := make(bson.M, 6) + + comment.EditedDate = time.Now() + changes["editedDate"] = comment.EditedDate + + if source != nil { + comment.Source = *source + changes["source"] = *source + } + if characterName != nil { + comment.CharacterName = *characterName + changes["characterName"] = *characterName + } + if characterID != nil { + comment.CharacterID = *characterID + changes["characterId"] = *characterID + } + if subject != nil { + comment.Subject = *subject + changes["subject"] = *subject + } + if fictionalDate != nil { + comment.FictionalDate = *fictionalDate + changes["fictionalDate"] = *fictionalDate + } + + err := collection.UpdateId(comment.ID, bson.M{"$set": changes}) + if err != nil { + return models.Comment{}, err + } + + return comment, nil +}