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