]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/videos/blacklist.ts
Add moderation helpers to plugins
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / blacklist.ts
index d08c6e13f0e6c4a614e0aae5fa306715f1141f76..3b25ceea28bbbfe04e3104999024180a0209eb76 100644 (file)
@@ -1,17 +1,22 @@
 import * as express from 'express'
-import { logger, getFormattedObjects } from '../../../helpers'
+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,
+  blacklistSortValidator,
   ensureUserHasRight,
-  videosBlacklistAddValidator,
-  videosBlacklistRemoveValidator,
   paginationValidator,
-  blacklistSortValidator,
   setBlacklistSort,
-  setPagination,
-  asyncMiddleware
+  setDefaultPagination,
+  videosBlacklistAddValidator,
+  videosBlacklistFiltersValidator,
+  videosBlacklistRemoveValidator,
+  videosBlacklistUpdateValidator
 } from '../../../middlewares'
-import { BlacklistedVideo, UserRight } from '../../../../shared'
 import { VideoBlacklistModel } from '../../../models/video/video-blacklist'
 
 const blacklistRouter = express.Router()
@@ -20,7 +25,7 @@ blacklistRouter.post('/:videoId/blacklist',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
   asyncMiddleware(videosBlacklistAddValidator),
-  asyncMiddleware(addVideoToBlacklist)
+  asyncMiddleware(addVideoToBlacklistController)
 )
 
 blacklistRouter.get('/blacklist',
@@ -29,10 +34,18 @@ blacklistRouter.get('/blacklist',
   paginationValidator,
   blacklistSortValidator,
   setBlacklistSort,
-  setPagination,
+  setDefaultPagination,
+  videosBlacklistFiltersValidator,
   asyncMiddleware(listBlacklist)
 )
 
+blacklistRouter.put('/:videoId/blacklist',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
+  asyncMiddleware(videosBlacklistUpdateValidator),
+  asyncMiddleware(updateVideoBlacklistController)
+)
+
 blacklistRouter.delete('/:videoId/blacklist',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_VIDEO_BLACKLIST),
@@ -48,34 +61,48 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async 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').sendStatus(204)
+}
+
+async function updateVideoBlacklistController (req: express.Request, res: express.Response) {
+  const videoBlacklist = res.locals.videoBlacklist
+
+  if (req.body.reason !== undefined) videoBlacklist.reason = req.body.reason
 
-  const toCreate = {
-    videoId: videoInstance.id
-  }
+  await sequelizeTypescript.transaction(t => {
+    return videoBlacklist.save({ transaction: t })
+  })
 
-  await VideoBlacklistModel.create(toCreate)
-  return res.type('json').status(204).end()
+  return res.type('json').sendStatus(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<BlacklistedVideo, 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 blacklistedVideo = res.locals.blacklistedVideo as VideoBlacklistModel
+async function removeVideoFromBlacklistController (req: express.Request, res: express.Response) {
+  const videoBlacklist = res.locals.videoBlacklist
+  const video = res.locals.videoAll
 
-  try {
-    await blacklistedVideo.destroy()
+  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.sendStatus(204)
-  } catch (err) {
-    logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)
-    throw err
-  }
+  return res.type('json').sendStatus(204)
 }