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