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