summaryrefslogtreecommitdiffstats
path: root/core/views.py
blob: 462a04162d9aa0e4da44637c825a69eb35f644ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import markdown_it
from django.http import JsonResponse
from django.shortcuts import redirect
from django.templatetags.static import static
from django.utils.decorators import method_decorator
from django.utils.safestring import mark_safe
from django.views.generic import TemplateView, View
from django.views.static import serve

from activities.views.timelines import Home
from core.decorators import cache_page
from core.models import Config
from users.models import Identity


def homepage(request):
    if request.user.is_authenticated:
        return Home.as_view()(request)
    else:
        return LoggedOutHomepage.as_view()(request)


@method_decorator(cache_page(public_only=True), name="dispatch")
class LoggedOutHomepage(TemplateView):

    template_name = "index.html"

    def get_context_data(self):
        return {
            "about": mark_safe(
                markdown_it.MarkdownIt().render(Config.system.site_about)
            ),
            "identities": Identity.objects.filter(
                local=True,
                discoverable=True,
            ).order_by("-created")[:20],
        }


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",
                    },
                ],
            }
        )


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

    def get(self, request, *args, **kwargs):
        if self.config_option is None:
            raise ValueError("No config option provided")
        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)
        return {
            "title": self.title,
            "content": mark_safe(html),
        }


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