]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-channels.ts
Add video channels
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-channels.ts
1 import * as Promise from 'bluebird'
2 import * as validator from 'validator'
3 import * as express from 'express'
4 import 'express-validator'
5 import 'multer'
6
7 import { database as db, CONSTRAINTS_FIELDS } from '../../initializers'
8 import { VideoChannelInstance } from '../../models'
9 import { logger } from '../logger'
10 import { exists } from './misc'
11
12 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
13
14 function isVideoChannelDescriptionValid (value: string) {
15 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
16 }
17
18 function isVideoChannelNameValid (value: string) {
19 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
20 }
21
22 function isVideoChannelUUIDValid (value: string) {
23 return exists(value) && validator.isUUID('' + value, 4)
24 }
25
26 function checkVideoChannelExists (id: string, res: express.Response, callback: () => void) {
27 let promise: Promise<VideoChannelInstance>
28 if (validator.isInt(id)) {
29 promise = db.VideoChannel.loadAndPopulateAuthor(+id)
30 } else { // UUID
31 promise = db.VideoChannel.loadByUUIDAndPopulateAuthor(id)
32 }
33
34 promise.then(videoChannel => {
35 if (!videoChannel) {
36 return res.status(404)
37 .json({ error: 'Video channel not found' })
38 .end()
39 }
40
41 res.locals.videoChannel = videoChannel
42 callback()
43 })
44 .catch(err => {
45 logger.error('Error in video channel request validator.', err)
46 return res.sendStatus(500)
47 })
48 }
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 isVideoChannelDescriptionValid,
54 isVideoChannelNameValid,
55 isVideoChannelUUIDValid,
56 checkVideoChannelExists
57 }