nitter/src/routes/timeline.nim

159 lines
5 KiB
Nim
Raw Normal View History

2021-12-27 01:37:38 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, sequtils, uri, options, times
2020-01-07 02:00:16 +00:00
import jester, karax/vdom
2019-09-06 00:42:35 +00:00
import router_utils
2020-06-01 00:22:56 +00:00
import ".."/[types, redis_cache, formatters, query, api]
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
2020-06-01 00:22:56 +00:00
export redis_cache, formatters, query, api
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
2022-01-23 07:15:35 +00:00
template skipIf[T](cond: bool; default; body: Future[T]): Future[T] =
if cond:
let fut = newFuture[T]()
fut.complete(default)
fut
else:
body
proc fetchProfile*(after: string; query: Query; skipRail=false;
skipPinned=false): Future[Profile] {.async.} =
2022-01-23 07:15:35 +00:00
let
name = query.fromUser[0]
userId = await getUserId(name)
2019-09-06 00:42:35 +00:00
if userId.len == 0:
return Profile(user: User(username: name))
elif userId == "suspended":
return Profile(user: User(username: name, suspended: true))
2019-09-06 00:42:35 +00:00
# temporary fix to prevent errors from people browsing
# timelines during/immediately after deployment
var after = after
if query.kind in {posts, replies} and after.startsWith("scroll"):
after.setLen 0
2022-01-23 07:15:35 +00:00
let
rail =
skipIf(skipRail or query.kind == media, @[]):
getCachedPhotoRail(name)
2020-06-01 00:22:56 +00:00
user = getCachedUser(name)
2020-06-01 00:22:56 +00:00
result =
case query.kind
# of posts: await getTimeline(userId, after)
of replies: await getGraphUserTweets(userId, TimelineKind.replies, after)
of media: await getGraphUserTweets(userId, TimelineKind.media, after)
2023-07-12 01:37:44 +00:00
else: Profile(tweets: await getTweetSearch(query, after))
2020-06-01 00:22:56 +00:00
result.user = await user
result.photoRail = await rail
2020-06-09 16:19:20 +00:00
result.tweets.query = query
if result.user.protected or result.user.suspended:
return
2020-06-01 00:22:56 +00:00
if query.kind == posts:
if result.user.verified:
for chain in result.tweets.content:
if chain[0].user.id == result.user.id:
chain[0].user.verified = true
if not skipPinned and result.user.pinnedTweet > 0 and after.len == 0:
let tweet = await getCachedTweet(result.user.pinnedTweet)
if not tweet.isNil:
tweet.pinned = true
tweet.user = result.user
result.pinned = some tweet
2019-09-06 00:42:35 +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.} =
if query.fromUser.len != 1:
let
2023-07-12 01:37:44 +00:00
timeline = await getTweetSearch(query, after)
html = renderTweetSearch(timeline, prefs, getPath())
2020-06-09 14:45:21 +00:00
return renderMain(html, request, cfg, prefs, "Multi", rss=rss)
2019-09-06 00:42:35 +00:00
2022-01-30 15:58:20 +00:00
var profile = await fetchProfile(after, query, skipPinned=prefs.hidePins)
template u: untyped = profile.user
if u.suspended:
return showError(getSuspended(u.username), cfg)
2020-06-01 00:22:56 +00:00
if profile.user.id.len == 0: return
2020-04-14 21:56:31 +00:00
let pHtml = renderProfile(profile, prefs, getPath())
result = renderMain(pHtml, request, cfg, prefs, pageTitle(u), pageDesc(u),
rss=rss, images = @[u.getUserPic("_400x400")],
banner=u.banner)
2019-10-23 07:03:15 +00:00
2019-09-06 00:42:35 +00:00
template respTimeline*(timeline: typed) =
let t = timeline
if t.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t
2019-09-06 00:42:35 +00:00
template respUserId*() =
cond @"user_id".len > 0
let username = await getCachedUsername(@"user_id")
if username.len > 0:
redirect("/" & username)
else:
resp Http404, showError("User not found", cfg)
2019-09-06 00:42:35 +00:00
proc createTimelineRouter*(cfg: Config) =
router timeline:
get "/i/user/@user_id":
respUserId()
get "/intent/user":
respUserId()
2020-05-26 12:24:41 +00:00
get "/@name/?@tab?/?":
2019-09-06 00:42:35 +00:00
cond '.' notin @"name"
cond @"name" notin ["pic", "gif", "video", "search", "settings", "login", "intent", "i"]
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()
2020-06-01 00:22:56 +00:00
after = getCursor()
names = getNames(@"name")
var query = request.getQuery(@"tab", @"name")
if names.len != 1:
query.fromUser = names
2020-01-07 02:00:16 +00:00
2021-12-30 03:18:40 +00:00
# used for the infinite scroll feature
2020-01-07 02:00:16 +00:00
if @"scroll".len > 0:
if query.fromUser.len != 1:
var timeline = (await getGraphSearch(query, after)).tweets
2020-06-01 00:22:56 +00:00
if timeline.content.len == 0: resp Http404
timeline.beginning = true
resp $renderTweetSearch(timeline, prefs, getPath())
else:
var profile = await fetchProfile(after, query, skipRail=true)
if profile.tweets.content.len == 0: resp Http404
profile.tweets.beginning = true
resp $renderTimelineTweets(profile.tweets, prefs, getPath())
2020-01-07 02:00:16 +00:00
2021-12-30 03:18:40 +00: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 02:00:16 +00:00
respTimeline(await showTimeline(request, query, cfg, prefs, rss, after))