1 import * as express from 'express'
2 import { body, param } from 'express-validator/check'
3 import { UserRight } from '../../../../shared'
4 import { isAccountNameWithHostExist } from '../../../helpers/custom-validators/accounts'
6 isLocalVideoChannelNameExist,
7 isVideoChannelDescriptionValid,
8 isVideoChannelNameValid,
9 isVideoChannelNameWithHostExist,
10 isVideoChannelSupportValid
11 } from '../../../helpers/custom-validators/video-channels'
12 import { logger } from '../../../helpers/logger'
13 import { UserModel } from '../../../models/account/user'
14 import { VideoChannelModel } from '../../../models/video/video-channel'
15 import { areValidationErrors } from '../utils'
16 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
17 import { ActorModel } from '../../../models/activitypub/actor'
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.' })
42 const videoChannelsUpdateValidator = [
43 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
44 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
45 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
46 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
48 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
49 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
51 if (areValidationErrors(req, res)) return
52 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
54 // We need to make additional checks
55 if (res.locals.videoChannel.Actor.isOwned() === false) {
56 return res.status(403)
57 .json({ error: 'Cannot update video channel of another server' })
61 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
62 return res.status(403)
63 .json({ error: 'Cannot update video channel of another user' })
71 const videoChannelsRemoveValidator = [
72 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
74 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
75 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
77 if (areValidationErrors(req, res)) return
78 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
80 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
81 if (!await checkVideoChannelIsNotTheLastOne(res)) return
87 const videoChannelsNameWithHostValidator = [
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 videoChannelsNameWithHostValidator parameters', { parameters: req.params })
93 if (areValidationErrors(req, res)) return
95 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
101 const localVideoChannelValidator = [
102 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
107 if (areValidationErrors(req, res)) return
108 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
114 // ---------------------------------------------------------------------------
117 videoChannelsAddValidator,
118 videoChannelsUpdateValidator,
119 videoChannelsRemoveValidator,
120 videoChannelsNameWithHostValidator,
121 localVideoChannelValidator
124 // ---------------------------------------------------------------------------
126 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
127 if (videoChannel.Actor.isOwned() === false) {
129 .json({ error: 'Cannot remove video channel of another server.' })
135 // Check if the user can delete the video channel
136 // The user can delete it if s/he is an admin
137 // Or if s/he is the video channel's account
138 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
140 .json({ error: 'Cannot remove video channel of another user' })
149 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
150 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
154 .json({ error: 'Cannot remove the last channel of this user' })