]>
Commit | Line | Data |
---|---|---|
244e76a5 RK |
1 | import * as express from 'express' |
2 | import { CONFIG } from '../initializers' | |
7b87d2d5 C |
3 | import { |
4 | asyncMiddleware, | |
5 | feedsValidator, | |
6 | setDefaultPagination, | |
7 | setDefaultSort, | |
8 | videosSortValidator | |
9 | } from '../middlewares' | |
244e76a5 RK |
10 | import { VideoModel } from '../models/video/video' |
11 | import * as Feed from 'pfeed' | |
12 | import { ResultList } from '../../shared/models' | |
13 | import { AccountModel } from '../models/account/account' | |
14 | ||
15 | const feedsRouter = express.Router() | |
16 | ||
17 | feedsRouter.get('/feeds/videos.:format', | |
7b87d2d5 C |
18 | videosSortValidator, |
19 | setDefaultSort, | |
244e76a5 RK |
20 | asyncMiddleware(feedsValidator), |
21 | asyncMiddleware(generateFeed) | |
22 | ) | |
23 | ||
24 | // --------------------------------------------------------------------------- | |
25 | ||
26 | export { | |
27 | feedsRouter | |
28 | } | |
29 | ||
30 | // --------------------------------------------------------------------------- | |
31 | ||
32 | async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) { | |
33 | let feed = initFeed() | |
7b87d2d5 C |
34 | const paginationStart = 0 |
35 | const paginationCount = 20 | |
244e76a5 RK |
36 | |
37 | let resultList: ResultList<VideoModel> | |
38 | const account: AccountModel = res.locals.account | |
39 | ||
40 | if (account) { | |
7b87d2d5 | 41 | resultList = await VideoModel.listAccountVideosForApi( |
244e76a5 | 42 | account.id, |
7b87d2d5 C |
43 | paginationStart, |
44 | paginationCount, | |
45 | req.query.sort, | |
244e76a5 RK |
46 | true |
47 | ) | |
48 | } else { | |
49 | resultList = await VideoModel.listForApi( | |
7b87d2d5 C |
50 | paginationStart, |
51 | paginationCount, | |
52 | req.query.sort, | |
244e76a5 RK |
53 | req.query.filter, |
54 | true | |
55 | ) | |
56 | } | |
57 | ||
58 | // Adding video items to the feed, one at a time | |
59 | resultList.data.forEach(video => { | |
60 | const formattedVideoFiles = video.getFormattedVideoFilesJSON() | |
61 | const torrents = formattedVideoFiles.map(videoFile => ({ | |
62 | title: video.name, | |
63 | url: videoFile.torrentUrl, | |
64 | size_in_bytes: videoFile.size | |
65 | })) | |
66 | ||
67 | feed.addItem({ | |
68 | title: video.name, | |
69 | id: video.url, | |
70 | link: video.url, | |
71 | description: video.getTruncatedDescription(), | |
72 | content: video.description, | |
73 | author: [ | |
74 | { | |
75 | name: video.VideoChannel.Account.getDisplayName(), | |
76 | link: video.VideoChannel.Account.Actor.url | |
77 | } | |
78 | ], | |
79 | date: video.publishedAt, | |
80 | language: video.language, | |
81 | nsfw: video.nsfw, | |
82 | torrent: torrents | |
83 | }) | |
84 | }) | |
85 | ||
86 | // Now the feed generation is done, let's send it! | |
87 | return sendFeed(feed, req, res) | |
88 | } | |
89 | ||
90 | function initFeed () { | |
91 | const webserverUrl = CONFIG.WEBSERVER.URL | |
92 | ||
93 | return new Feed({ | |
94 | title: CONFIG.INSTANCE.NAME, | |
95 | description: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
96 | // updated: TODO: somehowGetLatestUpdate, // optional, default = today | |
97 | id: webserverUrl, | |
98 | link: webserverUrl, | |
99 | image: webserverUrl + '/client/assets/images/icons/icon-96x96.png', | |
100 | favicon: webserverUrl + '/client/assets/images/favicon.png', | |
101 | copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` + | |
102 | ` and potential licenses granted by each content's rightholder.`, | |
103 | generator: `ToraifÅsu`, // ^.~ | |
104 | feedLinks: { | |
105 | json: `${webserverUrl}/feeds/videos.json`, | |
106 | atom: `${webserverUrl}/feeds/videos.atom`, | |
107 | rss: `${webserverUrl}/feeds/videos.xml` | |
108 | }, | |
109 | author: { | |
7b87d2d5 | 110 | name: 'Instance admin of ' + CONFIG.INSTANCE.NAME, |
244e76a5 RK |
111 | email: CONFIG.ADMIN.EMAIL, |
112 | link: `${webserverUrl}/about` | |
113 | } | |
114 | }) | |
115 | } | |
116 | ||
117 | function sendFeed (feed, req: express.Request, res: express.Response) { | |
118 | const format = req.params.format | |
119 | ||
120 | if (format === 'atom' || format === 'atom1') { | |
121 | res.set('Content-Type', 'application/atom+xml') | |
122 | return res.send(feed.atom1()).end() | |
123 | } | |
124 | ||
125 | if (format === 'json' || format === 'json1') { | |
126 | res.set('Content-Type', 'application/json') | |
127 | return res.send(feed.json1()).end() | |
128 | } | |
129 | ||
130 | if (format === 'rss' || format === 'rss2') { | |
131 | res.set('Content-Type', 'application/rss+xml') | |
132 | return res.send(feed.rss2()).end() | |
133 | } | |
134 | ||
135 | // We're in the ambiguous '.xml' case and we look at the format query parameter | |
136 | if (req.query.format === 'atom' || req.query.format === 'atom1') { | |
137 | res.set('Content-Type', 'application/atom+xml') | |
138 | return res.send(feed.atom1()).end() | |
139 | } | |
140 | ||
141 | res.set('Content-Type', 'application/rss+xml') | |
142 | return res.send(feed.rss2()).end() | |
143 | } |