1 import * as express from 'express'
2 import { body, param } from 'express-validator'
3 import { UserRight } from '../../../../shared'
5 isVideoChannelDescriptionValid,
6 isVideoChannelNameValid,
7 isVideoChannelSupportValid
8 } from '../../../helpers/custom-validators/video-channels'
9 import { logger } from '../../../helpers/logger'
10 import { VideoChannelModel } from '../../../models/video/video-channel'
11 import { areValidationErrors } from '../utils'
12 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
13 import { ActorModel } from '../../../models/activitypub/actor'
14 import { isBooleanValid } from '../../../helpers/custom-validators/misc'
15 import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
16 import { MChannelAccountDefault, MUser } from '@server/typings/models'
17 import { VIDEO_CHANNELS } from '@server/initializers/constants'
19 const videoChannelsAddValidator = [
20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelNameValid).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'),
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
28 if (areValidationErrors(req, res)) return
30 const actor = await ActorModel.loadLocalByName(req.body.name)
33 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
38 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
39 if (count >= VIDEO_CHANNELS.MAX_PER_USER) {
41 .send({ error: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` })
50 const videoChannelsUpdateValidator = [
51 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
54 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
57 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
60 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
61 body('bulkVideosSupportUpdate')
63 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
66 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
68 if (areValidationErrors(req, res)) return
69 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
71 // We need to make additional checks
72 if (res.locals.videoChannel.Actor.isOwned() === false) {
73 return res.status(403)
74 .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(403)
80 .json({ error: 'Cannot update video channel of another user' })
88 const videoChannelsRemoveValidator = [
89 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
94 if (areValidationErrors(req, res)) return
95 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
97 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
98 if (!await checkVideoChannelIsNotTheLastOne(res)) return
104 const videoChannelsNameWithHostValidator = [
105 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
108 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
110 if (areValidationErrors(req, res)) return
112 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
118 const localVideoChannelValidator = [
119 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
121 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
124 if (areValidationErrors(req, res)) return
125 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
131 // ---------------------------------------------------------------------------
134 videoChannelsAddValidator,
135 videoChannelsUpdateValidator,
136 videoChannelsRemoveValidator,
137 videoChannelsNameWithHostValidator,
138 localVideoChannelValidator
141 // ---------------------------------------------------------------------------
143 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
144 if (videoChannel.Actor.isOwned() === false) {
146 .json({ error: 'Cannot remove video channel of another server.' })
152 // Check if the user can delete the video channel
153 // The user can delete it if s/he is an admin
154 // Or if s/he is the video channel's account
155 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
157 .json({ error: 'Cannot remove video channel of another user' })
166 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
167 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
171 .json({ error: 'Cannot remove the last channel of this user' })