1 import * as express from 'express'
2 import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
5 commonVideosFiltersValidator,
7 videoCommentsFeedsValidator,
10 } from '../middlewares'
11 import { VideoModel } from '../models/video/video'
12 import * as Feed from 'pfeed'
13 import { cacheRoute } from '../middlewares/cache'
14 import { VideoCommentModel } from '../models/video/video-comment'
15 import { buildNSFWFilter } from '../helpers/express-utils'
16 import { CONFIG } from '../initializers/config'
18 const feedsRouter = express.Router()
20 feedsRouter.get('/feeds/video-comments.:format',
21 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
22 asyncMiddleware(videoCommentsFeedsValidator),
23 asyncMiddleware(generateVideoCommentsFeed)
26 feedsRouter.get('/feeds/videos.:format',
29 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
30 commonVideosFiltersValidator,
31 asyncMiddleware(videoFeedsValidator),
32 asyncMiddleware(generateVideoFeed)
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
46 const video = res.locals.videoAll
47 const videoId: number = video ? video.id : undefined
49 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
51 const name = video ? video.name : CONFIG.INSTANCE.NAME
52 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
53 const feed = initFeed(name, description)
55 // Adding video items to the feed, one at a time
56 comments.forEach(comment => {
57 const link = WEBSERVER.URL + comment.getCommentStaticPath()
59 let title = comment.Video.name
60 if (comment.Account) title += ` - ${comment.Account.getDisplayName()}`
66 content: comment.text,
69 name: comment.Account.getDisplayName(),
70 link: comment.Account.Actor.url
73 date: comment.createdAt
77 // Now the feed generation is done, let's send it!
78 return sendFeed(feed, req, res)
81 async function generateVideoFeed (req: express.Request, res: express.Response) {
84 const account = res.locals.account
85 const videoChannel = res.locals.videoChannel
86 const nsfw = buildNSFWFilter(res, req.query.nsfw)
89 let description: string
92 name = videoChannel.getDisplayName()
93 description = videoChannel.description
95 name = account.getDisplayName()
96 description = account.description
98 name = CONFIG.INSTANCE.NAME
99 description = CONFIG.INSTANCE.DESCRIPTION
102 const feed = initFeed(name, description)
104 const resultList = await VideoModel.listForApi({
107 sort: req.query.sort,
108 includeLocalVideos: true,
110 filter: req.query.filter,
112 accountId: account ? account.id : null,
113 videoChannelId: videoChannel ? videoChannel.id : null
116 // Adding video items to the feed, one at a time
117 resultList.data.forEach(video => {
118 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
120 const torrents = formattedVideoFiles.map(videoFile => ({
122 url: videoFile.torrentUrl,
123 size_in_bytes: videoFile.size
126 const videos = formattedVideoFiles.map(videoFile => {
130 height: videoFile.resolution.label.replace('p', ''),
131 fileSize: videoFile.size,
132 url: videoFile.fileUrl,
133 framerate: videoFile.fps,
134 duration: video.duration
137 if (video.language) Object.assign(result, { lang: video.language })
142 const categories: { value: number, label: string }[] = []
143 if (video.category) {
145 value: video.category,
146 label: VideoModel.getCategoryLabel(video.category)
153 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
154 description: video.getTruncatedDescription(),
155 content: video.description,
158 name: video.VideoChannel.Account.getDisplayName(),
159 link: video.VideoChannel.Account.Actor.url
162 date: video.publishedAt,
167 url: video.getEmbedStaticPath(),
168 allowFullscreen: true
171 url: video.getWatchStaticPath()
181 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
182 height: THUMBNAILS_SIZE.height,
183 width: THUMBNAILS_SIZE.width
189 // Now the feed generation is done, let's send it!
190 return sendFeed(feed, req, res)
193 function initFeed (name: string, description: string) {
194 const webserverUrl = WEBSERVER.URL
199 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
202 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
203 favicon: webserverUrl + '/client/assets/images/favicon.png',
204 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
205 ` and potential licenses granted by each content's rightholder.`,
206 generator: `ToraifÅsu`, // ^.~
208 json: `${webserverUrl}/feeds/videos.json`,
209 atom: `${webserverUrl}/feeds/videos.atom`,
210 rss: `${webserverUrl}/feeds/videos.xml`
213 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
214 email: CONFIG.ADMIN.EMAIL,
215 link: `${webserverUrl}/about`
220 function sendFeed (feed, req: express.Request, res: express.Response) {
221 const format = req.params.format
223 if (format === 'atom' || format === 'atom1') {
224 res.set('Content-Type', 'application/atom+xml')
225 return res.send(feed.atom1()).end()
228 if (format === 'json' || format === 'json1') {
229 res.set('Content-Type', 'application/json')
230 return res.send(feed.json1()).end()
233 if (format === 'rss' || format === 'rss2') {
234 res.set('Content-Type', 'application/rss+xml')
235 return res.send(feed.rss2()).end()
238 // We're in the ambiguous '.xml' case and we look at the format query parameter
239 if (req.query.format === 'atom' || req.query.format === 'atom1') {
240 res.set('Content-Type', 'application/atom+xml')
241 return res.send(feed.atom1()).end()
244 res.set('Content-Type', 'application/rss+xml')
245 return res.send(feed.rss2()).end()