1 import * as express from 'express'
2 import * as Feed from 'pfeed'
3 import { buildNSFWFilter } from '../helpers/express-utils'
4 import { CONFIG } from '../initializers/config'
5 import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
8 commonVideosFiltersValidator,
11 setFeedFormatContentType,
12 videoCommentsFeedsValidator,
15 } from '../middlewares'
16 import { cacheRoute } from '../middlewares/cache'
17 import { VideoModel } from '../models/video/video'
18 import { VideoCommentModel } from '../models/video/video-comment'
20 const feedsRouter = express.Router()
22 feedsRouter.get('/feeds/video-comments.:format',
24 setFeedFormatContentType,
25 asyncMiddleware(cacheRoute({
29 })(ROUTE_CACHE_LIFETIME.FEEDS)),
30 asyncMiddleware(videoFeedsValidator),
31 asyncMiddleware(videoCommentsFeedsValidator),
32 asyncMiddleware(generateVideoCommentsFeed)
35 feedsRouter.get('/feeds/videos.:format',
39 setFeedFormatContentType,
40 asyncMiddleware(cacheRoute({
44 })(ROUTE_CACHE_LIFETIME.FEEDS)),
45 commonVideosFiltersValidator,
46 asyncMiddleware(videoFeedsValidator),
47 asyncMiddleware(generateVideoFeed)
50 // ---------------------------------------------------------------------------
56 // ---------------------------------------------------------------------------
58 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
61 const video = res.locals.videoAll
62 const account = res.locals.account
63 const videoChannel = res.locals.videoChannel
65 const comments = await VideoCommentModel.listForFeed({
68 videoId: video ? video.id : undefined,
69 accountId: account ? account.id : undefined,
70 videoChannelId: videoChannel ? videoChannel.id : undefined
74 let description: string
77 name = videoChannel.getDisplayName()
78 description = videoChannel.description
80 name = account.getDisplayName()
81 description = account.description
83 name = video ? video.name : CONFIG.INSTANCE.NAME
84 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
86 const feed = initFeed({
89 resourceType: 'video-comments',
90 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
93 // Adding video items to the feed, one at a time
94 for (const comment of comments) {
95 const link = WEBSERVER.URL + comment.getCommentStaticPath()
97 let title = comment.Video.name
98 const author: { name: string, link: string }[] = []
100 if (comment.Account) {
101 title += ` - ${comment.Account.getDisplayName()}`
103 name: comment.Account.getDisplayName(),
104 link: comment.Account.Actor.url
112 content: comment.text,
114 date: comment.createdAt
118 // Now the feed generation is done, let's send it!
119 return sendFeed(feed, req, res)
122 async function generateVideoFeed (req: express.Request, res: express.Response) {
125 const account = res.locals.account
126 const videoChannel = res.locals.videoChannel
127 const nsfw = buildNSFWFilter(res, req.query.nsfw)
130 let description: string
133 name = videoChannel.getDisplayName()
134 description = videoChannel.description
135 } else if (account) {
136 name = account.getDisplayName()
137 description = account.description
139 name = CONFIG.INSTANCE.NAME
140 description = CONFIG.INSTANCE.DESCRIPTION
143 const feed = initFeed({
146 resourceType: 'videos',
147 queryString: new URL(WEBSERVER.URL + req.url).search
150 const resultList = await VideoModel.listForApi({
153 sort: req.query.sort,
154 includeLocalVideos: true,
156 filter: req.query.filter,
158 accountId: account ? account.id : null,
159 videoChannelId: videoChannel ? videoChannel.id : null
162 // Adding video items to the feed, one at a time
163 resultList.data.forEach(video => {
164 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
166 const torrents = formattedVideoFiles.map(videoFile => ({
168 url: videoFile.torrentUrl,
169 size_in_bytes: videoFile.size
172 const videos = formattedVideoFiles.map(videoFile => {
176 height: videoFile.resolution.label.replace('p', ''),
177 fileSize: videoFile.size,
178 url: videoFile.fileUrl,
179 framerate: videoFile.fps,
180 duration: video.duration
183 if (video.language) Object.assign(result, { lang: video.language })
188 const categories: { value: number, label: string }[] = []
189 if (video.category) {
191 value: video.category,
192 label: VideoModel.getCategoryLabel(video.category)
199 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
200 description: video.getTruncatedDescription(),
201 content: video.description,
204 name: video.VideoChannel.Account.getDisplayName(),
205 link: video.VideoChannel.Account.Actor.url
208 date: video.publishedAt,
213 url: video.getEmbedStaticPath(),
214 allowFullscreen: true
217 url: video.getWatchStaticPath()
227 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
228 height: THUMBNAILS_SIZE.height,
229 width: THUMBNAILS_SIZE.width
235 // Now the feed generation is done, let's send it!
236 return sendFeed(feed, req, res)
239 function initFeed (parameters: {
242 resourceType?: 'videos' | 'video-comments'
245 const webserverUrl = WEBSERVER.URL
246 const { name, description, resourceType, queryString } = parameters
251 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
254 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
255 favicon: webserverUrl + '/client/assets/images/favicon.png',
256 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
257 ` and potential licenses granted by each content's rightholder.`,
258 generator: `ToraifÅsu`, // ^.~
260 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
261 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
262 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
265 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
266 email: CONFIG.ADMIN.EMAIL,
267 link: `${webserverUrl}/about`
272 function sendFeed (feed, req: express.Request, res: express.Response) {
273 const format = req.params.format
275 if (format === 'atom' || format === 'atom1') {
276 return res.send(feed.atom1()).end()
279 if (format === 'json' || format === 'json1') {
280 return res.send(feed.json1()).end()
283 if (format === 'rss' || format === 'rss2') {
284 return res.send(feed.rss2()).end()
287 // We're in the ambiguous '.xml' case and we look at the format query parameter
288 if (req.query.format === 'atom' || req.query.format === 'atom1') {
289 return res.send(feed.atom1()).end()
292 return res.send(feed.rss2()).end()