From 0b07ac0a7053f5674433d4d7610a2f0ed6f735f0 Mon Sep 17 00:00:00 2001 From: Daniel Woznicki Date: Tue, 27 Jun 2023 00:15:04 -0700 Subject: [PATCH] Added support for hiding vote counts as a user setting. Implements #780 --- ...19_add_user_setting_to_hide_vote_counts.py | 32 +++++++++++++++++++ tildes/scripts/clean_private_data.py | 1 + tildes/scss/modules/_topic.scss | 7 ++++ tildes/tildes/models/user/user.py | 1 + tildes/tildes/templates/macros/buttons.jinja2 | 4 +-- .../tildes/templates/macros/comments.jinja2 | 8 ++--- tildes/tildes/templates/macros/topics.jinja2 | 31 ++++++++++++------ tildes/tildes/templates/settings.jinja2 | 29 ++++++++++++++--- tildes/tildes/views/api/web/user.py | 16 ++++++++++ 9 files changed, 109 insertions(+), 20 deletions(-) create mode 100644 tildes/alembic/versions/8056ce0a4f19_add_user_setting_to_hide_vote_counts.py diff --git a/tildes/alembic/versions/8056ce0a4f19_add_user_setting_to_hide_vote_counts.py b/tildes/alembic/versions/8056ce0a4f19_add_user_setting_to_hide_vote_counts.py new file mode 100644 index 0000000..b353de3 --- /dev/null +++ b/tildes/alembic/versions/8056ce0a4f19_add_user_setting_to_hide_vote_counts.py @@ -0,0 +1,32 @@ +"""Add user setting to hide vote counts + +Revision ID: 8056ce0a4f19 +Revises: 55f4c1f951d5 +Create Date: 2023-06-26 05:59:44.862789 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "8056ce0a4f19" +down_revision = "55f4c1f951d5" +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column( + "users", + sa.Column( + "show_vote_counts", + sa.Boolean(), + server_default="true", + nullable=False, + ), + ) + + +def downgrade(): + op.drop_column("users", "show_vote_counts") diff --git a/tildes/scripts/clean_private_data.py b/tildes/scripts/clean_private_data.py index 452b77e..99bcb2d 100644 --- a/tildes/scripts/clean_private_data.py +++ b/tildes/scripts/clean_private_data.py @@ -207,6 +207,7 @@ class DataCleaner: "last_exemplary_label_time": DEFAULT, "_bio_markdown": DEFAULT, "bio_rendered_html": DEFAULT, + "show_vote_counts": DEFAULT, }, synchronize_session=False, ) diff --git a/tildes/scss/modules/_topic.scss b/tildes/scss/modules/_topic.scss index ed1a770..08b167c 100644 --- a/tildes/scss/modules/_topic.scss +++ b/tildes/scss/modules/_topic.scss @@ -236,6 +236,13 @@ .topic-voting-label { font-size: 0.6rem; line-height: 0.6rem; + + // When the vote count is hidden, the topic voting button becomes fairly short. + // This padding helps is somewhat match the original button size. + &.count-hidden { + padding-top: 0.5rem; + padding-bottom: 0.5rem; + } } .topic-text-excerpt { diff --git a/tildes/tildes/models/user/user.py b/tildes/tildes/models/user/user.py index eea2d91..f957eff 100644 --- a/tildes/tildes/models/user/user.py +++ b/tildes/tildes/models/user/user.py @@ -145,6 +145,7 @@ class User(DatabaseModel): ) ) bio_rendered_html: str = deferred(Column(Text)) + show_vote_counts: bool = Column(Boolean, nullable=False, server_default="true") @hybrid_property def bio_markdown(self) -> str: diff --git a/tildes/tildes/templates/macros/buttons.jinja2 b/tildes/tildes/templates/macros/buttons.jinja2 index 4fa84a6..5d2a7ce 100644 --- a/tildes/tildes/templates/macros/buttons.jinja2 +++ b/tildes/tildes/templates/macros/buttons.jinja2 @@ -38,8 +38,8 @@ {% set normal_label = "Vote" %} {% set toggled_label = "Voted" %} - {% if subject.num_votes > 0 %} - {# Append the vote count in parentheses #} + {% if subject.num_votes > 0 and request.user.show_vote_counts %} + {# Append the vote count in parentheses if vote counts are not hidden #} {% set normal_label = normal_label~" ("~subject.num_votes~")" %} {% set toggled_label = toggled_label~" ("~subject.num_votes~")" %} {% endif %} diff --git a/tildes/tildes/templates/macros/comments.jinja2 b/tildes/tildes/templates/macros/comments.jinja2 index 1bec7e4..0e51a5e 100644 --- a/tildes/tildes/templates/macros/comments.jinja2 +++ b/tildes/tildes/templates/macros/comments.jinja2 @@ -98,8 +98,8 @@ {% if request.has_permission('view', comment) %} - {# Show votes at the top only if it's your own comment #} - {% if request.user == comment.user and comment.num_votes > 0 and not comment.is_removed %} + {# Show votes at the top only if it's your own comment and votes counts aren't hidden #} + {% if request.user == comment.user and comment.num_votes > 0 and not comment.is_removed and request.user.show_vote_counts %}
{{ pluralize(comment.num_votes, "vote") }}
{% endif %} @@ -157,8 +157,8 @@ - {# Show non-button vote count if the viewer can't vote and it's not their comment #} - {% if not request.has_permission("vote", comment) and comment.num_votes > 0 and comment.user != request.user %} + {# Show non-button vote count if the viewer can't vote and it's not their comment and don't have vote counts hidden #} + {% if not request.has_permission("vote", comment) and comment.num_votes > 0 and comment.user != request.user and request.user.show_vote_counts %}
{{ pluralize(comment.num_votes, "vote") }}
{% endif %} diff --git a/tildes/tildes/templates/macros/topics.jinja2 b/tildes/tildes/templates/macros/topics.jinja2 index b7e38fb..c62d8ef 100644 --- a/tildes/tildes/templates/macros/topics.jinja2 +++ b/tildes/tildes/templates/macros/topics.jinja2 @@ -183,16 +183,27 @@
{% endif %} - {# Hide voting from the topic's author if it has zero votes #} - {% if request.user != topic.user or topic.num_votes > 0 %} - {{ topic.num_votes }} - - {% trans num_votes=topic.num_votes %} - vote - {% pluralize %} - votes - {% endtrans %} - + {# Hide voting from the topic's author if it has zero votes, or the author has vote counts turned off #} + {# For other users, also hide vote count if turned off, but still show the vote button #} + {% if request.user != topic.user or (request.user == topic.user and topic.num_votes > 0 and request.user.show_vote_counts) %} + {% if request.user.show_vote_counts %} + {{ topic.num_votes }} + + {% trans num_votes=topic.num_votes %} + vote + {% pluralize %} + votes + {% endtrans %} + + {% else %} + + {% if topic.user_voted %} + Voted + {% else %} + Vote + {% endif %} + + {% endif %} {% endif %} {% if request.has_permission('vote', topic) %} diff --git a/tildes/tildes/templates/settings.jinja2 b/tildes/tildes/templates/settings.jinja2 index 064762d..1fca7ad 100644 --- a/tildes/tildes/templates/settings.jinja2 +++ b/tildes/tildes/templates/settings.jinja2 @@ -9,7 +9,7 @@ {% block content %}
-

Display settings

+

Display settings

  • @@ -70,7 +70,7 @@
-

Site behavior settings

+

Site behavior settings

  • @@ -119,6 +119,27 @@
  • +
  • +
    +
    + +
    +
    +
  • +
  • Open links in new tabs

    -

    Notification settings

    +

    Notification settings

    • @@ -253,7 +274,7 @@
    -

    Account settings

    +

    Account settings

    • diff --git a/tildes/tildes/views/api/web/user.py b/tildes/tildes/views/api/web/user.py index ef80973..786b1a9 100644 --- a/tildes/tildes/views/api/web/user.py +++ b/tildes/tildes/views/api/web/user.py @@ -272,6 +272,22 @@ def patch_change_collapse_old_comments(request: Request) -> Response: return IC_NOOP +@ic_view_config( + route_name="user", + request_method="PATCH", + request_param="ic-trigger-name=show-vote-counts", + permission="change_settings", +) +def patch_change_show_vote_counts(request: Request) -> Response: + """Change the user's "display vote counts" setting.""" + user = request.context + + show_vote_counts = bool(request.params.get("show_vote_counts")) + user.show_vote_counts = show_vote_counts + + return IC_NOOP + + @ic_view_config( route_name="user", request_method="PATCH",