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 videoSubscriptonFeedsValidator
16 } from '../middlewares'
17 import { cacheRoute } from '../middlewares/cache'
18 import { VideoModel } from '../models/video/video'
19 import { VideoCommentModel } from '../models/video/video-comment'
20 import { VideoFilter } from '../../shared/models/videos/video-query.type'
21 import { logger } from '../helpers/logger'
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(videoSubscriptonFeedsValidator),
51 asyncMiddleware(generateVideoFeed)
54 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
65 const video = res.locals.videoAll
66 const account = res.locals.account
67 const videoChannel = res.locals.videoChannel
69 const comments = await VideoCommentModel.listForFeed({
72 videoId: video ? video.id : undefined,
73 accountId: account ? account.id : undefined,
74 videoChannelId: videoChannel ? videoChannel.id : undefined
78 let description: string
81 name = videoChannel.getDisplayName()
82 description = videoChannel.description
84 name = account.getDisplayName()
85 description = account.description
87 name = video ? video.name : CONFIG.INSTANCE.NAME
88 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
90 const feed = initFeed({
93 resourceType: 'video-comments',
94 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
97 // Adding video items to the feed, one at a time
98 for (const comment of comments) {
99 const link = WEBSERVER.URL + comment.getCommentStaticPath()
101 let title = comment.Video.name
102 const author: { name: string, link: string }[] = []
104 if (comment.Account) {
105 title += ` - ${comment.Account.getDisplayName()}`
107 name: comment.Account.getDisplayName(),
108 link: comment.Account.Actor.url
116 content: comment.text,
118 date: comment.createdAt
122 // Now the feed generation is done, let's send it!
123 return sendFeed(feed, req, res)
126 async function generateVideoFeed (req: express.Request, res: express.Response) {
129 const account = res.locals.account
130 const videoChannel = res.locals.videoChannel
131 const token = req.query.token
132 const nsfw = buildNSFWFilter(res, req.query.nsfw)
135 let description: string
138 name = videoChannel.getDisplayName()
139 description = videoChannel.description
140 } else if (account) {
141 name = account.getDisplayName()
142 description = account.description
144 name = CONFIG.INSTANCE.NAME
145 description = CONFIG.INSTANCE.DESCRIPTION
148 const feed = initFeed({
151 resourceType: 'videos',
152 queryString: new URL(WEBSERVER.URL + req.url).search
156 * We have two ways to query video results:
157 * - one with account and token -> get subscription videos
158 * - one with either account, channel, or nothing: just videos with these filters
160 const options = token && token !== '' && res.locals.user
162 followerActorId: res.locals.user.Account.Actor.id,
163 user: res.locals.user,
164 includeLocalVideos: false
167 accountId: account ? account.id : null,
168 videoChannelId: videoChannel ? videoChannel.id : null
171 const resultList = await VideoModel.listForApi({
174 sort: req.query.sort,
175 includeLocalVideos: true,
177 filter: req.query.filter as VideoFilter,
183 * Adding video items to the feed object, one at a time
185 resultList.data.forEach(video => {
186 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
188 const torrents = formattedVideoFiles.map(videoFile => ({
190 url: videoFile.torrentUrl,
191 size_in_bytes: videoFile.size
194 const videos = formattedVideoFiles.map(videoFile => {
198 height: videoFile.resolution.label.replace('p', ''),
199 fileSize: videoFile.size,
200 url: videoFile.fileUrl,
201 framerate: videoFile.fps,
202 duration: video.duration
205 if (video.language) Object.assign(result, { lang: video.language })
210 const categories: { value: number, label: string }[] = []
211 if (video.category) {
213 value: video.category,
214 label: VideoModel.getCategoryLabel(video.category)
221 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
222 description: video.getTruncatedDescription(),
223 content: video.description,
226 name: video.VideoChannel.Account.getDisplayName(),
227 link: video.VideoChannel.Account.Actor.url
230 date: video.publishedAt,
235 url: video.getEmbedStaticPath(),
236 allowFullscreen: true
239 url: video.getWatchStaticPath()
249 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
250 height: THUMBNAILS_SIZE.height,
251 width: THUMBNAILS_SIZE.width
257 // Now the feed generation is done, let's send it!
258 return sendFeed(feed, req, res)
261 function initFeed (parameters: {
264 resourceType?: 'videos' | 'video-comments'
267 const webserverUrl = WEBSERVER.URL
268 const { name, description, resourceType, queryString } = parameters
273 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
276 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
277 favicon: webserverUrl + '/client/assets/images/favicon.png',
278 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
279 ` and potential licenses granted by each content's rightholder.`,
280 generator: `ToraifÅsu`, // ^.~
282 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
283 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
284 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
287 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
288 email: CONFIG.ADMIN.EMAIL,
289 link: `${webserverUrl}/about`
294 function sendFeed (feed, req: express.Request, res: express.Response) {
295 const format = req.params.format
297 if (format === 'atom' || format === 'atom1') {
298 return res.send(feed.atom1()).end()
301 if (format === 'json' || format === 'json1') {
302 return res.send(feed.json1()).end()
305 if (format === 'rss' || format === 'rss2') {
306 return res.send(feed.rss2()).end()
309 // We're in the ambiguous '.xml' case and we look at the format query parameter
310 if (req.query.format === 'atom' || req.query.format === 'atom1') {
311 return res.send(feed.atom1()).end()
314 return res.send(feed.rss2()).end()