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.video
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()
60 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
63 content: comment.text,
66 name: comment.Account.getDisplayName(),
67 link: comment.Account.Actor.url
70 date: comment.createdAt
74 // Now the feed generation is done, let's send it!
75 return sendFeed(feed, req, res)
78 async function generateVideoFeed (req: express.Request, res: express.Response) {
81 const account = res.locals.account
82 const videoChannel = res.locals.videoChannel
83 const nsfw = buildNSFWFilter(res, req.query.nsfw)
86 let description: string
89 name = videoChannel.getDisplayName()
90 description = videoChannel.description
92 name = account.getDisplayName()
93 description = account.description
95 name = CONFIG.INSTANCE.NAME
96 description = CONFIG.INSTANCE.DESCRIPTION
99 const feed = initFeed(name, description)
101 const resultList = await VideoModel.listForApi({
104 sort: req.query.sort,
105 includeLocalVideos: true,
107 filter: req.query.filter,
109 accountId: account ? account.id : null,
110 videoChannelId: videoChannel ? videoChannel.id : null
113 // Adding video items to the feed, one at a time
114 resultList.data.forEach(video => {
115 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
116 const torrents = formattedVideoFiles.map(videoFile => ({
118 url: videoFile.torrentUrl,
119 size_in_bytes: videoFile.size
125 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
126 description: video.getTruncatedDescription(),
127 content: video.description,
130 name: video.VideoChannel.Account.getDisplayName(),
131 link: video.VideoChannel.Account.Actor.url
134 date: video.publishedAt,
135 language: video.language,
140 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
141 height: THUMBNAILS_SIZE.height,
142 width: THUMBNAILS_SIZE.width
148 // Now the feed generation is done, let's send it!
149 return sendFeed(feed, req, res)
152 function initFeed (name: string, description: string) {
153 const webserverUrl = WEBSERVER.URL
158 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
161 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
162 favicon: webserverUrl + '/client/assets/images/favicon.png',
163 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
164 ` and potential licenses granted by each content's rightholder.`,
165 generator: `ToraifÅsu`, // ^.~
167 json: `${webserverUrl}/feeds/videos.json`,
168 atom: `${webserverUrl}/feeds/videos.atom`,
169 rss: `${webserverUrl}/feeds/videos.xml`
172 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
173 email: CONFIG.ADMIN.EMAIL,
174 link: `${webserverUrl}/about`
179 function sendFeed (feed, req: express.Request, res: express.Response) {
180 const format = req.params.format
182 if (format === 'atom' || format === 'atom1') {
183 res.set('Content-Type', 'application/atom+xml')
184 return res.send(feed.atom1()).end()
187 if (format === 'json' || format === 'json1') {
188 res.set('Content-Type', 'application/json')
189 return res.send(feed.json1()).end()
192 if (format === 'rss' || format === 'rss2') {
193 res.set('Content-Type', 'application/rss+xml')
194 return res.send(feed.rss2()).end()
197 // We're in the ambiguous '.xml' case and we look at the format query parameter
198 if (req.query.format === 'atom' || req.query.format === 'atom1') {
199 res.set('Content-Type', 'application/atom+xml')
200 return res.send(feed.atom1()).end()
203 res.set('Content-Type', 'application/rss+xml')
204 return res.send(feed.rss2()).end()