1 import express from 'express'
2 import Feed from 'pfeed'
3 import { mdToOneLinePlainText, toSafeHtml } from '@server/helpers/markdown'
4 import { getServerActor } from '@server/models/application/application'
5 import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
6 import { VideoInclude } from '@shared/models'
7 import { buildNSFWFilter } from '../helpers/express-utils'
8 import { CONFIG } from '../initializers/config'
9 import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
12 commonVideosFiltersValidator,
15 setFeedFormatContentType,
16 videoCommentsFeedsValidator,
19 videoSubscriptionFeedsValidator
20 } from '../middlewares'
21 import { cacheRouteFactory } from '../middlewares/cache/cache'
22 import { VideoModel } from '../models/video/video'
23 import { VideoCommentModel } from '../models/video/video-comment'
25 const feedsRouter = express.Router()
27 const cacheRoute = cacheRouteFactory({
28 headerBlacklist: [ 'Content-Type' ]
31 feedsRouter.get('/feeds/video-comments.:format',
33 setFeedFormatContentType,
34 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
35 asyncMiddleware(videoFeedsValidator),
36 asyncMiddleware(videoCommentsFeedsValidator),
37 asyncMiddleware(generateVideoCommentsFeed)
40 feedsRouter.get('/feeds/videos.:format',
44 setFeedFormatContentType,
45 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
46 commonVideosFiltersValidator,
47 asyncMiddleware(videoFeedsValidator),
48 asyncMiddleware(generateVideoFeed)
51 feedsRouter.get('/feeds/subscriptions.:format',
55 setFeedFormatContentType,
56 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
57 commonVideosFiltersValidator,
58 asyncMiddleware(videoSubscriptionFeedsValidator),
59 asyncMiddleware(generateVideoFeedForSubscriptions)
62 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
72 const video = res.locals.videoAll
73 const account = res.locals.account
74 const videoChannel = res.locals.videoChannel
76 const comments = await VideoCommentModel.listForFeed({
79 videoId: video ? video.id : undefined,
80 accountId: account ? account.id : undefined,
81 videoChannelId: videoChannel ? videoChannel.id : undefined
85 let description: string
88 name = videoChannel.getDisplayName()
89 description = videoChannel.description
91 name = account.getDisplayName()
92 description = account.description
94 name = video ? video.name : CONFIG.INSTANCE.NAME
95 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
97 const feed = initFeed({
100 resourceType: 'video-comments',
101 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
104 // Adding video items to the feed, one at a time
105 for (const comment of comments) {
106 const link = WEBSERVER.URL + comment.getCommentStaticPath()
108 let title = comment.Video.name
109 const author: { name: string, link: string }[] = []
111 if (comment.Account) {
112 title += ` - ${comment.Account.getDisplayName()}`
114 name: comment.Account.getDisplayName(),
115 link: comment.Account.Actor.url
123 content: toSafeHtml(comment.text),
125 date: comment.createdAt
129 // Now the feed generation is done, let's send it!
130 return sendFeed(feed, req, res)
133 async function generateVideoFeed (req: express.Request, res: express.Response) {
135 const account = res.locals.account
136 const videoChannel = res.locals.videoChannel
137 const nsfw = buildNSFWFilter(res, req.query.nsfw)
140 let description: string
143 name = videoChannel.getDisplayName()
144 description = videoChannel.description
145 } else if (account) {
146 name = account.getDisplayName()
147 description = account.description
149 name = CONFIG.INSTANCE.NAME
150 description = CONFIG.INSTANCE.DESCRIPTION
153 const feed = initFeed({
156 resourceType: 'videos',
157 queryString: new URL(WEBSERVER.URL + req.url).search
161 accountId: account ? account.id : null,
162 videoChannelId: videoChannel ? videoChannel.id : null
165 const server = await getServerActor()
166 const { data } = await VideoModel.listForApi({
169 sort: req.query.sort,
170 displayOnlyForFollower: {
175 isLocal: req.query.isLocal,
176 include: req.query.include | VideoInclude.FILES,
182 addVideosToFeed(feed, data)
184 // Now the feed generation is done, let's send it!
185 return sendFeed(feed, req, res)
188 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
190 const account = res.locals.account
191 const nsfw = buildNSFWFilter(res, req.query.nsfw)
192 const name = account.getDisplayName()
193 const description = account.description
195 const feed = initFeed({
198 resourceType: 'videos',
199 queryString: new URL(WEBSERVER.URL + req.url).search
202 const { data } = await VideoModel.listForApi({
205 sort: req.query.sort,
208 isLocal: req.query.isLocal,
211 include: req.query.include | VideoInclude.FILES,
215 displayOnlyForFollower: {
216 actorId: res.locals.user.Account.Actor.id,
219 user: res.locals.user
222 addVideosToFeed(feed, data)
224 // Now the feed generation is done, let's send it!
225 return sendFeed(feed, req, res)
228 function initFeed (parameters: {
231 resourceType?: 'videos' | 'video-comments'
234 const webserverUrl = WEBSERVER.URL
235 const { name, description, resourceType, queryString } = parameters
239 description: mdToOneLinePlainText(description),
240 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
243 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
244 favicon: webserverUrl + '/client/assets/images/favicon.png',
245 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
246 ` and potential licenses granted by each content's rightholder.`,
247 generator: `ToraifÅsu`, // ^.~
249 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
250 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
251 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
254 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
255 email: CONFIG.ADMIN.EMAIL,
256 link: `${webserverUrl}/about`
261 function addVideosToFeed (feed, videos: VideoModel[]) {
263 * Adding video items to the feed object, one at a time
265 for (const video of videos) {
266 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
268 const torrents = formattedVideoFiles.map(videoFile => ({
270 url: videoFile.torrentUrl,
271 size_in_bytes: videoFile.size
274 const videos = formattedVideoFiles.map(videoFile => {
278 height: videoFile.resolution.label.replace('p', ''),
279 fileSize: videoFile.size,
280 url: videoFile.fileUrl,
281 framerate: videoFile.fps,
282 duration: video.duration
285 if (video.language) Object.assign(result, { lang: video.language })
290 const categories: { value: number, label: string }[] = []
291 if (video.category) {
293 value: video.category,
294 label: getCategoryLabel(video.category)
301 link: WEBSERVER.URL + video.getWatchStaticPath(),
302 description: mdToOneLinePlainText(video.getTruncatedDescription()),
303 content: toSafeHtml(video.description),
306 name: video.VideoChannel.Account.getDisplayName(),
307 link: video.VideoChannel.Account.Actor.url
310 date: video.publishedAt,
315 url: video.getEmbedStaticPath(),
316 allowFullscreen: true
319 url: video.getWatchStaticPath()
329 url: WEBSERVER.URL + video.getPreviewStaticPath(),
330 height: PREVIEWS_SIZE.height,
331 width: PREVIEWS_SIZE.width
338 function sendFeed (feed, req: express.Request, res: express.Response) {
339 const format = req.params.format
341 if (format === 'atom' || format === 'atom1') {
342 return res.send(feed.atom1()).end()
345 if (format === 'json' || format === 'json1') {
346 return res.send(feed.json1()).end()
349 if (format === 'rss' || format === 'rss2') {
350 return res.send(feed.rss2()).end()
353 // We're in the ambiguous '.xml' case and we look at the format query parameter
354 if (req.query.format === 'atom' || req.query.format === 'atom1') {
355 return res.send(feed.atom1()).end()
358 return res.send(feed.rss2()).end()