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 { cacheRoute } from '../middlewares/cache'
20 import { VideoModel } from '../models/video/video'
21 import { VideoCommentModel } from '../models/video/video-comment'
23 const feedsRouter = express.Router()
25 feedsRouter.get('/feeds/video-comments.:format',
27 setFeedFormatContentType,
28 asyncMiddleware(cacheRoute({
32 })(ROUTE_CACHE_LIFETIME.FEEDS)),
33 asyncMiddleware(videoFeedsValidator),
34 asyncMiddleware(videoCommentsFeedsValidator),
35 asyncMiddleware(generateVideoCommentsFeed)
38 feedsRouter.get('/feeds/videos.:format',
42 setFeedFormatContentType,
43 asyncMiddleware(cacheRoute({
47 })(ROUTE_CACHE_LIFETIME.FEEDS)),
48 commonVideosFiltersValidator,
49 asyncMiddleware(videoFeedsValidator),
50 asyncMiddleware(generateVideoFeed)
53 feedsRouter.get('/feeds/subscriptions.:format',
57 setFeedFormatContentType,
58 asyncMiddleware(cacheRoute({
62 })(ROUTE_CACHE_LIFETIME.FEEDS)),
63 commonVideosFiltersValidator,
64 asyncMiddleware(videoSubscriptionFeedsValidator),
65 asyncMiddleware(generateVideoFeedForSubscriptions)
68 // ---------------------------------------------------------------------------
74 // ---------------------------------------------------------------------------
76 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
78 const video = res.locals.videoAll
79 const account = res.locals.account
80 const videoChannel = res.locals.videoChannel
82 const comments = await VideoCommentModel.listForFeed({
85 videoId: video ? video.id : undefined,
86 accountId: account ? account.id : undefined,
87 videoChannelId: videoChannel ? videoChannel.id : undefined
91 let description: string
94 name = videoChannel.getDisplayName()
95 description = videoChannel.description
97 name = account.getDisplayName()
98 description = account.description
100 name = video ? video.name : CONFIG.INSTANCE.NAME
101 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
103 const feed = initFeed({
106 resourceType: 'video-comments',
107 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
110 // Adding video items to the feed, one at a time
111 for (const comment of comments) {
112 const link = WEBSERVER.URL + comment.getCommentStaticPath()
114 let title = comment.Video.name
115 const author: { name: string, link: string }[] = []
117 if (comment.Account) {
118 title += ` - ${comment.Account.getDisplayName()}`
120 name: comment.Account.getDisplayName(),
121 link: comment.Account.Actor.url
129 content: comment.text,
131 date: comment.createdAt
135 // Now the feed generation is done, let's send it!
136 return sendFeed(feed, req, res)
139 async function generateVideoFeed (req: express.Request, res: express.Response) {
141 const account = res.locals.account
142 const videoChannel = res.locals.videoChannel
143 const nsfw = buildNSFWFilter(res, req.query.nsfw)
146 let description: string
149 name = videoChannel.getDisplayName()
150 description = videoChannel.description
151 } else if (account) {
152 name = account.getDisplayName()
153 description = account.description
155 name = CONFIG.INSTANCE.NAME
156 description = CONFIG.INSTANCE.DESCRIPTION
159 const feed = initFeed({
162 resourceType: 'videos',
163 queryString: new URL(WEBSERVER.URL + req.url).search
167 accountId: account ? account.id : null,
168 videoChannelId: videoChannel ? videoChannel.id : null
171 const { data } = await VideoModel.listForApi({
174 sort: req.query.sort,
175 includeLocalVideos: true,
177 filter: req.query.filter as VideoFilter,
183 addVideosToFeed(feed, data)
185 // Now the feed generation is done, let's send it!
186 return sendFeed(feed, req, res)
189 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
191 const account = res.locals.account
192 const nsfw = buildNSFWFilter(res, req.query.nsfw)
193 const name = account.getDisplayName()
194 const description = account.description
196 const feed = initFeed({
199 resourceType: 'videos',
200 queryString: new URL(WEBSERVER.URL + req.url).search
203 const { data } = await VideoModel.listForApi({
206 sort: req.query.sort,
207 includeLocalVideos: false,
209 filter: req.query.filter as VideoFilter,
214 followerActorId: res.locals.user.Account.Actor.id,
215 user: res.locals.user
218 addVideosToFeed(feed, data)
220 // Now the feed generation is done, let's send it!
221 return sendFeed(feed, req, res)
224 function initFeed (parameters: {
227 resourceType?: 'videos' | 'video-comments'
230 const webserverUrl = WEBSERVER.URL
231 const { name, description, resourceType, queryString } = parameters
236 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
239 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
240 favicon: webserverUrl + '/client/assets/images/favicon.png',
241 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
242 ` and potential licenses granted by each content's rightholder.`,
243 generator: `ToraifÅsu`, // ^.~
245 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
246 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
247 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
250 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
251 email: CONFIG.ADMIN.EMAIL,
252 link: `${webserverUrl}/about`
257 function addVideosToFeed (feed, videos: VideoModel[]) {
259 * Adding video items to the feed object, one at a time
261 for (const video of videos) {
262 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
264 const torrents = formattedVideoFiles.map(videoFile => ({
266 url: videoFile.torrentUrl,
267 size_in_bytes: videoFile.size
270 const videos = formattedVideoFiles.map(videoFile => {
274 height: videoFile.resolution.label.replace('p', ''),
275 fileSize: videoFile.size,
276 url: videoFile.fileUrl,
277 framerate: videoFile.fps,
278 duration: video.duration
281 if (video.language) Object.assign(result, { lang: video.language })
286 const categories: { value: number, label: string }[] = []
287 if (video.category) {
289 value: video.category,
290 label: getCategoryLabel(video.category)
297 link: WEBSERVER.URL + '/w/' + video.uuid,
298 description: video.getTruncatedDescription(),
299 content: video.description,
302 name: video.VideoChannel.Account.getDisplayName(),
303 link: video.VideoChannel.Account.Actor.url
306 date: video.publishedAt,
311 url: video.getEmbedStaticPath(),
312 allowFullscreen: true
315 url: video.getWatchStaticPath()
325 url: WEBSERVER.URL + video.getPreviewStaticPath(),
326 height: PREVIEWS_SIZE.height,
327 width: PREVIEWS_SIZE.width
334 function sendFeed (feed, req: express.Request, res: express.Response) {
335 const format = req.params.format
337 if (format === 'atom' || format === 'atom1') {
338 return res.send(feed.atom1()).end()
341 if (format === 'json' || format === 'json1') {
342 return res.send(feed.json1()).end()
345 if (format === 'rss' || format === 'rss2') {
346 return res.send(feed.rss2()).end()
349 // We're in the ambiguous '.xml' case and we look at the format query parameter
350 if (req.query.format === 'atom' || req.query.format === 'atom1') {
351 return res.send(feed.atom1()).end()
354 return res.send(feed.rss2()).end()