]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Cleanup useless express validator messages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc'
4 import { CONFIG } from '@server/initializers/config'
5 import { MChannelAccountDefault } from '@server/types/models'
6 import { VideosImportInChannelCreate } from '@shared/models'
7 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
8 import { isBooleanValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
9 import {
10 isVideoChannelDescriptionValid,
11 isVideoChannelDisplayNameValid,
12 isVideoChannelSupportValid,
13 isVideoChannelUsernameValid
14 } from '../../../helpers/custom-validators/video-channels'
15 import { logger } from '../../../helpers/logger'
16 import { ActorModel } from '../../../models/actor/actor'
17 import { VideoChannelModel } from '../../../models/video/video-channel'
18 import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
19 import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs'
20
21 export const videoChannelsAddValidator = [
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),
32
33 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
34 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
35
36 if (areValidationErrors(req, res)) return
37
38 const actor = await ActorModel.loadLocalByName(req.body.name)
39 if (actor) {
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 })
44 return false
45 }
46
47 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
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` })
50 return false
51 }
52
53 return next()
54 }
55 ]
56
57 export const videoChannelsUpdateValidator = [
58 param('nameWithHost')
59 .exists(),
60
61 body('displayName')
62 .optional()
63 .custom(isVideoChannelDisplayNameValid),
64 body('description')
65 .optional()
66 .custom(isVideoChannelDescriptionValid),
67 body('support')
68 .optional()
69 .custom(isVideoChannelSupportValid),
70 body('bulkVideosSupportUpdate')
71 .optional()
72 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
73
74 (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
76
77 if (areValidationErrors(req, res)) return
78
79 return next()
80 }
81 ]
82
83 export const videoChannelsRemoveValidator = [
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
86
87 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
88
89 return next()
90 }
91 ]
92
93 export const videoChannelsNameWithHostValidator = [
94 param('nameWithHost')
95 .exists(),
96
97 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
98 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
99
100 if (areValidationErrors(req, res)) return
101
102 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
103
104 return next()
105 }
106 ]
107
108 export const ensureIsLocalChannel = [
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 }
116
117 return next()
118 }
119 ]
120
121 export 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
132 export const videoChannelStatsValidator = [
133 query('withStats')
134 .optional()
135 .customSanitizer(toBooleanOrNull)
136 .custom(isBooleanValid).withMessage('Should have a valid stats flag boolean'),
137
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 if (areValidationErrors(req, res)) return
140 return next()
141 }
142 ]
143
144 export const videoChannelsListValidator = [
145 query('search')
146 .optional()
147 .not().isEmpty(),
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
158 export const videoChannelImportVideosValidator = [
159 body('externalChannelUrl')
160 .custom(isUrlValid),
161
162 body('videoChannelSyncId')
163 .optional()
164 .custom(isIdValid),
165
166 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
167 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
168
169 if (areValidationErrors(req, res)) return
170
171 const body: VideosImportInChannelCreate = req.body
172
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
180 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
181
182 return next()
183 }
184 ]
185
186 // ---------------------------------------------------------------------------
187
188 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
189 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
190
191 if (count <= 1) {
192 res.fail({
193 status: HttpStatusCode.CONFLICT_409,
194 message: 'Cannot remove the last channel of this user'
195 })
196 return false
197 }
198
199 return true
200 }