1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { VIDEO_CHANNELS } from '@server/initializers/constants'
4 import { MChannelAccountDefault, MUser } from '@server/types/models'
5 import { UserRight } from '../../../../shared'
6 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
7 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
8 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
10 isVideoChannelDescriptionValid,
11 isVideoChannelNameValid,
12 isVideoChannelSupportValid
13 } from '../../../helpers/custom-validators/video-channels'
14 import { logger } from '../../../helpers/logger'
15 import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
16 import { ActorModel } from '../../../models/actor/actor'
17 import { VideoChannelModel } from '../../../models/video/video-channel'
18 import { areValidationErrors } from '../utils'
20 const videoChannelsAddValidator = [
21 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
22 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
23 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
24 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
29 if (areValidationErrors(req, res)) return
31 const actor = await ActorModel.loadLocalByName(req.body.name)
33 res.status(HttpStatusCode.CONFLICT_409)
34 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
39 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
40 if (count >= VIDEO_CHANNELS.MAX_PER_USER) {
41 res.status(HttpStatusCode.BAD_REQUEST_400)
42 .send({ error: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` })
51 const videoChannelsUpdateValidator = [
52 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
55 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
58 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
61 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
62 body('bulkVideosSupportUpdate')
64 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
66 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
67 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
69 if (areValidationErrors(req, res)) return
70 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
72 // We need to make additional checks
73 if (res.locals.videoChannel.Actor.isOwned() === false) {
74 return res.status(HttpStatusCode.FORBIDDEN_403)
75 .json({ error: 'Cannot update video channel of another server' })
78 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
79 return res.status(HttpStatusCode.FORBIDDEN_403)
80 .json({ error: 'Cannot update video channel of another user' })
87 const videoChannelsRemoveValidator = [
88 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
91 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
93 if (areValidationErrors(req, res)) return
94 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
96 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
97 if (!await checkVideoChannelIsNotTheLastOne(res)) return
103 const videoChannelsNameWithHostValidator = [
104 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
106 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
109 if (areValidationErrors(req, res)) return
111 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
117 const localVideoChannelValidator = [
118 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
120 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
121 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
123 if (areValidationErrors(req, res)) return
124 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
130 const videoChannelStatsValidator = [
133 .customSanitizer(toBooleanOrNull)
134 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
136 (req: express.Request, res: express.Response, next: express.NextFunction) => {
137 if (areValidationErrors(req, res)) return
142 // ---------------------------------------------------------------------------
145 videoChannelsAddValidator,
146 videoChannelsUpdateValidator,
147 videoChannelsRemoveValidator,
148 videoChannelsNameWithHostValidator,
149 localVideoChannelValidator,
150 videoChannelStatsValidator
153 // ---------------------------------------------------------------------------
155 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
156 if (videoChannel.Actor.isOwned() === false) {
157 res.status(HttpStatusCode.FORBIDDEN_403)
158 .json({ error: 'Cannot remove video channel of another server.' })
164 // Check if the user can delete the video channel
165 // The user can delete it if s/he is an admin
166 // Or if s/he is the video channel's account
167 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
168 res.status(HttpStatusCode.FORBIDDEN_403)
169 .json({ error: 'Cannot remove video channel of another user' })
178 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
179 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
182 res.status(HttpStatusCode.CONFLICT_409)
183 .json({ error: 'Cannot remove the last channel of this user' })