Refactor timeline route

This commit is contained in:
Zed 2020-01-07 02:23:20 +01:00
parent a39b891a3d
commit 2e97c1c98f

View file

@ -10,6 +10,19 @@ export router_utils
export api, cache, formatters, query, agents export api, cache, formatters, query, agents
export profile, timeline, status export profile, timeline, status
proc getQuery*(request: Request; tab, name: string): Query =
case tab
of "with_replies": getReplyQuery(name)
of "media": getMediaQuery(name)
of "search": initQuery(params(request), name=name)
else: Query()
proc fetchTimeline*(name, after, agent: string; query: Query): Future[Timeline] =
case query.kind
of QueryKind.media: getMediaTimeline(name, after, agent)
of posts: getTimeline(name, after, agent)
else: getSearch[Tweet](query, after, agent)
proc fetchSingleTimeline*(name, after, agent: string; query: Query; proc fetchSingleTimeline*(name, after, agent: string; query: Query;
media=true): Future[(Profile, Timeline)] {.async.} = media=true): Future[(Profile, Timeline)] {.async.} =
var timeline: Timeline var timeline: Timeline
@ -19,18 +32,11 @@ proc fetchSingleTimeline*(name, after, agent: string; query: Query;
if cachedProfile.isSome: if cachedProfile.isSome:
profile = get(cachedProfile) profile = get(cachedProfile)
if query.kind == posts: if query.kind == posts and cachedProfile.isNone:
if cachedProfile.isSome:
timeline = await getTimeline(name, after, agent, media)
else:
(profile, timeline) = await getProfileAndTimeline(name, after, agent, media) (profile, timeline) = await getProfileAndTimeline(name, after, agent, media)
cache(profile) cache(profile)
else: else:
var timelineFut = let timelineFut = fetchTimeline(name, after, agent, query)
if query.kind == QueryKind.media:
getMediaTimeline(name, after, agent, media)
else:
getSearch[Tweet](query, after, agent, media)
if cachedProfile.isNone: if cachedProfile.isNone:
profile = await getCachedProfile(name, agent) profile = await getCachedProfile(name, agent)
timeline = await timelineFut timeline = await timelineFut
@ -58,9 +64,8 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; rss: string): Fu
names = getNames(name) names = getNames(name)
if names.len != 1: if names.len != 1:
let let timeline = await fetchMultiTimeline(names, after, agent, query)
timeline = await fetchMultiTimeline(names, after, agent, query) let html = renderTweetSearch(timeline, prefs, getPath())
html = renderTweetSearch(timeline, prefs, getPath())
return renderMain(html, request, cfg, "Multi", rss=rss) return renderMain(html, request, cfg, "Multi", rss=rss)
let let
@ -84,12 +89,7 @@ proc createTimelineRouter*(cfg: Config) =
get "/@name/?@tab?": get "/@name/?@tab?":
cond '.' notin @"name" cond '.' notin @"name"
cond @"tab" in ["with_replies", "media", "search", ""] cond @"tab" in ["with_replies", "media", "search", ""]
let query = let query = request.getQuery(@"tab", @"name")
case @"tab"
of "with_replies": getReplyQuery(@"name")
of "media": getMediaQuery(@"name")
of "search": initQuery(params(request), name=(@"name"))
else: Query()
var rss = "/$1/$2/rss" % [@"name", @"tab"] var rss = "/$1/$2/rss" % [@"name", @"tab"]
if @"tab".len == 0: if @"tab".len == 0:
rss = "/$1/rss" % @"name" rss = "/$1/rss" % @"name"