27ebecc404138a92e43fbbc8ebc9617fff5df43d
[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   let resultList: ResultList<VideoModel>
34   const account: AccountModel = res.locals.account
35   const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
36
37   if (account) {
38     resultList = await VideoModel.listAccountVideosForApi(
39       account.id,
40       start,
41       FEEDS.COUNT,
42       req.query.sort as VideoSortField,
43       hideNSFW
44     )
45   } else {
46     resultList = await VideoModel.listForApi(
47       start,
48       FEEDS.COUNT,
49       req.query.sort as VideoSortField,
50       hideNSFW,
51       req.query.filter,
52       true
53     )
54   }
55
56   // Adding video items to the feed, one at a time
57   resultList.data.forEach(video => {
58     const formattedVideoFiles = video.getFormattedVideoFilesJSON()
59     const torrents = formattedVideoFiles.map(videoFile => ({
60       title: video.name,
61       url: videoFile.torrentUrl,
62       size_in_bytes: videoFile.size
63     }))
64
65     feed.addItem({
66       title: video.name,
67       id: video.url,
68       link: video.url,
69       description: video.getTruncatedDescription(),
70       content: video.description,
71       author: [
72         {
73           name: video.VideoChannel.Account.getDisplayName(),
74           link: video.VideoChannel.Account.Actor.url
75         }
76       ],
77       date: video.publishedAt,
78       language: video.language,
79       nsfw: video.nsfw,
80       torrent: torrents
81     })
82   })
83
84   // Now the feed generation is done, let's send it!
85   return sendFeed(feed, req, res)
86 }
87
88 function initFeed () {
89   const webserverUrl = CONFIG.WEBSERVER.URL
90
91   return new Feed({
92     title: CONFIG.INSTANCE.NAME,
93     description: CONFIG.INSTANCE.SHORT_DESCRIPTION,
94     // updated: TODO: somehowGetLatestUpdate, // optional, default = today
95     id: webserverUrl,
96     link: webserverUrl,
97     image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
98     favicon: webserverUrl + '/client/assets/images/favicon.png',
99     copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
100     ` and potential licenses granted by each content's rightholder.`,
101     generator: `Toraifōsu`, // ^.~
102     feedLinks: {
103       json: `${webserverUrl}/feeds/videos.json`,
104       atom: `${webserverUrl}/feeds/videos.atom`,
105       rss: `${webserverUrl}/feeds/videos.xml`
106     },
107     author: {
108       name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
109       email: CONFIG.ADMIN.EMAIL,
110       link: `${webserverUrl}/about`
111     }
112   })
113 }
114
115 function sendFeed (feed, req: express.Request, res: express.Response) {
116   const format = req.params.format
117
118   if (format === 'atom' || format === 'atom1') {
119     res.set('Content-Type', 'application/atom+xml')
120     return res.send(feed.atom1()).end()
121   }
122
123   if (format === 'json' || format === 'json1') {
124     res.set('Content-Type', 'application/json')
125     return res.send(feed.json1()).end()
126   }
127
128   if (format === 'rss' || format === 'rss2') {
129     res.set('Content-Type', 'application/rss+xml')
130     return res.send(feed.rss2()).end()
131   }
132
133   // We're in the ambiguous '.xml' case and we look at the format query parameter
134   if (req.query.format === 'atom' || req.query.format === 'atom1') {
135     res.set('Content-Type', 'application/atom+xml')
136     return res.send(feed.atom1()).end()
137   }
138
139   res.set('Content-Type', 'application/rss+xml')
140   return res.send(feed.rss2()).end()
141 }