From e93733510b621057f4fdc6859f1f5987387dab25 Mon Sep 17 00:00:00 2001 From: Adhityaa Date: Thu, 14 Jun 2018 14:40:19 +0530 Subject: [PATCH] api: don't auto-upvote new comments --- api/comment_list_test.go | 2 +- api/comment_new.go | 4 ---- api/comment_new_test.go | 4 ++-- api/comment_vote.go | 17 ++++++++++++++++ api/comment_vote_test.go | 44 +++++++++++++++++++++++----------------- api/errors.go | 1 + 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/api/comment_list_test.go b/api/comment_list_test.go index 437abdc..1d677f4 100644 --- a/api/comment_list_test.go +++ b/api/comment_list_test.go @@ -47,7 +47,7 @@ func TestCommentListBasics(t *testing.T) { return } - if c[0].Direction != 1 { + if c[0].Direction != 0 { t.Errorf("expected c.Direction = 1 got c.Direction = %d", c[0].Direction) return } diff --git a/api/comment_new.go b/api/comment_new.go index 647d827..b832814 100644 --- a/api/comment_new.go +++ b/api/comment_new.go @@ -31,10 +31,6 @@ func commentNew(commenterHex string, domain string, path string, parentHex strin return "", errorInternal } - if err = commentVote(commenterHex, commentHex, 1); err != nil { - logger.Warningf("error: cannot upvote new comment automatically: %v", err) - } - return commentHex, nil } diff --git a/api/comment_new_test.go b/api/comment_new_test.go index 3d29815..daad2ca 100644 --- a/api/comment_new_test.go +++ b/api/comment_new_test.go @@ -51,8 +51,8 @@ func TestCommentNewUpvoted(t *testing.T) { return } - if score != 1 { - t.Errorf("expected comment to be auto-upvoted") + if score != 0 { + t.Errorf("expected comment to be at 0 points") return } } diff --git a/api/comment_vote.go b/api/comment_vote.go index 591c6d1..3cebf6d 100644 --- a/api/comment_vote.go +++ b/api/comment_vote.go @@ -11,6 +11,23 @@ func commentVote(commenterHex string, commentHex string, direction int) error { } statement := ` + SELECT commenterHex + FROM comments + WHERE commentHex = $1; + ` + row := db.QueryRow(statement, commentHex) + + var authorHex string + if err := row.Scan(&authorHex); err != nil { + logger.Errorf("erorr selecting authorHex for vote") + return errorInternal + } + + if authorHex == commenterHex { + return errorSelfVote + } + + statement = ` INSERT INTO votes (commentHex, commenterHex, direction, voteDate) VALUES ($1, $2, $3, $4 ) diff --git a/api/comment_vote_test.go b/api/comment_vote_test.go index bbb2c66..95583fe 100644 --- a/api/comment_vote_test.go +++ b/api/comment_vote_test.go @@ -14,42 +14,48 @@ func TestCommentVoteBasics(t *testing.T) { c0, _ := commentNew(cr0, "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC()) - commentVote(cr0, c0, -1) - if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -1 { - t.Errorf("expected c[0].Score = -1 got c[0].Score = %d", c[0].Score) + if err := commentVote(cr0, c0, 1); err != errorSelfVote { + t.Errorf("expected err=errorSelfVote got err=%v", err) return } - commentVote(cr1, c0, -1) - commentVote(cr2, c0, -1) - if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -3 { - t.Errorf("expected c[0].Score = -3 got c[0].Score = %d", c[0].Score) + if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != 0 { + t.Errorf("expected c[0].Score = 0 got c[0].Score = %d", c[0].Score) return } - commentVote(cr1, c0, -1) - if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -3 { - t.Errorf("expected c[0].Score = -3 got c[0].Score = %d", c[0].Score) + if err := commentVote(cr1, c0, -1); err != nil { + t.Errorf("unexpected error voting: %v", err) + return + } + + if err := commentVote(cr2, c0, -1); err != nil { + t.Errorf("unexpected error voting: %v", err) return } - commentVote(cr1, c0, 0) if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -2 { t.Errorf("expected c[0].Score = -2 got c[0].Score = %d", c[0].Score) return } - c1, _ := commentNew(cr1, "example.com", "/path.html", "root", "**bar**", "approved", time.Now().UTC()) - - commentVote(cr0, c1, 0) - if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[1].Score != 1 { - t.Errorf("expected c[1].Score = 1 got c[1].Score = %d", c[1].Score) + if err := commentVote(cr1, c0, -1); err != nil { + t.Errorf("unexpected error voting: %v", err) return } - commentVote(cr1, c1, 0) - if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[1].Score != 0 { - t.Errorf("expected c[1].Score = 0 got c[1].Score = %d", c[1].Score) + if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -2 { + t.Errorf("expected c[0].Score = -2 got c[0].Score = %d", c[0].Score) + return + } + + if err := commentVote(cr1, c0, 0); err != nil { + t.Errorf("unexpected error voting: %v", err) + return + } + + if c, _, _ := commentList("temp", "example.com", "/path.html", false); c[0].Score != -1 { + t.Errorf("expected c[0].Score = -1 got c[0].Score = %d", c[0].Score) return } } diff --git a/api/errors.go b/api/errors.go index 99f9d06..c4834f6 100644 --- a/api/errors.go +++ b/api/errors.go @@ -39,3 +39,4 @@ var errorNotModerator = errors.New("You need to be a moderator to do that.") var errorNotADirectory = errors.New("The given path is not a directory.") var errorGzip = errors.New("Cannot GZip content.") var errorCannotDownloadDisqus = errors.New("We could not download your Disqus export file.") +var errorSelfVote = errors.New("You cannot vote on your own comment.")