1 import express from 'express'
2 import Feed from 'pfeed'
3 import { getServerActor } from '@server/models/application/application'
4 import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
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 server = await getServerActor()
164 const { data } = await VideoModel.listForApi({
167 sort: req.query.sort,
168 displayOnlyForFollower: {
173 isLocal: req.query.isLocal,
174 include: req.query.include,
180 addVideosToFeed(feed, data)
182 // Now the feed generation is done, let's send it!
183 return sendFeed(feed, req, res)
186 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
188 const account = res.locals.account
189 const nsfw = buildNSFWFilter(res, req.query.nsfw)
190 const name = account.getDisplayName()
191 const description = account.description
193 const feed = initFeed({
196 resourceType: 'videos',
197 queryString: new URL(WEBSERVER.URL + req.url).search
200 const { data } = await VideoModel.listForApi({
203 sort: req.query.sort,
206 isLocal: req.query.isLocal,
207 include: req.query.include,
212 displayOnlyForFollower: {
213 actorId: res.locals.user.Account.Actor.id,
216 user: res.locals.user
219 addVideosToFeed(feed, data)
221 // Now the feed generation is done, let's send it!
222 return sendFeed(feed, req, res)
225 function initFeed (parameters: {
228 resourceType?: 'videos' | 'video-comments'
231 const webserverUrl = WEBSERVER.URL
232 const { name, description, resourceType, queryString } = parameters
237 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
240 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
241 favicon: webserverUrl + '/client/assets/images/favicon.png',
242 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
243 ` and potential licenses granted by each content's rightholder.`,
244 generator: `ToraifÅsu`, // ^.~
246 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
247 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
248 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
251 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
252 email: CONFIG.ADMIN.EMAIL,
253 link: `${webserverUrl}/about`
258 function addVideosToFeed (feed, videos: VideoModel[]) {
260 * Adding video items to the feed object, one at a time
262 for (const video of videos) {
263 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
265 const torrents = formattedVideoFiles.map(videoFile => ({
267 url: videoFile.torrentUrl,
268 size_in_bytes: videoFile.size
271 const videos = formattedVideoFiles.map(videoFile => {
275 height: videoFile.resolution.label.replace('p', ''),
276 fileSize: videoFile.size,
277 url: videoFile.fileUrl,
278 framerate: videoFile.fps,
279 duration: video.duration
282 if (video.language) Object.assign(result, { lang: video.language })
287 const categories: { value: number, label: string }[] = []
288 if (video.category) {
290 value: video.category,
291 label: getCategoryLabel(video.category)
298 link: WEBSERVER.URL + video.getWatchStaticPath(),
299 description: video.getTruncatedDescription(),
300 content: video.description,
303 name: video.VideoChannel.Account.getDisplayName(),
304 link: video.VideoChannel.Account.Actor.url
307 date: video.publishedAt,
312 url: video.getEmbedStaticPath(),
313 allowFullscreen: true
316 url: video.getWatchStaticPath()
326 url: WEBSERVER.URL + video.getPreviewStaticPath(),
327 height: PREVIEWS_SIZE.height,
328 width: PREVIEWS_SIZE.width
335 function sendFeed (feed, req: express.Request, res: express.Response) {
336 const format = req.params.format
338 if (format === 'atom' || format === 'atom1') {
339 return res.send(feed.atom1()).end()
342 if (format === 'json' || format === 'json1') {
343 return res.send(feed.json1()).end()
346 if (format === 'rss' || format === 'rss2') {
347 return res.send(feed.rss2()).end()
350 // We're in the ambiguous '.xml' case and we look at the format query parameter
351 if (req.query.format === 'atom' || req.query.format === 'atom1') {
352 return res.send(feed.atom1()).end()
355 return res.send(feed.rss2()).end()