nitter/src/routes/timeline.nim

156 lines
4.6 KiB
Nim
Raw Normal View History

2021-12-27 02:37:38 +01:00
# SPDX-License-Identifier: AGPL-3.0-only
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
proc fetchTimeline*(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
2020-06-09 18:19:20 +02:00
profile: Profile
2020-06-01 02:22:56 +02:00
profileId = await getProfileId(name)
2020-06-09 18:19:20 +02:00
fetched = false
2020-06-01 02:22:56 +02:00
2020-06-09 18:19:20 +02:00
if profileId.len == 0:
profile = await getCachedProfile(name)
profileId = if profile.suspended: "s"
else: profile.id
if profileId.len > 0:
await cacheProfileId(profile.username, profileId)
2020-06-09 18:19:20 +02:00
fetched = true
2020-06-01 02:22:56 +02:00
2020-06-09 18:19:20 +02:00
if profileId.len == 0 or profile.protected:
2020-06-01 02:22:56 +02:00
result[0] = profile
return
2020-06-09 18:19:20 +02:00
elif profileId == "s":
result[0] = Profile(username: name, suspended: true)
return
2019-09-06 02:42:35 +02:00
2020-06-01 02:22:56 +02:00
var rail: Future[PhotoRail]
if skipRail or profile.protected or query.kind == media:
2020-06-01 02:22:56 +02:00
rail = newFuture[PhotoRail]()
rail.complete(@[])
2019-09-06 02:42:35 +02:00
else:
2020-06-17 00:20:34 +02:00
rail = getCachedPhotoRail(name)
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
2020-06-09 18:19:20 +02:00
var found = false
2020-06-03 00:03:41 +02:00
for tweet in timeline.content.mitems:
2020-06-01 02:22:56 +02:00
if tweet.profile.id == profileId or
tweet.profile.username.cmpIgnoreCase(name) == 0:
profile = tweet.profile
2020-06-09 18:19:20 +02:00
found = true
2020-06-01 02:22:56 +02:00
break
if profile.username.len == 0:
profile = await getCachedProfile(name)
fetched = true
2020-06-09 18:19:20 +02:00
if fetched and not found:
2020-06-01 02:22:56 +02:00
await cache(profile)
return (profile, timeline, await rail)
2019-09-06 02:42:35 +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())
2020-06-09 16:45:21 +02:00
return renderMain(html, request, cfg, prefs, "Multi", rss=rss)
2019-09-06 02:42:35 +02:00
var (p, t, r) = await fetchTimeline(after, query)
2020-06-01 02:22:56 +02:00
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-09 16:45:21 +02:00
result = renderMain(pHtml, request, cfg, prefs, pageTitle(p), pageDesc(p),
2022-01-06 03:57:14 +01:00
rss=rss, images = @[p.getUserPic("_400x400")],
banner=p.banner)
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
template respUserId*() =
cond @"user_id".len > 0
2021-12-30 01:48:48 +01:00
let username = await getCachedProfileUsername(@"user_id")
if username.len > 0:
redirect("/" & username)
else:
resp Http404, showError("User not found", cfg)
2019-09-06 02:42:35 +02:00
proc createTimelineRouter*(cfg: Config) =
router timeline:
get "/i/user/@user_id":
respUserId()
get "/intent/user":
respUserId()
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.fromUser = names
2020-01-07 03:00:16 +01:00
2021-12-30 04:18:40 +01:00
# used for the infinite scroll feature
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:
var (_, timeline, _) = await fetchTimeline(after, query, skipRail=true)
2020-06-01 02:22:56 +02:00
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTimelineTweets(timeline, prefs, getPath())
2020-01-07 03:00:16 +01:00
2021-12-30 04:18:40 +01: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 03:00:16 +01:00
respTimeline(await showTimeline(request, query, cfg, prefs, rss, after))