2023-02-07 19:07:15 +00:00
|
|
|
from django.http import HttpRequest
|
2023-02-14 04:17:26 +00:00
|
|
|
from hatchway import ApiError, ApiResponse, api_view
|
2022-12-29 17:31:32 +00:00
|
|
|
|
2023-03-10 16:10:34 +00:00
|
|
|
from activities.models import Post, TimelineEvent
|
2022-12-21 19:47:48 +00:00
|
|
|
from activities.services import TimelineService
|
2022-12-12 07:38:02 +00:00
|
|
|
from api import schemas
|
2023-02-19 18:37:02 +00:00
|
|
|
from api.decorators import scope_required
|
2023-02-07 19:07:15 +00:00
|
|
|
from api.pagination import MastodonPaginator, PaginatingApiResponse, PaginationResult
|
2022-12-29 17:31:32 +00:00
|
|
|
from core.models import Config
|
2022-12-11 07:25:48 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-11 18:22:06 +00:00
|
|
|
def home(
|
2022-12-29 17:31:32 +00:00
|
|
|
request: HttpRequest,
|
2022-12-11 18:22:06 +00:00
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> ApiResponse[list[schemas.Status]]:
|
2023-01-03 08:01:20 +00:00
|
|
|
# Grab a paginated result set of instances
|
2023-01-09 06:06:09 +00:00
|
|
|
paginator = MastodonPaginator()
|
2022-12-21 19:47:48 +00:00
|
|
|
queryset = TimelineService(request.identity).home()
|
2023-01-11 00:16:54 +00:00
|
|
|
queryset = queryset.select_related(
|
|
|
|
"subject_post_interaction__post",
|
|
|
|
"subject_post_interaction__post__author",
|
|
|
|
"subject_post_interaction__post__author__domain",
|
|
|
|
)
|
|
|
|
queryset = queryset.prefetch_related(
|
|
|
|
"subject_post__mentions__domain",
|
|
|
|
"subject_post_interaction__post__attachments",
|
|
|
|
"subject_post_interaction__post__mentions",
|
|
|
|
"subject_post_interaction__post__emojis",
|
|
|
|
"subject_post_interaction__post__mentions__domain",
|
|
|
|
"subject_post_interaction__post__author__posts",
|
|
|
|
)
|
2023-03-10 16:10:34 +00:00
|
|
|
pager: PaginationResult[TimelineEvent] = paginator.paginate(
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
2023-03-10 16:10:34 +00:00
|
|
|
home=True,
|
2022-12-12 07:38:02 +00:00
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
return PaginatingApiResponse(
|
|
|
|
schemas.Status.map_from_timeline_event(pager.results, request.identity),
|
|
|
|
request=request,
|
|
|
|
include_params=["limit"],
|
|
|
|
)
|
2022-12-11 18:22:06 +00:00
|
|
|
|
|
|
|
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-11 18:22:06 +00:00
|
|
|
def public(
|
2022-12-29 17:31:32 +00:00
|
|
|
request: HttpRequest,
|
2022-12-11 18:22:06 +00:00
|
|
|
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,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> ApiResponse[list[schemas.Status]]:
|
2022-12-29 17:31:32 +00:00
|
|
|
if not request.identity and not Config.system.public_timeline:
|
2023-02-07 19:07:15 +00:00
|
|
|
raise ApiError(error="public timeline is disabled", status=422)
|
2022-12-29 17:31:32 +00:00
|
|
|
|
2022-12-11 18:22:06 +00:00
|
|
|
if local:
|
2022-12-21 19:47:48 +00:00
|
|
|
queryset = TimelineService(request.identity).local()
|
|
|
|
else:
|
|
|
|
queryset = TimelineService(request.identity).federated()
|
|
|
|
if remote:
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset = queryset.filter(local=False)
|
2022-12-11 18:22:06 +00:00
|
|
|
if only_media:
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset = queryset.filter(attachments__id__isnull=True)
|
2023-01-03 08:01:20 +00:00
|
|
|
# Grab a paginated result set of instances
|
2023-01-09 06:06:09 +00:00
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 19:07:15 +00:00
|
|
|
pager: PaginationResult[Post] = paginator.paginate(
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
return PaginatingApiResponse(
|
|
|
|
schemas.Status.map_from_post(pager.results, request.identity),
|
|
|
|
request=request,
|
|
|
|
include_params=["limit", "local", "remote", "only_media"],
|
|
|
|
)
|
2022-12-11 18:22:06 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-11 18:22:06 +00:00
|
|
|
def hashtag(
|
2022-12-29 17:31:32 +00:00
|
|
|
request: HttpRequest,
|
2022-12-11 18:22:06 +00:00
|
|
|
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,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> ApiResponse[list[schemas.Status]]:
|
2022-12-11 18:22:06 +00:00
|
|
|
if limit > 40:
|
|
|
|
limit = 40
|
2023-04-06 21:14:21 +00:00
|
|
|
queryset = TimelineService(request.identity).hashtag(hashtag.lower())
|
2022-12-11 18:22:06 +00:00
|
|
|
if local:
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset = queryset.filter(local=True)
|
2022-12-11 18:22:06 +00:00
|
|
|
if only_media:
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset = queryset.filter(attachments__id__isnull=True)
|
2023-01-03 08:01:20 +00:00
|
|
|
# Grab a paginated result set of instances
|
2023-01-09 06:06:09 +00:00
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 19:07:15 +00:00
|
|
|
pager: PaginationResult[Post] = paginator.paginate(
|
2022-12-12 07:38:02 +00:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
return PaginatingApiResponse(
|
|
|
|
schemas.Status.map_from_post(pager.results, request.identity),
|
|
|
|
request=request,
|
|
|
|
include_params=["limit", "local", "remote", "only_media"],
|
|
|
|
)
|
2022-12-11 18:22:06 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:conversations")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2022-12-11 18:22:06 +00:00
|
|
|
def conversations(
|
2022-12-29 17:31:32 +00:00
|
|
|
request: HttpRequest,
|
2022-12-11 18:22:06 +00:00
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> list[schemas.Status]:
|
2022-12-11 18:22:06 +00:00
|
|
|
# We don't implement this yet
|
|
|
|
return []
|
2023-01-21 02:49:55 +00:00
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:favourites")
|
2023-02-07 19:07:15 +00:00
|
|
|
@api_view.get
|
2023-01-21 02:49:55 +00:00
|
|
|
def favourites(
|
|
|
|
request: HttpRequest,
|
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
2023-02-07 19:07:15 +00:00
|
|
|
) -> ApiResponse[list[schemas.Status]]:
|
2023-01-21 02:49:55 +00:00
|
|
|
queryset = TimelineService(request.identity).likes()
|
|
|
|
|
|
|
|
paginator = MastodonPaginator()
|
2023-02-07 19:07:15 +00:00
|
|
|
pager: PaginationResult[Post] = paginator.paginate(
|
2023-01-21 02:49:55 +00:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2023-02-07 19:07:15 +00:00
|
|
|
return PaginatingApiResponse(
|
|
|
|
schemas.Status.map_from_post(pager.results, request.identity),
|
|
|
|
request=request,
|
|
|
|
include_params=["limit"],
|
|
|
|
)
|