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