Simplify queries

This commit is contained in:
Zed 2019-09-20 22:56:27 +02:00
parent cd16ad8086
commit d1fbcef64d
9 changed files with 61 additions and 103 deletions

View File

@ -83,20 +83,8 @@ proc genQueryParam*(query: Query): string =
if query.text.len > 0: if query.text.len > 0:
result &= " " & query.text result &= " " & query.text
proc genQueryUrl*(query: Query; onlyParam=false): string = proc genQueryUrl*(query: Query): string =
if query.fromUser.len > 0: if query.kind notin {custom, users}: return
result = "/" & query.fromUser.join(",")
if query.fromUser.len > 1 and query.kind == posts:
return result & "?"
if query.kind notin {custom, users}:
return result & &"/{query.kind}?"
if onlyParam:
result = ""
else:
result &= &"/search?"
var params = @[&"kind={query.kind}"] var params = @[&"kind={query.kind}"]
if query.text.len > 0: if query.text.len > 0:

View File

@ -14,9 +14,8 @@ proc createPrefRouter*(cfg: Config) =
setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps) setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps)
get "/settings": get "/settings":
let prefs = cookiePrefs() let html = renderPreferences(cookiePrefs(), refPath())
let path = refPath() resp renderMain(html, request, cfg.title, "Preferences")
resp renderMain(renderPreferences(prefs, path), prefs, cfg.title, "Preferences", path)
post "/saveprefs": post "/saveprefs":
var prefs = cookiePrefs() var prefs = cookiePrefs()

View File

@ -9,4 +9,3 @@ template getPath*(): untyped {.dirty.} =
template refPath*(): untyped {.dirty.} = template refPath*(): untyped {.dirty.} =
if @"referer".len > 0: @"referer" else: "/" if @"referer".len > 0: @"referer" else: "/"

View File

@ -22,9 +22,9 @@ proc createSearchRouter*(cfg: Config) =
if "," in @"text": if "," in @"text":
redirect("/" & @"text") redirect("/" & @"text")
let users = await getSearch[Profile](query, @"after", getAgent()) let users = await getSearch[Profile](query, @"after", getAgent())
resp renderMain(renderUserSearch(users, prefs), prefs, cfg.title, path=getPath()) resp renderMain(renderUserSearch(users, prefs), request, cfg.title)
of custom: of custom:
let tweets = await getSearch[Tweet](query, @"after", getAgent()) let tweets = await getSearch[Tweet](query, @"after", getAgent())
resp renderMain(renderTweetSearch(tweets, prefs, getPath()), prefs, cfg.title, path=getPath()) resp renderMain(renderTweetSearch(tweets, prefs, getPath()), request, cfg.title)
else: else:
resp Http404, showError("Invalid search.", cfg.title) resp Http404, showError("Invalid search.", cfg.title)

View File

@ -24,23 +24,23 @@ proc createStatusRouter*(cfg: Config) =
else: else:
resp Http404, showError("Tweet not found", cfg.title) resp Http404, showError("Tweet not found", cfg.title)
let path = getPath() let
let title = pageTitle(conversation.tweet.profile) title = pageTitle(conversation.tweet.profile)
let desc = conversation.tweet.text desc = conversation.tweet.text
let html = renderConversation(conversation, prefs, path) html = renderConversation(conversation, prefs, getPath())
if conversation.tweet.video.isSome(): if conversation.tweet.video.isSome():
let thumb = get(conversation.tweet.video).thumb let thumb = get(conversation.tweet.video).thumb
let vidUrl = getVideoEmbed(conversation.tweet.id) let vidUrl = getVideoEmbed(conversation.tweet.id)
resp renderMain(html, prefs, cfg.title, title, desc, path, images = @[thumb], resp renderMain(html, request, cfg.title, title, desc, images = @[thumb],
`type`="video", video=vidUrl) `type`="video", video=vidUrl)
elif conversation.tweet.gif.isSome(): elif conversation.tweet.gif.isSome():
let thumb = get(conversation.tweet.gif).thumb let thumb = get(conversation.tweet.gif).thumb
let vidUrl = getVideoEmbed(conversation.tweet.id) let vidUrl = getVideoEmbed(conversation.tweet.id)
resp renderMain(html, prefs, cfg.title, title, desc, path, images = @[thumb], resp renderMain(html, request, cfg.title, title, desc, images = @[thumb],
`type`="video", video=vidUrl) `type`="video", video=vidUrl)
else: else:
resp renderMain(html, prefs, cfg.title, title, desc, path, resp renderMain(html, request, cfg.title, title, desc,
images=conversation.tweet.photos, `type`="photo") images=conversation.tweet.photos, `type`="photo")
get "/@name/status/@id/photo/1": get "/@name/status/@id/photo/1":

View File

