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