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

149 行
4.5 KiB
Nim
Raw 通常表示 履歴

2021-12-27 10:37:38 +09:00
# SPDX-License-Identifier: AGPL-3.0-only
2020-06-01 09:22:56 +09:00
import asyncdispatch, strutils, sequtils, uri, options, times
2020-01-07 11:00:16 +09:00
import jester, karax/vdom
2019-09-06 09:42:35 +09:00
import router_utils
2020-06-01 09:22:56 +09:00
import ".."/[types, redis_cache, formatters, query, api]
2019-09-14 05:24:58 +09:00
import ../views/[general, profile, timeline, status, search]
2019-09-06 09:42:35 +09:00
2020-01-07 11:00:16 +09:00
export vdom
2019-09-06 09:42:35 +09:00
export uri, sequtils
export router_utils
2020-06-01 09:22:56 +09:00
export redis_cache, formatters, query, api
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(fromUser: @[name])
2020-01-07 10:23:20 +09:00
proc fetchTimeline*(after: string; query: Query; skipRail=false):
Future[(Profile, Timeline, PhotoRail)] {.async.} =
let name = query.fromUser[0]
2019-09-06 09:42:35 +09:00
2020-06-01 09:22:56 +09:00
var
2020-06-10 01:19:20 +09:00
profile: Profile
2020-06-01 09:22:56 +09:00
profileId = await getProfileId(name)
2020-06-10 01:19:20 +09:00
fetched = false
2020-06-01 09:22:56 +09:00
2020-06-10 01:19:20 +09:00
if profileId.len == 0:
profile = await getCachedProfile(name)
profileId = profile.id
2020-06-10 01:19:20 +09:00
fetched = true
2020-06-01 09:22:56 +09:00
if profile.protected or profile.suspended:
return (profile, Timeline(), @[])
elif profileId.len == 0:
return (Profile(username: name), Timeline(), @[])
2019-09-06 09:42:35 +09:00
2020-06-01 09:22:56 +09:00
var rail: Future[PhotoRail]
if skipRail or profile.protected or query.kind == media:
2020-06-01 09:22:56 +09:00
rail = newFuture[PhotoRail]()
rail.complete(@[])
2019-09-06 09:42:35 +09:00
else:
2020-06-17 07:20:34 +09:00
rail = getCachedPhotoRail(name)
2019-09-06 09:42:35 +09:00
2020-06-01 09:22:56 +09: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
2020-06-10 01:19:20 +09:00
var found = false
2020-06-03 07:03:41 +09:00
for tweet in timeline.content.mitems:
2020-06-01 09:22:56 +09:00
if tweet.profile.id == profileId or
tweet.profile.username.cmpIgnoreCase(name) == 0:
profile = tweet.profile
2020-06-10 01:19:20 +09:00
found = true
2020-06-01 09:22:56 +09:00
break
if profile.username.len == 0:
profile = await getCachedProfile(name)
fetched = true
2020-06-10 01:19:20 +09:00
if fetched and not found:
2020-06-01 09:22:56 +09:00
await cache(profile)
return (profile, timeline, await rail)
2019-09-06 09:42:35 +09:00
2020-01-07 11:00:16 +09: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 09:22:56 +09:00
timeline = await getSearch[Tweet](query, after)
html = renderTweetSearch(timeline, prefs, getPath())
2020-06-09 23:45:21 +09:00
return renderMain(html, request, cfg, prefs, "Multi", rss=rss)
2019-09-06 09:42:35 +09:00
var (p, t, r) = await fetchTimeline(after, query)
2020-06-01 09:22:56 +09:00
if p.suspended: return showError(getSuspended(p.username), cfg)
if p.id.len == 0: return
2020-04-15 06:56:31 +09:00
2019-10-23 16:03:15 +09:00
let pHtml = renderProfile(p, t, r, prefs, getPath())
2020-06-09 23:45:21 +09:00
result = renderMain(pHtml, request, cfg, prefs, pageTitle(p), pageDesc(p),
2022-01-06 11:57:14 +09:00
rss=rss, images = @[p.getUserPic("_400x400")],
banner=p.banner)
2019-10-23 16:03:15 +09:00
2019-09-06 09:42:35 +09:00
template respTimeline*(timeline: typed) =
let t = timeline
if t.len == 0:
2019-10-21 14:59:22 +09:00
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t
2019-09-06 09:42:35 +09:00
template respUserId*() =
cond @"user_id".len > 0
2021-12-30 09:48:48 +09:00
let username = await getCachedProfileUsername(@"user_id")
if username.len > 0:
redirect("/" & username)
else:
resp Http404, showError("User not found", cfg)
2019-09-06 09:42:35 +09:00
proc createTimelineRouter*(cfg: Config) =
router timeline:
get "/i/user/@user_id":
respUserId()
get "/intent/user":
respUserId()
2020-05-26 21:24:41 +09:00
get "/@name/?@tab?/?":
2019-09-06 09:42:35 +09:00
cond '.' notin @"name"
2020-05-26 21:24:41 +09:00
cond @"name" notin ["pic", "gif", "video"]
2019-12-08 20:38:55 +09:00
cond @"tab" in ["with_replies", "media", "search", ""]
2020-01-07 11:00:16 +09:00
let
prefs = cookiePrefs()
2020-06-01 09:22:56 +09:00
after = getCursor()
names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query.fromUser = names
2020-01-07 11:00:16 +09:00
2021-12-30 12:18:40 +09:00
# used for the infinite scroll feature
2020-01-07 11:00:16 +09:00
if @"scroll".len > 0:
if query.fromUser.len != 1:
2020-06-01 09:22:56 +09:00
var timeline = await getSearch[Tweet](query, after)
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTweetSearch(timeline, prefs, getPath())
else:
var (_, timeline, _) = await fetchTimeline(after, query, skipRail=true)
2020-06-01 09:22:56 +09:00
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTimelineTweets(timeline, prefs, getPath())
2020-01-07 11:00:16 +09:00
2021-12-30 12:18:40 +09:00
let rss =
if @"tab".len == 0:
"/$1/rss" % @"name"
elif @"tab" == "search":
"/$1/search/rss?$2" % [@"name", genQueryUrl(query)]
else:
"/$1/$2/rss" % [@"name", @"tab"]
2020-01-07 11:00:16 +09:00
respTimeline(await showTimeline(request, query, cfg, prefs, rss, after))