This repository has been archived on 2023-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
takahe/api/views/timelines.py

128 lines
3.6 KiB
Python
Raw Normal View History

2022-12-11 20:37:28 +01:00
from activities.models import Post, PostInteraction, TimelineEvent
2022-12-12 08:38:02 +01:00
from api import schemas
from api.decorators import identity_required
from api.pagination import MastodonPaginator
from api.views.base import api_router
2022-12-17 03:42:48 +01:00
from users.models import Identity
2022-12-11 08:25:48 +01:00
2022-12-11 19:22:06 +01:00
@api_router.get("/v1/timelines/home", response=list[schemas.Status])
2022-12-11 08:25:48 +01:00
@identity_required
2022-12-11 19:22:06 +01:00
def home(
request,
max_id: str | None = None,
since_id: str | None = None,
min_id: str | None = None,
limit: int = 20,
):
2022-12-12 08:38:02 +01:00
paginator = MastodonPaginator(Post)
queryset = (
2022-12-11 08:25:48 +01:00
TimelineEvent.objects.filter(
identity=request.identity,
type__in=[TimelineEvent.Types.post],
)
.select_related("subject_post", "subject_post__author")
.prefetch_related("subject_post__attachments")
2022-12-11 19:22:06 +01:00
.order_by("-created")
2022-12-11 08:25:48 +01:00
)
2022-12-12 08:38:02 +01:00
events = paginator.paginate(
queryset,
min_id=min_id,
max_id=max_id,
since_id=since_id,
limit=limit,
)
2022-12-11 20:37:28 +01:00
interactions = PostInteraction.get_event_interactions(events, request.identity)
return [
event.subject_post.to_mastodon_json(interactions=interactions)
for event in events
]
2022-12-11 19:22:06 +01:00
@api_router.get("/v1/timelines/public", response=list[schemas.Status])
@identity_required
def public(
request,
local: bool = False,
remote: bool = False,
only_media: bool = False,
max_id: str | None = None,
since_id: str | None = None,
min_id: str | None = None,
limit: int = 20,
):
2022-12-12 08:38:02 +01:00
queryset = (
2022-12-11 19:22:06 +01:00
Post.objects.public()
2022-12-17 03:42:48 +01:00
.filter(author__restriction=Identity.Restriction.none)
2022-12-11 19:22:06 +01:00
.select_related("author")
.prefetch_related("attachments")
.order_by("-created")
)
if local:
2022-12-12 08:38:02 +01:00
queryset = queryset.filter(local=True)
2022-12-11 19:22:06 +01:00
elif remote:
2022-12-12 08:38:02 +01:00
queryset = queryset.filter(local=False)
2022-12-11 19:22:06 +01:00
if only_media:
2022-12-12 08:38:02 +01:00
queryset = queryset.filter(attachments__id__isnull=True)
paginator = MastodonPaginator(Post)
posts = paginator.paginate(
queryset,
min_id=min_id,
max_id=max_id,
since_id=since_id,
limit=limit,
)
2022-12-11 20:37:28 +01:00
interactions = PostInteraction.get_post_interactions(posts, request.identity)
return [post.to_mastodon_json(interactions=interactions) for post in posts]
2022-12-11 19:22:06 +01:00
@api_router.get("/v1/timelines/tag/{hashtag}", response=list[schemas.Status])
@identity_required
def hashtag(
request,
hashtag: str,
local: bool = False,
only_media: bool = False,
max_id: str | None = None,
since_id: str | None = None,
min_id: str | None = None,
limit: int = 20,
):
if limit > 40:
limit = 40
2022-12-12 08:38:02 +01:00
queryset = (
2022-12-11 19:22:06 +01:00
Post.objects.public()
2022-12-17 03:42:48 +01:00
.filter(author__restriction=Identity.Restriction.none)
2022-12-11 19:22:06 +01:00
.tagged_with(hashtag)
.select_related("author")
.prefetch_related("attachments")
.order_by("-created")
)
if local:
2022-12-12 08:38:02 +01:00
queryset = queryset.filter(local=True)
2022-12-11 19:22:06 +01:00
if only_media:
2022-12-12 08:38:02 +01:00
queryset = queryset.filter(attachments__id__isnull=True)
paginator = MastodonPaginator(Post)
posts = paginator.paginate(
queryset,
min_id=min_id,
max_id=max_id,
since_id=since_id,
limit=limit,
)
2022-12-11 20:37:28 +01:00
interactions = PostInteraction.get_post_interactions(posts, request.identity)
return [post.to_mastodon_json(interactions=interactions) for post in posts]
2022-12-11 19:22:06 +01:00
@api_router.get("/v1/conversations", response=list[schemas.Status])
@identity_required
def conversations(
request,
max_id: str | None = None,
since_id: str | None = None,
min_id: str | None = None,
limit: int = 20,
):
# We don't implement this yet
return []