]>
Commit | Line | Data |
---|---|---|
244e76a5 | 1 | import * as express from 'express' |
fd4484f1 | 2 | import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants' |
749c7247 | 3 | import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, videosSortValidator } from '../middlewares' |
244e76a5 RK |
4 | import { VideoModel } from '../models/video/video' |
5 | import * as Feed from 'pfeed' | |
244e76a5 | 6 | import { AccountModel } from '../models/account/account' |
4195cd2b | 7 | import { cacheRoute } from '../middlewares/cache' |
e0ea4b1d | 8 | import { VideoChannelModel } from '../models/video/video-channel' |
fe3a55b0 | 9 | import { VideoCommentModel } from '../models/video/video-comment' |
244e76a5 RK |
10 | |
11 | const feedsRouter = express.Router() | |
12 | ||
fe3a55b0 C |
13 | feedsRouter.get('/feeds/video-comments.:format', |
14 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), | |
15 | asyncMiddleware(videoCommentsFeedsValidator), | |
16 | asyncMiddleware(generateVideoCommentsFeed) | |
17 | ) | |
18 | ||
244e76a5 | 19 | feedsRouter.get('/feeds/videos.:format', |
7b87d2d5 C |
20 | videosSortValidator, |
21 | setDefaultSort, | |
fd4484f1 | 22 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), |
fe3a55b0 C |
23 | asyncMiddleware(videoFeedsValidator), |
24 | asyncMiddleware(generateVideoFeed) | |
244e76a5 RK |
25 | ) |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | export { | |
30 | feedsRouter | |
31 | } | |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | ||
fe3a55b0 | 35 | async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) { |
fe3a55b0 C |
36 | const start = 0 |
37 | ||
749c7247 C |
38 | const video = res.locals.video as VideoModel |
39 | const videoId: number = video ? video.id : undefined | |
fe3a55b0 C |
40 | |
41 | const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId) | |
42 | ||
749c7247 C |
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 | ||
fe3a55b0 C |
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) { | |
4195cd2b | 69 | const start = 0 |
244e76a5 | 70 | |
244e76a5 | 71 | const account: AccountModel = res.locals.account |
e0ea4b1d | 72 | const videoChannel: VideoChannelModel = res.locals.videoChannel |
0883b324 | 73 | const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list' |
244e76a5 | 74 | |
749c7247 C |
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 | ||
48dce1c9 | 91 | const resultList = await VideoModel.listForApi({ |
0626e7af | 92 | start, |
48dce1c9 C |
93 | count: FEEDS.COUNT, |
94 | sort: req.query.sort, | |
0626e7af | 95 | hideNSFW, |
48dce1c9 C |
96 | filter: req.query.filter, |
97 | withFiles: true, | |
e0ea4b1d C |
98 | accountId: account ? account.id : null, |
99 | videoChannelId: videoChannel ? videoChannel.id : null | |
48dce1c9 | 100 | }) |
244e76a5 RK |
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 | ||
749c7247 | 134 | function initFeed (name: string, description: string) { |
244e76a5 RK |
135 | const webserverUrl = CONFIG.WEBSERVER.URL |
136 | ||
137 | return new Feed({ | |
749c7247 C |
138 | title: name, |
139 | description, | |
244e76a5 RK |
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: { | |
7b87d2d5 | 154 | name: 'Instance admin of ' + CONFIG.INSTANCE.NAME, |
244e76a5 RK |
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 | } |