2022-12-21 19:47:48 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2022-12-24 17:50:01 +00:00
|
|
|
from activities.models import (
|
|
|
|
Hashtag,
|
|
|
|
Post,
|
|
|
|
PostInteraction,
|
|
|
|
PostInteractionStates,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
|
|
|
from activities.services import PostService
|
2022-12-21 19:47:48 +00:00
|
|
|
from users.models import Identity
|
|
|
|
|
|
|
|
|
|
|
|
class TimelineService:
|
|
|
|
"""
|
|
|
|
Timelines and stuff!
|
|
|
|
"""
|
|
|
|
|
2022-12-22 21:11:47 +00:00
|
|
|
def __init__(self, identity: Identity | None):
|
2022-12-21 19:47:48 +00:00
|
|
|
self.identity = identity
|
|
|
|
|
2022-12-24 17:50:01 +00:00
|
|
|
@classmethod
|
|
|
|
def event_queryset(cls):
|
2022-12-21 19:47:48 +00:00
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
TimelineEvent.objects.select_related(
|
2022-12-21 19:47:48 +00:00
|
|
|
"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 21:28:28 +00:00
|
|
|
.annotate(
|
|
|
|
like_count=models.Count(
|
|
|
|
"subject_post__interactions",
|
|
|
|
filter=models.Q(
|
2022-12-24 17:50:01 +00:00
|
|
|
subject_post__interactions__type=PostInteraction.Types.like,
|
|
|
|
subject_post__interactions__state__in=PostInteractionStates.group_active(),
|
2022-12-22 21:28:28 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
boost_count=models.Count(
|
|
|
|
"subject_post__interactions",
|
|
|
|
filter=models.Q(
|
2022-12-24 17:50:01 +00:00
|
|
|
subject_post__interactions__type=PostInteraction.Types.boost,
|
|
|
|
subject_post__interactions__state__in=PostInteractionStates.group_active(),
|
2022-12-22 21:28:28 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2022-12-24 17:50:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def home(self) -> models.QuerySet[TimelineEvent]:
|
|
|
|
return (
|
|
|
|
self.event_queryset()
|
|
|
|
.filter(
|
|
|
|
identity=self.identity,
|
|
|
|
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
|
|
|
|
)
|
2022-12-21 19:47:48 +00:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def local(self) -> models.QuerySet[Post]:
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
PostService.queryset()
|
|
|
|
.local_public()
|
2022-12-21 19:47:48 +00:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def federated(self) -> models.QuerySet[Post]:
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
PostService.queryset()
|
|
|
|
.public()
|
2022-12-21 19:47:48 +00:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def hashtag(self, hashtag: str | Hashtag) -> models.QuerySet[Post]:
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
PostService.queryset()
|
|
|
|
.public()
|
2022-12-21 19:47:48 +00:00
|
|
|
.filter(author__restriction=Identity.Restriction.none)
|
|
|
|
.tagged_with(hashtag)
|
|
|
|
.order_by("-published")
|
|
|
|
)
|
|
|
|
|
|
|
|
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
self.event_queryset()
|
|
|
|
.filter(identity=self.identity, type__in=types)
|
2022-12-21 19:47:48 +00:00
|
|
|
.order_by("-published")
|
|
|
|
)
|
2022-12-22 21:11:47 +00:00
|
|
|
|
|
|
|
def identity_public(self, identity: Identity):
|
|
|
|
"""
|
|
|
|
Returns all publically visible posts for an identity
|
|
|
|
"""
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
PostService.queryset()
|
|
|
|
.filter(author=identity)
|
2022-12-22 21:11:47 +00:00
|
|
|
.unlisted(include_replies=True)
|
|
|
|
.order_by("-created")
|
|
|
|
)
|