]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Add ability to list comments on local videos
[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').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
23 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
24 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
25 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
26
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
29
30 if (areValidationErrors(req, res)) return
31
32 const actor = await ActorModel.loadLocalByName(req.body.name)
33 if (actor) {
34 res.fail({
35 status: HttpStatusCode.CONFLICT_409,
36 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
37 })
38 return false
39 }
40
41 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
42 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
43 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
44 return false
45 }
46
47 return next()
48 }
49 ]
50
51 export const videoChannelsUpdateValidator = [
52 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
53 body('displayName')
54 .optional()
55 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
56 body('description')
57 .optional()
58 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
59 body('support')
60 .optional()
61 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
62 body('bulkVideosSupportUpdate')
63 .optional()
64 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
65
66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
68
69 if (areValidationErrors(req, res)) return
70
71 return next()
72 }
73 ]
74
75 export const videoChannelsRemoveValidator = [
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
78
79 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
80
81 return next()
82 }
83 ]
84
85 export const videoChannelsNameWithHostValidator = [
86 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
87
88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
89 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
90
91 if (areValidationErrors(req, res)) return
92
93 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
94
95 return next()
96 }
97 ]
98
99 export const ensureIsLocalChannel = [
100 (req: express.Request, res: express.Response, next: express.NextFunction) => {
101 if (res.locals.videoChannel.Actor.isOwned() === false) {
102 return res.fail({
103 status: HttpStatusCode.FORBIDDEN_403,
104 message: 'This channel is not owned.'
105 })
106 }
107
108 return next()
109 }
110 ]
111
112 export const ensureChannelOwnerCanUpload = [
113 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 const channel = res.locals.videoChannel
115 const user = { id: channel.Account.userId }
116
117 if (!await checkUserQuota(user, 1, res)) return
118
119 next()
120 }
121 ]
122
123 export const videoChannelStatsValidator = [
124 query('withStats')
125 .optional()
126 .customSanitizer(toBooleanOrNull)
127 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
128
129 (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 if (areValidationErrors(req, res)) return
131 return next()
132 }
133 ]
134
135 export const videoChannelsListValidator = [
136 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
137
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 logger.debug('Checking video channels search query', { parameters: req.query })
140
141 if (areValidationErrors(req, res)) return
142
143 return next()
144 }
145 ]
146
147 export const videoChannelImportVideosValidator = [
148 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
149
150 body('videoChannelSyncId')
151 .optional()
152 .custom(isIdValid).withMessage('Should have a valid channel sync id'),
153
154 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
155 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
156
157 if (areValidationErrors(req, res)) return
158
159 const body: VideosImportInChannelCreate = req.body
160
161 if (!CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
162 return res.fail({
163 status: HttpStatusCode.FORBIDDEN_403,
164 message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
165 })
166 }
167
168 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
169
170 return next()
171 }
172 ]
173
174 // ---------------------------------------------------------------------------
175
176 async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
177 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
178
179 if (count <= 1) {
180 res.fail({
181 status: HttpStatusCode.CONFLICT_409,
182 message: 'Cannot remove the last channel of this user'
183 })
184 return false
185 }
186
187 return true
188 }