]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Channel sync (#5135)
[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 { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
7 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
8 import {
9 isVideoChannelDescriptionValid,
10 isVideoChannelDisplayNameValid,
11 isVideoChannelSupportValid,
12 isVideoChannelUsernameValid
13 } from '../../../helpers/custom-validators/video-channels'
14 import { logger } from '../../../helpers/logger'
15 import { ActorModel } from '../../../models/actor/actor'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { areValidationErrors, checkUserQuota, doesVideoChannelNameWithHostExist } from '../shared'
18
19 export const videoChannelsAddValidator = [
20 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
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'),
24
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
28 if (areValidationErrors(req, res)) return
29
30 const actor = await ActorModel.loadLocalByName(req.body.name)
31 if (actor) {
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 })
36 return false
37 }
38
39 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
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` })
42 return false
43 }
44
45 return next()
46 }
47 ]
48
49 export const videoChannelsUpdateValidator = [
50 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
51 body('displayName')
52 .optional()
53 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
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'),
63
64 (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
66
67 if (areValidationErrors(req, res)) return
68
69 return next()
70 }
71 ]
72
73 export const videoChannelsRemoveValidator = [
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
76
77 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
78
79 return next()
80 }
81 ]
82
83 export const videoChannelsNameWithHostValidator = [
84 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
85
86 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
87 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
88
89 if (areValidationErrors(req, res)) return
90
91 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
92
93 return next()
94 }
95 ]
96
97 export const ensureIsLocalChannel = [
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 }
105
106 return next()
107 }
108 ]
109
110 export 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
121 export const videoChannelStatsValidator = [
122 query('withStats')
123 .optional()
124 .customSanitizer(toBooleanOrNull)
125 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
126
127 (req: express.Request, res: express.Response, next: express.NextFunction) => {
128 if (areValidationErrors(req, res)) return
129 return next()
130 }
131 ]
132
133 export const videoChannelsListValidator = [
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
145 export const videoChannelImportVideosValidator = [
146 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
147
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 ]
163
164 // ---------------------------------------------------------------------------
165
166 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
167 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
168
169 if (count <= 1) {
170 res.fail({
171 status: HttpStatusCode.CONFLICT_409,
172 message: 'Cannot remove the last channel of this user'
173 })
174 return false
175 }
176
177 return true
178 }