Move config in its own file
[oweals/peertube.git] / server / controllers / feeds.ts
1 import * as express from 'express'
2 import { FEEDS, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
3 import { THUMBNAILS_SIZE } from '../initializers'
4 import {
5   asyncMiddleware,
6   commonVideosFiltersValidator,
7   setDefaultSort,
8   videoCommentsFeedsValidator,
9   videoFeedsValidator,
10   videosSortValidator
11 } from '../middlewares'
12 import { VideoModel } from '../models/video/video'
13 import * as Feed from 'pfeed'
14 import { cacheRoute } from '../middlewares/cache'
15 import { VideoCommentModel } from '../models/video/video-comment'
16 import { buildNSFWFilter } from '../helpers/express-utils'
17 import { CONFIG } from '../initializers/config'
18
19 const feedsRouter = express.Router()
20
21 feedsRouter.get('/feeds/video-comments.:format',
22   asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
23   asyncMiddleware(videoCommentsFeedsValidator),
24   asyncMiddleware(generateVideoCommentsFeed)
25 )
26
27 feedsRouter.get('/feeds/videos.:format',
28   videosSortValidator,
29   setDefaultSort,
30   asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
31   commonVideosFiltersValidator,
32   asyncMiddleware(videoFeedsValidator),
33   asyncMiddleware(generateVideoFeed)
34 )
35
36 // ---------------------------------------------------------------------------
37
38 export {
39   feedsRouter
40 }
41
42 // ---------------------------------------------------------------------------
43
44 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
45   const start = 0
46
47   const video = res.locals.video
48   const videoId: number = video ? video.id : undefined
49
50   const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
51
52   const name = video ? video.name : CONFIG.INSTANCE.NAME
53   const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
54   const feed = initFeed(name, description)
55
56   // Adding video items to the feed, one at a time
57   comments.forEach(comment => {
58     const link = WEBSERVER.URL + comment.getCommentStaticPath()
59
60     feed.addItem({
61       title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
62       id: comment.url,
63       link,
64       content: comment.text,
65       author: [
66         {
67           name: comment.Account.getDisplayName(),
68           link: comment.Account.Actor.url
69         }
70       ],
71       date: comment.createdAt
72     })
73   })
74
75   // Now the feed generation is done, let's send it!
76   return sendFeed(feed, req, res)
77 }
78
79 async function generateVideoFeed (req: express.Request, res: express.Response) {
80   const start = 0
81
82   const account = res.locals.account
83   const videoChannel = res.locals.videoChannel
84   const nsfw = buildNSFWFilter(res, req.query.nsfw)
85
86   let name: string
87   let description: string
88
89   if (videoChannel) {
90     name = videoChannel.getDisplayName()
91     description = videoChannel.description
92   } else if (account) {
93     name = account.getDisplayName()
94     description = account.description
95   } else {
96     name = CONFIG.INSTANCE.NAME
97     description = CONFIG.INSTANCE.DESCRIPTION
98   }
99
100   const feed = initFeed(name, description)
101
102   const resultList = await VideoModel.listForApi({
103     start,
104     count: FEEDS.COUNT,
105     sort: req.query.sort,
106     includeLocalVideos: true,
107     nsfw,
108     filter: req.query.filter,
109     withFiles: true,
110     accountId: account ? account.id : null,
111     videoChannelId: videoChannel ? videoChannel.id : null
112   })
113
114   // Adding video items to the feed, one at a time
115   resultList.data.forEach(video => {
116     const formattedVideoFiles = video.getFormattedVideoFilesJSON()
117     const torrents = formattedVideoFiles.map(videoFile => ({
118       title: video.name,
119       url: videoFile.torrentUrl,
120       size_in_bytes: videoFile.size
121     }))
122
123     feed.addItem({
124       title: video.name,
125       id: video.url,
126       link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
127       description: video.getTruncatedDescription(),
128       content: video.description,
129       author: [
130         {
131           name: video.VideoChannel.Account.getDisplayName(),
132           link: video.VideoChannel.Account.Actor.url
133         }
134       ],
135       date: video.publishedAt,
136       language: video.language,
137       nsfw: video.nsfw,
138       torrent: torrents,
139       thumbnail: [
140         {
141           url: WEBSERVER.URL + video.getThumbnailStaticPath(),
142           height: THUMBNAILS_SIZE.height,
143           width: THUMBNAILS_SIZE.width
144         }
145       ]
146     })
147   })
148
149   // Now the feed generation is done, let's send it!
150   return sendFeed(feed, req, res)
151 }
152
153 function initFeed (name: string, description: string) {
154   const webserverUrl = WEBSERVER.URL
155
156   return new Feed({
157     title: name,
158     description,
159     // updated: TODO: somehowGetLatestUpdate, // optional, default = today
160     id: webserverUrl,
161     link: webserverUrl,
162     image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
163     favicon: webserverUrl + '/client/assets/images/favicon.png',
164     copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
165     ` and potential licenses granted by each content's rightholder.`,
166     generator: `Toraifōsu`, // ^.~
167     feedLinks: {
168       json: `${webserverUrl}/feeds/videos.json`,
169       atom: `${webserverUrl}/feeds/videos.atom`,
170       rss: `${webserverUrl}/feeds/videos.xml`
171     },
172     author: {
173       name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
174       email: CONFIG.ADMIN.EMAIL,
175       link: `${webserverUrl}/about`
176     }
177   })
178 }
179
180 function sendFeed (feed, req: express.Request, res: express.Response) {
181   const format = req.params.format
182
183   if (format === 'atom' || format === 'atom1') {
184     res.set('Content-Type', 'application/atom+xml')
185     return res.send(feed.atom1()).end()
186   }
187
188   if (format === 'json' || format === 'json1') {
189     res.set('Content-Type', 'application/json')
190     return res.send(feed.json1()).end()
191   }
192
193   if (format === 'rss' || format === 'rss2') {
194     res.set('Content-Type', 'application/rss+xml')
195     return res.send(feed.rss2()).end()
196   }
197
198   // We're in the ambiguous '.xml' case and we look at the format query parameter
199   if (req.query.format === 'atom' || req.query.format === 'atom1') {
200     res.set('Content-Type', 'application/atom+xml')
201     return res.send(feed.atom1()).end()
202   }
203
204   res.set('Content-Type', 'application/rss+xml')
205   return res.send(feed.rss2()).end()
206 }