From 5faa727ef830f113c5b133009e6c90cbdee7981f Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Wed, 15 May 2019 09:13:39 -0700 Subject: [PATCH] api: add option to delete own comments --- api/comment_delete.go | 8 +++++++- api/comment_get.go | 41 +++++++++++++++++++++++++++++++++++++++++ frontend/js/commento.js | 24 +++++++++++++----------- 3 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 api/comment_get.go diff --git a/api/comment_delete.go b/api/comment_delete.go index d736bb7..81d5a00 100644 --- a/api/comment_delete.go +++ b/api/comment_delete.go @@ -41,6 +41,12 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) { return } + cm, err := commentGetByCommentHex(*x.CommentHex) + if err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) + return + } + domain, _, err := commentDomainPathGet(*x.CommentHex) if err != nil { bodyMarshal(w, response{"success": false, "message": err.Error()}) @@ -53,7 +59,7 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) { return } - if !isModerator { + if !isModerator && cm.CommenterHex != c.CommenterHex { bodyMarshal(w, response{"success": false, "message": errorNotModerator.Error()}) return } diff --git a/api/comment_get.go b/api/comment_get.go new file mode 100644 index 0000000..e75873b --- /dev/null +++ b/api/comment_get.go @@ -0,0 +1,41 @@ +package main + +import ( +) + +func commentGetByCommentHex(commentHex string) (comment, error) { + if commentHex == "" { + return comment{}, errorMissingField + } + + statement := ` + SELECT + commentHex, + commenterHex, + markdown, + html, + parentHex, + score, + state, + creationDate + FROM comments + WHERE comments.commentHex = $1; + ` + row := db.QueryRow(statement, commentHex) + + c := comment{} + if err := row.Scan( + &c.CommentHex, + &c.CommenterHex, + &c.Markdown, + &c.Html, + &c.ParentHex, + &c.Score, + &c.State, + &c.CreationDate); err != nil { + // TODO: is this the only error? + return c, errorNoSuchComment + } + + return c, nil +} diff --git a/frontend/js/commento.js b/frontend/js/commento.js index 7bfdb73..f5a2a40 100644 --- a/frontend/js/commento.js +++ b/frontend/js/commento.js @@ -936,18 +936,20 @@ append(options, reply); - if (isModerator) { - if (parentHex === "root") { - append(options, sticky); - } + if (isModerator && parentHex === "root") { + append(options, sticky); + } + + if (isModerator || comment.commenterHex === selfHex) { append(options, remove); - if (comment.state !== "approved") { - append(options, approve); - } - } else { - if (stickyCommentHex === comment.commentHex) { - append(options, sticky); - } + } + + if (isModerator && comment.state !== "approved") { + append(options, approve); + } + + if (!isModerator && stickyCommentHex === comment.commentHex) { + append(options, sticky); } attrSet(options, "style", "width: " + ((options.childNodes.length+1)*32) + "px;");