Drive user timeline and RSS off a single service

pull/247/head
Andrew Godwin 2022-12-22 13:11:47 -08:00
rodzic a058140d11
commit af6ddcf8f1
2 zmienionych plików z 28 dodań i 16 usunięć

Wyświetl plik

@ -9,7 +9,7 @@ class TimelineService:
Timelines and stuff! Timelines and stuff!
""" """
def __init__(self, identity: Identity): def __init__(self, identity: Identity | None):
self.identity = identity self.identity = identity
def home(self) -> models.QuerySet[TimelineEvent]: def home(self) -> models.QuerySet[TimelineEvent]:
@ -145,3 +145,27 @@ class TimelineService:
), ),
) )
) )
def identity_public(self, identity: Identity):
"""
Returns all publically visible posts for an identity
"""
return (
identity.posts.not_hidden()
.unlisted(include_replies=True)
.select_related("author")
.prefetch_related("attachments")
.select_related("author", "author__domain")
.prefetch_related("attachments", "mentions")
.annotate(
like_count=models.Count(
"interactions",
filter=models.Q(interactions__type=PostInteraction.Types.like),
),
boost_count=models.Count(
"interactions",
filter=models.Q(interactions__type=PostInteraction.Types.boost),
),
)
.order_by("-created")
)

Wyświetl plik

@ -11,6 +11,7 @@ from django.views.decorators.vary import vary_on_headers
from django.views.generic import FormView, ListView, TemplateView, View from django.views.generic import FormView, ListView, TemplateView, View
from activities.models import Post, PostInteraction from activities.models import Post, PostInteraction
from activities.services import TimelineService
from core.decorators import cache_page, cache_page_by_ap_json from core.decorators import cache_page, cache_page_by_ap_json
from core.ld import canonicalise from core.ld import canonicalise
from core.models import Config from core.models import Config
@ -62,13 +63,7 @@ class ViewIdentity(ListView):
) )
def get_queryset(self): def get_queryset(self):
return ( return TimelineService(self.request.identity).identity_public(self.identity)
self.identity.posts.not_hidden()
.unlisted(include_replies=True)
.select_related("author")
.prefetch_related("attachments")
.order_by("-created")
)
def get_context_data(self): def get_context_data(self):
context = super().get_context_data() context = super().get_context_data()
@ -121,14 +116,7 @@ class IdentityFeed(Feed):
return identity.absolute_profile_uri() return identity.absolute_profile_uri()
def items(self, identity: Identity): def items(self, identity: Identity):
return ( return TimelineService(None).identity_public(identity)[:20]
identity.posts.filter(
visibility=Post.Visibilities.public,
)
.select_related("author")
.prefetch_related("attachments")
.order_by("-created")
)
def item_description(self, item: Post): def item_description(self, item: Post):
return item.safe_content_remote() return item.safe_content_remote()