このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
Nitter-mod/src/routes/timeline.nim

99 行
3.4 KiB
Nim
Raw 通常表示 履歴

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