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)
|
2020-05-02 19:22:43 +02:00
|
|
|
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.} =
|
2020-05-02 19:22:43 +02:00
|
|
|
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
|
2021-01-07 22:04:01 +01:00
|
|
|
|
|
|
|
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]
|
2021-12-26 05:02:57 +01:00
|
|
|
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:
|
2021-01-07 22:04:01 +01:00
|
|
|
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
|
|
|
|
2019-09-20 22:56:27 +02:00
|
|
|
proc get*(req: Request; key: string): string =
|
2019-12-06 08:21:37 +01:00
|
|
|
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.} =
|
2020-05-02 19:22:43 +02:00
|
|
|
if query.fromUser.len != 1:
|
2020-01-19 08:34:32 +01:00
|
|
|
let
|
2020-06-01 02:22:56 +02:00
|
|
|
timeline = await getSearch[Tweet](query, after)
|
2020-01-19 08:34:32 +01:00
|
|
|
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
|
|
|
|
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-09 16:45:21 +02:00
|
|
|
result = renderMain(pHtml, request, cfg, prefs, pageTitle(p), pageDesc(p),
|
2021-01-08 00:50:10 +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) =
|
2020-05-02 19:23:47 +02:00
|
|
|
let t = timeline
|
|
|
|
if t.len == 0:
|
2019-10-21 07:59:22 +02:00
|
|
|
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
|
2020-05-02 19:23:47 +02:00
|
|
|
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()
|
2020-05-02 19:22:43 +02:00
|
|
|
names = getNames(@"name")
|
|
|
|
|
|
|
|
var query = request.getQuery(@"tab", @"name")
|
|
|
|
if names.len != 1:
|
2020-06-17 14:12:38 +02:00
|
|
|
query.fromUser = names
|
2020-01-07 03:00:16 +01:00
|
|
|
|
|
|
|
if @"scroll".len > 0:
|
2020-05-02 19:22:43 +02:00
|
|
|
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
|
2020-05-02 19:22:43 +02:00
|
|
|
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
|
2020-05-02 19:22:43 +02:00
|
|
|
timeline.beginning = true
|
|
|
|
resp $renderTimelineTweets(timeline, prefs, getPath())
|
2020-01-07 03:00:16 +01:00
|
|
|
|
2019-12-10 10:11:55 +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))
|