Make queries non-optional

This commit is contained in:
Zed 2019-09-19 02:23:22 +02:00
parent 53c6247d8b
commit bd774cf0ca
10 changed files with 48 additions and 64 deletions

View File

@ -9,7 +9,7 @@ proc getResult[T](json: JsonNode; query: Query; after: string): Result[T] =
hasMore: json["has_more_items"].to(bool), hasMore: json["has_more_items"].to(bool),
maxId: json.getOrDefault("max_position").getStr(""), maxId: json.getOrDefault("max_position").getStr(""),
minId: json.getOrDefault("min_position").getStr("").cleanPos(), minId: json.getOrDefault("min_position").getStr("").cleanPos(),
query: some query, query: query,
beginning: after.len == 0 beginning: after.len == 0
) )
@ -42,14 +42,14 @@ proc getSearch*[T](query: Query; after, agent: string): Future[Result[T]] {.asyn
} }
let json = await fetchJson(base / searchUrl ? params, headers) let json = await fetchJson(base / searchUrl ? params, headers)
if json == nil: return Result[T](query: some query) if json == nil: return Result[T](query: query)
result = getResult[T](json, query, after) result = getResult[T](json, query, after)
if not json.hasKey("items_html"): return if not json.hasKey("items_html"): return
let html = parseHtml(json["items_html"].to(string)) let html = parseHtml(json["items_html"].to(string))
when T is Tweet: when T is Tweet:
result = await finishTimeline(json, some query, after, agent) result = await finishTimeline(json, query, after, agent)
elif T is Profile: elif T is Profile:
result.hasMore = json["items_html"].to(string) != "\n" result.hasMore = json["items_html"].to(string) != "\n"
for p in html.selectAll(".js-stream-item"): for p in html.selectAll(".js-stream-item"):

View File

