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