]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/feeds.ts
Remove video not found
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
1 import * as express from 'express'
2 import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3 import { asyncMiddleware, feedsValidator, setDefaultSort, videosSortValidator } from '../middlewares'
4 import { VideoModel } from '../models/video/video'
5 import * as Feed from 'pfeed'
6 import { AccountModel } from '../models/account/account'
7 import { cacheRoute } from '../middlewares/cache'
8 import { VideoChannelModel } from '../models/video/video-channel'
9
10 const feedsRouter = express.Router()
11
12 feedsRouter.get('/feeds/videos.:format',
13 videosSortValidator,
14 setDefaultSort,
15 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
16 asyncMiddleware(feedsValidator),
17 asyncMiddleware(generateFeed)
18 )
19
20 // ---------------------------------------------------------------------------
21
22 export {
23 feedsRouter
24 }
25
26 // ---------------------------------------------------------------------------
27
28 async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
29 let feed = initFeed()
30 const start = 0
31
32 const account: AccountModel = res.locals.account
33 const videoChannel: VideoChannelModel = res.locals.videoChannel
34 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
35
36 const resultList = await VideoModel.listForApi({
37 start,
38 count: FEEDS.COUNT,
39 sort: req.query.sort,
40 hideNSFW,
41 filter: req.query.filter,
42 withFiles: true,
43 accountId: account ? account.id : null,
44 videoChannelId: videoChannel ? videoChannel.id : null
45 })
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
79 function initFeed () {
80 const webserverUrl = CONFIG.WEBSERVER.URL
81
82 return new Feed({
83 title: CONFIG.INSTANCE.NAME,
84 description: CONFIG.INSTANCE.DESCRIPTION,
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: {
99 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
100 email: CONFIG.ADMIN.EMAIL,
101 link: `${webserverUrl}/about`
102 }
103 })
104 }
105
106 function 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 }