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