optimize can vote checking

This commit is contained in:
rimu 2025-01-11 12:19:42 +13:00
parent cb7d4fc40f
commit e6d81db15f
4 changed files with 17 additions and 9 deletions

View file

@ -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()),

View file

@ -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()),

View file

@ -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 %}
<div class="upvote_button {{ 'voted_up' if in_sorted_list(recently_upvoted_replies, comment.id) }}" role="button" aria-label="{{ _('UpVote button.') }}" aria-live="assertive"
hx-post="/comment/{{ comment.id }}/upvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons_new" tabindex="0">
<span class="fe fe-arrow-up"></span>
@ -8,7 +8,7 @@
</div>
{% endif %}
<span title="{{ comment.up_votes }}, {{ comment.down_votes }}" aria-live="assertive" aria-label="{{ _('Score: ') }}{{ comment.up_votes - comment.down_votes }}.">{{ comment.up_votes - comment.down_votes }}</span>
{% if can_downvote(current_user, community) and not disable_voting %}
{% if (can_downvote_here or can_downvote(current_user, community)) and not disable_voting %}
<div class="downvote_button {{ 'voted_down' if in_sorted_list(recently_downvoted_replies, comment.id) }}" role="button" aria-label="{{ _('DownVote button.') }}" aria-live="assertive"
hx-post="/comment/{{ comment.id }}/downvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons_new" tabindex="0">
<span class="fe fe-arrow-down"></span>

View file

@ -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 -%}
<div class="upvote_button {{ 'voted_up' if in_sorted_list(recently_upvoted, post.id) }}" role="button" aria-label="{{ _('UpVote button, %(count)d upvotes so far.', count=post.up_votes) }}" aria-live="assertive"
hx-post="/post/{{ post.id }}/upvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons_new" tabindex="0">
<span class="fe fe-arrow-up"></span>
@ -7,7 +7,7 @@
</div>
{% endif -%}
<span title="{{ post.up_votes }}, {{ post.down_votes }}" aria-live="assertive" aria-label="{{ _('Score: ') }}{{ post.up_votes - post.down_votes }}.">{{ shorten_number(post.up_votes - post.down_votes) }}</span>
{%- 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 -%}
<div class="downvote_button {{ 'voted_down' if in_sorted_list(recently_downvoted, post.id) }}" role="button" aria-label="{{ _('DownVote button, %(count)d downvotes so far.', count=post.down_votes) }}" aria-live="assertive"
hx-post="/post/{{ post.id }}/downvote" hx-trigger="click throttle:1s" hx-target="closest .voting_buttons_new" tabindex="0">
<span class="fe fe-arrow-down"></span>