nitter/src/routes/timeline.nim

120 lines
3.9 KiB
Nim
Raw Normal View History

2019-10-26 13:34:30 +00:00
import asyncdispatch, strutils, sequtils, uri, options
2020-01-07 02:00:16 +00:00
import jester, karax/vdom
2019-09-06 00:42:35 +00:00
import router_utils
2019-09-20 23:08:30 +00:00
import ".."/[api, types, cache, formatters, agents, query]
2019-09-13 20:24:58 +00:00
import ../views/[general, profile, timeline, status, search]
2019-09-06 00:42:35 +00:00
2020-01-07 02:00:16 +00:00
export vdom
2019-09-06 00:42:35 +00:00
export uri, sequtils
export router_utils
2019-09-13 20:24:58 +00:00
export api, cache, formatters, query, agents
2019-09-06 00:42:35 +00:00
export profile, timeline, status
2020-01-07 01:23:20 +00:00
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(fromUser: @[name])
2020-01-07 01:23:20 +00:00
proc fetchTimeline*(after, agent: string; query: Query): Future[Timeline] =
2020-01-07 01:23:20 +00:00
case query.kind
of QueryKind.media: getMediaTimeline(query.fromUser[0], after, agent)
of posts: getTimeline(query.fromUser[0], after, agent)
2020-01-07 01:23:20 +00:00
else: getSearch[Tweet](query, after, agent)
proc fetchSingleTimeline*(after, agent: string; query: Query;
media=true): Future[(Profile, Timeline)] {.async.} =
let name = query.fromUser[0]
2019-09-06 00:42:35 +00:00
var timeline: Timeline
var profile: Profile
var cachedProfile = hasCachedProfile(name)
if cachedProfile.isSome:
profile = get(cachedProfile)
2020-01-07 01:23:20 +00:00
if query.kind == posts and cachedProfile.isNone:
(profile, timeline) = await getProfileAndTimeline(name, after, agent, media)
cache(profile)
2019-09-06 00:42:35 +00:00
else:
let timelineFut = fetchTimeline(after, agent, query)
2019-09-06 00:42:35 +00:00
if cachedProfile.isNone:
profile = await getCachedProfile(name, agent)
timeline = await timelineFut
if profile.username.len == 0: return
return (profile, timeline)
2019-09-06 00:42:35 +00:00
proc getMultiQuery*(q: Query; names: seq[string]): Query =
result = q
result.fromUser = names
2019-09-19 00:23:22 +00:00
if q.kind == posts and "replies" notin q.excludes:
result.excludes.add "replies"
2019-09-06 00:42:35 +00:00
2019-09-20 20:56:27 +00:00
proc get*(req: Request; key: string): string =
params(req).getOrDefault(key)
2019-09-20 20:56:27 +00:00
2020-01-07 02:00:16 +00:00
proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
rss, after: string): Future[string] {.async.} =
let agent = getAgent()
2019-09-06 00:42:35 +00:00
if query.fromUser.len != 1:
let
timeline = await getSearch[Tweet](query, after, agent)
html = renderTweetSearch(timeline, prefs, getPath())
2019-12-04 04:58:18 +00:00
return renderMain(html, request, cfg, "Multi", rss=rss)
2019-09-06 00:42:35 +00:00
2019-10-23 07:03:15 +00:00
let
rail = getPhotoRail(query.fromUser[0], agent, skip=(query.kind == media))
(p, t) = await fetchSingleTimeline(after, agent, query)
2019-10-23 07:03:15 +00:00
r = await rail
if p.username.len == 0: return
2020-04-14 21:56:31 +00:00
if p.suspended:
return showError(getSuspended(p.username), cfg)
2019-10-23 07:03:15 +00:00
let pHtml = renderProfile(p, t, r, prefs, getPath())
return renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p),
rss=rss, images = @[p.getUserpic("_200x200")])
2019-09-06 00:42:35 +00:00
template respTimeline*(timeline: typed) =
let t = timeline
if t.len == 0:
2019-10-21 05:59:22 +00:00
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t
2019-09-06 00:42:35 +00:00
proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime)
router timeline:
2020-05-26 12:24:41 +00:00
get "/@name/?@tab?/?":
2019-09-06 00:42:35 +00:00
cond '.' notin @"name"
2020-05-26 12:24:41 +00:00
cond @"name" notin ["pic", "gif", "video"]
2019-12-08 11:38:55 +00:00
cond @"tab" in ["with_replies", "media", "search", ""]
2020-01-07 02:00:16 +00:00
let
prefs = cookiePrefs()
after = @"max_position"
names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query = query.getMultiQuery(names)
2020-01-07 02:00:16 +00:00
if @"scroll".len > 0:
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())
2020-01-07 02:00:16 +00:00
var rss = "/$1/$2/rss" % [@"name", @"tab"]
if @"tab".len == 0:
rss = "/$1/rss" % @"name"
elif @"tab" == "search":
rss &= "?" & genQueryUrl(query)
2020-01-07 02:00:16 +00:00
respTimeline(await showTimeline(request, query, cfg, prefs, rss, after))