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