From 27db78400c558e19bfac0da885fe0b7d0a3e6a0c Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Thu, 5 Aug 2021 13:54:35 +0200
Subject: Fix backend channel name validator consistency

---
 server/helpers/custom-validators/video-channels.ts     | 11 +++++++++--
 server/middlewares/validators/users.ts                 | 12 +++++++-----
 server/middlewares/validators/videos/video-channels.ts | 14 +++++++-------
 server/models/video/video-channel.ts                   |  4 ++--
 4 files changed, 25 insertions(+), 16 deletions(-)

(limited to 'server')

diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts
index ded5d5171..249083f39 100644
--- a/server/helpers/custom-validators/video-channels.ts
+++ b/server/helpers/custom-validators/video-channels.ts
@@ -1,14 +1,20 @@
 import validator from 'validator'
 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
 import { exists } from './misc'
+import { isUserUsernameValid } from './users'
 
 const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS
 
+function isVideoChannelUsernameValid (value: string) {
+  // Use the same constraints than user username
+  return isUserUsernameValid(value)
+}
+
 function isVideoChannelDescriptionValid (value: string) {
   return value === null || validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.DESCRIPTION)
 }
 
-function isVideoChannelNameValid (value: string) {
+function isVideoChannelDisplayNameValid (value: string) {
   return exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.NAME)
 }
 
@@ -19,7 +25,8 @@ function isVideoChannelSupportValid (value: string) {
 // ---------------------------------------------------------------------------
 
 export {
+  isVideoChannelUsernameValid,
   isVideoChannelDescriptionValid,
-  isVideoChannelNameValid,
+  isVideoChannelDisplayNameValid,
   isVideoChannelSupportValid
 }
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts
index 748b89f8f..bc8607523 100644
--- a/server/middlewares/validators/users.ts
+++ b/server/middlewares/validators/users.ts
@@ -6,7 +6,6 @@ import { MUserDefault } from '@server/types/models'
 import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
 import { UserRole } from '../../../shared/models/users'
 import { UserRegister } from '../../../shared/models/users/user-register.model'
-import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
 import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
 import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
 import {
@@ -28,7 +27,7 @@ import {
   isUserVideoQuotaValid,
   isUserVideosHistoryEnabledValid
 } from '../../helpers/custom-validators/users'
-import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
+import { isVideoChannelDisplayNameValid, isVideoChannelUsernameValid } from '../../helpers/custom-validators/video-channels'
 import { logger } from '../../helpers/logger'
 import { isThemeRegistered } from '../../lib/plugins/theme-utils'
 import { Redis } from '../../lib/redis'
@@ -56,9 +55,12 @@ const usersAddValidator = [
   body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
   body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'),
   body('email').isEmail().withMessage('Should have a valid email'),
-  body('channelName').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
+
+  body('channelName').optional().custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
+
   body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
   body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
+
   body('role')
     .customSanitizer(toIntOrNull)
     .custom(isUserRoleValid).withMessage('Should have a valid role'),
@@ -106,10 +108,10 @@ const usersRegisterValidator = [
 
   body('channel.name')
     .optional()
-    .custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
+    .custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
   body('channel.displayName')
     .optional()
-    .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
+    .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts
index ea10fe425..3b5693d23 100644
--- a/server/middlewares/validators/videos/video-channels.ts
+++ b/server/middlewares/validators/videos/video-channels.ts
@@ -4,12 +4,12 @@ import { VIDEO_CHANNELS } from '@server/initializers/constants'
 import { MChannelAccountDefault, MUser } from '@server/types/models'
 import { UserRight } from '../../../../shared'
 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
-import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
 import {
   isVideoChannelDescriptionValid,
-  isVideoChannelNameValid,
-  isVideoChannelSupportValid
+  isVideoChannelDisplayNameValid,
+  isVideoChannelSupportValid,
+  isVideoChannelUsernameValid
 } from '../../../helpers/custom-validators/video-channels'
 import { logger } from '../../../helpers/logger'
 import { ActorModel } from '../../../models/actor/actor'
@@ -17,8 +17,8 @@ import { VideoChannelModel } from '../../../models/video/video-channel'
 import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
 
 const videoChannelsAddValidator = [
-  body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
-  body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
+  body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
+  body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
 
@@ -50,7 +50,7 @@ const videoChannelsUpdateValidator = [
   param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
   body('displayName')
     .optional()
-    .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
+    .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
   body('description')
     .optional()
     .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
@@ -117,7 +117,7 @@ const videoChannelsNameWithHostValidator = [
 ]
 
 const localVideoChannelValidator = [
-  param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
+  param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'),
 
   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts
index 9f04a57c6..278149d60 100644
--- a/server/models/video/video-channel.ts
+++ b/server/models/video/video-channel.ts
@@ -23,7 +23,7 @@ import { ActivityPubActor } from '../../../shared/models/activitypub'
 import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
 import {
   isVideoChannelDescriptionValid,
-  isVideoChannelNameValid,
+  isVideoChannelDisplayNameValid,
   isVideoChannelSupportValid
 } from '../../helpers/custom-validators/video-channels'
 import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
@@ -308,7 +308,7 @@ export type SummaryOptions = {
 export class VideoChannelModel extends Model<Partial<AttributesOnly<VideoChannelModel>>> {
 
   @AllowNull(false)
-  @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
+  @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
   @Column
   name: string
 
-- 
cgit v1.2.3