Add account search endpoint

This commit is contained in:
Andrew Godwin 2022-12-21 21:54:49 +00:00
parent f5f74dc150
commit da38889aa7
2 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,8 @@
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from ninja import Field
from activities.models import Post, PostInteraction from activities.models import Post, PostInteraction
from activities.services import SearchService
from api import schemas from api import schemas
from api.decorators import identity_required from api.decorators import identity_required
from api.pagination import MastodonPaginator from api.pagination import MastodonPaginator
@ -55,6 +57,28 @@ def familiar_followers(request):
return result return result
@api_router.get("/v1/accounts/search", response=list[schemas.Account])
@identity_required
def search(
request,
q: str,
fetch_identities: bool = Field(False, alias="resolve"),
following: bool = False,
limit: int = 20,
offset: int = 0,
):
"""
Handles searching for accounts by username or handle
"""
if limit > 40:
limit = 40
if offset:
return []
searcher = SearchService(q, request.identity)
search_result = searcher.search_identities_handle()
return [i.to_mastodon_json() for i in search_result]
@api_router.get("/v1/accounts/{id}", response=schemas.Account) @api_router.get("/v1/accounts/{id}", response=schemas.Account)
@identity_required @identity_required
def account(request, id: str): def account(request, id: str):

View File

@ -10,3 +10,14 @@ def test_verify_credentials(api_token, identity, client):
).json() ).json()
assert response["id"] == str(identity.pk) assert response["id"] == str(identity.pk)
assert response["username"] == identity.username assert response["username"] == identity.username
@pytest.mark.django_db
def test_account_search(api_token, identity, client):
response = client.get(
"/api/v1/accounts/search?q=test",
HTTP_AUTHORIZATION=f"Bearer {api_token.token}",
HTTP_ACCEPT="application/json",
).json()
assert response[0]["id"] == str(identity.pk)
assert response[0]["username"] == identity.username