nitter/src/views/timeline.nim

99 lines
3.2 KiB
Nim
Raw Normal View History

2019-08-06 19:16:31 +00:00
import strutils, strformat, sequtils, algorithm, times
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 =
if query.kind == posts:
2019-09-13 17:57:27 +00:00
result = "?"
else:
2019-09-19 00:23:22 +00:00
result = genQueryUrl(query)
2019-09-13 17:57:27 +00:00
if result[^1] != '?':
result &= "&"
2019-09-19 00:23:22 +00:00
proc renderNewer(query: Query): VNode =
2019-09-13 17:57:27 +00:00
buildHtml(tdiv(class="timeline-item show-more")):
a(href=(getQuery(query).strip(chars={'?', '&'}))):
text "Load newest"
2019-09-19 00:23:22 +00:00
proc renderOlder(query: Query; minId: string): VNode =
buildHtml(tdiv(class="show-more")):
2019-09-13 17:57:27 +00:00
a(href=(&"{getQuery(query)}after={minId}")):
text "Load older"
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")):
for i, threadTweet in thread.sortedByIt(it.time):
2019-09-19 01:51:15 +00:00
let show = i == thread.len and thread[0].id != threadTweet.threadId
renderTweet(threadTweet, prefs, path, class="thread",
2019-09-19 01:51:15 +00:00
index=i, total=thread.high, showThread=show)
proc threadFilter(it: Tweet; tweetThread: string): bool =
it.retweet.isNone and it.reply.len == 0 and it.threadId == tweetThread
2019-09-13 20:24:58 +00:00
proc renderUser(user: Profile; prefs: Prefs): VNode =
buildHtml(tdiv(class="timeline-item")):
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")
tdiv(class="tweet-content media-body"):
verbatim linkifyText(user.bio, prefs)
proc renderTimelineUsers*(results: Result[Profile]; prefs: Prefs): VNode =
buildHtml(tdiv(class="timeline")):
if not results.beginning:
renderNewer(results.query)
if results.content.len > 0:
for user in results.content:
renderUser(user, prefs)
renderOlder(results.query, results.minId)
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:
renderNewer(results.query)
if results.content.len == 0:
renderNoneFound()
else:
2019-09-13 17:57:27 +00:00
var threads: seq[string]
var retweets: seq[string]
2019-09-13 17:57:27 +00:00
for tweet in results.content:
if tweet.threadId in threads or tweet.id in retweets: continue
2019-09-13 17:57:27 +00:00
let thread = results.content.filterIt(threadFilter(it, tweet.threadId))
if thread.len < 2:
if tweet.retweet.isSome:
retweets &= tweet.id
2019-09-19 01:51:15 +00:00
renderTweet(tweet, prefs, path, showThread=tweet.hasThread)
2019-09-13 17:57:27 +00:00
else:
renderThread(thread, prefs, path)
threads &= tweet.threadId
2019-09-19 00:23:22 +00:00
if results.hasMore or results.query.kind != posts:
2019-09-13 17:57:27 +00:00
renderOlder(results.query, results.minId)
else:
renderNoMore()