1 import * as express from 'express'
2 import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3 import { asyncMiddleware, videoFeedsValidator, setDefaultSort, videosSortValidator, videoCommentsFeedsValidator } 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) {
39 const videoId: number = res.locals.video ? res.locals.video.id : undefined
41 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
43 // Adding video items to the feed, one at a time
44 comments.forEach(comment => {
46 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
49 content: comment.text,
52 name: comment.Account.getDisplayName(),
53 link: comment.Account.Actor.url
56 date: comment.createdAt
60 // Now the feed generation is done, let's send it!
61 return sendFeed(feed, req, res)
64 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
68 const account: AccountModel = res.locals.account
69 const videoChannel: VideoChannelModel = res.locals.videoChannel
70 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
72 const resultList = await VideoModel.listForApi({
77 filter: req.query.filter,
79 accountId: account ? account.id : null,
80 videoChannelId: videoChannel ? videoChannel.id : null
83 // Adding video items to the feed, one at a time
84 resultList.data.forEach(video => {
85 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
86 const torrents = formattedVideoFiles.map(videoFile => ({
88 url: videoFile.torrentUrl,
89 size_in_bytes: videoFile.size
96 description: video.getTruncatedDescription(),
97 content: video.description,
100 name: video.VideoChannel.Account.getDisplayName(),
101 link: video.VideoChannel.Account.Actor.url
104 date: video.publishedAt,
105 language: video.language,
111 // Now the feed generation is done, let's send it!
112 return sendFeed(feed, req, res)
115 function initFeed () {
116 const webserverUrl = CONFIG.WEBSERVER.URL
119 title: CONFIG.INSTANCE.NAME,
120 description: CONFIG.INSTANCE.DESCRIPTION,
121 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
124 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
125 favicon: webserverUrl + '/client/assets/images/favicon.png',
126 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
127 ` and potential licenses granted by each content's rightholder.`,
128 generator: `ToraifÅsu`, // ^.~
130 json: `${webserverUrl}/feeds/videos.json`,
131 atom: `${webserverUrl}/feeds/videos.atom`,
132 rss: `${webserverUrl}/feeds/videos.xml`
135 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
136 email: CONFIG.ADMIN.EMAIL,
137 link: `${webserverUrl}/about`
142 function sendFeed (feed, req: express.Request, res: express.Response) {
143 const format = req.params.format
145 if (format === 'atom' || format === 'atom1') {
146 res.set('Content-Type', 'application/atom+xml')
147 return res.send(feed.atom1()).end()
150 if (format === 'json' || format === 'json1') {
151 res.set('Content-Type', 'application/json')
152 return res.send(feed.json1()).end()
155 if (format === 'rss' || format === 'rss2') {
156 res.set('Content-Type', 'application/rss+xml')
157 return res.send(feed.rss2()).end()
160 // We're in the ambiguous '.xml' case and we look at the format query parameter
161 if (req.query.format === 'atom' || req.query.format === 'atom1') {
162 res.set('Content-Type', 'application/atom+xml')
163 return res.send(feed.atom1()).end()
166 res.set('Content-Type', 'application/rss+xml')
167 return res.send(feed.rss2()).end()