]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/blacklist.ts
Prevent broken transcoding with audio only input
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
index e4be6f0f9cb8d11d7763dfbd4a7d1633e50ba946..4103bb0630793b7f5edf3c521f46c821b004cb22 100644 (file)
@@ -1,20 +1,60 @@
-import * as express from 'express'
-
-import { database as db } from '../../../initializers/database'
-import { logger } from '../../../helpers'
+import express from 'express'
+import { blacklistVideo, unblacklistVideo } from '@server/lib/video-blacklist'
+import { HttpStatusCode, UserRight, VideoBlacklistCreate } from '@shared/models'
+import { logger } from '../../../helpers/logger'
+import { getFormattedObjects } from '../../../helpers/utils'
+import { sequelizeTypescript } from '../../../initializers/database'
 import {
+  asyncMiddleware,
   authenticate,
-  ensureIsAdmin,
-  videosBlacklistValidator
+  blacklistSortValidator,
+  ensureUserHasRight,
+  openapiOperationDoc,
+  paginationValidator,
+  setBlacklistSort,
+  setDefaultPagination,
+  videosBlacklistAddValidator,
+  videosBlacklistFiltersValidator,
+  videosBlacklistRemoveValidator,
+  videosBlacklistUpdateValidator
 } from '../../../middlewares'
+import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
 
 const blacklistRouter = express.Router()
 
-blacklistRouter.post('/:id/blacklist',
+blacklistRouter.post('/:videoId/blacklist',
+  openapiOperationDoc({ operationId: 'addVideoBlock' }),
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
+  asyncMiddleware(videosBlacklistAddValidator),
+  asyncMiddleware(addVideoToBlacklistController)
+)
+
+blacklistRouter.get('/blacklist',
+  openapiOperationDoc({ operationId: 'getVideoBlocks' }),
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
+  paginationValidator,
+  blacklistSortValidator,
+  setBlacklistSort,
+  setDefaultPagination,
+  videosBlacklistFiltersValidator,
+  asyncMiddleware(listBlacklist)
+)
+
+blacklistRouter.put('/:videoId/blacklist',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
+  asyncMiddleware(videosBlacklistUpdateValidator),
+  asyncMiddleware(updateVideoBlacklistController)
+)
+
+blacklistRouter.delete('/:videoId/blacklist',
+  openapiOperationDoc({ operationId: 'delVideoBlock' }),
   authenticate,
-  ensureIsAdmin,
-  videosBlacklistValidator,
-  addVideoToBlacklist
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
+  asyncMiddleware(videosBlacklistRemoveValidator),
+  asyncMiddleware(removeVideoFromBlacklistController)
 )
 
 // ---------------------------------------------------------------------------
@@ -25,17 +65,48 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const videoInstance = res.locals.video
+async function addVideoToBlacklistController (req: express.Request, res: express.Response) {
+  const videoInstance = res.locals.videoAll
+  const body: VideoBlacklistCreate = req.body
+
+  await blacklistVideo(videoInstance, body)
+
+  logger.info('Video %s blacklisted.', videoInstance.uuid)
+
+  return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
+}
+
+async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
+  const videoBlacklist = res.locals.videoBlacklist
+
+  if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
+
+  await sequelizeTypescript.transaction(t => {
+    return videoBlacklist.save({ transaction: t })
+  })
+
+  return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
+}
+
+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(resultList.data, resultList.total))
+}
+
+async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
+  const videoBlacklist = res.locals.videoBlacklist
+  const video = res.locals.videoAll
+
+  await unblacklistVideo(videoBlacklist, video)
 
-  const toCreate = {
-    videoId: videoInstance.id
-  }
+  logger.info('Video %s removed from blacklist.', video.uuid)
 
-  db.BlacklistedVideo.create(toCreate)
-    .then(() => res.type('json').status(204).end())
-    .catch(err => {
-      logger.error('Errors when blacklisting video ', { error: err })
-      return next(err)
-    })
+  return res.type('json').status(HttpStatusCode.NO_CONTENT_204).end()
 }