From a93f03d639be9581db4041cd2bc57a63998d04fb Mon Sep 17 00:00:00 2001 From: Tyler Kennedy Date: Fri, 30 Dec 2022 13:19:38 -0500 Subject: [PATCH] /v1/statuses/{id}/favourited_by (#324) --- api/views/statuses.py | 51 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/api/views/statuses.py b/api/views/statuses.py index eb3b464..86028f2 100644 --- a/api/views/statuses.py +++ b/api/views/statuses.py @@ -1,16 +1,25 @@ from typing import Literal from django.forms import ValidationError +from django.http import HttpRequest, HttpResponse from django.shortcuts import get_object_or_404 from ninja import Schema -from activities.models import Post, PostAttachment, PostInteraction, TimelineEvent +from activities.models import ( + Post, + PostAttachment, + PostInteraction, + PostInteractionStates, + TimelineEvent, +) from activities.services import PostService from api import schemas from api.views.base import api_router from core.models import Config +from users.models import Identity from ..decorators import identity_required +from ..pagination import MastodonPaginator class PostStatusSchema(Schema): @@ -116,6 +125,46 @@ def unfavourite_status(request, id: str): return post.to_mastodon_json(interactions=interactions) +@api_router.get("/v1/statuses/{id}/favourited_by", response=list[schemas.Account]) +def favourited_by( + request: HttpRequest, + response: HttpResponse, + id: str, + max_id: str | None = None, + since_id: str | None = None, + min_id: str | None = None, + limit: int = 20, +): + """ + View who favourited a given status. + """ + # This method should filter out private statuses, but we don't really have + # a concept of "private status" yet. + post = get_object_or_404(Post, pk=id) + + paginator = MastodonPaginator(Identity, sort_attribute="published") + pager = paginator.paginate( + post.interactions.filter( + type=PostInteraction.Types.like, + state__in=PostInteractionStates.group_active(), + ) + .select_related("identity") + .order_by("published"), + min_id=min_id, + max_id=max_id, + since_id=since_id, + limit=limit, + ) + + if pager.results: + response.headers["Link"] = pager.link_header( + request, + ["limit"], + ) + + return [result.identity.to_mastodon_json() for result in pager.results] + + @api_router.post("/v1/statuses/{id}/reblog", response=schemas.Status) @identity_required def reblog_status(request, id: str):