]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/feeds.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / controllers / feeds.ts
index 2182b42f56976df03a019749e09869f817bd2e3c..29502a15458d8fd91aba60b893a5dd5e32f0bab4 100644 (file)
@@ -1,8 +1,11 @@
-import * as express from 'express'
-import * as Feed from 'pfeed'
+import express from 'express'
+import Feed from 'pfeed'
+import { getServerActor } from '@server/models/application/application'
+import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils'
+import { VideoInclude } from '@shared/models'
 import { buildNSFWFilter } from '../helpers/express-utils'
 import { CONFIG } from '../initializers/config'
-import { FEEDS, ROUTE_CACHE_LIFETIME, THUMBNAILS_SIZE, WEBSERVER } from '../initializers/constants'
+import { FEEDS, PREVIEWS_SIZE, ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants'
 import {
   asyncMiddleware,
   commonVideosFiltersValidator,
@@ -14,21 +17,20 @@ import {
   videosSortValidator,
   videoSubscriptionFeedsValidator
 } from '../middlewares'
-import { cacheRoute } from '../middlewares/cache'
+import { cacheRouteFactory } from '../middlewares/cache/cache'
 import { VideoModel } from '../models/video/video'
 import { VideoCommentModel } from '../models/video/video-comment'
-import { VideoFilter } from '../../shared/models/videos/video-query.type'
 
 const feedsRouter = express.Router()
 
+const cacheRoute = cacheRouteFactory({
+  headerBlacklist: [ 'Content-Type' ]
+})
+
 feedsRouter.get('/feeds/video-comments.:format',
   feedsFormatValidator,
   setFeedFormatContentType,
-  asyncMiddleware(cacheRoute({
-    headerBlacklist: [
-      'Content-Type'
-    ]
-  })(ROUTE_CACHE_LIFETIME.FEEDS)),
+  cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
   asyncMiddleware(videoFeedsValidator),
   asyncMiddleware(videoCommentsFeedsValidator),
   asyncMiddleware(generateVideoCommentsFeed)
@@ -39,11 +41,7 @@ feedsRouter.get('/feeds/videos.:format',
   setDefaultVideosSort,
   feedsFormatValidator,
   setFeedFormatContentType,
-  asyncMiddleware(cacheRoute({
-    headerBlacklist: [
-      'Content-Type'
-    ]
-  })(ROUTE_CACHE_LIFETIME.FEEDS)),
+  cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
   commonVideosFiltersValidator,
   asyncMiddleware(videoFeedsValidator),
   asyncMiddleware(generateVideoFeed)
@@ -54,11 +52,7 @@ feedsRouter.get('/feeds/subscriptions.:format',
   setDefaultVideosSort,
   feedsFormatValidator,
   setFeedFormatContentType,
-  asyncMiddleware(cacheRoute({
-    headerBlacklist: [
-      'Content-Type'
-    ]
-  })(ROUTE_CACHE_LIFETIME.FEEDS)),
+  cacheRoute(ROUTE_CACHE_LIFETIME.FEEDS),
   commonVideosFiltersValidator,
   asyncMiddleware(videoSubscriptionFeedsValidator),
   asyncMiddleware(generateVideoFeedForSubscriptions)
@@ -167,18 +161,24 @@ async function generateVideoFeed (req: express.Request, res: express.Response) {
     videoChannelId: videoChannel ? videoChannel.id : null
   }
 
-  const resultList = await VideoModel.listForApi({
+  const server = await getServerActor()
+  const { data } = await VideoModel.listForApi({
     start,
     count: FEEDS.COUNT,
     sort: req.query.sort,
-    includeLocalVideos: true,
+    displayOnlyForFollower: {
+      actorId: server.id,
+      orLocalVideos: true
+    },
     nsfw,
-    filter: req.query.filter as VideoFilter,
-    withFiles: true,
+    isLocal: req.query.isLocal,
+    include: req.query.include | VideoInclude.FILES,
+    hasFiles: true,
+    countVideos: false,
     ...options
   })
 
-  addVideosToFeed(feed, resultList.data)
+  addVideosToFeed(feed, data)
 
   // Now the feed generation is done, let's send it!
   return sendFeed(feed, req, res)
@@ -198,20 +198,27 @@ async function generateVideoFeedForSubscriptions (req: express.Request, res: exp
     queryString: new URL(WEBSERVER.URL + req.url).search
   })
 
-  const resultList = await VideoModel.listForApi({
+  const { data } = await VideoModel.listForApi({
     start,
     count: FEEDS.COUNT,
     sort: req.query.sort,
-    includeLocalVideos: false,
     nsfw,
-    filter: req.query.filter as VideoFilter,
-    withFiles: true,
 
-    followerActorId: res.locals.user.Account.Actor.id,
+    isLocal: req.query.isLocal,
+
+    hasFiles: true,
+    include: req.query.include | VideoInclude.FILES,
+
+    countVideos: false,
+
+    displayOnlyForFollower: {
+      actorId: res.locals.user.Account.Actor.id,
+      orLocalVideos: false
+    },
     user: res.locals.user
   })
 
-  addVideosToFeed(feed, resultList.data)
+  addVideosToFeed(feed, data)
 
   // Now the feed generation is done, let's send it!
   return sendFeed(feed, req, res)
@@ -255,7 +262,7 @@ function addVideosToFeed (feed, videos: VideoModel[]) {
    * Adding video items to the feed object, one at a time
    */
   for (const video of videos) {
-    const formattedVideoFiles = video.getFormattedVideoFilesJSON()
+    const formattedVideoFiles = video.getFormattedVideoFilesJSON(false)
 
     const torrents = formattedVideoFiles.map(videoFile => ({
       title: video.name,
@@ -283,14 +290,14 @@ function addVideosToFeed (feed, videos: VideoModel[]) {
     if (video.category) {
       categories.push({
         value: video.category,
-        label: VideoModel.getCategoryLabel(video.category)
+        label: getCategoryLabel(video.category)
       })
     }
 
     feed.addItem({
       title: video.name,
       id: video.url,
-      link: WEBSERVER.URL + '/videos/watch/' + video.uuid,
+      link: WEBSERVER.URL + video.getWatchStaticPath(),
       description: video.getTruncatedDescription(),
       content: video.description,
       author: [
@@ -318,9 +325,9 @@ function addVideosToFeed (feed, videos: VideoModel[]) {
       },
       thumbnail: [
         {
-          url: WEBSERVER.URL + video.getMiniatureStaticPath(),
-          height: THUMBNAILS_SIZE.height,
-          width: THUMBNAILS_SIZE.width
+          url: WEBSERVER.URL + video.getPreviewStaticPath(),
+          height: PREVIEWS_SIZE.height,
+          width: PREVIEWS_SIZE.width
         }
       ]
     })