2022-12-21 11:47:48 -08:00
|
|
|
from django.db import models
|
|
|
|
|
2022-12-22 13:28:28 -08:00
|
|
|
from activities.models import Hashtag, Post, PostInteraction, TimelineEvent
|
2022-12-21 11:47:48 -08:00
|
|
|
from users.models import Identity
|
|
|
|
|
|
|
|
|
|
|
|
class TimelineService:
|
|
|
|
"""
|
|
|
|
Timelines and stuff!
|
|
|
|
"""
|
|
|
|
|
2022-12-22 13:11:47 -08:00
|
|
|
def __init__(self, identity: Identity | None):
|
2022-12-21 11:47:48 -08:00
|
|
|
self.identity = identity
|
|
|
|
|
|
|
|
def home(self) -> models.QuerySet[TimelineEvent]:
|
|
|
|
return (
|
|
|
|
TimelineEvent.objects.filter(
|
|
|
|
identity=self.identity,
|
|
|
|
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
|
|
|
)
|
|
|
|
.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__attachments",
|
|
|
|
"subject_post__mentions",
|
|
|
|
"subject_post__emojis",
|
|
|
|
)
|
2022-12-22 13:28:28 -08:00
|
|
|
.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
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-12-21 11:47:48 -08:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def local(self) -> models.QuerySet[Post]:
|
|
|
|
return (
|
|
|
|
Post.objects.local_public()
|
|
|
|
.not_hidden()
|
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.select_related("author", "author__domain")
|
|
|
|
.prefetch_related("attachments", "mentions", "emojis")
|
2022-12-21 20:12:42 -08:00
|
|
|
.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),
|
|
|
|
),
|
|
|
|
)
|
2022-12-21 11:47:48 -08:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def federated(self) -> models.QuerySet[Post]:
|
|
|
|
return (
|
|
|
|
Post.objects.public()
|
|
|
|
.not_hidden()
|
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.select_related("author", "author__domain")
|
|
|
|
.prefetch_related("attachments", "mentions", "emojis")
|
2022-12-21 20:12:42 -08:00
|
|
|
.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),
|
|
|
|
),
|
|
|
|
)
|
2022-12-21 11:47:48 -08:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def hashtag(self, hashtag: str | Hashtag) -> models.QuerySet[Post]:
|
|
|
|
return (
|
|
|
|
Post.objects.public()
|
|
|
|
.not_hidden()
|
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.tagged_with(hashtag)
|
|
|
|
.select_related("author", "author__domain")
|
|
|
|
.prefetch_related("attachments", "mentions")
|
2022-12-21 20:12:42 -08:00
|
|
|
.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),
|
|
|
|
),
|
|
|
|
)
|
2022-12-21 11:47:48 -08:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
|
|
|
return (
|
|
|
|
TimelineEvent.objects.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",
|
|
|
|
)
|
2022-12-21 20:12:42 -08:00
|
|
|
.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
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-12-21 11:47:48 -08:00
|
|
|
)
|
2022-12-22 13:11:47 -08:00
|
|
|
|
|
|
|
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")
|
|
|
|
)
|