2022-12-29 09:31:32 -08:00
|
|
|
from django.http import HttpRequest, HttpResponse, JsonResponse
|
|
|
|
|
2022-12-21 11:47:48 -08:00
|
|
|
from activities.models import Post, PostInteraction
|
|
|
|
from activities.services import TimelineService
|
2022-12-11 23:38:02 -08: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-29 09:31:32 -08:00
|
|
|
from core.models import Config
|
2022-12-10 23:25:48 -08:00
|
|
|
|
|
|
|
|
2022-12-11 10:22:06 -08:00
|
|
|
@api_router.get("/v1/timelines/home", response=list[schemas.Status])
|
2022-12-10 23:25:48 -08:00
|
|
|
@identity_required
|
2022-12-11 10:22:06 -08:00
|
|
|
def home(
|
2022-12-29 09:31:32 -08:00
|
|
|
request: HttpRequest,
|
|
|
|
response: HttpResponse,
|
2022-12-11 10:22:06 -08:00
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
|
|
|
):
|
2022-12-29 13:00:37 -08:00
|
|
|
paginator = MastodonPaginator(Post, sort_attribute="published")
|
2022-12-21 11:47:48 -08:00
|
|
|
queryset = TimelineService(request.identity).home()
|
2022-12-29 09:31:32 -08:00
|
|
|
pager = paginator.paginate(
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2022-12-29 09:31:32 -08:00
|
|
|
interactions = PostInteraction.get_event_interactions(
|
|
|
|
pager.results, request.identity
|
|
|
|
)
|
|
|
|
|
|
|
|
if pager.results:
|
2022-12-29 10:46:25 -08:00
|
|
|
response.headers["Link"] = pager.link_header(request, ["limit"])
|
2022-12-29 09:31:32 -08:00
|
|
|
|
2022-12-11 11:37:28 -08:00
|
|
|
return [
|
2022-12-29 09:53:31 -08:00
|
|
|
event.to_mastodon_status_json(interactions=interactions)
|
2022-12-29 09:31:32 -08:00
|
|
|
for event in pager.results
|
2022-12-11 11:37:28 -08:00
|
|
|
]
|
2022-12-11 10:22:06 -08:00
|
|
|
|
|
|
|
|
|
|
|
@api_router.get("/v1/timelines/public", response=list[schemas.Status])
|
|
|
|
def public(
|
2022-12-29 09:31:32 -08:00
|
|
|
request: HttpRequest,
|
|
|
|
response: HttpResponse,
|
2022-12-11 10:22:06 -08: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,
|
|
|
|
):
|
2022-12-29 09:31:32 -08:00
|
|
|
if not request.identity and not Config.system.public_timeline:
|
|
|
|
return JsonResponse({"error": "public timeline is disabled"}, status=422)
|
|
|
|
|
2022-12-11 10:22:06 -08:00
|
|
|
if local:
|
2022-12-21 11:47:48 -08:00
|
|
|
queryset = TimelineService(request.identity).local()
|
|
|
|
else:
|
|
|
|
queryset = TimelineService(request.identity).federated()
|
|
|
|
if remote:
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset = queryset.filter(local=False)
|
2022-12-11 10:22:06 -08:00
|
|
|
if only_media:
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset = queryset.filter(attachments__id__isnull=True)
|
2022-12-29 13:00:37 -08:00
|
|
|
paginator = MastodonPaginator(Post, sort_attribute="published")
|
2022-12-29 09:31:32 -08:00
|
|
|
pager = paginator.paginate(
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2022-12-29 09:31:32 -08:00
|
|
|
|
|
|
|
if pager.results:
|
2022-12-29 10:46:25 -08:00
|
|
|
response.headers["Link"] = pager.link_header(
|
|
|
|
request,
|
|
|
|
["limit", "local", "remote", "only_media"],
|
2022-12-29 09:31:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
interactions = PostInteraction.get_post_interactions(
|
|
|
|
pager.results, request.identity
|
|
|
|
)
|
|
|
|
return [post.to_mastodon_json(interactions=interactions) for post in pager.results]
|
2022-12-11 10:22:06 -08:00
|
|
|
|
|
|
|
|
|
|
|
@api_router.get("/v1/timelines/tag/{hashtag}", response=list[schemas.Status])
|
|
|
|
@identity_required
|
|
|
|
def hashtag(
|
2022-12-29 09:31:32 -08:00
|
|
|
request: HttpRequest,
|
|
|
|
response: HttpResponse,
|
2022-12-11 10:22:06 -08: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,
|
|
|
|
):
|
|
|
|
if limit > 40:
|
|
|
|
limit = 40
|
2022-12-21 11:47:48 -08:00
|
|
|
queryset = TimelineService(request.identity).hashtag(hashtag)
|
2022-12-11 10:22:06 -08:00
|
|
|
if local:
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset = queryset.filter(local=True)
|
2022-12-11 10:22:06 -08:00
|
|
|
if only_media:
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset = queryset.filter(attachments__id__isnull=True)
|
2022-12-29 13:00:37 -08:00
|
|
|
paginator = MastodonPaginator(Post, sort_attribute="published")
|
2022-12-29 09:31:32 -08:00
|
|
|
pager = paginator.paginate(
|
2022-12-11 23:38:02 -08:00
|
|
|
queryset,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
since_id=since_id,
|
|
|
|
limit=limit,
|
|
|
|
)
|
2022-12-29 09:31:32 -08:00
|
|
|
|
|
|
|
if pager.results:
|
2022-12-29 10:46:25 -08:00
|
|
|
response.headers["Link"] = pager.link_header(
|
|
|
|
request,
|
|
|
|
["limit", "local", "remote", "only_media"],
|
2022-12-29 09:31:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
interactions = PostInteraction.get_post_interactions(
|
|
|
|
pager.results, request.identity
|
|
|
|
)
|
|
|
|
return [post.to_mastodon_json(interactions=interactions) for post in pager.results]
|
2022-12-11 10:22:06 -08:00
|
|
|
|
|
|
|
|
|
|
|
@api_router.get("/v1/conversations", response=list[schemas.Status])
|
|
|
|
@identity_required
|
|
|
|
def conversations(
|
2022-12-29 09:31:32 -08:00
|
|
|
request: HttpRequest,
|
|
|
|
response: HttpResponse,
|
2022-12-11 10:22:06 -08:00
|
|
|
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 []
|