2019-09-06 00:42:35 +00:00
|
|
|
import asyncdispatch, strutils, sequtils, uri
|
|
|
|
|
|
|
|
import jester
|
|
|
|
|
|
|
|
import router_utils
|
2019-09-13 20:24:58 +00:00
|
|
|
import ".."/[api, prefs, types, utils, cache, formatters, agents, query]
|
|
|
|
import ../views/[general, profile, timeline, status, search]
|
2019-09-06 00:42:35 +00:00
|
|
|
|
2019-09-15 07:57:45 +00:00
|
|
|
include "../views/rss.nimf"
|
|
|
|
|
2019-09-06 00:42:35 +00:00
|
|
|
export uri, sequtils
|
|
|
|
export router_utils
|
2019-09-13 20:24:58 +00:00
|
|
|
export api, cache, formatters, query, agents
|
2019-09-06 00:42:35 +00:00
|
|
|
export profile, timeline, status
|
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
type ProfileTimeline = (Profile, Timeline, seq[GalleryPhoto])
|
|
|
|
|
|
|
|
proc fetchSingleTimeline*(name, after, agent: string;
|
2019-09-19 00:23:22 +00:00
|
|
|
query: Query): Future[ProfileTimeline] {.async.} =
|
2019-09-06 00:42:35 +00:00
|
|
|
let railFut = getPhotoRail(name, agent)
|
|
|
|
|
|
|
|
var timeline: Timeline
|
|
|
|
var profile: Profile
|
|
|
|
var cachedProfile = hasCachedProfile(name)
|
|
|
|
|
|
|
|
if cachedProfile.isSome:
|
|
|
|
profile = get(cachedProfile)
|
|
|
|
|
2019-09-19 00:23:22 +00:00
|
|
|
if query.kind == posts:
|
2019-09-06 00:42:35 +00:00
|
|
|
if cachedProfile.isSome:
|
|
|
|
timeline = await getTimeline(name, after, agent)
|
|
|
|
else:
|
|
|
|
(profile, timeline) = await getProfileAndTimeline(name, agent, after)
|
|
|
|
cache(profile)
|
|
|
|
else:
|
2019-09-19 00:23:22 +00:00
|
|
|
var timelineFut = getSearch[Tweet](query, after, agent)
|
2019-09-06 00:42:35 +00:00
|
|
|
if cachedProfile.isNone:
|
|
|
|
profile = await getCachedProfile(name, agent)
|
|
|
|
timeline = await timelineFut
|
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
if profile.username.len == 0: return
|
|
|
|
return (profile, timeline, await railFut)
|
2019-09-06 00:42:35 +00:00
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
proc fetchMultiTimeline*(names: seq[string]; after, agent: string;
|
2019-09-19 00:23:22 +00:00
|
|
|
query: Query): Future[Timeline] {.async.} =
|
2019-09-06 00:42:35 +00:00
|
|
|
var q = query
|
2019-09-19 00:23:22 +00:00
|
|
|
q.fromUser = names
|
|
|
|
if q.kind == posts and "replies" notin q.excludes:
|
|
|
|
q.excludes.add "replies"
|
|
|
|
return await getSearch[Tweet](q, after, agent)
|
2019-09-06 00:42:35 +00:00
|
|
|
|
2019-09-19 00:23:22 +00:00
|
|
|
proc showTimeline*(name, after: string; query: Query;
|
2019-09-15 10:10:43 +00:00
|
|
|
prefs: Prefs; path, title, rss: string): Future[string] {.async.} =
|
2019-09-06 00:42:35 +00:00
|
|
|
let agent = getAgent()
|
|
|
|
let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
|
|
|
|
|
|
|
|
if names.len == 1:
|
2019-09-15 10:10:43 +00:00
|
|
|
let (p, t, r) = await fetchSingleTimeline(names[0], after, agent, query)
|
|
|
|
if p.username.len == 0: return
|
|
|
|
let pHtml = renderProfile(p, t, r, prefs, path)
|
|
|
|
return renderMain(pHtml, prefs, title, pageTitle(p), pageDesc(p), path, rss=rss)
|
2019-09-06 00:42:35 +00:00
|
|
|
else:
|
2019-09-15 10:10:43 +00:00
|
|
|
let
|
|
|
|
timeline = await fetchMultiTimeline(names, after, agent, query)
|
2019-09-18 23:01:47 +00:00
|
|
|
html = renderTweetSearch(timeline, prefs, path)
|
2019-09-15 10:10:43 +00:00
|
|
|
return renderMain(html, prefs, title, "Multi")
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
template respTimeline*(timeline: typed) =
|
|
|
|
if timeline.len == 0:
|
|
|
|
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)
|
|
|
|
resp timeline
|
|
|
|
|
|
|
|
proc createTimelineRouter*(cfg: Config) =
|
|
|
|
setProfileCacheTime(cfg.profileCacheTime)
|
|
|
|
|
|
|
|
router timeline:
|
|
|
|
get "/@name/?":
|
|
|
|
cond '.' notin @"name"
|
2019-09-15 10:10:43 +00:00
|
|
|
let rss = "/$1/rss" % @"name"
|
2019-09-19 00:23:22 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", Query(), cookiePrefs(),
|
2019-09-15 10:10:43 +00:00
|
|
|
getPath(), cfg.title, rss))
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
get "/@name/search":
|
|
|
|
cond '.' notin @"name"
|
2019-09-19 00:23:22 +00:00
|
|
|
let query = initQuery(params(request), name=(@"name"))
|
2019-09-18 23:01:47 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", query, cookiePrefs(),
|
|
|
|
getPath(), cfg.title, ""))
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
get "/@name/replies":
|
|
|
|
cond '.' notin @"name"
|
2019-09-15 10:10:43 +00:00
|
|
|
let rss = "/$1/replies/rss" % @"name"
|
2019-09-19 00:23:22 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", getReplyQuery(@"name"),
|
2019-09-15 10:10:43 +00:00
|
|
|
cookiePrefs(), getPath(), cfg.title, rss))
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
get "/@name/media":
|
|
|
|
cond '.' notin @"name"
|
2019-09-15 10:10:43 +00:00
|
|
|
let rss = "/$1/media/rss" % @"name"
|
2019-09-19 00:23:22 +00:00
|
|
|
respTimeline(await showTimeline(@"name", @"after", getMediaQuery(@"name"),
|
2019-09-15 10:10:43 +00:00
|
|
|
cookiePrefs(), getPath(), cfg.title, rss))
|
2019-09-15 07:57:45 +00:00
|
|
|
|
2019-09-06 00:42:35 +00:00
|
|
|
get "/@name/status/@id":
|
|
|
|
cond '.' notin @"name"
|
|
|
|
let prefs = cookiePrefs()
|
|
|
|
|
|
|
|
let conversation = await getTweet(@"name", @"id", getAgent())
|
|
|
|
if conversation == nil or conversation.tweet.id.len == 0:
|
2019-09-09 04:38:25 +00:00
|
|
|
if conversation != nil and conversation.tweet.tombstone.len > 0:
|
2019-09-08 12:34:26 +00:00
|
|
|
resp Http404, showError(conversation.tweet.tombstone, cfg.title)
|
|
|
|
else:
|
|
|
|
resp Http404, showError("Tweet not found", cfg.title)
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
let path = getPath()
|
|
|
|
let title = pageTitle(conversation.tweet.profile)
|
|
|
|
let desc = conversation.tweet.text
|
|
|
|
let html = renderConversation(conversation, prefs, path)
|
|
|
|
|
|
|
|
if conversation.tweet.video.isSome():
|
|
|
|
let thumb = get(conversation.tweet.video).thumb
|
|
|
|
let vidUrl = getVideoEmbed(conversation.tweet.id)
|
|
|
|
resp renderMain(html, prefs, cfg.title, title, desc, path, images = @[thumb],
|
|
|
|
`type`="video", video=vidUrl)
|
|
|
|
elif conversation.tweet.gif.isSome():
|
|
|
|
let thumb = get(conversation.tweet.gif).thumb
|
|
|
|
let vidUrl = getVideoEmbed(conversation.tweet.id)
|
|
|
|
resp renderMain(html, prefs, cfg.title, title, desc, path, images = @[thumb],
|
|
|
|
`type`="video", video=vidUrl)
|
|
|
|
else:
|
2019-09-15 12:03:47 +00:00
|
|
|
resp renderMain(html, prefs, cfg.title, title, desc, path,
|
|
|
|
images=conversation.tweet.photos, `type`="photo")
|
2019-09-06 00:42:35 +00:00
|
|
|
|
|
|
|
get "/i/web/status/@id":
|
|
|
|
redirect("/i/status/" & @"id")
|