tweet.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #? stdtmpl(subsChar = '$', metaChar = '#')
  2. #import xmltree, strutils, times, sequtils, uri
  3. #import ../types, ../formatters, ../utils
  4. #
  5. #proc renderHeading(tweet: Tweet): string =
  6. #if tweet.retweetBy.isSome:
  7. <div class="retweet">
  8. <span>🔄 ${tweet.retweetBy.get()} retweeted</span>
  9. </div>
  10. #end if
  11. #if tweet.pinned:
  12. <div class="pinned">
  13. <span>📌 Pinned Tweet</span>
  14. </div>
  15. #end if
  16. <div class="media-heading">
  17. <div class="heading-name-row">
  18. <img class="avatar" src=${tweet.profile.getUserpic("_bigger").getSigUrl("pic")}>
  19. <div class="name-and-account-name">
  20. ${linkUser(tweet.profile, "h4", class="username", username=false)}
  21. ${linkUser(tweet.profile, "", class="account-name")}
  22. </div>
  23. <span class="heading-right">
  24. <a href="${tweet.link}" class="timeago faint-link">
  25. <time title="${tweet.time.format("d/M/yyyy', ' HH:mm:ss")}">${tweet.shortTime}</time>
  26. </a>
  27. </span>
  28. </div>
  29. </div>
  30. #end proc
  31. #
  32. #proc renderMediaGroup(tweet: Tweet): string =
  33. #let groups = if tweet.photos.len > 2: tweet.photos.distribute(2) else: @[tweet.photos]
  34. #let groupStyle = if groups.len == 1 and groups[0].len < 2: "" else: "background-color: #0f0f0f;"
  35. #var first = true
  36. <div class="attachments media-body" style="${groupStyle}">
  37. #for photos in groups:
  38. #let style = if first: "" else: "margin-top: .25em;"
  39. <div class="gallery-row cover-fit" style="${style}">
  40. #for photo in photos:
  41. <div class="attachment image">
  42. ##TODO: why doesn't this work?
  43. <a href=${getSigUrl(photo & ":large", "pic")} target="_blank" class="image-attachment">
  44. #let style = if photos.len > 1 or groups.len > 1: "display: flex;" else: ""
  45. #let istyle = if photos.len > 1 or groups.len > 1: "" else: "border-radius: 7px;"
  46. <div class="still-image" style="${style}">
  47. <img src=${getSigUrl(photo, "pic")} referrerpolicy="" style="${istyle}">
  48. </div>
  49. </a>
  50. </div>
  51. #end for
  52. </div>
  53. #first = false
  54. #end for
  55. </div>
  56. #end proc
  57. #
  58. #proc renderVideo(tweet: Tweet): string =
  59. <div class="attachments media-body">
  60. <div class="gallery-row" style="max-height: unset;">
  61. <div class="attachment image">
  62. <video poster=${tweet.videoThumb.get()} style="width: 100%; height: 100%;" autoplay muted loop></video>
  63. <div class="video-overlay">
  64. <p>Video playback not supported</p>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. #end proc
  70. #
  71. #proc renderGif(tweet: Tweet): string =
  72. #let thumbUrl = getGifThumb(tweet).getSigUrl("pic")
  73. #let videoUrl = getGifSrc(tweet).getSigUrl("video")
  74. <div class="attachments media-body">
  75. <div class="gallery-row" style="max-height: unset;">
  76. <div class="attachment image">
  77. <video poster=${thumbUrl} style="width: 100%; height: 100%;" autoplay muted loop>
  78. <source src=${videoUrl} type="video/mp4">
  79. </video>
  80. </div>
  81. </div>
  82. </div>
  83. #end proc
  84. #
  85. #proc renderStats(tweet: Tweet): string =
  86. <div class="tweet-stats">
  87. <span class="tweet-stat">💬 ${$tweet.replies}</span>
  88. <span class="tweet-stat">🔄 ${$tweet.retweets}</span>
  89. <span class="tweet-stat">👍 ${$tweet.likes}</span>
  90. </div>
  91. #end proc
  92. #
  93. #proc renderTweet*(tweet: Tweet; class=""): string =
  94. #if class.len > 0:
  95. <div class="${class}">
  96. #end if
  97. <div class="status-el">
  98. <div class="status-body">
  99. ${renderHeading(tweet)}
  100. <div class="status-content-wrapper">
  101. <div class="status-content media-body">
  102. ${linkifyText(xmltree.escape(tweet.text))}
  103. </div>
  104. </div>
  105. #if tweet.photos.len > 0:
  106. ${renderMediaGroup(tweet)}
  107. #elif tweet.videoThumb.isSome:
  108. ${renderVideo(tweet)}
  109. #elif tweet.gif.isSome:
  110. ${renderGif(tweet)}
  111. #end if
  112. ${renderStats(tweet)}
  113. </div>
  114. </div>
  115. #if class.len > 0:
  116. </div>
  117. #end if
  118. #end proc