2022-12-11 10:22:06 -08:00
|
|
|
from typing import Literal
|
|
|
|
|
2023-02-13 20:17:26 -08:00
|
|
|
from hatchway import Field, api_view
|
|
|
|
|
2022-12-11 11:37:28 -08:00
|
|
|
from activities.models import PostInteraction
|
2022-12-20 02:17:52 -08:00
|
|
|
from activities.services.search import SearchService
|
2022-12-11 10:22:06 -08:00
|
|
|
from api import schemas
|
2023-02-19 10:37:02 -08:00
|
|
|
from api.decorators import scope_required
|
2022-12-11 10:22:06 -08:00
|
|
|
|
|
|
|
|
2023-02-19 10:37:02 -08:00
|
|
|
@scope_required("read")
|
2023-02-07 11:07:15 -08:00
|
|
|
@api_view.get
|
2022-12-11 10:22:06 -08:00
|
|
|
def search(
|
|
|
|
request,
|
|
|
|
q: str,
|
2023-08-20 19:25:48 -07:00
|
|
|
type: Literal["accounts", "hashtags", "statuses", ""] | None = None,
|
2022-12-11 10:22:06 -08:00
|
|
|
fetch_identities: bool = Field(False, alias="resolve"),
|
|
|
|
following: bool = False,
|
|
|
|
exclude_unreviewed: bool = False,
|
|
|
|
account_id: str | None = None,
|
|
|
|
max_id: str | None = None,
|
|
|
|
since_id: str | None = None,
|
|
|
|
min_id: str | None = None,
|
|
|
|
limit: int = 20,
|
|
|
|
offset: int = 0,
|
2023-02-07 11:07:15 -08:00
|
|
|
) -> schemas.Search:
|
2022-12-11 10:22:06 -08:00
|
|
|
if limit > 40:
|
|
|
|
limit = 40
|
|
|
|
result: dict[str, list] = {"accounts": [], "statuses": [], "hashtags": []}
|
|
|
|
# We don't support pagination for searches yet
|
|
|
|
if max_id or since_id or min_id or offset:
|
2023-02-07 11:07:15 -08:00
|
|
|
return schemas.Search(**result)
|
2022-12-11 10:22:06 -08:00
|
|
|
# Run search
|
2022-12-20 02:17:52 -08:00
|
|
|
searcher = SearchService(q, request.identity)
|
2022-12-11 10:22:06 -08:00
|
|
|
search_result = searcher.search_all()
|
2023-08-20 19:25:48 -07:00
|
|
|
if type == "":
|
|
|
|
type = None
|
2022-12-11 10:22:06 -08:00
|
|
|
if type is None or type == "accounts":
|
2023-02-07 11:07:15 -08:00
|
|
|
result["accounts"] = [
|
|
|
|
schemas.Account.from_identity(i, include_counts=False)
|
|
|
|
for i in search_result["identities"]
|
|
|
|
]
|
2022-12-11 10:22:06 -08:00
|
|
|
if type is None or type == "hashtag":
|
2023-05-10 09:17:00 -07:00
|
|
|
result["hashtags"] = [
|
2023-02-07 11:07:15 -08:00
|
|
|
schemas.Tag.from_hashtag(h) for h in search_result["hashtags"]
|
|
|
|
]
|
2022-12-11 10:22:06 -08:00
|
|
|
if type is None or type == "statuses":
|
2022-12-11 11:37:28 -08:00
|
|
|
interactions = PostInteraction.get_post_interactions(
|
|
|
|
search_result["posts"], request.identity
|
|
|
|
)
|
|
|
|
result["statuses"] = [
|
2023-02-15 09:42:56 -08:00
|
|
|
schemas.Status.from_post(
|
|
|
|
p, interactions=interactions, identity=request.identity
|
|
|
|
)
|
2022-12-11 11:37:28 -08:00
|
|
|
for p in search_result["posts"]
|
|
|
|
]
|
2023-02-07 11:07:15 -08:00
|
|
|
return schemas.Search(**result)
|