nitter/src/routes/timeline.nim

132 lines
4.2 KiB
Nim
Raw Normal View History

2020-06-01 02:22:56 +02:00
import asyncdispatch, strutils, sequtils, uri, options, times
2020-01-07 03:00:16 +01:00
import jester, karax/vdom
2019-09-06 02:42:35 +02:00
import router_utils
2020-06-01 02:22:56 +02:00
import ".."/[types, redis_cache, formatters, query, api]
2019-09-13 22:24:58 +02:00
import ../views/[general, profile, timeline, status, search]
2019-09-06 02:42:35 +02:00
2020-01-07 03:00:16 +01:00
export vdom
2019-09-06 02:42:35 +02:00
export uri, sequtils
export router_utils
2020-06-01 02:22:56 +02:00
export redis_cache, formatters, query, api
2019-09-06 02:42:35 +02:00
export profile, timeline, status
2020-01-07 02:23:20 +01: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 02:23:20 +01:00
2020-06-01 02:22:56 +02:00
proc fetchSingleTimeline*(after: string; query: Query; skipRail=false):
Future[(Profile, Timeline, PhotoRail)] {.async.} =
let name = query.fromUser[0]
2019-09-06 02:42:35 +02:00
2020-06-01 02:22:56 +02:00
var
profile = await getCachedProfile(name, fetch=false)
profileId = await getProfileId(name)
if profile.username.len == 0 and profileId.len == 0:
profile = await getCachedProfile(name)
profileId = profile.id
if profile.suspended or profileId.len == 0:
result[0] = profile
return
2019-09-06 02:42:35 +02:00
2020-06-01 02:22:56 +02:00
var rail: Future[PhotoRail]
if skipRail or query.kind == media or profile.suspended or profile.protected:
rail = newFuture[PhotoRail]()
rail.complete(@[])
2019-09-06 02:42:35 +02:00
else:
2020-06-01 02:22:56 +02:00
rail = getCachedPhotoRail(profileId)
2019-09-06 02:42:35 +02:00
2020-06-01 02:22:56 +02:00
var timeline =
case query.kind
of posts: await getTimeline(profileId, after)
of replies: await getTimeline(profileId, after, replies=true)
of media: await getMediaTimeline(profileId, after)
else: await getSearch[Tweet](query, after)
timeline.query = query
for tweet in timeline.content:
if tweet.profile.id == profileId or
tweet.profile.username.cmpIgnoreCase(name) == 0:
profile = tweet.profile
break
if profile.username.len == 0:
profile = await getCachedProfile(name)
else:
await cache(profile)
return (profile, timeline, await rail)
2019-09-06 02:42:35 +02:00
proc getMultiQuery*(q: Query; names: seq[string]): Query =
result = q
result.fromUser = names
2019-09-19 02:23:22 +02:00
if q.kind == posts and "replies" notin q.excludes:
result.excludes.add "replies"
2019-09-06 02:42:35 +02:00
2019-09-20 22:56:27 +02:00
proc get*(req: Request; key: string): string =
params(req).getOrDefault(key)
2019-09-20 22:56:27 +02:00
2020-01-07 03:00:16 +01:00
proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
rss, after: string): Future[string] {.async.} =
if query.fromUser.len != 1:
let
2020-06-01 02:22:56 +02:00
timeline = await getSearch[Tweet](query, after)
html = renderTweetSearch(timeline, prefs, getPath())
2019-12-04 05:58:18 +01:00
return renderMain(html, request, cfg, "Multi", rss=rss)
2019-09-06 02:42:35 +02:00
2020-06-01 02:22:56 +02:00
var (p, t, r) = await fetchSingleTimeline(after, query)
if p.suspended: return showError(getSuspended(p.username), cfg)
if p.id.len == 0: return
2020-04-14 23:56:31 +02:00
2019-10-23 09:03:15 +02:00
let pHtml = renderProfile(p, t, r, prefs, getPath())
2020-06-01 02:22:56 +02:00
result = renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p),
rss=rss, images = @[p.getUserpic("_200x200")])
2019-10-23 09:03:15 +02:00
2019-09-06 02:42:35 +02:00
template respTimeline*(timeline: typed) =
let t = timeline
if t.len == 0:
2019-10-21 07:59:22 +02:00
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t
2019-09-06 02:42:35 +02:00
proc createTimelineRouter*(cfg: Config) =
router timeline:
2020-05-26 14:24:41 +02:00
get "/@name/?@tab?/?":
2019-09-06 02:42:35 +02:00
cond '.' notin @"name"
2020-05-26 14:24:41 +02:00
cond @"name" notin ["pic", "gif", "video"]
2019-12-08 12:38:55 +01:00
cond @"tab" in ["with_replies", "media", "search", ""]
2020-01-07 03:00:16 +01:00
let
prefs = cookiePrefs()
2020-06-01 02:22:56 +02:00
after = getCursor()
names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query = query.getMultiQuery(names)
2020-01-07 03:00:16 +01:00
if @"scroll".len > 0:
if query.fromUser.len != 1:
2020-06-01 02:22:56 +02:00
var timeline = await getSearch[Tweet](query, after)
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTweetSearch(timeline, prefs, getPath())
else:
2020-06-01 02:22:56 +02:00
var (_, timeline, _) = await fetchSingleTimeline(after, query, skipRail=true)
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTimelineTweets(timeline, prefs, getPath())
2020-01-07 03:00:16 +01: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 03:00:16 +01:00
respTimeline(await showTimeline(request, query, cfg, prefs, rss, after))