nitter/src/views/timeline.nim

125 lines
4.1 KiB
Nim
Raw Normal View History

2019-10-26 13:34:30 +00:00
import strutils, strformat, sequtils, algorithm, times, uri, options
import karax/[karaxdsl, vdom, vstyles]
2019-09-13 17:57:27 +00:00
import ".."/[types, query, formatters]
import tweet, renderutils
2019-09-19 00:23:22 +00:00
proc getQuery(query: Query): string =
2019-09-20 20:56:27 +00:00
if query.kind != posts:
2019-09-19 00:23:22 +00:00
result = genQueryUrl(query)
2019-09-20 20:56:27 +00:00
if result.len > 0:
result &= "&"
2020-06-01 00:22:22 +00:00
proc renderToTop*(focus="#"): VNode =
buildHtml(tdiv(class="top-ref")):
icon "down", href=focus
proc renderNewer*(query: Query; path: string; focus=""): VNode =
let
q = genQueryUrl(query)
url = if q.len > 0: "?" & q else: ""
p = if focus.len > 0: path.replace("#m", focus) else: path
2019-09-13 17:57:27 +00:00
buildHtml(tdiv(class="timeline-item show-more")):
2020-06-01 00:22:22 +00:00
a(href=(p & url)):
2019-09-13 17:57:27 +00:00
text "Load newest"
2020-06-01 00:22:22 +00:00
proc renderMore*(query: Query; cursor: string; focus=""): VNode =
buildHtml(tdiv(class="show-more")):
2020-06-01 00:22:22 +00:00
a(href=(&"?{getQuery(query)}cursor={encodeUrl(cursor)}{focus}")):
2019-09-20 20:56:27 +00:00
text "Load more"
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"
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"
proc renderThread(thread: seq[Tweet]; prefs: Prefs; path: string): VNode =
2019-09-13 17:57:27 +00:00
buildHtml(tdiv(class="thread-line")):
let sortedThread = thread.sortedByIt(it.id)
for i, tweet in sortedThread:
let show = i == thread.high and sortedThread[0].id != tweet.threadId
let header = if tweet.pinned or tweet.retweet.isSome: "with-header " else: ""
renderTweet(tweet, prefs, path, class=(header & "thread"),
2020-06-01 00:22:22 +00:00
index=i, last=(i == thread.high), showThread=show)
2020-06-01 00:22:22 +00:00
proc threadFilter(tweets: openArray[Tweet]; threads: openArray[int64]; it: Tweet): seq[Tweet] =
result = @[it]
if it.retweet.isSome or it.replyId in threads: return
for t in tweets:
if t.id == result[0].replyId:
result.insert t
elif t.replyId == result[0].id:
result.add t
2019-09-13 20:24:58 +00:00
proc renderUser(user: Profile; prefs: Prefs): VNode =
buildHtml(tdiv(class="timeline-item")):
2019-09-20 00:49:54 +00:00
a(class="tweet-link", href=("/" & user.username))
2019-09-13 20:24:58 +00:00
tdiv(class="tweet-body profile-result"):
tdiv(class="tweet-header"):
a(class="tweet-avatar", href=("/" & user.username)):
genImg(user.getUserpic("_bigger"), class="avatar")
tdiv(class="tweet-name-row"):
tdiv(class="fullname-and-username"):
linkUser(user, class="fullname")
linkUser(user, class="username")
2019-10-17 22:54:22 +00:00
tdiv(class="tweet-content media-body", dir="auto"):
verbatim replaceUrl(user.bio, prefs)
2019-09-13 20:24:58 +00:00
2019-09-20 20:56:27 +00:00
proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs; path=""): VNode =
2019-09-13 20:24:58 +00:00
buildHtml(tdiv(class="timeline")):
if not results.beginning:
2019-09-20 20:56:27 +00:00
renderNewer(results.query, path)
2019-09-13 20:24:58 +00:00
if results.content.len > 0:
for user in results.content:
renderUser(user, prefs)
2020-06-01 00:22:22 +00:00
if results.bottom.len > 0:
renderMore(results.query, results.bottom)
2019-10-13 18:17:12 +00:00
renderToTop()
2019-09-13 20:24:58 +00:00
elif results.beginning:
renderNoneFound()
else:
renderNoMore()
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:
2019-09-20 20:56:27 +00:00
renderNewer(results.query, parseUri(path).path)
2019-09-13 17:57:27 +00:00
if results.content.len == 0:
2020-06-01 00:22:22 +00:00
if not results.beginning:
renderNoMore()
else:
renderNoneFound()
else:
2020-06-01 00:22:22 +00:00
var
threads: seq[int64]
retweets: seq[int64]
2019-09-13 17:57:27 +00:00
for tweet in results.content:
2020-06-01 00:22:22 +00:00
let rt = if tweet.retweet.isSome: get(tweet.retweet).id else: 0
2020-06-01 03:18:32 +00:00
if tweet.id in threads or rt in retweets or tweet.id in retweets or
2020-06-01 00:22:22 +00:00
tweet.pinned and prefs.hidePins: continue
let thread = results.content.threadFilter(threads, tweet)
2019-09-13 17:57:27 +00:00
if thread.len < 2:
2020-06-01 00:22:22 +00:00
var hasThread = tweet.hasThread
if rt != 0:
retweets &= rt
hasThread = get(tweet.retweet).hasThread
renderTweet(tweet, prefs, path, showThread=hasThread)
2019-09-13 17:57:27 +00:00
else:
renderThread(thread, prefs, path)
2020-06-01 00:22:22 +00:00
threads &= thread.mapIt(it.id)
2019-09-13 17:57:27 +00:00
2020-06-01 00:22:22 +00:00
renderMore(results.query, results.bottom)
2019-10-13 18:17:12 +00:00
renderToTop()