@ -4,7 +4,7 @@ import sequtils, strutils, json, xmltree, uri
import ".."/[types, parser, parserutils, formatters, query] import ".."/[types, parser, parserutils, formatters, query]
import utils, consts, media import utils, consts, media
proc finishTimeline*(json: JsonNode; query: Option[Query]; after, agent: string): Future[Timeline] {.async.} = proc finishTimeline*(json: JsonNode; query: Query; after, agent: string): Future[Timeline] {.async.} =
if json == nil: return Timeline() if json == nil: return Timeline()
result = Timeline( result = Timeline(
@ -49,7 +49,7 @@ proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
params.add {"max_position": after} params.add {"max_position": after}
let json = await fetchJson(base / (timelineUrl % username) ? params, headers) let json = await fetchJson(base / (timelineUrl % username) ? params, headers)
result = await finishTimeline(json, none Query, after, agent) result = await finishTimeline(json, Query(), after, agent)
proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} = proc getProfileAndTimeline*(username, agent, after: string): Future[(Profile, Timeline)] {.async.} =
let headers = newHttpHeaders({ let headers = newHttpHeaders({

View File

@ -157,7 +157,7 @@ proc parseConversation*(node: XmlNode): Conversation =
result.replies.add parseThread(thread) result.replies.add parseThread(thread)
proc parseTimeline*(node: XmlNode; after: string): Timeline = proc parseTimeline*(node: XmlNode; after: string): Timeline =
if node == nil: return if node == nil: return Timeline()
result = Timeline( result = Timeline(
content: parseThread(node.select(".stream > .stream-items")).content, content: parseThread(node.select(".stream > .stream-items")).content,
minId: node.attr("data-min-position"), minId: node.attr("data-min-position"),

View File

@ -78,7 +78,7 @@ proc genQueryUrl*(query: Query): string =
if query.fromUser.len > 0: if query.fromUser.len > 0:
result = "/" & query.fromUser.join(",") result = "/" & query.fromUser.join(",")
if query.kind == multi: if query.fromUser.len > 1:
return result & "?" return result & "?"
if query.kind notin {custom, users}: if query.kind notin {custom, users}:

View File

@ -8,7 +8,7 @@ import ../views/general
include "../views/rss.nimf" include "../views/rss.nimf"
proc showRss*(name: string; query: Option[Query]): Future[string] {.async.} = proc showRss*(name: string; query: Query): Future[string] {.async.} =
let (profile, timeline, _) = await fetchSingleTimeline(name, "", getAgent(), query) let (profile, timeline, _) = await fetchSingleTimeline(name, "", getAgent(), query)
return renderTimelineRss(timeline.content, profile) return renderTimelineRss(timeline.content, profile)
@ -21,12 +21,12 @@ proc createRssRouter*(cfg: Config) =
router rss: router rss:
get "/@name/rss": get "/@name/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", none Query)) respRss(await showRss(@"name", Query()))
get "/@name/replies/rss": get "/@name/replies/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", some getReplyQuery(@"name"))) respRss(await showRss(@"name", getReplyQuery(@"name")))
get "/@name/media/rss": get "/@name/media/rss":
cond '.' notin @"name" cond '.' notin @"name"
respRss(await showRss(@"name", some getMediaQuery(@"name"))) respRss(await showRss(@"name", getMediaQuery(@"name")))

View File

@ -16,7 +16,7 @@ export profile, timeline, status
type ProfileTimeline = (Profile, Timeline, seq[GalleryPhoto]) type ProfileTimeline = (Profile, Timeline, seq[GalleryPhoto])
proc fetchSingleTimeline*(name, after, agent: string; proc fetchSingleTimeline*(name, after, agent: string;
query: Option[Query]): Future[ProfileTimeline] {.async.} = query: Query): Future[ProfileTimeline] {.async.} =
let railFut = getPhotoRail(name, agent) let railFut = getPhotoRail(name, agent)
var timeline: Timeline var timeline: Timeline
@ -26,14 +26,14 @@ proc fetchSingleTimeline*(name, after, agent: string;
if cachedProfile.isSome: if cachedProfile.isSome:
profile = get(cachedProfile) profile = get(cachedProfile)
if query.isNone: if query.kind == posts:
if cachedProfile.isSome: if cachedProfile.isSome:
timeline = await getTimeline(name, after, agent) timeline = await getTimeline(name, after, agent)
else: else:
(profile, timeline) = await getProfileAndTimeline(name, agent, after) (profile, timeline) = await getProfileAndTimeline(name, agent, after)
cache(profile) cache(profile)
else: else:
var timelineFut = getSearch[Tweet](get(query), after, agent) var timelineFut = getSearch[Tweet](query, after, agent)
if cachedProfile.isNone: if cachedProfile.isNone:
profile = await getCachedProfile(name, agent) profile = await getCachedProfile(name, agent)
timeline = await timelineFut timeline = await timelineFut
@ -42,16 +42,14 @@ proc fetchSingleTimeline*(name, after, agent: string;
return (profile, timeline, await railFut) return (profile, timeline, await railFut)
proc fetchMultiTimeline*(names: seq[string]; after, agent: string; proc fetchMultiTimeline*(names: seq[string]; after, agent: string;
query: Option[Query]): Future[Timeline] {.async.} = query: Query): Future[Timeline] {.async.} =
var q = query var q = query
if q.isSome: q.fromUser = names
get(q).fromUser = names if q.kind == posts and "replies" notin q.excludes:
else: q.excludes.add "replies"
q = some Query(kind: multi, fromUser: names, excludes: @["replies"]) return await getSearch[Tweet](q, after, agent)
return await getSearch[Tweet](get(q), after, agent) proc showTimeline*(name, after: string; query: Query;
proc showTimeline*(name, after: string; query: Option[Query];
prefs: Prefs; path, title, rss: string): Future[string] {.async.} = prefs: Prefs; path, title, rss: string): Future[string] {.async.} =
let agent = getAgent() let agent = getAgent()
let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) let names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0)
@ -79,25 +77,25 @@ 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", none Query, cookiePrefs(), respTimeline(await showTimeline(@"name", @"after", Query(), cookiePrefs(),
getPath(), cfg.title, rss)) getPath(), cfg.title, rss))
get "/@name/search": get "/@name/search":
cond '.' notin @"name" cond '.' notin @"name"
let query = some initQuery(params(request), name=(@"name")) let query = initQuery(params(request), name=(@"name"))
respTimeline(await showTimeline(@"name", @"after", query, cookiePrefs(), respTimeline(await showTimeline(@"name", @"after", query, cookiePrefs(),
getPath(), cfg.title, "")) getPath(), cfg.title, ""))
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", some getReplyQuery(@"name"), respTimeline(await showTimeline(@"name", @"after", getReplyQuery(@"name"),
cookiePrefs(), getPath(), 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", some getMediaQuery(@"name"), respTimeline(await showTimeline(@"name", @"after", getMediaQuery(@"name"),
cookiePrefs(), getPath(), cfg.title, rss)) cookiePrefs(), getPath(), cfg.title, rss))
get "/@name/status/@id": get "/@name/status/@id":

