]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-channels.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
CommitLineData
41fb13c3 1import express from 'express'
747c5628 2import { body, param, query } from 'express-validator'
2a491182 3import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
a37e9e74 4import { CONFIG } from '@server/initializers/config'
5import { MChannelAccountDefault } from '@server/types/models'
a3b472a1 6import { VideosImportInChannelCreate } from '@shared/models'
c0e8b12e 7import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
a3b472a1 8import { isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
4e50b6a1 9import {
7d8e778a 10 isVideoChannelDescriptionValid,
27db7840
C
11 isVideoChannelDisplayNameValid,
12 isVideoChannelSupportValid,
13 isVideoChannelUsernameValid
6e46de09
C
14} from '../../../helpers/custom-validators/video-channels'
15import { logger } from '../../../helpers/logger'
7d9ba5c0 16import { ActorModel } from '../../../models/actor/actor'
6e46de09 17import { VideoChannelModel } from '../../../models/video/video-channel'
2a491182 18import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
a3b472a1 19import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
72c7248b 20
2a491182 21export const videoChannelsAddValidator = [
396f6f01
C
22 body('name')
23 .custom(isVideoChannelUsernameValid),
24 body('displayName')
25 .custom(isVideoChannelDisplayNameValid),
26 body('description')
27 .optional()
28 .custom(isVideoChannelDescriptionValid),
29 body('support')
30 .optional()
31 .custom(isVideoChannelSupportValid),
72c7248b 32
601527d7 33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
35
a2431b7d
C
36 if (areValidationErrors(req, res)) return
37
601527d7
C
38 const actor = await ActorModel.loadLocalByName(req.body.name)
39 if (actor) {
76148b27
RK
40 res.fail({
41 status: HttpStatusCode.CONFLICT_409,
42 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
43 })
601527d7
C
44 return false
45 }
46
a3ce4ae8 47 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
754b6f5f
FC
48 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
49 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
a3ce4ae8
C
50 return false
51 }
52
a2431b7d 53 return next()
72c7248b
C
54 }
55]
56
2a491182 57export const videoChannelsUpdateValidator = [
396f6f01
C
58 param('nameWithHost')
59 .exists(),
60
7d14d4d2
C
61 body('displayName')
62 .optional()
396f6f01 63 .custom(isVideoChannelDisplayNameValid),
7d14d4d2
C
64 body('description')
65 .optional()
396f6f01 66 .custom(isVideoChannelDescriptionValid),
7d14d4d2
C
67 body('support')
68 .optional()
396f6f01 69 .custom(isVideoChannelSupportValid),
7d14d4d2
C
70 body('bulkVideosSupportUpdate')
71 .optional()
72 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
72c7248b 73
906f46d0 74 (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
75 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
76
a2431b7d 77 if (areValidationErrors(req, res)) return
a2431b7d
C
78
79 return next()
72c7248b
C
80 }
81]
82
2a491182 83export const videoChannelsRemoveValidator = [
a2431b7d 84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
85 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
86
a37e9e74 87 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
a2431b7d
C
88
89 return next()
72c7248b
C
90 }
91]
92
2a491182 93export const videoChannelsNameWithHostValidator = [
396f6f01
C
94 param('nameWithHost')
95 .exists(),
72c7248b 96
a2431b7d 97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 98 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 99
a2431b7d 100 if (areValidationErrors(req, res)) return
6b738c7a 101
0f6acda1 102 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
103
104 return next()
72c7248b
C
105 }
106]
107
2a491182 108export const ensureIsLocalChannel = [
a37e9e74 109 (req: express.Request, res: express.Response, next: express.NextFunction) => {
110 if (res.locals.videoChannel.Actor.isOwned() === false) {
111 return res.fail({
112 status: HttpStatusCode.FORBIDDEN_403,
113 message: 'This channel is not owned.'
114 })
115 }
06a05d5f
C
116
117 return next()
118 }
119]
120
2a491182
F
121export const ensureChannelOwnerCanUpload = [
122 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 const channel = res.locals.videoChannel
124 const user = { id: channel.Account.userId }
125
126 if (!await checkUserQuota(user, 1, res)) return
127
128 next()
129 }
130]
131
132export const videoChannelStatsValidator = [
5a61ffbb
C
133 query('withStats')
134 .optional()
135 .customSanitizer(toBooleanOrNull)
396f6f01 136 .custom(isBooleanValid).withMessage('Should have a valid stats flag boolean'),
747c5628
RK
137
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 if (areValidationErrors(req, res)) return
140 return next()
141 }
142]
143
2a491182 144export const videoChannelsListValidator = [
396f6f01
C
145 query('search')
146 .optional()
147 .not().isEmpty(),
37a44fc9
C
148
149 (req: express.Request, res: express.Response, next: express.NextFunction) => {
150 logger.debug('Checking video channels search query', { parameters: req.query })
151
152 if (areValidationErrors(req, res)) return
153
154 return next()
155 }
156]
157
2a491182 158export const videoChannelImportVideosValidator = [
396f6f01
C
159 body('externalChannelUrl')
160 .custom(isUrlValid),
72c7248b 161
a3b472a1
C
162 body('videoChannelSyncId')
163 .optional()
396f6f01 164 .custom(isIdValid),
a3b472a1
C
165
166 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2a491182
F
167 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
168
169 if (areValidationErrors(req, res)) return
170
a3b472a1
C
171 const body: VideosImportInChannelCreate = req.body
172
2a491182
F
173 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
174 return res.fail({
175 status: HttpStatusCode.FORBIDDEN_403,
176 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
177 })
178 }
179
a3b472a1
C
180 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
181
2a491182
F
182 return next()
183 }
184]
72c7248b
C
185
186// ---------------------------------------------------------------------------
187
a37e9e74 188async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
189 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
a2431b7d
C
190
191 if (count <= 1) {
76148b27
RK
192 res.fail({
193 status: HttpStatusCode.CONFLICT_409,
194 message: 'Cannot remove the last channel of this user'
195 })
a2431b7d
C
196 return false
197 }
198
199 return true
72c7248b 200}