nitter/src/routes/search.nim

49 lines
1.4 KiB
Nim
Raw Normal View History

import strutils, sequtils, uri
2019-09-13 20:24:58 +00:00
import jester
import router_utils
2020-06-01 00:22:56 +00:00
import ".."/[query, types, api]
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":
var url = ""
if cfg.useHttps:
url = "https://" & cfg.hostname & "/search?q="
else:
url = "http://" & cfg.hostname & "/search?q="
resp Http200, {"Content-Type": "application/opensearchdescription+xml"},
generateOpenSearchXML(cfg.title, cfg.hostname, url)