takahe/activities/views/timelines.py

110 lines
3.8 KiB
Python
Raw Normal View History

from django.contrib.auth.decorators import login_required
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.utils.decorators import method_decorator
from django.views.generic import ListView, TemplateView
2022-11-13 23:14:38 +00:00
from activities.models import Hashtag, TimelineEvent
2022-12-21 19:47:48 +00:00
from activities.services import TimelineService
from core.decorators import cache_page
from users.models import Identity
from users.views.base import IdentityViewMixin
2022-11-13 23:14:38 +00:00
2022-12-11 16:34:44 +00:00
@method_decorator(login_required, name="dispatch")
class Home(TemplateView):
"""
Homepage for logged-in users - shows identities primarily.
"""
2022-11-13 23:14:38 +00:00
template_name = "activities/home.html"
def get_context_data(self):
return {
"identities": Identity.objects.filter(
users__pk=self.request.user.pk
).order_by("created"),
}
2022-11-13 23:14:38 +00:00
2022-12-05 17:55:30 +00:00
@method_decorator(
cache_page("cache_timeout_page_timeline", public_only=True), name="dispatch"
2022-12-05 17:55:30 +00:00
)
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 = 25
2022-11-29 04:41:36 +00:00
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 TimelineService(None).hashtag(self.hashtag)
2022-11-29 04:41:36 +00:00
def get_context_data(self):
context = super().get_context_data()
context["hashtag"] = self.hashtag
return context
class Notifications(IdentityViewMixin, ListView):
2022-11-14 01:42:47 +00:00
template_name = "activities/notifications.html"
extra_context = {
"current_page": "notifications",
"allows_refresh": True,
}
paginate_by = 25
notification_types = {
"followed": TimelineEvent.Types.followed,
"boosted": TimelineEvent.Types.boosted,
"mentioned": TimelineEvent.Types.mentioned,
"liked": TimelineEvent.Types.liked,
}
2022-11-14 01:42:47 +00:00
2022-11-22 15:57:40 +00:00
def get_queryset(self):
# 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)
return TimelineService(self.identity).notifications(types)
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)
# Retrieve what kinds of things to show
context["events"] = events
context["identity"] = self.identity
context["notification_options"] = self.request.session["notification_options"]
return context