1 import * as express from 'express'
2 import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants'
3 import { THUMBNAILS_SIZE } from '../initializers'
4 import { asyncMiddleware, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, videosSortValidator } from '../middlewares'
5 import { VideoModel } from '../models/video/video'
6 import * as Feed from 'pfeed'
7 import { AccountModel } from '../models/account/account'
8 import { cacheRoute } from '../middlewares/cache'
9 import { VideoChannelModel } from '../models/video/video-channel'
10 import { VideoCommentModel } from '../models/video/video-comment'
11 import { buildNSFWFilter } from '../helpers/express-utils'
13 const feedsRouter = express.Router()
15 feedsRouter.get('/feeds/video-comments.:format',
16 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
17 asyncMiddleware(videoCommentsFeedsValidator),
18 asyncMiddleware(generateVideoCommentsFeed)
21 feedsRouter.get('/feeds/videos.:format',
24 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
25 asyncMiddleware(videoFeedsValidator),
26 asyncMiddleware(generateVideoFeed)
29 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
37 async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
40 const video = res.locals.video as VideoModel
41 const videoId: number = video ? video.id : undefined
43 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
45 const name = video ? video.name : CONFIG.INSTANCE.NAME
46 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
47 const feed = initFeed(name, description)
49 // Adding video items to the feed, one at a time
50 comments.forEach(comment => {
51 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId()
54 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
57 content: comment.text,
60 name: comment.Account.getDisplayName(),
61 link: comment.Account.Actor.url
64 date: comment.createdAt
68 // Now the feed generation is done, let's send it!
69 return sendFeed(feed, req, res)
72 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
75 const account: AccountModel = res.locals.account
76 const videoChannel: VideoChannelModel = res.locals.videoChannel
77 const nsfw = buildNSFWFilter(res, req.query.nsfw)
80 let description: string
83 name = videoChannel.getDisplayName()
84 description = videoChannel.description
86 name = account.getDisplayName()
87 description = account.description
89 name = CONFIG.INSTANCE.NAME
90 description = CONFIG.INSTANCE.DESCRIPTION
93 const feed = initFeed(name, description)
95 const resultList = await VideoModel.listForApi({
99 includeLocalVideos: true,
101 filter: req.query.filter,
103 accountId: account ? account.id : null,
104 videoChannelId: videoChannel ? videoChannel.id : null
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 => ({
112 url: videoFile.torrentUrl,
113 size_in_bytes: videoFile.size
119 link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid,
120 description: video.getTruncatedDescription(),
121 content: video.description,
124 name: video.VideoChannel.Account.getDisplayName(),
125 link: video.VideoChannel.Account.Actor.url
128 date: video.publishedAt,
129 language: video.language,
134 url: CONFIG.WEBSERVER.URL + video.getThumbnailStaticPath(),
135 height: THUMBNAILS_SIZE.height,
136 width: THUMBNAILS_SIZE.width
142 // Now the feed generation is done, let's send it!
143 return sendFeed(feed, req, res)
146 function initFeed (name: string, description: string) {
147 const webserverUrl = CONFIG.WEBSERVER.URL
152 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
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`, // ^.~
161 json: `${webserverUrl}/feeds/videos.json`,
162 atom: `${webserverUrl}/feeds/videos.atom`,
163 rss: `${webserverUrl}/feeds/videos.xml`
166 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
167 email: CONFIG.ADMIN.EMAIL,
168 link: `${webserverUrl}/about`
173 function sendFeed (feed, req: express.Request, res: express.Response) {
174 const format = req.params.format
176 if (format === 'atom' || format === 'atom1') {
177 res.set('Content-Type', 'application/atom+xml')
178 return res.send(feed.atom1()).end()
181 if (format === 'json' || format === 'json1') {
182 res.set('Content-Type', 'application/json')
183 return res.send(feed.json1()).end()
186 if (format === 'rss' || format === 'rss2') {
187 res.set('Content-Type', 'application/rss+xml')
188 return res.send(feed.rss2()).end()
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()
197 res.set('Content-Type', 'application/rss+xml')
198 return res.send(feed.rss2()).end()