View File

@ -57,7 +57,7 @@ dbFromTypes("cache.db", "", "", "", [Profile, Video])
type type
QueryKind* = enum QueryKind* = enum
posts, replies, media, multi, users, custom posts, replies, media, users, custom
Query* = object Query* = object
kind*: QueryKind kind*: QueryKind
@ -74,7 +74,7 @@ type
maxId*: string maxId*: string
hasMore*: bool hasMore*: bool
beginning*: bool beginning*: bool
query*: Option[Query] query*: Query
Gif* = object Gif* = object
url*: string url*: string

View File

@ -83,9 +83,7 @@ proc renderProtected(username: string): VNode =
proc renderProfile*(profile: Profile; timeline: Timeline; proc renderProfile*(profile: Profile; timeline: Timeline;
photoRail: seq[GalleryPhoto]; prefs: Prefs; path: string): VNode = photoRail: seq[GalleryPhoto]; prefs: Prefs; path: string): VNode =
if timeline.query.isNone: timeline.query.fromUser = @[profile.username]
timeline.query = some Query(fromUser: @[profile.username])
buildHtml(tdiv(class="profile-tabs")): buildHtml(tdiv(class="profile-tabs")):
if not prefs.hideBanner: if not prefs.hideBanner:
tdiv(class="profile-banner"): tdiv(class="profile-banner"):

View File

