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]
|
|
|
|
|
|
|
|
import ../types, ../search
|
|
|
|
import tweet, renderutils
|
|
|
|
|
|
|
|
proc getQuery(timeline: Timeline): string =
|
|
|
|
if timeline.query.isNone: "?"
|
|
|
|
else: genQueryUrl(get(timeline.query))
|
|
|
|
|
|
|
|
proc getTabClass(timeline: Timeline; tab: string): string =
|
|
|
|
var classes = @["tab-item"]
|
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
if timeline.query.isNone or get(timeline.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-08-06 15:41:06 +00:00
|
|
|
elif $get(timeline.query).kind == tab:
|
2019-07-10 22:42:31 +00:00
|
|
|
classes.add "active"
|
|
|
|
|
|
|
|
return classes.join(" ")
|
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
proc renderSearchTabs(timeline: Timeline; username: string): VNode =
|
|
|
|
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-08-06 15:41:06 +00:00
|
|
|
proc renderNewer(timeline: Timeline; username: string): VNode =
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(tdiv(class="status-el show-more")):
|
2019-08-06 15:41:06 +00:00
|
|
|
a(href=("/" & username & getQuery(timeline).strip(chars={'?'}))):
|
2019-07-10 22:42:31 +00:00
|
|
|
text "Load newest tweets"
|
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
proc renderOlder(timeline: Timeline; username: string): VNode =
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(tdiv(class="show-more")):
|
2019-08-06 15:41:06 +00:00
|
|
|
a(href=(&"/{username}{getQuery(timeline)}after={timeline.minId}")):
|
2019-07-10 22:42:31 +00:00
|
|
|
text "Load older tweets"
|
|
|
|
|
|
|
|
proc renderNoMore(): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-footer")):
|
|
|
|
h2(class="timeline-end", style={textAlign: "center"}):
|
|
|
|
text "No more tweets."
|
|
|
|
|
|
|
|
proc renderNoneFound(): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-header")):
|
|
|
|
h2(class="timeline-none", style={textAlign: "center"}):
|
|
|
|
text "No tweets found."
|
|
|
|
|
|
|
|
proc renderProtected(username: string): VNode =
|
|
|
|
buildHtml(tdiv(class="timeline-header timeline-protected")):
|
|
|
|
h2: text "This account's tweets are protected."
|
|
|
|
p: text &"Only confirmed followers have access to @{username}'s tweets."
|
|
|
|
|
2019-08-13 17:44:29 +00:00
|
|
|
proc renderThread(thread: seq[Tweet]; prefs: Prefs): VNode =
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(tdiv(class="timeline-tweet thread-line")):
|
|
|
|
for i, threadTweet in thread.sortedByIt(it.time):
|
2019-08-13 17:44:29 +00:00
|
|
|
renderTweet(threadTweet, prefs, 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-08-13 17:44:29 +00:00
|
|
|
proc renderTweets(timeline: Timeline; prefs: Prefs): VNode =
|
2019-07-31 01:59:38 +00:00
|
|
|
buildHtml(tdiv(id="posts")):
|
2019-07-10 22:42:31 +00:00
|
|
|
var threads: seq[string]
|
2019-08-23 00:15:25 +00:00
|
|
|
for tweet in timeline.content:
|
2019-07-10 22:42:31 +00:00
|
|
|
if tweet.threadId in threads: continue
|
2019-08-23 00:15:25 +00:00
|
|
|
let thread = timeline.content.filterIt(threadFilter(it, tweet.threadId))
|
2019-07-10 22:42:31 +00:00
|
|
|
if thread.len < 2:
|
2019-08-13 17:44:29 +00:00
|
|
|
renderTweet(tweet, prefs, class="timeline-tweet")
|
2019-07-10 22:42:31 +00:00
|
|
|
else:
|
2019-08-13 17:44:29 +00:00
|
|
|
renderThread(thread, prefs)
|
2019-07-10 22:42:31 +00:00
|
|
|
threads &= tweet.threadId
|
|
|
|
|
2019-08-13 17:44:29 +00:00
|
|
|
proc renderTimeline*(timeline: Timeline; username: string; protected: bool;
|
|
|
|
prefs: Prefs; multi=false): VNode =
|
2019-07-10 22:42:31 +00:00
|
|
|
buildHtml(tdiv):
|
2019-08-06 15:41:06 +00:00
|
|
|
if multi:
|
|
|
|
tdiv(class="multi-header"):
|
|
|
|
text username.replace(",", " | ")
|
2019-07-10 22:42:31 +00:00
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
if not protected:
|
|
|
|
renderSearchTabs(timeline, username)
|
|
|
|
if not timeline.beginning:
|
|
|
|
renderNewer(timeline, username)
|
2019-07-10 22:42:31 +00:00
|
|
|
|
2019-08-06 15:41:06 +00:00
|
|
|
if protected:
|
|
|
|
renderProtected(username)
|
2019-08-23 00:15:25 +00:00
|
|
|
elif timeline.content.len == 0:
|
2019-07-10 22:42:31 +00:00
|
|
|
renderNoneFound()
|
|
|
|
else:
|
2019-08-13 17:44:29 +00:00
|
|
|
renderTweets(timeline, prefs)
|
2019-07-10 22:42:31 +00:00
|
|
|
if timeline.hasMore or timeline.query.isSome:
|
2019-08-06 15:41:06 +00:00
|
|
|
renderOlder(timeline, username)
|
2019-07-10 22:42:31 +00:00
|
|
|
else:
|
|
|
|
renderNoMore()
|