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 listVideoAccountChannelsValidator = [
20 param('accountName').exists().withMessage('Should have a valid account name'),
22 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
23 logger.debug('Checking listVideoAccountChannelsValidator parameters', { parameters: req.body })
25 if (areValidationErrors(req, res)) return
26 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
32 const videoChannelsAddValidator = [
33 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
34 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
35 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
36 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
38 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
39 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
41 if (areValidationErrors(req, res)) return
43 const actor = await ActorModel.loadLocalByName(req.body.name)
46 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
55 const videoChannelsUpdateValidator = [
56 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
57 body('displayName').optional().custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
58 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
59 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
61 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
64 if (areValidationErrors(req, res)) return
65 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
67 // We need to make additional checks
68 if (res.locals.videoChannel.Actor.isOwned() === false) {
69 return res.status(403)
70 .json({ error: 'Cannot update video channel of another server' })
74 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
75 return res.status(403)
76 .json({ error: 'Cannot update video channel of another user' })
84 const videoChannelsRemoveValidator = [
85 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
87 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
88 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
90 if (areValidationErrors(req, res)) return
91 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
93 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
94 if (!await checkVideoChannelIsNotTheLastOne(res)) return
100 const videoChannelsNameWithHostValidator = [
101 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
103 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
104 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
106 if (areValidationErrors(req, res)) return
108 if (!await isVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
114 const localVideoChannelValidator = [
115 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
117 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
118 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
120 if (areValidationErrors(req, res)) return
121 if (!await isLocalVideoChannelNameExist(req.params.name, res)) return
127 // ---------------------------------------------------------------------------
130 listVideoAccountChannelsValidator,
131 videoChannelsAddValidator,
132 videoChannelsUpdateValidator,
133 videoChannelsRemoveValidator,
134 videoChannelsNameWithHostValidator,
135 localVideoChannelValidator
138 // ---------------------------------------------------------------------------
140 function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) {
141 if (videoChannel.Actor.isOwned() === false) {
143 .json({ error: 'Cannot remove video channel of another server.' })
149 // Check if the user can delete the video channel
150 // The user can delete it if s/he is an admin
151 // Or if s/he is the video channel's account
152 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
154 .json({ error: 'Cannot remove video channel of another user' })
163 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
164 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
168 .json({ error: 'Cannot remove the last channel of this user' })