1 import * as express from 'express'
2 import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
5 commonVideosFiltersValidator,
7 videoCommentsFeedsValidator,
11 setFeedFormatContentType
12 } from '../middlewares'
13 import { VideoModel } from '../models/video/video'
14 import * as Feed from 'pfeed'
15 import { cacheRoute } from '../middlewares/cache'
16 import { VideoCommentModel } from '../models/video/video-comment'
17 import { buildNSFWFilter } from '../helpers/express-utils'
18 import { CONFIG } from '../initializers/config'
20 const feedsRouter = express.Router()
22 feedsRouter.get('/feeds/video-comments.:format',
24 setFeedFormatContentType,
25 asyncMiddleware(cacheRoute({
29 })(ROUTE_CACHE_LIFETIME.FEEDS)),
30 asyncMiddleware(videoCommentsFeedsValidator),
31 asyncMiddleware(generateVideoCommentsFeed)
34 feedsRouter.get('/feeds/videos.:format',
38 setFeedFormatContentType,
39 asyncMiddleware(cacheRoute({
43 })(ROUTE_CACHE_LIFETIME.FEEDS)),
44 commonVideosFiltersValidator,
45 asyncMiddleware(videoFeedsValidator),
46 asyncMiddleware(generateVideoFeed)
49 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
60 const video = res.locals.videoAll
61 const videoId: number = video ? video.id : undefined
63 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
65 const name = video ? video.name : CONFIG.INSTANCE.NAME
66 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
67 const feed = initFeed(name, description)
69 // Adding video items to the feed, one at a time
70 comments.forEach(comment => {
71 const link = WEBSERVER.URL + comment.getCommentStaticPath()
73 let title = comment.Video.name
74 const author: { name: string, link: string }[] = []
76 if (comment.Account) {
77 title += ` - ${comment.Account.getDisplayName()}`
79 name: comment.Account.getDisplayName(),
80 link: comment.Account.Actor.url
88 content: comment.text,
90 date: comment.createdAt
94 // Now the feed generation is done, let's send it!
95 return sendFeed(feed, req, res)
98 async function generateVideoFeed (req: express.Request, res: express.Response) {
101 const account = res.locals.account
102 const videoChannel = res.locals.videoChannel
103 const nsfw = buildNSFWFilter(res, req.query.nsfw)
106 let description: string
109 name = videoChannel.getDisplayName()
110 description = videoChannel.description
111 } else if (account) {
112 name = account.getDisplayName()
113 description = account.description
115 name = CONFIG.INSTANCE.NAME
116 description = CONFIG.INSTANCE.DESCRIPTION
119 const feed = initFeed(name, description)
121 const resultList = await VideoModel.listForApi({
124 sort: req.query.sort,
125 includeLocalVideos: true,
127 filter: req.query.filter,
129 accountId: account ? account.id : null,
130 videoChannelId: videoChannel ? videoChannel.id : null
133 // Adding video items to the feed, one at a time
134 resultList.data.forEach(video => {
135 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
137 const torrents = formattedVideoFiles.map(videoFile => ({
139 url: videoFile.torrentUrl,
140 size_in_bytes: videoFile.size
143 const videos = formattedVideoFiles.map(videoFile => {
147 height: videoFile.resolution.label.replace('p', ''),
148 fileSize: videoFile.size,
149 url: videoFile.fileUrl,
150 framerate: videoFile.fps,
151 duration: video.duration
154 if (video.language) Object.assign(result, { lang: video.language })
159 const categories: { value: number, label: string }[] = []
160 if (video.category) {
162 value: video.category,
163 label: VideoModel.getCategoryLabel(video.category)
170 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
171 description: video.getTruncatedDescription(),
172 content: video.description,
175 name: video.VideoChannel.Account.getDisplayName(),
176 link: video.VideoChannel.Account.Actor.url
179 date: video.publishedAt,
184 url: video.getEmbedStaticPath(),
185 allowFullscreen: true
188 url: video.getWatchStaticPath()
198 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
199 height: THUMBNAILS_SIZE.height,
200 width: THUMBNAILS_SIZE.width
206 // Now the feed generation is done, let's send it!
207 return sendFeed(feed, req, res)
210 function initFeed (name: string, description: string) {
211 const webserverUrl = WEBSERVER.URL
216 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
219 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
220 favicon: webserverUrl + '/client/assets/images/favicon.png',
221 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
222 ` and potential licenses granted by each content's rightholder.`,
223 generator: `ToraifÅsu`, // ^.~
225 json: `${webserverUrl}/feeds/videos.json`,
226 atom: `${webserverUrl}/feeds/videos.atom`,
227 rss: `${webserverUrl}/feeds/videos.xml`
230 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
231 email: CONFIG.ADMIN.EMAIL,
232 link: `${webserverUrl}/about`
237 function sendFeed (feed, req: express.Request, res: express.Response) {
238 const format = req.params.format
240 if (format === 'atom' || format === 'atom1') {
241 return res.send(feed.atom1()).end()
244 if (format === 'json' || format === 'json1') {
245 return res.send(feed.json1()).end()
248 if (format === 'rss' || format === 'rss2') {
249 return res.send(feed.rss2()).end()
252 // We're in the ambiguous '.xml' case and we look at the format query parameter
253 if (req.query.format === 'atom' || req.query.format === 'atom1') {
254 return res.send(feed.atom1()).end()
257 return res.send(feed.rss2()).end()