]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-channels.ts
WIP plugins: move plugin CLI in peertube script
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-channels.ts
CommitLineData
72c7248b
C
1import * as express from 'express'
2import 'express-validator'
3import 'multer'
4e50b6a1 4import * as validator from 'validator'
74dc3bca 5import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
3fd3ab2d 6import { VideoChannelModel } from '../../models/video/video-channel'
4e50b6a1 7import { exists } from './misc'
72c7248b
C
8
9const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
10
11function isVideoChannelDescriptionValid (value: string) {
12 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
13}
14
15function isVideoChannelNameValid (value: string) {
16 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
17}
18
2422c46b
C
19function isVideoChannelSupportValid (value: string) {
20 return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
21}
22
0f6acda1 23async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
8a19bee1 24 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
06a05d5f
C
25
26 return processVideoChannelExist(videoChannel, res)
27}
28
57cfff78
C
29async function doesVideoChannelIdExist (id: number, res: express.Response) {
30 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
4e50b6a1 31
06a05d5f 32 return processVideoChannelExist(videoChannel, res)
4e50b6a1
C
33}
34
0f6acda1 35async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
92bf2f62 36 const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
8a19bee1
C
37
38 return processVideoChannelExist(videoChannel, res)
39}
40
72c7248b
C
41// ---------------------------------------------------------------------------
42
43export {
0f6acda1
C
44 doesVideoChannelNameWithHostExist,
45 doesLocalVideoChannelNameExist,
72c7248b 46 isVideoChannelDescriptionValid,
4e50b6a1 47 isVideoChannelNameValid,
2422c46b 48 isVideoChannelSupportValid,
0f6acda1 49 doesVideoChannelIdExist
72c7248b 50}
06a05d5f
C
51
52function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
53 if (!videoChannel) {
8d2be0ed
C
54 res.status(404)
55 .json({ error: 'Video channel not found' })
56 .end()
06a05d5f
C
57
58 return false
59 }
60
61 res.locals.videoChannel = videoChannel
62 return true
63}