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