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