This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/activities/views/timelines.py

138 lines
4.2 KiB
Python
Raw Normal View History

2022-11-14 00:14:38 +01:00
from django import forms
from django.shortcuts import redirect
from django.template.defaultfilters import linebreaks_filter
from django.utils.decorators import method_decorator
2022-11-22 16:57:40 +01:00
from django.views.generic import FormView, ListView
2022-11-14 00:14:38 +01:00
from activities.models import Post, PostInteraction, TimelineEvent
from core.models import Config
2022-11-14 00:14:38 +01:00
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,
label=Config.lazy_system_value("content_warning_text"),
2022-11-14 00:14:38 +01:00
widget=forms.TextInput(
attrs={
"class": "hidden",
"placeholder": Config.lazy_system_value("content_warning_text"),
2022-11-14 00:14:38 +01:00
},
),
)
def get_context_data(self):
context = super().get_context_data()
context["events"] = list(
2022-11-14 02:42:47 +01:00
TimelineEvent.objects.filter(
2022-11-14 00:14:38 +01:00
identity=self.request.identity,
2022-11-14 02:42:47 +01:00
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
2022-11-14 00:14:38 +01:00
)
.select_related("subject_post", "subject_post__author")
2022-11-17 07:00:10 +01:00
.prefetch_related("subject_post__attachments")
2022-11-18 01:43:00 +01:00
.order_by("-created")[:50]
2022-11-14 02:42:47 +01:00
)
context["interactions"] = PostInteraction.get_event_interactions(
context["events"], self.request.identity
)
2022-11-14 00:14:38 +01:00
context["current_page"] = "home"
context["allows_refresh"] = True
2022-11-14 00:14:38 +01:00
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(".")
2022-11-22 16:57:40 +01:00
class Local(ListView):
2022-11-14 02:42:47 +01:00
template_name = "activities/local.html"
extra_context = {
"current_page": "local",
"allows_refresh": True,
}
2022-11-22 16:57:40 +01:00
paginate_by = 50
2022-11-14 02:42:47 +01:00
2022-11-22 16:57:40 +01:00
def get_queryset(self):
return (
2022-11-14 02:42:47 +01:00
Post.objects.filter(visibility=Post.Visibilities.public, author__local=True)
.select_related("author")
2022-11-17 07:00:10 +01:00
.prefetch_related("attachments")
2022-11-18 01:43:00 +01:00
.order_by("-created")[:50]
2022-11-14 02:42:47 +01:00
)
def get_context_data(self):
context = super().get_context_data()
context["interactions"] = PostInteraction.get_post_interactions(
context["page_obj"], self.request.identity
)
return context
2022-11-14 02:42:47 +01:00
2022-11-14 00:14:38 +01:00
@method_decorator(identity_required, name="dispatch")
2022-11-22 16:57:40 +01:00
class Federated(ListView):
2022-11-14 00:14:38 +01:00
template_name = "activities/federated.html"
extra_context = {
"current_page": "federated",
"allows_refresh": True,
}
2022-11-22 16:57:40 +01:00
paginate_by = 50
2022-11-14 00:14:38 +01:00
2022-11-22 16:57:40 +01:00
def get_queryset(self):
return (
2022-11-14 00:14:38 +01:00
Post.objects.filter(visibility=Post.Visibilities.public)
.select_related("author")
2022-11-17 07:00:10 +01:00
.prefetch_related("attachments")
2022-11-18 01:43:00 +01:00
.order_by("-created")[:50]
2022-11-14 00:14:38 +01:00
)
2022-11-14 02:42:47 +01:00
def get_context_data(self):
context = super().get_context_data()
context["interactions"] = PostInteraction.get_post_interactions(
context["page_obj"], self.request.identity
)
return context
2022-11-14 02:42:47 +01:00
@method_decorator(identity_required, name="dispatch")
2022-11-22 16:57:40 +01:00
class Notifications(ListView):
2022-11-14 02:42:47 +01:00
template_name = "activities/notifications.html"
extra_context = {
"current_page": "notifications",
"allows_refresh": True,
}
2022-11-22 16:57:40 +01:00
paginate_by = 50
2022-11-14 02:42:47 +01:00
2022-11-22 16:57:40 +01:00
def get_queryset(self):
return (
2022-11-18 02:52:00 +01:00
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")
)