Router refactoring

This commit is contained in:
Zed 2019-12-08 12:38:55 +01:00
parent f3aa28709a
commit d93a6691ef
6 changed files with 45 additions and 49 deletions

View file

@ -23,7 +23,8 @@ proc finishTimeline*(json: JsonNode; query: Query; after, agent: string;
if media: await getMedia(timeline, agent)
result.content = timeline.content
proc getProfileAndTimeline*(username, after, agent: string; media=true): Future[(Profile, Timeline)] {.async.} =
proc getProfileAndTimeline*(username, after, agent: string;
media=true): Future[(Profile, Timeline)] {.async.} =
var url = base / username
if after.len > 0:
url = url ? {"max_position": after}
@ -37,7 +38,8 @@ proc getProfileAndTimeline*(username, after, agent: string; media=true): Future[
if media: await getMedia(timeline, agent)
result = (profile, timeline)
proc getTimeline*(username, after, agent: string; media=true): Future[Timeline] {.async.} =
proc getTimeline*(username, after, agent: string;
media=true): Future[Timeline] {.async.} =
var params = toSeq({
"include_available_features": "1",
"include_entities": "1",
@ -53,7 +55,8 @@ proc getTimeline*(username, after, agent: string; media=true): Future[Timeline]
result = await finishTimeline(json, Query(), after, agent, media)
proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timeline] {.async.} =
proc getMediaTimeline*(username, after, agent: string;
media=true): Future[Timeline] {.async.} =
var params = toSeq({
"include_available_features": "1",
"include_entities": "1",
@ -63,8 +66,10 @@ proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timel
if after.len > 0:
params.add {"max_position": after}
let headers = genHeaders(agent, base / username, xml=true)
let json = await fetchJson(base / (timelineMediaUrl % username) ? params, headers)
let
headers = genHeaders(agent, base / username, xml=true)
json = await fetchJson(base / (timelineMediaUrl % username) ? params, headers)
query = Query(kind: QueryKind.media)
result = await finishTimeline(json, Query(kind: QueryKind.media), after, agent, media)
result = await finishTimeline(json, query, after, agent, media)
result.minId = getLastId(result)

View file

@ -59,6 +59,7 @@ proc createRssRouter*(cfg: Config) =
get "/@name/@tab/rss":
cond '.' notin @"name"
cond @"tab" in ["with_replies", "media", "search"]
let query =
case @"tab"
of "with_replies": getReplyQuery(@"name")

View file

@ -24,32 +24,25 @@ proc createStatusRouter*(cfg: Config) =
error = conversation.tweet.tombstone
resp Http404, showError(error, cfg)
let
var
title = pageTitle(conversation.tweet.profile)
desc = conversation.tweet.text
html = renderConversation(conversation, prefs, getPath() & "#m")
images = conversation.tweet.photos
video = ""
if conversation.tweet.video.isSome():
let thumb = get(conversation.tweet.video).thumb
let vidUrl = getVideoEmbed(cfg, conversation.tweet.id)
resp renderMain(html, request, cfg, title, desc, images = @[thumb],
`type`="video", video=vidUrl)
images = @[get(conversation.tweet.video).thumb]
video = getVideoEmbed(cfg, conversation.tweet.id)
elif conversation.tweet.gif.isSome():
let thumb = get(conversation.tweet.gif).thumb
let vidUrl = getGifUrl(get(conversation.tweet.gif).url)
resp renderMain(html, request, cfg, title, desc, images = @[thumb],
`type`="video", video=vidUrl)
else:
resp renderMain(html, request, cfg, title, desc,
images=conversation.tweet.photos, `type`="photo")
images = @[get(conversation.tweet.gif).thumb]
video = getGifUrl(get(conversation.tweet.gif).url)
get "/@name/status/@id/photo/@i":
redirect("/$1/status/$2" % [@"name", @"id"])
let html = renderConversation(conversation, prefs, getPath() & "#m")
resp renderMain(html, request, cfg, title, desc, images=images, video=video)
get "/@name/status/@id/video/@i":
redirect("/$1/status/$2" % [@"name", @"id"])
get "/@name/statuses/@id":
get "/@name/@s/@id/@m/?@i?":
cond @"s" in ["status", "statuses"]
cond @"m" in ["video", "photo"]
redirect("/$1/status/$2" % [@"name", @"id"])
get "/i/web/status/@id":

View file

@ -81,23 +81,15 @@ proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime)
router timeline:
get "/@name/?":
get "/@name/?@tab?":
cond '.' notin @"name"
let rss = "/$1/rss" % @"name"
respTimeline(await showTimeline(request, Query(), cfg, rss))
get "/@name/with_replies":
cond '.' notin @"name"
let rss = "/$1/with_replies/rss" % @"name"
respTimeline(await showTimeline(request, getReplyQuery(@"name"), cfg, rss))
get "/@name/media":
cond '.' notin @"name"
let rss = "/$1/media/rss" % @"name"
respTimeline(await showTimeline(request, getMediaQuery(@"name"), cfg, rss))
get "/@name/search":
cond '.' notin @"name"
let query = initQuery(params(request), name=(@"name"))
let rss = "/$1/search/rss?$2" % [@"name", genQueryUrl(query)]
cond @"tab" in ["with_replies", "media", "search", ""]
var rss = "/$1/$2/rss" % [@"name", @"tab"]
let query =
case @"tab"
of "with_replies": getReplyQuery(@"name")
of "media": getMediaQuery(@"name")
of "search": initQuery(params(request), name=(@"name"))
else: Query()
if @"tab" == "search": rss &= "?" & genQueryUrl(query)
respTimeline(await showTimeline(request, query, cfg, rss))

View file

@ -11,7 +11,7 @@ proc renderVideoEmbed*(cfg: Config; tweet: Tweet): string =
let vidUrl = getVideoEmbed(cfg, tweet.id)
let prefs = Prefs(hlsPlayback: true)
let node = buildHtml(html(lang="en")):
renderHead(prefs, cfg, `type`="video", images = @[thumb], video=vidUrl)
renderHead(prefs, cfg, video=vidUrl, images=(@[thumb]))
tdiv(class="embed-video"):
renderVideo(get(tweet.video), prefs, "")

View file

@ -28,8 +28,13 @@ proc renderNavbar*(title, rss: string; req: Request): VNode =
icon "info-circled", title="About", href="/about"
iconReferer "cog", "/settings", path, title="Preferences"
proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; `type`="article";
video=""; images: seq[string] = @[]): VNode =
proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
images: seq[string] = @[]): VNode =
let ogType =
if video.len > 0: "video"
elif images.len > 0: "photo"
else: "article"
buildHtml(head):
link(rel="stylesheet", `type`="text/css", href="/css/style.css")
link(rel="stylesheet", `type`="text/css", href="/css/fontello.css")
@ -50,7 +55,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; `type`="artic
text cfg.title
meta(name="viewport", content="width=device-width, initial-scale=1.0")
meta(property="og:type", content=`type`)
meta(property="og:type", content=ogType)
meta(property="og:title", content=titleText)
meta(property="og:description", content=stripHtml(desc))
meta(property="og:site_name", content="Nitter")
@ -64,12 +69,12 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; `type`="artic
meta(property="og:video:type", content="text/html")
proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc="";
rss=""; `type`="article"; video=""; images: seq[string] = @[]): string =
rss=""; video=""; images: seq[string] = @[]): string =
let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg)
let theme = toLowerAscii(prefs.theme).replace(" ", "_")
let node = buildHtml(html(lang="en")):
renderHead(prefs, cfg, titleText, desc, `type`, video, images):
renderHead(prefs, cfg, titleText, desc, video, images):
if theme.len > 0:
link(rel="stylesheet", `type`="text/css", href=(&"/css/themes/{theme}.css"))