Browse Source

Change comment voting to not replace comment

This switches comment voting to use the "toggle buttons" that most other
actions are using (e.g. Bookmark). This makes it so that only the vote
button is replaced, instead of the entire comment's contents. This is
mostly significant when the comment contains a <details> block, because
previously voting/unvoting while one of those was expanded would cause
it to collapse when the comment was replaced.

As another side-effect of this, voting will now always appear to
increase the count by 1, and unvoting will always appear to decrease it
by 1. Previously it would re-query the comment, which could result in
larger jumps in the vote count if other people were voting at the same
time. That confused some people, so this will probably be better.
merge-requests/86/head
Deimos 5 years ago
parent
commit
611a2928e4
  1. 9
      tildes/tildes/templates/macros/buttons.jinja2
  2. 24
      tildes/tildes/templates/macros/comments.jinja2
  3. 26
      tildes/tildes/views/api/web/comment.py

9
tildes/tildes/templates/macros/buttons.jinja2

@ -22,6 +22,15 @@
{% set toggled_label = "Un-remove" %}
{% set normal_confirm = "Remove this " + type_name + "?" %}
{% set toggled_confirm = "Un-remove this " + type_name + "?" %}
{% elif name == "vote" %}
{% set normal_label = "Vote" %}
{% set toggled_label = "Voted" %}
{% if subject.num_votes > 0 %}
{# Append the vote count in parentheses #}
{% set normal_label = normal_label~" ("~subject.num_votes~")" %}
{% set toggled_label = toggled_label~" ("~subject.num_votes~")" %}
{% endif %}
{% endif %}
<li>

24
tildes/tildes/templates/macros/comments.jinja2

@ -151,29 +151,7 @@
{% endif %}
{% if request.has_permission('vote', comment) %}
{% if comment.user_voted %}
<li><button class="btn-post-action btn-post-action-used" name="unvote"
data-ic-delete-from="{{ request.route_url(
'ic_comment_vote',
comment_id36=comment.comment_id36,
) }}"
data-ic-target="#comment-{{ comment.comment_id36 }} .comment-itself:first"
data-ic-replace-target="true"
>Voted
{% else %}
<li><button class="btn-post-action" name="vote"
data-ic-put-to="{{ request.route_url(
'ic_comment_vote',
comment_id36=comment.comment_id36,
) }}"
data-ic-target="#comment-{{ comment.comment_id36 }} .comment-itself:first"
data-ic-replace-target="true"
>Vote
{% endif %}
{% if comment.num_votes > 0 %}
({{ comment.num_votes }})
{% endif %}
</button></li>
{{ post_action_toggle_button("vote", comment, is_toggled=comment.user_voted) }}
{% endif %}
{% if request.has_permission('label', comment) %}

26
tildes/tildes/views/api/web/comment.py

@ -213,7 +213,7 @@ def delete_comment(request: Request) -> dict:
route_name="comment_vote",
request_method="PUT",
permission="vote",
renderer="comment_contents.jinja2",
renderer="post_action_toggle_button.jinja2",
)
def put_vote_comment(request: Request) -> dict:
"""Vote on a comment with Intercooler."""
@ -237,22 +237,17 @@ def put_vote_comment(request: Request) -> dict:
# the user has already voted on this comment
savepoint.rollback()
# re-query the comment to get complete data
comment = (
request.query(Comment)
.join_all_relationships()
.filter_by(comment_id=comment.comment_id)
.one()
)
# a trigger updates this - set it manually so the button displays the correct number
comment.num_votes += 1
return {"comment": comment}
return {"name": "vote", "subject": comment, "is_toggled": True}
@ic_view_config(
route_name="comment_vote",
request_method="DELETE",
permission="vote",
renderer="comment_contents.jinja2",
renderer="post_action_toggle_button.jinja2",
)
def delete_vote_comment(request: Request) -> dict:
"""Remove the user's vote from a comment with Intercooler."""
@ -269,15 +264,10 @@ def delete_vote_comment(request: Request) -> dict:
# manually commit the transaction so triggers will execute
request.tm.commit()
# re-query the comment to get complete data
comment = (
request.query(Comment)
.join_all_relationships()
.filter_by(comment_id=comment.comment_id)
.one()
)
# a trigger updates this - set it manually so the button displays the correct number
comment.num_votes -= 1
return {"comment": comment}
return {"name": "vote", "subject": comment, "is_toggled": False}
@ic_view_config(

Loading…
Cancel
Save