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) if media: await getMedia(timeline, agent)
result.content = timeline.content 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 var url = base / username
if after.len > 0: if after.len > 0:
url = url ? {"max_position": after} url = url ? {"max_position": after}
@ -37,7 +38,8 @@ proc getProfileAndTimeline*(username, after, agent: string; media=true): Future[
if media: await getMedia(timeline, agent) if media: await getMedia(timeline, agent)
result = (profile, timeline) 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({ var params = toSeq({
"include_available_features": "1", "include_available_features": "1",
"include_entities": "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) 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({ var params = toSeq({
"include_available_features": "1", "include_available_features": "1",
"include_entities": "1", "include_entities": "1",
@ -63,8 +66,10 @@ proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timel
if after.len > 0: if after.len > 0:
params.add {"max_position": after} params.add {"max_position": after}
let headers = genHeaders(agent, base / username, xml=true) let
let json = await fetchJson(base / (timelineMediaUrl % username) ? params, headers) 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) result.minId = getLastId(result)

View file

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

View file

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

View file

@ -81,23 +81,15 @@ proc createTimelineRouter*(cfg: Config) =
setProfileCacheTime(cfg.profileCacheTime) setProfileCacheTime(cfg.profileCacheTime)
router timeline: router timeline:
get "/@name/?": get "/@name/?@tab?":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/rss" % @"name" cond @"tab" in ["with_replies", "media", "search", ""]
respTimeline(await showTimeline(request, Query(), cfg, rss)) var rss = "/$1/$2/rss" % [@"name", @"tab"]
let query =
get "/@name/with_replies": case @"tab"
cond '.' notin @"name" of "with_replies": getReplyQuery(@"name")
let rss = "/$1/with_replies/rss" % @"name" of "media": getMediaQuery(@"name")
respTimeline(await showTimeline(request, getReplyQuery(@"name"), cfg, rss)) of "search": initQuery(params(request), name=(@"name"))
else: Query()
get "/@name/media": if @"tab" == "search": rss &= "?" & genQueryUrl(query)
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)]
respTimeline(await showTimeline(request, query, cfg, rss)) 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 vidUrl = getVideoEmbed(cfg, tweet.id)
let prefs = Prefs(hlsPlayback: true) let prefs = Prefs(hlsPlayback: true)
let node = buildHtml(html(lang="en")): 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"): tdiv(class="embed-video"):
renderVideo(get(tweet.video), prefs, "") 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" icon "info-circled", title="About", href="/about"
iconReferer "cog", "/settings", path, title="Preferences" iconReferer "cog", "/settings", path, title="Preferences"
proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; `type`="article"; proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
video=""; images: seq[string] = @[]): VNode = images: seq[string] = @[]): VNode =
let ogType =
if video.len > 0: "video"
elif images.len > 0: "photo"
else: "article"
buildHtml(head): buildHtml(head):
link(rel="stylesheet", `type`="text/css", href="/css/style.css") link(rel="stylesheet", `type`="text/css", href="/css/style.css")
link(rel="stylesheet", `type`="text/css", href="/css/fontello.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 text cfg.title
meta(name="viewport", content="width=device-width, initial-scale=1.0") 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:title", content=titleText)
meta(property="og:description", content=stripHtml(desc)) meta(property="og:description", content=stripHtml(desc))
meta(property="og:site_name", content="Nitter") 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") meta(property="og:video:type", content="text/html")
proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc=""; 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 prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg)
let theme = toLowerAscii(prefs.theme).replace(" ", "_") let theme = toLowerAscii(prefs.theme).replace(" ", "_")
let node = buildHtml(html(lang="en")): 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: if theme.len > 0:
link(rel="stylesheet", `type`="text/css", href=(&"/css/themes/{theme}.css")) link(rel="stylesheet", `type`="text/css", href=(&"/css/themes/{theme}.css"))