Show video link in RSS feed

This commit is contained in:
blankie 2023-06-06 17:09:06 +07:00
parent cd1fd532e6
commit 4e0b16d2bc
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 67 additions and 17 deletions

View File

@ -15,7 +15,7 @@ proc redisKey*(page, name, cursor: string): string =
if cursor.len > 0:
result &= ":" & cursor
proc timelineRss*(req: Request; cfg: Config; query: Query; tab, param: string): Future[Rss] {.async.} =
proc timelineRss*(req: Request; cfg: Config; query: Query; prefs: Prefs; tab, param: string): Future[Rss] {.async.} =
var profile: Profile
let
name = req.params.getOrDefault("name")
@ -39,7 +39,7 @@ proc timelineRss*(req: Request; cfg: Config; query: Query; tab, param: string):
return Rss(feed: profile.user.username, cursor: "suspended")
if profile.user.fullname.len > 0:
let rss = renderTimelineRss(profile, cfg, tab, param, multi=(names.len > 1))
let rss = renderTimelineRss(profile, cfg, prefs, tab, param, multi=(names.len > 1))
return Rss(feed: rss, cursor: profile.tweets.bottom)
template respRss*(rss, page) =
@ -78,7 +78,7 @@ proc createRssRouter*(cfg: Config) =
let tweets = await getGraphTweetSearch(query, cursor)
rss.cursor = tweets.bottom
rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg)
rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg, cookiePrefs())
await cacheRss(key, rss)
respRss(rss, "Search")
@ -94,7 +94,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0:
respRss(rss, "User")
rss = await timelineRss(request, cfg, Query(fromUser: @[name]), "", "")
rss = await timelineRss(request, cfg, Query(fromUser: @[name]), cookiePrefs(), "", "")
await cacheRss(key, rss)
respRss(rss, "User")
@ -124,7 +124,7 @@ proc createRssRouter*(cfg: Config) =
if rss.cursor.len > 0:
respRss(rss, "User")
rss = await timelineRss(request, cfg, query, tab, param)
rss = await timelineRss(request, cfg, query, cookiePrefs(), tab, param)
await cacheRss(key, rss)
respRss(rss, "User")
@ -161,7 +161,7 @@ proc createRssRouter*(cfg: Config) =
list = await getCachedList(id=id)
timeline = await getGraphListTweets(list.id, cursor)
rss.cursor = timeline.bottom
rss.feed = renderListRss(timeline.content, list, cfg)
rss.feed = renderListRss(timeline.content, list, cfg, cookiePrefs())
await cacheRss(key, rss)
respRss(rss, "List")

View File

