2022-11-13 23:14:38 +00:00
|
|
|
from django import forms
|
2022-11-29 04:41:36 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2022-11-13 23:14:38 +00:00
|
|
|
from django.template.defaultfilters import linebreaks_filter
|
|
|
|
from django.utils.decorators import method_decorator
|
2022-11-22 15:57:40 +00:00
|
|
|
from django.views.generic import FormView, ListView
|
2022-11-13 23:14:38 +00:00
|
|
|
|
2022-11-29 04:41:36 +00:00
|
|
|
from activities.models import Hashtag, Post, PostInteraction, TimelineEvent
|
2022-12-05 17:55:30 +00:00
|
|
|
from core.decorators import per_identity_cache_page
|
2022-11-23 02:52:40 +00:00
|
|
|
from core.models import Config
|
2022-11-13 23:14:38 +00:00
|
|
|
from users.decorators import identity_required
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-12-05 17:55:30 +00:00
|
|
|
@method_decorator(per_identity_cache_page(), name="dispatch")
|
2022-11-13 23:14:38 +00:00
|
|
|
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,
|
2022-11-23 02:52:40 +00:00
|
|
|
label=Config.lazy_system_value("content_warning_text"),
|
2022-11-13 23:14:38 +00:00
|
|
|
widget=forms.TextInput(
|
|
|
|
attrs={
|
|
|
|
"class": "hidden",
|
2022-11-23 02:52:40 +00:00
|
|
|
"placeholder": Config.lazy_system_value("content_warning_text"),
|
2022-11-13 23:14:38 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
2022-11-16 01:30:30 +00:00
|
|
|
context["events"] = list(
|
2022-11-14 01:42:47 +00:00
|
|
|
TimelineEvent.objects.filter(
|
2022-11-13 23:14:38 +00:00
|
|
|
identity=self.request.identity,
|
2022-11-14 01:42:47 +00:00
|
|
|
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
2022-11-13 23:14:38 +00:00
|
|
|
)
|
|
|
|
.select_related("subject_post", "subject_post__author")
|
2022-11-17 06:00:10 +00:00
|
|
|
.prefetch_related("subject_post__attachments")
|
2022-11-18 00:43:00 +00:00
|
|
|
.order_by("-created")[:50]
|
2022-11-14 01:42:47 +00:00
|
|
|
)
|
2022-11-16 01:30:30 +00:00
|
|
|
context["interactions"] = PostInteraction.get_event_interactions(
|
|
|
|
context["events"], self.request.identity
|
|
|
|
)
|
2022-11-13 23:14:38 +00:00
|
|
|
context["current_page"] = "home"
|
2022-11-23 02:21:01 +00:00
|
|
|
context["allows_refresh"] = True
|
2022-11-13 23:14:38 +00: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"),
|
2022-11-26 02:33:46 +00:00
|
|
|
visibility=self.request.identity.config_identity.default_post_visibility,
|
2022-11-13 23:14:38 +00:00
|
|
|
)
|
|
|
|
return redirect(".")
|
|
|
|
|
|
|
|
|
2022-12-05 17:55:30 +00:00
|
|
|
@method_decorator(
|
|
|
|
per_identity_cache_page("cache_timeout_page_timeline"), name="dispatch"
|
|
|
|
)
|
2022-11-29 04:41:36 +00:00
|
|
|
class Tag(ListView):
|
|
|
|
|
|
|
|
template_name = "activities/tag.html"
|
|
|
|
extra_context = {
|
|
|
|
"current_page": "tag",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
|
|
|
paginate_by = 50
|
|
|
|
|
|
|
|
def get(self, request, hashtag, *args, **kwargs):
|
|
|
|
tag = hashtag.lower().lstrip("#")
|
|
|
|
if hashtag != tag:
|
|
|
|
# SEO sanitize
|
|
|
|
return redirect(f"/tags/{tag}/", permanent=True)
|
|
|
|
self.hashtag = get_object_or_404(Hashtag.objects.public(), hashtag=tag)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-29 05:14:52 +00:00
|
|
|
Post.objects.public()
|
2022-11-29 04:41:36 +00:00
|
|
|
.tagged_with(self.hashtag)
|
|
|
|
.select_related("author")
|
|
|
|
.prefetch_related("attachments")
|
|
|
|
.order_by("-created")[:50]
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
context = super().get_context_data()
|
|
|
|
context["hashtag"] = self.hashtag
|
|
|
|
context["interactions"] = PostInteraction.get_post_interactions(
|
|
|
|
context["page_obj"], self.request.identity
|
|
|
|
)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2022-12-05 17:55:30 +00:00
|
|
|
@method_decorator(
|
|
|
|
per_identity_cache_page("cache_timeout_page_timeline"), name="dispatch"
|
|
|
|
)
|
2022-11-22 15:57:40 +00:00
|
|
|
class Local(ListView):
|
2022-11-14 01:42:47 +00:00
|
|
|
|
|
|
|
template_name = "activities/local.html"
|
2022-11-23 02:21:01 +00:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "local",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 15:57:40 +00:00
|
|
|
paginate_by = 50
|
2022-11-14 01:42:47 +00:00
|
|
|
|
2022-11-22 15:57:40 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-29 04:41:36 +00:00
|
|
|
Post.objects.local_public()
|
2022-11-14 01:42:47 +00:00
|
|
|
.select_related("author")
|
2022-11-17 06:00:10 +00:00
|
|
|
.prefetch_related("attachments")
|
2022-11-18 00:43:00 +00:00
|
|
|
.order_by("-created")[:50]
|
2022-11-14 01:42:47 +00:00
|
|
|
)
|
|
|
|
|
2022-11-23 02:58:42 +00: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 01:42:47 +00:00
|
|
|
|
2022-11-13 23:14:38 +00:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-12-05 17:55:30 +00:00
|
|
|
@method_decorator(
|
|
|
|
per_identity_cache_page("cache_timeout_page_timeline"), name="dispatch"
|
|
|
|
)
|
2022-11-22 15:57:40 +00:00
|
|
|
class Federated(ListView):
|
2022-11-13 23:14:38 +00:00
|
|
|
|
|
|
|
template_name = "activities/federated.html"
|
2022-11-23 02:21:01 +00:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "federated",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 15:57:40 +00:00
|
|
|
paginate_by = 50
|
2022-11-13 23:14:38 +00:00
|
|
|
|
2022-11-22 15:57:40 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-26 01:20:59 +00:00
|
|
|
Post.objects.filter(
|
|
|
|
visibility=Post.Visibilities.public, in_reply_to__isnull=True
|
|
|
|
)
|
2022-11-13 23:14:38 +00:00
|
|
|
.select_related("author")
|
2022-11-17 06:00:10 +00:00
|
|
|
.prefetch_related("attachments")
|
2022-11-18 00:43:00 +00:00
|
|
|
.order_by("-created")[:50]
|
2022-11-13 23:14:38 +00:00
|
|
|
)
|
2022-11-14 01:42:47 +00:00
|
|
|
|
2022-11-23 02:58:42 +00: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 01:42:47 +00:00
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-12-05 17:55:30 +00:00
|
|
|
@method_decorator(
|
|
|
|
per_identity_cache_page("cache_timeout_page_timeline"), name="dispatch"
|
|
|
|
)
|
2022-11-22 15:57:40 +00:00
|
|
|
class Notifications(ListView):
|
2022-11-14 01:42:47 +00:00
|
|
|
|
|
|
|
template_name = "activities/notifications.html"
|
2022-11-23 02:21:01 +00:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "notifications",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 15:57:40 +00:00
|
|
|
paginate_by = 50
|
2022-11-14 01:42:47 +00:00
|
|
|
|
2022-11-22 15:57:40 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-18 01:52:00 +00: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")
|
|
|
|
)
|
2022-11-29 05:42:40 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
# Collapse similar notifications into one
|
|
|
|
events = []
|
|
|
|
for event in context["page_obj"]:
|
|
|
|
if (
|
|
|
|
events
|
|
|
|
and event.type
|
|
|
|
in [
|
|
|
|
TimelineEvent.Types.liked,
|
|
|
|
TimelineEvent.Types.boosted,
|
|
|
|
TimelineEvent.Types.mentioned,
|
|
|
|
]
|
|
|
|
and event.subject_post_id == events[-1].subject_post_id
|
|
|
|
):
|
|
|
|
events[-1].collapsed = True
|
|
|
|
events.append(event)
|
|
|
|
context["events"] = events
|
|
|
|
return context
|