]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import Feed from 'pfeed' | |
3 | import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils' | |
4 | import { VideoFilter } from '../../shared/models/videos/video-query.type' | |
5 | import { buildNSFWFilter } from '../helpers/express-utils' | |
6 | import { CONFIG } from '../initializers/config' | |
7 | import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants' | |
8 | import { | |
9 | asyncMiddleware, | |
10 | commonVideosFiltersValidator, | |
11 | feedsFormatValidator, | |
12 | setDefaultVideosSort, | |
13 | setFeedFormatContentType, | |
14 | videoCommentsFeedsValidator, | |
15 | videoFeedsValidator, | |
16 | videosSortValidator, | |
17 | videoSubscriptionFeedsValidator | |
18 | } from '../middlewares' | |
19 | import { cacheRouteFactory } from '../middlewares/cache/cache' | |
20 | import { VideoModel } from '../models/video/video' | |
21 | import { VideoCommentModel } from '../models/video/video-comment' | |
22 | ||
23 | const feedsRouter = express.Router() | |
24 | ||
25 | const cacheRoute = cacheRouteFactory({ | |
26 | headerBlacklist: [ 'Content-Type' ] | |
27 | }) | |
28 | ||
29 | feedsRouter.get('/feeds/video-comments.:format', | |
30 | feedsFormatValidator, | |
31 | setFeedFormatContentType, | |
32 | cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS), | |
33 | asyncMiddleware(videoFeedsValidator), | |
34 | asyncMiddleware(videoCommentsFeedsValidator), | |
35 | asyncMiddleware(generateVideoCommentsFeed) | |
36 | ) | |
37 | ||
38 | feedsRouter.get('/feeds/videos.:format', | |
39 | videosSortValidator, | |
40 | setDefaultVideosSort, | |
41 | feedsFormatValidator, | |
42 | setFeedFormatContentType, | |
43 | cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS), | |
44 | commonVideosFiltersValidator, | |
45 | asyncMiddleware(videoFeedsValidator), | |
46 | asyncMiddleware(generateVideoFeed) | |
47 | ) | |
48 | ||
49 | feedsRouter.get('/feeds/subscriptions.:format', | |
50 | videosSortValidator, | |
51 | setDefaultVideosSort, | |
52 | feedsFormatValidator, | |
53 | setFeedFormatContentType, | |
54 | cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS), | |
55 | commonVideosFiltersValidator, | |
56 | asyncMiddleware(videoSubscriptionFeedsValidator), | |
57 | asyncMiddleware(generateVideoFeedForSubscriptions) | |
58 | ) | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | feedsRouter | |
64 | } | |
65 | ||
66 | // --------------------------------------------------------------------------- | |
67 | ||
68 | async function generateVideoCommentsFeed (req: express.Request, res: express.Response) { | |
69 | const start = 0 | |
70 | const video = res.locals.videoAll | |
71 | const account = res.locals.account | |
72 | const videoChannel = res.locals.videoChannel | |
73 | ||
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 | }) | |
81 | ||
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 | }) | |
101 | ||
102 | // Adding video items to the feed, one at a time | |
103 | for (const comment of comments) { | |
104 | const link = WEBSERVER.URL + comment.getCommentStaticPath() | |
105 | ||
106 | let title = comment.Video.name | |
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 | } | |
116 | ||
117 | feed.addItem({ | |
118 | title, | |
119 | id: comment.url, | |
120 | link, | |
121 | content: comment.text, | |
122 | author, | |
123 | date: comment.createdAt | |
124 | }) | |
125 | } | |
126 | ||
127 | // Now the feed generation is done, let's send it! | |
128 | return sendFeed(feed, req, res) | |
129 | } | |
130 | ||
131 | async function generateVideoFeed (req: express.Request, res: express.Response) { | |
132 | const start = 0 | |
133 | const account = res.locals.account | |
134 | const videoChannel = res.locals.videoChannel | |
135 | const nsfw = buildNSFWFilter(res, req.query.nsfw) | |
136 | ||
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 | ||
151 | const feed = initFeed({ | |
152 | name, | |
153 | description, | |
154 | resourceType: 'videos', | |
155 | queryString: new URL(WEBSERVER.URL + req.url).search | |
156 | }) | |
157 | ||
158 | const options = { | |
159 | accountId: account ? account.id : null, | |
160 | videoChannelId: videoChannel ? videoChannel.id : null | |
161 | } | |
162 | ||
163 | const { data } = await VideoModel.listForApi({ | |
164 | start, | |
165 | count: FEEDS.COUNT, | |
166 | sort: req.query.sort, | |
167 | includeLocalVideos: true, | |
168 | nsfw, | |
169 | filter: req.query.filter as VideoFilter, | |
170 | withFiles: true, | |
171 | countVideos: false, | |
172 | ...options | |
173 | }) | |
174 | ||
175 | addVideosToFeed(feed, data) | |
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 | ||
195 | const { data } = await VideoModel.listForApi({ | |
196 | start, | |
197 | count: FEEDS.COUNT, | |
198 | sort: req.query.sort, | |
199 | includeLocalVideos: false, | |
200 | nsfw, | |
201 | filter: req.query.filter as VideoFilter, | |
202 | ||
203 | withFiles: true, | |
204 | countVideos: false, | |
205 | ||
206 | followerActorId: res.locals.user.Account.Actor.id, | |
207 | user: res.locals.user | |
208 | }) | |
209 | ||
210 | addVideosToFeed(feed, data) | |
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[]) { | |
250 | /** | |
251 | * Adding video items to the feed object, one at a time | |
252 | */ | |
253 | for (const video of videos) { | |
254 | const formattedVideoFiles = video.getFormattedVideoFilesJSON(false) | |
255 | ||
256 | const torrents = formattedVideoFiles.map(videoFile => ({ | |
257 | title: video.name, | |
258 | url: videoFile.torrentUrl, | |
259 | size_in_bytes: videoFile.size | |
260 | })) | |
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, | |
282 | label: getCategoryLabel(video.category) | |
283 | }) | |
284 | } | |
285 | ||
286 | feed.addItem({ | |
287 | title: video.name, | |
288 | id: video.url, | |
289 | link: WEBSERVER.URL + video.getWatchStaticPath(), | |
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, | |
299 | nsfw: video.nsfw, | |
300 | torrent: torrents, | |
301 | videos, | |
302 | embed: { | |
303 | url: video.getEmbedStaticPath(), | |
304 | allowFullscreen: true | |
305 | }, | |
306 | player: { | |
307 | url: video.getWatchStaticPath() | |
308 | }, | |
309 | categories, | |
310 | community: { | |
311 | statistics: { | |
312 | views: video.views | |
313 | } | |
314 | }, | |
315 | thumbnail: [ | |
316 | { | |
317 | url: WEBSERVER.URL + video.getPreviewStaticPath(), | |
318 | height: PREVIEWS_SIZE.height, | |
319 | width: PREVIEWS_SIZE.width | |
320 | } | |
321 | ] | |
322 | }) | |
323 | } | |
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') { | |
330 | return res.send(feed.atom1()).end() | |
331 | } | |
332 | ||
333 | if (format === 'json' || format === 'json1') { | |
334 | return res.send(feed.json1()).end() | |
335 | } | |
336 | ||
337 | if (format === 'rss' || format === 'rss2') { | |
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') { | |
343 | return res.send(feed.atom1()).end() | |
344 | } | |
345 | ||
346 | return res.send(feed.rss2()).end() | |
347 | } |