@ -1,6 +1,6 @@
#? stdtmpl(subsChar = '$', metaChar = '#')
## SPDX-License-Identifier: AGPL-3.0-only
#import strutils, xmltree, strformat, options, unicode
#import strutils, xmltree, strformat, options, unicode, algorithm
#import ../types, ../utils, ../formatters, ../prefs
#
#proc getTitle(tweet: Tweet; retweet: string): string =
@ -28,7 +28,37 @@
Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end proc
#
#proc renderRssTweet(tweet: Tweet; cfg: Config): string =
## Copied from views/tweet.nim
#proc isPlaybackEnabled(prefs: Prefs; playbackType: VideoType): bool =
# case playbackType
# of mp4: return prefs.mp4Playback
# of m3u8, vmap: return prefs.hlsPlayback
# end case
#end proc
#
#proc hasMp4Url(video: Video): bool =
# video.variants.anyIt(it.contentType == mp4)
#end proc
#
#proc getVideoDisabledText(playbackType: VideoType): string =
# case playbackType
# of mp4:
mp4 playback disabled in preferences
# of m3u8, vmap:
hls playback disabled in preferences
# end case
#end proc
#
#proc getVideoUnavailableText(video: Video): string =
# case video.reason
# of "dmcaed":
This media has been disabled in response to a report by the copyright owner
# else:
This media is unavailable
# end case
#end proc
#
#proc renderRssTweet(tweet: Tweet; cfg: Config; prefs: Prefs): string =
#let tweet = tweet.retweet.get(tweet)
#let urlPrefix = getUrlPrefix(cfg)
#let text = replaceUrls(tweet.text, defaultPrefs, absolute=urlPrefix)
@ -42,7 +72,27 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<img src="${urlPrefix}${getPicUrl(photo)}" style="max-width:250px;" />
# end for
#elif tweet.video.isSome:
<img src="${urlPrefix}${getPicUrl(get(tweet.video).thumb)}" style="max-width:250px;" />
# let video = get(tweet.video)
# let thumb = &"{urlPrefix}{getPicUrl(video.thumb)}"
# let playbackType = if not prefs.proxyVideos and video.hasMp4Url: mp4
# else: video.playbackType
# if not video.available:
<img src="${thumb}" style="max-width:250px;" />
<p>${getVideoUnavailableText(video)}</p>
# elif not isPlaybackEnabled(prefs, playbackType):
<img src="${thumb}" style="max-width:250px;" />
<p>${getVideoDisabledText(playbackType)}</p>
# else:
# let vars = video.variants.filterIt(it.contentType == playbackType)
# let vidUrl = vars.sortedByIt(it.resolution)[^1].url
# let source = if prefs.proxyVideos: getVidUrl(vidUrl)
# else: vidUrl
# let mutedAttribute = if prefs.muteVideos: "muted=\"\""
# else: ""
<video poster="${thumb}" controls="" ${mutedAttribute} style="max-width:250px;">
<source src="${source}" type="${playbackType}" />
</video>
# end if
#elif tweet.gif.isSome:
# let thumb = &"{urlPrefix}{getPicUrl(get(tweet.gif).thumb)}"
# let url = &"{urlPrefix}{getPicUrl(get(tweet.gif).url)}"
@ -56,7 +106,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end if
#end proc
#
#proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; userId=""): string =
#proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; prefs: Prefs; userId=""): string =
#let urlPrefix = getUrlPrefix(cfg)
#var links: seq[string]
#for thread in tweets:
@ -73,7 +123,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<item>
<title>${getTitle(tweet, retweet)}</title>
<dc:creator>@${tweet.user.username}</dc:creator>
<description><![CDATA[${renderRssTweet(tweet, cfg).strip(chars={'\n'})}]]></description>
<description><![CDATA[${renderRssTweet(tweet, cfg, prefs).strip(chars={'\n'})}]]></description>
<pubDate>${getRfc822Time(tweet)}</pubDate>
<guid>${urlPrefix & link}</guid>
<link>${urlPrefix & link}</link>
@ -82,7 +132,7 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
#end for
#end proc
#
#proc renderTimelineRss*(profile: Profile; cfg: Config; tab, param: string; multi=false): string =
#proc renderTimelineRss*(profile: Profile; cfg: Config; prefs: Prefs; tab, param: string; multi=false): string =
#let urlPrefix = getUrlPrefix(cfg)
#var atomLink = &"{urlPrefix}/{profile.user.username}"
#var link = &"{urlPrefix}/{profile.user.username}"
@ -119,13 +169,13 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname}
<height>128</height>
</image>
#if profile.tweets.content.len > 0:
${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)}
${renderRssTweets(profile.tweets.content, cfg, prefs, userId=profile.user.id)}
#end if
</channel>
</rss>
#end proc
#
#proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config): string =
#proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config; prefs: Prefs): string =
#let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}"
#result = ""
<?xml version="1.0" encoding="UTF-8"?>
@ -137,12 +187,12 @@ ${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)}
<description>${getDescription(&"{list.name} by @{list.username}", cfg)}</description>
<language>en-us</language>
<ttl>40</ttl>
${renderRssTweets(tweets, cfg)}
${renderRssTweets(tweets, cfg, prefs)}
</channel>
</rss>
#end proc
#
#proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config): string =
#proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config; prefs: Prefs): string =
#let urlPrefix = getUrlPrefix(cfg)
#let escName = xmltree.escape(name)
#let escParam = xmltree.escape(param)
@ -156,7 +206,7 @@ ${renderRssTweets(tweets, cfg)}
<description>${getDescription(&"Search \"{escName}\"", cfg)}</description>
<language>en-us</language>
<ttl>40</ttl>
${renderRssTweets(tweets, cfg)}
${renderRssTweets(tweets, cfg, prefs)}
</channel>
</rss>
#end proc