]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/video-channels.ts
Fix trending days display on first load
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-channels.ts
... / ...
CommitLineData
1import * as express from 'express'
2import 'express-validator'
3import 'multer'
4import * as validator from 'validator'
5import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
6import { VideoChannelModel } from '../../models/video/video-channel'
7import { exists } from './misc'
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
19function isVideoChannelSupportValid (value: string) {
20 return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT))
21}
22
23async function isLocalVideoChannelNameExist (name: string, res: express.Response) {
24 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
25
26 return processVideoChannelExist(videoChannel, res)
27}
28
29async function isVideoChannelIdExist (id: string, res: express.Response) {
30 let videoChannel: VideoChannelModel
31 if (validator.isInt(id)) {
32 videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
33 } else { // UUID
34 videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id)
35 }
36
37 return processVideoChannelExist(videoChannel, res)
38}
39
40async function isVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
41 const [ name, host ] = nameWithDomain.split('@')
42 let videoChannel: VideoChannelModel
43
44 if (!host || host === CONFIG.WEBSERVER.HOST) videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
45 else videoChannel = await VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
46
47 return processVideoChannelExist(videoChannel, res)
48}
49
50// ---------------------------------------------------------------------------
51
52export {
53 isVideoChannelNameWithHostExist,
54 isLocalVideoChannelNameExist,
55 isVideoChannelDescriptionValid,
56 isVideoChannelNameValid,
57 isVideoChannelSupportValid,
58 isVideoChannelIdExist
59}
60
61function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
62 if (!videoChannel) {
63 res.status(404)
64 .json({ error: 'Video channel not found' })
65 .end()
66
67 return false
68 }
69
70 res.locals.videoChannel = videoChannel
71 return true
72}