1 import * as express from 'express'
2 import * as Feed from 'pfeed'
3 import { VideoFilter } from '../../shared/models/videos/video-query.type'
4 import { buildNSFWFilter } from '../helpers/express-utils'
5 import { CONFIG } from '../initializers/config'
6 import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
9 commonVideosFiltersValidator,
12 setFeedFormatContentType,
13 videoCommentsFeedsValidator,
16 videoSubscriptionFeedsValidator
17 } from '../middlewares'
18 import { cacheRoute } from '../middlewares/cache'
19 import { VideoModel } from '../models/video/video'
20 import { VideoCommentModel } from '../models/video/video-comment'
22 const feedsRouter = express.Router()
24 feedsRouter.get('/feeds/video-comments.:format',
26 setFeedFormatContentType,
27 asyncMiddleware(cacheRoute({
31 })(ROUTE_CACHE_LIFETIME.FEEDS)),
32 asyncMiddleware(videoFeedsValidator),
33 asyncMiddleware(videoCommentsFeedsValidator),
34 asyncMiddleware(generateVideoCommentsFeed)
37 feedsRouter.get('/feeds/videos.:format',
41 setFeedFormatContentType,
42 asyncMiddleware(cacheRoute({
46 })(ROUTE_CACHE_LIFETIME.FEEDS)),
47 commonVideosFiltersValidator,
48 asyncMiddleware(videoFeedsValidator),
49 asyncMiddleware(generateVideoFeed)
52 feedsRouter.get('/feeds/subscriptions.:format',
56 setFeedFormatContentType,
57 asyncMiddleware(cacheRoute({
61 })(ROUTE_CACHE_LIFETIME.FEEDS)),
62 commonVideosFiltersValidator,
63 asyncMiddleware(videoSubscriptionFeedsValidator),
64 asyncMiddleware(generateVideoFeedForSubscriptions)
67 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
75 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
77 const video = res.locals.videoAll
78 const account = res.locals.account
79 const videoChannel = res.locals.videoChannel
81 const comments = await VideoCommentModel.listForFeed({
84 videoId: video ? video.id : undefined,
85 accountId: account ? account.id : undefined,
86 videoChannelId: videoChannel ? videoChannel.id : undefined
90 let description: string
93 name = videoChannel.getDisplayName()
94 description = videoChannel.description
96 name = account.getDisplayName()
97 description = account.description
99 name = video ? video.name : CONFIG.INSTANCE.NAME
100 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
102 const feed = initFeed({
105 resourceType: 'video-comments',
106 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
109 // Adding video items to the feed, one at a time
110 for (const comment of comments) {
111 const link = WEBSERVER.URL + comment.getCommentStaticPath()
113 let title = comment.Video.name
114 const author: { name: string, link: string }[] = []
116 if (comment.Account) {
117 title += ` - ${comment.Account.getDisplayName()}`
119 name: comment.Account.getDisplayName(),
120 link: comment.Account.Actor.url
128 content: comment.text,
130 date: comment.createdAt
134 // Now the feed generation is done, let's send it!
135 return sendFeed(feed, req, res)
138 async function generateVideoFeed (req: express.Request, res: express.Response) {
140 const account = res.locals.account
141 const videoChannel = res.locals.videoChannel
142 const nsfw = buildNSFWFilter(res, req.query.nsfw)
145 let description: string
148 name = videoChannel.getDisplayName()
149 description = videoChannel.description
150 } else if (account) {
151 name = account.getDisplayName()
152 description = account.description
154 name = CONFIG.INSTANCE.NAME
155 description = CONFIG.INSTANCE.DESCRIPTION
158 const feed = initFeed({
161 resourceType: 'videos',
162 queryString: new URL(WEBSERVER.URL + req.url).search
166 accountId: account ? account.id : null,
167 videoChannelId: videoChannel ? videoChannel.id : null
170 const { data } = await VideoModel.listForApi({
173 sort: req.query.sort,
174 includeLocalVideos: true,
176 filter: req.query.filter as VideoFilter,
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,
206 includeLocalVideos: false,
208 filter: req.query.filter as VideoFilter,
213 followerActorId: res.locals.user.Account.Actor.id,
214 user: res.locals.user
217 addVideosToFeed(feed, data)
219 // Now the feed generation is done, let's send it!
220 return sendFeed(feed, req, res)
223 function initFeed (parameters: {
226 resourceType?: 'videos' | 'video-comments'
229 const webserverUrl = WEBSERVER.URL
230 const { name, description, resourceType, queryString } = parameters
235 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
238 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
239 favicon: webserverUrl + '/client/assets/images/favicon.png',
240 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
241 ` and potential licenses granted by each content's rightholder.`,
242 generator: `ToraifÅsu`, // ^.~
244 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
245 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
246 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
249 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
250 email: CONFIG.ADMIN.EMAIL,
251 link: `${webserverUrl}/about`
256 function addVideosToFeed (feed, videos: VideoModel[]) {
258 * Adding video items to the feed object, one at a time
260 for (const video of videos) {
261 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
263 const torrents = formattedVideoFiles.map(videoFile => ({
265 url: videoFile.torrentUrl,
266 size_in_bytes: videoFile.size
269 const videos = formattedVideoFiles.map(videoFile => {
273 height: videoFile.resolution.label.replace('p', ''),
274 fileSize: videoFile.size,
275 url: videoFile.fileUrl,
276 framerate: videoFile.fps,
277 duration: video.duration
280 if (video.language) Object.assign(result, { lang: video.language })
285 const categories: { value: number, label: string }[] = []
286 if (video.category) {
288 value: video.category,
289 label: VideoModel.getCategoryLabel(video.category)
296 link: WEBSERVER.URL + '/w/' + video.uuid,
297 description: video.getTruncatedDescription(),
298 content: video.description,
301 name: video.VideoChannel.Account.getDisplayName(),
302 link: video.VideoChannel.Account.Actor.url
305 date: video.publishedAt,
310 url: video.getEmbedStaticPath(),
311 allowFullscreen: true
314 url: video.getWatchStaticPath()
324 url: WEBSERVER.URL + video.getPreviewStaticPath(),
325 height: PREVIEWS_SIZE.height,
326 width: PREVIEWS_SIZE.width
333 function sendFeed (feed, req: express.Request, res: express.Response) {
334 const format = req.params.format
336 if (format === 'atom' || format === 'atom1') {
337 return res.send(feed.atom1()).end()
340 if (format === 'json' || format === 'json1') {
341 return res.send(feed.json1()).end()
344 if (format === 'rss' || format === 'rss2') {
345 return res.send(feed.rss2()).end()
348 // We're in the ambiguous '.xml' case and we look at the format query parameter
349 if (req.query.format === 'atom' || req.query.format === 'atom1') {
350 return res.send(feed.atom1()).end()
353 return res.send(feed.rss2()).end()