]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/feeds.ts
More robust actor image lazy load
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
1 import * as express from 'express'
2 import * as 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 { cacheRoute } from '../middlewares/cache'
20 import { VideoModel } from '../models/video/video'
21 import { VideoCommentModel } from '../models/video/video-comment'
22
23 const feedsRouter = express.Router()
24
25 feedsRouter.get('/feeds/video-comments.:format',
26 feedsFormatValidator,
27 setFeedFormatContentType,
28 asyncMiddleware(cacheRoute({
29 headerBlacklist: [
30 'Content-Type'
31 ]
32 })(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 asyncMiddleware(cacheRoute({
44 headerBlacklist: [
45 'Content-Type'
46 ]
47 })(ROUTE_CACHE_LIFETIME.FEEDS)),
48 commonVideosFiltersValidator,
49 asyncMiddleware(videoFeedsValidator),
50 asyncMiddleware(generateVideoFeed)
51 )
52
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,
64 asyncMiddleware(videoSubscriptionFeedsValidator),
65 asyncMiddleware(generateVideoFeedForSubscriptions)
66 )
67
68 // ---------------------------------------------------------------------------
69
70 export {
71 feedsRouter
72 }
73
74 // ---------------------------------------------------------------------------
75
76 async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
77 const start = 0
78 const video = res.locals.videoAll
79 const account = res.locals.account
80 const videoChannel = res.locals.videoChannel
81
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 })
89
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 })
109
110 // Adding video items to the feed, one at a time
111 for (const comment of comments) {
112 const link = WEBSERVER.URL + comment.getCommentStaticPath()
113
114 let title = comment.Video.name
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 }
124
125 feed.addItem({
126 title,
127 id: comment.url,
128 link,
129 content: comment.text,
130 author,
131 date: comment.createdAt
132 })
133 }
134
135 // Now the feed generation is done, let's send it!
136 return sendFeed(feed, req, res)
137 }
138
139 async function generateVideoFeed (req: express.Request, res: express.Response) {
140 const start = 0
141 const account = res.locals.account
142 const videoChannel = res.locals.videoChannel
143 const nsfw = buildNSFWFilter(res, req.query.nsfw)
144
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
159 const feed = initFeed({
160 name,
161 description,
162 resourceType: 'videos',
163 queryString: new URL(WEBSERVER.URL + req.url).search
164 })
165
166 const options = {
167 accountId: account ? account.id : null,
168 videoChannelId: videoChannel ? videoChannel.id : null
169 }
170
171 const { data } = await VideoModel.listForApi({
172 start,
173 count: FEEDS.COUNT,
174 sort: req.query.sort,
175 includeLocalVideos: true,
176 nsfw,
177 filter: req.query.filter as VideoFilter,
178 withFiles: true,
179 countVideos: false,
180 ...options
181 })
182
183 addVideosToFeed(feed, data)
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
203 const { data } = await VideoModel.listForApi({
204 start,
205 count: FEEDS.COUNT,
206 sort: req.query.sort,
207 includeLocalVideos: false,
208 nsfw,
209 filter: req.query.filter as VideoFilter,
210
211 withFiles: true,
212 countVideos: false,
213
214 followerActorId: res.locals.user.Account.Actor.id,
215 user: res.locals.user
216 })
217
218 addVideosToFeed(feed, data)
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[]) {
258 /**
259 * Adding video items to the feed object, one at a time
260 */
261 for (const video of videos) {
262 const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
263
264 const torrents = formattedVideoFiles.map(videoFile => ({
265 title: video.name,
266 url: videoFile.torrentUrl,
267 size_in_bytes: videoFile.size
268 }))
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,
290 label: getCategoryLabel(video.category)
291 })
292 }
293
294 feed.addItem({
295 title: video.name,
296 id: video.url,
297 link: WEBSERVER.URL + '/w/' + video.uuid,
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,
307 nsfw: video.nsfw,
308 torrent: torrents,
309 videos,
310 embed: {
311 url: video.getEmbedStaticPath(),
312 allowFullscreen: true
313 },
314 player: {
315 url: video.getWatchStaticPath()
316 },
317 categories,
318 community: {
319 statistics: {
320 views: video.views
321 }
322 },
323 thumbnail: [
324 {
325 url: WEBSERVER.URL + video.getPreviewStaticPath(),
326 height: PREVIEWS_SIZE.height,
327 width: PREVIEWS_SIZE.width
328 }
329 ]
330 })
331 }
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') {
338 return res.send(feed.atom1()).end()
339 }
340
341 if (format === 'json' || format === 'json1') {
342 return res.send(feed.json1()).end()
343 }
344
345 if (format === 'rss' || format === 'rss2') {
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') {
351 return res.send(feed.atom1()).end()
352 }
353
354 return res.send(feed.rss2()).end()
355 }