1 import * as express from 'express'
2 import { CONFIG, FEEDS } from '../initializers/constants'
3 import { asyncMiddleware, feedsValidator, setDefaultSort, 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'
10 const feedsRouter = express.Router()
12 feedsRouter.get('/feeds/videos.:format',
15 asyncMiddleware(feedsValidator),
16 asyncMiddleware(cacheRoute),
17 asyncMiddleware(generateFeed)
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
32 const account: AccountModel = res.locals.account
33 const videoChannel: VideoChannelModel = res.locals.videoChannel
34 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
36 const resultList = await VideoModel.listForApi({
41 filter: req.query.filter,
43 accountId: account ? account.id : null,
44 videoChannelId: videoChannel ? videoChannel.id : null
47 // Adding video items to the feed, one at a time
48 resultList.data.forEach(video => {
49 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
50 const torrents = formattedVideoFiles.map(videoFile => ({
52 url: videoFile.torrentUrl,
53 size_in_bytes: videoFile.size
60 description: video.getTruncatedDescription(),
61 content: video.description,
64 name: video.VideoChannel.Account.getDisplayName(),
65 link: video.VideoChannel.Account.Actor.url
68 date: video.publishedAt,
69 language: video.language,
75 // Now the feed generation is done, let's send it!
76 return sendFeed(feed, req, res)
79 function initFeed () {
80 const webserverUrl = CONFIG.WEBSERVER.URL
83 title: CONFIG.INSTANCE.NAME,
84 description: CONFIG.INSTANCE.SHORT_DESCRIPTION,
85 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
88 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
89 favicon: webserverUrl + '/client/assets/images/favicon.png',
90 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
91 ` and potential licenses granted by each content's rightholder.`,
92 generator: `ToraifÅsu`, // ^.~
94 json: `${webserverUrl}/feeds/videos.json`,
95 atom: `${webserverUrl}/feeds/videos.atom`,
96 rss: `${webserverUrl}/feeds/videos.xml`
99 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
100 email: CONFIG.ADMIN.EMAIL,
101 link: `${webserverUrl}/about`
106 function sendFeed (feed, req: express.Request, res: express.Response) {
107 const format = req.params.format
109 if (format === 'atom' || format === 'atom1') {
110 res.set('Content-Type', 'application/atom+xml')
111 return res.send(feed.atom1()).end()
114 if (format === 'json' || format === 'json1') {
115 res.set('Content-Type', 'application/json')
116 return res.send(feed.json1()).end()
119 if (format === 'rss' || format === 'rss2') {
120 res.set('Content-Type', 'application/rss+xml')
121 return res.send(feed.rss2()).end()
124 // We're in the ambiguous '.xml' case and we look at the format query parameter
125 if (req.query.format === 'atom' || req.query.format === 'atom1') {
126 res.set('Content-Type', 'application/atom+xml')
127 return res.send(feed.atom1()).end()
130 res.set('Content-Type', 'application/rss+xml')
131 return res.send(feed.rss2()).end()