@ -27,36 +27,31 @@ proc renderSearch*(): VNode =
input(`type`="text", name="text", autofocus="", placeholder="Enter username...") input(`type`="text", name="text", autofocus="", placeholder="Enter username...")
button(`type`="submit"): icon "search" button(`type`="submit"): icon "search"
proc getTabClass(query: Option[Query]; tab: string): string = proc getTabClass(query: Query; tab: QueryKind): string =
var classes = @["tab-item"] var classes = @["tab-item"]
if query.kind == tab:
if query.isNone or get(query).kind == multi:
if tab == "posts":
classes.add "active"
elif $get(query).kind == tab:
classes.add "active" classes.add "active"
return classes.join(" ") return classes.join(" ")
proc renderProfileTabs*(query: Option[Query]; username: string): VNode = proc renderProfileTabs*(query: Query; username: string): VNode =
let link = "/" & username let link = "/" & username
buildHtml(ul(class="tab")): buildHtml(ul(class="tab")):
li(class=query.getTabClass("posts")): li(class=query.getTabClass(posts)):
a(href=link): text "Tweets" a(href=link): text "Tweets"
li(class=query.getTabClass("replies")): li(class=query.getTabClass(replies)):
a(href=(link & "/replies")): text "Tweets & Replies" a(href=(link & "/replies")): text "Tweets & Replies"
li(class=query.getTabClass("media")): li(class=query.getTabClass(media)):
a(href=(link & "/media")): text "Media" a(href=(link & "/media")): text "Media"
li(class=query.getTabClass("custom")): li(class=query.getTabClass(custom)):
a(href=(link & "/search")): text "Custom" a(href=(link & "/search")): text "Custom"
proc renderSearchTabs*(query: Option[Query]): VNode = proc renderSearchTabs*(query: Query): VNode =
var q = if query.isSome: get(query) else: Query() var q = query
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"
@ -81,10 +76,7 @@ proc renderSearchPanel*(query: Query): VNode =
genCheckbox(&"{f[0]}-{k}", v, state) genCheckbox(&"{f[0]}-{k}", v, state)
proc renderTweetSearch*(tweets: Result[Tweet]; prefs: Prefs; path: string): VNode = proc renderTweetSearch*(tweets: Result[Tweet]; prefs: Prefs; path: string): VNode =
let query = let query = tweets.query
if tweets.query.isSome: get(tweets.query)
else: Query(kind: custom)
buildHtml(tdiv(class="timeline-container")): buildHtml(tdiv(class="timeline-container")):
if query.fromUser.len > 1: if query.fromUser.len > 1:
tdiv(class="timeline-header"): tdiv(class="timeline-header"):
@ -94,22 +86,18 @@ proc renderTweetSearch*(tweets: Result[Tweet]; prefs: Prefs; path: string): VNod
renderSearchPanel(query) renderSearchPanel(query)
if query.fromUser.len > 0: if query.fromUser.len > 0:
renderProfileTabs(tweets.query, query.fromUser.join(",")) renderProfileTabs(query, query.fromUser.join(","))
else: else:
renderSearchTabs(tweets.query) renderSearchTabs(query)
renderTimelineTweets(tweets, prefs, path) renderTimelineTweets(tweets, prefs, path)
proc renderUserSearch*(users: Result[Profile]; prefs: Prefs): VNode = proc renderUserSearch*(users: Result[Profile]; prefs: Prefs): VNode =
let searchText =
if users.query.isSome: get(users.query).text
else: ""
buildHtml(tdiv(class="timeline-container")): buildHtml(tdiv(class="timeline-container")):
tdiv(class="timeline-header"): tdiv(class="timeline-header"):
form(`method`="get", action="/search", class="search-field"): form(`method`="get", action="/search", class="search-field"):
hiddenField("kind", "users") hiddenField("kind", "users")
genInput("text", "", searchText, "Enter username...", class="pref-inline") genInput("text", "", users.query.text, "Enter username...", class="pref-inline")
button(`type`="submit"): icon "search" button(`type`="submit"): icon "search"
renderSearchTabs(users.query) renderSearchTabs(users.query)

View File

@ -4,20 +4,20 @@ import karax/[karaxdsl, vdom, vstyles]
import ".."/[types, query, formatters] import ".."/[types, query, formatters]
import tweet, renderutils import tweet, renderutils
proc getQuery(query: Option[Query]): string = proc getQuery(query: Query): string =
if query.isNone: if query.kind == posts:
result = "?" result = "?"
else: else:
result = genQueryUrl(get(query)) result = genQueryUrl(query)
if result[^1] != '?': if result[^1] != '?':
result &= "&" result &= "&"
proc renderNewer(query: Option[Query]): VNode = proc renderNewer(query: Query): VNode =
buildHtml(tdiv(class="timeline-item show-more")): buildHtml(tdiv(class="timeline-item show-more")):
a(href=(getQuery(query).strip(chars={'?', '&'}))): a(href=(getQuery(query).strip(chars={'?', '&'}))):
text "Load newest" text "Load newest"
proc renderOlder(query: Option[Query]; minId: string): VNode = proc renderOlder(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 older"
@ -88,7 +88,7 @@ proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string):
renderThread(thread, prefs, path) renderThread(thread, prefs, path)
threads &= tweet.threadId threads &= tweet.threadId
if results.hasMore or results.query.isSome: if results.hasMore or results.query.kind != posts:
renderOlder(results.query, results.minId) renderOlder(results.query, results.minId)
else: else:
renderNoMore() renderNoMore()