From fc8a21fc5c6809ea115092eeec57e09e984cdd76 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 11 Dec 2022 11:22:06 -0700 Subject: More API read coverage --- api/views/accounts.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) (limited to 'api/views/accounts.py') diff --git a/api/views/accounts.py b/api/views/accounts.py index 79906dc..1b883e8 100644 --- a/api/views/accounts.py +++ b/api/views/accounts.py @@ -1,9 +1,94 @@ -from .. import schemas +from django.shortcuts import get_object_or_404 + +from activities.models import Post +from api import schemas +from api.views.base import api_router +from users.models import Identity + from ..decorators import identity_required -from .base import api -@api.get("/v1/accounts/verify_credentials", response=schemas.Account) +@api_router.get("/v1/accounts/verify_credentials", response=schemas.Account) @identity_required def verify_credentials(request): return request.identity.to_mastodon_json() + + +@api_router.get("/v1/accounts/relationships", response=list[schemas.Relationship]) +@identity_required +def account_relationships(request): + ids = request.GET.getlist("id[]") + result = [] + for id in ids: + identity = get_object_or_404(Identity, pk=id) + result.append( + { + "id": identity.pk, + "following": identity.inbound_follows.filter( + source=request.identity + ).exists(), + "followed_by": identity.outbound_follows.filter( + target=request.identity + ).exists(), + "showing_reblogs": True, + "notifying": False, + "blocking": False, + "blocked_by": False, + "muting": False, + "muting_notifications": False, + "requested": False, + "domain_blocking": False, + "endorsed": False, + "note": "", + } + ) + return result + + +@api_router.get("/v1/accounts/{id}", response=schemas.Account) +@identity_required +def account(request, id: str): + identity = get_object_or_404(Identity, pk=id) + return identity.to_mastodon_json() + + +@api_router.get("/v1/accounts/{id}/statuses", response=list[schemas.Status]) +@identity_required +def account_statuses( + request, + id: str, + exclude_reblogs: bool = False, + exclude_replies: bool = False, + only_media: bool = False, + pinned: bool = False, + tagged: str | None = None, + max_id: str | None = None, + since_id: str | None = None, + min_id: str | None = None, + limit: int = 20, +): + identity = get_object_or_404(Identity, pk=id) + posts = ( + identity.posts.public() + .select_related("author") + .prefetch_related("attachments") + .order_by("-created") + ) + if pinned: + return [] + if only_media: + posts = posts.filter(attachments__pk__isnull=False) + if tagged: + posts = posts.tagged_with(tagged) + if max_id: + anchor_post = Post.objects.get(pk=max_id) + posts = posts.filter(created__lt=anchor_post.created) + if since_id: + anchor_post = Post.objects.get(pk=since_id) + posts = posts.filter(created__gt=anchor_post.created) + if min_id: + # Min ID requires LIMIT posts _immediately_ newer than specified, so we + # invert the ordering to accomodate + anchor_post = Post.objects.get(pk=min_id) + posts = posts.filter(created__gt=anchor_post.created).order_by("created") + return [post.to_mastodon_json() for post in posts[:limit]] -- cgit v1.2.3