mirror of
https://github.com/jointakahe/takahe.git
synced 2024-11-22 15:21:01 +00:00
Add account search endpoint
This commit is contained in:
parent
f5f74dc150
commit
da38889aa7
2 changed files with 35 additions and 0 deletions
|
@ -1,6 +1,8 @@
|
|||
from django.shortcuts import get_object_or_404
|
||||
from ninja import Field
|
||||
|
||||
from activities.models import Post, PostInteraction
|
||||
from activities.services import SearchService
|
||||
from api import schemas
|
||||
from api.decorators import identity_required
|
||||
from api.pagination import MastodonPaginator
|
||||
|
@ -55,6 +57,28 @@ def familiar_followers(request):
|
|||
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)
|
||||
@identity_required
|
||||
def account(request, id: str):
|
||||
|
|
|
@ -10,3 +10,14 @@ def test_verify_credentials(api_token, identity, client):
|
|||
).json()
|
||||
assert response["id"] == str(identity.pk)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue