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 const videoChannelsListValidator = [
145 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
147 (req: express.Request, res: express.Response, next: express.NextFunction) => {
148 logger.debug('Checking video channels search query', { parameters: req.query })
150 if (areValidationErrors(req, res)) return
156 // ---------------------------------------------------------------------------
159 videoChannelsAddValidator,
160 videoChannelsUpdateValidator,
161 videoChannelsRemoveValidator,
162 videoChannelsNameWithHostValidator,
163 videoChannelsListValidator,
164 localVideoChannelValidator,
165 videoChannelStatsValidator
168 // ---------------------------------------------------------------------------
170 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
171 if (videoChannel.Actor.isOwned() === false) {
173 status: HttpStatusCode.FORBIDDEN_403,
174 message: 'Cannot remove video channel of another server.'
179 // Check if the user can delete the video channel
180 // The user can delete it if s/he is an admin
181 // Or if s/he is the video channel's account
182 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
184 status: HttpStatusCode.FORBIDDEN_403,
185 message: 'Cannot remove video channel of another user'
193 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
194 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
198 status: HttpStatusCode.CONFLICT_409,
199 message: 'Cannot remove the last channel of this user'