]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/blacklist.ts
Fix video comments display with deleted comments
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
index 7f803c8e939a3668050b8438f7082b284e717634..fa8448c864a1976c69cad3168303e4a5ad079fa6 100644 (file)
@@ -1,7 +1,9 @@
 import * as express from 'express'
-import { VideoBlacklist, UserRight, VideoBlacklistCreate } from '../../../../shared'
+import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist'
+import { UserRight, VideoBlacklistCreate } from '../../../../shared'
 import { logger } from '../../../helpers/logger'
 import { getFormattedObjects } from '../../../helpers/utils'
+import { sequelizeTypescript } from '../../../initializers/database'
 import {
   asyncMiddleware,
   authenticate,
@@ -11,11 +13,12 @@ import {
   setBlacklistSort,
   setDefaultPagination,
   videosBlacklistAddValidator,
+  videosBlacklistFiltersValidator,
   videosBlacklistRemoveValidator,
   videosBlacklistUpdateValidator
 } from '../../../middlewares'
 import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
-import { sequelizeTypescript } from '../../../initializers'
+import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
 
 const blacklistRouter = express.Router()
 
@@ -23,7 +26,7 @@ blacklistRouter.post('/:videoId/blacklist',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
   asyncMiddleware(videosBlacklistAddValidator),
-  asyncMiddleware(addVideoToBlacklist)
+  asyncMiddleware(addVideoToBlacklistController)
 )
 
 blacklistRouter.get('/blacklist',
@@ -33,6 +36,7 @@ blacklistRouter.get('/blacklist',
   blacklistSortValidator,
   setBlacklistSort,
   setDefaultPagination,
+  videosBlacklistFiltersValidator,
   asyncMiddleware(listBlacklist)
 )
 
@@ -58,22 +62,19 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function addVideoToBlacklist (req: express.Request, res: express.Response) {
-  const videoInstance = res.locals.video
+async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
+  const videoInstance = res.locals.videoAll
   const body: VideoBlacklistCreate = req.body
 
-  const toCreate = {
-    videoId: videoInstance.id,
-    reason: body.reason
-  }
+  await blacklistVideo(videoInstance, body)
 
-  await VideoBlacklistModel.create(toCreate)
-  return res.type('json').status(204).end()
+  logger.info('Video %s blacklisted.', videoInstance.uuid)
+
+  return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
 }
 
 async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
-  const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
-  logger.info(videoBlacklist)
+  const videoBlacklist = res.locals.videoBlacklist
 
   if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
 
@@ -81,23 +82,28 @@ async function updateVideoBlacklistController (req: express.Request, res: expres
     return videoBlacklist.save({ transaction: t })
   })
 
-  return res.type('json').status(204).end()
+  return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
 }
 
-async function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const resultList = await VideoBlacklistModel.listForApi(req.query.start, req.query.count, req.query.sort)
+async function listBlacklist (req: express.Request, res: express.Response) {
+  const resultList = await VideoBlacklistModel.listForApi({
+    start: req.query.start,
+    count: req.query.count,
+    sort: req.query.sort,
+    search: req.query.search,
+    type: req.query.type
+  })
 
-  return res.json(getFormattedObjects<VideoBlacklist, VideoBlacklistModel>(resultList.data, resultList.total))
+  return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
-async function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const videoBlacklist = res.locals.videoBlacklist as VideoBlacklistModel
+async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
+  const videoBlacklist = res.locals.videoBlacklist
+  const video = res.locals.videoAll
 
-  await sequelizeTypescript.transaction(t => {
-    return videoBlacklist.destroy({ transaction: t })
-  })
+  await unblacklistVideo(videoBlacklist, video)
 
-  logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
+  logger.info('Video %s removed from blacklist.', video.uuid)
 
-  return res.type('json').status(204).end()
+  return res.type('json').sendStatus(HttpStatusCode.NO_CONTENT_204)
 }