2019-08-06 19:16:31 +00:00
|
|
|
import strutils, strformat, sequtils, algorithm, times
|
2019-07-10 22:42:31 +00:00
|
|
|
import karax/[karaxdsl, vdom, vstyles]
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
import ".."/[types, query, formatters]
|
2019-07-10 22:42:31 +00:00
|
|
|
import tweet, renderutils
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
proc getQuery(query: Option[Query]): string =
|
|
|
|
if query.isNone:
|
|
|
|
result = "?"
|
|
|
|
else:
|
|
|
|
result = genQueryUrl(get(query))
|
|
|
|
if result[^1] != '?':
|
|
|
|
result &= "&"
|
2019-07-10 22:42:31 +00:00
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
proc getTabClass(results: Result; tab: string): string =
|
2019-07-10 22:42:31 +00:00
|
|
|
var classes = @["tab-item"]
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
if results.query.isNone or get(results.query).kind == multi:
|
2019-07-31 01:59:38 +00:00
|
|
|
if tab == "posts":
|
2019-07-10 22:42:31 +00:00
|
|
|
classes.add "active"
|
2019-09-13 17:57:27 +00:00
|
|
|
elif $get(results.query).kind == tab:
|
2019-07-10 22:42:31 +00:00
|
|
|
classes.add "active"
|
|
|
|
|
|
|
|
return classes.join(" ")
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
proc renderProfileTabs*(timeline: Timeline; username: string): VNode =
|
2019-08-06 15:41:06 +00:00
|
|
|
let link = "/" & username
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(ul(class="tab")):
|
2019-07-31 01:59:38 +00:00
|
|
|
li(class=timeline.getTabClass("posts")):
|
2019-07-10 22:42:31 +00:00
|
|
|
a(href=link): text "Tweets"
|
|
|
|
li(class=timeline.getTabClass("replies")):
|
|
|
|
a(href=(link & "/replies")): text "Tweets & Replies"
|
|
|
|
li(class=timeline.getTabClass("media")):
|
|
|
|
a(href=(link & "/media")): text "Media"
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
proc renderNewer(query: Option[Query]): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-item show-more")):
|
|
|
|
a(href=(getQuery(query).strip(chars={'?', '&'}))):
|
|
|
|
text "Load newest"
|
2019-07-10 22:42:31 +00:00
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
proc renderOlder(query: Option[Query]; minId: string): VNode =
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(tdiv(class="show-more")):
|
2019-09-13 17:57:27 +00:00
|
|
|
a(href=(&"{getQuery(query)}after={minId}")):
|
|
|
|
text "Load older"
|
2019-07-10 22:42:31 +00:00
|
|
|
|
|
|
|
proc renderNoMore(): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-footer")):
|
2019-09-13 08:44:21 +00:00
|
|
|
h2(class="timeline-end"):
|
2019-09-13 17:57:27 +00:00
|
|
|
text "No more items"
|
2019-07-10 22:42:31 +00:00
|
|
|
|
|
|
|
proc renderNoneFound(): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-header")):
|
2019-09-13 08:44:21 +00:00
|
|
|
h2(class="timeline-none"):
|
2019-09-13 17:57:27 +00:00
|
|
|
text "No items found"
|
2019-07-10 22:42:31 +00:00
|
|
|
|
2019-09-05 20:40:36 +00:00
|
|
|
proc renderThread(thread: seq[Tweet]; prefs: Prefs; path: string): VNode =
|
2019-09-13 17:57:27 +00:00
|
|
|
buildHtml(tdiv(class="thread-line")):
|
2019-07-10 22:42:31 +00:00
|
|
|
for i, threadTweet in thread.sortedByIt(it.time):
|
2019-09-05 20:40:36 +00:00
|
|
|
renderTweet(threadTweet, prefs, path, class="thread",
|
|
|
|
index=i, total=thread.high)
|
2019-07-10 22:42:31 +00:00
|
|
|
|
|
|
|
proc threadFilter(it: Tweet; tweetThread: string): bool =
|
|
|
|
it.retweet.isNone and it.reply.len == 0 and it.threadId == tweetThread
|
|
|
|
|
2019-09-13 17:57:27 +00:00
|
|
|
|
|
|
|
proc renderTimelineTweets*(results: Result[Tweet]; prefs: Prefs; path: string): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline")):
|
|
|
|
if not results.beginning:
|
|
|
|
renderNewer(results.query)
|
|
|
|
|
|
|
|
if results.content.len == 0:
|
2019-07-10 22:42:31 +00:00
|
|
|
renderNoneFound()
|
|
|
|
else:
|
2019-09-13 17:57:27 +00:00
|
|
|
var threads: seq[string]
|
|
|
|
for tweet in results.content:
|
|
|
|
if tweet.threadId in threads: continue
|
|
|
|
let thread = results.content.filterIt(threadFilter(it, tweet.threadId))
|
|
|
|
if thread.len < 2:
|
|
|
|
renderTweet(tweet, prefs, path)
|
|
|
|
else:
|
|
|
|
renderThread(thread, prefs, path)
|
|
|
|
threads &= tweet.threadId
|
|
|
|
|
|
|
|
if results.hasMore or results.query.isSome:
|
|
|
|
renderOlder(results.query, results.minId)
|
2019-07-10 22:42:31 +00:00
|
|
|
else:
|
|
|
|
renderNoMore()
|