2022-12-05 18:21:00 -08:00
|
|
|
import markdown_it
|
2022-11-16 05:53:39 -08:00
|
|
|
from django.http import JsonResponse
|
2022-12-17 14:30:51 -08:00
|
|
|
from django.shortcuts import redirect
|
2022-11-16 05:53:39 -08:00
|
|
|
from django.templatetags.static import static
|
2022-12-05 09:55:30 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-05 18:21:00 -08:00
|
|
|
from django.utils.safestring import mark_safe
|
2022-11-16 05:53:39 -08:00
|
|
|
from django.views.generic import TemplateView, View
|
2022-12-15 09:31:25 -08:00
|
|
|
from django.views.static import serve
|
2022-11-05 13:17:27 -07:00
|
|
|
|
2022-11-13 15:14:38 -08:00
|
|
|
from activities.views.timelines import Home
|
2022-12-05 09:55:30 -08:00
|
|
|
from core.decorators import cache_page
|
2022-12-05 18:21:00 -08:00
|
|
|
from core.models import Config
|
2022-11-05 13:17:27 -07:00
|
|
|
from users.models import Identity
|
|
|
|
|
|
|
|
|
|
|
|
def homepage(request):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
return Home.as_view()(request)
|
|
|
|
else:
|
|
|
|
return LoggedOutHomepage.as_view()(request)
|
|
|
|
|
|
|
|
|
2022-12-05 21:23:07 -08:00
|
|
|
@method_decorator(cache_page(public_only=True), name="dispatch")
|
2022-11-05 13:17:27 -07:00
|
|
|
class LoggedOutHomepage(TemplateView):
|
|
|
|
|
|
|
|
template_name = "index.html"
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
return {
|
2022-12-05 18:21:00 -08:00
|
|
|
"about": mark_safe(
|
|
|
|
markdown_it.MarkdownIt().render(Config.system.site_about)
|
|
|
|
),
|
2022-11-25 17:32:45 -08:00
|
|
|
"identities": Identity.objects.filter(
|
|
|
|
local=True,
|
|
|
|
discoverable=True,
|
|
|
|
).order_by("-created")[:20],
|
2022-11-05 13:17:27 -07:00
|
|
|
}
|
2022-11-16 05:53:39 -08:00
|
|
|
|
|
|
|
|
|
|
|
class AppManifest(View):
|
|
|
|
"""
|
|
|
|
Serves a PWA manifest file. This is a view as we want to drive some
|
|
|
|
items from settings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"$schema": "https://json.schemastore.org/web-manifest-combined.json",
|
|
|
|
"name": "Takahē",
|
|
|
|
"short_name": "Takahē",
|
|
|
|
"start_url": "/",
|
|
|
|
"display": "standalone",
|
|
|
|
"background_color": "#26323c",
|
|
|
|
"theme_color": "#26323c",
|
|
|
|
"description": "An ActivityPub server",
|
|
|
|
"icons": [
|
|
|
|
{
|
|
|
|
"src": static("img/icon-128.png"),
|
|
|
|
"sizes": "128x128",
|
|
|
|
"type": "image/png",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"src": static("img/icon-1024.png"),
|
|
|
|
"sizes": "1024x1024",
|
|
|
|
"type": "image/png",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
2022-12-05 18:21:00 -08:00
|
|
|
|
|
|
|
|
|
|
|
class FlatPage(TemplateView):
|
|
|
|
"""
|
|
|
|
Serves a "flat page" from a config option,
|
|
|
|
returning 404 if it is empty.
|
|
|
|
"""
|
|
|
|
|
|
|
|
template_name = "flatpage.html"
|
|
|
|
config_option = None
|
|
|
|
title = None
|
|
|
|
|
2022-12-17 14:30:51 -08:00
|
|
|
def get(self, request, *args, **kwargs):
|
2022-12-05 18:21:00 -08:00
|
|
|
if self.config_option is None:
|
|
|
|
raise ValueError("No config option provided")
|
2022-12-17 14:30:51 -08:00
|
|
|
self.content = getattr(Config.system, self.config_option)
|
|
|
|
# If the content is a plain URL, then redirect to it instead
|
|
|
|
if (
|
|
|
|
"\n" not in self.content
|
|
|
|
and " " not in self.content
|
|
|
|
and "://" in self.content
|
|
|
|
):
|
|
|
|
return redirect(self.content)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
html = markdown_it.MarkdownIt().render(self.content)
|
2022-12-05 18:21:00 -08:00
|
|
|
return {
|
|
|
|
"title": self.title,
|
|
|
|
"content": mark_safe(html),
|
|
|
|
}
|
2022-12-15 09:31:25 -08:00
|
|
|
|
|
|
|
|
|
|
|
def custom_static_serve(*args, **keywords):
|
|
|
|
"""
|
|
|
|
Set the correct `Content-Type` header for static WebP images
|
|
|
|
since Django cannot guess the MIME type of WebP images.
|
|
|
|
"""
|
|
|
|
response = serve(*args, **keywords)
|
|
|
|
if keywords["path"].endswith(".webp"):
|
|
|
|
response.headers["Content-Type"] = "image/webp"
|
|
|
|
return response
|