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