aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/video-blacklist.ts
blob: 30c6d4bd96bf0db6e2e44abc21881c08473cbd7d (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
import { param } from 'express-validator/check'
import * as express from 'express'

import { database as db } from '../../initializers/database'
import { checkErrors } from './utils'
import { logger, isVideoIdOrUUIDValid, checkVideoExists } from '../../helpers'

const videosBlacklistRemoveValidator = [
  param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking blacklistRemove parameters.', { parameters: req.params })

    checkErrors(req, res, () => {
      checkVideoExists(req.params.videoId, res, () => {
        checkVideoIsBlacklisted(req, res, next)
      })
    })
  }
]

const videosBlacklistAddValidator = [
  param('videoId').custom(isVideoIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking videosBlacklist parameters', { parameters: req.params })

    checkErrors(req, res, () => {
      checkVideoExists(req.params.videoId, res, () => {
        checkVideoIsBlacklistable(req, res, next)
      })
    })
  }
]

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

export {
  videosBlacklistAddValidator,
  videosBlacklistRemoveValidator
}
// ---------------------------------------------------------------------------

function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) {
  if (res.locals.video.isOwned() === true) {
    return res.status(403)
              .json({ error: 'Cannot blacklist a local video' })
              .end()
  }

  callback()
}

function checkVideoIsBlacklisted (req: express.Request, res: express.Response, callback: () => void) {
  db.BlacklistedVideo.loadByVideoId(res.locals.video.id)
    .then(blacklistedVideo => {
      if (!blacklistedVideo) return res.status(404).send('Blacklisted video not found')

      res.locals.blacklistedVideo = blacklistedVideo

      callback()
    })
    .catch(err => {
      logger.error('Error in blacklistRemove request validator', { error: err })
      return res.sendStatus(500)
    })
}