X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Ffeeds.ts;h=72628dffb94bc3d26a919a2971e44e3bc3d46965;hb=000eb0e40d74e914f6691305511c44e89cd8bf07;hp=960085af1af685d017c79e71fd324959105453a6;hpb=73471b1a52f242e86364ffb077ea6cadb3b07ae2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index 960085af1..72628dffb 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -1,26 +1,32 @@ import * as express from 'express' -import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants' -import { THUMBNAILS_SIZE } from '../initializers' +import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants' import { asyncMiddleware, commonVideosFiltersValidator, setDefaultSort, videoCommentsFeedsValidator, videoFeedsValidator, - videosSortValidator + videosSortValidator, + feedsFormatValidator, + setFeedFormatContentType } from '../middlewares' import { VideoModel } from '../models/video/video' import * as Feed from 'pfeed' -import { AccountModel } from '../models/account/account' import { cacheRoute } from '../middlewares/cache' -import { VideoChannelModel } from '../models/video/video-channel' import { VideoCommentModel } from '../models/video/video-comment' import { buildNSFWFilter } from '../helpers/express-utils' +import { CONFIG } from '../initializers/config' const feedsRouter = express.Router() feedsRouter.get('/feeds/video-comments.:format', - asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), + feedsFormatValidator, + setFeedFormatContentType, + asyncMiddleware(cacheRoute({ + headerBlacklist: [ + 'Content-Type' + ] + })(ROUTE_CACHE_LIFETIME.FEEDS)), asyncMiddleware(videoCommentsFeedsValidator), asyncMiddleware(generateVideoCommentsFeed) ) @@ -28,7 +34,13 @@ feedsRouter.get('/feeds/video-comments.:format', feedsRouter.get('/feeds/videos.:format', videosSortValidator, setDefaultSort, - asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), + feedsFormatValidator, + setFeedFormatContentType, + asyncMiddleware(cacheRoute({ + headerBlacklist: [ + 'Content-Type' + ] + })(ROUTE_CACHE_LIFETIME.FEEDS)), commonVideosFiltersValidator, asyncMiddleware(videoFeedsValidator), asyncMiddleware(generateVideoFeed) @@ -42,10 +54,10 @@ export { // --------------------------------------------------------------------------- -async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) { +async function generateVideoCommentsFeed (req: express.Request, res: express.Response) { const start = 0 - const video = res.locals.video as VideoModel + const video = res.locals.videoAll const videoId: number = video ? video.id : undefined const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId) @@ -56,19 +68,25 @@ async function generateVideoCommentsFeed (req: express.Request, res: express.Res // Adding video items to the feed, one at a time comments.forEach(comment => { - const link = CONFIG.WEBSERVER.URL + comment.getCommentStaticPath() + const link = WEBSERVER.URL + comment.getCommentStaticPath() + + let title = comment.Video.name + const author: { name: string, link: string }[] = [] + + if (comment.Account) { + title += ` - ${comment.Account.getDisplayName()}` + author.push({ + name: comment.Account.getDisplayName(), + link: comment.Account.Actor.url + }) + } feed.addItem({ - title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`, + title, id: comment.url, link, content: comment.text, - author: [ - { - name: comment.Account.getDisplayName(), - link: comment.Account.Actor.url - } - ], + author, date: comment.createdAt }) }) @@ -77,11 +95,11 @@ async function generateVideoCommentsFeed (req: express.Request, res: express.Res return sendFeed(feed, req, res) } -async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) { +async function generateVideoFeed (req: express.Request, res: express.Response) { const start = 0 - const account: AccountModel = res.locals.account - const videoChannel: VideoChannelModel = res.locals.videoChannel + const account = res.locals.account + const videoChannel = res.locals.videoChannel const nsfw = buildNSFWFilter(res, req.query.nsfw) let name: string @@ -115,16 +133,41 @@ async function generateVideoFeed (req: express.Request, res: express.Response, n // Adding video items to the feed, one at a time resultList.data.forEach(video => { const formattedVideoFiles = video.getFormattedVideoFilesJSON() + const torrents = formattedVideoFiles.map(videoFile => ({ title: video.name, url: videoFile.torrentUrl, size_in_bytes: videoFile.size })) + const videos = formattedVideoFiles.map(videoFile => { + const result = { + type: 'video/mp4', + medium: 'video', + height: videoFile.resolution.label.replace('p', ''), + fileSize: videoFile.size, + url: videoFile.fileUrl, + framerate: videoFile.fps, + duration: video.duration + } + + if (video.language) Object.assign(result, { lang: video.language }) + + return result + }) + + const categories: { value: number, label: string }[] = [] + if (video.category) { + categories.push({ + value: video.category, + label: VideoModel.getCategoryLabel(video.category) + }) + } + feed.addItem({ title: video.name, id: video.url, - link: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid, + link: WEBSERVER.URL + '/videos/watch/' + video.uuid, description: video.getTruncatedDescription(), content: video.description, author: [ @@ -134,12 +177,25 @@ async function generateVideoFeed (req: express.Request, res: express.Response, n } ], date: video.publishedAt, - language: video.language, nsfw: video.nsfw, torrent: torrents, + videos, + embed: { + url: video.getEmbedStaticPath(), + allowFullscreen: true + }, + player: { + url: video.getWatchStaticPath() + }, + categories, + community: { + statistics: { + views: video.views + } + }, thumbnail: [ { - url: CONFIG.WEBSERVER.URL + video.getThumbnailStaticPath(), + url: WEBSERVER.URL + video.getMiniatureStaticPath(), height: THUMBNAILS_SIZE.height, width: THUMBNAILS_SIZE.width } @@ -152,7 +208,7 @@ async function generateVideoFeed (req: express.Request, res: express.Response, n } function initFeed (name: string, description: string) { - const webserverUrl = CONFIG.WEBSERVER.URL + const webserverUrl = WEBSERVER.URL return new Feed({ title: name, @@ -182,26 +238,21 @@ function sendFeed (feed, req: express.Request, res: express.Response) { const format = req.params.format if (format === 'atom' || format === 'atom1') { - res.set('Content-Type', 'application/atom+xml') return res.send(feed.atom1()).end() } if (format === 'json' || format === 'json1') { - res.set('Content-Type', 'application/json') return res.send(feed.json1()).end() } if (format === 'rss' || format === 'rss2') { - res.set('Content-Type', 'application/rss+xml') return res.send(feed.rss2()).end() } // We're in the ambiguous '.xml' case and we look at the format query parameter if (req.query.format === 'atom' || req.query.format === 'atom1') { - res.set('Content-Type', 'application/atom+xml') return res.send(feed.atom1()).end() } - res.set('Content-Type', 'application/rss+xml') return res.send(feed.rss2()).end() }