2022-12-21 19:47:48 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2023-02-15 23:05:48 +00:00
|
|
|
from activities.models import (
|
|
|
|
Hashtag,
|
|
|
|
Post,
|
|
|
|
PostInteraction,
|
|
|
|
PostInteractionStates,
|
|
|
|
TimelineEvent,
|
|
|
|
)
|
2022-12-24 17:50:01 +00:00
|
|
|
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-31 20:48:35 +00:00
|
|
|
return TimelineEvent.objects.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-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],
|
|
|
|
)
|
2023-01-14 18:31:17 +00:00
|
|
|
.order_by("-created")
|
2022-12-21 19:47:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def local(self) -> models.QuerySet[Post]:
|
2023-05-04 04:42:37 +00:00
|
|
|
queryset = (
|
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)
|
2023-01-14 18:31:17 +00:00
|
|
|
.order_by("-id")
|
2022-12-21 19:47:48 +00:00
|
|
|
)
|
2023-05-04 04:42:37 +00:00
|
|
|
if self.identity is not None:
|
|
|
|
queryset = queryset.filter(author__domain=self.identity.domain)
|
|
|
|
return queryset
|
2022-12-21 19:47:48 +00:00
|
|
|
|
|
|
|
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)
|
2023-01-14 18:31:17 +00:00
|
|
|
.order_by("-id")
|
2022-12-21 19:47:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2023-01-14 18:31:17 +00:00
|
|
|
.order_by("-id")
|
2022-12-21 19:47:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def notifications(self, types: list[str]) -> models.QuerySet[TimelineEvent]:
|
|
|
|
return (
|
2022-12-24 17:50:01 +00:00
|
|
|
self.event_queryset()
|
2023-07-11 22:37:03 +00:00
|
|
|
.filter(identity=self.identity, type__in=types, dismissed=False)
|
2023-01-14 18:31:17 +00:00
|
|
|
.order_by("-created")
|
2022-12-21 19:47:48 +00:00
|
|
|
)
|
2022-12-22 21:11:47 +00:00
|
|
|
|
2023-05-13 17:07:57 +00:00
|
|
|
def identity_public(
|
|
|
|
self,
|
|
|
|
identity: Identity,
|
|
|
|
include_boosts: bool = True,
|
|
|
|
include_replies: bool = True,
|
|
|
|
):
|
2022-12-22 21:11:47 +00:00
|
|
|
"""
|
2023-05-12 23:43:26 +00:00
|
|
|
Returns timeline events with all of an identity's publicly visible posts
|
|
|
|
and their boosts
|
2022-12-22 21:11:47 +00:00
|
|
|
"""
|
2023-05-12 23:43:26 +00:00
|
|
|
filter = models.Q(
|
|
|
|
type=TimelineEvent.Types.post,
|
|
|
|
subject_post__author=identity,
|
|
|
|
subject_post__visibility__in=[
|
|
|
|
Post.Visibilities.public,
|
|
|
|
Post.Visibilities.local_only,
|
|
|
|
Post.Visibilities.unlisted,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
if include_boosts:
|
|
|
|
filter = filter | models.Q(
|
|
|
|
type=TimelineEvent.Types.boost, subject_identity=identity
|
|
|
|
)
|
2023-05-13 17:07:57 +00:00
|
|
|
if not include_replies:
|
|
|
|
filter = filter & models.Q(subject_post__in_reply_to__isnull=True)
|
2022-12-22 21:11:47 +00:00
|
|
|
return (
|
2023-05-12 23:43:26 +00:00
|
|
|
self.event_queryset()
|
|
|
|
.filter(
|
|
|
|
filter,
|
|
|
|
identity=identity,
|
|
|
|
)
|
|
|
|
.order_by("-created")
|
2022-12-22 21:11:47 +00:00
|
|
|
)
|
2023-01-21 02:49:55 +00:00
|
|
|
|
2023-05-13 16:01:27 +00:00
|
|
|
def identity_pinned(self) -> models.QuerySet[Post]:
|
|
|
|
"""
|
|
|
|
Return all pinned posts that are publicly visible for an identity
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
PostService.queryset()
|
|
|
|
.public()
|
|
|
|
.filter(
|
|
|
|
interactions__identity=self.identity,
|
|
|
|
interactions__type=PostInteraction.Types.pin,
|
|
|
|
interactions__state__in=PostInteractionStates.group_active(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-01-21 02:49:55 +00:00
|
|
|
def likes(self) -> models.QuerySet[Post]:
|
|
|
|
"""
|
|
|
|
Return all liked posts for an identity
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
PostService.queryset()
|
|
|
|
.filter(
|
|
|
|
interactions__identity=self.identity,
|
|
|
|
interactions__type=PostInteraction.Types.like,
|
2023-02-15 23:05:48 +00:00
|
|
|
interactions__state__in=PostInteractionStates.group_active(),
|
2023-01-21 02:49:55 +00:00
|
|
|
)
|
|
|
|
.order_by("-id")
|
|
|
|
)
|
2023-03-11 18:17:20 +00:00
|
|
|
|
|
|
|
def bookmarks(self) -> models.QuerySet[Post]:
|
|
|
|
"""
|
|
|
|
Return all bookmarked posts for an identity
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
PostService.queryset()
|
|
|
|
.filter(bookmarks__identity=self.identity)
|
|
|
|
.order_by("-id")
|
|
|
|
)
|