@ -47,21 +47,28 @@ proc fetchMultiTimeline*(names: seq[string]; after, agent: string;
q.excludes.add "replies" q.excludes.add "replies"
return await getSearch[Tweet](q, after, agent) return await getSearch[Tweet](q, after, agent)
proc showTimeline*(name, after: string; query: Query; proc get*(req: Request; key: string): string =
prefs: Prefs; path, title, rss: string): Future[string] {.async.} = if key in params(req): params(req)[key]
let agent = getAgent() else: ""
let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
proc showTimeline*(request: Request; query: Query; title, rss: string): Future[string] {.async.} =
let
agent = getAgent()
prefs = cookiePrefs()
name = request.get("name")
after = request.get("after")
names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
if names.len == 1: if names.len == 1:
let (p, t, r) = await fetchSingleTimeline(names[0], after, agent, query) let (p, t, r) = await fetchSingleTimeline(names[0], after, agent, query)
if p.username.len == 0: return if p.username.len == 0: return
let pHtml = renderProfile(p, t, r, prefs, path) let pHtml = renderProfile(p, t, r, prefs, getPath())
return renderMain(pHtml, prefs, title, pageTitle(p), pageDesc(p), path, rss=rss) return renderMain(pHtml, request, title, pageTitle(p), pageDesc(p), rss=rss)
else: else:
let let
timeline = await fetchMultiTimeline(names, after, agent, query) timeline = await fetchMultiTimeline(names, after, agent, query)
html = renderTweetSearch(timeline, prefs, path) html = renderTweetSearch(timeline, prefs, getPath())
return renderMain(html, prefs, title, "Multi") return renderMain(html, request, title, "Multi")
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
if timeline.len == 0: if timeline.len == 0:
@ -75,57 +82,20 @@ proc createTimelineRouter*(cfg: Config) =
get "/@name/?": get "/@name/?":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/rss" % @"name" let rss = "/$1/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", Query(), cookiePrefs(), respTimeline(await showTimeline(request, Query(), cfg.title, rss))
getPath(), cfg.title, rss))
get "/@name/replies": get "/@name/replies":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/replies/rss" % @"name" let rss = "/$1/replies/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", getReplyQuery(@"name"), respTimeline(await showTimeline(request, getReplyQuery(@"name"), cfg.title, rss))
cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/media": get "/@name/media":
cond '.' notin @"name" cond '.' notin @"name"
let rss = "/$1/media/rss" % @"name" let rss = "/$1/media/rss" % @"name"
respTimeline(await showTimeline(@"name", @"after", getMediaQuery(@"name"), respTimeline(await showTimeline(request, getMediaQuery(@"name"), cfg.title, rss))
cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/search": get "/@name/search":
cond '.' notin @"name" cond '.' notin @"name"
let query = initQuery(params(request), name=(@"name")) let query = initQuery(params(request), name=(@"name"))
let rss = "/$1/search/rss?$2" % [@"name", genQueryUrl(query, onlyParam=true)] let rss = "/$1/search/rss?$2" % [@"name", genQueryUrl(query)]
respTimeline(await showTimeline(@"name", @"after", query, cookiePrefs(), respTimeline(await showTimeline(request, query, cfg.title, rss))
getPath(), cfg.title, rss))
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:
if conversation != nil and conversation.tweet.tombstone.len > 0:
resp Http404, showError(conversation.tweet.tombstone, cfg.title)
else:
resp Http404, showError("Tweet not found", cfg.title)
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:
resp renderMain(html, prefs, cfg.title, title, desc, path,
images=conversation.tweet.photos, `type`="photo")
get "/i/web/status/@id":
redirect("/i/status/" & @"id")

View File

@ -1,11 +1,15 @@
import uri
import karax/[karaxdsl, vdom] import karax/[karaxdsl, vdom]
import renderutils import renderutils
import ../utils, ../types import ../utils, ../types, ../prefs
import jester
const doctype = "<!DOCTYPE html>\n" const doctype = "<!DOCTYPE html>\n"
proc renderNavbar*(title, path, rss: string): VNode = proc renderNavbar*(title, rss: string; req: Request): VNode =
let path = $(parseUri(req.path) ? filterParams(req.params))
buildHtml(nav): buildHtml(nav):
tdiv(class="inner-nav"): tdiv(class="inner-nav"):
tdiv(class="nav-item"): tdiv(class="nav-item"):
@ -20,8 +24,9 @@ proc renderNavbar*(title, path, rss: string): 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 renderMain*(body: VNode; prefs: Prefs; title="Nitter"; titleText=""; desc=""; path="/"; proc renderMain*(body: VNode; req: Request; title="Nitter"; titleText=""; desc="";
rss=""; `type`="article"; video=""; images: seq[string] = @[]): string = rss=""; `type`="article"; video=""; images: seq[string] = @[]): string =
let prefs = getPrefs(req.cookies.getOrDefault("preferences"))
let node = buildHtml(html(lang="en")): let node = buildHtml(html(lang="en")):
head: head:
link(rel="stylesheet", `type`="text/css", href="/css/style.css") link(rel="stylesheet", `type`="text/css", href="/css/style.css")
@ -54,7 +59,7 @@ proc renderMain*(body: VNode; prefs: Prefs; title="Nitter"; titleText=""; desc="
meta(property="og:video:secure_url", content=video) meta(property="og:video:secure_url", content=video)
body: body:
renderNavbar(title, path, rss) renderNavbar(title, rss, req)
tdiv(class="container"): tdiv(class="container"):
body body
@ -67,4 +72,4 @@ proc renderError*(error: string): VNode =
span: text error span: text error
proc showError*(error, title: string): string = proc showError*(error, title: string): string =
renderMain(renderError(error), Prefs(), title, "Error") renderMain(renderError(error), Request(), title, "Error")

View File

@ -28,10 +28,9 @@ proc renderSearch*(): VNode =
button(`type`="submit"): icon "search" button(`type`="submit"): icon "search"
proc getTabClass(query: Query; tab: QueryKind): string = proc getTabClass(query: Query; tab: QueryKind): string =
var classes = @["tab-item"] result = "tab-item"
if query.kind == tab: if query.kind == tab:
classes.add "active" result &= " active"
return classes.join(" ")
proc renderProfileTabs*(query: Query; username: string): VNode = proc renderProfileTabs*(query: Query; username: string): VNode =
let link = "/" & username let link = "/" & username
@ -50,10 +49,10 @@ proc renderSearchTabs*(query: Query): VNode =
buildHtml(ul(class="tab")): buildHtml(ul(class="tab")):
li(class=query.getTabClass(custom)): li(class=query.getTabClass(custom)):
q.kind = custom q.kind = custom
a(href=genQueryUrl(q)): text "Tweets" a(href=("?" & genQueryUrl(q))): text "Tweets"
li(class=query.getTabClass(users)): li(class=query.getTabClass(users)):
q.kind = users q.kind = users
a(href=genQueryUrl(q)): text "Users" a(href=("?" & genQueryUrl(q))): text "Users"
proc isPanelOpen(q: Query): bool = proc isPanelOpen(q: Query): bool =
q.fromUser.len == 0 and (q.filters.len > 0 or q.excludes.len > 0 or q.fromUser.len == 0 and (q.filters.len > 0 or q.excludes.len > 0 or

View File

@ -1,26 +1,24 @@
import strutils, strformat, sequtils, algorithm, times import strutils, strformat, sequtils, algorithm, times, uri
import karax/[karaxdsl, vdom, vstyles] import karax/[karaxdsl, vdom, vstyles]
import ".."/[types, query, formatters] import ".."/[types, query, formatters]
import tweet, renderutils import tweet, renderutils
proc getQuery(query: Query): string = proc getQuery(query: Query): string =
if query.kind == posts: if query.kind != posts:
result = "?"
else:
result = genQueryUrl(query) result = genQueryUrl(query)
if result[^1] != '?': if result.len > 0:
result &= "&" result &= "&"
proc renderNewer(query: Query): VNode = proc renderNewer(query: Query; path: string): VNode =
buildHtml(tdiv(class="timeline-item show-more")): buildHtml(tdiv(class="timeline-item show-more")):
a(href=(getQuery(query).strip(chars={'?', '&'}))): a(href=(&"{path}?{genQueryUrl(query)}")):
text "Load newest" text "Load newest"
proc renderOlder(query: Query; minId: string): VNode = proc renderMore(query: Query; minId: string): VNode =
buildHtml(tdiv(class="show-more")): buildHtml(tdiv(class="show-more")):
a(href=(&"{getQuery(query)}after={minId}")): a(href=(&"?{getQuery(query)}after={minId}")):
text "Load older" text "Load more"
proc renderNoMore(): VNode = proc renderNoMore(): VNode =
buildHtml(tdiv(class="timeline-footer")): buildHtml(tdiv(class="timeline-footer")):
@ -58,15 +56,15 @@ proc renderUser(user: Profile; prefs: Prefs): VNode =
tdiv(class="tweet-content media-body"): tdiv(class="tweet-content media-body"):
verbatim linkifyText(user.bio, prefs) verbatim linkifyText(user.bio, prefs)
proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs): VNode = proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs; path=""): VNode =
buildHtml(tdiv(class="timeline")): buildHtml(tdiv(class="timeline")):
if not results.beginning: if not results.beginning:
renderNewer(results.query) renderNewer(results.query, path)
if results.content.len > 0: if results.content.len > 0:
for user in results.content: for user in results.content:
renderUser(user, prefs) renderUser(user, prefs)
renderOlder(results.query, results.minId) renderMore(results.query, results.minId)
elif results.beginning: elif results.beginning:
renderNoneFound() renderNoneFound()
else: else:
@ -75,7 +73,7 @@ proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs): VNode =
proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string): VNode = proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string): VNode =
buildHtml(tdiv(class="timeline")): buildHtml(tdiv(class="timeline")):
if not results.beginning: if not results.beginning:
renderNewer(results.query) renderNewer(results.query, parseUri(path).path)
if results.content.len == 0: if results.content.len == 0:
renderNoneFound() renderNoneFound()
@ -94,6 +92,6 @@ proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string):
threads &= tweet.threadId threads &= tweet.threadId
if results.hasMore or results.query.kind != posts: if results.hasMore or results.query.kind != posts:
renderOlder(results.query, results.minId) renderMore(results.query, results.minId)
else: else:
renderNoMore() renderNoMore()