From e6d81db15f36feb3576907dc2fd05001d8cfa534 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sat, 11 Jan 2025 12:19:42 +1300 Subject: [PATCH] optimize can vote checking --- app/community/routes.py | 4 +++- app/post/routes.py | 14 ++++++++++---- app/templates/post/_comment_voting_buttons.html | 4 ++-- app/templates/post/_post_voting_buttons.html | 4 ++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/community/routes.py b/app/community/routes.py index efee21e4..7bcb3db3 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -225,7 +225,9 @@ def show_community(community: Community): if current_user.is_anonymous: posts = posts.filter(Post.from_bot == False, Post.nsfw == False, Post.nsfl == False, Post.deleted == False) content_filters = {} + user = None else: + user = current_user if current_user.ignore_bots == 1: posts = posts.filter(Post.from_bot == False) if current_user.hide_nsfl == 1: @@ -330,7 +332,7 @@ def show_community(community: Community): etag=f"{community.id}{sort}{post_layout}_{hash(community.last_active)}", related_communities=related_communities, next_url=next_url, prev_url=prev_url, low_bandwidth=low_bandwidth, un_moderated=un_moderated, recently_upvoted=recently_upvoted, recently_downvoted=recently_downvoted, - canonical=community.profile_id(), + canonical=community.profile_id(), can_upvote_here=can_upvote(user, community), can_downvote_here=can_downvote(user, community, g.site), rss_feed=f"https://{current_app.config['SERVER_NAME']}/community/{community.link()}/feed", rss_feed_name=f"{community.title} on {g.site.name}", content_filters=content_filters, moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), diff --git a/app/post/routes.py b/app/post/routes.py index f870b142..fe7c7634 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -32,7 +32,7 @@ from app.utils import get_setting, render_template, allowlist_html, markdown_to_ blocked_instances, blocked_domains, community_moderators, blocked_phrases, show_ban_message, recently_upvoted_posts, \ recently_downvoted_posts, recently_upvoted_post_replies, recently_downvoted_post_replies, reply_is_stupid, \ languages_for_form, menu_topics, add_to_modlog, blocked_communities, piefed_markdown_to_lemmy_markdown, \ - permission_required, blocked_users, get_request, is_local_image_url, is_video_url + permission_required, blocked_users, get_request, is_local_image_url, is_video_url, can_upvote, can_downvote from app.shared.reply import make_reply, edit_reply @@ -161,9 +161,13 @@ def show_post(post_id: int): # for logged in users who have the 'hide read posts' function enabled # mark this post as read - if current_user.is_authenticated and current_user.hide_read_posts: - current_user.mark_post_as_read(post) - db.session.commit() + if current_user.is_authenticated: + user = current_user + if current_user.hide_read_posts: + current_user.mark_post_as_read(post) + db.session.commit() + else: + user = None response = render_template('post/post.html', title=post.title, post=post, is_moderator=is_moderator, is_owner=community.is_owner(), community=post.community, @@ -177,6 +181,8 @@ def show_post(post_id: int): recently_upvoted_replies=recently_upvoted_replies, recently_downvoted_replies=recently_downvoted_replies, reply_collapse_threshold=reply_collapse_threshold, etag=f"{post.id}{sort}_{hash(post.last_active)}", markdown_editor=current_user.is_authenticated and current_user.markdown_editor, + can_upvote_here=can_upvote(user, community), + can_downvote_here=can_downvote(user, community, g.site), low_bandwidth=request.cookies.get('low_bandwidth', '0') == '1', moderating_communities=moderating_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()), diff --git a/app/templates/post/_comment_voting_buttons.html b/app/templates/post/_comment_voting_buttons.html index b071e4ea..054aaeda 100644 --- a/app/templates/post/_comment_voting_buttons.html +++ b/app/templates/post/_comment_voting_buttons.html @@ -1,6 +1,6 @@ {% if current_user.is_authenticated and current_user.verified %} {% if disable_voting %} {% endif %} - {% if can_upvote(current_user, community) and not disable_voting %} + {% if (can_upvote_here or can_upvote(current_user, community)) and not disable_voting %}
@@ -8,7 +8,7 @@
{% endif %} {{ comment.up_votes - comment.down_votes }} - {% if can_downvote(current_user, community) and not disable_voting %} + {% if (can_downvote_here or can_downvote(current_user, community)) and not disable_voting %}
diff --git a/app/templates/post/_post_voting_buttons.html b/app/templates/post/_post_voting_buttons.html index a4fc2d40..071a354e 100644 --- a/app/templates/post/_post_voting_buttons.html +++ b/app/templates/post/_post_voting_buttons.html @@ -1,5 +1,5 @@ {% if current_user.is_authenticated and current_user.verified -%} - {% if can_upvote(current_user, post.community) and not disable_voting -%} + {% if (can_upvote_here or can_upvote(current_user, post.community)) and not disable_voting -%}
@@ -7,7 +7,7 @@
{% endif -%} {{ shorten_number(post.up_votes - post.down_votes) }} - {%- if can_downvote(current_user, post.community) and not disable_voting -%} + {%- if (can_downvote_here or can_downvote(current_user, post.community)) and not disable_voting -%}