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 { ActorModel } from '../../../models/actor/actor'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
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 status: HttpStatusCode.CONFLICT_409,
34 message: '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.fail({ message: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` })
49 const videoChannelsUpdateValidator = [
50 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
53 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
56 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
59 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
60 body('bulkVideosSupportUpdate')
62 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
64 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
67 if (areValidationErrors(req, res)) return
68 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
70 // We need to make additional checks
71 if (res.locals.videoChannel.Actor.isOwned() === false) {
73 status: HttpStatusCode.FORBIDDEN_403,
74 message: 'Cannot update video channel of another server'
78 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
80 status: HttpStatusCode.FORBIDDEN_403,
81 message: 'Cannot update video channel of another user'
89 const videoChannelsRemoveValidator = [
90 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
95 if (areValidationErrors(req, res)) return
96 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
98 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
99 if (!await checkVideoChannelIsNotTheLastOne(res)) return
105 const videoChannelsNameWithHostValidator = [
106 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
111 if (areValidationErrors(req, res)) return
113 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
119 const localVideoChannelValidator = [
120 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
122 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
125 if (areValidationErrors(req, res)) return
126 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
132 const videoChannelStatsValidator = [
135 .customSanitizer(toBooleanOrNull)
136 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 if (areValidationErrors(req, res)) return
144 // ---------------------------------------------------------------------------
147 videoChannelsAddValidator,
148 videoChannelsUpdateValidator,
149 videoChannelsRemoveValidator,
150 videoChannelsNameWithHostValidator,
151 localVideoChannelValidator,
152 videoChannelStatsValidator
155 // ---------------------------------------------------------------------------
157 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
158 if (videoChannel.Actor.isOwned() === false) {
160 status: HttpStatusCode.FORBIDDEN_403,
161 message: 'Cannot remove video channel of another server.'
166 // Check if the user can delete the video channel
167 // The user can delete it if s/he is an admin
168 // Or if s/he is the video channel's account
169 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
171 status: HttpStatusCode.FORBIDDEN_403,
172 message: 'Cannot remove video channel of another user'
180 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
181 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
185 status: HttpStatusCode.CONFLICT_409,
186 message: 'Cannot remove the last channel of this user'