Cache status interact buttons

This commit is contained in:
Mouse Reeve 2022-01-05 14:54:51 -08:00
parent 0da0091237
commit 7df99afdc7
3 changed files with 46 additions and 29 deletions

View file

@ -82,6 +82,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
if not self.reply_parent: if not self.reply_parent:
self.thread_id = self.id self.thread_id = self.id
super().save(broadcast=False, update_fields=["thread_id"]) super().save(broadcast=False, update_fields=["thread_id"])
def delete(self, *args, **kwargs): # pylint: disable=unused-argument def delete(self, *args, **kwargs): # pylint: disable=unused-argument

View file

@ -1,6 +1,7 @@
{% extends 'components/card.html' %} {% extends 'components/card.html' %}
{% load i18n %} {% load i18n %}
{% load utilities %} {% load utilities %}
{% load cache %}
{% block card-header %} {% block card-header %}
<div <div
@ -30,38 +31,41 @@
{# nothing here #} {# nothing here #}
{% elif request.user.is_authenticated %} {% elif request.user.is_authenticated %}
<div class="card-footer-item"> {% cache 259200 interact request.user.id status.id %}
{% trans "Reply" as button_text %} <div class="card-footer-item">
{% include 'snippets/toggle/toggle_button.html' with controls_text="show_comment" controls_uid=status.id text=button_text icon_with_text="comment" class="is-small is-light is-transparent toggle-button" focus="id_content_reply" %} {% trans "Reply" as button_text %}
</div> {% include 'snippets/toggle/toggle_button.html' with controls_text="show_comment" controls_uid=status.id text=button_text icon_with_text="comment" class="is-small is-light is-transparent toggle-button" focus="id_content_reply" %}
<div class="card-footer-item"> </div>
{% include 'snippets/boost_button.html' with status=status %} <div class="card-footer-item">
</div> {% include 'snippets/boost_button.html' with status=status %}
<div class="card-footer-item"> </div>
{% include 'snippets/fav_button.html' with status=status %} <div class="card-footer-item">
</div> {% include 'snippets/fav_button.html' with status=status %}
{% if not moderation_mode %} </div>
<div class="card-footer-item"> {% if not moderation_mode %}
{% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %} <div class="card-footer-item">
</div> {% include 'snippets/status/status_options.html' with class="is-small is-light is-transparent" right=True %}
{% endif %} </div>
{% endif %}
{% endcache %}
{% else %} {% else %}
<div class="card-footer-item">
<a href="{% url 'login' %}">
<span class="icon icon-comment is-small" title="{% trans 'Reply' %}">
<span class="is-sr-only">{% trans "Reply" %}</span>
</span>
<span class="icon icon-boost is-small ml-4" title="{% trans 'Boost status' %}"> <div class="card-footer-item">
<span class="is-sr-only">{% trans "Boost status" %}</span> <a href="{% url 'login' %}">
</span> <span class="icon icon-comment is-small" title="{% trans 'Reply' %}">
<span class="is-sr-only">{% trans "Reply" %}</span>
</span>
<span class="icon icon-heart is-small ml-4" title="{% trans 'Like status' %}"> <span class="icon icon-boost is-small ml-4" title="{% trans 'Boost status' %}">
<span class="is-sr-only">{% trans "Like status" %}</span> <span class="is-sr-only">{% trans "Boost status" %}</span>
</span> </span>
</a>
</div> <span class="icon icon-heart is-small ml-4" title="{% trans 'Like status' %}">
<span class="is-sr-only">{% trans "Like status" %}</span>
</span>
</a>
</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}

View file

@ -1,6 +1,8 @@
""" boosts and favs """ """ boosts and favs """
from django.db import IntegrityError
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound
from django.shortcuts import redirect from django.shortcuts import redirect
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -17,6 +19,7 @@ class Favorite(View):
def post(self, request, status_id): def post(self, request, status_id):
"""create a like""" """create a like"""
clear_cache(request.user.id, status_id)
status = models.Status.objects.get(id=status_id) status = models.Status.objects.get(id=status_id)
try: try:
models.Favorite.objects.create(status=status, user=request.user) models.Favorite.objects.create(status=status, user=request.user)
@ -43,6 +46,7 @@ class Unfavorite(View):
return HttpResponseNotFound() return HttpResponseNotFound()
favorite.delete() favorite.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@ -70,6 +74,7 @@ class Boost(View):
privacy=status.privacy, privacy=status.privacy,
user=request.user, user=request.user,
) )
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
@ -87,6 +92,13 @@ class Unboost(View):
).first() ).first()
boost.delete() boost.delete()
clear_cache(request.user.id, status_id)
if is_api_request(request): if is_api_request(request):
return HttpResponse() return HttpResponse()
return redirect(request.headers.get("Referer", "/")) return redirect(request.headers.get("Referer", "/"))
def clear_cache(user_id, status_id):
"""clear template cache"""
cache_key = make_template_fragment_key("interact", [user_id, status_id])
cache.delete(cache_key)