1 import * as express from 'express'
2 import * as Feed from 'pfeed'
3 import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
4 import { VideoFilter } from '../../shared/models/videos/video-query.type'
5 import { buildNSFWFilter } from '../helpers/express-utils'
6 import { CONFIG } from '../initializers/config'
7 import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
10 commonVideosFiltersValidator,
13 setFeedFormatContentType,
14 videoCommentsFeedsValidator,
17 videoSubscriptionFeedsValidator
18 } from '../middlewares'
19 import { cacheRouteFactory } from '../middlewares/cache/cache'
20 import { VideoModel } from '../models/video/video'
21 import { VideoCommentModel } from '../models/video/video-comment'
23 const feedsRouter = express.Router()
25 const cacheRoute = cacheRouteFactory({
26 headerBlacklist: [ 'Content-Type' ]
29 feedsRouter.get('/feeds/video-comments.:format',
31 setFeedFormatContentType,
32 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
33 asyncMiddleware(videoFeedsValidator),
34 asyncMiddleware(videoCommentsFeedsValidator),
35 asyncMiddleware(generateVideoCommentsFeed)
38 feedsRouter.get('/feeds/videos.:format',
42 setFeedFormatContentType,
43 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
44 commonVideosFiltersValidator,
45 asyncMiddleware(videoFeedsValidator),
46 asyncMiddleware(generateVideoFeed)
49 feedsRouter.get('/feeds/subscriptions.:format',
53 setFeedFormatContentType,
54 cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
55 commonVideosFiltersValidator,
56 asyncMiddleware(videoSubscriptionFeedsValidator),
57 asyncMiddleware(generateVideoFeedForSubscriptions)
60 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
70 const video = res.locals.videoAll
71 const account = res.locals.account
72 const videoChannel = res.locals.videoChannel
74 const comments = await VideoCommentModel.listForFeed({
77 videoId: video ? video.id : undefined,
78 accountId: account ? account.id : undefined,
79 videoChannelId: videoChannel ? videoChannel.id : undefined
83 let description: string
86 name = videoChannel.getDisplayName()
87 description = videoChannel.description
89 name = account.getDisplayName()
90 description = account.description
92 name = video ? video.name : CONFIG.INSTANCE.NAME
93 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
95 const feed = initFeed({
98 resourceType: 'video-comments',
99 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
102 // Adding video items to the feed, one at a time
103 for (const comment of comments) {
104 const link = WEBSERVER.URL + comment.getCommentStaticPath()
106 let title = comment.Video.name
107 const author: { name: string, link: string }[] = []
109 if (comment.Account) {
110 title += ` - ${comment.Account.getDisplayName()}`
112 name: comment.Account.getDisplayName(),
113 link: comment.Account.Actor.url
121 content: comment.text,
123 date: comment.createdAt
127 // Now the feed generation is done, let's send it!
128 return sendFeed(feed, req, res)
131 async function generateVideoFeed (req: express.Request, res: express.Response) {
133 const account = res.locals.account
134 const videoChannel = res.locals.videoChannel
135 const nsfw = buildNSFWFilter(res, req.query.nsfw)
138 let description: string
141 name = videoChannel.getDisplayName()
142 description = videoChannel.description
143 } else if (account) {
144 name = account.getDisplayName()
145 description = account.description
147 name = CONFIG.INSTANCE.NAME
148 description = CONFIG.INSTANCE.DESCRIPTION
151 const feed = initFeed({
154 resourceType: 'videos',
155 queryString: new URL(WEBSERVER.URL + req.url).search
159 accountId: account ? account.id : null,
160 videoChannelId: videoChannel ? videoChannel.id : null
163 const { data } = await VideoModel.listForApi({
166 sort: req.query.sort,
167 includeLocalVideos: true,
169 filter: req.query.filter as VideoFilter,
175 addVideosToFeed(feed, data)
177 // Now the feed generation is done, let's send it!
178 return sendFeed(feed, req, res)
181 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
183 const account = res.locals.account
184 const nsfw = buildNSFWFilter(res, req.query.nsfw)
185 const name = account.getDisplayName()
186 const description = account.description
188 const feed = initFeed({
191 resourceType: 'videos',
192 queryString: new URL(WEBSERVER.URL + req.url).search
195 const { data } = await VideoModel.listForApi({
198 sort: req.query.sort,
199 includeLocalVideos: false,
201 filter: req.query.filter as VideoFilter,
206 followerActorId: res.locals.user.Account.Actor.id,
207 user: res.locals.user
210 addVideosToFeed(feed, data)
212 // Now the feed generation is done, let's send it!
213 return sendFeed(feed, req, res)
216 function initFeed (parameters: {
219 resourceType?: 'videos' | 'video-comments'
222 const webserverUrl = WEBSERVER.URL
223 const { name, description, resourceType, queryString } = parameters
228 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
231 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
232 favicon: webserverUrl + '/client/assets/images/favicon.png',
233 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
234 ` and potential licenses granted by each content's rightholder.`,
235 generator: `ToraifÅsu`, // ^.~
237 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
238 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
239 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
242 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
243 email: CONFIG.ADMIN.EMAIL,
244 link: `${webserverUrl}/about`
249 function addVideosToFeed (feed, videos: VideoModel[]) {
251 * Adding video items to the feed object, one at a time
253 for (const video of videos) {
254 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
256 const torrents = formattedVideoFiles.map(videoFile => ({
258 url: videoFile.torrentUrl,
259 size_in_bytes: videoFile.size
262 const videos = formattedVideoFiles.map(videoFile => {
266 height: videoFile.resolution.label.replace('p', ''),
267 fileSize: videoFile.size,
268 url: videoFile.fileUrl,
269 framerate: videoFile.fps,
270 duration: video.duration
273 if (video.language) Object.assign(result, { lang: video.language })
278 const categories: { value: number, label: string }[] = []
279 if (video.category) {
281 value: video.category,
282 label: getCategoryLabel(video.category)
289 link: WEBSERVER.URL + video.getWatchStaticPath(),
290 description: video.getTruncatedDescription(),
291 content: video.description,
294 name: video.VideoChannel.Account.getDisplayName(),
295 link: video.VideoChannel.Account.Actor.url
298 date: video.publishedAt,
303 url: video.getEmbedStaticPath(),
304 allowFullscreen: true
307 url: video.getWatchStaticPath()
317 url: WEBSERVER.URL + video.getPreviewStaticPath(),
318 height: PREVIEWS_SIZE.height,
319 width: PREVIEWS_SIZE.width
326 function sendFeed (feed, req: express.Request, res: express.Response) {
327 const format = req.params.format
329 if (format === 'atom' || format === 'atom1') {
330 return res.send(feed.atom1()).end()
333 if (format === 'json' || format === 'json1') {
334 return res.send(feed.json1()).end()
337 if (format === 'rss' || format === 'rss2') {
338 return res.send(feed.rss2()).end()
341 // We're in the ambiguous '.xml' case and we look at the format query parameter
342 if (req.query.format === 'atom' || req.query.format === 'atom1') {
343 return res.send(feed.atom1()).end()
346 return res.send(feed.rss2()).end()