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