1 import express from 'express'
2 import Feed from 'pfeed'
3 import { getServerActor } from '@server/models/application/application'
4 import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
5 import { VideoInclude } from '@shared/models'
6 import { buildNSFWFilter } from '../helpers/express-utils'
7 import { CONFIG } from '../initializers/config'
8 import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
11 commonVideosFiltersValidator,
14 setFeedFormatContentType,
15 videoCommentsFeedsValidator,
18 videoSubscriptionFeedsValidator
19 } from '../middlewares'
20 import { cacheRouteFactory } from '../middlewares/cache/cache'
21 import { VideoModel } from '../models/video/video'
22 import { VideoCommentModel } from '../models/video/video-comment'
24 const feedsRouter = express.Router()
26 const cacheRoute = cacheRouteFactory({
27 headerBlacklist: [ 'Content-Type' ]
30 feedsRouter.get('/feeds/video-comments.:format',
32 setFeedFormatContentType,
33 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
34 asyncMiddleware(videoFeedsValidator),
35 asyncMiddleware(videoCommentsFeedsValidator),
36 asyncMiddleware(generateVideoCommentsFeed)
39 feedsRouter.get('/feeds/videos.:format',
43 setFeedFormatContentType,
44 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
45 commonVideosFiltersValidator,
46 asyncMiddleware(videoFeedsValidator),
47 asyncMiddleware(generateVideoFeed)
50 feedsRouter.get('/feeds/subscriptions.:format',
54 setFeedFormatContentType,
55 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
56 commonVideosFiltersValidator,
57 asyncMiddleware(videoSubscriptionFeedsValidator),
58 asyncMiddleware(generateVideoFeedForSubscriptions)
61 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
71 const video = res.locals.videoAll
72 const account = res.locals.account
73 const videoChannel = res.locals.videoChannel
75 const comments = await VideoCommentModel.listForFeed({
78 videoId: video ? video.id : undefined,
79 accountId: account ? account.id : undefined,
80 videoChannelId: videoChannel ? videoChannel.id : undefined
84 let description: string
87 name = videoChannel.getDisplayName()
88 description = videoChannel.description
90 name = account.getDisplayName()
91 description = account.description
93 name = video ? video.name : CONFIG.INSTANCE.NAME
94 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
96 const feed = initFeed({
99 resourceType: 'video-comments',
100 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
103 // Adding video items to the feed, one at a time
104 for (const comment of comments) {
105 const link = WEBSERVER.URL + comment.getCommentStaticPath()
107 let title = comment.Video.name
108 const author: { name: string, link: string }[] = []
110 if (comment.Account) {
111 title += ` - ${comment.Account.getDisplayName()}`
113 name: comment.Account.getDisplayName(),
114 link: comment.Account.Actor.url
122 content: comment.text,
124 date: comment.createdAt
128 // Now the feed generation is done, let's send it!
129 return sendFeed(feed, req, res)
132 async function generateVideoFeed (req: express.Request, res: express.Response) {
134 const account = res.locals.account
135 const videoChannel = res.locals.videoChannel
136 const nsfw = buildNSFWFilter(res, req.query.nsfw)
139 let description: string
142 name = videoChannel.getDisplayName()
143 description = videoChannel.description
144 } else if (account) {
145 name = account.getDisplayName()
146 description = account.description
148 name = CONFIG.INSTANCE.NAME
149 description = CONFIG.INSTANCE.DESCRIPTION
152 const feed = initFeed({
155 resourceType: 'videos',
156 queryString: new URL(WEBSERVER.URL + req.url).search
160 accountId: account ? account.id : null,
161 videoChannelId: videoChannel ? videoChannel.id : null
164 const server = await getServerActor()
165 const { data } = await VideoModel.listForApi({
168 sort: req.query.sort,
169 displayOnlyForFollower: {
174 isLocal: req.query.isLocal,
175 include: req.query.include | VideoInclude.FILES,
181 addVideosToFeed(feed, data)
183 // Now the feed generation is done, let's send it!
184 return sendFeed(feed, req, res)
187 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
189 const account = res.locals.account
190 const nsfw = buildNSFWFilter(res, req.query.nsfw)
191 const name = account.getDisplayName()
192 const description = account.description
194 const feed = initFeed({
197 resourceType: 'videos',
198 queryString: new URL(WEBSERVER.URL + req.url).search
201 const { data } = await VideoModel.listForApi({
204 sort: req.query.sort,
207 isLocal: req.query.isLocal,
210 include: req.query.include | VideoInclude.FILES,
214 displayOnlyForFollower: {
215 actorId: res.locals.user.Account.Actor.id,
218 user: res.locals.user
221 addVideosToFeed(feed, data)
223 // Now the feed generation is done, let's send it!
224 return sendFeed(feed, req, res)
227 function initFeed (parameters: {
230 resourceType?: 'videos' | 'video-comments'
233 const webserverUrl = WEBSERVER.URL
234 const { name, description, resourceType, queryString } = parameters
239 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
242 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
243 favicon: webserverUrl + '/client/assets/images/favicon.png',
244 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
245 ` and potential licenses granted by each content's rightholder.`,
246 generator: `ToraifÅsu`, // ^.~
248 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
249 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
250 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
253 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
254 email: CONFIG.ADMIN.EMAIL,
255 link: `${webserverUrl}/about`
260 function addVideosToFeed (feed, videos: VideoModel[]) {
262 * Adding video items to the feed object, one at a time
264 for (const video of videos) {
265 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
267 const torrents = formattedVideoFiles.map(videoFile => ({
269 url: videoFile.torrentUrl,
270 size_in_bytes: videoFile.size
273 const videos = formattedVideoFiles.map(videoFile => {
277 height: videoFile.resolution.label.replace('p', ''),
278 fileSize: videoFile.size,
279 url: videoFile.fileUrl,
280 framerate: videoFile.fps,
281 duration: video.duration
284 if (video.language) Object.assign(result, { lang: video.language })
289 const categories: { value: number, label: string }[] = []
290 if (video.category) {
292 value: video.category,
293 label: getCategoryLabel(video.category)
300 link: WEBSERVER.URL + video.getWatchStaticPath(),
301 description: video.getTruncatedDescription(),
302 content: video.description,
305 name: video.VideoChannel.Account.getDisplayName(),
306 link: video.VideoChannel.Account.Actor.url
309 date: video.publishedAt,
314 url: video.getEmbedStaticPath(),
315 allowFullscreen: true
318 url: video.getWatchStaticPath()
328 url: WEBSERVER.URL + video.getPreviewStaticPath(),
329 height: PREVIEWS_SIZE.height,
330 width: PREVIEWS_SIZE.width
337 function sendFeed (feed, req: express.Request, res: express.Response) {
338 const format = req.params.format
340 if (format === 'atom' || format === 'atom1') {
341 return res.send(feed.atom1()).end()
344 if (format === 'json' || format === 'json1') {
345 return res.send(feed.json1()).end()
348 if (format === 'rss' || format === 'rss2') {
349 return res.send(feed.rss2()).end()
352 // We're in the ambiguous '.xml' case and we look at the format query parameter
353 if (req.query.format === 'atom' || req.query.format === 'atom1') {
354 return res.send(feed.atom1()).end()
357 return res.send(feed.rss2()).end()