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