]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/feeds.ts
refactor scoped token service
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
CommitLineData
244e76a5 1import * as express from 'express'
8054669f
C
2import * as Feed from 'pfeed'
3import { buildNSFWFilter } from '../helpers/express-utils'
4import { CONFIG } from '../initializers/config'
74dc3bca 5import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
1cd3facc
C
6import {
7 asyncMiddleware,
8 commonVideosFiltersValidator,
8054669f
C
9 feedsFormatValidator,
10 setDefaultVideosSort,
11 setFeedFormatContentType,
1cd3facc
C
12 videoCommentsFeedsValidator,
13 videoFeedsValidator,
afff310e
RK
14 videosSortValidator,
15 videoSubscriptonFeedsValidator
1cd3facc 16} from '../middlewares'
98d3324d 17import { cacheRoute } from '../middlewares/cache'
8054669f 18import { VideoModel } from '../models/video/video'
fe3a55b0 19import { VideoCommentModel } from '../models/video/video-comment'
afff310e 20import { VideoFilter } from '../../shared/models/videos/video-query.type'
244e76a5
RK
21
22const feedsRouter = express.Router()
23
fe3a55b0 24feedsRouter.get('/feeds/video-comments.:format',
f2f0eda5
RK
25 feedsFormatValidator,
26 setFeedFormatContentType,
27 asyncMiddleware(cacheRoute({
28 headerBlacklist: [
29 'Content-Type'
30 ]
31 })(ROUTE_CACHE_LIFETIME.FEEDS)),
00494d6e 32 asyncMiddleware(videoFeedsValidator),
fe3a55b0
C
33 asyncMiddleware(videoCommentsFeedsValidator),
34 asyncMiddleware(generateVideoCommentsFeed)
35)
36
244e76a5 37feedsRouter.get('/feeds/videos.:format',
7b87d2d5 38 videosSortValidator,
8054669f 39 setDefaultVideosSort,
f2f0eda5
RK
40 feedsFormatValidator,
41 setFeedFormatContentType,
42 asyncMiddleware(cacheRoute({
43 headerBlacklist: [
44 'Content-Type'
45 ]
46 })(ROUTE_CACHE_LIFETIME.FEEDS)),
1cd3facc 47 commonVideosFiltersValidator,
fe3a55b0
C
48 asyncMiddleware(videoFeedsValidator),
49 asyncMiddleware(generateVideoFeed)
244e76a5
RK
50)
51
5beb89f2
RK
52feedsRouter.get('/feeds/subscriptions.:format',
53 videosSortValidator,
54 setDefaultVideosSort,
55 feedsFormatValidator,
56 setFeedFormatContentType,
57 asyncMiddleware(cacheRoute({
58 headerBlacklist: [
59 'Content-Type'
60 ]
61 })(ROUTE_CACHE_LIFETIME.FEEDS)),
62 commonVideosFiltersValidator,
63 asyncMiddleware(videoSubscriptonFeedsValidator),
64 asyncMiddleware(generateVideoFeedForSubscriptions)
65)
66
244e76a5
RK
67// ---------------------------------------------------------------------------
68
69export {
70 feedsRouter
71}
72
73// ---------------------------------------------------------------------------
74
dae86118 75async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
fe3a55b0 76 const start = 0
453e83ea 77 const video = res.locals.videoAll
00494d6e
RK
78 const account = res.locals.account
79 const videoChannel = res.locals.videoChannel
fe3a55b0 80
00494d6e
RK
81 const comments = await VideoCommentModel.listForFeed({
82 start,
83 count: FEEDS.COUNT,
84 videoId: video ? video.id : undefined,
85 accountId: account ? account.id : undefined,
86 videoChannelId: videoChannel ? videoChannel.id : undefined
87 })
fe3a55b0 88
00494d6e
RK
89 let name: string
90 let description: string
91
92 if (videoChannel) {
93 name = videoChannel.getDisplayName()
94 description = videoChannel.description
95 } else if (account) {
96 name = account.getDisplayName()
97 description = account.description
98 } else {
99 name = video ? video.name : CONFIG.INSTANCE.NAME
100 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
101 }
102 const feed = initFeed({
103 name,
104 description,
105 resourceType: 'video-comments',
106 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
107 })
749c7247 108
fe3a55b0 109 // Adding video items to the feed, one at a time
68b6fd21 110 for (const comment of comments) {
6dd9de95 111 const link = WEBSERVER.URL + comment.getCommentStaticPath()
4dae00e6 112
65d2ae2a 113 let title = comment.Video.name
c3340977
C
114 const author: { name: string, link: string }[] = []
115
116 if (comment.Account) {
117 title += ` - ${comment.Account.getDisplayName()}`
118 author.push({
119 name: comment.Account.getDisplayName(),
120 link: comment.Account.Actor.url
121 })
122 }
65d2ae2a 123
fe3a55b0 124 feed.addItem({
65d2ae2a 125 title,
fe3a55b0 126 id: comment.url,
4dae00e6 127 link,
fe3a55b0 128 content: comment.text,
c3340977 129 author,
fe3a55b0
C
130 date: comment.createdAt
131 })
68b6fd21 132 }
fe3a55b0
C
133
134 // Now the feed generation is done, let's send it!
135 return sendFeed(feed, req, res)
136}
137
dae86118 138async function generateVideoFeed (req: express.Request, res: express.Response) {
4195cd2b 139 const start = 0
dae86118
C
140 const account = res.locals.account
141 const videoChannel = res.locals.videoChannel
d525fc39 142 const nsfw = buildNSFWFilter(res, req.query.nsfw)
244e76a5 143
749c7247
C
144 let name: string
145 let description: string
146
147 if (videoChannel) {
148 name = videoChannel.getDisplayName()
149 description = videoChannel.description
150 } else if (account) {
151 name = account.getDisplayName()
152 description = account.description
153 } else {
154 name = CONFIG.INSTANCE.NAME
155 description = CONFIG.INSTANCE.DESCRIPTION
156 }
157
00494d6e
RK
158 const feed = initFeed({
159 name,
160 description,
161 resourceType: 'videos',
162 queryString: new URL(WEBSERVER.URL + req.url).search
163 })
749c7247 164
5beb89f2
RK
165 const options = {
166 accountId: account ? account.id : null,
167 videoChannelId: videoChannel ? videoChannel.id : null
168 }
afff310e 169
48dce1c9 170 const resultList = await VideoModel.listForApi({
0626e7af 171 start,
48dce1c9
C
172 count: FEEDS.COUNT,
173 sort: req.query.sort,
06a05d5f 174 includeLocalVideos: true,
d525fc39 175 nsfw,
afff310e 176 filter: req.query.filter as VideoFilter,
48dce1c9 177 withFiles: true,
afff310e 178 ...options
48dce1c9 179 })
244e76a5 180
5beb89f2
RK
181 addVideosToFeed(feed, resultList.data)
182
183 // Now the feed generation is done, let's send it!
184 return sendFeed(feed, req, res)
185}
186
187async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
188 const start = 0
189 const account = res.locals.account
190 const nsfw = buildNSFWFilter(res, req.query.nsfw)
191 const name = account.getDisplayName()
192 const description = account.description
193
194 const feed = initFeed({
195 name,
196 description,
197 resourceType: 'videos',
198 queryString: new URL(WEBSERVER.URL + req.url).search
199 })
200
201 const options = {
202 followerActorId: res.locals.user.Account.Actor.id,
203 user: res.locals.user
204 }
205
206 const resultList = await VideoModel.listForApi({
207 start,
208 count: FEEDS.COUNT,
209 sort: req.query.sort,
210 includeLocalVideos: true,
211 nsfw,
212 filter: req.query.filter as VideoFilter,
213 withFiles: true,
214 ...options
215 })
216
217 addVideosToFeed(feed, resultList.data)
218
219 // Now the feed generation is done, let's send it!
220 return sendFeed(feed, req, res)
221}
222
223function initFeed (parameters: {
224 name: string
225 description: string
226 resourceType?: 'videos' | 'video-comments'
227 queryString?: string
228}) {
229 const webserverUrl = WEBSERVER.URL
230 const { name, description, resourceType, queryString } = parameters
231
232 return new Feed({
233 title: name,
234 description,
235 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
236 id: webserverUrl,
237 link: webserverUrl,
238 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
239 favicon: webserverUrl + '/client/assets/images/favicon.png',
240 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
241 ` and potential licenses granted by each content's rightholder.`,
242 generator: `Toraifōsu`, // ^.~
243 feedLinks: {
244 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
245 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
246 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
247 },
248 author: {
249 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
250 email: CONFIG.ADMIN.EMAIL,
251 link: `${webserverUrl}/about`
252 }
253 })
254}
255
256function addVideosToFeed (feed, videos: VideoModel[]) {
afff310e
RK
257 /**
258 * Adding video items to the feed object, one at a time
259 */
5beb89f2 260 for (const video of videos) {
244e76a5 261 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
c4b4ab71 262
244e76a5
RK
263 const torrents = formattedVideoFiles.map(videoFile => ({
264 title: video.name,
265 url: videoFile.torrentUrl,
266 size_in_bytes: videoFile.size
267 }))
c4b4ab71
C
268
269 const videos = formattedVideoFiles.map(videoFile => {
270 const result = {
271 type: 'video/mp4',
272 medium: 'video',
273 height: videoFile.resolution.label.replace('p', ''),
274 fileSize: videoFile.size,
275 url: videoFile.fileUrl,
276 framerate: videoFile.fps,
277 duration: video.duration
278 }
279
280 if (video.language) Object.assign(result, { lang: video.language })
281
282 return result
283 })
284
285 const categories: { value: number, label: string }[] = []
286 if (video.category) {
287 categories.push({
288 value: video.category,
289 label: VideoModel.getCategoryLabel(video.category)
290 })
291 }
244e76a5
RK
292
293 feed.addItem({
294 title: video.name,
295 id: video.url,
6dd9de95 296 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
244e76a5
RK
297 description: video.getTruncatedDescription(),
298 content: video.description,
299 author: [
300 {
301 name: video.VideoChannel.Account.getDisplayName(),
302 link: video.VideoChannel.Account.Actor.url
303 }
304 ],
305 date: video.publishedAt,
244e76a5 306 nsfw: video.nsfw,
b81eb8fd 307 torrent: torrents,
16d9224a
RK
308 videos,
309 embed: {
310 url: video.getEmbedStaticPath(),
311 allowFullscreen: true
312 },
313 player: {
314 url: video.getWatchStaticPath()
315 },
c4b4ab71 316 categories,
16d9224a
RK
317 community: {
318 statistics: {
319 views: video.views
320 }
321 },
b81eb8fd
RK
322 thumbnail: [
323 {
3acc5084 324 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
b81eb8fd
RK
325 height: THUMBNAILS_SIZE.height,
326 width: THUMBNAILS_SIZE.width
327 }
328 ]
244e76a5 329 })
5beb89f2 330 }
244e76a5
RK
331}
332
333function sendFeed (feed, req: express.Request, res: express.Response) {
334 const format = req.params.format
335
336 if (format === 'atom' || format === 'atom1') {
244e76a5
RK
337 return res.send(feed.atom1()).end()
338 }
339
340 if (format === 'json' || format === 'json1') {
244e76a5
RK
341 return res.send(feed.json1()).end()
342 }
343
344 if (format === 'rss' || format === 'rss2') {
244e76a5
RK
345 return res.send(feed.rss2()).end()
346 }
347
348 // We're in the ambiguous '.xml' case and we look at the format query parameter
349 if (req.query.format === 'atom' || req.query.format === 'atom1') {
244e76a5
RK
350 return res.send(feed.atom1()).end()
351 }
352
244e76a5
RK
353 return res.send(feed.rss2()).end()
354}