tweet.nim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import strutils, sequtils
  2. import karax/[karaxdsl, vdom, vstyles]
  3. import renderutils
  4. import ../types, ../utils, ../formatters
  5. proc renderHeader(tweet: Tweet): VNode =
  6. buildHtml(tdiv):
  7. if tweet.retweet.isSome:
  8. tdiv(class="retweet"):
  9. span: icon "retweet-1", get(tweet.retweet).by & " retweeted"
  10. if tweet.pinned:
  11. tdiv(class="pinned"):
  12. span: icon "pin", "Pinned Tweet"
  13. tdiv(class="tweet-header"):
  14. a(class="tweet-avatar", href=("/" & tweet.profile.username)):
  15. genImg(tweet.profile.getUserpic("_bigger"), class="avatar")
  16. tdiv(class="tweet-name-row"):
  17. tdiv(class="fullname-and-username"):
  18. linkUser(tweet.profile, class="fullname")
  19. linkUser(tweet.profile, class="username")
  20. span(class="tweet-date"):
  21. a(href=getLink(tweet), title=tweet.getTime()):
  22. text tweet.shortTime
  23. proc renderAlbum(tweet: Tweet): VNode =
  24. let
  25. groups = if tweet.photos.len < 3: @[tweet.photos]
  26. else: tweet.photos.distribute(2)
  27. class = if groups.len == 1 and groups[0].len == 1: "single-image"
  28. else: ""
  29. buildHtml(tdiv(class=("attachments " & class))):
  30. for i, photos in groups:
  31. let margin = if i > 0: ".25em" else: ""
  32. let flex = if photos.len > 1 or groups.len > 1: "flex" else: "block"
  33. tdiv(class="gallery-row", style={marginTop: margin}):
  34. for photo in photos:
  35. tdiv(class="attachment image"):
  36. a(href=getSigUrl(photo & "?name=orig", "pic"), class="still-image",
  37. target="_blank", style={display: flex}):
  38. genImg(photo)
  39. proc isPlaybackEnabled(prefs: Prefs; video: Video): bool =
  40. case video.playbackType
  41. of mp4: prefs.mp4Playback
  42. of m3u8, vmap: prefs.hlsPlayback
  43. proc renderVideoDisabled(video: Video): VNode =
  44. buildHtml(tdiv):
  45. img(src=video.thumb.getSigUrl("pic"))
  46. tdiv(class="video-overlay"):
  47. case video.playbackType
  48. of mp4:
  49. p: text "mp4 playback disabled in preferences"
  50. of m3u8, vmap:
  51. p: text "hls playback disabled in preferences"
  52. proc renderVideo(video: Video; prefs: Prefs): VNode =
  53. buildHtml(tdiv(class="attachments")):
  54. tdiv(class="gallery-video"):
  55. tdiv(class="attachment video-container"):
  56. if prefs.isPlaybackEnabled(video):
  57. let thumb = video.thumb.getSigUrl("pic")
  58. let source = video.url.getSigUrl("video")
  59. case video.playbackType
  60. of mp4:
  61. if prefs.muteVideos:
  62. video(poster=thumb, controls="", muted=""):
  63. source(src=source, `type`="video/mp4")
  64. else:
  65. video(poster=thumb, controls=""):
  66. source(src=source, `type`="video/mp4")
  67. of m3u8, vmap:
  68. video(poster=thumb)
  69. tdiv(class="video-overlay"):
  70. p: text "Video playback not supported yet"
  71. else:
  72. renderVideoDisabled(video)
  73. proc renderGif(gif: Gif; prefs: Prefs): VNode =
  74. buildHtml(tdiv(class="attachments media-gif")):
  75. tdiv(class="gallery-gif", style=style(maxHeight, "unset")):
  76. tdiv(class="attachment"):
  77. let thumb = gif.thumb.getSigUrl("pic")
  78. let url = gif.url.getSigUrl("video")
  79. if prefs.autoplayGifs:
  80. video(class="gif", poster=thumb, autoplay="", muted="", loop=""):
  81. source(src=url, `type`="video/mp4")
  82. else:
  83. video(class="gif", poster=thumb, controls="", muted="", loop=""):
  84. source(src=url, `type`="video/mp4")
  85. proc renderPoll(poll: Poll): VNode =
  86. buildHtml(tdiv(class="poll")):
  87. for i in 0 ..< poll.options.len:
  88. let leader = if poll.leader == i: " leader" else: ""
  89. let perc = $poll.values[i] & "%"
  90. tdiv(class=("poll-meter" & leader)):
  91. span(class="poll-choice-bar", style=style(width, perc))
  92. span(class="poll-choice-value"): text perc
  93. span(class="poll-choice-option"): text poll.options[i]
  94. span(class="poll-info"):
  95. text $poll.votes & " votes • " & poll.status
  96. proc renderCardImage(card: Card): VNode =
  97. buildHtml(tdiv(class="card-image-container")):
  98. tdiv(class="card-image"):
  99. img(src=getSigUrl(get(card.image), "pic"))
  100. if card.kind == player:
  101. tdiv(class="card-overlay"):
  102. tdiv(class="card-overlay-circle"):
  103. span(class="card-overlay-triangle")
  104. proc renderCard(card: Card; prefs: Prefs): VNode =
  105. const largeCards = {summaryLarge, liveEvent, promoWebsite, promoVideo}
  106. let large = if card.kind in largeCards: " large" else: ""
  107. buildHtml(tdiv(class=("card" & large))):
  108. a(class="card-container", href=replaceUrl(card.url, prefs)):
  109. if card.image.isSome:
  110. renderCardImage(card)
  111. elif card.video.isSome:
  112. renderVideo(get(card.video), prefs)
  113. tdiv(class="card-content-container"):
  114. tdiv(class="card-content"):
  115. h2(class="card-title"): text card.title
  116. p(class="card-description"): text card.text
  117. span(class="card-destination"): text card.dest
  118. proc renderStats(stats: TweetStats): VNode =
  119. buildHtml(tdiv(class="tweet-stats")):
  120. span(class="tweet-stat"): icon "comment", $stats.replies
  121. span(class="tweet-stat"): icon "retweet-1", $stats.retweets
  122. span(class="tweet-stat"): icon "thumbs-up-alt", $stats.likes
  123. proc renderReply(tweet: Tweet): VNode =
  124. buildHtml(tdiv(class="replying-to")):
  125. text "Replying to "
  126. for i, u in tweet.reply:
  127. if i > 0: text " "
  128. a(href=("/" & u)): text "@" & u
  129. proc renderReply(quote: Quote): VNode =
  130. buildHtml(tdiv(class="replying-to")):
  131. text "Replying to "
  132. for i, u in quote.reply:
  133. if i > 0: text " "
  134. a(href=("/" & u)): text "@" & u
  135. proc renderQuoteMedia(quote: Quote): VNode =
  136. buildHtml(tdiv(class="quote-media-container")):
  137. if quote.thumb.len > 0:
  138. tdiv(class="quote-media"):
  139. genImg(quote.thumb)
  140. if quote.badge.len > 0:
  141. tdiv(class="quote-badge"):
  142. tdiv(class="quote-badge-text"): text quote.badge
  143. elif quote.sensitive:
  144. tdiv(class="quote-sensitive"):
  145. icon "attention", class="quote-sensitive-icon"
  146. proc renderQuote(quote: Quote; prefs: Prefs): VNode =
  147. if not quote.available:
  148. return buildHtml(tdiv(class="quote unavailable")):
  149. tdiv(class="unavailable-quote"):
  150. text "This tweet is unavailable"
  151. buildHtml(tdiv(class="quote")):
  152. a(class="quote-link", href=getLink(quote))
  153. if quote.thumb.len > 0 or quote.sensitive:
  154. renderQuoteMedia(quote)
  155. tdiv(class="fullname-and-username"):
  156. linkUser(quote.profile, class="fullname")
  157. linkUser(quote.profile, class="username")
  158. if quote.reply.len > 0:
  159. renderReply(quote)
  160. tdiv(class="quote-text"):
  161. verbatim linkifyText(quote.text, prefs)
  162. if quote.hasThread:
  163. a(class="show-thread", href=getLink(quote)):
  164. text "Show this thread"
  165. proc renderTweet*(tweet: Tweet; prefs: Prefs; class="";
  166. index=0; total=(-1); last=false): VNode =
  167. var divClass = class
  168. if index == total or last:
  169. divClass = "thread-last " & class
  170. if not tweet.available:
  171. return buildHtml(tdiv(class=divClass)):
  172. tdiv(class="status-el unavailable"):
  173. tdiv(class="unavailable-box"):
  174. text "This tweet is unavailable"
  175. buildHtml(tdiv(class=divClass)):
  176. tdiv(class="status-el"):
  177. tdiv(class="status-body"):
  178. renderHeader(tweet)
  179. if index == 0 and tweet.reply.len > 0:
  180. renderReply(tweet)
  181. tdiv(class="status-content media-body"):
  182. verbatim linkifyText(tweet.text, prefs)
  183. if tweet.quote.isSome:
  184. renderQuote(tweet.quote.get(), prefs)
  185. if tweet.card.isSome:
  186. renderCard(tweet.card.get(), prefs)
  187. elif tweet.photos.len > 0:
  188. renderAlbum(tweet)
  189. elif tweet.video.isSome:
  190. renderVideo(tweet.video.get(), prefs)
  191. elif tweet.gif.isSome:
  192. renderGif(tweet.gif.get(), prefs)
  193. elif tweet.poll.isSome:
  194. renderPoll(tweet.poll.get())
  195. if not prefs.hideTweetStats:
  196. renderStats(tweet.stats)
  197. if tweet.hasThread and "timeline" in class:
  198. a(class="show-thread", href=getLink(tweet)):
  199. text "Show this thread"