From 521595a781379dc500cb4b59f3c77d2270b2b624 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 22 Aug 2022 14:37:29 +0100 Subject: [PATCH] Add view to serve matrix server well-known URL --- website/common/templates/matrix-server.json | 1 + website/common/tests/test_views.py | 10 ++++++++++ website/common/views.py | 5 +++++ website/urls.py | 12 +++++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 website/common/templates/matrix-server.json diff --git a/website/common/templates/matrix-server.json b/website/common/templates/matrix-server.json new file mode 100644 index 0000000..c1991cd --- /dev/null +++ b/website/common/templates/matrix-server.json @@ -0,0 +1 @@ +{"m.server": "matrix.jakehoward.tech:443"} diff --git a/website/common/tests/test_views.py b/website/common/tests/test_views.py index c076e0a..c7faa01 100644 --- a/website/common/tests/test_views.py +++ b/website/common/tests/test_views.py @@ -49,3 +49,13 @@ class SecurityViewTestCase(TestCase): response.context["security_txt"], "http://testserver/.well-known/security.txt", ) + + +class MatrixServerViewTestCase(SimpleTestCase): + url = reverse("matrix-server") + + def test_accessible(self) -> None: + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + self.assertEqual(response["Content-Type"], "application/json") + self.assertTemplateUsed(response, "matrix-server.json") diff --git a/website/common/views.py b/website/common/views.py index 77037ec..ba047ad 100644 --- a/website/common/views.py +++ b/website/common/views.py @@ -54,3 +54,8 @@ class SecurityView(TemplateView): (timezone.now() + self.expires).replace(microsecond=0).isoformat() ) return context + + +class MatrixServerView(TemplateView): + template_name = "matrix-server.json" + content_type = "application/json" diff --git a/website/urls.py b/website/urls.py index abeb08e..ba1ef4c 100644 --- a/website/urls.py +++ b/website/urls.py @@ -7,7 +7,12 @@ from wagtail.contrib.sitemaps.views import sitemap from wagtail.documents import urls as wagtaildocs_urls from wagtail.images.views.serve import ServeView -from website.common.views import RobotsView, SecurityView, page_not_found +from website.common.views import ( + MatrixServerView, + RobotsView, + SecurityView, + page_not_found, +) urlpatterns = [ path("admin/", include(wagtailadmin_urls)), @@ -28,6 +33,11 @@ urlpatterns = [ cache_page(SecurityView.expires.total_seconds())(SecurityView.as_view()), name="securitytxt", ), + path( + ".well-known/matrix/server", + cache_page(60 * 60)(MatrixServerView.as_view()), + name="matrix-server", + ), path("404/", page_not_found, name="404"), ]