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