Return HttpResponse instead of redirects for AJAX follows

This is more efficient, and most follow activities are ajax
This commit is contained in:
Mouse Reeve 2022-07-09 12:20:18 -07:00
parent 923495e454
commit a7553c0b8c
2 changed files with 10 additions and 3 deletions

View file

@ -42,11 +42,11 @@ def get_relationship(context, user_object):
"""caches the relationship between the logged in user and another user"""
user = context["request"].user
return get_or_set(
f"cached-relationship-{user.id}-{user_object.id}",
f"relationship-{user.id}-{user_object.id}",
get_relationship_name,
user,
user_object,
timeout=259200,
timeout=60 * 60,
)

View file

@ -1,7 +1,9 @@
""" views for actions you can take in the application """
import urllib.parse
import re
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
@ -13,6 +15,7 @@ from .helpers import (
handle_remote_webfinger,
subscribe_remote_webfinger,
WebFingerError,
is_api_request
)
@ -34,6 +37,8 @@ def follow(request):
# that means we should save to trigger a re-broadcast
follow_request.save()
if is_api_request(request):
return HttpResponse()
return redirect(to_follow.local_path)
@ -58,8 +63,10 @@ def unfollow(request):
except models.UserFollowRequest.DoesNotExist:
clear_cache(request.user, to_unfollow)
if is_api_request(request):
return HttpResponse()
# this is handled with ajax so it shouldn't really matter
return redirect(request.headers.get("Referer", "/"))
return redirect("/")
@login_required