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