Refractor retry transaction function
[oweals/peertube.git] / server / controllers / feeds.ts
index 4a4dc3820a81b2ec480c03de67ff0f9fc751dc43..c928dfacb4282abaeb7efc9f066f9e952153c897 100644 (file)
@@ -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,33 +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<VideoModel>
   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,
-      true
-    )
-  } 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 => {
@@ -91,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,