rss.nimf 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #? stdtmpl(subsChar = '$', metaChar = '#')
  2. ## SPDX-License-Identifier: AGPL-3.0-only
  3. #import strutils, xmltree, strformat, options, unicode
  4. #import ../types, ../utils, ../formatters, ../prefs
  5. ## Snowflake ID cutoff for RSS GUID format transition
  6. ## Corresponds to approximately December 14, 2025 UTC
  7. #const guidCutoff = 2000000000000000000'i64
  8. #
  9. #proc getTitle(tweet: Tweet; retweet: string): string =
  10. #if tweet.pinned: result = "Pinned: "
  11. #elif retweet.len > 0: result = &"RT by @{retweet}: "
  12. #elif tweet.reply.len > 0: result = &"R to @{tweet.reply[0]}: "
  13. #end if
  14. #var text = stripHtml(tweet.text)
  15. ##if unicode.runeLen(text) > 32:
  16. ## text = unicode.runeSubStr(text, 0, 32) & "..."
  17. ##end if
  18. #result &= xmltree.escape(text)
  19. #if result.len > 0: return
  20. #end if
  21. #if tweet.photos.len > 0:
  22. # result &= "Image"
  23. #elif tweet.video.isSome:
  24. # result &= "Video"
  25. #elif tweet.gif.isSome:
  26. # result &= "Gif"
  27. #end if
  28. #end proc
  29. #
  30. #proc getDescription(desc: string; cfg: Config): string =
  31. Twitter feed for: ${desc}. Generated by ${getUrlPrefix(cfg)}
  32. #end proc
  33. #
  34. #proc getTweetsWithPinned(profile: Profile): seq[Tweets] =
  35. #result = profile.tweets.content
  36. #if profile.pinned.isSome and result.len > 0:
  37. # let pinnedTweet = profile.pinned.get
  38. # var inserted = false
  39. # for threadIdx in 0 ..< result.len:
  40. # if not inserted:
  41. # for tweetIdx in 0 ..< result[threadIdx].len:
  42. # if result[threadIdx][tweetIdx].id < pinnedTweet.id:
  43. # result[threadIdx].insert(pinnedTweet, tweetIdx)
  44. # inserted = true
  45. # end if
  46. # end for
  47. # end if
  48. # end for
  49. #end if
  50. #end proc
  51. #
  52. #proc renderRssTweet(tweet: Tweet; cfg: Config): string =
  53. #let tweet = tweet.retweet.get(tweet)
  54. #let urlPrefix = getUrlPrefix(cfg)
  55. #let text = replaceUrls(tweet.text, defaultPrefs, absolute=urlPrefix)
  56. <p>${text.replace("\n", "<br>\n")}</p>
  57. #if tweet.photos.len > 0:
  58. # for photo in tweet.photos:
  59. <img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
  60. # end for
  61. #elif tweet.video.isSome:
  62. <a href="${urlPrefix}${tweet.getLink}">
  63. <br>Video<br>
  64. <img src="${urlPrefix}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" />
  65. </a>
  66. #elif tweet.gif.isSome:
  67. # let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}"
  68. # let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}"
  69. <video poster="${thumb}" autoplay muted loop style="max-width:250px;">
  70. <source src="${url}" type="video/mp4"></video>
  71. #elif tweet.card.isSome:
  72. # let card = tweet.card.get()
  73. # if card.image.len > 0:
  74. <img src="${urlPrefix}${getPicUrl(card.image)}" style="max-width:250px;" />
  75. # end if
  76. #end if
  77. #if tweet.quote.isSome and get(tweet.quote).available:
  78. # let quoteTweet = get(tweet.quote)
  79. # let quoteLink = urlPrefix & getLink(quoteTweet)
  80. <hr/>
  81. <blockquote>
  82. <b>${quoteTweet.user.fullname} (@${quoteTweet.user.username})</b>
  83. <p>
  84. ${renderRssTweet(quoteTweet, cfg)}
  85. </p>
  86. <footer>
  87. — <cite><a href="${quoteLink}">${quoteLink}</a>
  88. </footer>
  89. </blockquote>
  90. #end if
  91. #end proc
  92. #
  93. #proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; userId=""): string =
  94. #let urlPrefix = getUrlPrefix(cfg)
  95. #var links: seq[string]
  96. #for thread in tweets:
  97. # for tweet in thread:
  98. # if userId.len > 0 and tweet.user.id != userId: continue
  99. # end if
  100. #
  101. # let retweet = if tweet.retweet.isSome: tweet.user.username else: ""
  102. # let tweet = if retweet.len > 0: tweet.retweet.get else: tweet
  103. # let link = getLink(tweet)
  104. # if link in links: continue
  105. # end if
  106. # links.add link
  107. # let useGlobalGuid = tweet.id >= guidCutoff
  108. <item>
  109. <title>${getTitle(tweet, retweet)}</title>
  110. <dc:creator>@${tweet.user.username}</dc:creator>
  111. <description><![CDATA[${renderRssTweet(tweet, cfg).strip(chars={'\n'})}]]></description>
  112. <pubDate>${getRfc822Time(tweet)}</pubDate>
  113. #if useGlobalGuid:
  114. <guid isPermaLink="false">${tweet.id}</guid>
  115. #else:
  116. <guid>${urlPrefix & link}</guid>
  117. #end if
  118. <link>${urlPrefix & link}</link>
  119. </item>
  120. # end for
  121. #end for
  122. #end proc
  123. #
  124. #proc renderTimelineRss*(profile: Profile; cfg: Config; multi=false): string =
  125. #let urlPrefix = getUrlPrefix(cfg)
  126. #result = ""
  127. #let handle = (if multi: "" else: "@") & profile.user.username
  128. #var title = profile.user.fullname
  129. #if not multi: title &= " / " & handle
  130. #end if
  131. #title = xmltree.escape(title).sanitizeXml
  132. <?xml version="1.0" encoding="UTF-8"?>
  133. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  134. <channel>
  135. <atom:link href="${urlPrefix}/${profile.user.username}/rss" rel="self" type="application/rss+xml" />
  136. <title>${title}</title>
  137. <link>${urlPrefix}/${profile.user.username}</link>
  138. <description>${getDescription(handle, cfg)}</description>
  139. <language>en-us</language>
  140. <ttl>40</ttl>
  141. <image>
  142. <title>${title}</title>
  143. <link>${urlPrefix}/${profile.user.username}</link>
  144. <url>${urlPrefix}${getPicUrl(profile.user.getUserPic(style="_400x400"))}</url>
  145. <width>128</width>
  146. <height>128</height>
  147. </image>
  148. #let tweetsList = getTweetsWithPinned(profile)
  149. #if tweetsList.len > 0:
  150. ${renderRssTweets(tweetsList, cfg, userId=profile.user.id)}
  151. #end if
  152. </channel>
  153. </rss>
  154. #end proc
  155. #
  156. #proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config): string =
  157. #let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}"
  158. #result = ""
  159. <?xml version="1.0" encoding="UTF-8"?>
  160. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  161. <channel>
  162. <atom:link href="${link}" rel="self" type="application/rss+xml" />
  163. <title>${xmltree.escape(list.name)} / @${list.username}</title>
  164. <link>${link}</link>
  165. <description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description>
  166. <language>en-us</language>
  167. <ttl>40</ttl>
  168. ${renderRssTweets(tweets, cfg)}
  169. </channel>
  170. </rss>
  171. #end proc
  172. #
  173. #proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config): string =
  174. #let link = &"{getUrlPrefix(cfg)}/search"
  175. #let escName = xmltree.escape(name)
  176. #result = ""
  177. <?xml version="1.0" encoding="UTF-8"?>
  178. <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  179. <channel>
  180. <atom:link href="${link}" rel="self" type="application/rss+xml" />
  181. <title>Search results for "${escName}"</title>
  182. <link>${link}</link>
  183. <description>${getDescription(&"Search \"{escName}\"", cfg)}</description>
  184. <language>en-us</language>
  185. <ttl>40</ttl>
  186. ${renderRssTweets(tweets, cfg)}
  187. </channel>
  188. </rss>
  189. #end proc