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"), minId: html.selectAttr(".stream-container", "data-min-position"),
hasMore: html.select(".has-more-items") != nil, hasMore: html.select(".has-more-items") != nil,
beginning: true, beginning: true,
query: Query(kind: users), query: Query(kind: userList),
content: html.selectAll(".account").map(parseListProfile) 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) 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 if json == nil or not json.hasKey("items_html"): return
let html = json["items_html"].to(string) 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.} = proc getSearch*[T](query: Query; after, agent: string): Future[Result[T]] {.async.} =
let 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 pos = when T is Tweet: genPos(after) else: after
param = genQueryParam(query) param = genQueryParam(query)

View File

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

View File

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

View File

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

View File

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

View File

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