From 376b444500a6ef2a69e0a8f41e85ef8c8f6314b4 Mon Sep 17 00:00:00 2001 From: Zed Date: Fri, 21 Apr 2023 04:28:43 +0200 Subject: [PATCH] Fix empty search query handling --- src/api.nim | 55 +++++++++++++++++--------------- src/experimental/parser/user.nim | 7 ++-- src/types.nim | 1 + 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/api.nim b/src/api.nim index ed6b4f6..b23aa87 100644 --- a/src/api.nim +++ b/src/api.nim @@ -69,24 +69,6 @@ proc getGraphListMembers*(list: List; after=""): Future[Result[User]] {.async.} let url = graphListMembers ? {"variables": $variables, "features": gqlFeatures} result = parseGraphListMembers(await fetchRaw(url, Api.listMembers), after) -proc getGraphSearch*(query: Query; after=""): Future[Result[Tweet]] {.async.} = - let q = genQueryParam(query) - if q.len == 0 or q == emptyQuery: return - var - variables = %*{ - "rawQuery": q, - "count": 20, - "product": "Latest", - "withDownvotePerspective": false, - "withReactionsMetadata": false, - "withReactionsPerspective": false - } - if after.len > 0: - variables["cursor"] = % after - let url = graphSearchTimeline ? {"variables": $variables, "features": gqlFeatures} - result = parseGraphSearch(await fetch(url, Api.search), after) - result.query = query - proc getGraphTweetResult*(id: string): Future[Tweet] {.async.} = if id.len == 0: return let @@ -113,15 +95,30 @@ proc getTweet*(id: string; after=""): Future[Conversation] {.async.} = if after.len > 0: result.replies = await getReplies(id, after) -proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} = - if name.len == 0: return - let - ps = genParams({"screen_name": name, "trim_user": "true"}, - count="18", ext=false) - url = photoRail ? ps - result = parsePhotoRail(await fetch(url, Api.timeline)) +proc getGraphSearch*(query: Query; after=""): Future[Result[Tweet]] {.async.} = + let q = genQueryParam(query) + if q.len == 0 or q == emptyQuery: + return Result[Tweet](query: query, beginning: true) + + var + variables = %*{ + "rawQuery": q, + "count": 20, + "product": "Latest", + "withDownvotePerspective": false, + "withReactionsMetadata": false, + "withReactionsPerspective": false + } + if after.len > 0: + variables["cursor"] = % after + let url = graphSearchTimeline ? {"variables": $variables, "features": gqlFeatures} + result = parseGraphSearch(await fetch(url, Api.search), after) + result.query = query proc getUserSearch*(query: Query; page="1"): Future[Result[User]] {.async.} = + if query.text.len == 0: + return Result[User](query: query, beginning: true) + var url = userSearch ? { "q": query.text, "skip_status": "1", @@ -136,6 +133,14 @@ proc getUserSearch*(query: Query; page="1"): Future[Result[User]] {.async.} = elif page.allCharsInSet(Digits): result.bottom = $(parseInt(page) + 1) +proc getPhotoRail*(name: string): Future[PhotoRail] {.async.} = + if name.len == 0: return + let + ps = genParams({"screen_name": name, "trim_user": "true"}, + count="18", ext=false) + url = photoRail ? ps + result = parsePhotoRail(await fetch(url, Api.timeline)) + proc resolve*(url: string; prefs: Prefs): Future[string] {.async.} = let client = newAsyncHttpClient(maxRedirects=0) try: diff --git a/src/experimental/parser/user.nim b/src/experimental/parser/user.nim index ec35aae..a956aa8 100644 --- a/src/experimental/parser/user.nim +++ b/src/experimental/parser/user.nim @@ -80,7 +80,6 @@ proc parseUser*(json: string; username=""): User = proc parseUsers*(json: string; after=""): Result[User] = result = Result[User](beginning: after.len == 0) - if json[0] == '[': - let raw = json.fromJson(seq[RawUser]) - for user in raw: - result.content.add user.toUser + let raw = json.fromJson(seq[RawUser]) + for user in raw: + result.content.add user.toUser diff --git a/src/types.nim b/src/types.nim index d2e7aad..13d2f91 100644 --- a/src/types.nim +++ b/src/types.nim @@ -45,6 +45,7 @@ type null = 0 noUserMatches = 17 protectedUser = 22 + paramsMissing = 25 couldntAuth = 32 doesntExist = 34 userNotFound = 50