]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-channels.ts
32faf36f757a45f5b0b4dcbf57613f8e096ff73e
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-channels.ts
1 import * as express from 'express'
2 import 'express-validator'
3 import 'multer'
4 import * as validator from 'validator'
5 import { CONSTRAINTS_FIELDS } from '../../initializers'
6 import { VideoChannelModel } from '../../models/video/video-channel'
7 import { exists } from './misc'
8 import { Response } from 'express'
9
10 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
11
12 function isVideoChannelDescriptionValid (value: string) {
13 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
14 }
15
16 function isVideoChannelNameValid (value: string) {
17 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
18 }
19
20 function isVideoChannelSupportValid (value: string) {
21 return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
22 }
23
24 async function isLocalVideoChannelNameExist (name: string, res: Response) {
25 const videoChannel = await VideoChannelModel.loadLocalByName(name)
26
27 return processVideoChannelExist(videoChannel, res)
28 }
29
30 async function isVideoChannelExist (id: string, res: express.Response) {
31 let videoChannel: VideoChannelModel
32 if (validator.isInt(id)) {
33 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
34 } else { // UUID
35 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id)
36 }
37
38 return processVideoChannelExist(videoChannel, res)
39 }
40
41 // ---------------------------------------------------------------------------
42
43 export {
44 isLocalVideoChannelNameExist,
45 isVideoChannelDescriptionValid,
46 isVideoChannelNameValid,
47 isVideoChannelSupportValid,
48 isVideoChannelExist
49 }
50
51 function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
52 if (!videoChannel) {
53 res.status(404)
54 .json({ error: 'Video channel not found' })
55 .end()
56
57 return false
58 }
59
60 res.locals.videoChannel = videoChannel
61 return true
62 }