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:
result &= " " & query.text
proc genQueryUrl*(query: Query; onlyParam=false): string =
if query.fromUser.len > 0:
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?"
proc genQueryUrl*(query: Query): string =
if query.kind notin {custom, users}: return
var params = @[&"kind={query.kind}"]
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)
get "/settings":
let prefs = cookiePrefs()
let path = refPath()
resp renderMain(renderPreferences(prefs, path), prefs, cfg.title, "Preferences", path)
let html = renderPreferences(cookiePrefs(), refPath())
resp renderMain(html, request, cfg.title, "Preferences")
post "/saveprefs":
var prefs = cookiePrefs()

View file

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

View file

@ -22,9 +22,9 @@ proc createSearchRouter*(cfg: Config) =
if "," in @"text":
redirect("/" & @"text")
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:
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:
resp Http404, showError("Invalid search.", cfg.title)

View file

@ -24,23 +24,23 @@ proc createStatusRouter*(cfg: Config) =
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)
let
title = pageTitle(conversation.tweet.profile)
desc = conversation.tweet.text
html = renderConversation(conversation, prefs, getPath())
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],
resp renderMain(html, request, cfg.title, title, desc, 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],
resp renderMain(html, request, cfg.title, title, desc, images = @[thumb],
`type`="video", video=vidUrl)
else:
resp renderMain(html, prefs, cfg.title, title, desc, path,
resp renderMain(html, request, cfg.title, title, desc,
images=conversation.tweet.photos, `type`="photo")
get "/@name/status/@id/photo/1":

View file

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

View file

@ -1,11 +1,15 @@
import uri
import karax/[karaxdsl, vdom]
import renderutils
import ../utils, ../types
import ../utils, ../types, ../prefs
import jester
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):
tdiv(class="inner-nav"):
tdiv(class="nav-item"):
@ -20,8 +24,9 @@ proc renderNavbar*(title, path, rss: string): VNode =
icon "info-circled", title="About", href="/about"
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 =
let prefs = getPrefs(req.cookies.getOrDefault("preferences"))
let node = buildHtml(html(lang="en")):
head:
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)
body:
renderNavbar(title, path, rss)
renderNavbar(title, rss, req)
tdiv(class="container"):
body
@ -67,4 +72,4 @@ proc renderError*(error: string): VNode =
span: text error
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"
proc getTabClass(query: Query; tab: QueryKind): string =
var classes = @["tab-item"]
result = "tab-item"
if query.kind == tab:
classes.add "active"
return classes.join(" ")
result &= " active"
proc renderProfileTabs*(query: Query; username: string): VNode =
let link = "/" & username
@ -50,10 +49,10 @@ proc renderSearchTabs*(query: Query): VNode =
buildHtml(ul(class="tab")):
li(class=query.getTabClass(custom)):
q.kind = custom
a(href=genQueryUrl(q)): text "Tweets"
a(href=("?" & genQueryUrl(q))): text "Tweets"
li(class=query.getTabClass(users)):
q.kind = users
a(href=genQueryUrl(q)): text "Users"
a(href=("?" & genQueryUrl(q))): text "Users"
proc isPanelOpen(q: Query): bool =
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 ".."/[types, query, formatters]
import tweet, renderutils
proc getQuery(query: Query): string =
if query.kind == posts:
result = "?"
else:
if query.kind != posts:
result = genQueryUrl(query)
if result[^1] != '?':
result &= "&"
if result.len > 0:
result &= "&"
proc renderNewer(query: Query): VNode =
proc renderNewer(query: Query; path: string): VNode =
buildHtml(tdiv(class="timeline-item show-more")):
a(href=(getQuery(query).strip(chars={'?', '&'}))):
a(href=(&"{path}?{genQueryUrl(query)}")):
text "Load newest"
proc renderOlder(query: Query; minId: string): VNode =
proc renderMore(query: Query; minId: string): VNode =
buildHtml(tdiv(class="show-more")):
a(href=(&"{getQuery(query)}after={minId}")):
text "Load older"
a(href=(&"?{getQuery(query)}after={minId}")):
text "Load more"
proc renderNoMore(): VNode =
buildHtml(tdiv(class="timeline-footer")):
@ -58,15 +56,15 @@ proc renderUser(user: Profile; prefs: Prefs): VNode =
tdiv(class="tweet-content media-body"):
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")):
if not results.beginning:
renderNewer(results.query)
renderNewer(results.query, path)
if results.content.len > 0:
for user in results.content:
renderUser(user, prefs)
renderOlder(results.query, results.minId)
renderMore(results.query, results.minId)
elif results.beginning:
renderNoneFound()
else:
@ -75,7 +73,7 @@ proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs): VNode =
proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string): VNode =
buildHtml(tdiv(class="timeline")):
if not results.beginning:
renderNewer(results.query)
renderNewer(results.query, parseUri(path).path)
if results.content.len == 0:
renderNoneFound()
@ -94,6 +92,6 @@ proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string):
threads &= tweet.threadId
if results.hasMore or results.query.kind != posts:
renderOlder(results.query, results.minId)
renderMore(results.query, results.minId)
else:
renderNoMore()