From a3e11e3272ebdd6118cd818591530ac8be5ae491 Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 23 Aug 2023 10:14:44 +0200 Subject: [PATCH] Switch to using typeahead for user search --- src/api.nim | 12 +++--------- src/consts.nim | 2 +- src/experimental/parser/user.nim | 7 +++++++ src/experimental/types/user.nim | 3 +++ src/routes/search.nim | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/api.nim b/src/api.nim index 4ac999c..a7efbd0 100644 --- a/src/api.nim +++ b/src/api.nim @@ -115,22 +115,16 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} = result = parseGraphSearch(await fetch(url, Api.search), after) result.query = query -proc getUserSearch*(query: Query; page="1"): Future[Result[User]] {.async.} = +proc getUserSearch*(query: Query): Future[Result[User]] {.async.} = if query.text.len == 0: return Result[User](query: query, beginning: true) let - page = if page.len == 0: "1" else: page - url = userSearch ? genParams({"q": query.text, "skip_status": "1", "page": page}) + url = userSearch ? genParams({"q": query.text, "result_type": "users"}) js = await fetchRaw(url, Api.userSearch) - result = parseUsers(js) - + result = parseTypeahead(js) result.query = query - if page.len == 0: - result.bottom = "2" - elif page.allCharsInSet(Digits): - result.bottom = $(parseInt(page) + 1) proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} = if name.len == 0: return diff --git a/src/consts.nim b/src/consts.nim index 8bf6422..1bb950e 100644 --- a/src/consts.nim +++ b/src/consts.nim @@ -9,7 +9,7 @@ const activate* = $(api / "1.1/guest/activate.json") photoRail* = api / "1.1/statuses/media_timeline.json" - userSearch* = api / "1.1/users/search.json" + userSearch* = api / "1.1/search/typeahead.json" graphql = api / "graphql" graphUser* = graphql / "u7wQyGi6oExe8_TRWGMq4Q/UserResultByScreenNameQuery" diff --git a/src/experimental/parser/user.nim b/src/experimental/parser/user.nim index 5962a87..400e740 100644 --- a/src/experimental/parser/user.nim +++ b/src/experimental/parser/user.nim @@ -85,3 +85,10 @@ proc parseUsers*(json: string; after=""): Result[User] = let raw = json.fromJson(seq[RawUser]) for user in raw: result.content.add user.toUser + +proc parseTypeahead*(json: string): Result[User] = + result = Result[User](beginning: true) + + let raw = json.fromJson(Typeahead) + for user in raw.users: + result.content.add user.toUser diff --git a/src/experimental/types/user.nim b/src/experimental/types/user.nim index 39331a0..7d34f7b 100644 --- a/src/experimental/types/user.nim +++ b/src/experimental/types/user.nim @@ -42,3 +42,6 @@ type Color* = object red*, green*, blue*: int + + Typeahead* = object + users*: seq[RawUser] diff --git a/src/routes/search.nim b/src/routes/search.nim index 676229e..e0a888c 100644 --- a/src/routes/search.nim +++ b/src/routes/search.nim @@ -29,7 +29,7 @@ proc createSearchRouter*(cfg: Config) = redirect("/" & q) var users: Result[User] try: - users = await getUserSearch(query, getCursor()) + users = await getUserSearch(query) except InternalError: users = Result[User](beginning: true, query: query) resp renderMain(renderUserSearch(users, prefs), request, cfg, prefs, title)