7dcaf7004f311e29bac7d43d701ff5d6e11161ed
[oweals/peertube.git] / server / controllers / feeds.ts
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 { ResultList } from '../../shared/models'
7 import { AccountModel } from '../models/account/account'
8 import { cacheRoute } from '../middlewares/cache'
9 import { VideoSortField } from '../../client/src/app/shared/video/sort-field.type'
10
11 const feedsRouter = express.Router()
12
13 feedsRouter.get('/feeds/videos.:format',
14   videosSortValidator,
15   setDefaultSort,
16   asyncMiddleware(feedsValidator),
17   asyncMiddleware(cacheRoute),
18   asyncMiddleware(generateFeed)
19 )
20
21 // ---------------------------------------------------------------------------
22
23 export {
24   feedsRouter
25 }
26
27 // ---------------------------------------------------------------------------
28
29 async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
30   let feed = initFeed()
31   const start = 0
32
33   const account: AccountModel = res.locals.account
34   const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
35
36   const resultList = await VideoModel.listForApi({
37     start,
38     count: FEEDS.COUNT,
39     sort: req.query.sort,
40     hideNSFW,
41     filter: req.query.filter,
42     withFiles: true,
43     accountId: account ? account.id : null
44   })
45
46   // Adding video items to the feed, one at a time
47   resultList.data.forEach(video => {
48     const formattedVideoFiles = video.getFormattedVideoFilesJSON()
49     const torrents = formattedVideoFiles.map(videoFile => ({
50       title: video.name,
51       url: videoFile.torrentUrl,
52       size_in_bytes: videoFile.size
53     }))
54
55     feed.addItem({
56       title: video.name,
57       id: video.url,
58       link: video.url,
59       description: video.getTruncatedDescription(),
60       content: video.description,
61       author: [
62         {
63           name: video.VideoChannel.Account.getDisplayName(),
64           link: video.VideoChannel.Account.Actor.url
65         }
66       ],
67       date: video.publishedAt,
68       language: video.language,
69       nsfw: video.nsfw,
70       torrent: torrents
71     })
72   })
73
74   // Now the feed generation is done, let's send it!
75   return sendFeed(feed, req, res)
76 }
77
78 function initFeed () {
79   const webserverUrl = CONFIG.WEBSERVER.URL
80
81   return new Feed({
82     title: CONFIG.INSTANCE.NAME,
83     description: CONFIG.INSTANCE.SHORT_DESCRIPTION,
84     // updated: TODO: somehowGetLatestUpdate, // optional, default = today
85     id: webserverUrl,
86     link: webserverUrl,
87     image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
88     favicon: webserverUrl + '/client/assets/images/favicon.png',
89     copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
90     ` and potential licenses granted by each content's rightholder.`,
91     generator: `Toraifōsu`, // ^.~
92     feedLinks: {
93       json: `${webserverUrl}/feeds/videos.json`,
94       atom: `${webserverUrl}/feeds/videos.atom`,
95       rss: `${webserverUrl}/feeds/videos.xml`
96     },
97     author: {
98       name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
99       email: CONFIG.ADMIN.EMAIL,
100       link: `${webserverUrl}/about`
101     }
102   })
103 }
104
105 function sendFeed (feed, req: express.Request, res: express.Response) {
106   const format = req.params.format
107
108   if (format === 'atom' || format === 'atom1') {
109     res.set('Content-Type', 'application/atom+xml')
110     return res.send(feed.atom1()).end()
111   }
112
113   if (format === 'json' || format === 'json1') {
114     res.set('Content-Type', 'application/json')
115     return res.send(feed.json1()).end()
116   }
117
118   if (format === 'rss' || format === 'rss2') {
119     res.set('Content-Type', 'application/rss+xml')
120     return res.send(feed.rss2()).end()
121   }
122
123   // We're in the ambiguous '.xml' case and we look at the format query parameter
124   if (req.query.format === 'atom' || req.query.format === 'atom1') {
125     res.set('Content-Type', 'application/atom+xml')
126     return res.send(feed.atom1()).end()
127   }
128
129   res.set('Content-Type', 'application/rss+xml')
130   return res.send(feed.rss2()).end()
131 }