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