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