]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/feeds.ts
Add videos list filters
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
1 import * as express from 'express'
2 import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3 import { THUMBNAILS_SIZE } from '../initializers'
4 import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, videosSortValidator } from '../middlewares'
5 import { VideoModel } from '../models/video/video'
6 import * as Feed from 'pfeed'
7 import { AccountModel } from '../models/account/account'
8 import { cacheRoute } from '../middlewares/cache'
9 import { VideoChannelModel } from '../models/video/video-channel'
10 import { VideoCommentModel } from '../models/video/video-comment'
11 import { buildNSFWFilter } from '../helpers/express-utils'
12
13 const feedsRouter = express.Router()
14
15 feedsRouter.get('/feeds/video-comments.:format',
16 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
17 asyncMiddleware(videoCommentsFeedsValidator),
18 asyncMiddleware(generateVideoCommentsFeed)
19 )
20
21 feedsRouter.get('/feeds/videos.:format',
22 videosSortValidator,
23 setDefaultSort,
24 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
25 asyncMiddleware(videoFeedsValidator),
26 asyncMiddleware(generateVideoFeed)
27 )
28
29 // ---------------------------------------------------------------------------
30
31 export {
32 feedsRouter
33 }
34
35 // ---------------------------------------------------------------------------
36
37 async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
38 const start = 0
39
40 const video = res.locals.video as VideoModel
41 const videoId: number = video ? video.id : undefined
42
43 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
44
45 const name = video ? video.name : CONFIG.INSTANCE.NAME
46 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
47 const feed = initFeed(name, description)
48
49 // Adding video items to the feed, one at a time
50 comments.forEach(comment => {
51 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId()
52
53 feed.addItem({
54 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
55 id: comment.url,
56 link,
57 content: comment.text,
58 author: [
59 {
60 name: comment.Account.getDisplayName(),
61 link: comment.Account.Actor.url
62 }
63 ],
64 date: comment.createdAt
65 })
66 })
67
68 // Now the feed generation is done, let's send it!
69 return sendFeed(feed, req, res)
70 }
71
72 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
73 const start = 0
74
75 const account: AccountModel = res.locals.account
76 const videoChannel: VideoChannelModel = res.locals.videoChannel
77 const nsfw = buildNSFWFilter(res, req.query.nsfw)
78
79 let name: string
80 let description: string
81
82 if (videoChannel) {
83 name = videoChannel.getDisplayName()
84 description = videoChannel.description
85 } else if (account) {
86 name = account.getDisplayName()
87 description = account.description
88 } else {
89 name = CONFIG.INSTANCE.NAME
90 description = CONFIG.INSTANCE.DESCRIPTION
91 }
92
93 const feed = initFeed(name, description)
94
95 const resultList = await VideoModel.listForApi({
96 start,
97 count: FEEDS.COUNT,
98 sort: req.query.sort,
99 nsfw,
100 filter: req.query.filter,
101 withFiles: true,
102 accountId: account ? account.id : null,
103 videoChannelId: videoChannel ? videoChannel.id : null
104 })
105
106 // Adding video items to the feed, one at a time
107 resultList.data.forEach(video => {
108 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
109 const torrents = formattedVideoFiles.map(videoFile => ({
110 title: video.name,
111 url: videoFile.torrentUrl,
112 size_in_bytes: videoFile.size
113 }))
114
115 feed.addItem({
116 title: video.name,
117 id: video.url,
118 link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid,
119 description: video.getTruncatedDescription(),
120 content: video.description,
121 author: [
122 {
123 name: video.VideoChannel.Account.getDisplayName(),
124 link: video.VideoChannel.Account.Actor.url
125 }
126 ],
127 date: video.publishedAt,
128 language: video.language,
129 nsfw: video.nsfw,
130 torrent: torrents,
131 thumbnail: [
132 {
133 url: CONFIG.WEBSERVER.URL + video.getThumbnailStaticPath(),
134 height: THUMBNAILS_SIZE.height,
135 width: THUMBNAILS_SIZE.width
136 }
137 ]
138 })
139 })
140
141 // Now the feed generation is done, let's send it!
142 return sendFeed(feed, req, res)
143 }
144
145 function initFeed (name: string, description: string) {
146 const webserverUrl = CONFIG.WEBSERVER.URL
147
148 return new Feed({
149 title: name,
150 description,
151 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
152 id: webserverUrl,
153 link: webserverUrl,
154 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
155 favicon: webserverUrl + '/client/assets/images/favicon.png',
156 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
157 ` and potential licenses granted by each content's rightholder.`,
158 generator: `Toraifōsu`, // ^.~
159 feedLinks: {
160 json: `${webserverUrl}/feeds/videos.json`,
161 atom: `${webserverUrl}/feeds/videos.atom`,
162 rss: `${webserverUrl}/feeds/videos.xml`
163 },
164 author: {
165 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
166 email: CONFIG.ADMIN.EMAIL,
167 link: `${webserverUrl}/about`
168 }
169 })
170 }
171
172 function sendFeed (feed, req: express.Request, res: express.Response) {
173 const format = req.params.format
174
175 if (format === 'atom' || format === 'atom1') {
176 res.set('Content-Type', 'application/atom+xml')
177 return res.send(feed.atom1()).end()
178 }
179
180 if (format === 'json' || format === 'json1') {
181 res.set('Content-Type', 'application/json')
182 return res.send(feed.json1()).end()
183 }
184
185 if (format === 'rss' || format === 'rss2') {
186 res.set('Content-Type', 'application/rss+xml')
187 return res.send(feed.rss2()).end()
188 }
189
190 // We're in the ambiguous '.xml' case and we look at the format query parameter
191 if (req.query.format === 'atom' || req.query.format === 'atom1') {
192 res.set('Content-Type', 'application/atom+xml')
193 return res.send(feed.atom1()).end()
194 }
195
196 res.set('Content-Type', 'application/rss+xml')
197 return res.send(feed.rss2()).end()
198 }