]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-channels.ts
Handle theater mode for playlists
[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'
8a19bee1 5import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
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
8a19bee1
C
23async function isLocalVideoChannelNameExist (name: string, res: express.Response) {
24 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
06a05d5f
C
25
26 return processVideoChannelExist(videoChannel, res)
27}
28
07b1a18a 29async function isVideoChannelIdExist (id: number | string, res: express.Response) {
3fd3ab2d 30 let videoChannel: VideoChannelModel
07b1a18a 31 if (validator.isInt('' + id)) {
3fd3ab2d 32 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
4e50b6a1 33 } else { // UUID
07b1a18a 34 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount('' + id)
4e50b6a1
C
35 }
36
06a05d5f 37 return processVideoChannelExist(videoChannel, res)
4e50b6a1
C
38}
39
8a19bee1 40async function isVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
92bf2f62 41 const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
8a19bee1
C
42
43 return processVideoChannelExist(videoChannel, res)
44}
45
72c7248b
C
46// ---------------------------------------------------------------------------
47
48export {
8a19bee1 49 isVideoChannelNameWithHostExist,
06a05d5f 50 isLocalVideoChannelNameExist,
72c7248b 51 isVideoChannelDescriptionValid,
4e50b6a1 52 isVideoChannelNameValid,
2422c46b 53 isVideoChannelSupportValid,
8a19bee1 54 isVideoChannelIdExist
72c7248b 55}
06a05d5f
C
56
57function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
58 if (!videoChannel) {
59 res.status(404)
60 .json({ error: 'Video channel not found' })
61 .end()
62
63 return false
64 }
65
66 res.locals.videoChannel = videoChannel
67 return true
68}