mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-25 16:51:00 +00:00
Rework post/event querysets to always fetch stuff
This commit is contained in:
parent
a6c973337c
commit
087cb2a15f
4 changed files with 92 additions and 110 deletions
|
@ -20,6 +20,10 @@ class PostInteractionStates(StateGraph):
|
|||
fanned_out.transitions_to(undone)
|
||||
undone.transitions_to(undone_fanned_out)
|
||||
|
||||
@classmethod
|
||||
def group_active(cls):
|
||||
return [cls.new, cls.fanned_out]
|
||||
|
||||
@classmethod
|
||||
async def handle_new(cls, instance: "PostInteraction"):
|
||||
"""
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from typing import cast
|
||||
from django.db import models
|
||||
|
||||
from activities.models import Post, PostInteraction, PostInteractionStates, PostStates
|
||||
from users.models import Identity
|
||||
|
@ -21,10 +21,7 @@ class PostService:
|
|||
identity=identity,
|
||||
post=self.post,
|
||||
)[0]
|
||||
if interaction.state in [
|
||||
PostInteractionStates.undone,
|
||||
PostInteractionStates.undone_fanned_out,
|
||||
]:
|
||||
if interaction.state not in PostInteractionStates.group_active():
|
||||
interaction.transition_perform(PostInteractionStates.new)
|
||||
|
||||
def uninteract_as(self, identity, type):
|
||||
|
@ -50,6 +47,40 @@ class PostService:
|
|||
def unboost_as(self, identity: Identity):
|
||||
self.uninteract_as(identity, PostInteraction.Types.boost)
|
||||
|
||||
@classmethod
|
||||
def queryset(cls):
|
||||
"""
|
||||
Returns the base queryset to use for fetching posts efficiently.
|
||||
"""
|
||||
return (
|
||||
Post.objects.not_hidden()
|
||||
.prefetch_related(
|
||||
"attachments",
|
||||
"mentions",
|
||||
"emojis",
|
||||
)
|
||||
.select_related(
|
||||
"author",
|
||||
"author__domain",
|
||||
)
|
||||
.annotate(
|
||||
like_count=models.Count(
|
||||
"interactions",
|
||||
filter=models.Q(
|
||||
interactions__type=PostInteraction.Types.like,
|
||||
interactions__state__in=PostInteractionStates.group_active(),
|
||||
),
|
||||
),
|
||||
boost_count=models.Count(
|
||||
"interactions",
|
||||
filter=models.Q(
|
||||
interactions__type=PostInteraction.Types.boost,
|
||||
interactions__state__in=PostInteractionStates.group_active(),
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
def context(self, identity: Identity | None) -> tuple[list[Post], list[Post]]:
|
||||
"""
|
||||
Returns ancestor/descendant information.
|
||||
|
@ -66,7 +97,7 @@ class PostService:
|
|||
ancestors: list[Post] = []
|
||||
ancestor = self.post
|
||||
while ancestor.in_reply_to and len(ancestors) < num_ancestors:
|
||||
ancestor = cast(Post, ancestor.in_reply_to_post())
|
||||
ancestor = self.queryset().get(object_uri=ancestor.in_reply_to)
|
||||
if ancestor is None:
|
||||
break
|
||||
if ancestor.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
||||
|
@ -78,10 +109,8 @@ class PostService:
|
|||
while queue and len(descendants) < num_descendants:
|
||||
node = queue.pop()
|
||||
child_queryset = (
|
||||
Post.objects.not_hidden()
|
||||
self.queryset()
|
||||
.filter(in_reply_to=node.object_uri)
|
||||
.select_related("author", "author__domain")
|
||||
.prefetch_related("emojis")
|
||||
.order_by("published")
|
||||
)
|
||||
if identity:
|
||||
|
|
|
@ -1,6 +1,13 @@
|
|||
from django.db import models
|
||||
|
||||
from activities.models import Hashtag, Post, PostInteraction, TimelineEvent
|
||||
from activities.models import (
|
||||
Hashtag,
|
||||
Post,
|
||||
PostInteraction,
|
||||
PostInteractionStates,
|
||||
TimelineEvent,
|
||||
)
|
||||
from activities.services import PostService
|
||||
from users.models import Identity
|
||||
|
||||
|
||||
|
@ -12,13 +19,10 @@ class TimelineService:
|
|||
def __init__(self, identity: Identity | None):
|
||||
self.identity = identity
|
||||
|
||||
def home(self) -> models.QuerySet[TimelineEvent]:
|
||||
@classmethod
|
||||
def event_queryset(cls):
|
||||
return (
|
||||
TimelineEvent.objects.filter(
|
||||
identity=self.identity,
|
||||
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
||||
)
|
||||
.select_related(
|
||||
TimelineEvent.objects.select_related(
|
||||
"subject_post",
|
||||
"subject_post__author",
|
||||
"subject_post__author__domain",
|
||||
|
@ -37,113 +41,60 @@ class TimelineService:
|
|||
like_count=models.Count(
|
||||
"subject_post__interactions",
|
||||
filter=models.Q(
|
||||
subject_post__interactions__type=PostInteraction.Types.like
|
||||
subject_post__interactions__type=PostInteraction.Types.like,
|
||||
subject_post__interactions__state__in=PostInteractionStates.group_active(),
|
||||
),
|
||||
),
|
||||
boost_count=models.Count(
|
||||
"subject_post__interactions",
|
||||
filter=models.Q(
|
||||
subject_post__interactions__type=PostInteraction.Types.boost
|
||||
subject_post__interactions__type=PostInteraction.Types.boost,
|
||||
subject_post__interactions__state__in=PostInteractionStates.group_active(),
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
def home(self) -> models.QuerySet[TimelineEvent]:
|
||||
return (
|
||||
self.event_queryset()
|
||||
.filter(
|
||||
identity=self.identity,
|
||||
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
||||
)
|
||||
.order_by("-published")
|
||||
)
|
||||
|
||||
def local(self) -> models.QuerySet[Post]:
|
||||
return (
|
||||
Post.objects.local_public()
|
||||
.not_hidden()
|
||||
PostService.queryset()
|
||||
.local_public()
|
||||
.filter(author__restriction=Identity.Restriction.none)
|
||||
.select_related("author", "author__domain")
|
||||
.prefetch_related("attachments", "mentions", "emojis")
|
||||
.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("-published")
|
||||
)
|
||||
|
||||
def federated(self) -> models.QuerySet[Post]:
|
||||
return (
|
||||
Post.objects.public()
|
||||
.not_hidden()
|
||||
PostService.queryset()
|
||||
.public()
|
||||
.filter(author__restriction=Identity.Restriction.none)
|
||||
.select_related("author", "author__domain")
|
||||
.prefetch_related("attachments", "mentions", "emojis")
|
||||
.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("-published")
|
||||
)
|
||||
|
||||
def hashtag(self, hashtag: str | Hashtag) -> models.QuerySet[Post]:
|
||||
return (
|
||||
Post.objects.public()
|
||||
.not_hidden()
|
||||
PostService.queryset()
|
||||
.public()
|
||||
.filter(author__restriction=Identity.Restriction.none)
|
||||
.tagged_with(hashtag)
|
||||
.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("-published")
|
||||
)
|
||||
|
||||
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
||||
return (
|
||||
TimelineEvent.objects.filter(identity=self.identity, type__in=types)
|
||||
self.event_queryset()
|
||||
.filter(identity=self.identity, type__in=types)
|
||||
.order_by("-published")
|
||||
.select_related(
|
||||
"subject_post",
|
||||
"subject_post__author",
|
||||
"subject_post__author__domain",
|
||||
"subject_identity",
|
||||
"subject_identity__domain",
|
||||
"subject_post_interaction",
|
||||
"subject_post_interaction__identity",
|
||||
"subject_post_interaction__identity__domain",
|
||||
)
|
||||
.prefetch_related(
|
||||
"subject_post__emojis",
|
||||
"subject_post__mentions",
|
||||
"subject_post__attachments",
|
||||
)
|
||||
.annotate(
|
||||
like_count=models.Count(
|
||||
"subject_post__interactions",
|
||||
filter=models.Q(
|
||||
subject_post__interactions__type=PostInteraction.Types.like
|
||||
),
|
||||
),
|
||||
boost_count=models.Count(
|
||||
"subject_post__interactions",
|
||||
filter=models.Q(
|
||||
subject_post__interactions__type=PostInteraction.Types.boost
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
def identity_public(self, identity: Identity):
|
||||
|
@ -151,21 +102,8 @@ class TimelineService:
|
|||
Returns all publically visible posts for an identity
|
||||
"""
|
||||
return (
|
||||
identity.posts.not_hidden()
|
||||
PostService.queryset()
|
||||
.filter(author=identity)
|
||||
.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")
|
||||
)
|
||||
|
|
|
@ -25,7 +25,10 @@ class Individual(TemplateView):
|
|||
self.identity = by_handle_or_404(self.request, handle, local=False)
|
||||
if self.identity.blocked:
|
||||
raise Http404("Blocked user")
|
||||
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
|
||||
self.post_obj = get_object_or_404(
|
||||
PostService.queryset().filter(author=self.identity),
|
||||
pk=post_id,
|
||||
)
|
||||
if self.post_obj.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
||||
raise Http404("Deleted post")
|
||||
# If they're coming in looking for JSON, they want the actor
|
||||
|
@ -73,13 +76,16 @@ class Like(View):
|
|||
def post(self, request, handle, post_id):
|
||||
identity = by_handle_or_404(self.request, handle, local=False)
|
||||
post = get_object_or_404(
|
||||
identity.posts.prefetch_related("attachments"), pk=post_id
|
||||
PostService.queryset().filter(author=identity),
|
||||
pk=post_id,
|
||||
)
|
||||
service = PostService(post)
|
||||
if self.undo:
|
||||
service.unlike_as(self.request.identity)
|
||||
service.unlike_as(request.identity)
|
||||
post.like_count = max(0, post.like_count - 1)
|
||||
else:
|
||||
service.like_as(self.request.identity)
|
||||
service.like_as(request.identity)
|
||||
post.like_count += 1
|
||||
# Return either a redirect or a HTMX snippet
|
||||
if request.htmx:
|
||||
return render(
|
||||
|
@ -103,12 +109,17 @@ class Boost(View):
|
|||
|
||||
def post(self, request, handle, post_id):
|
||||
identity = by_handle_or_404(self.request, handle, local=False)
|
||||
post = get_object_or_404(identity.posts, pk=post_id)
|
||||
post = get_object_or_404(
|
||||
PostService.queryset().filter(author=identity),
|
||||
pk=post_id,
|
||||
)
|
||||
service = PostService(post)
|
||||
if self.undo:
|
||||
service.unboost_as(request.identity)
|
||||
post.boost_count = max(0, post.boost_count - 1)
|
||||
else:
|
||||
service.boost_as(request.identity)
|
||||
post.boost_count += 1
|
||||
# Return either a redirect or a HTMX snippet
|
||||
if request.htmx:
|
||||
return render(
|
||||
|
|
Loading…
Reference in a new issue