1 import * as express from 'express'
2 import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3 import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, videosSortValidator } from '../middlewares'
4 import { VideoModel } from '../models/video/video'
5 import * as Feed from 'pfeed'
6 import { AccountModel } from '../models/account/account'
7 import { cacheRoute } from '../middlewares/cache'
8 import { VideoChannelModel } from '../models/video/video-channel'
9 import { VideoCommentModel } from '../models/video/video-comment'
11 const feedsRouter = express.Router()
13 feedsRouter.get('/feeds/video-comments.:format',
14 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
15 asyncMiddleware(videoCommentsFeedsValidator),
16 asyncMiddleware(generateVideoCommentsFeed)
19 feedsRouter.get('/feeds/videos.:format',
22 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
23 asyncMiddleware(videoFeedsValidator),
24 asyncMiddleware(generateVideoFeed)
27 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const video = res.locals.video as VideoModel
39 const videoId: number = video ? video.id : undefined
41 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
43 const name = video ? video.name : CONFIG.INSTANCE.NAME
44 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
45 const feed = initFeed(name, description)
47 // Adding video items to the feed, one at a time
48 comments.forEach(comment => {
50 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
53 content: comment.text,
56 name: comment.Account.getDisplayName(),
57 link: comment.Account.Actor.url
60 date: comment.createdAt
64 // Now the feed generation is done, let's send it!
65 return sendFeed(feed, req, res)
68 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
71 const account: AccountModel = res.locals.account
72 const videoChannel: VideoChannelModel = res.locals.videoChannel
73 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
76 let description: string
79 name = videoChannel.getDisplayName()
80 description = videoChannel.description
82 name = account.getDisplayName()
83 description = account.description
85 name = CONFIG.INSTANCE.NAME
86 description = CONFIG.INSTANCE.DESCRIPTION
89 const feed = initFeed(name, description)
91 const resultList = await VideoModel.listForApi({
96 filter: req.query.filter,
98 accountId: account ? account.id : null,
99 videoChannelId: videoChannel ? videoChannel.id : null
102 // Adding video items to the feed, one at a time
103 resultList.data.forEach(video => {
104 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
105 const torrents = formattedVideoFiles.map(videoFile => ({
107 url: videoFile.torrentUrl,
108 size_in_bytes: videoFile.size
115 description: video.getTruncatedDescription(),
116 content: video.description,
119 name: video.VideoChannel.Account.getDisplayName(),
120 link: video.VideoChannel.Account.Actor.url
123 date: video.publishedAt,
124 language: video.language,
130 // Now the feed generation is done, let's send it!
131 return sendFeed(feed, req, res)
134 function initFeed (name: string, description: string) {
135 const webserverUrl = CONFIG.WEBSERVER.URL
140 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
143 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
144 favicon: webserverUrl + '/client/assets/images/favicon.png',
145 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
146 ` and potential licenses granted by each content's rightholder.`,
147 generator: `ToraifÅsu`, // ^.~
149 json: `${webserverUrl}/feeds/videos.json`,
150 atom: `${webserverUrl}/feeds/videos.atom`,
151 rss: `${webserverUrl}/feeds/videos.xml`
154 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
155 email: CONFIG.ADMIN.EMAIL,
156 link: `${webserverUrl}/about`
161 function sendFeed (feed, req: express.Request, res: express.Response) {
162 const format = req.params.format
164 if (format === 'atom' || format === 'atom1') {
165 res.set('Content-Type', 'application/atom+xml')
166 return res.send(feed.atom1()).end()
169 if (format === 'json' || format === 'json1') {
170 res.set('Content-Type', 'application/json')
171 return res.send(feed.json1()).end()
174 if (format === 'rss' || format === 'rss2') {
175 res.set('Content-Type', 'application/rss+xml')
176 return res.send(feed.rss2()).end()
179 // We're in the ambiguous '.xml' case and we look at the format query parameter
180 if (req.query.format === 'atom' || req.query.format === 'atom1') {
181 res.set('Content-Type', 'application/atom+xml')
182 return res.send(feed.atom1()).end()
185 res.set('Content-Type', 'application/rss+xml')
186 return res.send(feed.rss2()).end()