]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/feeds.ts
1773fc71e2a38eed02a72d5d2fc4314e8819eb36
[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
12 const feedsRouter = express.Router()
13
14 feedsRouter.get('/feeds/video-comments.:format',
15 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
16 asyncMiddleware(videoCommentsFeedsValidator),
17 asyncMiddleware(generateVideoCommentsFeed)
18 )
19
20 feedsRouter.get('/feeds/videos.:format',
21 videosSortValidator,
22 setDefaultSort,
23 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
24 asyncMiddleware(videoFeedsValidator),
25 asyncMiddleware(generateVideoFeed)
26 )
27
28 // ---------------------------------------------------------------------------
29
30 export {
31 feedsRouter
32 }
33
34 // ---------------------------------------------------------------------------
35
36 async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
37 const start = 0
38
39 const video = res.locals.video as VideoModel
40 const videoId: number = video ? video.id : undefined
41
42 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
43
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
48 // Adding video items to the feed, one at a time
49 comments.forEach(comment => {
50 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId()
51
52 feed.addItem({
53 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
54 id: comment.url,
55 link,
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
71 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
72 const start = 0
73
74 const account: AccountModel = res.locals.account
75 const videoChannel: VideoChannelModel = res.locals.videoChannel
76 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
77
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
94 const resultList = await VideoModel.listForApi({
95 start,
96 count: FEEDS.COUNT,
97 sort: req.query.sort,
98 hideNSFW,
99 filter: req.query.filter,
100 withFiles: true,
101 accountId: account ? account.id : null,
102 videoChannelId: videoChannel ? videoChannel.id : null
103 })
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,
117 link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid,
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,
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 ]
137 })
138 })
139
140 // Now the feed generation is done, let's send it!
141 return sendFeed(feed, req, res)
142 }
143
144 function initFeed (name: string, description: string) {
145 const webserverUrl = CONFIG.WEBSERVER.URL
146
147 return new Feed({
148 title: name,
149 description,
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: {
164 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
165 email: CONFIG.ADMIN.EMAIL,
166 link: `${webserverUrl}/about`
167 }
168 })
169 }
170
171 function 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 }