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