From 1c9c6a29479d155b3dc21fbbe6d22d948bb58582 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 8 Oct 2019 13:45:47 +0200 Subject: [PATCH] Improve search endpoint compatibility Fixes #63 --- src/api/list.nim | 4 ++-- src/api/search.nim | 2 +- src/query.nim | 8 ++++---- src/routes/search.nim | 2 +- src/types.nim | 2 +- src/views/list.nim | 2 +- src/views/search.nim | 10 +++++----- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/api/list.nim b/src/api/list.nim index dc3d4c1..ba4ca09 100644 --- a/src/api/list.nim +++ b/src/api/list.nim @@ -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) diff --git a/src/api/search.nim b/src/api/search.nim index 379244b..dd0acd3 100644 --- a/src/api/search.nim +++ b/src/api/search.nim @@ -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) diff --git a/src/query.nim b/src/query.nim index 1cbff3a..19e46d6 100644 --- a/src/query.nim +++ b/src/query.nim @@ -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: diff --git a/src/routes/search.nim b/src/routes/search.nim index 706804a..3d1a7d6 100644 --- a/src/routes/search.nim +++ b/src/routes/search.nim @@ -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()) diff --git a/src/types.nim b/src/types.nim index 7035c43..607b3f7 100644 --- a/src/types.nim +++ b/src/types.nim @@ -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 diff --git a/src/views/list.nim b/src/views/list.nim index 8020423..f66a605 100644 --- a/src/views/list.nim +++ b/src/views/list.nim @@ -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 = diff --git a/src/views/search.nim b/src/views/search.nim index aaf8d79..4200adf 100644 --- a/src/views/search.nim +++ b/src/views/search.nim @@ -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"