From d93a6691eff1afceda9296196aecb7d74f0bb0b4 Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 8 Dec 2019 12:38:55 +0100 Subject: [PATCH] Router refactoring --- src/api/timeline.nim | 17 +++++++++++------ src/routes/rss.nim | 1 + src/routes/status.nim | 31 ++++++++++++------------------- src/routes/timeline.nim | 28 ++++++++++------------------ src/views/embed.nim | 2 +- src/views/general.nim | 15 ++++++++++----- 6 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/api/timeline.nim b/src/api/timeline.nim index a709da5..1058637 100644 --- a/src/api/timeline.nim +++ b/src/api/timeline.nim @@ -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) diff --git a/src/routes/rss.nim b/src/routes/rss.nim index cbff54e..93c0d6e 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -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") diff --git a/src/routes/status.nim b/src/routes/status.nim index 708c2f4..eddd24d 100644 --- a/src/routes/status.nim +++ b/src/routes/status.nim @@ -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": diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index c7d8fc2..db49c36 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -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)) diff --git a/src/views/embed.nim b/src/views/embed.nim index 717ba7b..7db258f 100644 --- a/src/views/embed.nim +++ b/src/views/embed.nim @@ -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, "") diff --git a/src/views/general.nim b/src/views/general.nim index 1826cb4..58edace 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -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"))