2022-12-16 16:06:29 -08:00
|
|
|
from django.core.paginator import Paginator
|
2022-11-28 20:41:36 -08:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2022-11-13 15:14:38 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-18 08:44:56 -08:00
|
|
|
from django.views.generic import ListView, TemplateView
|
2022-11-13 15:14:38 -08:00
|
|
|
|
2022-11-28 20:41:36 -08:00
|
|
|
from activities.models import Hashtag, Post, PostInteraction, TimelineEvent
|
2022-12-05 21:23:07 -08:00
|
|
|
from core.decorators import cache_page
|
2022-11-13 15:14:38 -08:00
|
|
|
from users.decorators import identity_required
|
2022-12-16 18:42:48 -08:00
|
|
|
from users.models import Identity
|
2022-11-13 15:14:38 -08:00
|
|
|
|
2022-12-11 08:34:44 -08:00
|
|
|
from .compose import Compose
|
|
|
|
|
2022-11-13 15:14:38 -08:00
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-12-18 08:44:56 -08:00
|
|
|
class Home(TemplateView):
|
2022-11-13 15:14:38 -08:00
|
|
|
|
|
|
|
template_name = "activities/home.html"
|
|
|
|
|
2022-12-11 08:34:44 -08:00
|
|
|
form_class = Compose.form_class
|
2022-11-13 15:14:38 -08:00
|
|
|
|
2022-12-15 14:55:33 -08:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
return self.form_class(request=self.request, **self.get_form_kwargs())
|
|
|
|
|
2022-11-13 15:14:38 -08:00
|
|
|
def get_context_data(self):
|
2022-12-16 16:06:29 -08:00
|
|
|
events = (
|
2022-11-13 17:42:47 -08:00
|
|
|
TimelineEvent.objects.filter(
|
2022-11-13 15:14:38 -08:00
|
|
|
identity=self.request.identity,
|
2022-11-13 17:42:47 -08:00
|
|
|
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
2022-11-13 15:14:38 -08:00
|
|
|
)
|
|
|
|
.select_related("subject_post", "subject_post__author")
|
2022-12-14 09:16:22 -08:00
|
|
|
.prefetch_related("subject_post__attachments", "subject_post__mentions")
|
2022-12-18 08:44:56 -08:00
|
|
|
.order_by("-published")
|
2022-11-15 17:30:30 -08:00
|
|
|
)
|
2022-12-16 16:06:29 -08:00
|
|
|
paginator = Paginator(events, 50)
|
|
|
|
page_number = self.request.GET.get("page")
|
2022-12-18 08:44:56 -08:00
|
|
|
context = {
|
|
|
|
"interactions": PostInteraction.get_event_interactions(
|
|
|
|
events,
|
|
|
|
self.request.identity,
|
|
|
|
),
|
|
|
|
"current_page": "home",
|
|
|
|
"allows_refresh": True,
|
|
|
|
"page_obj": paginator.get_page(page_number),
|
|
|
|
"form": self.form_class(request=self.request),
|
|
|
|
}
|
2022-11-13 15:14:38 -08:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2022-12-05 09:55:30 -08:00
|
|
|
@method_decorator(
|
2022-12-05 21:23:07 -08:00
|
|
|
cache_page("cache_timeout_page_timeline", public_only=True), name="dispatch"
|
2022-12-05 09:55:30 -08:00
|
|
|
)
|
2022-11-28 20:41:36 -08: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-28 21:14:52 -08:00
|
|
|
Post.objects.public()
|
2022-12-16 18:42:48 -08:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
2022-11-28 20:41:36 -08:00
|
|
|
.tagged_with(self.hashtag)
|
|
|
|
.select_related("author")
|
2022-12-14 09:16:22 -08:00
|
|
|
.prefetch_related("attachments", "mentions")
|
2022-12-18 08:44:56 -08:00
|
|
|
.order_by("-published")
|
2022-11-28 20:41:36 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
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 09:55:30 -08:00
|
|
|
@method_decorator(
|
2022-12-05 21:23:07 -08:00
|
|
|
cache_page("cache_timeout_page_timeline", public_only=True), name="dispatch"
|
2022-12-05 09:55:30 -08:00
|
|
|
)
|
2022-11-22 07:57:40 -08:00
|
|
|
class Local(ListView):
|
2022-11-13 17:42:47 -08:00
|
|
|
|
|
|
|
template_name = "activities/local.html"
|
2022-11-22 18:21:01 -08:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "local",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 07:57:40 -08:00
|
|
|
paginate_by = 50
|
2022-11-13 17:42:47 -08:00
|
|
|
|
2022-11-22 07:57:40 -08:00
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-28 20:41:36 -08:00
|
|
|
Post.objects.local_public()
|
2022-12-16 18:42:48 -08:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
2022-12-14 23:50:54 -08:00
|
|
|
.select_related("author", "author__domain")
|
|
|
|
.prefetch_related("attachments", "mentions", "emojis")
|
2022-12-18 08:44:56 -08:00
|
|
|
.order_by("-published")
|
2022-11-13 17:42:47 -08:00
|
|
|
)
|
|
|
|
|
2022-11-22 18:58:42 -08: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-13 17:42:47 -08:00
|
|
|
|
2022-11-13 15:14:38 -08:00
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-11-22 07:57:40 -08:00
|
|
|
class Federated(ListView):
|
2022-11-13 15:14:38 -08:00
|
|
|
|
|
|
|
template_name = "activities/federated.html"
|
2022-11-22 18:21:01 -08:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "federated",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 07:57:40 -08:00
|
|
|
paginate_by = 50
|
2022-11-13 15:14:38 -08:00
|
|
|
|
2022-11-22 07:57:40 -08:00
|
|
|
def get_queryset(self):
|
|
|
|
return (
|
2022-11-25 17:20:59 -08:00
|
|
|
Post.objects.filter(
|
|
|
|
visibility=Post.Visibilities.public, in_reply_to__isnull=True
|
|
|
|
)
|
2022-12-16 18:42:48 -08:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
2022-12-14 23:50:54 -08:00
|
|
|
.select_related("author", "author__domain")
|
|
|
|
.prefetch_related("attachments", "mentions", "emojis")
|
2022-12-18 08:44:56 -08:00
|
|
|
.order_by("-published")
|
2022-11-13 15:14:38 -08:00
|
|
|
)
|
2022-11-13 17:42:47 -08:00
|
|
|
|
2022-11-22 18:58:42 -08: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-13 17:42:47 -08:00
|
|
|
|
|
|
|
@method_decorator(identity_required, name="dispatch")
|
2022-11-22 07:57:40 -08:00
|
|
|
class Notifications(ListView):
|
2022-11-13 17:42:47 -08:00
|
|
|
|
|
|
|
template_name = "activities/notifications.html"
|
2022-11-22 18:21:01 -08:00
|
|
|
extra_context = {
|
|
|
|
"current_page": "notifications",
|
|
|
|
"allows_refresh": True,
|
|
|
|
}
|
2022-11-22 07:57:40 -08:00
|
|
|
paginate_by = 50
|
2022-12-05 21:14:50 -08:00
|
|
|
notification_types = {
|
|
|
|
"followed": TimelineEvent.Types.followed,
|
|
|
|
"boosted": TimelineEvent.Types.boosted,
|
|
|
|
"mentioned": TimelineEvent.Types.mentioned,
|
|
|
|
"liked": TimelineEvent.Types.liked,
|
|
|
|
}
|
2022-11-13 17:42:47 -08:00
|
|
|
|
2022-11-22 07:57:40 -08:00
|
|
|
def get_queryset(self):
|
2022-12-05 21:14:50 -08:00
|
|
|
# Did they ask to change options?
|
|
|
|
notification_options = self.request.session.get("notification_options", {})
|
|
|
|
for type_name in self.notification_types:
|
|
|
|
notification_options.setdefault(type_name, True)
|
|
|
|
if self.request.GET.get(type_name) == "true":
|
|
|
|
notification_options[type_name] = True
|
|
|
|
elif self.request.GET.get(type_name) == "false":
|
|
|
|
notification_options[type_name] = False
|
|
|
|
self.request.session["notification_options"] = notification_options
|
|
|
|
# Return appropriate events
|
|
|
|
types = []
|
|
|
|
for type_name, type in self.notification_types.items():
|
|
|
|
if notification_options.get(type_name, True):
|
|
|
|
types.append(type)
|
2022-11-22 07:57:40 -08:00
|
|
|
return (
|
2022-12-05 21:14:50 -08:00
|
|
|
TimelineEvent.objects.filter(identity=self.request.identity, type__in=types)
|
2022-12-18 08:44:56 -08:00
|
|
|
.order_by("-published")
|
2022-12-14 23:50:54 -08:00
|
|
|
.select_related(
|
|
|
|
"subject_post",
|
|
|
|
"subject_post__author",
|
|
|
|
"subject_post__author__domain",
|
|
|
|
"subject_identity",
|
|
|
|
)
|
|
|
|
.prefetch_related("subject_post__emojis")
|
2022-11-17 17:52:00 -08:00
|
|
|
)
|
2022-11-28 21:42:40 -08: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)
|
2022-12-05 21:14:50 -08:00
|
|
|
# Retrieve what kinds of things to show
|
2022-11-28 21:42:40 -08:00
|
|
|
context["events"] = events
|
2022-12-05 21:14:50 -08:00
|
|
|
context["notification_options"] = self.request.session["notification_options"]
|
2022-12-08 21:58:58 -08:00
|
|
|
context["interactions"] = PostInteraction.get_event_interactions(
|
|
|
|
context["page_obj"],
|
|
|
|
self.request.identity,
|
|
|
|
)
|
2022-11-28 21:42:40 -08:00
|
|
|
return context
|