Cleanup profile fetching logic

This commit is contained in:
Zed 2022-01-23 08:15:35 +01:00
parent 27183f1a74
commit c355beda85
1 changed files with 23 additions and 16 deletions

View File

@ -19,37 +19,44 @@ proc getQuery*(request: Request; tab, name: string): Query =
of "search": initQuery(params(request), name=name) of "search": initQuery(params(request), name=name)
else: Query(fromUser: @[name]) else: Query(fromUser: @[name])
template skipIf[T](cond: bool; default; body: Future[T]): Future[T] =
if cond:
let fut = newFuture[T]()
fut.complete(default)
fut
else:
body
proc fetchProfile*(after: string; query: Query; skipRail=false; proc fetchProfile*(after: string; query: Query; skipRail=false;
skipPinned=false): Future[Profile] {.async.} = skipPinned=false): Future[Profile] {.async.} =
let name = query.fromUser[0] let
name = query.fromUser[0]
userId = await getUserId(name)
let userId = await getUserId(name)
if userId.len == 0: if userId.len == 0:
return Profile(user: User(username: name)) return Profile(user: User(username: name))
elif userId == "suspended": elif userId == "suspended":
return Profile(user: User(username: name, suspended: true)) return Profile(user: User(username: name, suspended: true))
var rail: Future[PhotoRail]
if skipRail or result.user.protected or query.kind == media:
rail = newFuture[PhotoRail]()
rail.complete(@[])
else:
rail = getCachedPhotoRail(name)
# temporary fix to prevent errors from people browsing # temporary fix to prevent errors from people browsing
# timelines during/immediately after deployment # timelines during/immediately after deployment
var after = after var after = after
if query.kind in {posts, replies} and after.startsWith("scroll"): if query.kind in {posts, replies} and after.startsWith("scroll"):
after.setLen 0 after.setLen 0
let timeline = let
case query.kind timeline =
of posts: getTimeline(userId, after) case query.kind
of replies: getTimeline(userId, after, replies=true) of posts: getTimeline(userId, after)
of media: getMediaTimeline(userId, after) of replies: getTimeline(userId, after, replies=true)
else: getSearch[Tweet](query, after) of media: getMediaTimeline(userId, after)
else: getSearch[Tweet](query, after)
let user = await getCachedUser(name) rail =
skipIf(skipRail or query.kind == media, @[]):
getCachedPhotoRail(name)
user = await getCachedUser(name)
var pinned: Option[Tweet] var pinned: Option[Tweet]
if not skipPinned and user.pinnedTweet > 0 and if not skipPinned and user.pinnedTweet > 0 and