]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/feeds.ts
Add subscriptions endpoints to REST API
[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'
b81eb8fd 3import { THUMBNAILS_SIZE } from '../initializers'
749c7247 4import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, videosSortValidator } from '../middlewares'
244e76a5
RK
5import { VideoModel } from '../models/video/video'
6import * as Feed from 'pfeed'
244e76a5 7import { AccountModel } from '../models/account/account'
98d3324d 8import { cacheRoute } from '../middlewares/cache'
e0ea4b1d 9import { VideoChannelModel } from '../models/video/video-channel'
fe3a55b0 10import { VideoCommentModel } from '../models/video/video-comment'
d525fc39 11import { buildNSFWFilter } from '../helpers/express-utils'
244e76a5
RK
12
13const feedsRouter = express.Router()
14
fe3a55b0 15feedsRouter.get('/feeds/video-comments.:format',
98d3324d 16 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
fe3a55b0
C
17 asyncMiddleware(videoCommentsFeedsValidator),
18 asyncMiddleware(generateVideoCommentsFeed)
19)
20
244e76a5 21feedsRouter.get('/feeds/videos.:format',
7b87d2d5
C
22 videosSortValidator,
23 setDefaultSort,
98d3324d 24 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
fe3a55b0
C
25 asyncMiddleware(videoFeedsValidator),
26 asyncMiddleware(generateVideoFeed)
244e76a5
RK
27)
28
29// ---------------------------------------------------------------------------
30
31export {
32 feedsRouter
33}
34
35// ---------------------------------------------------------------------------
36
fe3a55b0 37async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
fe3a55b0
C
38 const start = 0
39
749c7247
C
40 const video = res.locals.video as VideoModel
41 const videoId: number = video ? video.id : undefined
fe3a55b0
C
42
43 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
44
749c7247
C
45 const name = video ? video.name : CONFIG.INSTANCE.NAME
46 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
47 const feed = initFeed(name, description)
48
fe3a55b0
C
49 // Adding video items to the feed, one at a time
50 comments.forEach(comment => {
4dae00e6
C
51 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId()
52
fe3a55b0
C
53 feed.addItem({
54 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
55 id: comment.url,
4dae00e6 56 link,
fe3a55b0
C
57 content: comment.text,
58 author: [
59 {
60 name: comment.Account.getDisplayName(),
61 link: comment.Account.Actor.url
62 }
63 ],
64 date: comment.createdAt
65 })
66 })
67
68 // Now the feed generation is done, let's send it!
69 return sendFeed(feed, req, res)
70}
71
72async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
4195cd2b 73 const start = 0
244e76a5 74
244e76a5 75 const account: AccountModel = res.locals.account
e0ea4b1d 76 const videoChannel: VideoChannelModel = res.locals.videoChannel
d525fc39 77 const nsfw = buildNSFWFilter(res, req.query.nsfw)
244e76a5 78
749c7247
C
79 let name: string
80 let description: string
81
82 if (videoChannel) {
83 name = videoChannel.getDisplayName()
84 description = videoChannel.description
85 } else if (account) {
86 name = account.getDisplayName()
87 description = account.description
88 } else {
89 name = CONFIG.INSTANCE.NAME
90 description = CONFIG.INSTANCE.DESCRIPTION
91 }
92
93 const feed = initFeed(name, description)
94
48dce1c9 95 const resultList = await VideoModel.listForApi({
0626e7af 96 start,
48dce1c9
C
97 count: FEEDS.COUNT,
98 sort: req.query.sort,
06a05d5f 99 includeLocalVideos: true,
d525fc39 100 nsfw,
48dce1c9
C
101 filter: req.query.filter,
102 withFiles: true,
e0ea4b1d
C
103 accountId: account ? account.id : null,
104 videoChannelId: videoChannel ? videoChannel.id : null
48dce1c9 105 })
244e76a5
RK
106
107 // Adding video items to the feed, one at a time
108 resultList.data.forEach(video => {
109 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
110 const torrents = formattedVideoFiles.map(videoFile => ({
111 title: video.name,
112 url: videoFile.torrentUrl,
113 size_in_bytes: videoFile.size
114 }))
115
116 feed.addItem({
117 title: video.name,
118 id: video.url,
4dae00e6 119 link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid,
244e76a5
RK
120 description: video.getTruncatedDescription(),
121 content: video.description,
122 author: [
123 {
124 name: video.VideoChannel.Account.getDisplayName(),
125 link: video.VideoChannel.Account.Actor.url
126 }
127 ],
128 date: video.publishedAt,
129 language: video.language,
130 nsfw: video.nsfw,
b81eb8fd
RK
131 torrent: torrents,
132 thumbnail: [
133 {
40e87e9e 134 url: CONFIG.WEBSERVER.URL + video.getThumbnailStaticPath(),
b81eb8fd
RK
135 height: THUMBNAILS_SIZE.height,
136 width: THUMBNAILS_SIZE.width
137 }
138 ]
244e76a5
RK
139 })
140 })
141
142 // Now the feed generation is done, let's send it!
143 return sendFeed(feed, req, res)
144}
145
749c7247 146function initFeed (name: string, description: string) {
244e76a5
RK
147 const webserverUrl = CONFIG.WEBSERVER.URL
148
149 return new Feed({
749c7247
C
150 title: name,
151 description,
244e76a5
RK
152 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
153 id: webserverUrl,
154 link: webserverUrl,
155 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
156 favicon: webserverUrl + '/client/assets/images/favicon.png',
157 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
158 ` and potential licenses granted by each content's rightholder.`,
159 generator: `Toraifōsu`, // ^.~
160 feedLinks: {
161 json: `${webserverUrl}/feeds/videos.json`,
162 atom: `${webserverUrl}/feeds/videos.atom`,
163 rss: `${webserverUrl}/feeds/videos.xml`
164 },
165 author: {
7b87d2d5 166 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
244e76a5
RK
167 email: CONFIG.ADMIN.EMAIL,
168 link: `${webserverUrl}/about`
169 }
170 })
171}
172
173function sendFeed (feed, req: express.Request, res: express.Response) {
174 const format = req.params.format
175
176 if (format === 'atom' || format === 'atom1') {
177 res.set('Content-Type', 'application/atom+xml')
178 return res.send(feed.atom1()).end()
179 }
180
181 if (format === 'json' || format === 'json1') {
182 res.set('Content-Type', 'application/json')
183 return res.send(feed.json1()).end()
184 }
185
186 if (format === 'rss' || format === 'rss2') {
187 res.set('Content-Type', 'application/rss+xml')
188 return res.send(feed.rss2()).end()
189 }
190
191 // We're in the ambiguous '.xml' case and we look at the format query parameter
192 if (req.query.format === 'atom' || req.query.format === 'atom1') {
193 res.set('Content-Type', 'application/atom+xml')
194 return res.send(feed.atom1()).end()
195 }
196
197 res.set('Content-Type', 'application/rss+xml')
198 return res.send(feed.rss2()).end()
199}