X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Fcontrollers%2Ffeeds.ts;h=c928dfacb4282abaeb7efc9f066f9e952153c897;hb=90d4bb8125e80c8060416d4d135ddeaf0a622ede;hp=27ebecc404138a92e43fbbc8ebc9617fff5df43d;hpb=0883b3245bf0deb9106c4041e9afbd3521b79280;p=oweals%2Fpeertube.git diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index 27ebecc40..c928dfacb 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -1,21 +1,27 @@ import * as express from 'express' -import { CONFIG, FEEDS } from '../initializers/constants' -import { asyncMiddleware, feedsValidator, setDefaultSort, videosSortValidator } from '../middlewares' +import { CONFIG, FEEDS, ROUTE_CACHE_LIFETIME } from '../initializers/constants' +import { asyncMiddleware, videoFeedsValidator, setDefaultSort, videosSortValidator, videoCommentsFeedsValidator } from '../middlewares' import { VideoModel } from '../models/video/video' import * as Feed from 'pfeed' -import { ResultList } from '../../shared/models' import { AccountModel } from '../models/account/account' import { cacheRoute } from '../middlewares/cache' -import { VideoSortField } from '../../client/src/app/shared/video/sort-field.type' +import { VideoChannelModel } from '../models/video/video-channel' +import { VideoCommentModel } from '../models/video/video-comment' const feedsRouter = express.Router() +feedsRouter.get('/feeds/video-comments.:format', + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), + asyncMiddleware(videoCommentsFeedsValidator), + asyncMiddleware(generateVideoCommentsFeed) +) + feedsRouter.get('/feeds/videos.:format', videosSortValidator, setDefaultSort, - asyncMiddleware(feedsValidator), - asyncMiddleware(cacheRoute), - asyncMiddleware(generateFeed) + asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS)), + asyncMiddleware(videoFeedsValidator), + asyncMiddleware(generateVideoFeed) ) // --------------------------------------------------------------------------- @@ -26,32 +32,53 @@ export { // --------------------------------------------------------------------------- -async function generateFeed (req: express.Request, res: express.Response, next: express.NextFunction) { +async function generateVideoCommentsFeed (req: express.Request, res: express.Response, next: express.NextFunction) { + let feed = initFeed() + const start = 0 + + const videoId: number = res.locals.video ? res.locals.video.id : undefined + + const comments = await VideoCommentModel.listForFeed(start, FEEDS.COUNT, videoId) + + // Adding video items to the feed, one at a time + comments.forEach(comment => { + feed.addItem({ + title: `${comment.Video.name} - ${comment.Account.getDisplayName()}`, + id: comment.url, + link: comment.url, + content: comment.text, + author: [ + { + name: comment.Account.getDisplayName(), + link: comment.Account.Actor.url + } + ], + date: comment.createdAt + }) + }) + + // Now the feed generation is done, let's send it! + return sendFeed(feed, req, res) +} + +async function generateVideoFeed (req: express.Request, res: express.Response, next: express.NextFunction) { let feed = initFeed() const start = 0 - let resultList: ResultList const account: AccountModel = res.locals.account + const videoChannel: VideoChannelModel = res.locals.videoChannel const hideNSFW = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY === 'do_not_list' - if (account) { - resultList = await VideoModel.listAccountVideosForApi( - account.id, - start, - FEEDS.COUNT, - req.query.sort as VideoSortField, - hideNSFW - ) - } else { - resultList = await VideoModel.listForApi( - start, - FEEDS.COUNT, - req.query.sort as VideoSortField, - hideNSFW, - req.query.filter, - true - ) - } + const resultList = await VideoModel.listForApi({ + start, + count: FEEDS.COUNT, + sort: req.query.sort, + hideNSFW, + filter: req.query.filter, + withFiles: true, + accountId: account ? account.id : null, + videoChannelId: videoChannel ? videoChannel.id : null + }) // Adding video items to the feed, one at a time resultList.data.forEach(video => { @@ -90,7 +117,7 @@ function initFeed () { return new Feed({ title: CONFIG.INSTANCE.NAME, - description: CONFIG.INSTANCE.SHORT_DESCRIPTION, + description: CONFIG.INSTANCE.DESCRIPTION, // updated: TODO: somehowGetLatestUpdate, // optional, default = today id: webserverUrl, link: webserverUrl,