Cache user relationship for follow buttons

This commit is contained in:
Mouse Reeve 2022-01-08 12:05:42 -08:00
parent b6d04e9997
commit 2cca9fab2d
2 changed files with 36 additions and 4 deletions

View file

@ -1,13 +1,18 @@
{% load i18n %}
{% load interaction %}
{% if request.user == user or not request.user.is_authenticated %}
{% elif user in request.user.blocks.all %}
{# nothing to see here -- either it's yourself or your logged out #}
{% else %}
{% get_relationship user as relationship %}
{% if relationship.is_blocked %}
{% include 'snippets/block_button.html' with blocks=True %}
{% else %}
<div class="field{% if not minimal %} has-addons{% else %} mb-0{% endif %}">
<div class="control">
<form action="{% url 'follow' %}" method="POST" class="interaction follow_{{ user.id }} {% if request.user in user.followers.all or request.user in user.follower_requests.all %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
<form action="{% url 'follow' %}" method="POST" class="interaction follow_{{ user.id }} {% if relationship.is_following or relationship.is_follow_pending %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}">
<button class="button is-small{% if not minimal %} is-link{% endif %}" type="submit">
@ -18,10 +23,10 @@
{% endif %}
</button>
</form>
<form action="{% url 'unfollow' %}" method="POST" class="interaction follow_{{ user.id }} {% if not request.user in user.followers.all and not request.user in user.follower_requests.all %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
<form action="{% url 'unfollow' %}" method="POST" class="interaction follow_{{ user.id }} {% if not relationship.is_following and not relationship.is_follow_pending %}is-hidden{%endif %}" data-id="follow_{{ user.id }}">
{% csrf_token %}
<input type="hidden" name="user" value="{{ user.username }}">
{% if user.manually_approves_followers and request.user not in user.followers.all %}
{% if user.manually_approves_followers and not relationship.is_following %}
<button class="button is-small is-danger is-light" type="submit">
{% trans "Undo follow request" %}
</button>
@ -42,4 +47,7 @@
</div>
{% endif %}
</div>
{% endif %}
{% endif %}

View file

@ -32,3 +32,27 @@ def get_user_boosted(user, status):
def get_user_saved_lists(user, book_list):
"""did the user save a list"""
return user.saved_lists.filter(id=book_list.id).exists()
@register.simple_tag(takes_context=True)
def get_relationship(context, user_object):
"""caches the relationship between the logged in user and another user"""
user = context["request"].user
return cache.get(f"relationship-{user.id}-{user_object.id}") or cache.set(
get_relationship_name(user, user_object),
timeout=259200,
)
def get_relationship_name(user, user_object):
"""returns the relationship type"""
types = {
"is_following": False,
"is_follow_pending": False,
"is_blocked": False,
}
if user_object in user.blocks.all():
types["is_blocked"] = True
elif user_object in user.following.all():
types["is_following"] = True
elif user_object in user.follower_requests.all():
types["is_follow_pending"] = True
return types