Skip to content

Commit

Permalink
Implement user_id to screen_name router
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyzy823 committed Dec 28, 2021
1 parent aa2fed1 commit 52af6b2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ proc getProfile*(username: string): Future[Profile] {.async.} =
url = userShow ? ps
result = parseUserShow(await fetch(url, oldApi=true), username)

proc getProfileById*(userId: string): Future[Profile] {.async.} =
let
ps = genParams({"user_id": userId})
url = userShow ? ps
result = parseUserShowId(await fetch(url, oldApi=true), userId)

proc getTimeline*(id: string; after=""; replies=false): Future[Timeline] {.async.} =
let
ps = genParams({"userId": id, "include_tweet_replies": $replies}, after)
Expand Down
12 changes: 12 additions & 0 deletions src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ proc parseUserShow*(js: JsonNode; username: string): Profile =

result = parseProfile(js)

proc parseUserShowId*(js: JsonNode; userId: string): Profile =
if js.isNull:
return Profile(id: userId)

with error, js{"errors"}:
result = Profile(id: userId)
if error.getError == suspended:
result.suspended = true
return

result = parseProfile(js)

proc parseGraphProfile*(js: JsonNode; username: string): Profile =
if js.isNull: return
with error, js{"errors"}:
Expand Down
10 changes: 10 additions & 0 deletions src/redis_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ proc cache*(data: Profile) {.async.} =
pool.withAcquire(r):
r.startPipelining()
discard await r.setex(name.profileKey, baseCacheTime, compress(toFlatty(data)))
discard await r.setex("i:" & data.id , baseCacheTime, data.username)
discard await r.hset(name.pidKey, name, data.id)
discard await r.flushPipeline()

Expand Down Expand Up @@ -110,6 +111,15 @@ proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.}
elif fetch:
result = await getProfile(username)

proc getCachedProfileScreenName*(userId: string): Future[string] {.async.} =
let username = await get("i:" & userId)
if username != redisNil:
result = username
else:
let profile = await getProfileById(userId)
result = profile.username
await cache(profile)

proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
if name.len == 0: return
let rail = await get("pr:" & toLower(name))
Expand Down
14 changes: 14 additions & 0 deletions src/routes/timeline.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,22 @@ template respTimeline*(timeline: typed) =
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t

template respUserId*() =
cond @"user_id".len > 0
let username = await getCachedProfileScreenName(@"user_id")
if username.len > 0:
redirect("/" & username)
else:
resp Http404, showError("User not found", cfg)

proc createTimelineRouter*(cfg: Config) =
router timeline:
get "/i/user/@user_id":
respUserId()

get "/intent/user":
respUserId()

get "/@name/?@tab?/?":
cond '.' notin @"name"
cond @"name" notin ["pic", "gif", "video"]
Expand Down
7 changes: 5 additions & 2 deletions src/routes/unsupported.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ proc createUnsupportedRouter*(cfg: Config) =
resp renderMain(renderFeature(), request, cfg, themePrefs())

get "/about/feature": feature()
get "/intent/?@i?": feature()
get "/login/?@i?": feature()
get "/@name/lists/?": feature()

get "/intent/?@i?":
cond @"i" notin ["user"]
feature()

get "/i/@i?/?@j?":
cond @"i" notin ["status", "lists"]
cond @"i" notin ["status", "lists" , "user"]
feature()

0 comments on commit 52af6b2

Please sign in to comment.