summaryrefslogtreecommitdiffstats
path: root/activities/views/timelines.py
blob: e8e74edb871e59ab9acb31652477dc5d132da8ca (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
from django import forms
from django.shortcuts import redirect
from django.template.defaultfilters import linebreaks_filter
from django.utils.decorators import method_decorator
from django.views.generic import FormView, ListView

from activities.models import Post, PostInteraction, TimelineEvent
from users.decorators import identity_required


@method_decorator(identity_required, name="dispatch")
class Home(FormView):

    template_name = "activities/home.html"

    class form_class(forms.Form):
        text = forms.CharField(
            widget=forms.Textarea(
                attrs={
                    "placeholder": "What's on your mind?",
                },
            )
        )
        content_warning = forms.CharField(
            required=False,
            widget=forms.TextInput(
                attrs={
                    "placeholder": "Content Warning",
                    "class": "hidden",
                },
            ),
        )

    def get_context_data(self):
        context = super().get_context_data()
        context["events"] = list(
            TimelineEvent.objects.filter(
                identity=self.request.identity,
                type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
            )
            .select_related("subject_post", "subject_post__author")
            .prefetch_related("subject_post__attachments")
            .order_by("-created")[:50]
        )
        context["interactions"] = PostInteraction.get_event_interactions(
            context["events"], self.request.identity
        )
        context["current_page"] = "home"
        return context

    def form_valid(self, form):
        Post.create_local(
            author=self.request.identity,
            content=linebreaks_filter(form.cleaned_data["text"]),
            summary=form.cleaned_data.get("content_warning"),
        )
        return redirect(".")


class Local(ListView):

    template_name = "activities/local.html"
    extra_context = {"current_page": "local"}
    paginate_by = 50

    def get_queryset(self):
        return (
            Post.objects.filter(visibility=Post.Visibilities.public, author__local=True)
            .select_related("author")
            .prefetch_related("attachments")
            .order_by("-created")[:50]
        )


@method_decorator(identity_required, name="dispatch")
class Federated(ListView):

    template_name = "activities/federated.html"
    extra_context = {"current_page": "federated"}
    paginate_by = 50

    def get_queryset(self):
        return (
            Post.objects.filter(visibility=Post.Visibilities.public)
            .select_related("author")
            .prefetch_related("attachments")
            .order_by("-created")[:50]
        )


@method_decorator(identity_required, name="dispatch")
class Notifications(ListView):

    template_name = "activities/notifications.html"
    extra_context = {"current_page": "notifications"}
    paginate_by = 50

    def get_queryset(self):
        return (
            TimelineEvent.objects.filter(
                identity=self.request.identity,
                type__in=[
                    TimelineEvent.Types.mentioned,
                    TimelineEvent.Types.boosted,
                    TimelineEvent.Types.liked,
                    TimelineEvent.Types.followed,
                ],
            )
            .order_by("-created")[:50]
            .select_related("subject_post", "subject_post__author", "subject_identity")
        )