nitter/src/routes/search.nim

52 lines
1.5 KiB
Nim
Raw Normal View History

2021-12-27 01:37:38 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
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/?":
let q = @"q"
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))
title = "Search" & (if q.len > 0: " (" & q & ")" else: "")
2019-09-13 20:24:58 +00:00
case query.kind
of users:
if "," in q:
redirect("/" & q)
var users: Result[User]
try:
users = await getUserSearch(query, getCursor())
except InternalError:
users = Result[User](beginning: true, query: query)
resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs, title)
of tweets:
2020-06-09 14:45:21 +00:00
let
tweets = await getGraphSearch(query, getCursor())
2020-06-09 14:45:21 +00:00
rss = "/search/rss?" & genQueryUrl(query)
resp renderMain(renderTweetSearch(tweets, prefs, getPath()),
request, cfg, prefs, title, 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"},
2022-11-27 15:20:53 +00:00
generateOpenSearchXML(cfg.title, cfg.hostname, url)