nitter/src/views/tweet.nim

410 lines
14 KiB
Nim
Raw Normal View History

2021-12-27 01:37:38 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
2019-10-26 13:34:30 +00:00
import strutils, sequtils, strformat, options
import karax/[karaxdsl, vdom, vstyles]
import renderutils
2019-09-06 00:42:35 +00:00
import ".."/[types, utils, formatters]
2022-01-14 17:01:47 +00:00
import general
2020-06-07 06:26:39 +00:00
proc getSmallPic(url: string): string =
result = url
if "?" notin url and not url.endsWith("placeholder.png"):
2020-06-07 06:26:39 +00:00
result &= ":small"
result = getPicUrl(result)
2022-01-14 02:16:09 +00:00
proc renderMiniAvatar(profile: Profile; prefs: Prefs): VNode =
2022-01-06 02:57:14 +00:00
let url = getPicUrl(profile.getUserPic("_mini"))
2020-06-10 15:04:48 +00:00
buildHtml():
2022-01-14 02:16:09 +00:00
img(class=(prefs.getAvatarClass & " mini"), src=url)
2020-06-10 15:04:48 +00:00
proc renderHeader(tweet: Tweet; retweet: string; prefs: Prefs): VNode =
buildHtml(tdiv):
2020-06-01 00:22:22 +00:00
if retweet.len > 0:
tdiv(class="retweet-header"):
2020-06-01 00:22:22 +00:00
span: icon "retweet", retweet & " retweeted"
if tweet.pinned:
tdiv(class="pinned"):
span: icon "pin", "Pinned Tweet"
tdiv(class="tweet-header"):
2019-08-12 15:02:07 +00:00
a(class="tweet-avatar", href=("/" & tweet.profile.username)):
2020-11-08 01:56:06 +00:00
var size = "_bigger"
2022-01-06 02:57:14 +00:00
if not prefs.autoplayGifs and tweet.profile.userPic.endsWith("gif"):
size = "_400x400"
2022-01-14 02:16:09 +00:00
genImg(tweet.profile.getUserPic(size), class=prefs.getAvatarClass)
2019-08-12 15:02:07 +00:00
tdiv(class="tweet-name-row"):
tdiv(class="fullname-and-username"):
linkUser(tweet.profile, class="fullname")
linkUser(tweet.profile, class="username")
span(class="tweet-date"):
2020-06-01 00:22:22 +00:00
a(href=getLink(tweet), title=tweet.getTime):
text tweet.getShortTime
proc renderAlbum(tweet: Tweet): VNode =
let
groups = if tweet.photos.len < 3: @[tweet.photos]
else: tweet.photos.distribute(2)
2020-06-01 00:22:22 +00:00
buildHtml(tdiv(class="attachments")):
for i, photos in groups:
let margin = if i > 0: ".25em" else: ""
tdiv(class="gallery-row", style={marginTop: margin}):
for photo in photos:
tdiv(class="attachment image"):
let
named = "name=" in photo
orig = if named: photo else: photo & "?name=orig"
small = if named: photo else: photo & "?name=small"
a(href=getPicUrl(orig), class="still-image", target="_blank"):
genImg(small)
2019-08-19 01:28:04 +00:00
proc isPlaybackEnabled(prefs: Prefs; video: Video): bool =
case video.playbackType
of mp4: prefs.mp4Playback
of m3u8, vmap: prefs.hlsPlayback
proc renderVideoDisabled(video: Video; path: string): VNode =
2022-01-03 02:27:04 +00:00
buildHtml(tdiv(class="video-overlay")):
case video.playbackType
of mp4:
p: text "mp4 playback disabled in preferences"
of m3u8, vmap:
buttonReferer "/enablehls", "Enable hls playback", path
2019-08-19 01:28:04 +00:00
2019-08-19 20:03:00 +00:00
proc renderVideoUnavailable(video: Video): VNode =
2022-01-03 02:27:04 +00:00
buildHtml(tdiv(class="video-overlay")):
case video.reason
of "dmcaed":
p: text "This media has been disabled in response to a report by the copyright owner"
else:
p: text "This media is unavailable"
2019-08-19 20:03:00 +00:00
2019-12-06 14:15:56 +00:00
proc renderVideo*(video: Video; prefs: Prefs; path: string): VNode =
let container =
if video.description.len > 0 or video.title.len > 0: " card-container"
else: ""
2022-01-03 02:27:04 +00:00
buildHtml(tdiv(class="attachments card")):
tdiv(class="gallery-video" & container):
tdiv(class="attachment video-container"):
2020-06-07 06:26:39 +00:00
let thumb = getSmallPic(video.thumb)
2019-08-19 20:03:00 +00:00
if not video.available:
2022-01-03 02:27:04 +00:00
img(src=thumb)
2019-08-19 20:03:00 +00:00
renderVideoUnavailable(video)
elif not prefs.isPlaybackEnabled(video):
2022-01-03 02:27:04 +00:00
img(src=thumb)
renderVideoDisabled(video, path)
2019-08-19 20:03:00 +00:00
else:
let vid = video.variants.filterIt(it.contentType == video.playbackType)
2020-06-01 00:22:22 +00:00
let source = getVidUrl(vid[0].url)
2019-08-19 01:28:04 +00:00
case video.playbackType
of mp4:
if prefs.muteVideos:
video(poster=thumb, controls="", muted=""):
source(src=source, `type`="video/mp4")
else:
video(poster=thumb, controls=""):
source(src=source, `type`="video/mp4")
of m3u8, vmap:
2019-08-19 18:25:00 +00:00
video(poster=thumb, data-url=source, data-autoload="false")
verbatim "<div class=\"video-overlay\" onclick=\"playVideo(this)\">"
2019-11-12 09:57:28 +00:00
tdiv(class="overlay-circle"): span(class="overlay-triangle")
verbatim "</div>"
if container.len > 0:
tdiv(class="card-content"):
h2(class="card-title"): text video.title
if video.description.len > 0:
p(class="card-description"): text video.description
2019-08-13 17:44:29 +00:00
proc renderGif(gif: Gif; prefs: Prefs): VNode =
buildHtml(tdiv(class="attachments media-gif")):
2020-05-26 12:24:41 +00:00
tdiv(class="gallery-gif", style={maxHeight: "unset"}):
tdiv(class="attachment"):
2020-06-07 06:26:39 +00:00
let thumb = getSmallPic(gif.thumb)
let url = getPicUrl(gif.url)
2019-08-13 17:44:29 +00:00
if prefs.autoplayGifs:
2020-03-29 06:05:09 +00:00
video(class="gif", poster=thumb, controls="", autoplay="", muted="", loop=""):
2019-08-13 17:44:29 +00:00
source(src=url, `type`="video/mp4")
else:
video(class="gif", poster=thumb, controls="", muted="", loop=""):
source(src=url, `type`="video/mp4")
proc renderPoll(poll: Poll): VNode =
buildHtml(tdiv(class="poll")):
for i in 0 ..< poll.options.len:
2020-05-26 12:24:41 +00:00
let
leader = if poll.leader == i: " leader" else: ""
val = poll.values[i]
perc = if val > 0: val / poll.votes * 100 else: 0
2020-05-26 12:24:41 +00:00
percStr = (&"{perc:>3.0f}").strip(chars={'.'}) & '%'
tdiv(class=("poll-meter" & leader)):
2020-05-26 12:24:41 +00:00
span(class="poll-choice-bar", style={width: percStr})
span(class="poll-choice-value"): text percStr
span(class="poll-choice-option"): text poll.options[i]
span(class="poll-info"):
2020-05-26 12:24:41 +00:00
text insertSep($poll.votes, ',') & " votes • " & poll.status
2019-07-15 14:03:01 +00:00
proc renderCardImage(card: Card): VNode =
buildHtml(tdiv(class="card-image-container")):
tdiv(class="card-image"):
2020-06-01 00:22:22 +00:00
img(src=getPicUrl(card.image), alt="")
2019-07-15 14:03:01 +00:00
if card.kind == player:
tdiv(class="card-overlay"):
2019-08-19 19:27:28 +00:00
tdiv(class="overlay-circle"):
span(class="overlay-triangle")
2019-07-15 14:03:01 +00:00
2019-08-19 18:53:57 +00:00
proc renderCardContent(card: Card): VNode =
buildHtml(tdiv(class="card-content")):
h2(class="card-title"): text card.title
2020-06-10 14:13:40 +00:00
if card.text.len > 0:
p(class="card-description"): text card.text
if card.dest.len > 0:
span(class="card-destination"): text card.dest
2019-08-19 18:53:57 +00:00
proc renderCard(card: Card; prefs: Prefs; path: string): VNode =
const smallCards = {app, player, summary, storeLink}
2020-06-01 00:22:22 +00:00
let large = if card.kind notin smallCards: " large" else: ""
2021-12-27 01:27:49 +00:00
let url = replaceUrls(card.url, prefs)
2019-07-15 11:41:27 +00:00
buildHtml(tdiv(class=("card" & large))):
2019-08-19 18:53:57 +00:00
if card.video.isSome:
tdiv(class="card-container"):
renderVideo(get(card.video), prefs, path)
2019-08-19 18:53:57 +00:00
a(class="card-content-container", href=url):
renderCardContent(card)
else:
a(class="card-container", href=url):
2020-06-01 00:22:22 +00:00
if card.image.len > 0:
2019-08-19 18:53:57 +00:00
renderCardImage(card)
tdiv(class="card-content-container"):
renderCardContent(card)
2019-07-15 11:41:27 +00:00
func formatStat(stat: int): string =
if stat > 0: insertSep($stat, ',')
else: ""
2019-08-19 19:18:18 +00:00
proc renderStats(stats: TweetStats; views: string): VNode =
buildHtml(tdiv(class="tweet-stats")):
span(class="tweet-stat"): icon "comment", formatStat(stats.replies)
span(class="tweet-stat"): icon "retweet", formatStat(stats.retweets)
span(class="tweet-stat"): icon "quote", formatStat(stats.quotes)
span(class="tweet-stat"): icon "heart", formatStat(stats.likes)
2019-08-19 19:18:18 +00:00
if views.len > 0:
2020-05-26 12:24:41 +00:00
span(class="tweet-stat"): icon "play", insertSep(views, ',')
proc renderReply(tweet: Tweet): VNode =
buildHtml(tdiv(class="replying-to")):
text "Replying to "
for i, u in tweet.reply:
if i > 0: text " "
a(href=("/" & u)): text "@" & u
2022-01-14 02:16:09 +00:00
proc renderAttribution(profile: Profile; prefs: Prefs): VNode =
2019-10-26 14:37:58 +00:00
buildHtml(a(class="attribution", href=("/" & profile.username))):
2022-01-14 02:16:09 +00:00
renderMiniAvatar(profile, prefs)
2019-10-26 14:37:58 +00:00
strong: text profile.fullname
if profile.verified:
icon "ok", class="verified-icon", title="Verified account"
2019-10-26 14:37:58 +00:00
2019-12-21 04:07:50 +00:00
proc renderMediaTags(tags: seq[Profile]): VNode =
buildHtml(tdiv(class="media-tag-block")):
icon "user"
for i, p in tags:
a(class="media-tag", href=("/" & p.username), title=p.username):
text p.fullname
if i < tags.high:
text ", "
2020-06-01 00:22:22 +00:00
proc renderQuoteMedia(quote: Tweet; prefs: Prefs; path: string): VNode =
buildHtml(tdiv(class="quote-media-container")):
2020-06-01 00:22:22 +00:00
if quote.photos.len > 0:
renderAlbum(quote)
elif quote.video.isSome:
renderVideo(quote.video.get(), prefs, path)
elif quote.gif.isSome:
renderGif(quote.gif.get(), prefs)
proc renderQuote(quote: Tweet; prefs: Prefs; path: string): VNode =
if not quote.available:
return buildHtml(tdiv(class="quote unavailable")):
tdiv(class="unavailable-quote"):
if quote.tombstone.len > 0:
text quote.tombstone
elif quote.text.len > 0:
text quote.text
else:
text "This tweet is unavailable"
2020-06-01 00:22:22 +00:00
buildHtml(tdiv(class="quote quote-big")):
a(class="quote-link", href=getLink(quote))
2020-06-01 00:22:22 +00:00
tdiv(class="tweet-name-row"):
tdiv(class="fullname-and-username"):
2022-01-14 02:16:09 +00:00
renderMiniAvatar(quote.profile, prefs)
2020-06-01 00:22:22 +00:00
linkUser(quote.profile, class="fullname")
linkUser(quote.profile, class="username")
2020-06-01 00:22:22 +00:00
span(class="tweet-date"):
a(href=getLink(quote), title=quote.getTime):
text quote.getShortTime
if quote.reply.len > 0:
renderReply(quote)
2020-06-01 00:22:22 +00:00
if quote.text.len > 0:
2020-11-07 23:06:37 +00:00
tdiv(class="quote-text", dir="auto"):
2021-12-27 01:27:49 +00:00
verbatim replaceUrls(quote.text, prefs)
if quote.hasThread:
a(class="show-thread", href=getLink(quote)):
text "Show this thread"
2020-06-01 00:22:22 +00:00
if quote.photos.len > 0 or quote.video.isSome or quote.gif.isSome:
renderQuoteMedia(quote, prefs, path)
2019-12-21 04:44:58 +00:00
proc renderLocation*(tweet: Tweet): string =
let (place, url) = tweet.getLocation()
if place.len == 0: return
let node = buildHtml(span(class="tweet-geo")):
text " at "
if url.len > 1:
a(href=url): text place
else:
text place
return $node
2022-01-14 17:01:47 +00:00
proc renderEmbeddedTweet*(tweet: Tweet; cfg: Config; prefs: Prefs; path: string): VNode =
let fullTweet = tweet
var retweet: string
var tweet = fullTweet
if tweet.retweet.isSome:
tweet = tweet.retweet.get
retweet = fullTweet.profile.fullname
# handle unavailable
buildHtml(tdiv(class="timeline-item")):
renderHead(prefs, cfg)
tdiv(class="tweet-body"):
var views = ""
renderHeader(tweet, retweet, prefs)
var tweetClass = "tweet-content media-body"
if prefs.bidiSupport:
tweetClass &= " tweet-bidi"
tdiv(class=tweetClass, dir="auto"):
verbatim replaceUrls(tweet.text, prefs) & renderLocation(tweet)
if tweet.attribution.isSome:
renderAttribution(tweet.attribution.get(), prefs)
if tweet.card.isSome:
renderCard(tweet.card.get(), prefs, path)
if tweet.photos.len > 0:
renderAlbum(tweet)
elif tweet.video.isSome:
renderVideo(tweet.video.get(), prefs, path)
views = tweet.video.get().views
elif tweet.gif.isSome:
renderGif(tweet.gif.get(), prefs)
views = "GIF"
if tweet.poll.isSome:
renderPoll(tweet.poll.get())
if tweet.quote.isSome:
renderQuote(tweet.quote.get(), prefs, path)
p(class="tweet-published"): text getTime(tweet)
if tweet.mediaTags.len > 0:
renderMediaTags(tweet.mediaTags)
if not prefs.hideTweetStats:
renderStats(tweet.stats, views)
2020-06-01 00:22:22 +00:00
proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0;
last=false; showThread=false; mainTweet=false; afterTweet=false): VNode =
var divClass = class
2020-06-01 00:22:22 +00:00
if index == -1 or last:
divClass = "thread-last " & class
if not tweet.available:
2019-09-19 01:19:06 +00:00
return buildHtml(tdiv(class=divClass & "unavailable timeline-item")):
tdiv(class="unavailable-box"):
if tweet.tombstone.len > 0:
text tweet.tombstone
elif tweet.text.len > 0:
text tweet.text
2019-09-19 01:19:06 +00:00
else:
text "This tweet is unavailable"
if tweet.quote.isSome:
renderQuote(tweet.quote.get(), prefs, path)
2020-06-01 00:22:22 +00:00
let fullTweet = tweet
var retweet: string
var tweet = fullTweet
if tweet.retweet.isSome:
tweet = tweet.retweet.get
retweet = fullTweet.profile.fullname
2019-09-13 17:57:27 +00:00
buildHtml(tdiv(class=("timeline-item " & divClass))):
if not mainTweet:
a(class="tweet-link", href=getLink(tweet))
2019-09-13 17:57:27 +00:00
tdiv(class="tweet-body"):
var views = ""
renderHeader(tweet, retweet, prefs)
2019-09-13 17:57:27 +00:00
2020-06-01 00:22:22 +00:00
if not afterTweet and index == 0 and tweet.reply.len > 0 and
2020-05-26 12:24:41 +00:00
(tweet.reply.len > 1 or tweet.reply[0] != tweet.profile.username):
2019-09-13 17:57:27 +00:00
renderReply(tweet)
var tweetClass = "tweet-content media-body"
if prefs.bidiSupport:
tweetClass &= " tweet-bidi"
tdiv(class=tweetClass, dir="auto"):
2021-12-27 01:27:49 +00:00
verbatim replaceUrls(tweet.text, prefs) & renderLocation(tweet)
2019-09-13 17:57:27 +00:00
2019-10-26 14:37:58 +00:00
if tweet.attribution.isSome:
2022-01-14 02:16:09 +00:00
renderAttribution(tweet.attribution.get(), prefs)
2019-10-26 14:37:58 +00:00
2019-09-13 17:57:27 +00:00
if tweet.card.isSome:
renderCard(tweet.card.get(), prefs, path)
2020-06-01 00:22:22 +00:00
if tweet.photos.len > 0:
2019-09-13 17:57:27 +00:00
renderAlbum(tweet)
elif tweet.video.isSome:
renderVideo(tweet.video.get(), prefs, path)
views = tweet.video.get().views
elif tweet.gif.isSome:
renderGif(tweet.gif.get(), prefs)
2020-03-29 06:05:09 +00:00
views = "GIF"
2020-06-01 00:22:22 +00:00
if tweet.poll.isSome:
2019-09-13 17:57:27 +00:00
renderPoll(tweet.poll.get())
2020-05-26 12:24:41 +00:00
if tweet.quote.isSome:
2020-06-01 00:22:22 +00:00
renderQuote(tweet.quote.get(), prefs, path)
2020-05-26 12:24:41 +00:00
if mainTweet:
p(class="tweet-published"): text getTime(tweet)
2019-12-21 04:07:50 +00:00
if tweet.mediaTags.len > 0:
renderMediaTags(tweet.mediaTags)
2019-09-13 17:57:27 +00:00
if not prefs.hideTweetStats:
renderStats(tweet.stats, views)
2019-09-19 01:51:15 +00:00
if showThread:
a(class="show-thread", href=("/i/status/" & $tweet.threadId)):
2019-09-13 17:57:27 +00:00
text "Show this thread"