From ee7875cc1e435aaadb94846055baa88faa2c57be Mon Sep 17 00:00:00 2001 From: Adhityaa Chandrasekar Date: Fri, 13 Sep 2019 18:13:38 -0700 Subject: [PATCH] commento.js: don't recursively delete comments --- api/comment.go | 1 + api/comment_delete.go | 5 +++-- api/comment_get.go | 2 ++ api/comment_list.go | 2 ++ db/20190913175445-delete-comments.sql | 6 ++++++ frontend/js/commento.js | 28 ++++++++++++++++----------- 6 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 db/20190913175445-delete-comments.sql diff --git a/api/comment.go b/api/comment.go index 11a3f4e..2022ec3 100644 --- a/api/comment.go +++ b/api/comment.go @@ -16,4 +16,5 @@ type comment struct { State string `json:"state,omitempty"` CreationDate time.Time `json:"creationDate"` Direction int `json:"direction"` + Deleted bool `json:"deleted"` } diff --git a/api/comment_delete.go b/api/comment_delete.go index 81d5a00..81b8a77 100644 --- a/api/comment_delete.go +++ b/api/comment_delete.go @@ -10,8 +10,9 @@ func commentDelete(commentHex string) error { } statement := ` - DELETE FROM comments - WHERE commentHex=$1; + UPDATE comments + SET deleted = true, markdown = '[deleted]', html = '[deleted]', commenterHex = 'anonymous' + WHERE commentHex = $1; ` _, err := db.Exec(statement, commentHex) diff --git a/api/comment_get.go b/api/comment_get.go index 90c984b..5c14540 100644 --- a/api/comment_get.go +++ b/api/comment_get.go @@ -16,6 +16,7 @@ func commentGetByCommentHex(commentHex string) (comment, error) { parentHex, score, state, + deleted, creationDate FROM comments WHERE comments.commentHex = $1; @@ -31,6 +32,7 @@ func commentGetByCommentHex(commentHex string) (comment, error) { &c.ParentHex, &c.Score, &c.State, + &c.Deleted, &c.CreationDate); err != nil { // TODO: is this the only error? return c, errorNoSuchComment diff --git a/api/comment_list.go b/api/comment_list.go index 353f5ec..8827417 100644 --- a/api/comment_list.go +++ b/api/comment_list.go @@ -20,6 +20,7 @@ func commentList(commenterHex string, domain string, path string, includeUnappro parentHex, score, state, + deleted, creationDate FROM comments WHERE @@ -66,6 +67,7 @@ func commentList(commenterHex string, domain string, path string, includeUnappro &c.ParentHex, &c.Score, &c.State, + &c.Deleted, &c.CreationDate); err != nil { return nil, nil, errorInternal } diff --git a/db/20190913175445-delete-comments.sql b/db/20190913175445-delete-comments.sql new file mode 100644 index 0000000..1ae224d --- /dev/null +++ b/db/20190913175445-delete-comments.sql @@ -0,0 +1,6 @@ +DROP TRIGGER IF EXISTS commentsDeleteTrigger ON comments; + +DROP FUNCTION IF EXISTS commentsDeleteTriggerFunction; + +ALTER TABLE comments + ADD deleted BOOLEAN NOT NULL DEFAULT false; diff --git a/frontend/js/commento.js b/frontend/js/commento.js index afe00ab..7aaf067 100644 --- a/frontend/js/commento.js +++ b/frontend/js/commento.js @@ -764,9 +764,9 @@ } cur.sort(function(a, b) { - if (a.commentHex === stickyCommentHex) { + if (!a.deleted && a.commentHex === stickyCommentHex) { return -Infinity; - } else if (b.commentHex === stickyCommentHex) { + } else if (!b.deleted && b.commentHex === stickyCommentHex) { return Infinity; } @@ -852,7 +852,11 @@ timeago.title = comment.creationDate.toString(); card.style["borderLeft"] = "2px solid " + color; - name.innerText = commenter.name; + if (comment.deleted) { + name.innerText = "[deleted]"; + } else { + name.innerText = commenter.name; + } text.innerHTML = comment.html; timeago.innerHTML = timeDifference(curTime, comment.creationDate); score.innerText = scorify(comment.score); @@ -947,20 +951,22 @@ append(options, collapse); - append(options, downvote); - append(options, upvote); + if (!comment.deleted) { + append(options, downvote); + append(options, upvote); + } if (comment.commenterHex === selfHex) { append(options, edit); - } else { + } else if (!comment.deleted) { append(options, reply); } - if (isModerator && parentHex === "root") { + if (!comment.deleted && (isModerator && parentHex === "root")) { append(options, sticky); } - if (isModerator || comment.commenterHex === selfHex) { + if (!comment.deleted && (isModerator || comment.commenterHex === selfHex)) { append(options, remove); } @@ -968,7 +974,7 @@ append(options, approve); } - if (!isModerator && stickyCommentHex === comment.commentHex) { + if (!comment.deleted && (!isModerator && stickyCommentHex === comment.commentHex)) { append(options, sticky); } @@ -1048,8 +1054,8 @@ errorHide(); } - var card = $(ID_CARD + commentHex); - remove(card); + var text = $(ID_TEXT + commentHex); + text.innerText = "[deleted]"; }); }