]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/video-channels.ts
feature: initial syndication feeds support
[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
9 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
10
11 function isVideoChannelDescriptionValid (value: string) {
12 return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
13 }
14
15 function isVideoChannelNameValid (value: string) {
16 return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
17 }
18
19 function isVideoChannelSupportValid (value: string) {
20 return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
21 }
22
23 async function isVideoChannelExist (id: string, res: express.Response) {
24 let videoChannel: VideoChannelModel
25 if (validator.isInt(id)) {
26 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
27 } else { // UUID
28 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id)
29 }
30
31 if (!videoChannel) {
32 res.status(404)
33 .json({ error: 'Video channel not found' })
34 .end()
35
36 return false
37 }
38
39 res.locals.videoChannel = videoChannel
40 return true
41 }
42
43 // ---------------------------------------------------------------------------
44
45 export {
46 isVideoChannelDescriptionValid,
47 isVideoChannelNameValid,
48 isVideoChannelSupportValid,
49 isVideoChannelExist
50 }