1
0
Fork 0
mirror of https://github.com/zedeus/nitter.git synced 2025-04-21 16:34:12 +00:00

feat: add /api/search

This commit is contained in:
guanbinrui 2025-04-02 21:21:03 +08:00
parent 5062656664
commit 3846cf30e8
No known key found for this signature in database
GPG key ID: 849DB262B0F9CFE6

39
src/jsons/search.nim Normal file
View file

@ -0,0 +1,39 @@
# SPDX-License-Identifier: AGPL-3.0-only
import strutils, uri
import jester
import ".."/routes/[router_utils, timeline]
import ".."/[query, types, api, formatters]
import ../views/[general, search]
proc createJsonApiSearchRouter*(cfg: Config) =
router jsonapi_search:
get "/api/search?":
let q = @"q"
if q.len > 500:
respJsonError "Search input too long."
let
prefs = cookiePrefs()
query = initQuery(params(request))
title = "Search" & (if q.len > 0: " (" & q & ")" else: "")
case query.kind
of users:
if "," in q:
redirect("/" & q)
var users: Result[User]
try:
users = await getGraphUserSearch(query, getCursor())
except InternalError:
users = Result[User](beginning: true, query: query)
respJsonSuccess formatUsersAsJson(users)
of tweets:
let
tweets = await getGraphTweetSearch(query, getCursor())
respJsonSuccess formatTweetsAsJson(tweets)
else:
respJsonError "Invalid search"
get "/api/hashtag/@hash":
redirect("/search?q=" & encodeUrl("#" & @"hash"))