mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-26 09:11: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)
|
fanned_out.transitions_to(undone)
|
||||||
undone.transitions_to(undone_fanned_out)
|
undone.transitions_to(undone_fanned_out)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def group_active(cls):
|
||||||
|
return [cls.new, cls.fanned_out]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def handle_new(cls, instance: "PostInteraction"):
|
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 activities.models import Post, PostInteraction, PostInteractionStates, PostStates
|
||||||
from users.models import Identity
|
from users.models import Identity
|
||||||
|
@ -21,10 +21,7 @@ class PostService:
|
||||||
identity=identity,
|
identity=identity,
|
||||||
post=self.post,
|
post=self.post,
|
||||||
)[0]
|
)[0]
|
||||||
if interaction.state in [
|
if interaction.state not in PostInteractionStates.group_active():
|
||||||
PostInteractionStates.undone,
|
|
||||||
PostInteractionStates.undone_fanned_out,
|
|
||||||
]:
|
|
||||||
interaction.transition_perform(PostInteractionStates.new)
|
interaction.transition_perform(PostInteractionStates.new)
|
||||||
|
|
||||||
def uninteract_as(self, identity, type):
|
def uninteract_as(self, identity, type):
|
||||||
|
@ -50,6 +47,40 @@ class PostService:
|
||||||
def unboost_as(self, identity: Identity):
|
def unboost_as(self, identity: Identity):
|
||||||
self.uninteract_as(identity, PostInteraction.Types.boost)
|
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]]:
|
def context(self, identity: Identity | None) -> tuple[list[Post], list[Post]]:
|
||||||
"""
|
"""
|
||||||
Returns ancestor/descendant information.
|
Returns ancestor/descendant information.
|
||||||
|
@ -66,7 +97,7 @@ class PostService:
|
||||||
ancestors: list[Post] = []
|
ancestors: list[Post] = []
|
||||||
ancestor = self.post
|
ancestor = self.post
|
||||||
while ancestor.in_reply_to and len(ancestors) < num_ancestors:
|
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:
|
if ancestor is None:
|
||||||
break
|
break
|
||||||
if ancestor.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
if ancestor.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
||||||
|
@ -78,10 +109,8 @@ class PostService:
|
||||||
while queue and len(descendants) < num_descendants:
|
while queue and len(descendants) < num_descendants:
|
||||||
node = queue.pop()
|
node = queue.pop()
|
||||||
child_queryset = (
|
child_queryset = (
|
||||||
Post.objects.not_hidden()
|
self.queryset()
|
||||||
.filter(in_reply_to=node.object_uri)
|
.filter(in_reply_to=node.object_uri)
|
||||||
.select_related("author", "author__domain")
|
|
||||||
.prefetch_related("emojis")
|
|
||||||
.order_by("published")
|
.order_by("published")
|
||||||
)
|
)
|
||||||
if identity:
|
if identity:
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
from django.db import models
|
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
|
from users.models import Identity
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,13 +19,10 @@ class TimelineService:
|
||||||
def __init__(self, identity: Identity | None):
|
def __init__(self, identity: Identity | None):
|
||||||
self.identity = identity
|
self.identity = identity
|
||||||
|
|
||||||
def home(self) -> models.QuerySet[TimelineEvent]:
|
@classmethod
|
||||||
|
def event_queryset(cls):
|
||||||
return (
|
return (
|
||||||
TimelineEvent.objects.filter(
|
TimelineEvent.objects.select_related(
|
||||||
identity=self.identity,
|
|
||||||
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
|
||||||
)
|
|
||||||
.select_related(
|
|
||||||
"subject_post",
|
"subject_post",
|
||||||
"subject_post__author",
|
"subject_post__author",
|
||||||
"subject_post__author__domain",
|
"subject_post__author__domain",
|
||||||
|
@ -37,113 +41,60 @@ class TimelineService:
|
||||||
like_count=models.Count(
|
like_count=models.Count(
|
||||||
"subject_post__interactions",
|
"subject_post__interactions",
|
||||||
filter=models.Q(
|
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(
|
boost_count=models.Count(
|
||||||
"subject_post__interactions",
|
"subject_post__interactions",
|
||||||
filter=models.Q(
|
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")
|
.order_by("-published")
|
||||||
)
|
)
|
||||||
|
|
||||||
def local(self) -> models.QuerySet[Post]:
|
def local(self) -> models.QuerySet[Post]:
|
||||||
return (
|
return (
|
||||||
Post.objects.local_public()
|
PostService.queryset()
|
||||||
.not_hidden()
|
.local_public()
|
||||||
.filter(author__restriction=Identity.Restriction.none)
|
.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")
|
.order_by("-published")
|
||||||
)
|
)
|
||||||
|
|
||||||
def federated(self) -> models.QuerySet[Post]:
|
def federated(self) -> models.QuerySet[Post]:
|
||||||
return (
|
return (
|
||||||
Post.objects.public()
|
PostService.queryset()
|
||||||
.not_hidden()
|
.public()
|
||||||
.filter(author__restriction=Identity.Restriction.none)
|
.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")
|
.order_by("-published")
|
||||||
)
|
)
|
||||||
|
|
||||||
def hashtag(self, hashtag: str | Hashtag) -> models.QuerySet[Post]:
|
def hashtag(self, hashtag: str | Hashtag) -> models.QuerySet[Post]:
|
||||||
return (
|
return (
|
||||||
Post.objects.public()
|
PostService.queryset()
|
||||||
.not_hidden()
|
.public()
|
||||||
.filter(author__restriction=Identity.Restriction.none)
|
.filter(author__restriction=Identity.Restriction.none)
|
||||||
.tagged_with(hashtag)
|
.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")
|
.order_by("-published")
|
||||||
)
|
)
|
||||||
|
|
||||||
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
||||||
return (
|
return (
|
||||||
TimelineEvent.objects.filter(identity=self.identity, type__in=types)
|
self.event_queryset()
|
||||||
|
.filter(identity=self.identity, type__in=types)
|
||||||
.order_by("-published")
|
.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):
|
def identity_public(self, identity: Identity):
|
||||||
|
@ -151,21 +102,8 @@ class TimelineService:
|
||||||
Returns all publically visible posts for an identity
|
Returns all publically visible posts for an identity
|
||||||
"""
|
"""
|
||||||
return (
|
return (
|
||||||
identity.posts.not_hidden()
|
PostService.queryset()
|
||||||
|
.filter(author=identity)
|
||||||
.unlisted(include_replies=True)
|
.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")
|
.order_by("-created")
|
||||||
)
|
)
|
||||||
|
|
|
@ -25,7 +25,10 @@ class Individual(TemplateView):
|
||||||
self.identity = by_handle_or_404(self.request, handle, local=False)
|
self.identity = by_handle_or_404(self.request, handle, local=False)
|
||||||
if self.identity.blocked:
|
if self.identity.blocked:
|
||||||
raise Http404("Blocked user")
|
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]:
|
if self.post_obj.state in [PostStates.deleted, PostStates.deleted_fanned_out]:
|
||||||
raise Http404("Deleted post")
|
raise Http404("Deleted post")
|
||||||
# If they're coming in looking for JSON, they want the actor
|
# 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):
|
def post(self, request, handle, post_id):
|
||||||
identity = by_handle_or_404(self.request, handle, local=False)
|
identity = by_handle_or_404(self.request, handle, local=False)
|
||||||
post = get_object_or_404(
|
post = get_object_or_404(
|
||||||
identity.posts.prefetch_related("attachments"), pk=post_id
|
PostService.queryset().filter(author=identity),
|
||||||
|
pk=post_id,
|
||||||
)
|
)
|
||||||
service = PostService(post)
|
service = PostService(post)
|
||||||
if self.undo:
|
if self.undo:
|
||||||
service.unlike_as(self.request.identity)
|
service.unlike_as(request.identity)
|
||||||
|
post.like_count = max(0, post.like_count - 1)
|
||||||
else:
|
else:
|
||||||
service.like_as(self.request.identity)
|
service.like_as(request.identity)
|
||||||
|
post.like_count += 1
|
||||||
# Return either a redirect or a HTMX snippet
|
# Return either a redirect or a HTMX snippet
|
||||||
if request.htmx:
|
if request.htmx:
|
||||||
return render(
|
return render(
|
||||||
|
@ -103,12 +109,17 @@ class Boost(View):
|
||||||
|
|
||||||
def post(self, request, handle, post_id):
|
def post(self, request, handle, post_id):
|
||||||
identity = by_handle_or_404(self.request, handle, local=False)
|
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)
|
service = PostService(post)
|
||||||
if self.undo:
|
if self.undo:
|
||||||
service.unboost_as(request.identity)
|
service.unboost_as(request.identity)
|
||||||
|
post.boost_count = max(0, post.boost_count - 1)
|
||||||
else:
|
else:
|
||||||
service.boost_as(request.identity)
|
service.boost_as(request.identity)
|
||||||
|
post.boost_count += 1
|
||||||
# Return either a redirect or a HTMX snippet
|
# Return either a redirect or a HTMX snippet
|
||||||
if request.htmx:
|
if request.htmx:
|
||||||
return render(
|
return render(
|
||||||
|
|
Loading…
Reference in a new issue