Fix multi-timeline infinite scroll, minor refactor

This commit is contained in:
Zed 2020-05-02 19:22:43 +02:00
parent 4c35d0b723
commit 9197c686d3
3 changed files with 33 additions and 26 deletions

View file

@ -18,9 +18,9 @@ proc createListRouter*(cfg: Config) =
get "/@name/lists/@list": get "/@name/lists/@list":
cond '.' notin @"name" cond '.' notin @"name"
let list = await getListTimeline(@"name", @"list", @"max_position", getAgent()) let list = await getListTimeline(@"name", @"list", @"max_position", getAgent())
respList list, renderTimelineTweets(list, cookiePrefs(), request.path) respList(list, renderTimelineTweets(list, cookiePrefs(), request.path))
get "/@name/lists/@list/members": get "/@name/lists/@list/members":
cond '.' notin @"name" cond '.' notin @"name"
let list = await getListMembers(@"name", @"list", @"max_position", getAgent()) let list = await getListMembers(@"name", @"list", @"max_position", getAgent())
respList list, renderTimelineUsers(list, cookiePrefs(), request.path) respList(list, renderTimelineUsers(list, cookiePrefs(), request.path))

View file

@ -18,7 +18,7 @@ proc showRss*(req: Request; hostname: string; query: Query): Future[(string, str
if names.len == 1: if names.len == 1:
(profile, timeline) = (profile, timeline) =
await fetchSingleTimeline(names[0], after, getAgent(), query, media=false) await fetchSingleTimeline(after, getAgent(), query, media=false)
else: else:
let multiQuery = query.getMultiQuery(names) let multiQuery = query.getMultiQuery(names)
timeline = await getSearch[Tweet](multiQuery, after, getAgent(), media=false) timeline = await getSearch[Tweet](multiQuery, after, getAgent(), media=false)
@ -60,18 +60,19 @@ proc createRssRouter*(cfg: Config) =
get "/@name/rss": get "/@name/rss":
cond '.' notin @"name" cond '.' notin @"name"
let (rss, minId) = await showRss(request, cfg.hostname, Query()) let (rss, minId) = await showRss(request, cfg.hostname, Query(fromUser: @[@"name"]))
respRss(rss, minId) respRss(rss, minId)
get "/@name/@tab/rss": get "/@name/@tab/rss":
cond '.' notin @"name" cond '.' notin @"name"
cond @"tab" in ["with_replies", "media", "search"] cond @"tab" in ["with_replies", "media", "search"]
let name = @"name"
let query = let query =
case @"tab" case @"tab"
of "with_replies": getReplyQuery(@"name") of "with_replies": getReplyQuery(name)
of "media": getMediaQuery(@"name") of "media": getMediaQuery(name)
of "search": initQuery(params(request), name=(@"name")) of "search": initQuery(params(request), name=name)
else: Query() else: Query(fromUser: @[name])
let (rss, minId) = await showRss(request, cfg.hostname, query) let (rss, minId) = await showRss(request, cfg.hostname, query)
respRss(rss, minId) respRss(rss, minId)

View file

@ -16,16 +16,17 @@ proc getQuery*(request: Request; tab, name: string): Query =
of "with_replies": getReplyQuery(name) of "with_replies": getReplyQuery(name)
of "media": getMediaQuery(name) of "media": getMediaQuery(name)
of "search": initQuery(params(request), name=name) of "search": initQuery(params(request), name=name)
else: Query() else: Query(fromUser: @[name])
proc fetchTimeline*(name, after, agent: string; query: Query): Future[Timeline] = proc fetchTimeline*(after, agent: string; query: Query): Future[Timeline] =
case query.kind case query.kind
of QueryKind.media: getMediaTimeline(name, after, agent) of QueryKind.media: getMediaTimeline(query.fromUser[0], after, agent)
of posts: getTimeline(name, after, agent) of posts: getTimeline(query.fromUser[0], after, agent)
else: getSearch[Tweet](query, after, agent) else: getSearch[Tweet](query, after, agent)
proc fetchSingleTimeline*(name, after, agent: string; query: Query; proc fetchSingleTimeline*(after, agent: string; query: Query;
media=true): Future[(Profile, Timeline)] {.async.} = media=true): Future[(Profile, Timeline)] {.async.} =
let name = query.fromUser[0]
var timeline: Timeline var timeline: Timeline
var profile: Profile var profile: Profile
var cachedProfile = hasCachedProfile(name) var cachedProfile = hasCachedProfile(name)
@ -37,7 +38,7 @@ proc fetchSingleTimeline*(name, after, agent: string; query: Query;
(profile, timeline) = await getProfileAndTimeline(name, after, agent, media) (profile, timeline) = await getProfileAndTimeline(name, after, agent, media)
cache(profile) cache(profile)
else: else:
let timelineFut = fetchTimeline(name, after, agent, query) let timelineFut = fetchTimeline(after, agent, query)
if cachedProfile.isNone: if cachedProfile.isNone:
profile = await getCachedProfile(name, agent) profile = await getCachedProfile(name, agent)
timeline = await timelineFut timeline = await timelineFut
@ -57,18 +58,16 @@ proc get*(req: Request; key: string): string =
proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs; proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
rss, after: string): Future[string] {.async.} = rss, after: string): Future[string] {.async.} =
let agent = getAgent() let agent = getAgent()
let names = getNames(request.get("name"))
if names.len != 1: if query.fromUser.len != 1:
let let
multiQuery = query.getMultiQuery(names) timeline = await getSearch[Tweet](query, after, agent)
timeline = await getSearch[Tweet](multiQuery, after, agent)
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
rail = getPhotoRail(names[0], agent, skip=(query.kind == media)) rail = getPhotoRail(query.fromUser[0], agent, skip=(query.kind == media))
(p, t) = await fetchSingleTimeline(names[0], after, agent, query) (p, t) = await fetchSingleTimeline(after, agent, query)
r = await rail r = await rail
if p.username.len == 0: return if p.username.len == 0: return
if p.suspended: if p.suspended:
@ -83,10 +82,6 @@ template respTimeline*(timeline: typed) =
resp Http404, showError("User \"" & @"name" & "\" not found", cfg) resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp timeline resp timeline
template respScroll*(timeline: typed) =
timeline.beginning = true # don't render "load newest"
resp $renderTimelineTweets(timeline, prefs, getPath())
proc createTimelineRouter*(cfg: Config) = proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime) setProfileCacheTime(cfg.profileCacheTime)
@ -97,10 +92,21 @@ proc createTimelineRouter*(cfg: Config) =
let let
prefs = cookiePrefs() prefs = cookiePrefs()
after = @"max_position" after = @"max_position"
query = request.getQuery(@"tab", @"name") names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query = query.getMultiQuery(names)
if @"scroll".len > 0: if @"scroll".len > 0:
respScroll(await fetchTimeline(@"name", after, getAgent(), query)) if query.fromUser.len != 1:
let timeline = await getSearch[Tweet](query, after, getAgent())
timeline.beginning = true
resp $renderTweetSearch(timeline, prefs, getPath())
else:
let timeline = await fetchTimeline(after, getAgent(), query)
timeline.beginning = true
resp $renderTimelineTweets(timeline, prefs, getPath())
var rss = "/$1/$2/rss" % [@"name", @"tab"] var rss = "/$1/$2/rss" % [@"name", @"tab"]
if @"tab".len == 0: if @"tab".len == 0: