]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
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 = [
27db7840
C
22 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
23 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
2422c46b
C
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'),
72c7248b 26
601527d7 27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
28 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
29
a2431b7d
C
30 if (areValidationErrors(req, res)) return
31
601527d7
C
32 const actor = await ActorModel.loadLocalByName(req.body.name)
33 if (actor) {
76148b27
RK
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 })
601527d7
C
38 return false
39 }
40
a3ce4ae8 41 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
754b6f5f
FC
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` })
a3ce4ae8
C
44 return false
45 }
46
a2431b7d 47 return next()
72c7248b
C
48 }
49]
50
2a491182 51export const videoChannelsUpdateValidator = [
8a19bee1 52 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
7d14d4d2
C
53 body('displayName')
54 .optional()
27db7840 55 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
7d14d4d2
C
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'),
72c7248b 65
906f46d0 66 (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
67 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
68
a2431b7d 69 if (areValidationErrors(req, res)) return
a2431b7d
C
70
71 return next()
72c7248b
C
72 }
73]
74
2a491182 75export const videoChannelsRemoveValidator = [
a2431b7d 76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
77 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
78
a37e9e74 79 if (!await checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)) return
a2431b7d
C
80
81 return next()
72c7248b
C
82 }
83]
84
2a491182 85export const videoChannelsNameWithHostValidator = [
8a19bee1 86 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 87
a2431b7d 88 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 89 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 90
a2431b7d 91 if (areValidationErrors(req, res)) return
6b738c7a 92
0f6acda1 93 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
94
95 return next()
72c7248b
C
96 }
97]
98
2a491182 99export const ensureIsLocalChannel = [
a37e9e74 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 }
06a05d5f
C
107
108 return next()
109 }
110]
111
2a491182
F
112export 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
123export const videoChannelStatsValidator = [
5a61ffbb
C
124 query('withStats')
125 .optional()
126 .customSanitizer(toBooleanOrNull)
127 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
747c5628
RK
128
129 (req: express.Request, res: express.Response, next: express.NextFunction) => {
130 if (areValidationErrors(req, res)) return
131 return next()
132 }
133]
134
2a491182 135export const videoChannelsListValidator = [
37a44fc9
C
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
2a491182
F
147export const videoChannelImportVideosValidator = [
148 body('externalChannelUrl').custom(isUrlValid).withMessage('Should have a valid channel url'),
72c7248b 149
a3b472a1
C
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) => {
2a491182
F
155 logger.debug('Checking videoChannelImport parameters', { parameters: req.body })
156
157 if (areValidationErrors(req, res)) return
158
a3b472a1
C
159 const body: VideosImportInChannelCreate = req.body
160
2a491182
F
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
a3b472a1
C
168 if (body.videoChannelSyncId && !await doesVideoChannelSyncIdExist(body.videoChannelSyncId, res)) return
169
2a491182
F
170 return next()
171 }
172]
72c7248b
C
173
174// ---------------------------------------------------------------------------
175
a37e9e74 176async function checkVideoChannelIsNotTheLastOne (videoChannel: MChannelAccountDefault, res: express.Response) {
177 const count = await VideoChannelModel.countByAccount(videoChannel.Account.id)
a2431b7d
C
178
179 if (count <= 1) {
76148b27
RK
180 res.fail({
181 status: HttpStatusCode.CONFLICT_409,
182 message: 'Cannot remove the last channel of this user'
183 })
a2431b7d
C
184 return false
185 }
186
187 return true
72c7248b 188}