Improve search endpoint compatibility

Fixes #63
This commit is contained in:
Zed 2019-10-08 13:45:47 +02:00
parent aa4f3f69bb
commit 1c9c6a2947
7 changed files with 15 additions and 15 deletions

View file

@ -35,7 +35,7 @@ proc getListMembers*(username, list, agent: string): Future[Result[Profile]] {.a
minId: html.selectAttr(".stream-container", "data-min-position"),
hasMore: html.select(".has-more-items") != nil,
beginning: true,
query: Query(kind: users),
query: Query(kind: userList),
content: html.selectAll(".account").map(parseListProfile)
)
@ -56,7 +56,7 @@ proc getListMembersSearch*(username, list, agent, after: string): Future[Result[
let json = await fetchJson(url ? params, headers)
result = getResult[Profile](json, Query(kind: users), after)
result = getResult[Profile](json, Query(kind: userList), after)
if json == nil or not json.hasKey("items_html"): return
let html = json["items_html"].to(string)

View file

@ -16,7 +16,7 @@ proc getResult*[T](json: JsonNode; query: Query; after: string): Result[T] =
proc getSearch*[T](query: Query; after, agent: string): Future[Result[T]] {.async.} =
let
kind = if query.kind == userSearch: "users" else: "tweets"
kind = if query.kind == users: "users" else: "tweets"
pos = when T is Tweet: genPos(after) else: after
param = genQueryParam(query)

View file

@ -24,7 +24,7 @@ template `@`(param: string): untyped =
proc initQuery*(pms: Table[string, string]; name=""): Query =
result = Query(
kind: parseEnum[QueryKind](@"kind", custom),
kind: parseEnum[QueryKind](@"f", custom),
text: @"q",
filters: validFilters.filterIt("f-" & it in pms),
excludes: validFilters.filterIt("e-" & it in pms),
@ -58,7 +58,7 @@ proc genQueryParam*(query: Query): string =
var filters: seq[string]
var param: string
if query.kind == userSearch:
if query.kind == users:
return query.text
for i, user in query.fromUser:
@ -84,9 +84,9 @@ proc genQueryParam*(query: Query): string =
result &= " " & query.text
proc genQueryUrl*(query: Query): string =
if query.kind notin {custom, userSearch}: return
if query.kind notin {custom, users}: return
var params = @[&"kind={query.kind}"]
var params = @[&"f={query.kind}"]
if query.text.len > 0:
params.add "q=" & encodeUrl(query.text)
for f in query.filters:

View file

@ -18,7 +18,7 @@ proc createSearchRouter*(cfg: Config) =
let query = initQuery(params(request))
case query.kind
of userSearch:
of users:
if "," in @"q":
redirect("/" & @"q")
let users = await getSearch[Profile](query, @"after", getAgent())

View file

@ -57,7 +57,7 @@ dbFromTypes("cache.db", "", "", "", [Profile, Video])
type
QueryKind* = enum
posts, replies, media, users, userSearch, custom
posts, replies, media, users, custom, userList
Query* = object
kind*: QueryKind

View file

@ -8,7 +8,7 @@ proc renderListTabs*(query: Query; path: string): VNode =
buildHtml(ul(class="tab")):
li(class=query.getTabClass(posts)):
a(href=(path)): text "Tweets"
li(class=query.getTabClass(users)):
li(class=query.getTabClass(userList)):
a(href=(path & "/members")): text "Members"
proc renderList*(body: VNode; query: Query; name, list: string): VNode =

View file

@ -23,7 +23,7 @@ proc renderSearch*(): VNode =
buildHtml(tdiv(class="panel-container")):
tdiv(class="search-bar"):
form(`method`="get", action="/search"):
hiddenField("kind", "userSearch")
hiddenField("f", "users")
input(`type`="text", name="q", autofocus="", placeholder="Enter username...")
button(`type`="submit"): icon "search"
@ -45,8 +45,8 @@ proc renderSearchTabs*(query: Query): VNode =
li(class=query.getTabClass(custom)):
q.kind = custom
a(href=("?" & genQueryUrl(q))): text "Tweets"
li(class=query.getTabClass(userSearch)):
q.kind = userSearch
li(class=query.getTabClass(users)):
q.kind = users
a(href=("?" & genQueryUrl(q))): text "Users"
proc isPanelOpen(q: Query): bool =
@ -57,7 +57,7 @@ proc renderSearchPanel*(query: Query): VNode =
let user = query.fromUser.join(",")
let action = if user.len > 0: &"/{user}/search" else: "/search"
buildHtml(form(`method`="get", action=action, class="search-field")):
hiddenField("kind", "custom")
hiddenField("f", "custom")
genInput("q", "", query.text, "Enter search...",
class="pref-inline", autofocus=true)
button(`type`="submit"): icon "search"
@ -109,7 +109,7 @@ proc renderUserSearch*(users: Result[Profile]; prefs: Prefs): VNode =
buildHtml(tdiv(class="timeline-container")):
tdiv(class="timeline-header"):
form(`method`="get", action="/search", class="search-field"):
hiddenField("kind", "userSearch")
hiddenField("f", "users")
genInput("q", "", users.query.text, "Enter username...",
class="pref-inline", autofocus=true)
button(`type`="submit"): icon "search"