diff --git a/app/community/routes.py b/app/community/routes.py index c4346b45..1b131c7c 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -237,8 +237,55 @@ def show_post(post_id: int): flash('Your comment has been added.') # todo: flush cache # todo: federation - replies = post_replies(post.id, 'top', show_first=reply.id) + return redirect(url_for('community.show_post', post_id=post_id)) # redirect to current page to avoid refresh resubmitting the form else: replies = post_replies(post.id, 'top') return render_template('community/post.html', title=post.title, post=post, is_moderator=is_moderator, canonical=post.ap_id, form=form, replies=replies) + + +@bp.route('/comment//', methods=['POST']) +def comment_vote(comment_id, vote_direction): + upvoted_class = downvoted_class = '' + comment = PostReply.query.get_or_404(comment_id) + existing_vote = PostReplyVote.query.filter_by(user_id=current_user.id, post_reply_id=comment.id).first() + if existing_vote: + if existing_vote.effect > 0: # previous vote was up + if vote_direction == 'upvote': # new vote is also up, so remove it + db.session.delete(existing_vote) + comment.up_votes -= 1 + comment.score -= 1 + else: # new vote is down while previous vote was up, so reverse their previous vote + existing_vote.effect = -1 + comment.up_votes -= 1 + comment.down_votes += 1 + comment.score -= 2 + downvoted_class = 'voted_down' + else: # previous vote was down + if vote_direction == 'upvote': # new vote is upvote + existing_vote.effect = 1 + comment.up_votes += 1 + comment.down_votes -= 1 + comment.score += 1 + upvoted_class = 'voted_up' + else: # reverse a previous downvote + db.session.delete(existing_vote) + comment.down_votes -= 1 + comment.score += 2 + else: + if vote_direction == 'upvote': + effect = 1 + comment.up_votes += 1 + comment.score += 1 + upvoted_class = 'voted_up' + else: + effect = -1 + comment.down_votes += 1 + comment.score -= 1 + downvoted_class = 'voted_down' + vote = PostReplyVote(user_id=current_user.id, post_reply_id=comment_id, author_id=comment.user_id, effect=effect) + db.session.add(vote) + db.session.commit() + return render_template('community/_voting_buttons.html', comment=comment, + upvoted_class=upvoted_class, + downvoted_class=downvoted_class) diff --git a/app/static/structure.css b/app/static/structure.css index aef08294..5fc8ea26 100644 --- a/app/static/structure.css +++ b/app/static/structure.css @@ -370,6 +370,7 @@ fieldset legend { .comment .voting_buttons .upvote_button, .comment .voting_buttons .downvote_button { padding-left: 3px; border-radius: 3px; + cursor: pointer; } .comment .voting_buttons .upvote_button.digits_4, .comment .voting_buttons .downvote_button.digits_4 { width: 68px; @@ -380,6 +381,14 @@ fieldset legend { .comment .voting_buttons .upvote_button.digits_6, .comment .voting_buttons .downvote_button.digits_6 { width: 84px; } +.comment .voting_buttons .upvote_button.voted_up, .comment .voting_buttons .downvote_button.voted_up { + color: green; + font-weight: bold; +} +.comment .voting_buttons .upvote_button.voted_down, .comment .voting_buttons .downvote_button.voted_down { + color: darkred; + font-weight: bold; +} .comment .voting_buttons .downvote_button { margin-top: 5px; } diff --git a/app/static/structure.scss b/app/static/structure.scss index 3d664932..2ce49242 100644 --- a/app/static/structure.scss +++ b/app/static/structure.scss @@ -171,6 +171,7 @@ nav, etc which are used site-wide */ .upvote_button, .downvote_button { padding-left: 3px; border-radius: 3px; + cursor: pointer; &.digits_4 { width: 68px; @@ -183,6 +184,15 @@ nav, etc which are used site-wide */ &.digits_6 { width: 84px; } + + &.voted_up { + color: green; + font-weight: bold; + } + &.voted_down { + color: darkred; + font-weight: bold; + } } .downvote_button { diff --git a/app/templates/community/_voting_buttons.html b/app/templates/community/_voting_buttons.html new file mode 100644 index 00000000..6c300dc3 --- /dev/null +++ b/app/templates/community/_voting_buttons.html @@ -0,0 +1,10 @@ +
+ + {{ comment.up_votes }} +
+
+ + {{ comment.down_votes }} +
\ No newline at end of file diff --git a/app/templates/community/post.html b/app/templates/community/post.html index acc9de67..1508287f 100644 --- a/app/templates/community/post.html +++ b/app/templates/community/post.html @@ -82,12 +82,9 @@ {% macro render_comment(comment) %}
- - + {% with comment=comment['comment'] %} + {% include "community/_voting_buttons.html" %} + {% endwith %}