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'
12 const feedsRouter = express.Router()
14 feedsRouter.get('/feeds/video-comments.:format',
15 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
16 asyncMiddleware(videoCommentsFeedsValidator),
17 asyncMiddleware(generateVideoCommentsFeed)
20 feedsRouter.get('/feeds/videos.:format',
23 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)),
24 asyncMiddleware(videoFeedsValidator),
25 asyncMiddleware(generateVideoFeed)
28 // ---------------------------------------------------------------------------
34 // ---------------------------------------------------------------------------
36 async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
39 const video = res.locals.video as VideoModel
40 const videoId: number = video ? video.id : undefined
42 const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId)
44 const name = video ? video.name : CONFIG.INSTANCE.NAME
45 const description = video ? video.description : CONFIG.INSTANCE.DESCRIPTION
46 const feed = initFeed(name, description)
48 // Adding video items to the feed, one at a time
49 comments.forEach(comment => {
50 const link = CONFIG.WEBSERVER.URL + '/videos/watch/' + comment.Video.uuid + ';threadId=' + comment.getThreadId()
53 title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`,
56 content: comment.text,
59 name: comment.Account.getDisplayName(),
60 link: comment.Account.Actor.url
63 date: comment.createdAt
67 // Now the feed generation is done, let's send it!
68 return sendFeed(feed, req, res)
71 async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) {
74 const account: AccountModel = res.locals.account
75 const videoChannel: VideoChannelModel = res.locals.videoChannel
76 const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list'
79 let description: string
82 name = videoChannel.getDisplayName()
83 description = videoChannel.description
85 name = account.getDisplayName()
86 description = account.description
88 name = CONFIG.INSTANCE.NAME
89 description = CONFIG.INSTANCE.DESCRIPTION
92 const feed = initFeed(name, description)
94 const resultList = await VideoModel.listForApi({
99 filter: req.query.filter,
101 accountId: account ? account.id : null,
102 videoChannelId: videoChannel ? videoChannel.id : null
105 // Adding video items to the feed, one at a time
106 resultList.data.forEach(video => {
107 const formattedVideoFiles = video.getFormattedVideoFilesJSON()
108 const torrents = formattedVideoFiles.map(videoFile => ({
110 url: videoFile.torrentUrl,
111 size_in_bytes: videoFile.size
117 link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid,
118 description: video.getTruncatedDescription(),
119 content: video.description,
122 name: video.VideoChannel.Account.getDisplayName(),
123 link: video.VideoChannel.Account.Actor.url
126 date: video.publishedAt,
127 language: video.language,
132 url: CONFIG.WEBSERVER.URL + video.getThumbnailStaticPath(),
133 height: THUMBNAILS_SIZE.height,
134 width: THUMBNAILS_SIZE.width
140 // Now the feed generation is done, let's send it!
141 return sendFeed(feed, req, res)
144 function initFeed (name: string, description: string) {
145 const webserverUrl = CONFIG.WEBSERVER.URL
150 // updated: TODO: somehowGetLatestUpdate, // optional, default = today
153 image: webserverUrl + '/client/assets/images/icons/icon-96x96.png',
154 favicon: webserverUrl + '/client/assets/images/favicon.png',
155 copyright: `All rights reserved, unless otherwise specified in the terms specified at ${webserverUrl}/about` +
156 ` and potential licenses granted by each content's rightholder.`,
157 generator: `ToraifÅsu`, // ^.~
159 json: `${webserverUrl}/feeds/videos.json`,
160 atom: `${webserverUrl}/feeds/videos.atom`,
161 rss: `${webserverUrl}/feeds/videos.xml`
164 name: 'Instance admin of ' + CONFIG.INSTANCE.NAME,
165 email: CONFIG.ADMIN.EMAIL,
166 link: `${webserverUrl}/about`
171 function sendFeed (feed, req: express.Request, res: express.Response) {
172 const format = req.params.format
174 if (format === 'atom' || format === 'atom1') {
175 res.set('Content-Type', 'application/atom+xml')
176 return res.send(feed.atom1()).end()
179 if (format === 'json' || format === 'json1') {
180 res.set('Content-Type', 'application/json')
181 return res.send(feed.json1()).end()
184 if (format === 'rss' || format === 'rss2') {
185 res.set('Content-Type', 'application/rss+xml')
186 return res.send(feed.rss2()).end()
189 // We're in the ambiguous '.xml' case and we look at the format query parameter
190 if (req.query.format === 'atom' || req.query.format === 'atom1') {
191 res.set('Content-Type', 'application/atom+xml')
192 return res.send(feed.atom1()).end()
195 res.set('Content-Type', 'application/rss+xml')
196 return res.send(feed.rss2()).end()