]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/feeds.ts
add client overrides to nginx configuration (#3297)
[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,
8054669f 14 videosSortValidator
1cd3facc 15} from '../middlewares'
98d3324d 16import { cacheRoute } from '../middlewares/cache'
8054669f 17import { VideoModel } from '../models/video/video'
fe3a55b0 18import { VideoCommentModel } from '../models/video/video-comment'
244e76a5
RK
19
20const feedsRouter = express.Router()
21
fe3a55b0 22feedsRouter.get('/feeds/video-comments.:format',
f2f0eda5
RK
23 feedsFormatValidator,
24 setFeedFormatContentType,
25 asyncMiddleware(cacheRoute({
26 headerBlacklist: [
27 'Content-Type'
28 ]
29 })(ROUTE_CACHE_LIFETIME.FEEDS)),
00494d6e 30 asyncMiddleware(videoFeedsValidator),
fe3a55b0
C
31 asyncMiddleware(videoCommentsFeedsValidator),
32 asyncMiddleware(generateVideoCommentsFeed)
33)
34
244e76a5 35feedsRouter.get('/feeds/videos.:format',
7b87d2d5 36 videosSortValidator,
8054669f 37 setDefaultVideosSort,
f2f0eda5
RK
38 feedsFormatValidator,
39 setFeedFormatContentType,
40 asyncMiddleware(cacheRoute({
41 headerBlacklist: [
42 'Content-Type'
43 ]
44 })(ROUTE_CACHE_LIFETIME.FEEDS)),
1cd3facc 45 commonVideosFiltersValidator,
fe3a55b0
C
46 asyncMiddleware(videoFeedsValidator),
47 asyncMiddleware(generateVideoFeed)
244e76a5
RK
48)
49
50// ---------------------------------------------------------------------------
51
52export {
53 feedsRouter
54}
55
56// ---------------------------------------------------------------------------
57
dae86118 58async function generateVideoCommentsFeed (req: express.Request, res: express.Response) {
fe3a55b0
C
59 const start = 0
60
453e83ea 61 const video = res.locals.videoAll
00494d6e
RK
62 const account = res.locals.account
63 const videoChannel = res.locals.videoChannel
fe3a55b0 64
00494d6e
RK
65 const comments = await VideoCommentModel.listForFeed({
66 start,
67 count: FEEDS.COUNT,
68 videoId: video ? video.id : undefined,
69 accountId: account ? account.id : undefined,
70 videoChannelId: videoChannel ? videoChannel.id : undefined
71 })
fe3a55b0 72
00494d6e
RK
73 let name: string
74 let description: string
75
76 if (videoChannel) {
77 name = videoChannel.getDisplayName()
78 description = videoChannel.description
79 } else if (account) {
80 name = account.getDisplayName()
81 description = account.description
82 } else {
83 name = video ? video.name : CONFIG.INSTANCE.NAME
84 description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
85 }
86 const feed = initFeed({
87 name,
88 description,
89 resourceType: 'video-comments',
90 queryString: new URL(WEBSERVER.URL + req.originalUrl).search
91 })
749c7247 92
fe3a55b0 93 // Adding video items to the feed, one at a time
68b6fd21 94 for (const comment of comments) {
6dd9de95 95 const link = WEBSERVER.URL + comment.getCommentStaticPath()
4dae00e6 96
65d2ae2a 97 let title = comment.Video.name
c3340977
C
98 const author: { name: string, link: string }[] = []
99
100 if (comment.Account) {
101 title += ` - ${comment.Account.getDisplayName()}`
102 author.push({
103 name: comment.Account.getDisplayName(),
104 link: comment.Account.Actor.url
105 })
106 }
65d2ae2a 107
fe3a55b0 108 feed.addItem({
65d2ae2a 109 title,
fe3a55b0 110 id: comment.url,
4dae00e6 111 link,
fe3a55b0 112 content: comment.text,
c3340977 113 author,
fe3a55b0
C
114 date: comment.createdAt
115 })
68b6fd21 116 }
fe3a55b0
C
117
118 // Now the feed generation is done, let's send it!
119 return sendFeed(feed, req, res)
120}
121
dae86118 122async function generateVideoFeed (req: express.Request, res: express.Response) {
4195cd2b 123 const start = 0
244e76a5 124
dae86118
C
125 const account = res.locals.account
126 const videoChannel = res.locals.videoChannel
d525fc39 127 const nsfw = buildNSFWFilter(res, req.query.nsfw)
244e76a5 128
749c7247
C
129 let name: string
130 let description: string
131
132 if (videoChannel) {
133 name = videoChannel.getDisplayName()
134 description = videoChannel.description
135 } else if (account) {
136 name = account.getDisplayName()
137 description = account.description
138 } else {
139 name = CONFIG.INSTANCE.NAME
140 description = CONFIG.INSTANCE.DESCRIPTION
141 }
142
00494d6e
RK
143 const feed = initFeed({
144 name,
145 description,
146 resourceType: 'videos',
147 queryString: new URL(WEBSERVER.URL + req.url).search
148 })
749c7247 149
48dce1c9 150 const resultList = await VideoModel.listForApi({
0626e7af 151 start,
48dce1c9
C
152 count: FEEDS.COUNT,
153 sort: req.query.sort,
06a05d5f 154 includeLocalVideos: true,
d525fc39 155 nsfw,
48dce1c9
C
156 filter: req.query.filter,
157 withFiles: true,
e0ea4b1d
C
158 accountId: account ? account.id : null,
159 videoChannelId: videoChannel ? videoChannel.id : null
48dce1c9 160 })
244e76a5
RK
161
162 // Adding video items to the feed, one at a time
163 resultList.data.forEach(video => {
164 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
c4b4ab71 165
244e76a5
RK
166 const torrents = formattedVideoFiles.map(videoFile => ({
167 title: video.name,
168 url: videoFile.torrentUrl,
169 size_in_bytes: videoFile.size
170 }))
c4b4ab71
C
171
172 const videos = formattedVideoFiles.map(videoFile => {
173 const result = {
174 type: 'video/mp4',
175 medium: 'video',
176 height: videoFile.resolution.label.replace('p', ''),
177 fileSize: videoFile.size,
178 url: videoFile.fileUrl,
179 framerate: videoFile.fps,
180 duration: video.duration
181 }
182
183 if (video.language) Object.assign(result, { lang: video.language })
184
185 return result
186 })
187
188 const categories: { value: number, label: string }[] = []
189 if (video.category) {
190 categories.push({
191 value: video.category,
192 label: VideoModel.getCategoryLabel(video.category)
193 })
194 }
244e76a5
RK
195
196 feed.addItem({
197 title: video.name,
198 id: video.url,
6dd9de95 199 link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
244e76a5
RK
200 description: video.getTruncatedDescription(),
201 content: video.description,
202 author: [
203 {
204 name: video.VideoChannel.Account.getDisplayName(),
205 link: video.VideoChannel.Account.Actor.url
206 }
207 ],
208 date: video.publishedAt,
244e76a5 209 nsfw: video.nsfw,
b81eb8fd 210 torrent: torrents,
16d9224a
RK
211 videos,
212 embed: {
213 url: video.getEmbedStaticPath(),
214 allowFullscreen: true
215 },
216 player: {
217 url: video.getWatchStaticPath()
218 },
c4b4ab71 219 categories,
16d9224a
RK
220 community: {
221 statistics: {
222 views: video.views
223 }
224 },
b81eb8fd
RK
225 thumbnail: [
226 {
3acc5084 227 url: WEBSERVER.URL + video.getMiniatureStaticPath(),
b81eb8fd
RK
228 height: THUMBNAILS_SIZE.height,
229 width: THUMBNAILS_SIZE.width
230 }
231 ]
244e76a5
RK
232 })
233 })
234
235 // Now the feed generation is done, let's send it!
236 return sendFeed(feed, req, res)
237}
238
00494d6e
RK
239function initFeed (parameters: {
240 name: string
241 description: string
242 resourceType?: 'videos' | 'video-comments'
243 queryString?: string
244}) {
6dd9de95 245 const webserverUrl = WEBSERVER.URL
00494d6e 246 const { name, description, resourceType, queryString } = parameters
244e76a5
RK
247
248 return new Feed({
749c7247
C
249 title: name,
250 description,
244e76a5
RK
251 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
252 id: webserverUrl,
253 link: webserverUrl,
254 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
255 favicon: webserverUrl + '/client/assets/images/favicon.png',
256 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
257 ` and potential licenses granted by each content's rightholder.`,
258 generator: `Toraifōsu`, // ^.~
259 feedLinks: {
00494d6e
RK
260 json: `${webserverUrl}/feeds/${resourceType}.json${queryString}`,
261 atom: `${webserverUrl}/feeds/${resourceType}.atom${queryString}`,
262 rss: `${webserverUrl}/feeds/${resourceType}.xml${queryString}`
244e76a5
RK
263 },
264 author: {
7b87d2d5 265 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
244e76a5
RK
266 email: CONFIG.ADMIN.EMAIL,
267 link: `${webserverUrl}/about`
268 }
269 })
270}
271
272function sendFeed (feed, req: express.Request, res: express.Response) {
273 const format = req.params.format
274
275 if (format === 'atom' || format === 'atom1') {
244e76a5
RK
276 return res.send(feed.atom1()).end()
277 }
278
279 if (format === 'json' || format === 'json1') {
244e76a5
RK
280 return res.send(feed.json1()).end()
281 }
282
283 if (format === 'rss' || format === 'rss2') {
244e76a5
RK
284 return res.send(feed.rss2()).end()
285 }
286
287 // We're in the ambiguous '.xml' case and we look at the format query parameter
288 if (req.query.format === 'atom' || req.query.format === 'atom1') {
244e76a5
RK
289 return res.send(feed.atom1()).end()
290 }
291
244e76a5
RK
292 return res.send(feed.rss2()).end()
293}