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]
|
|
|
|
|
2020-01-03 14:47:57 +00:00
|
|
|
include "../views/opensearch.nimf"
|
|
|
|
|
2019-09-13 20:24:58 +00:00
|
|
|
export search
|
|
|
|
|
|
|
|
proc createSearchRouter*(cfg: Config) =
|
|
|
|
router search:
|
2019-09-28 01:22:46 +00:00
|
|
|
get "/search/?":
|
2020-06-17 12:12:38 +00:00
|
|
|
if @"q".len > 500:
|
2019-10-21 03:19:00 +00:00
|
|
|
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
|
2019-10-08 11:45:47 +00:00
|
|
|
of users:
|
2019-09-30 20:24:01 +00:00
|
|
|
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)
|
2019-10-08 11:54:20 +00:00
|
|
|
of tweets:
|
2020-06-09 14:45:21 +00:00
|
|
|
let
|
|
|
|
tweets = await getSearch[Tweet](query, getCursor())
|
|
|
|
rss = "/search/rss?" & genQueryUrl(query)
|
2019-10-21 03:19:00 +00:00
|
|
|
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":
|
2019-09-30 20:24:01 +00:00
|
|
|
redirect("/search?q=" & encodeUrl("#" & @"hash"))
|
2020-01-03 14:47:57 +00:00
|
|
|
|
|
|
|
get "/opensearch":
|
2021-12-30 03:18:40 +00:00
|
|
|
let url = getUrlPrefix(cfg) & "/search?q="
|
2020-01-03 14:47:57 +00:00
|
|
|
resp Http200, {"Content-Type": "application/opensearchdescription+xml"},
|
|
|
|
generateOpenSearchXML(cfg.title, cfg.hostname, url)
|