| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #? stdtmpl(subsChar = '$', metaChar = '#')
- ## SPDX-License-Identifier: AGPL-3.0-only
- #import strutils, xmltree, strformat, options, unicode
- #import ../types, ../utils, ../formatters, ../prefs
- ## Snowflake ID cutoff for RSS GUID format transition
- ## Corresponds to approximately December 14, 2025 UTC
- #const guidCutoff = 2000000000000000000'i64
- #
- #proc getTitle(tweet: Tweet; retweet: string): string =
- #if tweet.pinned: result = "Pinned: "
- #elif retweet.len > 0: result = &"RT by @{retweet}: "
- #elif tweet.reply.len > 0: result = &"R to @{tweet.reply[0]}: "
- #end if
- #var text = stripHtml(tweet.text)
- ##if unicode.runeLen(text) > 32:
- ## text = unicode.runeSubStr(text, 0, 32) & "..."
- ##end if
- #result &= xmltree.escape(text)
- #if result.len > 0: return
- #end if
- #if tweet.photos.len > 0:
- # result &= "Image"
- #elif tweet.video.isSome:
- # result &= "Video"
- #elif tweet.gif.isSome:
- # result &= "Gif"
- #end if
- #end proc
- #
- #proc getDescription(desc: string; cfg: Config): string =
- Twitter feed for: ${desc}. Generated by ${getUrlPrefix(cfg)}
- #end proc
- #
- #proc getTweetsWithPinned(profile: Profile): seq[Tweets] =
- #result = profile.tweets.content
- #if profile.pinned.isSome and result.len > 0:
- # let pinnedTweet = profile.pinned.get
- # var inserted = false
- # for threadIdx in 0 ..< result.len:
- # if not inserted:
- # for tweetIdx in 0 ..< result[threadIdx].len:
- # if result[threadIdx][tweetIdx].id < pinnedTweet.id:
- # result[threadIdx].insert(pinnedTweet, tweetIdx)
- # inserted = true
- # end if
- # end for
- # end if
- # end for
- #end if
- #end proc
- #
- #proc renderRssTweet(tweet: Tweet; cfg: Config): string =
- #let tweet = tweet.retweet.get(tweet)
- #let urlPrefix = getUrlPrefix(cfg)
- #let text = replaceUrls(tweet.text, defaultPrefs, absolute=urlPrefix)
- <p>${text.replace("\n", "<br>\n")}</p>
- #if tweet.photos.len > 0:
- # for photo in tweet.photos:
- <img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
- # end for
- #elif tweet.video.isSome:
- <a href="${urlPrefix}${tweet.getLink}">
- <br>Video<br>
- <img src="${urlPrefix}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" />
- </a>
- #elif tweet.gif.isSome:
- # let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}"
- # let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}"
- <video poster="${thumb}" autoplay muted loop style="max-width:250px;">
- <source src="${url}" type="video/mp4"></video>
- #elif tweet.card.isSome:
- # let card = tweet.card.get()
- # if card.image.len > 0:
- <img src="${urlPrefix}${getPicUrl(card.image)}" style="max-width:250px;" />
- # end if
- #end if
- #if tweet.quote.isSome and get(tweet.quote).available:
- # let quoteTweet = get(tweet.quote)
- # let quoteLink = urlPrefix & getLink(quoteTweet)
- <hr/>
- <blockquote>
- <b>${quoteTweet.user.fullname} (@${quoteTweet.user.username})</b>
- <p>
- ${renderRssTweet(quoteTweet, cfg)}
- </p>
- <footer>
- — <cite><a href="${quoteLink}">${quoteLink}</a>
- </footer>
- </blockquote>
- #end if
- #end proc
- #
- #proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; userId=""): string =
- #let urlPrefix = getUrlPrefix(cfg)
- #var links: seq[string]
- #for thread in tweets:
- # for tweet in thread:
- # if userId.len > 0 and tweet.user.id != userId: continue
- # end if
- #
- # let retweet = if tweet.retweet.isSome: tweet.user.username else: ""
- # let tweet = if retweet.len > 0: tweet.retweet.get else: tweet
- # let link = getLink(tweet)
- # if link in links: continue
- # end if
- # links.add link
- # let useGlobalGuid = tweet.id >= guidCutoff
- <item>
- <title>${getTitle(tweet, retweet)}</title>
- <dc:creator>@${tweet.user.username}</dc:creator>
- <description><![CDATA[${renderRssTweet(tweet, cfg).strip(chars={'\n'})}]]></description>
- <pubDate>${getRfc822Time(tweet)}</pubDate>
- #if useGlobalGuid:
- <guid isPermaLink="false">${tweet.id}</guid>
- #else:
- <guid>${urlPrefix & link}</guid>
- #end if
- <link>${urlPrefix & link}</link>
- </item>
- # end for
- #end for
- #end proc
- #
- #proc renderTimelineRss*(profile: Profile; cfg: Config; multi=false): string =
- #let urlPrefix = getUrlPrefix(cfg)
- #result = ""
- #let handle = (if multi: "" else: "@") & profile.user.username
- #var title = profile.user.fullname
- #if not multi: title &= " / " & handle
- #end if
- #title = xmltree.escape(title).sanitizeXml
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
- <channel>
- <atom:link href="${urlPrefix}/${profile.user.username}/rss" rel="self" type="application/rss+xml" />
- <title>${title}</title>
- <link>${urlPrefix}/${profile.user.username}</link>
- <description>${getDescription(handle, cfg)}</description>
- <language>en-us</language>
- <ttl>40</ttl>
- <image>
- <title>${title}</title>
- <link>${urlPrefix}/${profile.user.username}</link>
- <url>${urlPrefix}${getPicUrl(profile.user.getUserPic(style="_400x400"))}</url>
- <width>128</width>
- <height>128</height>
- </image>
- #let tweetsList = getTweetsWithPinned(profile)
- #if tweetsList.len > 0:
- ${renderRssTweets(tweetsList, cfg, userId=profile.user.id)}
- #end if
- </channel>
- </rss>
- #end proc
- #
- #proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config): string =
- #let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}"
- #result = ""
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
- <channel>
- <atom:link href="${link}" rel="self" type="application/rss+xml" />
- <title>${xmltree.escape(list.name)} / @${list.username}</title>
- <link>${link}</link>
- <description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description>
- <language>en-us</language>
- <ttl>40</ttl>
- ${renderRssTweets(tweets, cfg)}
- </channel>
- </rss>
- #end proc
- #
- #proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config): string =
- #let link = &"{getUrlPrefix(cfg)}/search"
- #let escName = xmltree.escape(name)
- #result = ""
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
- <channel>
- <atom:link href="${link}" rel="self" type="application/rss+xml" />
- <title>Search results for "${escName}"</title>
- <link>${link}</link>
- <description>${getDescription(&"Search \"{escName}\"", cfg)}</description>
- <language>en-us</language>
- <ttl>40</ttl>
- ${renderRssTweets(tweets, cfg)}
- </channel>
- </rss>
- #end proc
|