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