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