nitter/src/routes/search.nim

46 lines
1.3 KiB
Nim
Raw Normal View History

2021-12-27 01:37:38 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
2021-12-20 02:11:12 +00:00
import strutils, uri
2019-09-13 20:24:58 +00:00
import jester
import router_utils
2021-12-30 03:18:40 +00:00
import ".."/[query, types, api, formatters]
2019-09-13 20:24:58 +00:00
import ../views/[general, search]
include "../views/opensearch.nimf"
2019-09-13 20:24:58 +00:00
export search
proc createSearchRouter*(cfg: Config) =
router search:
get "/search/?":
if @"q".len > 500:
resp Http400, showError("Search input too long.", cfg)
2019-09-13 21:28:20 +00:00
2020-06-09 14:45:21 +00:00
let
prefs = cookiePrefs()
query = initQuery(params(request))
2019-09-13 20:24:58 +00:00
case query.kind
of users:
if "," in @"q":
redirect("/" & @"q")
2020-06-01 00:22:56 +00:00
let users = await getSearch[Profile](query, getCursor())
2020-06-09 14:45:21 +00:00
resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs)
of tweets:
2020-06-09 14:45:21 +00:00
let
tweets = await getSearch[Tweet](query, getCursor())
rss = "/search/rss?" & genQueryUrl(query)
resp renderMain(renderTweetSearch(tweets, prefs, getPath()),
2020-06-09 14:45:21 +00:00
request, cfg, prefs, rss=rss)
2019-09-13 20:24:58 +00:00
else:
2019-10-21 05:59:22 +00:00
resp Http404, showError("Invalid search", cfg)
2019-09-23 14:12:20 +00:00
get "/hashtag/@hash":
redirect("/search?q=" & encodeUrl("#" & @"hash"))
get "/opensearch":
2021-12-30 03:18:40 +00:00
let url = getUrlPrefix(cfg) & "/search?q="
resp Http200, {"Content-Type": "application/opensearchdescription+xml"},
generateOpenSearchXML(cfg.title, cfg.hostname, url)