2022-08-22 20:33:52 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2022-11-17 22:36:22 +00:00
|
|
|
from django.conf import settings
|
2022-08-22 20:33:52 +01:00
|
|
|
from django.http.request import HttpRequest
|
2022-11-17 22:36:22 +00:00
|
|
|
from django.http.response import Http404, HttpResponse
|
2022-08-22 20:33:52 +01:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.decorators import method_decorator
|
2022-11-17 22:36:22 +00:00
|
|
|
from django.views.decorators.cache import cache_control, cache_page
|
2022-08-22 20:33:52 +01:00
|
|
|
from django.views.generic import TemplateView
|
2022-11-17 22:36:22 +00:00
|
|
|
from proxy.views import proxy_view
|
2023-09-04 19:22:06 +01:00
|
|
|
from requests.exceptions import RequestException
|
2022-08-22 20:33:52 +01:00
|
|
|
|
|
|
|
from website.contact.models import ContactPage
|
2022-08-28 20:24:59 +01:00
|
|
|
from website.contrib.singleton_page.utils import SingletonPageCache
|
2022-08-22 20:33:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SecurityView(TemplateView):
|
2022-08-25 14:07:08 +01:00
|
|
|
template_name = "well-known/security.txt"
|
2022-08-22 20:33:52 +01:00
|
|
|
content_type = "text/plain"
|
|
|
|
|
|
|
|
expires = timedelta(days=7)
|
|
|
|
|
2022-09-04 17:29:04 +01:00
|
|
|
@method_decorator(cache_control(max_age=int(expires.total_seconds() / 2)))
|
2022-08-22 20:33:52 +01:00
|
|
|
def dispatch(self, request: HttpRequest) -> HttpResponse:
|
|
|
|
return super().dispatch(request)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs: dict) -> dict:
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context["security_txt"] = self.request.build_absolute_uri(self.request.path)
|
2022-08-31 19:06:02 +01:00
|
|
|
contact_page_path = SingletonPageCache.get_url(ContactPage, self.request)
|
2022-08-31 09:12:16 +01:00
|
|
|
context["contact_page_url"] = (
|
|
|
|
self.request.build_absolute_uri(contact_page_path)
|
|
|
|
if contact_page_path
|
|
|
|
else None
|
|
|
|
)
|
2022-08-22 20:33:52 +01:00
|
|
|
context["expires"] = (
|
|
|
|
(timezone.now() + self.expires).replace(microsecond=0).isoformat()
|
|
|
|
)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2022-09-04 17:29:04 +01:00
|
|
|
@method_decorator(cache_control(max_age=60 * 60), name="dispatch")
|
2022-08-22 20:33:52 +01:00
|
|
|
class MatrixServerView(TemplateView):
|
2022-08-25 14:07:08 +01:00
|
|
|
template_name = "well-known/matrix-server.json"
|
2022-08-22 20:33:52 +01:00
|
|
|
content_type = "application/json"
|
|
|
|
|
|
|
|
|
2022-09-04 17:29:04 +01:00
|
|
|
@method_decorator(cache_control(max_age=60 * 60), name="dispatch")
|
2022-08-22 20:33:52 +01:00
|
|
|
class MatrixClientView(TemplateView):
|
2022-08-25 14:07:08 +01:00
|
|
|
template_name = "well-known/matrix-client.json"
|
2022-08-22 20:33:52 +01:00
|
|
|
content_type = "application/json"
|
2022-11-17 22:36:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@cache_page(60)
|
|
|
|
def activitypub_proxy(request: HttpRequest) -> HttpResponse:
|
|
|
|
if not settings.ACTIVITYPUB_HOST:
|
|
|
|
raise Http404
|
|
|
|
|
2023-09-04 19:22:06 +01:00
|
|
|
try:
|
|
|
|
return proxy_view(
|
|
|
|
request,
|
|
|
|
f"https://{settings.ACTIVITYPUB_HOST}{request.path}",
|
|
|
|
)
|
|
|
|
except RequestException:
|
|
|
|
return HttpResponse(status_code=502)
|