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