]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/feeds.ts
Deprecate filter video query
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
1 import express from 'express'
2 import Feed from 'pfeed'
3 import { getServerActor } from '@server/models/application/application'
4 import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
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 server = await getServerActor()
164 const { data } = await VideoModel.listForApi({
165 start,
166 count: FEEDS.COUNT,
167 sort: req.query.sort,
168 displayOnlyForFollower: {
169 actorId: server.id,
170 orLocalVideos: true
171 },
172 nsfw,
173 isLocal: req.query.isLocal,
174 include: req.query.include,
175 withFiles: true,
176 countVideos: false,
177 ...options
178 })
179
180 addVideosToFeed(feed, data)
181
182 // Now the feed generation is done, let's send it!
183 return sendFeed(feed, req, res)
184 }
185
186 async function generateVideoFeedForSubscriptions (req: express.Request, res: express.Response) {
187 const start = 0
188 const account = res.locals.account
189 const nsfw = buildNSFWFilter(res, req.query.nsfw)
190 const name = account.getDisplayName()
191 const description = account.description
192
193 const feed = initFeed({
194 name,
195 description,
196 resourceType: 'videos',
197 queryString: new URL(WEBSERVER.URL + req.url).search
198 })
199
200 const { data } = await VideoModel.listForApi({
201 start,
202 count: FEEDS.COUNT,
203 sort: req.query.sort,
204 nsfw,
205
206 isLocal: req.query.isLocal,
207 include: req.query.include,
208
209 withFiles: true,
210 countVideos: false,
211
212 displayOnlyForFollower: {
213 actorId: res.locals.user.Account.Actor.id,
214 orLocalVideos: false
215 },
216 user: res.locals.user
217 })
218
219 addVideosToFeed(feed, data)
220
221 // Now the feed generation is done, let's send it!
222 return sendFeed(feed, req, res)
223 }
224
225 function initFeed (parameters: {
226 name: string
227 description: string
228 resourceType?: 'videos' | 'video-comments'
229 queryString?: string
230 }) {
231 const webserverUrl = WEBSERVER.URL
232 const { name, description, resourceType, queryString } = parameters
233
234 return new Feed({
235 title: name,
236 description,
237 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
238 id: webserverUrl,
239 link: webserverUrl,
240 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
241 favicon: webserverUrl + '/client/assets/images/favicon.png',
242 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
243 ` and potential licenses granted by each content's rightholder.`,
244 generator: `Toraifōsu`, // ^.~
245 feedLinks: {
246 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
247 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
248 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
249 },
250 author: {
251 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
252 email: CONFIG.ADMIN.EMAIL,
253 link: `${webserverUrl}/about`
254 }
255 })
256 }
257
258 function addVideosToFeed (feed, videos: VideoModel[]) {
259 /**
260 * Adding video items to the feed object, one at a time
261 */
262 for (const video of videos) {
263 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
264
265 const torrents = formattedVideoFiles.map(videoFile => ({
266 title: video.name,
267 url: videoFile.torrentUrl,
268 size_in_bytes: videoFile.size
269 }))
270
271 const videos = formattedVideoFiles.map(videoFile => {
272 const result = {
273 type: 'video/mp4',
274 medium: 'video',
275 height: videoFile.resolution.label.replace('p', ''),
276 fileSize: videoFile.size,
277 url: videoFile.fileUrl,
278 framerate: videoFile.fps,
279 duration: video.duration
280 }
281
282 if (video.language) Object.assign(result, { lang: video.language })
283
284 return result
285 })
286
287 const categories: { value: number, label: string }[] = []
288 if (video.category) {
289 categories.push({
290 value: video.category,
291 label: getCategoryLabel(video.category)
292 })
293 }
294
295 feed.addItem({
296 title: video.name,
297 id: video.url,
298 link: WEBSERVER.URL + video.getWatchStaticPath(),
299 description: video.getTruncatedDescription(),
300 content: video.description,
301 author: [
302 {
303 name: video.VideoChannel.Account.getDisplayName(),
304 link: video.VideoChannel.Account.Actor.url
305 }
306 ],
307 date: video.publishedAt,
308 nsfw: video.nsfw,
309 torrent: torrents,
310 videos,
311 embed: {
312 url: video.getEmbedStaticPath(),
313 allowFullscreen: true
314 },
315 player: {
316 url: video.getWatchStaticPath()
317 },
318 categories,
319 community: {
320 statistics: {
321 views: video.views
322 }
323 },
324 thumbnail: [
325 {
326 url: WEBSERVER.URL + video.getPreviewStaticPath(),
327 height: PREVIEWS_SIZE.height,
328 width: PREVIEWS_SIZE.width
329 }
330 ]
331 })
332 }
333 }
334
335 function sendFeed (feed, req: express.Request, res: express.Response) {
336 const format = req.params.format
337
338 if (format === 'atom' || format === 'atom1') {
339 return res.send(feed.atom1()).end()
340 }
341
342 if (format === 'json' || format === 'json1') {
343 return res.send(feed.json1()).end()
344 }
345
346 if (format === 'rss' || format === 'rss2') {
347 return res.send(feed.rss2()).end()
348 }
349
350 // We're in the ambiguous '.xml' case and we look at the format query parameter
351 if (req.query.format === 'atom' || req.query.format === 'atom1') {
352 return res.send(feed.atom1()).end()
353 }
354
355 return res.send(feed.rss2()).end()
356 }