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