aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos/blacklist.ts
blob: 66311598ecf4996437aebf065bef9e2c5238fd35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as express from 'express'

import { database as db } from '../../../initializers'
import { logger, getFormattedObjects } from '../../../helpers'
import {
  authenticate,
  ensureIsAdmin,
  videosBlacklistAddValidator,
  videosBlacklistRemoveValidator,
  paginationValidator,
  blacklistSortValidator,
  setBlacklistSort,
  setPagination
} from '../../../middlewares'
import { BlacklistedVideoInstance } from '../../../models'
import { BlacklistedVideo } from '../../../../shared'

const blacklistRouter = express.Router()

blacklistRouter.post('/:videoId/blacklist',
  authenticate,
  ensureIsAdmin,
  videosBlacklistAddValidator,
  addVideoToBlacklist
)

blacklistRouter.get('/blacklist',
  authenticate,
  ensureIsAdmin,
  paginationValidator,
  blacklistSortValidator,
  setBlacklistSort,
  setPagination,
  listBlacklist
)

blacklistRouter.delete('/:videoId/blacklist',
  authenticate,
  ensureIsAdmin,
  videosBlacklistRemoveValidator,
  removeVideoFromBlacklistController
)

// ---------------------------------------------------------------------------

export {
  blacklistRouter
}

// ---------------------------------------------------------------------------

function addVideoToBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
  const videoInstance = res.locals.video

  const toCreate = {
    videoId: videoInstance.id
  }

  db.BlacklistedVideo.create(toCreate)
    .then(() => res.type('json').status(204).end())
    .catch(err => {
      logger.error('Errors when blacklisting video ', err)
      return next(err)
    })
}

function listBlacklist (req: express.Request, res: express.Response, next: express.NextFunction) {
  db.BlacklistedVideo.listForApi(req.query.start, req.query.count, req.query.sort)
    .then(resultList => res.json(getFormattedObjects<BlacklistedVideo, BlacklistedVideoInstance>(resultList.data, resultList.total)))
    .catch(err => next(err))
}

function removeVideoFromBlacklistController (req: express.Request, res: express.Response, next: express.NextFunction) {
  const blacklistedVideo = res.locals.blacklistedVideo as BlacklistedVideoInstance

  blacklistedVideo.destroy()
    .then(() => {
      logger.info('Video %s removed from blacklist.', res.locals.video.uuid)
      res.sendStatus(204)
    })
    .catch(err => {
      logger.error('Some error while removing video %s from blacklist.', res.locals.video.uuid, err)
      next(err)
    })
}