Add a middleware to detect LD Accept headers
This commit is contained in:
parent
e2d28a4be0
commit
9fe2e6676c
@ -21,12 +21,7 @@ class Individual(TemplateView):
|
|||||||
self.identity = by_handle_or_404(self.request, handle, local=False)
|
self.identity = by_handle_or_404(self.request, handle, local=False)
|
||||||
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
|
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
|
||||||
# If they're coming in looking for JSON, they want the actor
|
# If they're coming in looking for JSON, they want the actor
|
||||||
accept = request.headers.get("accept", "text/html").lower()
|
if request.ap_json:
|
||||||
if (
|
|
||||||
"application/json" in accept
|
|
||||||
or "application/ld" in accept
|
|
||||||
or "application/activity" in accept
|
|
||||||
):
|
|
||||||
# Return post JSON
|
# Return post JSON
|
||||||
return self.serve_object()
|
return self.serve_object()
|
||||||
else:
|
else:
|
||||||
|
@ -6,6 +6,25 @@ from core import sentry
|
|||||||
from core.models import Config
|
from core.models import Config
|
||||||
|
|
||||||
|
|
||||||
|
class AcceptMiddleware:
|
||||||
|
"""
|
||||||
|
Detects any Accept headers signifying a fellow AP server is trying to get JSON.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
accept = request.headers.get("accept", "text/html").lower()
|
||||||
|
request.ap_json = (
|
||||||
|
"application/json" in accept
|
||||||
|
or "application/ld" in accept
|
||||||
|
or "application/activity" in accept
|
||||||
|
)
|
||||||
|
response = self.get_response(request)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
class ConfigLoadingMiddleware:
|
class ConfigLoadingMiddleware:
|
||||||
"""
|
"""
|
||||||
Caches the system config every request
|
Caches the system config every request
|
||||||
|
@ -178,6 +178,7 @@ MIDDLEWARE = [
|
|||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"django_htmx.middleware.HtmxMiddleware",
|
"django_htmx.middleware.HtmxMiddleware",
|
||||||
|
"core.middleware.AcceptMiddleware",
|
||||||
"core.middleware.ConfigLoadingMiddleware",
|
"core.middleware.ConfigLoadingMiddleware",
|
||||||
"users.middleware.IdentityMiddleware",
|
"users.middleware.IdentityMiddleware",
|
||||||
]
|
]
|
||||||
|
@ -42,12 +42,7 @@ class ViewIdentity(ListView):
|
|||||||
):
|
):
|
||||||
self.identity.transition_perform(IdentityStates.outdated)
|
self.identity.transition_perform(IdentityStates.outdated)
|
||||||
# If they're coming in looking for JSON, they want the actor
|
# If they're coming in looking for JSON, they want the actor
|
||||||
accept = request.headers.get("accept", "text/html").lower()
|
if request.ap_json:
|
||||||
if (
|
|
||||||
"application/json" in accept
|
|
||||||
or "application/ld" in accept
|
|
||||||
or "application/activity" in accept
|
|
||||||
):
|
|
||||||
# Return actor info
|
# Return actor info
|
||||||
return self.serve_actor(self.identity)
|
return self.serve_actor(self.identity)
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user